rules_foreign_cc icon indicating copy to clipboard operation
rules_foreign_cc copied to clipboard

Allow unsetting compiler env vars

Open attilaolah opened this issue 2 years ago • 3 comments
trafficstars

When I pass certain environment variables via env = {…}, instead of being set directly, they will be appended to the existing variables provided by the C toolchain. These are variables like CFLAGS, ASFLAGS, etc.

I hit the problem where some projects, like x264, need a specific assembler. So I pass in my custom-built nasm as a dependency, but now they will still use the compiler toolchain's ASFLAGS with nasm, which doesn't understand all the llvm-as flags so it fails to assemble anything.

When I pass in any other ASFLAGS via env, they simply get appended. I'd like to have a way to overwrite these flags when necessary. Not sure what would be the most elegant way to do this; it could be a separate boolean flag, something like override_env, which, when set to True, would replace these env vars instead of merging them? This would preserve existing behaviour which is probably appropriate for most other use cases.

I'm open to other suggestions too.

attilaolah avatar Nov 04 '23 09:11 attilaolah

Interestingly, looks like there is an example file here that does exactly the same: https://github.com/bazelbuild/rules_foreign_cc/blob/main/examples/third_party/openssl/BUILD.openssl.bazel#L59-L61

~Not sure what I'm doing wrong here but it doesn't seem to work for me.~

Actually, that config uses configure_options, which is not possible in the case of x264.

attilaolah avatar Nov 04 '23 10:11 attilaolah

I ran into the same thing with libx264 (although in my case I was just allowing a system installed nasm to run) and I solved it by setting configure_prefix like this:

configure_make(
    name = "libx264",
    configure_prefix = "ASFLAGS=\"\" CFLAGS=\"\" CXXFLAGS=\"\"",
    lib_source = "@experimental_libx264",
)

I had to also clear CFLAGS as the default toolchain that was getting pulled in was setting some flags that were incompatible with the libx264 build.

voxeljorge avatar Mar 16 '24 17:03 voxeljorge

Nice, definitely simpler than the env_override parameter I concocted. Clearing CFLAGS is a bit unfortunate, you may be able to work around that by fiddling with copts or maybe customising your toolchain, but this should work too. (That said, this will probably invalidate things like -c opt for you.)

attilaolah avatar Mar 17 '24 11:03 attilaolah