CMake構建學習筆記22-libxml2庫的構建
在上一篇文章《CMake構建學習筆記21-通用的CMake構建腳本》中,筆者封裝了一個通用的cmake構建腳本cmake-build.ps1,那么這里筆者就嘗試通過這個腳本來構建libxml2庫。
libxml2是GNOME項目下的XML庫,雖然比不上TinyXML-2輕量,但是勝在功能全面。這里就直接列出構建libxml2的腳本:
param(
[string]$Name = "libxml2-v2.14.4",
[string]$SourceDir = "../Source",
[string]$Generator,
[string]$InstallDir,
[string]$SymbolDir
)
# 根據 $Name 動態構建路徑
$zipFilePath = Join-Path -Path $SourceDir -ChildPath "$Name.zip"
$SourcePath = Join-Path -Path $SourceDir -ChildPath $Name
$BuildDir = Join-Path -Path "." -ChildPath $Name
# 解壓ZIP文件到指定目錄
if (!(Test-Path $SourcePath)) {
Expand-Archive -LiteralPath $zipFilePath -DestinationPath $SourceDir -Force
}
# 檢查目標文件是否存在,以判斷是否安裝
$DstFilePath = "$InstallDir/bin/libxml2.dll"
if (Test-Path $DstFilePath) {
Write-Output "The current library has been installed."
exit 1
}
# 復制符號庫
$PdbFiles = @(
"$BuildDir/RelWithDebInfo/libxml2.pdb"
)
# 額外構建參數
$CMakeCacheVariables = @{
BUILD_SHARED_LIBS = "ON"
LIBXML2_WITH_ZLIB = "ON"
LIBXML2_WITH_ICONV = "ON"
LIBXML2_WITH_HTTP = "ON"
}
# 調用通用構建腳本
. ./cmake-build.ps1 -SourceLocalPath $SourcePath `
-BuildDir $BuildDir `
-Generator $Generator `
-InstallDir $InstallDir `
-SymbolDir $SymbolDir `
-PdbFiles $PdbFiles `
-CMakeCacheVariables $CMakeCacheVariables `
-MultiConfig $true
這段腳本實現了解壓源代碼文件,判斷是否已安裝、復制符號庫、額外構建參數。最后再執行cmake-build.ps1腳本。有的步驟如何不需要可以省略,不過額外構建參數還是需要關心一下,比如LIBXML2_WITH_ZLIB表示使用依賴庫zlib參與構建(參看《CMake構建學習筆記2-zlib庫的構建》);LIBXML2_WITH_ICONV,表示使用依賴庫iconv參與構建(參看《CMake構建學習筆記20-iconv庫的構建》)。
在PowerShell中使用如下指令進行構建:
./libxml2.ps1 -Generator "Visual Studio 16 2019" `
-InstallDir "$env:eGova3rdParty" `
-SymbolDir "$env:eGova3rdParty/symbols" `

浙公網安備 33010602011771號