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

build.rs: if found more than one candidate, filter on arch #1626

Merged
merged 1 commit into from
Jun 5, 2021

Conversation

alonblade
Copy link
Contributor

Hi,

This is my first RP on pyo3. Thanks for the great work, I'm using this right now to convert a library from python to rust for a raspberry pi running in 32 bit mode, that is armhf architecture, using ubuntu 20.04 as the target distribution. I have both the host x86_64 python3.8 sysconfigdata and the foreign architecture armhf in /usr/lib/python3.8, which results in more than one candidate during search_lib_dir, and an error of "Detected multiple possible python versions".

I've conservatively added a filter in that case to keep only the paths with cross.arch in their name (to_lossy_string.contains(&config.arch))

There is already an existing filter on config.arch but only in another branch, so I didn't want to pull it in to the first branch (Ok(f) if starts_with(f, "_sysconfigdata") && ends_with(f, "py")).

I added a line in CHANGELOG, I think this is a bug so it should not change the documentation.

I've run clippy (no errors), run fmt (no changes), "cargo test" fails but on unrelated code (I can attach, fails in examples).

Alon

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Hi @alonblade, thanks for the PR!

We literally just merged a change to the build infrastructure, so I had to rebase and force-push your PR. Hope that's ok.

Let's see how CI does with this branch, also I have one thought...

Comment on lines 487 to 503
// If we got more than one file, only take those that contain the arch name.
// For ubuntu 20.04 with host architecture x86_64 and a foreign architecture of armhf
// this reduces the number of candidates to 1:
//
// $ find /usr/lib/python3.8/ -name '_sysconfigdata*.py' -not -lname '*'
// /usr/lib/python3.8/_sysconfigdata__x86_64-linux-gnu.py
// /usr/lib/python3.8/_sysconfigdata__arm-linux-gnueabihf.py
if sysconfig_paths.len() > 1 {
sysconfig_paths = sysconfig_paths
.iter()
.filter(|p| p.to_string_lossy().contains(&cross.arch))
.map(|p| p.clone())
.collect::<Vec<PathBuf>>();
}
Copy link
Member

Choose a reason for hiding this comment

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

The only potential issue I see is that if the arch doesn't match any sysconfig file (e.g. maybe the arch is spelt differently for some reason?) then this could reduce the number of candidates to zero.

In that case, it might be worth keeping the original full set of paths instead of listing zero paths and failing?

@alonblade
Copy link
Contributor Author

alonblade commented May 25, 2021 via email

@alonblade
Copy link
Contributor Author

@davidhewitt hmm, now that I rebased on latest I get a different error from my use case (pi armhf target, x86_64 host) - Trying to debug it.

   Compiling pyo3 v0.13.2 (/home/alon/catkin_ws/mpu6050/sub/pyo3)
error: failed to run custom build command for `pyo3 v0.13.2 (/home/alon/catkin_ws/mpu6050/sub/pyo3)`

Caused by:
  process didn't exit successfully: `/home/alon/catkin_ws/mpu6050/target/release/build/pyo3-6c31d3e862b113d6/build-script-build` (exit status: 1)
  --- stderr
  error: Your Rust target architecture (32-bit) does not match your python interpreter (64-bit)

@davidhewitt
Copy link
Member

@alonblade yikes, I'm pretty sure this is my fault; I have a guess at what's going on.

I'll try to open a PR to fix tonight.

@alonblade
Copy link
Contributor Author

@davidhewitt ok, I think I found the problem: the new refactoring of pyo3-build-config out of build.rs means that when
pyo3-build-config/src/impl_.rs is run the value of the cargo target environment variables is actually the host, not the real target. So cross.arch is actually x86_64, and I get a false error. To confirm, I commented out ensure_target_architecture and tested the resulting binary and it is the correct architecture.

I am not sure how to proceed - one option would be to pass the real target variables to pyo3-build-config in a different set of environment variables (i.e. CARGO_CFG_TARGET_ARCH becomes PYO3_CARGO_CFG_TARGET_ARCH)

What do you think?

@alonblade
Copy link
Contributor Author

@alonblade yikes, I'm pretty sure this is my fault; I have a guess at what's going on.

I'll try to open a PR to fix tonight.

Thanks!

@davidhewitt
Copy link
Member

(Apologies, I've run out of time to try to fix this tonight. I'm going to attempt to find time again soon, hopefully on Thursday evening.)

@davidhewitt davidhewitt mentioned this pull request May 25, 2021
7 tasks
@alonblade
Copy link
Contributor Author

alonblade commented May 26, 2021 via email

If we got more then one file, only take those that contain the arch name.
For ubuntu 20.04 with host architecture x86_64 and a foreign architecture of armhf
this reduces the number of candidates to 1:

  $ find /usr/lib/python3.8/ -name '_sysconfigdata*.py' -not -lname '*'
  /usr/lib/python3.8/_sysconfigdata__x86_64-linux-gnu.py
  /usr/lib/python3.8/_sysconfigdata__arm-linux-gnueabihf.py

CHANGELOG.md: add entry for cross-sysconfigdata filter on arch

commit changelog:
1. initial
2. if filtered list is empty, use pre filtered.
3. clippy is_empty and cloned
@davidhewitt
Copy link
Member

I'm going to merge this and rebase my cross-compilation fix on it. Thanks for implementing this!

@davidhewitt davidhewitt merged commit 97d6f15 into PyO3:main Jun 5, 2021
@alonblade
Copy link
Contributor Author

alonblade commented Jun 5, 2021 via email

messense added a commit to messense/maturin that referenced this pull request Jun 9, 2021
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.

2 participants