HadrienG2 / hwlocality

Rust bindings to Open MPI Portable Hardware Locality "hwloc" library, covering version 2.0 and above.
MIT License
24 stars 6 forks source link

Cross-compilation on macOS fails #91

Closed nazar-pc closed 11 months ago

nazar-pc commented 11 months ago

Tried to compile on x86-64 macOS for aarch64 and it failed like this:

  configure: error: in `/Users/runner/work/subspace/subspace/target/aarch64-apple-darwin/production/build/hwlocality-sys-5187208e9a266943/out/build':
  configure: error: cannot run C compiled programs.
  If you meant to cross compile, use `--host'.
  See `config.log' for more details
  thread 'main' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/autotools-0.2.6/src/lib.rs:781:5:

Not sure if this is hwlocality's fault yet, but this is the first package that failed in the project.

HadrienG2 commented 11 months ago

I think this happens because the autotools crate, which is used for vendored builds on Unix systems, does not have support for cross compilation yet and therefore does not pass the right flags for it to ./configure.

You may be able to work around it in the short term by extracting the target triplet (aarch64-apple-darwin) from the environment variables that cargo sets for build.rs, and adding a --host <triplet> to configure parameters (IIRC the autotools crate has the right hook for that). If it works, a contribution to the autotools crate to do it automatically would likely be welcome.

But be warned that cross-compilation of C code is not easy, and it may take a lot more effort than setting a configure flag to make it work.

nazar-pc commented 11 months ago

I have cross-compiled stuff for a long time and things like https://github.com/rust-lang/cc-rs handle it gracefully. I'll look into it once I'm in a mood to debug macOS issues again :upside_down_face:

lu-zero commented 11 months ago

Probably cc-rs picks the wrong compiler, I managed to cross-compile from macos to linux passing CC_x86_64_unknown_linux_musl=x86_64-linux-musl-gcc

I found some problems with the build.rs that are not related to autotools-rs but they shouldn't impact your use-case.

nazar-pc commented 11 months ago

I'm cross-compiling a large project which uses cc internally for some C dependencies and I didn't have issues with cross-compilation before autotools was introduced, so I'm pretty sure cc itself selects compiler correctly.

lu-zero commented 11 months ago

I'm sure there are cases in which the cc-rs heuristic fails (e.g. when it looks for musl-gcc), but this is not the case, I tried and I see -arch arm64 being correctly passed.