cargo sets a number of environment variables when running build scripts, including HOST and TARGET. When running a build script that is used by a crate that's being built for the host (because it's being used by another build script, say) it just gets HOST == TARGET. Similar to #5754, this can cause issues when a build script wants to compile C code. In a non-cross-compile, there's no useful way to communicate to the cc crate that a certain compiler and CFLAGS should be used for host compilation vs. target compilation. The canonical example is address sanitizer. For asan builds we have -fsanitize=address in CFLAGS, but we don't want to pass that when compiling objects that are linked into build scripts.
I ran into this problem while fixing the Firefox build to pass our compiler and flags to cargo so that build scripts using cc would compile their sources properly. Specifically, the libloading crate compiles a C source file on Linux. libloading is used by the clang-sys crate for runtime loading of libclang, and clang-sys is used by bindgen as part of the stylo build script, so libloading should compile its source file with the host compiler and flags. I was unable to find a workable solution so I just added some hacks to not pass the compiler and flags for our asan builds, which is not great.
Ideally cargo would set an environment variable for build scripts to indicate this scenario, like TARGET_IS_HOST or BUILDING_FOR_HOST (I don't care about the specifics of the name) then the cc crate could detect this and provide some way to set the compiler and cflags separately for this case.
cargo sets a number of environment variables when running build scripts, including
HOSTandTARGET. When running a build script that is used by a crate that's being built for the host (because it's being used by another build script, say) it just getsHOST == TARGET. Similar to #5754, this can cause issues when a build script wants to compile C code. In a non-cross-compile, there's no useful way to communicate to thecccrate that a certain compiler andCFLAGSshould be used for host compilation vs. target compilation. The canonical example is address sanitizer. For asan builds we have-fsanitize=addressinCFLAGS, but we don't want to pass that when compiling objects that are linked into build scripts.I ran into this problem while fixing the Firefox build to pass our compiler and flags to cargo so that build scripts using
ccwould compile their sources properly. Specifically, the libloading crate compiles a C source file on Linux.libloadingis used by theclang-syscrate for runtime loading of libclang, andclang-sysis used bybindgenas part of the stylo build script, so libloading should compile its source file with the host compiler and flags. I was unable to find a workable solution so I just added some hacks to not pass the compiler and flags for our asan builds, which is not great.Ideally cargo would set an environment variable for build scripts to indicate this scenario, like
TARGET_IS_HOSTorBUILDING_FOR_HOST(I don't care about the specifics of the name) then thecccrate could detect this and provide some way to set the compiler and cflags separately for this case.