2
votes

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

1 Answers

3
votes

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