cargo-ndk: CFLAGS getting ignored

Hello, because of the recent PR #115, CFLAGS from environment variables get ignored.

My specific use case / possible way to reproduce: Add fdk-aac-sys = "0.5.0" as dependency. It links to internal Android libraries so it fails to build, however if you run export CFLAGS="-U__ANDROID__" before cargo ndk it will successfully compile. This works fine in 3.2.0, but breaks in 3.2.1.

Thank you.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 17 (11 by maintainers)

Most upvoted comments

Ah, and of course I hit send without finishing my thoughts, heh.

Insofar as allowing people to completely override the flags we now rely on to pass --target to clang, I feel like that should be a CLI flag/CARGO_NDK_ env var to override the behaviour, because it’s not something I particularly want to see bug reports about.

I propose we just take the logic wholesale from here, and prepend our target to the result if it exists: https://github.com/rust-lang/cc-rs/blob/f074cd9900ab76454d25b52dc9bb9fbd4a0e0e47/src/lib.rs#L3198

yep I just mentioned quoting as a general concern with dealing with cflags - I didn’t really mean to suggest that we should try and do anything fancy about that ourselves - I was also just assuming we’d concatenate with a space.

I think the only real difference from what you just said was that I was thinking we may as well switch from setting <var>_<target> to setting <var>_<target_with_underscores>. It doesn’t make much difference but it does then give users the ability to set <var>_<target> and completely override what cargo ndk passes.

To fdk-aac-sys - I made a PR, but I don’t seem to find the changes in latest release. Anyway in my project it’s dependency of dependency of dependency of dependency of dependency of dependency… So I have to depend on older version for now and this workaround works.

Anyway - my whole point was, ignore the whole __ANDROID__ thing, that was an example of my use case. CFLAGS worked perfectly fine in 3.2.0, in 3.2.1 they stopped working as intended. Don’t you think this is more of a breaking change? Shouldn’t this be fixed for purposes other than __ANDROID__?

The cc crate checks a number of different alternatives with the following precedence:

  1. <var>_<target> - for example, CC_x86_64-unknown-linux-gnu
  2. <var>_<target_with_underscores> - for example, CC_x86_64_unknown_linux_gnu
  3. <build-kind>_<var> - for example, HOST_CC or TARGET_CFLAGS
  4. <var> - a plain CC, AR as above.

so yeah a plain CFLAGS would have the lowest precedence here, and also since we don’t currently merge you can’t set CFLAGS_<target> currently.

Maybe cargo-ndk could aim to set with <var>_<target_with_underscores> and also merge with any existing value for <var>_<target_with_underscores> so then you have the option of completely overriding what cargo-ndk or merging with what cargo ndk passes.