在 Fuchsia 上使用货运服务

Rust 生态系统中的许多工具都假定您使用的是 cargo。对于此类工具(在我们的 build 中没有对应的工具),可以使用一个用于生成 Cargo.toml 文件的实用程序。您可以使用它运行 cargo 本身(例如 cargo check)。

此功能由志愿者维护。GN 和 Cargo 存在一些设计不匹配问题,这可能会导致生成的 Cargo.toml 文件根本不起作用或需要手动调整。因此,Fuchsia 树中的货物不受官方支持;可能会不时发生故障。

正在生成 Cargo.toml 个文件

如需根据 GN 的 build 图生成 Cargo 文件,请将 --cargo-toml-gen 添加到 fx set 命令,或将 //build/rust:cargo_toml_gen 目标添加到 $OUT_DIR/args.gnuniverse_package_labels。这会使 gn gen 增加几秒钟。更新设置后,请务必运行完整的 fx build

fx set PRODUCT.BOARD --cargo-toml-gen <other fx args>
fx build

注意:如果需要使用 Cargo.toml 才能完成 fx build(例如在需要运行 cbindgen 为 Rust crate 生成新的 C 绑定时),您可能需要改用 //build/rust:cargo_toml_gen 构建目标。此目标将仅构建 Cargo.toml 文件。

大多数编辑器都要求将 Cargo.toml 文件放在靠近 src/ 目录的位置。您可以使用以下命令生成这些文件的符号链接,其中 //foo/path/to/target:some_label 是要处理的 GN 目标:

fx gen-cargo foo/path/to/target:some_label

注意:上述代码不适用于 rustc_staticlib 目标,例如,用于生成 C 绑定的 Rust crate 通常使用 rustc_staticlib 目标模板。对于 rustc_staticlib 目标,应改用以下命令。

fx gen-cargo foo/path/to/target:_some_label_rustc_static

请注意,此标签必须指向 rustc_... GN 模板(而不是 Fuchsia 软件包或其他 GN 目标)。例如:

rustc_binary("some_label") {
   ...
}

生成 .cargo/config 文件

某些插件需要 .cargo/config 文件才能允许 Fuchsia 正确运行 cargo(例如运行 cargo check)。为了轻松生成此文件,请使用 fargo 工具。

  1. 安装 rustup
  2. 运行以下命令,将 rustup 配置为使用 Fuchsia Rust 工具链:

    rustup toolchain link fuchsia $($FUCHSIA_DIR/scripts/youcompleteme/paths.py VSCODE_RUST_TOOLCHAIN)
    rustup default fuchsia
    
  3. 按照 Fago 的入门说明克隆并安装 $FUCHSIA_DIR 中的 fargo 工具。

  4. 创建配置:

    cd $FUCHSIA_DIR && fargo write-config
    # Note the caveats about changing architecture in the fargo readme
    # https://fuchsia.googlesource.com/fargo/#creating-a-cargo_config