仓颉SDK构建指导书
1 构建概述
本指导书用于指导用户:在 MacOS 系统中搭建 Cangjie SDK 构建环境及集成构建包含全部组件的 mac-ios Cangjie SDK。
构建流程全景图
kanban
1.准备阶段
A[准备环境]
B[编译核心依赖]
C[创建工作区]
2.源码获取与核心编译阶段
D[获取仓颉源码]
E[编译仓颉编译器和调试器]
F[编译仓颉运行时]
3.库编译阶段
G[编译仓颉标准库]
H[编译STDX扩展库]
4.工具集编译阶段
I[编译cjpm工具]
J[编译cjfmt工具]
K[编译cjlint工具]
L[编译cjcov工具]
M[编译cjtrace-recover工具]
N[编译hle工具]
O[编译lspserver工具]
P[编译cjprof工具]
Q[编译互操作库]
5.打包和验证
R[组织并打包文件]
S[编译Hello,Cangjie程序]
2 环境准备
2.1 系统要求
- 操作系统: 以 macOS 14 Sonoma 作为示例,其他版本也可以参考本文流程
- 磁盘空间: ≥50GB
- 内存: ≥8GB (物理内存+交换空间)
- 用户权限: 普通用户 + sudo权限
- 芯片指令集:请务必根据您的计算机芯片架构(x86_64或aarch64)设置对应的环境变量,同时请注意,
mac x86_64不能编出mac aarch64的运行时和库。 - Xcode: 本文以
Xcode 15.4作为示例
2.2 系统工具安装
推荐使用Homebrew安装编译仓颉所需的依赖:
(1) 安装所需依赖
1. cmake(要求>=3.17, <4)
前往CMake官网下载dmg或tar.gz包手动进行安装:
-
使用dmg安装: 下载后拖入Application中,并执行:
* 下方示例的
/path/to/cmake需更换为您的自定义安装目录# 下方命令二选一: # 安装至/usr/local/bin sudo "/Applications/CMake.app/Contents/bin/cmake-gui" --install # 自定义安装位置 sudo "/Applications/CMake.app/Contents/bin/cmake-gui" --install=/path/to/cmake # 使用前将cmake/bin加入环境变量 PATH="/Applications/CMake.app/Contents/bin:$PATH” -
使用tar.gz:
# 解压tar包 tar -zxvf cmake-3.x.x-macos-universal.tar.gz /path/to/cmake # 使用前将cmake/bin加入环境变量或将配置加入~/.zshrc中 export PATH="/path/to/cmake/bin:$PATH"
(2) 其他必要的构建工具
brew install python3 ninja llvm@16 openssl@3 bison googletest gnu-tar swig
2.3 Xcode 15.4
export BUILD_ROOT=/opt/buildtools;
export XCODE_ROOT=${BUILD_ROOT}/xcode_15.4;
sudo mkdir -p ${XCODE_ROOT} && cd ${XCODE_ROOT};
curl -O https://download.developer.apple.com/Developer_Tools/Xcode_15.4/Xcode_15.4.xip;
xip -x Xcode_15.4.xip;
2.4 设置环境变量
请在构建前务必设置以下变量:
export ARCH=aarch64 # 或x86_64
export CANGJIE_VERSION=1.0.0 # Cangjie SDK 版本
export STDX_VERSION=1 # Stdx 版本
export SDK_NAME=mac-aarch64-ios # 或mac-x64-ios
XCODE_TOOLCHAIN=${XCODE_ROOT}/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
IOS_SYSROOT=${XCODE_ROOT}/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.5.sdk
IOS_SIMULATOR_SYSROOT=${XCODE_ROOT}/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.5.sdk
IOS_TARGET=ios-aarch64
IOS_SIMULATOR_TARGET=ios-simulator-aarch64
IOS_SIMULATOR_X86_TARGET=ios-simulator-x86_64
OBJC_IOS_SIMULATOR_X86_TARGET=x86_64-apple-ios-simulator
* 请注意:以下构建工具版本请务必根据您安装的具体版本进行替换
* 下方示例的16.x、1.x、3.x等版本需更换为您所安装的具体版本号
export CELLAR_PATH=/path/to/Cellar
# 可使用brew --cellar命令查看
# 对于arm芯片,Cellar通常位于:/opt/homebrew/Cellar
# 对于intel芯片,Cellar通常位于:/usr/local/Cellar
# 如何找到版本号:
# 使用 `brew list --versions` 查看已安装的软件包及其版本号,例如:
# brew list --versions llvm@16
export PATH=$CELLAR_PATH/llvm@16/16.x/bin:$PATH
export OPENSSL_PATH=$CELLAR_PATH/openssl@3/3.x/lib
export LD_LIBRARY_PATH=$OPENSSL_PATH:$LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=$OPENSSL_PATH:$DYLD_LIBRARY_PATH
3 源码准备
3.1 创建工作区
* 下方示例的/path/to/workspace需更换为您构建仓颉SDK的工作目录
export WORKSPACE=/path/to/workspace
mkdir -p $WORKSPACE;
cd $WORKSPACE;
3.2 获取仓颉源码
git clone https://gitcode.com/Cangjie/cangjie_compiler.git -b main;
git clone https://gitcode.com/Cangjie/cangjie_runtime.git -b main;
git clone https://gitcode.com/Cangjie/cangjie_tools.git -b main;
git clone https://gitcode.com/Cangjie/cangjie_stdx.git -b main;
git clone https://gitcode.com/Cangjie/cangjie_multiplatform_interop.git -b main;
4 编译流程
4.1 编译仓颉编译器和调试器
执行构建:
cd $WORKSPACE/cangjie_compiler;
python3 build.py clean;
python3 build.py build -t release -v ${CANGJIE_VERSION} --no-tests --build-cjdb;
python3 build.py build -t release \
--target ${IOS_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN} \
--target-sysroot ${IOS_SYSROOT};
python3 build.py build -t release \
--target ${IOS_SIMULATOR_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN} \
--target-sysroot ${IOS_SIMULATOR_SYSROOT};
python3 build.py build -t release \
--target ${IOS_SIMULATOR_X86_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN} \
--target-sysroot ${IOS_SIMULATOR_SYSROOT};
python3 build.py install;
验证安装:
source output/envsetup.sh;
cjc -v;
4.2 编译仓颉运行时
cd $WORKSPACE/cangjie_runtime/runtime;
python3 build.py clean;
python3 build.py build -t release -v ${CANGJIE_VERSION};
python3 build.py install;
python3 build.py build -t release \
-v ${CANGJIE_VERSION} \
--target ${IOS_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN};
python3 build.py install;
python3 build.py build -t release \
-v ${CANGJIE_VERSION} \
--target ${IOS_SIMULATOR_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN};
python3 build.py install;
python3 build.py build -t release \
-v ${CANGJIE_VERSION} \
--target ${IOS_SIMULATOR_X86_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN};
python3 build.py install;
cp -R output/common/darwin_release_${ARCH}/{lib,runtime} ${WORKSPACE}/cangjie_compiler/output;
cp -R output/common/ios_release_aarch64/{lib,runtime} ${WORKSPACE}/cangjie_compiler/output;
cp -R output/common/ios_simulator_release_aarch64/{lib,runtime} ${WORKSPACE}/cangjie_compiler/output;
cp -R output/common/ios_simulator_release_x86_64/{lib,runtime} ${WORKSPACE}/cangjie_compiler/output;
4.3 编译仓颉标准库
cd $WORKSPACE/cangjie_runtime/stdlib;
python3 build.py clean;
python3 build.py build -t release \
--target-lib=$WORKSPACE/cangjie_runtime/runtime/output \
--target-lib=$OPENSSL_PATH;
python3 build.py build -t release \
--target-lib=${WORKSPACE}/cangjie_runtime/runtime/output \
--target-lib=$OPENSSL_PATH \
--target ${IOS_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN} \
--target-sysroot ${IOS_SYSROOT};
python3 build.py build -t release \
--target-lib=${WORKSPACE}/cangjie_runtime/runtime/output \
--target-lib=$OPENSSL_PATH \
--target ${IOS_SIMULATOR_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN} \
--target-sysroot ${IOS_SIMULATOR_SYSROOT};
python3 build.py build -t release \
--target-lib=${WORKSPACE}/cangjie_runtime/runtime/output \
--target-lib=$OPENSSL_PATH \
--target ${IOS_SIMULATOR_X86_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN} \
--target-sysroot ${IOS_SIMULATOR_SYSROOT};
python3 build.py install;
cp -R output/* $WORKSPACE/cangjie_compiler/output/;
4.4 编译 Stdx 扩展库
cd $WORKSPACE/cangjie_stdx;
python3 build.py clean;
python3 build.py build -t release \
--target-lib=$OPENSSL_PATH \
--include=${WORKSPACE}/cangjie_compiler/include;
python3 build.py build -t release \
--target-lib=$OPENSSL_PATH \
--target ${IOS_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN} \
--target-sysroot ${IOS_SYSROOT} \
--include=${WORKSPACE}/cangjie_compiler/include;
python3 build.py build -t release \
--target-lib=$OPENSSL_PATH \
--target ${IOS_SIMULATOR_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN} \
--target-sysroot ${IOS_SIMULATOR_SYSROOT} \
--include=${WORKSPACE}/cangjie_compiler/include;
python3 build.py build -t release \
--include=${WORKSPACE}/cangjie_compiler/include \
--target-lib=$OPENSSL_PATH \
--target ${IOS_SIMULATOR_X86_TARGET} \
--target-toolchain ${XCODE_TOOLCHAIN} \
--target-sysroot ${IOS_SIMULATOR_SYSROOT};
python3 build.py install;
export CANGJIE_STDX_PATH=$WORKSPACE/cangjie_stdx/target/darwin_${ARCH}_cjnative/static/stdx;
4.5 编译仓颉工具集
(1) cjpm
cd $WORKSPACE/cangjie_tools/cjpm/build;
python3 build.py clean;
python3 build.py build -t release --set-rpath @loader_path/../../runtime/lib/darwin_${ARCH}_cjnative;
python3 build.py install;
mkdir -p ${WORKSPACE}/cangjie_compiler/output/tools/config;
cp ${WORKSPACE}/cangjie_tools/cjpm/dist/cjpm ${WORKSPACE}/cangjie_compiler/output/tools/bin;
mv ${WORKSPACE}/cangjie_tools/cjpm/dist/*.toml ${WORKSPACE}/cangjie_compiler/output/tools/config;
(2) cjfmt
cd ${WORKSPACE}/cangjie_tools/cjfmt/build;
python3 build.py clean;
python3 build.py build -t release;
python3 build.py install;
mv ${WORKSPACE}/cangjie_tools/cjfmt/build/build/bin/cjfmt ${WORKSPACE}/cangjie_compiler/output/tools/bin;
mv ${WORKSPACE}/cangjie_tools/cjfmt/config/*.toml ${WORKSPACE}/cangjie_compiler/output/tools/config;
(3) cjlint
cd ${WORKSPACE}/cangjie_tools/cjlint/build;
python3 build.py clean;
python3 build.py build -t release;
python3 build.py install;
mv ${WORKSPACE}/cangjie_tools/cjlint/dist/bin/cjlint ${WORKSPACE}/cangjie_compiler/output/tools/bin
mv ${WORKSPACE}/cangjie_tools/cjlint/dist/lib/* ${WORKSPACE}/cangjie_compiler/output/tools/lib
mv ${WORKSPACE}/cangjie_tools/cjlint/dist/config/* ${WORKSPACE}/cangjie_compiler/output/tools/config
(4) cjcov
cd ${WORKSPACE}/cangjie_tools/cjcov/build;
python3 build.py clean;
python3 build.py build -t release;
python3 build.py install;
mv ${WORKSPACE}/cangjie_tools/cjcov/dist/cjcov ${WORKSPACE}/cangjie_compiler/output/tools/bin;
(5) cjtrace-recover
cd ${WORKSPACE}/cangjie_tools/cjtrace-recover/build;
python3 build.py clean;
python3 build.py build -t release;
python3 build.py install --prefix ${WORKSPACE}/cangjie_compiler/output/tools;
(6) HyperLangExtension
cd $WORKSPACE/cangjie_tools/hyperlangExtension/build;
python3 build.py clean;
python3 build.py build -t release;
python3 build.py install;
cp ${WORKSPACE}/cangjie_tools/hyperlangExtension/target/bin/main ${WORKSPACE}/cangjie_compiler/output/tools/bin/hle;
cp -R ${WORKSPACE}/cangjie_tools/hyperlangExtension/src/dtsparser ${WORKSPACE}/cangjie_compiler/output/tools;
rm -rf ${WORKSPACE}/cangjie_compiler/output/tools/dtsparser/*.cj;
(7) LSP Server
cd $WORKSPACE/cangjie_tools/cangjie-language-server/build;
python3 build.py clean;
python3 build.py build -t release;
python3 build.py install;
cp ${WORKSPACE}/cangjie_tools/cangjie-language-server/output/bin/LSPServer ${WORKSPACE}/cangjie_compiler/output/tools/bin
(8) cjprof
cd ${WORKSPACE}/cangjie_tools/cjprof/build;
python3 build.py build -t release;
python3 build.py install;
cp $WORKSPACE/cangjie_tools/cjprof/dist/bin/cjprof ${WORKSPACE}/cangjie_compiler/output/tools/bin
cp $WORKSPACE/cangjie_tools/cjprof/dist/lib/* ${WORKSPACE}/cangjie_compiler/output/tools/lib;
(9) Objective-C Interop
cd ${WORKSPACE}/cangjie_multiplatform_interop/objc/build
python3 build.py build
python3 build.py install --prefix ${WORKSPACE}/cangjie_compiler/output
python3 build.py build --target darwin_aarch64
python3 build.py install --target darwin_aarch64 --prefix ${WORKSPACE}/cangjie_compiler/output
python3 build.py build --target ios_aarch64 --target-lib=${IOS_TARGET} --target-toolchain ${XCODE_TOOLCHAIN} --target-sysroot ${IOS_SYSROOT}
python3 build.py install --target ios_aarch64 --prefix ${WORKSPACE}/cangjie_compiler/output
python3 build.py build --target ios_simulator_aarch64 --target-lib=${IOS_SIMULATOR_TARGET} --target-toolchain ${XCODE_TOOLCHAIN} --target-sysroot ${IOS_SIMULATOR_SYSROOT}
python3 build.py install --target ios_simulator_aarch64 --prefix ${WORKSPACE}/cangjie_compiler/output
python3 build.py build --target ios_simulator_x86_64 --target-lib=${OBJC_IOS_SIMULATOR_X86_TARGET} --target-toolchain ${XCODE_TOOLCHAIN} --target-sysroot ${IOS_SIMULATOR_SYSROOT}
python3 build.py install --target ios_simulator_x86_64 --prefix ${WORKSPACE}/cangjie_compiler/output
5 组织并打包文件
5.1 组织并打包 SDK
# 清空历史构建
mkdir -p $WORKSPACE/software;
rm -rf $WORKSPACE/software/*;
cd $WORKSPACE/software;
# 拷贝cangjie目录
cp -R $WORKSPACE/cangjie_compiler/output cangjie;
cp $WORKSPACE/cangjie_compiler/LICENSE cangjie;
cp $WORKSPACE/cangjie_compiler/Open_Source_Software_Notice.docx cangjie;
# 打包和设置权限
chmod -R 750 cangjie
gtar --format=gnu -zcvf cangjie-sdk-${SDK_NAME}-${CANGJIE_VERSION}.tar.gz cangjie;
chmod 550 cangjie-sdk-${SDK_NAME}-${CANGJIE_VERSION}.tar.gz;
5.2 组织并打包 Stdx
cd $WORKSPACE/software;
# 拷贝stdx目录
cp -R $WORKSPACE/cangjie_stdx/target/ios_*_cjnative ./;
cp $WORKSPACE/cangjie_stdx/LICENSE ios_aarch64_cjnative;
cp $WORKSPACE/cangjie_stdx/LICENSE ios_simulator_aarch64_cjnative;
cp $WORKSPACE/cangjie_stdx/LICENSE ios_simulator_x86_64_cjnative;
cp $WORKSPACE/cangjie_stdx/Open_Source_Software_Notice.docx ios_aarch64_cjnative;
cp $WORKSPACE/cangjie_stdx/Open_Source_Software_Notice.docx ios_simulator_aarch64_cjnative;
cp $WORKSPACE/cangjie_stdx/Open_Source_Software_Notice.docx ios_simulator_x86_64_cjnative;
chmod -R 750 ios_*_cjnative;
zip -qr cangjie-stdx-ios-aarch64-${CANGJIE_VERSION}.${STDX_VERSION}.zip ios_aarch64_cjnative;
zip -qr cangjie-stdx-ios-simulator-aarch64-${CANGJIE_VERSION}.${STDX_VERSION}.zip ios_simulator_aarch64_cjnative;
zip -qr cangjie-stdx-ios-simulator-x64-${CANGJIE_VERSION}.${STDX_VERSION}.zip ios_simulator_x86_64_cjnative;
chmod 550 cangjie-stdx-ios-*-${CANGJIE_VERSION}.${STDX_VERSION}.zip;
6 编译Hello, Cangjie程序
检查生成的文件:
ls -lh $WORKSPACE/software
# 应包含:
# - cangjie-sdk-${SDK_NAME}-${CANGJIE_VERSION}.tar.gz
# - cangjie-stdx-ios-aarch64-${CANGJIE_VERSION}.${STDX_VERSION}.zip
# - cangjie-stdx-ios-simulator-aarch64-${CANGJIE_VERSION}.${STDX_VERSION}.zip
# - cangjie-stdx-ios-simulator-x64-${CANGJIE_VERSION}.${STDX_VERSION}.zip
验证 Hello, Cangjie 程序:
cd $WORKSPACE;
source $WORKSPACE/software/cangjie/envsetup.sh;
echo "main() { println(\"Hello, Cangjie\") }" > hello.cj
cjc hello.cj -o hello && ./hello
# 您将在控制台看到以下输出:
# Hello, Cangjie
🎉 恭喜您成功构建了仓颉SDK并运行了Hello, Cangjie程序!