news 2026/5/9 15:53:19

别再折腾了!Windows 10/11下EDK2+VS2019+Python 3.11一键式环境搭建保姆级教程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
别再折腾了!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 InstallAllUsers=1 PrependPath=1 Include_launcher=1" # 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 Green

3. 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"; Url="https://github.com/google/brotli.git" }, @{ Path="$edk2Path\MdePkg\Library\MipiSysTLib\mipisyst"; Url="https://github.com/MIPI-Alliance/public-mipi-sys-t.git" }, @{ Path="$edk2Path\SecurityPkg\DeviceSecurity\SpdmLib\libspdm"; Url="https://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 Green

3.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 Green

4. 编译与验证

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:ICF=10 /INCREMENTAL:NO

5.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, L"Hello, EDK2 World!\r\n"); return EFI_SUCCESS; } EOF # 添加到编译系统 echo "MyPkg/MyApplication/MyApplication.inf" >> ../MyPkg.dsc
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/9 15:46:29

3个关键步骤:用MouseTester精准诊断鼠标性能瓶颈

3个关键步骤&#xff1a;用MouseTester精准诊断鼠标性能瓶颈 【免费下载链接】MouseTester 项目地址: https://gitcode.com/gh_mirrors/mo/MouseTester 还在为游戏中鼠标漂移、设计软件中光标抖动而烦恼吗&#xff1f;你的鼠标可能正在悄悄拖慢你的工作效率&#xff01…

作者头像 李华
网站建设 2026/5/9 15:40:30

CANN TileLang算子设计模板

{算子名称} 算子设计文档 【免费下载链接】cannbot-skills CANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体&#xff0c;本仓库为其提供可复用的 Skills 模块。 项目地址: https://gitcode.com/cann/cannbot-skills 1. 概述 1.1 算子名称 {算子名称} 1.2 功…

作者头像 李华