如何为目标设置Cargo的不稳定选项?

huangapple go评论59阅读模式
英文:

How to set cargo unstable options for a target?

问题

我有一个`.cargo/config.toml`文件,其中包含以下内容:

```toml
[unstable]
unstable-options = true
build-std = ["core", "alloc"]

但我只需要为一个目标(例如thumbv7em-none-eabihf)设置build-std。有没有办法__专门为一个目标声明它__?

可能是这样的:

# 错误的示例,显然不起作用
[target.thumbv7em-none-eabihf.unstable]
build-std = ["core", "alloc"]

请注意,我只在谈论配置文件,我知道如何使用cargo执行参数进行配置。这是为了自动忽略除了提到的目标之外的目标,例如默认的主机目标。


<details>
<summary>英文:</summary>

I have `.cargo/config.toml` with following:

```toml
[unstable]
unstable-options = true
build-std = [&quot;core&quot;, &quot;alloc&quot;]

But I need build-std only for one target (e.g. thumbv7em-none-eabihf).

Is there any way to declare it specifically for a target?

It could be something like this:

# wrong example, doesn&#39;t work apparently
[target.thumbv7em-none-eabihf.unstable]
build-std = [&quot;core&quot;, &quot;alloc&quot;]

Note, I’m talking about configs only, I know how to configure it with cargo execution arguments. That’s needed to automatically ignore build-std for targets other then mentioned, e.g. default host target.

答案1

得分: 3

截至2023年07月04日,仅使用内置的Cargo配置无法实现此目标。有关详细信息,请参阅背景部分。

解决方法

您需要使用外部调用Cargo来构建该目标,例如对于“core”和“alloc”。

cargo build --target thumbv7em-none-eabihf -Z build-std="core,alloc"

使用安装了相关工具链和此测试程序进行测试:

#![feature(restricted_std)]

fn main() {
    loop {}
}

使用Cargo调用:

cargo build --target thumbv7em-none-eabihf -Z build-std="std,panic_abort"

背景

经过一些实验和搜索,Cargo有一个开放的问题以添加此功能这里,但进展似乎很慢,因为已经尝试了多种不同的方法。由于build-std仍然是一个不稳定的接口,仅在Cargo的夜间版本中可用,因此仍然存在与其他功能冲突,以及与更改语义相关的一些开放已关闭 PR。此类功能的跟踪问题在这里

英文:

As of 2023-07-04 this is not possible to achieve using only the built-in cargo configuration. See the Background section for details.

Workaround

You will need to build with an external invocation of cargo for that target e.g. for "core" and "alloc"

cargo build --target thumbv7em-none-eabihf -Z build-std=&quot;core,alloc&quot;

Tested with the relevant toolchain installed and this test program:

#![feature(restricted_std)]

fn main() {
    loop {}
}

with cargo invocation

cargo build --target thumbv7em-none-eabihf -Z build-std=&quot;std,panic_abort&quot;

Background

After some experimentation and searching, there is an open issue for cargo to add this feature here but progress appears to be slow as multiple different approaches have been explored. As build-std is still an unstable interface, available only in nightly versions of cargo, there are still conflicts with other features and some open as well as closed PRs related to changing the semantics. The tracking issue for this kind of feature is here.

huangapple
  • 本文由 发表于 2023年6月29日 19:47:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定