如何根据 Cargo 功能选择性地传递 rustc 标志? [英] How can I optionally pass rustc flags depending on a Cargo feature?
问题描述
当将 -C target-cpu=native
标志传递给 rustc
时,我正在编写的程序运行得更快.我想给用户一个简单的、与平台无关的方式来在编译时启用它,所以我在 Cargo.toml 中添加了一个 Cargo 特性 cpu_native = []
并在我的项目中创建了这个 Cargo 配置:>
[target.'cfg(cpu_native)']rustflags = ["-C", "target-cpu=native"]
然而,这对我的程序没有影响,并且将 --features cpu_native
传递给 Cargo 甚至不会触发重新编译.更改为以下 Cargo 配置确实会强制使用更快的指令重新编译:
[构建]rustflags = ["-C", "target-cpu=native"]
但是,这将使用具有默认 Cargo 功能的 target-cpu=native
进行编译,这不是我想要的.从 Cargo 书中,我想要的似乎是可能的,但我看不出我做错了什么.
我认为不支持(还?).我增强了 Cargo 以打印出解析时检查的配置标志:
[名称(调试断言"),名称(proc_macro"),KeyPair("target_arch", "x86_64"),KeyPair("target_endian", "little"),KeyPair("target_env", ""),KeyPair("target_family", "unix"),KeyPair("target_os", "macos"),KeyPair("target_pointer_width", "64"),名称(unix"),]
<块引用>
[target.'cfg(cpu_native)']
这是 Cargo 功能的错误语法;它通常是 cfg(feature = "cpu_native")
.
The program I'm writing runs much faster when the -C target-cpu=native
flag is passed to rustc
. I want to give users a simple, platform-independent way to enable this when compiling, so I added a Cargo feature cpu_native = []
in Cargo.toml and created this Cargo config in my project:
[target.'cfg(cpu_native)']
rustflags = ["-C", "target-cpu=native"]
However, this has no effect on my program, and passing --features cpu_native
to Cargo does not even trigger a recompile. Changing to the following Cargo config does force re-compilation with faster instructions:
[build]
rustflags = ["-C", "target-cpu=native"]
However, this will compile with target-cpu=native
with the default Cargo features, which was not what I wanted. From the Cargo book, what I want seems to be possible, but I don't see what I'm doing wrong.
I don't think this is supported (yet?). I enhanced Cargo to print out what config flags are checked against when resolving:
[
Name("debug_assertions"),
Name("proc_macro"),
KeyPair("target_arch", "x86_64"),
KeyPair("target_endian", "little"),
KeyPair("target_env", ""),
KeyPair("target_family", "unix"),
KeyPair("target_os", "macos"),
KeyPair("target_pointer_width", "64"),
Name("unix"),
]
[target.'cfg(cpu_native)']
This is the incorrect syntax for a Cargo feature; it would normally be cfg(feature = "cpu_native")
.
这篇关于如何根据 Cargo 功能选择性地传递 rustc 标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!