Cmake实现mac自动化编译打包app

一、cmake指定xcode 完成项目编译

  1. 在CMakeList.txt文件中添加

set(CMAKE_XCODE_GENERATE_SCHEME ON)

  1. 运行cmake命令,指定生成器xcode

在项目CMakeList.txt同级目录下创建build文件夹

mkdir build
cd build
cmake .. -G Xcode -D CMAKE_C_COMPILER=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -D CMAKE_CXX_COMPILER=/Applications/Xcode.app/Content/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -DCMAKE_BUILD_TYPE=Release

执行完上方代码,将会在build文件夹中生成xcodeproj配置文件

参数说明:

CMAKE_C_COMPILER、CMAKE_CXX_COMPILER:

是指定c、c++编译器位置,如果不确定自己电脑的c、c++编译器位置,可以使用

xcrun -find cc以及xcrun -find c++找到对应的位置

以上传入的参数是计算机中的默认路径

  1. 使用xcodebuild编译xcodeproj配置文件

首先介绍一下xcodebuild命令的参数:

xcodebuild 包含以下参数:
-project: 指定项目文件;
-target: 指定要编译的target;
-configuration: 指定编译配置;
-sdk: 指定要使用的SDK;
-arch: 指定要编译的CPU架构;
-jobs: 指定要使用的CPU核心数;
-alltargets: 编译项目中的所有targets;
-destination: 指定设备的位置;
-buildstyle: 指定编译的style;
-toolchain: 指定要使用的工具链;
-showsdks: 显示可用的SDK列表;
-showdestinations: 显示可用的设备列表;
-dry-run: 显示编译过程,但不真正编译;
-quiet: 缩短编译过程中的输出;
-verbose: 输出详细的编译信息;
-help: 显示xcodebuild的帮助信息。

运行一下命令使用xcodebuild工具编译项目:

xcodebuild -project <projectname>.xcodeproj -scheme <schemename> -destination <destination> build

实例如下:

xcodebuild -project HelloWorld.xcodeproj -scheme HelloWorld -destination 'generic/platform=iOS' build

如果你想生成的是macos平台32位程序,可以在destination属性中设置platform=macOS,arch=x86_64。最后在build后添加ARCHS=x86_64

实例如下:

xcodebuild -project HelloWorld.xcodeproj -destination 'platform=macOS,arch=x86_64' build ARCHS=x86_64

执行完以上指令,将会生成项目对应的静态库动态库或可执行文件。

二、自动化生成app

上方使用xcodebuild直接生成编译产物,如果想要生成app文件,需要采用一下方法。

具体思路:首先生成正常的archive,然后再从archive中导出app

(1)生成正常的archive

xcodebuild archive  -project xxx.xcodeproj -scheme xxx -configuration release -archivePath ./Applications/xxx.xcarchive

实例如下:

xcodebuild archive -project HelloWorld.xcodeproj -scheme HelloWorld -configuration release -archivePath ~/Desktop/MyApp.xcarchive -UseModernBuildSystem=YES

(2)从archive中导出app

xcodebuild -exportArchive -archivePath ./Applications/xxx.xcarchive -exportPath ./Applications/ -exportOptionsPlist ./exportOptionsAdHoc.plist -allowProvisioningUpdates

示例如下:

xcodebuild -exportArchive -archivePath ./Desktop/MyApp.xcarchive -exportPath ~/Desktop -exportOptionsPlist ~/Desktop/ExportOptions.plist

在导出选项plist文件中,我们可执行应用程序的导出格式、签名文件、应用程序ID等选项。例如一下是一个实例plist文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>method</key>
    <string>app-store</string>
    <key>teamID</key>
    <string>YOUR_TEAM_ID</string>
    <key>uploadBitcode</key>
    <true/>
    <key>uploadSymbols</key>
    <true/>
  </dict>
</plist>

如果我们想验证导出选项plist文件格式是否正确,可以在终端使用以下命令验证文件格式:

plutil -lint ~/Desktop/ExportOptions.plist

导出选项plist文件中缺少必需的键值对时会报“error:exportArchive:exportOptionsPlist error for key”错误。"method","teamID"是必需的键。

在plist文件中,“method”键是必需的,用于指定导出应用程序的方法。可以使用以下三种方法之一:

1. ad-hoc:用于将应用程序导出到设备以进行测试或分发、
2. app-store:用于将应用程序打包并提交到App Store
3. enterprise:用于将应用程序导出并分发到企业内部。

三、编译失败问题总结

1、cmake编译时的错误

Error: your C compiler:"CMAKE_C_COMPILER-NOTFOUND" was not found. Please setCMAKE_C_COMPILER to a valid compiler path or name.
Error:your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name

这个错的的内容表面意思就是cmake没有找到c与c++编译器。

解决方法:

在cmake -G Xcode 后添加CMAKE_C_COMPILER、CMAKE_CXX_COMPILER参数

2、xcodebuild 提示error:The Legacy Build System will be removed in a future release.

Prepare build
error: The Legacy Build System will be removed in a future release. You can configure the selected build system and this deprecation message in File > Workspace Settings.
Build Preparation
The Legacy Build System will be removed in a future release. You can configure the selected build system and this deprecation message in File > Workspace Settings.

** CLEAN FAILED **
The following build commands failed:
    Prepare build
(1 failure)
Build step 'Execute shell' marked build as failure
Finished: FAILURE

解决方法:

在xcodebuild生成archive时添加-UseModernBuildSystem=value

  • UseModernBuildSystem=0NO,使用 legacy build system

  • UseModernBuildSystem=1YES,使用 new build system

3、运行xcodebuild命令时出现“error:exportArchive:exportOptionsPlist error for key”错误

exportArchive: exportOptionsPlist error for key 'method': expected one of {}, but found ad-hoc

目前该问题还没解决

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值