How to make cargo-c to always use the C compiler provided in CC
On FreeBSD cargo-c uses the "cc" executable, which is at odds with the ports' ability to override the C and C++ compilers.
How to make cargo-c to always use the CC environment variable?
cargo-c uses cargo's settings, so if you set the linker it would pick it. What does FreeBSD do for the normal cargo packages?
cargo is called with the environment like this:
CARGO_HOME=/usr/ports/shells/nushell/work/cargo-home CARGO_BUILD_JOBS=7 CARGO_TARGET_DIR=/usr/ports/shells/nushell/work/target RUSTC=/usr/local/bin/rustc RUSTDOC=/usr/local/bin/rustdoc RUSTFLAGS=" -C link-arg=-fstack-protector-strong" RUST_BACKTRACE=1 LIBGIT2_SYS_USE_PKG_CONFIG=1 LIBSSH2_SYS_USE_PKG_CONFIG=1 OPENSSL_LIB_DIR=/usr/lib OPENSSL_INCLUDE_DIR=/usr/include ZSTD_SYS_USE_PKG_CONFIG=1 CARGO_PROFILE_RELEASE_LTO="true" CARGO_PROFILE_RELEASE_PANIC="abort" CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
It might be that the linker isn't specifically set and cc is used in general.
We just noticed this while specifically looking at the cargo-c project.
CARGO_TARGET_<triple>_LINKER=the-compiler-you-want should take care of it.
CARGO_TARGET_<triple>_* and CARGO_BUILD_TARGET variables seem to trigger the cross-compilation mode.
How to pass a linker without enabling the cross-compilation mode?
You may set a config.toml https://doc.rust-lang.org/cargo/reference/config.html?highlight=linker#configuration-format that is potentially even nicer given how many settings you want to pass.
But this is not related to cargo-c itself so probably it is better moving to the cargo zulip :)
The cross-compilation option CARGO_TARGET_<triple>_LINKER=the-compiler-you-want works fine, but as I mentioned before we don't want cross-compilation in FreeBSD ports.
The option RUSTFLAGS="-Clinker=the_linker_you_want_to_use" is suggested here as a replacement of CARGO_TARGET_<triple>_LINKER=the-compiler-you-want.
Setting RUSTFLAGS="-Clinker=${CC}" however doesn't work the same way as CARGO_TARGET_<triple>_LINKER=${CC} with cargo-c for some reason.
Do you know why doesn't RUSTFLAGS="-Clinker=${CC}" work with cargo-c?
And again: how to make cargo-c to use the CC environment variable as a linker?
cargo-c uses the "cc" crate which does manipulate commands, it has "cc" and "CC" hard-coded.
You actually always "cross compile" when using clang or rustc.
And it gets even more important since cargo has a partial understanding on which flags go with the host build target (e.g. build.rs and proc macros) and which ones go with the target build target.
setting the blanket RUSTFLAGS has known issues. The simplest way is to setup a toml with all the targets populated.
setting the blanket RUSTFLAGS has known issues. The simplest way is to setup a toml with all the targets populated.
I only need to force a specific linker command. What should I add to config.toml for this?
The documentation linked above asks to add linker under [target.<triple>].
We don't have <triple>. We compile on the same platform and there is no <triple>.
You always have a triple.
I would suggest you to ask on the cargo zulip and solve it also for the rest of the rust packages.