本指南介绍了如何构建与 Fuchsia 配合使用的 Rust 编译器。如果您需要使用修补后的编译器或使用自定义选项构建的编译器构建 Fuchsia,这会非常有用。使用其他版本的 Rust 构建 Fuchsia 并不总是需要构建自定义 Rust 工具链;如需了解详情,请参阅使用自定义 Rust 工具链构建 Fuchsia。
前提条件
在为 Fuchsia 构建自定义 Rust 工具链之前,您需要执行以下操作:
如果您尚未克隆 Rust 源代码,请克隆。在使用编译器时,您不妨参考 Rustc 开发指南。
DEV_ROOT=DEV_ROOT # parent of your Rust directory git clone --recurse-submodules https://github.com/rust-lang/rust.git $DEV_ROOT/rust
运行以下命令安装 cmake 和 jq/tomlq:
sudo apt-get install cmake ninja-build jq yq
运行以下命令以获取基础架构来源:
DEV_ROOT=DEV_ROOT # parent of your Rust directory
mkdir -p $DEV_ROOT/infra && \ ( \ builtin cd $DEV_ROOT/infra && \ jiri init && \ jiri import -overwrite -name=fuchsia/manifest infra \ https://fuchsia.googlesource.com/manifest && \ jiri update \ )
运行以下命令,使用
cipd
获取 Fuchsia 核心 IDK、Linux sysroot、最新版本的 Clang,以及用于构建 Fuchsia 的 Rust 工具链的正确 Beta 版编译器:DEV_ROOT=DEV_ROOT HOST_TRIPLE=x86_64-unknown-linux-gnu cat << "EOF" > cipd.ensure @Subdir sdk fuchsia/sdk/core/${platform} latest @Subdir sysroot/linux fuchsia/third_party/sysroot/linux git_revision:db18eec0b4f14b6b16174aa2b91e016663157376 @Subdir sysroot/focal fuchsia/third_party/sysroot/focal latest @Subdir clang fuchsia/third_party/clang/${platform} integration EOF STAGE0_DATE=`jq .compiler.date ${DEV_ROOT}/rust/src/stage0.json --raw-output` STAGE0_VERSION=`jq .compiler.version ${DEV_ROOT}/rust/src/stage0.json --raw-output` STAGE0_COMMIT_HASH=`curl -s "https://static.rust-lang.org/dist/${STAGE0_DATE}/channel-rust-${STAGE0_VERSION}.toml" | tomlq .pkg.rust.git_commit_hash --raw-output` echo "@Subdir stage0" >> cipd.ensure echo "fuchsia/third_party/rust/host/\${platform} git_revision:${STAGE0_COMMIT_HASH}" >> cipd.ensure echo "fuchsia/third_party/rust/target/${HOST_TRIPLE} git_revision:${STAGE0_COMMIT_HASH}" >> cipd.ensure $DEV_ROOT/infra/fuchsia/prebuilt/tools/cipd ensure --root $DEV_ROOT --ensure-file cipd.ensure
您可以视需要下载 Fuchsia 构建的 step0 编译器,但这对于在 CI 中重新创建 build 非常有用。如果 level0 不可用,您可以指示 Rust build 下载并使用上游 step0 编译器,方法是从
cipd.ensure
文件中省略这些行,并移除以下generate_config.py
中的--stage0
参数。
为 Fuchsia 配置 Rust
- 切换到您的 Rust 目录。
运行以下命令,为 Rust 工具链生成配置:
DEV_ROOT=DEV_ROOT
$DEV_ROOT/infra/fuchsia/prebuilt/tools/vpython3 \ $DEV_ROOT/infra/fuchsia/recipes/recipes/rust_toolchain.resources/generate_config.py \ config_toml \ --clang-prefix=$DEV_ROOT/clang \ --host-sysroot=$DEV_ROOT/sysroot/linux \ --stage0=$DEV_ROOT/stage0 \ --prefix=$(pwd)/install/fuchsia-rust \ | tee fuchsia-config.toml
$DEV_ROOT/infra/fuchsia/prebuilt/tools/vpython3 \ $DEV_ROOT/infra/fuchsia/recipes/recipes/rust_toolchain.resources/generate_config.py \ environment \ --eval \ --clang-prefix=$DEV_ROOT/clang \ --sdk-dir=$DEV_ROOT/sdk \ --stage0=$DEV_ROOT/stage0 \ --linux-sysroot=$DEV_ROOT/sysroot/linux \ --linux-riscv64-sysroot=$DEV_ROOT/sysroot/focal \ | tee fuchsia-env.sh
(可选)运行以下命令,指示 git 忽略生成的文件:
echo fuchsia-config.toml >> .git/info/exclude
echo fuchsia-env.sh >> .git/info/exclude
(可选)自定义
fuchsia-config.toml
。
构建和安装 Rust
- 切换到您的 Rust 源目录。
运行以下命令以构建并安装 Rust 以及 Fuchsia 运行时规范:
DEV_ROOT=DEV_ROOT
rm -rf install/fuchsia-rust mkdir -p install/fuchsia-rust
# Copy and paste the following subshell to build and install Rust, as needed. # The subshell avoids polluting your environment with fuchsia-specific rust settings. ( source fuchsia-env.sh && ./x.py install --config fuchsia-config.toml \ --skip-stage0-validation ) && \ rm -rf install/fuchsia-rust/lib/.build-id && \ $DEV_ROOT/infra/fuchsia/prebuilt/tools/vpython3 \ $DEV_ROOT/infra/fuchsia/recipes/recipes/rust_toolchain.resources/generate_config.py \ runtime \ | $DEV_ROOT/infra/fuchsia/prebuilt/tools/vpython3 \ $DEV_ROOT/infra/fuchsia/recipes/recipe_modules/toolchain/resources/runtimes.py \ --dir install/fuchsia-rust/lib \ --dist dist \ --readelf fuchsia-build/host/llvm/bin/llvm-readelf \ --objcopy fuchsia-build/host/llvm/bin/llvm-objcopy \ > install/fuchsia-rust/lib/runtime.json
仅构建(可选)
如果您想跳过安装步骤(例如在 Rust 本身的开发期间),可以使用以下命令。
( source fuchsia-env.sh && ./x.py build --config fuchsia-config.toml \
--skip-stage0-validation )
问题排查
如果您遇到构建错误,请尝试删除 Rust 构建目录:
rm -rf fuchsia-build
然后重新运行该命令以构建 Rust。
使用自定义 Rust 工具链构建 Fuchsia
有了新编译的自定义 Rust 工具链,您就可以使用它来构建 Fuchsia 了。如需了解如何执行此操作,请参阅专用指南。