How can I optionally pass rustc flags depending on a Cargo feature?

流过昼夜 提交于 2019-12-12 10:04:22

问题


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.


回答1:


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").



来源:https://stackoverflow.com/questions/50669828/how-can-i-optionally-pass-rustc-flags-depending-on-a-cargo-feature

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!