在 buildroot 中开发 Rust 应用程序(cargo build --offline --locked 出现问题)

问题描述 投票:0回答:1

我创建了一个新的 buildroot 包,它是一个用 Cargo 构建的 Rust 应用程序。

我遵循了 Cargo 包的 buildroot 手册,我的

.mk
非常简单:

MY_RUST_APP_VERSION = 1.1.0
MY_RUST_APP_SITE = $(BR2_EXTERNAL_PATH)/package/my-rust-app
MY_RUST_APP_SUBDIR = my-rust-app
MY_RUST_APP_SITE_METHOD = local

# Release build for smaller size and faster execution
# cargo already builds as --release by default in buildroot
#MY_RUST_APP_CARGO_BUILD_OPTS = --release

$(eval $(cargo-package))

当 Cargo.toml 没有添加依赖项时,它构建得很好。

但是,当我将第一个外部依赖项添加到

Cargo.toml
时,buildroot 无法下载并构建它:

[dependencies]
tracing = "0.1.40"

buildroot 输出:

error: no matching package named `tracing` found
location searched: registry `crates-io`
required by package `my-rust-app v1.1.0`
As a reminder, you're using offline mode (--offline) which can sometimes cause surprising resolution failures, if this error is too confusing you may wish to retry without the offline flag.

我看到 buildroot 默认使用

--offline
--locked
调用 Cargo,这使得向 Cargo.toml 添加新的板条箱变得更加困难。

如何删除这些默认标志?或者也许开发和构建本地 Rust 包的方法应该不同?

make my-rust-app-reconfigure
>>> my-rust-app 1.1.0 Building
[...]
__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS="nightly"
CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST="true" CARGO_TARGET_APPLIES_TO_HOST="false"
CARGO_BUILD_TARGET="armv7-unknown-linux-gnueabihf"
CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-buildroot-linux-gnueabihf-gcc
RUSTFLAGS="-Clink-arg=-Wl,--allow-multiple-definition"  cargo build --offline --release --manifest-path Cargo.toml --locked

在发布问题之前,我还尝试清除构建目录,希望它允许 Cargo 构建。然而,即使

make my-rust-app-dirclean && make my-rust-app-reconfigure
也无济于事。我想从
buildroot/dl/
删除下载,但找不到与 Rust 依赖项相关的任何内容。同时,我可以使用
cargo build
从我的包源目录成功构建。

10月26日更新: 仍在寻找可行的解决方案。

rust embedded-linux buildroot cargo
1个回答
2
投票

您面临的问题是由于 Buildroot 在调用 Cargo 时使用的

--offline
标志造成的。此标志会阻止 Cargo 访问互联网来下载新的包,这就是您在添加新依赖项时看到错误的原因。

要解决此问题,您需要在使用 Buildroot 构建包之前下载依赖项。具体方法如下:

  1. 首先,您需要生成一个包含所有依赖项的

    Cargo.lock
    文件。您可以通过在项目目录中运行
    cargo generate-lockfile
    来完成此操作。

  2. 然后,您需要下载依赖项。您可以通过在项目目录中运行

    cargo fetch
    来完成此操作。这将下载所有依赖项并将它们存储在您本地的 Cargo 注册表中。

  3. 现在,您需要告诉 Buildroot 使用本地 Cargo 注册表,而不是尝试从互联网下载 crate。您可以通过将

    CARGO_HOME
    环境变量设置为本地 Cargo 注册表的路径来完成此操作。您可以将此行添加到您的
    .mk
    文件中:

     MY_RUST_APP_ENV = CARGO_HOME=$(HOME)/.cargo
    
  4. 最后,您需要告诉 Buildroot 使用您之前生成的

    Cargo.lock
    文件。您可以通过将此行添加到您的
    .mk
    文件中来完成此操作:

     MY_RUST_APP_CARGO_ENV = CARGO_LOCKFILE_PATH=$(BR2_EXTERNAL_PATH)/package/my-rust-app/Cargo.lock
    

现在,当您使用 Buildroot 构建软件包时,它应该能够在本地 Cargo 注册表中找到所有依赖项并成功构建您的软件包。

请记住将

$(HOME)/.cargo
$(BR2_EXTERNAL_PATH)/package/my-rust-app/Cargo.lock
替换为系统上的实际路径。

© www.soinside.com 2019 - 2024. All rights reserved.