CMake构建学习笔记24-使用通用脚本构建PROJ和GEOS
1. 通用脚本
在之前的文章《CMake构建学习笔记21-通用的CMake构建脚本》中我们创建了一个通用的cmake构建脚本cmake-build.ps1
:
param(
[string]$SourceLocalPath,
[string]$BuildDir,
[string]$Generator,
[string]$InstallDir,
[string]$SymbolDir,
[string[]]$PdbFiles,
[hashtable]$CMakeCacheVariables,
[bool]$MultiConfig = $false # 控制是否使用多配置类型
)
# 清除旧的构建目录
if (Test-Path $BuildDir) {
Remove-Item -Path $BuildDir -Recurse -Force
}
New-Item -ItemType Directory -Path $BuildDir
# 构建CMake命令行参数
$CMakeArgs = @(
"-B", "`"$BuildDir`"",
"-G", "`"$Generator`"",
"-A", "x64"
)
if ($MultiConfig) {
$CMakeArgs += "-DCMAKE_CONFIGURATION_TYPES=RelWithDebInfo"
}
else {
$CMakeArgs += "-DCMAKE_BUILD_TYPE=RelWithDebInfo"
}
$CMakeArgs += (
"-DCMAKE_PREFIX_PATH=`"$InstallDir`"",
"-DCMAKE_INSTALL_PREFIX=`"$InstallDir`""
)
# 添加额外的CMake缓存变量
foreach ($key in $CMakeCacheVariables.Keys) {
$CMakeArgs += "-D$key=$($CMakeCacheVariables[$key])"
}
# 配置CMake
cmake $SourceLocalPath $CMakeArgs
# 构建阶段,指定构建类型
cmake --build $BuildDir --config RelWithDebInfo --parallel
# 安装阶段,指定构建类型和安装目标
cmake --build $BuildDir --config RelWithDebInfo --target install
# 复制符号库
foreach ($file in $PdbFiles) {
Write-Output $file
if (Test-Path $file) {
Copy-Item -Path $file -Destination $SymbolDir
}
else {
Write-Output "Warning: PDB file not found: $file"
}
}
# 清理构建目录
#Remove-Item -Path $BuildDir -Recurse -Force
在《CMake构建学习笔记22-libxml2库的构建》这篇文章中使用这个脚本构建了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构建方式的程序的构建行为是比较统一的,这个构建libxml2库的脚本可以进一步封装,形成一个通用的调用cmake-build.ps1
构建程序的脚本。cmake-build.ps1
只是包含了调用cmake执行构建的内容,但是其实整个构建过程需要做的事情很多,比如安装符号库、安装程序的依赖库等等,这些过程指的再封装一层构建的脚本。笔者封装的脚本build-common.ps1
如下:
# build-library.ps1
param(
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$true)]
[string]$SourceDir,
[Parameter(Mandatory=$true)]
[string]$InstallDir,
[string]$SymbolDir,
[string]$Generator,
[string]$MSBuild,
[hashtable]$CMakeCacheVariables = @{},
[string[]]$PdbFiles = @(),
[string]$TargetDll, # 用于判断是否已安装的 DLL 路径
[bool]$MultiConfig = $false, # 控制是否使用多配置类型
[bool]$Force = $false, # 是否强制重新构建
[bool]$Cleanup = $true, # 是否在构建完成后删除源码和构建目录
[string[]]$Librarys = @() # 可选的依赖库数组,例如:-Librarys "zlib", "libjpeg"
)
# 动态路径构建
$zipFilePath = Join-Path -Path $SourceDir -ChildPath "$Name.zip"
$SourcePath = Join-Path -Path $SourceDir -ChildPath $Name
$BuildDir = Join-Path -Path "." -ChildPath $Name
# 检查是否已经安装(通过目标 DLL)
if (-not $Force -and $TargetDll -and (Test-Path $TargetDll)) {
Write-Output "Library already installed: $TargetDll"
exit 0
}
# 创建所有依赖库的容器
if ($Librarys.Count -gt 0) {
. "./BuildRequired.ps1"
BuildRequired -Librarys $Librarys
}
# 确保源码目录存在:解压 ZIP
if (!(Test-Path $SourcePath)) {
if (!(Test-Path $zipFilePath)) {
Write-Error "Archive not found: $zipFilePath"
exit 1
}
Write-Output "Extracting $zipFilePath to $SourceDir..."
Expand-Archive -LiteralPath $zipFilePath -DestinationPath $SourceDir -Force
}
# 如果是强制构建,且构建目录已存在,先删除旧的构建目录(确保干净构建)
if ($Force -and (Test-Path $BuildDir)) {
Write-Output "Force mode enabled. Removing previous build directory: $BuildDir"
Remove-Item $BuildDir -Recurse -Force -ErrorAction SilentlyContinue
}
# 遍历并添加前缀
$PdbFiles = $PdbFiles | ForEach-Object {
Join-Path -Path $BuildDir -ChildPath $_
}
# 调用通用 CMake 构建脚本
Write-Output "Starting build for $Name..."
. ./cmake-build.ps1 -SourceLocalPath $SourcePath `
-BuildDir $BuildDir `
-Generator $Generator `
-InstallDir $InstallDir `
-SymbolDir $SymbolDir `
-PdbFiles $PdbFiles `
-CMakeCacheVariables $CMakeCacheVariables `
-MultiConfig $MultiConfig
if ($LASTEXITCODE -ne 0) {
Write-Error "Build failed for $Name."
exit $LASTEXITCODE
}
# 构建成功后,根据 Cleanup 开关决定是否删除
if ($Cleanup) {
Write-Output "Build succeeded. Cleaning up temporary directories..."
if (Test-Path $SourcePath) {
Remove-Item $SourcePath -Recurse -Force -ErrorAction SilentlyContinue
Write-Output "Removed source directory: $SourcePath"
}
if (Test-Path $BuildDir) {
Remove-Item $BuildDir -Recurse -Force -ErrorAction SilentlyContinue
Write-Output "Removed build directory: $BuildDir"
}
}
Write-Output "Build completed for $Name."
这段脚本干了很多零碎的事情,但是对于一个完整的构建系统是必须的,比如判断是否需要强制构建、是否需要清理中间文件、安装程序的依赖库、安装符号库等等。另外,脚本的使用源代码其实是从压缩包解压出来的,这是因为笔者需要将源代码文件也值得放在git中进行管理,使用源代码压缩包更为方便。
2. 构建geos、proj
在实现了通用脚本build-common.ps1
之后,构建程序就非常容易了,比如构建geos的脚本如下:
# geos.ps1
param(
[string]$Name = "geos-3.12.2",
[string]$SourceDir = "../Source",
[string]$Generator,
[string]$InstallDir,
[string]$SymbolDir,
[bool]$Force = $false, # 是否强制重新构建
[bool]$Cleanup = $true # 是否在构建完成后删除源码和构建目录
)
# 目标文件
$DllPath = "$InstallDir/bin/geos_c.dll"
# 依赖库数组
$Librarys = @()
# 符号库文件
$PdbFiles = @(
"bin/RelWithDebInfo/geos.pdb",
"bin/RelWithDebInfo/geos_c.pdb"
)
# 额外构建参数
$CMakeCacheVariables = @{
BUILD_TESTING = "OFF"
}
. ./build-common.ps1 -Name $Name `
-SourceDir $SourceDir `
-InstallDir $InstallDir `
-SymbolDir $SymbolDir `
-Generator $Generator `
-TargetDll $DllPath `
-PdbFiles $PdbFiles `
-CMakeCacheVariables $CMakeCacheVariables `
-MultiConfig $false `
-Force $Force `
-Cleanup $Cleanup `
-Librarys $Librarys
在这个脚本中,$SourceDir
是源代码压缩包所在的文件夹,$Name
是压缩包和压缩包内文件夹的名称。而构建proj的脚本如下:
# proj.ps1
param(
[string]$Name = "proj-9.4.1",
[string]$SourceDir = "../Source",
[string]$Generator,
[string]$InstallDir,
[string]$SymbolDir,
[bool]$Force = $false, # 是否强制重新构建
[bool]$Cleanup = $true # 是否在构建完成后删除源码和构建目录
)
# 目标文件
$DllPath = "$InstallDir/bin/proj_9_4.dll"
# 依赖库数组
$Librarys = @("nlohmann-json", "sqlite", "libtiff")
# 符号库文件
$PdbFiles = @(
"bin/RelWithDebInfo/proj_9_4.pdb"
)
# 额外构建参数
$CMakeCacheVariables = @{
BUILD_TESTING = "OFF"
ENABLE_CURL = "OFF"
BUILD_PROJSYNC = "OFF"
}
. ./build-common.ps1 -Name $Name `
-SourceDir $SourceDir `
-InstallDir $InstallDir `
-SymbolDir $SymbolDir `
-Generator $Generator `
-TargetDll $DllPath `
-PdbFiles $PdbFiles `
-CMakeCacheVariables $CMakeCacheVariables `
-MultiConfig $false `
-Force $Force `
-Cleanup $Cleanup `
-Librarys $Librarys
proj必须依赖于sqlite,具体的构建办法可参看《CMake构建学习笔记23-SQLite库的构建》。因为库程序本身就可能会依赖别的依赖库,所以在这里干脆实现了在构建库之前,也构建该库的依赖库,具体实在build-common.ps1
中实现:
if ($Librarys.Count -gt 0) {
. "./BuildRequired.ps1"
BuildRequired -Librarys $Librarys
}
BuildRequired.ps1
也是个构建脚本,具体内容非常简单,就是调用依赖库的构建脚本:
function BuildRequired {
param (
[string[]]$Librarys
)
Write-Output "------------------------------------------------"
Write-Output "Start installing all required dependencies..."
foreach ($item in $Librarys) {
Write-Output "Find the library named $item and start installing..."
# 动态构建脚本文件名并执行
$BuildScript = "./$item.ps1";
& $BuildScript -Generator $Generator -InstallDir $InstallDir -SymbolDir $SymbolDir
}
Write-Output "All required dependencies have been installed."
Write-Output "------------------------------------------------"
}
3. 其他
提供的脚本太多,笔者确实也觉得有点太绕了,反而不如前面的文章的脚本内容直观。不过这也符合编程的基本思路吧,开始的程序都很简单直接,后来随着功能的增多,慢慢就变得越来越抽象难以理解。以上脚本都收录在项目中,可参考使用。
CMake构建学习笔记24-使用通用脚本构建PROJ和GEOS的更多相关文章
- Linux Shell输出颜色字符学习笔记(附Python脚本实现自动化定制生成)
齿轮发出咔嚓一声,向前进了一格.而一旦向前迈进,齿轮就不能倒退了.这就是世界的规则. 0x01背景 造了个轮子:御剑师傅的ipintervalmerge的Python版本.觉得打印的提示信息如果是普通 ...
- Android自动化学习笔记:编写MonkeyRunner脚本的几种方式
---------------------------------------------------------------------------------------------------- ...
- python 学习笔记 12 -- 写一个脚本获取城市天气信息
近期在玩树莓派,前面写过一篇在树莓派上使用1602液晶显示屏,那么可以显示后最重要的就是显示什么的问题了. 最easy想到的就是显示时间啊,CPU利用率啊.IP地址之类的.那么我认为呢,假设可以显示当 ...
- SpringCloud学习笔记(6):使用Zuul构建服务网关
简介 Zuul是Netflix提供的一个开源的API网关服务器,SpringCloud对Zuul进行了整合和增强.服务网关Zuul聚合了所有微服务接口,并统一对外暴露,外部客户端只需与服务网关交互即可 ...
- [原创]java WEB学习笔记24:MVC案例完整实践(part 5)---删除操作的设计与实现
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- Java学习笔记之使用反射+泛型构建通用DAO
PS:最近简单的学了学后台Servlet+JSP.也就只能学到这里了.没那么多精力去学SSH了,毕竟Android还有很多东西都没学完.. 学习内容: 1.如何使用反射+泛型构建通用DAO. 1.使用 ...
- Catlike学习笔记(1.4)-使用Unity构建分形
又两个星期没写文章了,主要是沉迷 Screeps 这个游戏,真的是太好玩了导致我这两个礼拜 Github 小绿点几乎天天刷.其实想开一个新坑大概把自己写 AI 的心路历程记录下,不过觉得因为要消耗太多 ...
- C++学习笔记24:makefile文件
makefile make命令:负责c/c++程序编译与链接 make根据指定命令进行建构 建构规则文件:GNUmakefile , makefile,Makefile makefile 文件格式 m ...
- 网站构建学习笔记(0)——基本概念了解及资源学习(copy自w3school)
一.学习方面 1.WWW - 万维网 什么是 WWW? WWW 指万维网(World Wide Web) 万维网常被称为Web Web 是由遍布全球的计算机所组成的网络 所有 Web 中的计算机都可以 ...
- 《Spring实战》学习笔记-第五章:构建Spring web应用
之前一直在看<Spring实战>第三版,看到第五章时发现很多东西已经过时被废弃了,于是现在开始读<Spring实战>第四版了,章节安排与之前不同了,里面应用的应该是最新的技术. ...
随机推荐
- HyperWorks的Loose Shrink Warp Mesh
我们希望用户通过对比学习的方式,研究 Loose Shrink Warp Mesh 和 Tight Shrink Warp Mesh 二者的技术细节及其区别.Loose Shrink Warp Mes ...
- [联合省选2025 游记] Now and forever
[联合省选2025 游记] Now and forever day -1 乐死我了,今天出了个巨大的乐子,总结为逐火十三英桀 文章链接:https://www.luogu.com.cn/article ...
- 前端切页面 架构配置 node npm grunt grunt合并HTML必看
快速搭建前端开发环境 1.npm包依赖 { "name": "demo", "version": "1.0.0", &q ...
- HarmonyOS NEXT仓颉开发语言实战案例:外卖App
各位周末好,今天为大家来仓颉语言外卖App的实战分享. 我们可以先分析一下页面的布局结构,它是由导航栏和List容器组成的.幽蓝君目前依然没有找到仓颉语言导航栏的系统组件,还是要自定义,这个导航栏有三 ...
- Blazor学习之旅(2)第一个Blazor应用
本篇我们来构建第一个Blazor Web应用,这里我们选择Blazor Server类型,后面我们再学习Blazor WebAssembly类型. 话外音:有人问我西门子在用Blazor吗?是的,西门 ...
- 11-2 MySQL 数据库对象编写建议(参考)
11-2 MySQL 数据库对象编写建议(参考) @ 目录 11-2 MySQL 数据库对象编写建议(参考) 1. 数据库对象编写建议/推荐 1.1 关于库 1.2 关于表.列 1.3 索引 1.4 ...
- 学习spring cloud记录7-nacos服务分级存储模型
前言 添加集群,级别分别为服务--集群--实例. 配置集群 可在配置文件中添加以下配置设置该服务的集群 cloud: nacos: server-addr: localhost:8848 # naco ...
- R Studio操作技巧笔记
快捷键 Ctrl+Shift+C 注释快捷键,可以添加/消除注释,也可多行注释 Ctrl + Shift + Enter 执行整个文件 Ctrl+Enter 运行当前/被选中的代码 Ctrl+L 清空 ...
- ZSH终端 > 乱码问题
参考链接 CSDN
- 什么是iPaaS?
一.iPaaS简介 iPaaS,即集成平台即服务(Integration Platform as a Service),是一种基于云计算的自助服务模型,它为企业提供了一种标准化的应用程序集成方式.能够 ...