Skip to content

Populate CFLAGS and CXXFLAGS when invoking cargo build script. #1081

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

Merged
merged 2 commits into from
Mar 10, 2022

Conversation

ddeville
Copy link
Contributor

@ddeville ddeville commented Jan 1, 2022

Downstream tools (in particular cc-rs) depend on them being set.

The particular use case that I noticed was broken was the macos deployment target (specified via the --macos_minimun_os Bazel option) wasn't being applied on binaries being built via cc-rs in a build.rs script. This was causing all sort of bugs such as symbols not being weakly linked since it was using the SDK version (the default) for the deployment target and bazel-built Rust binaries wouldn't load on older OS versions due to failures to resolve symbols at runtime, even though the deployment target was correctly set in Bazel.

@ddeville ddeville force-pushed the cargo-build-cflags branch 3 times, most recently from 1c7940d to aff27ba Compare January 1, 2022 08:46
@illicitonion
Copy link
Collaborator

Thanks for putting this together! It's pretty similar to #1004 but the differences are pretty interesting, so it's probably worth lingering on them for a moment:

  1. This one gets CFLAGS as well as CXXFLAGS - this seems good.
  2. This one gets env from C_COMPILE_ACTION_NAME and the other from CPP_COMPILE_ACTION_NAME - should we be getting one, the other, or both and somehow merging them?
  3. That one skips --sysroot because it's already set on the CC/CXX env vars. But maybe we can actually stop adding --sysroot to CC/CXX if it's set properly here? Or maybe we can always set it as part of CFLAGS/CXXFLAGS even if it's not set by the toolchain?

Either way, there's an open question for @hlopko - Do we have any prior art on wiring up a custom cc_toolchain in a unit analysis test, so that we could test that args get properly propagated from there?

(cc @ikalchev - sorry we've left your PR open for so long! I don't really have a preference for which PR we end up merging, but it'd be great to come to a consensus on the open questions above :))

@hlopko
Copy link
Member

hlopko commented Jan 21, 2022

We don't have an existing example, and creating even a fake cc toolchain is quite an endeavor (docs are here: https://docs.bazel.build/versions/main/cc-toolchain-config-reference.html, and I'd be surprised if they were up to date). I think I cannot ask anybody to try doing that without feeling like the pure evil. So I can commit to sending an PR with this fake cc toolchain next week. Hope that's quickly enough.

@ddeville ddeville force-pushed the cargo-build-cflags branch 2 times, most recently from 27dd302 to 953cce3 Compare February 8, 2022 21:15
@PiotrSikora
Copy link
Contributor

PiotrSikora commented Feb 10, 2022

I can confirm that this fixes some build issues for me. Thanks!

Copy link
Collaborator

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge this - we can address the c/cpp and sysroot questions in the future as/when needed :)

Sorry for the delay here!

Downstream tools (in particular cc-rs) depend on them being set.

The particular use case that I noticed was broken was the macos
deployment target (specified via the --macos_minimun_os Bazel option)
wasn't being applied on binaries being built via cc-rs in a build.rs
script. This was causing all sort of bugs such as symbols not being
weakly linked since it was using the SDK version (the default) for the
deployment target and bazel-built Rust binaries wouldn't load on older
OS versions due to failures to resolve symbols at runtime, even though
the deployment target was correctly set in Bazel.
@ddeville ddeville force-pushed the cargo-build-cflags branch from f513424 to b327469 Compare March 9, 2022 21:27
@illicitonion illicitonion merged commit 97fd329 into bazelbuild:main Mar 10, 2022
@ddeville ddeville deleted the cargo-build-cflags branch March 10, 2022 17:07
dae added a commit to ankitects/rules_rust that referenced this pull request Sep 25, 2022
PR bazelbuild#1081 started passing CFLAGS to build scripts, which cargo does
too. But unfortunately Bazel's cpp toolchain adds an explicit target to
the CFLAGS it provides, and that target uses the host platform instead
of the target platform. When building a crate like blake3 or zstd on a
Mac with iOS as the target, this ends up in the crate being built for
macOS instead of iOS.
dae added a commit to ankitects/rules_rust that referenced this pull request Sep 25, 2022
PR bazelbuild#1081 started passing CFLAGS to build scripts, which cargo does
too. But unfortunately Bazel's cpp toolchain adds an explicit target to
the CFLAGS it provides, and that target uses the host platform instead
of the target platform. When building a crate like blake3 or zstd on a
Mac with iOS as the target, this ends up in the crate being built for
macOS instead of iOS.
dae added a commit to ankitects/rules_rust that referenced this pull request Sep 25, 2022
PR bazelbuild#1081 started passing CFLAGS to build scripts, which cargo does
too. But unfortunately Bazel's cpp toolchain adds an explicit target to
the CFLAGS it provides, and that target uses the host platform instead
of the target platform. When building a crate like blake3 or zstd on a
Mac with iOS as the target, this ends up in the crate being built for
macOS instead of iOS.
dae added a commit to ankitects/rules_rust that referenced this pull request Sep 25, 2022
PR bazelbuild#1081 started passing CFLAGS to build scripts, which cargo does
too. But unfortunately Bazel's cpp toolchain adds an explicit target to
the CFLAGS it provides, and that target uses the host platform instead
of the target platform. When building a crate like blake3 or zstd on a
Mac with iOS as the target, this ends up in the crate being built for
macOS instead of iOS.
dae added a commit to ankitects/rules_rust that referenced this pull request Oct 7, 2022
PR bazelbuild#1081 started passing CFLAGS to build scripts, which cargo does
too. But unfortunately Bazel's cpp toolchain adds an explicit target to
the CFLAGS it provides, and that target uses the host platform instead
of the target platform. When building a crate like blake3 or zstd on a
Mac with iOS as the target, this ends up in the crate being built for
macOS instead of iOS.
illicitonion pushed a commit that referenced this pull request Oct 7, 2022
* Fix build scripts targeting the wrong architecture

PR #1081 started passing CFLAGS to build scripts, which cargo does
too. But unfortunately Bazel's cpp toolchain adds an explicit target to
the CFLAGS it provides, and that target uses the host platform instead
of the target platform. When building a crate like blake3 or zstd on a
Mac with iOS as the target, this ends up in the crate being built for
macOS instead of iOS.

* Add an end-to-end test to make sure building zstd for iOS works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants