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

How to make cargo-c to always use the C compiler provided in CC #419

Open
yurivict opened this issue Oct 25, 2024 · 9 comments
Open

How to make cargo-c to always use the C compiler provided in CC #419

yurivict opened this issue Oct 25, 2024 · 9 comments
Labels
A-Cargo-problem A problem related to cargo itself F-Documentation Related to the documentation

Comments

@yurivict
Copy link

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?

@lu-zero
Copy link
Owner

lu-zero commented Oct 25, 2024

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?

@yurivict
Copy link
Author

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.

@lu-zero
Copy link
Owner

lu-zero commented Oct 25, 2024

CARGO_TARGET_<triple>_LINKER=the-compiler-you-want should take care of it.

@yurivict
Copy link
Author

yurivict commented Oct 25, 2024

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?

@lu-zero
Copy link
Owner

lu-zero commented Oct 26, 2024

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 :)

@yurivict
Copy link
Author

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.

@lu-zero
Copy link
Owner

lu-zero commented Oct 27, 2024

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.

@yurivict
Copy link
Author

yurivict commented Oct 27, 2024

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

@lu-zero lu-zero added F-Documentation Related to the documentation A-Cargo-problem A problem related to cargo itself labels Nov 4, 2024
@lu-zero
Copy link
Owner

lu-zero commented Nov 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Cargo-problem A problem related to cargo itself F-Documentation Related to the documentation
Projects
None yet
Development

No branches or pull requests

2 participants