為 Fuchsia 建構自訂 Rust 工具鍊

本指南說明如何建構與 Fuchsia 搭配使用的 Rust 編譯器。這個 如果需要使用修補編譯器或編譯器建構 Fuchsia 自訂選項有時建構自訂 Rust 工具鍊不一定 使用不同的 Rust 版本建構 Fuchsia;看 使用自訂 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 和 ninja:

    sudo apt-get install cmake ninja-build
  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 工具鍊:

    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=$(sed -nr 's/^compiler_date=(.*)/\1/p' ${DEV_ROOT}/rust/src/stage0)
    STAGE0_VERSION
    =$(sed -nr 's/^compiler_version=(.*)/\1/p' ${DEV_ROOT}/rust/src/stage0)
    STAGE0_COMMIT_HASH
    =$( \
      curl
    -s "https://static.rust-lang.org/dist/${STAGE0_DATE}/channel-rust-${STAGE0_VERSION}.toml" \
     
    | python3 -c 'import tomllib, sys; print(tomllib.load(sys.stdin.buffer)["pkg"]["rust"]["git_commit_hash"])')
    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 無法使用,您可以指示 透過省略指令碼來下載和使用上游舞台編譯器 這些程式碼行,cipd.ensure並移除--stage0 傳送至下方 generate_config.py 的引數。

為 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 工具鍊,您可以開始使用該工具鍊建構 紫紅色。如需相關操作說明,請參閱這份專屬指南