為 Fuchsia 建構自訂 Rust 工具鍊

本指南說明如何建構 Rust 編譯器,與 Fuchsia 搭配使用。如果您需要使用修補的編譯器或使用自訂選項建構的編譯器,此方法非常實用。使用不同版本的 Rust 建構 Fuchsia 時,不一定需要建立自訂 Rust 工具鍊;詳情請參閱使用自訂 Rust 工具鍊建構 Fuchsia

必要條件

在為 Fuchsia 建構自訂 Rust 工具鍊之前,您需要先完成下列事項:

  1. 如果您尚未複製 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
    
  2. 執行下列指令,安裝 cmake 和 jq/tomlq:

    sudo apt-get install cmake ninja-build jq yq
    
  3. 執行下列指令以取得基礎架構來源:

    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 \
    )
    
  4. 執行下列指令,使用 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 建構的階段 0 編譯器,但在 CI 中重新建立建構作業時相當實用。如果舞台 0 無法使用,您可以指示 Rust 建構作業下載及使用上游階段 0 編譯器,方法是從 cipd.ensure 檔案中省略這些行,並移除以下 generate_config.py--stage0 引數。

為 Fuchsia 設定 Rust

  1. 變更到 Rust 目錄。
  2. 執行下列指令,產生 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
    
  3. (選用) 執行下列指令,指示 Git 忽略產生的檔案:

    echo fuchsia-config.toml >> .git/info/exclude
    echo fuchsia-env.sh >> .git/info/exclude
    
  4. (選用) 自訂 fuchsia-config.toml

建構及安裝 Rust

  1. 變更到 Rust 來源目錄。
  2. 執行下列指令,建構並安裝 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 工具鍊,可以用來建構 Fuuchsia。如需相關操作說明,請參閱專屬指南