Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build.rs should ignore RUSTFLAGS #6375

Open
polachok opened this issue Dec 4, 2018 · 7 comments
Open

build.rs should ignore RUSTFLAGS #6375

polachok opened this issue Dec 4, 2018 · 7 comments
Labels
A-build-scripts Area: build.rs scripts A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables A-rustflags Area: rustflags S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

Comments

@polachok
Copy link

polachok commented Dec 4, 2018

Problem

build.rs is compiled with RUSTFLAGS if target is not specified.

$ RUSTFLAGS="-C target-cpu=nehalem" cargo build -vv
   Compiling test-build v0.1.0 (/Users/plhk/Documents/src/test-build)
     Running `rustc --crate-name build_script_build build.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=8a72131a02621b95 -C extra-filename=-8a72131a02621b95
 --out-dir /Users/plhk/Documents/src/test-build/target/debug/build/test-build-8a72131a02621b95 
-C incremental=/Users/plhk/Documents/src/test-build/target/debug/incremental
-L dependency=/Users/plhk/Documents/src/test-build/target/debug/deps -C target-cpu=nehalem`

The problem here is that if a user compiles for a newer CPU than they have, build script can potentially use newer instructions and fail with SIGILL.

Steps
1.cargo new test-build
2.cd test-build
3.touch build.rs
4.cargo build -vv

Possible Solution(s)
I don't have a solution, but it seems like env_args() is the problem.

Notes

Output of cargo version:

cargo 1.30.0 (a1a4ad3 2018-11-02)

@alexcrichton
Copy link
Member

As a workaround I'd recommend passing --target which will disable this behavior. Otherwise this is a well known issue covered by a number of other issues too: #3349, #4423, #5754

@elichai
Copy link

elichai commented Dec 1, 2019

I'm still getting error: lto can only be run for executables, cdylibs and static library outputs sometimes even when passing --target
example:
command: RUSTFLAGS+=' -Clto -Ctarget-cpu=native' cargo install --force cargo-crev --target x86_64-unknown-linux-gnu --verbose

output:

     Running `sccache rustc --crate-name openssl_sys /home/elichai2/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.53/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C metadata=80e051d4886f40ee -C extra-filename=-80e051d4886f40ee --out-dir /tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps -L dependency=/tmp/cargo-installplOv5W/release/deps --extern libc=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/liblibc-f9604e2813c04637.rmeta --cap-lints allow -Clto -Ctarget-cpu=native --cfg const_fn --cfg 'osslconf="OPENSSL_NO_SSL3_METHOD"' --cfg ossl101 --cfg ossl102 --cfg ossl102f --cfg ossl102h --cfg ossl110 --cfg ossl110f --cfg ossl110g --cfg ossl111 --cfg ossl111b --cfg ossl111c -l ssl -l crypto`
     Running `/tmp/cargo-installplOv5W/release/build/libssh2-sys-da404dcad10cbb4b/build-script-build`
     Running `/tmp/cargo-installplOv5W/release/build/openssl-9d46d869025e0183/build-script-build`
     Running `/tmp/cargo-installplOv5W/release/build/native-tls-768c28bd77a5ca54/build-script-build`
error: lto can only be run for executables, cdylibs and static library outputs

error: aborting due to previous error

error: could not compile `miscreant`.

Caused by:
  process didn't exit successfully: `sccache rustc --edition=2018 --crate-name miscreant /home/elichai2/.cargo/registry/src/github.com-1ecc6299db9ec823/miscreant-0.4.2/src/lib.rs --color never --crate-type rlib --crate-type staticlib --emit=dep-info,link -C opt-level=3 --cfg 'feature="aes"' --cfg 'feature="alloc"' --cfg 'feature="byteorder"' --cfg 'feature="default"' --cfg 'feature="soft-aes"' --cfg 'feature="std"' --cfg 'feature="stream"' -C metadata=eb7b7ea891b79345 -C extra-filename=-eb7b7ea891b79345 --out-dir /tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps -L dependency=/tmp/cargo-installplOv5W/release/deps --extern aes=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libaes-59bc26c48e377af1.rlib --extern aesni=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libaesni-b0a0a29efbc7f08e.rlib --extern byteorder=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libbyteorder-379311baed87428e.rlib --extern cmac=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libcmac-51c758923f362247.rlib --extern crypto_mac=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libcrypto_mac-c71872e5c18ce69b.rlib --extern ctr=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libctr-840349a39310199e.rlib --extern dbl=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libdbl-3fb84d3a157ed756.rlib --extern generic_array=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libgeneric_array-075965c8c4c8ace9.rlib --extern pmac=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libpmac-d83af01cf6d8859c.rlib --extern stream_cipher=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libstream_cipher-701a688a1df56cd4.rlib --extern subtle=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libsubtle-7d7c1d3bbdcf00ab.rlib --extern zeroize=/tmp/cargo-installplOv5W/x86_64-unknown-linux-gnu/release/deps/libzeroize-afe337c85ef76089.rlib --cap-lints allow -Clto -Ctarget-cpu=native` (exit code: 1)
warning: build failed, waiting for other jobs to finish...
error: failed to compile `cargo-crev v0.13.0`, intermediate artifacts can be found at `/tmp/cargo-installplOv5W`

Caused by:
  build failed

full output: https://pastebin.com/raw/0fPA4Mhz

@ehuss
Copy link
Contributor

ehuss commented Dec 1, 2019

@elichai It looks like miscreant specifies multiple crate types (rlib/staticlib). LTO doesn't support that. You need to specify lto in the Cargo profile so that Cargo will handle passing the flag at the correct time.

@elichai
Copy link

elichai commented Dec 1, 2019

@elichai It looks like miscreant specifies multiple crate types (rlib/staticlib). LTO doesn't support that. You need to specify lto in the Cargo profile so that Cargo will handle passing the flag at the correct time.

How can I do that without editing Cargo.toml? (i.e. I have a script that adds ' -Clto -Ctarget-cpu=native' to all of my installs)

@ehuss
Copy link
Contributor

ehuss commented Dec 1, 2019

Config profiles support that, but unfortunately they are unstable/unfinished.

CARGO_PROFILE_RELEASE_LTO=true cargo install foo -Zconfig-profile

@ehuss ehuss added the A-rustflags Area: rustflags label Apr 6, 2020
lu-zero added a commit to rust-av/rav1e that referenced this issue May 26, 2020
lu-zero added a commit to rust-av/rav1e that referenced this issue May 28, 2020
shssoichiro pushed a commit to xiph/rav1e that referenced this issue May 28, 2020
* Update nightly in CI

See rust-lang/rust#70175 and
rust-lang/cargo#6375

* Bump minimum rust version to 1.43.1

* Add rustdocflags

* Bump grcov version
lu-zero added a commit to rust-av/rav1e that referenced this issue May 28, 2020
* Update nightly in CI

See rust-lang/rust#70175 and
rust-lang/cargo#6375

* Bump minimum rust version to 1.43.1

* Add rustdocflags

* Bump grcov version
lu-zero added a commit to xiph/rav1e that referenced this issue May 28, 2020
* Update nightly in CI

See rust-lang/rust#70175 and
rust-lang/cargo#6375

* Bump minimum rust version to 1.43.1

* Add rustdocflags

* Bump grcov version
toku-sa-n added a commit to toku-sa-n/antei that referenced this issue Jun 29, 2021
To avoid to compile `build.rs` wrongly. Without this it will be compiled
with `kernel.ld` as specified in `.cargo/config.toml`. See rust-lang/cargo#6375 (comment).
toku-sa-n added a commit to toku-sa-n/antei that referenced this issue Jun 30, 2021
Without specifying a target, `build.rs` will also be compiled with the
same codegen options. See: rust-lang/cargo#6375 (comment)
toku-sa-n added a commit to toku-sa-n/antei that referenced this issue Jun 30, 2021
Without specifying a target, `build.rs` will also be compiled with the
same codegen options. See: rust-lang/cargo#6375 (comment)
@andrewbaxter
Copy link

andrewbaxter commented Feb 7, 2023

For anyone else who hits the issue, the workaround is to

  1. Specify RUSTFLAGS as an environment variable when invoking cargo and
  2. Specify --target, which disables using the env var for build.rs (it will be used for building the binary).

Note that this does not work with rustflags = [...] in cargo/config.toml AFAICT, it must be done with the environment variable.

@mert-kurttutan
Copy link

mert-kurttutan commented Jun 7, 2023

Here I am putting a minimal example I have that gives this problem (that is reproducible on a Ubuntu machine, rustc rustc 1.70.0) in case someone finds it useful:

// build.rs                                                                         
fn main(){
    println!("Hello world!");
    let version = "rustc 1.70.0 1-(05".to_owned();
    let nightly = version.contains("dev");
    println!("End of world!");
}
// .cargo/config.toml
[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "target-feature=+avx512f"]

The problem goes away if you change version to

let version = "rustc 1.70.0 1-".to_owned();

@epage epage added the S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. label Oct 25, 2023
link2xt added a commit to deltachat/deltachat-ios that referenced this issue Apr 4, 2024
RUSTFLAGS is passed to build.rs scripts
and results in
error: lto can only be run for executables, cdylibs and static library outputs

Upstream issue: <rust-lang/cargo#6375>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-scripts Area: build.rs scripts A-configuration Area: cargo config files and env vars A-environment-variables Area: environment variables A-rustflags Area: rustflags S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Projects
None yet
Development

No branches or pull requests

7 participants