构建 J2ObjC

要求

  • Apple Mac OS X 系统
  • 最新版本的 Xcode
  • JDK JDK 17 已安装
  • Apache Maven
  • (推荐,但不是必需)Google Protocol Buffer 源

Mac 设置

  • 运行以下命令来安装 Xcode 命令行工具:
sudo xcode-select --install
  • 添加到您的 .bash_profile
export JAVA_HOME=`/usr/libexec/java_home -v 17`
  • 如果未安装 Maven(在终端中尝试 mvn -version),请使用 Homebrew:
brew install maven

创建 J2ObjC 项目的 Fork

如需派生 J2ObjC 项目,请打开其主页,然后点击“派生”按钮:

Fork

选择您的 GitHub 账号名称,系统会在几秒钟内创建分支。

接下来,将您的分支克隆到本地系统以进行开发。首先,点击 [Clone or download button],然后点击“复制到剪贴板”按钮:

克隆

然后克隆到本地目录:

git clone (Command-V)  # Paste the copied URL

安装 protobuf 和 abseil

您需要先安装 protobuf C++,然后才能构建 J2ObjC。

如需安装 protobuf 二进制软件包,请使用 Homebrew:

brew install protobuf

Protobuf C++ build 现在依赖于 Abseil 库,因此也请安装该库:

brew install abseil

您需要将 PROTOBUF_ROOT_DIR 设置为安装 protobuf 的根目录。通常,这是 Homebrew 默认的 /usr/local。将以下内容添加到 .bash_profile 中:

export PROTOBUF_ROOT_DIR=/usr/local # or your custom Homebrew dir

如需进行验证,请运行 $PROTOBUF_ROOT_DIR/bin/protoc --version

构建 J2ObjC

如需从终端窗口中的命令行进行构建,请运行以下命令:

# Build just the translator and libraries
make dist

# Build the protocol buffer compiler and runtime for J2ObjC
make protobuf_dist

# Build everything that is included in the project's
distribution bundles
make all_dist

测试 J2ObjC

如需在终端窗口中通过命令行进行测试,请运行以下命令:

# Run translator and library unit tests
make test

# Run all unit tests, including for protocol buffers
make test_all

清理 build

# Remove all files generated by the build
make clean

并行构建

J2ObjC build 支持并行 build,使用 -j<n> 标志指定,其中 n 是并发任务的最大数量。最大值取决于系统的速度;我们建议从 -j4 开始。 如果您的系统可以处理,请增加此数量以缩短构建时间;如果 make 因系统错误而失败,请减少此数量。

构建 32 位 iOS 架构

默认情况下,不会构建 32 位 iOS 架构(“iphone”和“simulator”)。您可以通过先在 J2OBJC_ARCHS 环境变量中指定这些架构来构建 j2objc:

$ export J2OBJC_ARCHS="iphone simulator"
$ make -j8 all_dist

指定部分受支持的架构通常可以加快本地 build 速度,只需指定应用所需的架构即可。 例如,若要仅构建 64 位 iOS(“iphone64”)及其关联的模拟器(“simulator64”),请在运行 make 之前定义 J2OBJC_ARCHS="iphone64 simulator64"

构建完整分发

虽然 j2objc 团队不再因文件大小限制而将预构建的发行版发布到 GitHub,但仍可使用“一步构建所有内容”脚本:

$ scripts/build_distribution.sh HEAD-<current-date> $PROTOBUF_ROOT_DIR

此脚本需要很长时间才能运行完毕,因为系统会针对每个 j2objc 软件包构建每种架构。不过,如果出现常规 build 失败,此命令会非常有用,因为它仅使用最少的环境变量集进行 build。如果 build_distribution 脚本成功运行,但常规 build 步骤失败,请检查是否设置了任何错误的本地环境变量。