别再折腾了!Windows 10/11下EDK2+VS2019+Python 3.11一键式环境搭建保姆级教程
Windows 10/11下EDK2开发环境一键式搭建终极指南每次打开EDK2的官方文档看到那密密麻麻的依赖项和复杂的配置步骤是不是感觉头都大了作为一名曾经被EDK2环境搭建折磨到怀疑人生的开发者我完全理解那种面对十几个安装包、无数环境变量时的绝望感。但今天我要告诉你一个好消息其实只需要一个脚本和30分钟就能搞定所有环境配置。1. 环境准备从零开始的自动化方案1.1 硬件与系统要求检查在开始之前我们需要确保你的Windows系统满足基本要求操作系统Windows 10 20H2或更高版本Windows 11所有版本磁盘空间至少20GB可用空间建议SSD内存8GB及以上16GB更佳网络连接稳定的互联网连接以下载必要组件注意避免使用包含中文或特殊字符的用户名和安装路径这可能导致后续编译失败。1.2 一键式环境检查脚本我开发了一个PowerShell脚本可以自动检测系统环境是否满足要求# 保存为Check-Env.ps1并右键使用PowerShell运行 $errors () # 检查Windows版本 if ((Get-ComputerInfo).WindowsVersion -lt 10.0.19042) { $errors 需要Windows 10 20H2或更高版本 } # 检查磁盘空间 $disk Get-PSDrive C | Select-Object -ExpandProperty Free if ($disk -lt 20GB) { $errors C盘需要至少20GB可用空间 } # 检查内存 $memory (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum).Sum / 1GB if ($memory -lt 8) { $errors 建议至少8GB内存 } if ($errors.Count -eq 0) { Write-Host ✅ 系统环境检查通过 -ForegroundColor Green } else { Write-Host ❌ 存在以下问题 -ForegroundColor Red $errors | ForEach-Object { Write-Host - $_ } }2. 依赖安装全自动解决方案2.1 必备组件自动安装脚本传统方式需要手动下载安装十几个软件包现在我们可以用这个一体化脚本# 保存为Install-Dependencies.ps1 $ProgressPreference SilentlyContinue # 1. 安装Visual Studio 2019 Build Tools Write-Host 正在安装VS2019 Build Tools... -ForegroundColor Cyan $vsInstaller https://aka.ms/vs/16/release/vs_buildtools.exe Invoke-WebRequest -Uri $vsInstaller -OutFile $env:TEMP\vs_buildtools.exe Start-Process -Wait -FilePath $env:TEMP\vs_buildtools.exe -ArgumentList --quiet --wait --norestart --nocache --add Microsoft.VisualStudio.Workload.VCTools --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 --add Microsoft.VisualStudio.Component.Windows10SDK.19041 # 2. 安装Python 3.11 Write-Host 正在安装Python 3.11... -ForegroundColor Cyan $pythonUrl https://www.python.org/ftp/python/3.11.4/python-3.11.4-amd64.exe Invoke-WebRequest -Uri $pythonUrl -OutFile $env:TEMP\python-installer.exe Start-Process -Wait -FilePath $env:TEMP\python-installer.exe -ArgumentList /quiet InstallAllUsers1 PrependPath1 Include_launcher1 # 3. 安装其他工具链 $tools { NASM https://www.nasm.us/pub/nasm/releasebuilds/2.16.01/win64/nasm-2.16.01-win64.zip LLVM https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.4/LLVM-16.0.4-win64.exe OpenSSL https://slproweb.com/download/Win64OpenSSL-3_1_1.exe IASL https://acpica.org/sites/acpica/files/iasl-win-20230628.zip } foreach ($tool in $tools.GetEnumerator()) { Write-Host 正在安装$($tool.Key)... -ForegroundColor Cyan $outFile $env:TEMP\$($tool.Key.ToLower()).exe Invoke-WebRequest -Uri $tool.Value -OutFile $outFile if ($tool.Key -eq NASM -or $tool.Key -eq IASL) { Expand-Archive -Path $outFile -DestinationPath C:\$($tool.Key) -Force } else { Start-Process -Wait -FilePath $outFile -ArgumentList /SILENT /NORESTART } }2.2 环境变量自动配置运行以下脚本自动设置所有必要的环境变量# 保存为Set-EnvVars.ps1 [System.Environment]::SetEnvironmentVariable(NASM_PREFIX, C:\NASM, Machine) [System.Environment]::SetEnvironmentVariable(PYTHON_HOME, C:\Python311, Machine) [System.Environment]::SetEnvironmentVariable(IASL_PREFIX, C:\IASL, Machine) $path [System.Environment]::GetEnvironmentVariable(PATH, Machine) $newPath ( C:\NASM, C:\Python311, C:\Python311\Scripts, C:\IASL, C:\Program Files\LLVM\bin, C:\Program Files\OpenSSL-Win64\bin ) -join ; ; $path [System.Environment]::SetEnvironmentVariable(PATH, $newPath, Machine) Write-Host ✅ 环境变量设置完成请重新启动终端使更改生效 -ForegroundColor Green3. EDK2源码获取与初始化3.1 一键克隆EDK2及其子模块传统git submodule方式经常因为网络问题失败这里提供一个更可靠的方法# 保存为Clone-EDK2.ps1 function Clone-WithRetry { param([string]$url, [string]$path, [int]$retry 3) for ($i 1; $i -le $retry; $i) { try { if (Test-Path $path) { Remove-Item $path -Recurse -Force } Write-Host 尝试克隆 $url (第$i次)... -ForegroundColor Yellow git clone --depth 1 $url $path if ($LASTEXITCODE -eq 0) { return $true } } catch { Start-Sleep -Seconds (5 * $i) } } return $false } $edk2Path C:\edk2 Clone-WithRetry https://github.com/tianocore/edk2.git $edk2Path # 关键子模块手动克隆 $submodules ( { Path$edk2Path\BaseTools\Source\C\BrotliCompress\brotli; Urlhttps://github.com/google/brotli.git }, { Path$edk2Path\MdePkg\Library\MipiSysTLib\mipisyst; Urlhttps://github.com/MIPI-Alliance/public-mipi-sys-t.git }, { Path$edk2Path\SecurityPkg\DeviceSecurity\SpdmLib\libspdm; Urlhttps://github.com/DMTF/libspdm.git } ) foreach ($sub in $submodules) { if (-not (Clone-WithRetry $sub.Url $sub.Path)) { Write-Host ❌ 克隆 $($sub.Url) 失败 -ForegroundColor Red exit 1 } } Write-Host ✅ EDK2源码及子模块克隆完成 -ForegroundColor Green3.2 初始化编译环境使用这个优化过的初始化脚本# 保存为Init-EDK2.ps1 $edk2Path C:\edk2 # 设置工作目录 Set-Location $edk2Path # 初始化环境 $edk2Path\edksetup.bat # 配置目标平台 $targetContent ACTIVE_PLATFORM EmulatorPkg/EmulatorPkg.dsc TARGET DEBUG TARGET_ARCH X64 TOOL_CHAIN_TAG VS2019 MAX_CONCURRENT_THREAD_NUMBER $([System.Environment]::ProcessorCount) Set-Content -Path $edk2Path\Conf\target.txt -Value $targetContent Write-Host ✅ EDK2环境初始化完成 -ForegroundColor Green4. 编译与验证4.1 一键编译脚本# 保存为Build-EDK2.ps1 $edk2Path C:\edk2 # 启动VS2019开发人员命令提示符 $vsDevCmd ${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\VsDevCmd.bat $buildCmd call $vsDevCmd cd /D $edk2Path call edksetup.bat Rebuild build $tempBat [System.IO.Path]::GetTempFileName() .bat Set-Content -Path $tempBat -Value $buildCmd Start-Process -Wait -FilePath cmd.exe -ArgumentList /c $tempBat if ($LASTEXITCODE -eq 0) { Write-Host ✅ 编译成功完成 -ForegroundColor Green } else { Write-Host ❌ 编译失败错误码: $LASTEXITCODE -ForegroundColor Red }4.2 常见问题自动修复如果编译失败可以尝试这个自动修复脚本# 保存为Fix-CommonIssues.ps1 $issuesFixed $false # 检查Python路径 if (-not (Test-Path C:\Python311\python.exe)) { Write-Host 修复Python路径问题... -ForegroundColor Yellow [System.Environment]::SetEnvironmentVariable(PYTHON_HOME, C:\Python311, Machine) $issuesFixed $true } # 检查BaseTools if (-not (Test-Path C:\edk2\BaseTools\Source\Python\build.exe)) { Write-Host 重建BaseTools... -ForegroundColor Yellow Set-Location C:\edk2\BaseTools C:\Python311\python.exe setup.py build $issuesFixed $true } # 检查环境变量 $requiredVars (NASM_PREFIX, PYTHON_HOME, IASL_PREFIX) foreach ($var in $requiredVars) { if ([string]::IsNullOrEmpty([System.Environment]::GetEnvironmentVariable($var, Machine))) { Write-Host 修复缺失的环境变量 $var... -ForegroundColor Yellow C:\edk2\Set-EnvVars.ps1 $issuesFixed $true break } } if (-not $issuesFixed) { Write-Host 未检测到常见问题请检查具体错误信息 -ForegroundColor Cyan } else { Write-Host ✅ 已尝试自动修复请重新尝试编译 -ForegroundColor Green }5. 高级配置与优化5.1 并行编译加速修改Conf/tools_def.txt中的以下参数可以显著提升编译速度# 在X64部分添加 *_*_*_OBJCOPY_ADDDEBUGFLAG --add-gnu-debuglink$(DEBUG_DIR)/$(MODULE_NAME).debug *_*_*_OBJCOPY_STRIPFLAG --strip-unneeded -R .eh_frame # 调整线程数 (根据CPU核心数) *_*_*_MAKE_FLAGS /nologo /NUMBER_OF_PROCESSORS$([System.Environment]::ProcessorCount)5.2 缓存优化配置在Conf/build_rule.txt中添加以下内容可以启用缓存加速[BuildOptions] # 启用缓存 *_*_*_CC_FLAGS /D DISABLE_NEW_DEPRECATED_INTERFACES /MP /Zc:inline /Gw /Gy /Zi /FS /Zc:wchar_t /Oi /Oy- # 链接时优化 *_*_*_DLINK_FLAGS /OPT:REF /OPT:ICF10 /INCREMENTAL:NO5.3 自定义平台开发创建一个简单的UEFI应用模板# 在edk2目录下 mkdir -p MyPkg/MyApplication cd MyPkg/MyApplication # 创建基本文件结构 cat MyApplication.inf EOF [Defines] INF_VERSION 0x00010005 BASE_NAME MyApplication FILE_GUID 12345678-1234-5678-1234-567812345678 MODULE_TYPE UEFI_APPLICATION VERSION_STRING 1.0 ENTRY_POINT UefiMain [Sources] MyApplication.c [Packages] MdePkg/MdePkg.dec [LibraryClasses] UefiApplicationEntryPoint UefiLib EOF cat MyApplication.c EOF #include Uefi.h #include Library/UefiLib.h #include Library/UefiApplicationEntryPoint.h EFI_STATUS EFIAPI UefiMain ( IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable ) { SystemTable-ConOut-OutputString(SystemTable-ConOut, LHello, EDK2 World!\r\n); return EFI_SUCCESS; } EOF # 添加到编译系统 echo MyPkg/MyApplication/MyApplication.inf ../MyPkg.dsc