我想覆盖一个构建脚本,这意味着添加一个如下所示的配置部分:
[target.x86_64-unknown-linux-gnu.foo]
rustc-link-search = ["/path/to/foo"]
rustc-link-lib = ["foo"]
root = "/path/to/foo"
key = "value"
Run Code Online (Sandbox Code Playgroud)
但是我使用的是 Mac,所以x86_64-unknown-linux-gnu不是正确的目标三元组。
我如何发现当前正在使用的目标三重 rustc 或货物?
rustc --print cfg打印似乎与三元组不对应的值列表(unknown特别是那里没有)。
rustc --print target-list显示所有可用目标;我只想要默认值。
gau*_*teh 12
基于@konstin 的回答:
$ rustc -vV | sed -n 's|host: ||p'
Run Code Online (Sandbox Code Playgroud)
这会给你类似的东西:
x86_64-unknown-linux-gnu
Run Code Online (Sandbox Code Playgroud)
货物用于rustc -vV检测默认目标三元组(源)。我们可以做同样的事情:
use std::process::Command;
use anyhow::{format_err, Context, Result};
use std::str;
fn get_target() -> Result<String> {
let output = Command::new("rustc")
.arg("-vV")
.output()
.context("Failed to run rustc to get the host target")?;
let output = str::from_utf8(&output.stdout).context("`rustc -vV` didn't return utf8 output")?;
let field = "host: ";
let host = output
.lines()
.find(|l| l.starts_with(field))
.map(|l| &l[field.len()..])
.ok_or_else(|| {
format_err!(
"`rustc -vV` didn't have a line for `{}`, got:\n{}",
field.trim(),
output
)
})?
.to_string();
Ok(host)
}
fn main() -> Result<()> {
let host = get_target()?;
println!("target triple: {}", host);
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
rustc --print cfg 将输出如下内容:
$ rustc --print cfg
debug_assertions
target_arch="x86_64"
target_endian="little"
target_env="gnu"
target_family="unix"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_os="linux"
target_pointer_width="64"
target_vendor="unknown"
unix
Run Code Online (Sandbox Code Playgroud)
目标是<arch>-<vendor>-<os>-<env>。
使用足够新的 rustc 编译器:
$ rustc -Z unstable-options --print target-spec-json | grep llvm-target
Run Code Online (Sandbox Code Playgroud)