CMake构建学习笔记22-libxml2库的构建

在上一篇文章《CMake构建学习笔记21-通用的CMake构建脚本》中,笔者封装了一个通用的cmake构建脚本cmake-build.ps1,那么这里笔者就尝试通过这个脚本来构建libxml2库。

libxml2是GNOME项目下的XML库,虽然比不上TinyXML-2轻量,但是胜在功能全面。这里就直接列出构建libxml2的脚本:

powershell 复制代码
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中使用如下指令进行构建:

powershell 复制代码
./libxml2.ps1 -Generator "Visual Studio 16 2019" `
-InstallDir "$env:eGova3rdParty" `
-SymbolDir "$env:eGova3rdParty/symbols" `

构建代码项目

相关推荐
code bean1 天前
【CMake 】[第十篇]CMake find_package 完全指南:让第三方库集成变得简单
c++·cmake
雪域迷影3 天前
Windows11中使用VS2022编译运行libevent网络库
网络·github·cmake·visual studio·libevent
code bean4 天前
【CMake 】CMake 中的 target_include_directories 详解
开发语言·c++·cmake
繁星蓝雨4 天前
Qt优雅的组织项目结构二(基于Qt5使用CmakeList进行模块化配置)——————附带详细示例代码
开发语言·qt·cmake·cmakefile.txt·.cmake
Peter·Pan爱编程5 天前
cmake 升级
c++·cmake·cuda
雪域迷影6 天前
macOS中使用cJSON解析库解析JSON
c++·macos·json·c·cmake·pkg-config
程序喵大人13 天前
CMake入门教程
开发语言·c++·cmake·cmake入门
威桑14 天前
一个 CMake 项目是否只能有一个 install 目录?
linux·c++·cmake
cape_NO_715 天前
10分钟学习CMake ①
cmake
gcfer15 天前
C/C++八股文知识积累5—项目从构建到运行的流程
make·cmake·c++八股·项目构建流程