5

使用 gcc 时,我可以通过运行gcc -dumpmachine. 在我当前的系统上,这给了我x86_64-linux-gnu.

我怎样才能稳定rustc打印我的主机三倍?(x86_64-unknown-linux-gnu在这种情况下)

rustc的文档似乎不包含除--printand之外的任何相关内容--version。似乎都没有产生宿主目标三倍。

澄清:到目前为止,关于 nightly 给出了两个答案,我想强调这个问题专门针对稳定 rustc的编译器。

4

3 回答 3

3

使用 Rust nightly,您可以打印“目标规范 JSON”:

$ rustc +nightly -Z unstable-options --print target-spec-json
{
  "arch": "x86_64",
  "cpu": "x86-64",
  "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
  "dynamic-linking": true,
  "env": "gnu",
  "executables": true,
  "has-elf-tls": true,
  "has-rpath": true,
  "is-builtin": true,
  "linker-flavor": "gcc",
  "linker-is-gnu": true,
  "llvm-target": "x86_64-unknown-linux-gnu",
  "max-atomic-width": 64,
  "os": "linux",
  "position-independent-executables": true,
  "pre-link-args": {
    "gcc": [
      "-Wl,--as-needed",
      "-Wl,-z,noexecstack",
      "-m64"
    ]
  },
  "relro-level": "full",
  "stack-probes": true,
  "target-c-int-width": "32",
  "target-endian": "little",
  "target-family": "unix",
  "target-pointer-width": "64",
  "vendor": "unknown"
}

要在命令行上从中解析目标三元组,您可以使用如下工具jq

$ rustc +nightly -Z unstable-options --print target-spec-json | jq -r '."llvm-target"'
x86_64-unknown-linux-gnu

这还不稳定(因此需要-Z unstable-options编译器选项),但它可能会在未来出现。该功能是在#38061中添加的。

于 2020-09-01T12:19:22.893 回答
2
rustc --version --verbose

会给你一些输出,比如:

rustc 1.35.0-nightly (474e7a648 2019-04-07)
binary: rustc
commit-hash: 474e7a6486758ea6fc761893b1a49cd9076fb0ab
commit-date: 2019-04-07
host: x86_64-unknown-linux-gnu
release: 1.35.0-nightly
LLVM version: 8.0

主机是您的目标三重奏。

于 2020-09-13T01:36:41.533 回答
1

rustc(从版本 1.45.2 开始)似乎没有提供任何方法来获得主机目标三倍。

作为一种解决方法,我决定使用strace一个小的虚拟程序来欺骗编译器显示主机三元组;rustc根据平台三元组将预编译的库存储在单独的目录中。

这就是我最终的结果:

strace -f -e trace=file rustc <(echo "fn main(){};") 2>&1 | \
   grep -E "(libstd-.*\.rlib)|(libcore-.*\.rlib)" | \
   sed -E  "s:^.*\"/.*/([^/]+-[^/]+(-[/^]+(-[^/]+)?)?)/.*\".*$:\1:g;t;d" | \
   sort | uniq

它可以像您期望的那样工作,在 64 位 Linux 系统上安装 ABI rust x86_64-unknown-linux-gnugnu

理论上,这应该适用于任何版本的 rustc。我可以确认它适用于 rustc 1.43.0(Ubuntu 18.04 附带)和 1.45.2。

以下是使用此命令时需要注意的一些注意事项:

  1. 此命令需要 bash,因为我正在使用该<(..)构造。可以修改此命令以使用显式临时文件,但会增加复杂性。
  2. 如果编译器尝试访问主机以外平台的文件,此命令将生成多个目标三元组。这是故意的。如果这是一个问题,您可以采用基于启发式的方法,使用uniq(with --count), sort, 并head选择访问次数最多的三元组。
  3. 用于过滤三元组的正则表达式并不像我想要的那样严格。如果三元组遵循正式规范,它可能会进一步缩小。如果我设法找到一些时间,我会稍后更新。

干杯!

于 2020-09-01T12:19:29.777 回答