Skip to content

Indicate to build scripts that the crate they are being run for is being built for the host #5755

@luser

Description

@luser

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-build-scriptsArea: build.rs scriptsS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions