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

-C link-arg is being ignored... maybe just in this specific case? #9089

Open
saurik opened this issue Jan 19, 2021 · 9 comments
Open

-C link-arg is being ignored... maybe just in this specific case? #9089

saurik opened this issue Jan 19, 2021 · 9 comments
Labels
A-configuration Area: cargo config files and env vars A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-bug Category: bug

Comments

@saurik
Copy link

saurik commented Jan 19, 2021

I'm on macOS (64-bit 10.14.6, if you care) and I'm compiling to x86_64-apple-darwin. I'm trying (and very much failing) to use CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS to set -C link-arg= to something I need to be passed to the compiler for it to choose the correct linker (I'm using a version of clang that selects its own version of ld unless I override it to Apple's ld64 with -fuse-ld=/usr/bin/ld). While CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER does seem to override the linker, and while I know my rustflags are at least being parsed (as if I try to -C something invalid it throws an immediate error), -C link-args is being ignored, which makes me sad :(. (FWIW, using bare RUSTFLAGS doesn't seem to help.)

$ cargo --version
cargo 1.49.0 (d00d64df9 2020-12-05)
$ rustc --version
rustc 1.49.0 (e1884a8e3 2020-12-29)

Here is the repository I happen to be working with:

$ git clone https://github.com/brave-intl/challenge-bypass-ristretto-ffi.git
Cloning into 'challenge-bypass-ristretto-ffi'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 320 (delta 0), reused 0 (delta 0), pack-reused 314
Receiving objects: 100% (320/320), 100.41 KiB | 952.00 KiB/s, done.
Resolving deltas: 100% (174/174), done.
$ cd challenge-bypass-ristretto-ffi

This should fail with an error on --invalid from clang:

$ git clean -fxd && CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS='-C link-arg=--invalid' cargo build --verbose --lib --release --target x86_64-apple-darwin -j1
Removing target/
   Compiling typenum v1.12.0
     Running `rustc --crate-name build_script_main /Users/saurik/.cargo/registry/src/github.com-1ecc6299db9ec823/typenum-1.12.0/build/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debug-assertions=off -C metadata=43e81e5495885b7c -C extra-filename=-43e81e5495885b7c --out-dir /Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c -L dependency=/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/deps --cap-lints allow`
   Compiling rand_core v0.4.2
...

I know the triple and CARGO_TARGET_* are "in play":

$ git clean -fxd && CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER=false cargo build --verbose --lib --release --target x86_64-apple-darwin -j1
Removing target/
   Compiling typenum v1.12.0
     Running `rustc --crate-name build_script_main /Users/saurik/.cargo/registry/src/github.com-1ecc6299db9ec823/typenum-1.12.0/build/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debug-assertions=off -C metadata=43e81e5495885b7c -C extra-filename=-43e81e5495885b7c --out-dir /Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c -C linker=false -L dependency=/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/deps --cap-lints allow`
error: linking with `false` failed: exit code: 1
  |
  = note: "false" "-m64" "-arch" "x86_64" "-L" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.0.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.1.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.10.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.11.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.12.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.13.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.14.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.15.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.2.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.3.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.4.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.5.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.6.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.7.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.8.rcgu.o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.build_script_main.6p3nhvh5-cgu.9.rcgu.o" "-o" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c/build_script_main-43e81e5495885b7c.3odpmtn7sqjnz0bt.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/deps" "-L" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-518979da66993550.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-f52081aedccc205f.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libobject-bad1c02788187328.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-6375d8dbf53aee9d.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libgimli-0ea8539778b0c0ab.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-64f30c6d69babb18.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-e27428d997fd2532.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-f02467a76e5a8054.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-9536efad7116d828.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-5ca171f6ec11d7a5.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-14b621861b38e51f.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-38f4a0c206512fa5.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-99bca6ace58141c5.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-72a66f4c97a4c0c8.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-e7cd9f0beed2fc0f.rlib" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: 

error: aborting due to previous error

error: could not compile `typenum`

Caused by:
  process didn't exit successfully: `rustc --crate-name build_script_main /Users/saurik/.cargo/registry/src/github.com-1ecc6299db9ec823/typenum-1.12.0/build/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debug-assertions=off -C metadata=43e81e5495885b7c -C extra-filename=-43e81e5495885b7c --out-dir /Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/build/typenum-43e81e5495885b7c -C linker=false -L dependency=/Users/saurik/qq/challenge-bypass-ristretto-ffi/target/release/deps --cap-lints allow` (exit code: 1)
@saurik saurik added the C-bug Category: bug label Jan 19, 2021
@ehuss
Copy link
Contributor

ehuss commented Jan 19, 2021

It seems to fail for me:

  = note: clang: error: unsupported option '--invalid'

Can you post the full output, particularly the line staring with Running `rustc --crate-name challenge_bypass_ristretto_ffi src/lib.rs …`

@saurik
Copy link
Author

saurik commented Jan 20, 2021

@ehuss That's a different crate... I appreciate that the build fails much later, when building the challenge_bypass_ristretto_ffi crate; but it should have already failed when building the build_script_main crate from typenum. I can provide a more complex scenario to show you why this matters, but it would involve you installing another copy of clang (specifically, the r22 build of the Android NDK). Essentially, you are just checking to see "does the command eventually fail?", but I'm carefully passing -j1 and clipping the output to show the specific case that is being handled incorrectly: compiling typenum.

@saurik
Copy link
Author

saurik commented Jan 20, 2021

@ehuss OK, so it hadn't really occurred to me to figure out how to compile just typenum before, but I found that package and can provide you a demonstration of the issue that doesn't rely on reading the output (so you can just check error codes).

Setup:

$ git clone https://github.com/paholg/typenum.git
Cloning into 'typenum'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 8411 (delta 7), reused 9 (delta 3), pack-reused 8390
Receiving objects: 100% (8411/8411), 6.61 MiB | 8.05 MiB/s, done.
Resolving deltas: 100% (4469/4469), done.
$ cd typenum

This should fail, but doesn't:

$ git clean -fxd && CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS='-C link-arg=--invalid' cargo build --verbose --lib --release --target x86_64-apple-darwin -j1
Removing Cargo.lock
Removing target/
   Compiling typenum v1.12.0 (/Users/saurik/qq/typenum)
     Running `rustc --crate-name build_script_main build/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debug-assertions=off -C metadata=bb2c890b5c8a0400 -C extra-filename=-bb2c890b5c8a0400 --out-dir /Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400 -L dependency=/Users/saurik/qq/typenum/target/release/deps`
     Running `/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build-script-main`
     Running `rustc --crate-name typenum src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=109d03bbaa262ad5 -C extra-filename=-109d03bbaa262ad5 --out-dir /Users/saurik/qq/typenum/target/x86_64-apple-darwin/release/deps --target x86_64-apple-darwin -L dependency=/Users/saurik/qq/typenum/target/x86_64-apple-darwin/release/deps -L dependency=/Users/saurik/qq/typenum/target/release/deps -C link-arg=--invalid`
    Finished release [optimized] target(s) in 8.06s
$ 

To demonstrate that it is, in fact, linking something (and should fail), here is a demonstration of it running the linker:

$ git clean -fxd && CARGO_TARGET_X86_64_APPLE_DARWIN_LINKER=false cargo build --verbose --lib --release --target x86_64-apple-darwin -j1
Removing Cargo.lock
Removing target/
   Compiling typenum v1.12.0 (/Users/saurik/qq/typenum)
     Running `rustc --crate-name build_script_main build/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debug-assertions=off -C metadata=bb2c890b5c8a0400 -C extra-filename=-bb2c890b5c8a0400 --out-dir /Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400 -C linker=false -L dependency=/Users/saurik/qq/typenum/target/release/deps`
error: linking with `false` failed: exit code: 1
  |
  = note: "false" "-m64" "-arch" "x86_64" "-L" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.0.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.1.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.10.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.11.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.12.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.13.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.14.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.15.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.2.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.3.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.4.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.5.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.6.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.7.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.8.rcgu.o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.build_script_main.3pu25wo0-cgu.9.rcgu.o" "-o" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400" "/Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400/build_script_main-bb2c890b5c8a0400.4cnvtz39fhlds35l.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/saurik/qq/typenum/target/release/deps" "-L" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-518979da66993550.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-f52081aedccc205f.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libobject-bad1c02788187328.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-6375d8dbf53aee9d.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libgimli-0ea8539778b0c0ab.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-64f30c6d69babb18.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-e27428d997fd2532.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-f02467a76e5a8054.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-9536efad7116d828.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-5ca171f6ec11d7a5.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-14b621861b38e51f.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-38f4a0c206512fa5.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-99bca6ace58141c5.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-72a66f4c97a4c0c8.rlib" "/Users/saurik/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-e7cd9f0beed2fc0f.rlib" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: 

error: aborting due to previous error

error: could not compile `typenum`

Caused by:
  process didn't exit successfully: `rustc --crate-name build_script_main build/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debug-assertions=off -C metadata=bb2c890b5c8a0400 -C extra-filename=-bb2c890b5c8a0400 --out-dir /Users/saurik/qq/typenum/target/release/build/typenum-bb2c890b5c8a0400 -C linker=false -L dependency=/Users/saurik/qq/typenum/target/release/deps` (exit code: 1)
$ 

I don't really understand anything at all about what Cargo is trying to do here, but I will note that by like, pattern analysis, the -C link-arg= doesn't seem to be being passed to that build_script_main invocation of rustc (and if I run it manually and pass it through myself the build then, correctly, fails).

@saurik
Copy link
Author

saurik commented Jan 20, 2021

FWIW, I would have been willing to believe that I should instead be using CARGO_BUILD_RUSTFLAGS--though, I'd argue that that is confusing, given that it is using CARGO_TARGET_..._LINKER, and so I'd expect the semantics to match--but that also doesn't work... even RUSTFLAGS (which I would have expected to be used unconditionally) doesn't work :(.

@ehuss
Copy link
Contributor

ehuss commented Jan 20, 2021

RUSTFLAGS are not passed to build scripts if you use the --target flag. Can you explain why you would need extra link args for that? In general, that shouldn't ever be necessary.

@saurik
Copy link
Author

saurik commented Jan 20, 2021

@ehuss My CARGO_TARGET_..._LINKER is "/path/to/a/special/clang" and I need to (minimally) pass "-fuse-ld=/usr/bin/ld" to make that copy of clang build things that run on x86_64-apple-darwin (I also should want to configure its sysroot and some paths, but I think it will at least work--if sort of wrongly--without those). If I don't pass -fuse-ld=, it doesn't link. The entire point of my passing extra link arguments is because I am saying "to compile for x86_64-apple-darwin, you need to pass these extra arguments" and I guess Cargo is deciding "well, except for build scripts, right?" and I'm saying "no: it doesn't matter what you are compiling... I am saying that if you want to compile for x86_64-apple-darwin, I need you to add these link arguments". I would agree that it usually wouldn't be necessary, as most people just use default compilers, but "in general" it certainly would be necessary, as the entire point of me being able to override the linker is to do something special.

@saurik
Copy link
Author

saurik commented Jan 20, 2021

FWIW, if you wanted to be more consistent about it (in an attempt to make building build scripts be unrelated to building for a target), then just don't use CARGO_TARGET_..._LINKER to build build scripts; at which point, if I want to do something extra strange there, maybe allow me to override a separate CARGO_BUILD_LINKER? (Though, I might not know what CARGO_BUILD_* is supposed to semantically mean, as I would have assumed it meant "build-time", and yet CARGO_BUILD_RUSTFLAGS isn't being honored, which must mean I don't know what "build" means.) Like, I feel the core problem here is that cargo is using my special/magic target-specific linker to build its build scripts without honoring my related/required special/magic target-specific configuration: CARGO_TARGET_* should either be fully honored or fully ignored.

@ehuss
Copy link
Contributor

ehuss commented Jan 20, 2021

Ah, I see. I didn't realize that the LINKER applied to build scripts, that sounds like possibly a bug. I think an issue is that it is somewhat rare to use --target $HOST, so people don't run into these issues too often.

Regardless, support for passing RUSTFLAGS to build scripts is tracked in #4423. There unfortunately isn't great support for controlling exactly where RUSTFLAGS gets applied.

@ehuss ehuss added A-configuration Area: cargo config files and env vars A-linkage Area: linker issues, dylib, cdylib, shared libraries, so labels Feb 13, 2021
@jameshilliard
Copy link
Contributor

Possible fix in #9322.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-configuration Area: cargo config files and env vars A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants