Skip to content
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

pyo3-build-config: fix cross compilation #1648

Merged
merged 2 commits into from
Jun 26, 2021

Conversation

davidhewitt
Copy link
Member

@davidhewitt davidhewitt commented Jun 1, 2021

This fixes cross-compilation with the new pyo3-build-config crate.

The problem is that CARGO_CFG_TARGET_OS (and similar) environment variables are set to the host OS when running the build.rs in pyo3-build-config, because pyo3-build-config is used in build-dependencies.

The best way I can see to fix this is to evaluate the build configuration in the build.rs of the first runtime dependency we control - PyO3's build.rs.

Before:

  • build.rs in pyo3-build-config runs (incorrectly) and generates config information, which gets baked into the pyo3-build-config crate
  • downstream crates (pyo3, pyo3-build-config) use this baked-in configuration

This PR:

  • build.rs in pyo3-build-config creates an empty OUT_DIR
  • build.rs in pyo3 writes the correct config to that OUT_DIR
  • downstream crates can load this configuration in their build.rs
  • pyo3-macros-backend can load this configuration at macro runtime and check it

cc @birkenfeld this will slightly change how to check for METH_FASTCALL support in #1619 - instead of using cfg!, it's now necessary to use pyo3_build_config::get() and check the configuration directly. This is necessary because pyo3-macros-backend is built before pyo3, so the configuration hasn't yet been written when pyo3-macros-backend is built.

@davidhewitt davidhewitt force-pushed the fix-cross-compile-config branch 2 times, most recently from fdcb439 to 55d9581 Compare June 5, 2021 08:29
@davidhewitt
Copy link
Member Author

@alonblade I just rebased this branch on your other cross-compilation PR.

Would you be willing to try this branch out to confirm whether it's now fixed the issues with cross-compilation? Thanks.

@davidhewitt davidhewitt force-pushed the fix-cross-compile-config branch from 55d9581 to ab6114a Compare June 5, 2021 11:46
@alonblade
Copy link
Contributor

@alonblade I just rebased this branch on your other cross-compilation PR.

Would you be willing to try this branch out to confirm whether it's now fixed the issues with cross-compilation? Thanks.

Yep - builds with target armhf on host x86_64.

@davidhewitt davidhewitt mentioned this pull request Jun 6, 2021
7 tasks
Copy link
Member

@messense messense left a comment

Choose a reason for hiding this comment

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

LGTM.

So it's important to note that pyo3-build-config should only be used in build time dependencies for example build.rs or proc macro, never intended to be used in the main pyo3 crate. pyo3 should use normal cfgs.

@davidhewitt
Copy link
Member Author

So it's important to note that pyo3-build-config should only be used in build time dependencies for example build.rs or proc macro, never intended to be used in the main pyo3 crate. pyo3 should use normal cfgs.

Yep precisely. I'm planning to add some updates to Architecture.md to note this.

@davidhewitt
Copy link
Member Author

(I'll merge this after I add some more test coverage.)

@davidhewitt davidhewitt force-pushed the fix-cross-compile-config branch 2 times, most recently from b7e192f to 6b6b312 Compare June 26, 2021 17:33
@davidhewitt davidhewitt force-pushed the fix-cross-compile-config branch from 6b6b312 to 5cc1e0f Compare June 26, 2021 18:30
@davidhewitt
Copy link
Member Author

I've pushed the coverage much higher. The job is still failing but I'm not sure it's worth doing further (only just slightly under the target), so proceeding to merge.

@davidhewitt davidhewitt merged commit b58fe20 into PyO3:main Jun 26, 2021
@davidhewitt davidhewitt deleted the fix-cross-compile-config branch June 26, 2021 19:06
@alonblade
Copy link
Contributor

Glad you merged it. Btw, somewhat related, I wanted to take the CI for cross builds ticket (I have a setup with bitbucket that works for my pyo3 using project for aarch64 & armhf) but I got stuck on the "I don't grok the yml of github yet" problem. I could just through my current bitbucket CI or even just a docker one using a simplified project, would that help? @davidhewitt

@messense
Copy link
Member

@alonblade I've done a lot of cross compilation jobs with GitHub Actions, so I've opened #1700

@alonblade
Copy link
Contributor

@messense sweet.

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.

3 participants