eclipse / paho.mqtt.rust

paho.mqtt.rust
Other
516 stars 102 forks source link

Failing to cross-compile on Mac due to use of gcc '-arch' argument #144

Open svet-b opened 2 years ago

svet-b commented 2 years ago

I'm building on an M1 Mac (arm64) for an armv7-unknown-linux-musleabihf target, and facing what looks to be a simple compilation issue below. By the way, I have no issues with either of the following:

  1. Doing a native build of paho-mqtt on the Mac host
  2. Cross-compiling others crates/code on the Mac host for the same target
  3. Cross-compiling the paho-mqtt crate for this target when building on a Linux host

Therefore I believe this to be specific to building paho-mqtt on a Mac.

I have the following in Cargo.toml:

paho-mqtt = { version = "*", default-features = false, features = ["bundled"] }

(but same issue with latest stable release)

I'm using gcc from the toolchain at https://github.com/messense/homebrew-macos-cross-toolchains.

The main error is

arm-linux-musleabihf-gcc: error: unrecognized command-line option '-arch'; did you mean '-march='?

My analysis: while -arch appears to be a valid option for Apple's native gcc, it's generally not valid for cross-compilation toolchains (related discussion). I don't know the details as to why the -arch option is used in the build script here, but perhaps it's not actually necessary (since -march is also passed). Unfortunately I'm not sufficiently familiar with the build process to figure out where in the codebase to look for a solution.

The full build error log is as follows:

   Compiling paho-mqtt-sys v0.6.0 (https://github.com/eclipse/paho.mqtt.rust.git?branch=master#9df0e507)
error: failed to run custom build command for `paho-mqtt-sys v0.6.0 (https://github.com/eclipse/paho.mqtt.rust.git?branch=master#9df0e507)`

Caused by:
  process didn't exit successfully: `/Users/svet/git/misc/rust/rust_modbus_relay/target/release/build/paho-mqtt-sys-ce4ebfc7962cb264/build-script-build` (exit status: 101)
  --- stdout
  debug:Running the bundled build for Paho C
  cargo:rerun-if-changed=build.rs
  running: "cmake" "/Users/svet/.cargo/git/checkouts/paho.mqtt.rust-44b07fd42c95bba6/9df0e50/paho-mqtt-sys/paho.mqtt.c/" "-DPAHO_BUILD_SHARED=off" "-DPAHO_BUILD_STATIC=on" "-DPAHO_ENABLE_TESTING=off" "-DPAHO_HIGH_PERFORMANCE=on" "-DPAHO_WITH_SSL=off" "-DCMAKE_INSTALL_PREFIX=/Users/svet/git/misc/rust/rust_modbus_relay/target/armv7-unknown-linux-musleabihf/release/build/paho-mqtt-sys-10e4ea6c9756035b/out" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC -march=armv7-a -mfpu=vfpv3-d16" "-DCMAKE_C_COMPILER=/Users/svet/.local/bin/arm-linux-musleabihf-gcc" "-DCMAKE_CXX_FLAGS= -ffunction-sections -fdata-sections -fPIC -march=armv7-a -mfpu=vfpv3-d16" "-DCMAKE_CXX_COMPILER=arm-linux-musleabihf-g++" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC -march=armv7-a -mfpu=vfpv3-d16" "-DCMAKE_ASM_COMPILER=/Users/svet/.local/bin/arm-linux-musleabihf-gcc" "-DCMAKE_BUILD_TYPE=MinSizeRel"
  -- The C compiler identification is GNU 10.3.0
  -- Checking whether C compiler has -isysroot
  -- Checking whether C compiler has -isysroot - yes
  -- Checking whether C compiler supports OSX deployment target flag
  -- Checking whether C compiler supports OSX deployment target flag - no
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - failed
  -- Check for working C compiler: /Users/svet/.local/bin/arm-linux-musleabihf-gcc
  -- Check for working C compiler: /Users/svet/.local/bin/arm-linux-musleabihf-gcc - broken
  -- Configuring incomplete, errors occurred!
  See also "/Users/svet/git/misc/rust/rust_modbus_relay/target/armv7-unknown-linux-musleabihf/release/build/paho-mqtt-sys-10e4ea6c9756035b/out/build/CMakeFiles/CMakeOutput.log".
  See also "/Users/svet/git/misc/rust/rust_modbus_relay/target/armv7-unknown-linux-musleabihf/release/build/paho-mqtt-sys-10e4ea6c9756035b/out/build/CMakeFiles/CMakeError.log".

  --- stderr
  CMake Error at /opt/homebrew/Cellar/cmake/3.22.1/share/cmake/Modules/CMakeTestCCompiler.cmake:69 (message):
    The C compiler

      "/Users/svet/.local/bin/arm-linux-musleabihf-gcc"

    is not able to compile a simple test program.

    It fails with the following output:

      Change Dir: /Users/svet/git/misc/rust/rust_modbus_relay/target/armv7-unknown-linux-musleabihf/release/build/paho-mqtt-sys-10e4ea6c9756035b/out/build/CMakeFiles/CMakeTmp

      Run Build Command(s):/usr/bin/make -f Makefile cmTC_0ae71/fast && /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/cmTC_0ae71.dir/build.make CMakeFiles/cmTC_0ae71.dir/build
      Building C object CMakeFiles/cmTC_0ae71.dir/testCCompiler.c.o
      /Users/svet/.local/bin/arm-linux-musleabihf-gcc   -ffunction-sections -fdata-sections -fPIC -march=armv7-a -mfpu=vfpv3-d16  -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -o CMakeFiles/cmTC_0ae71.dir/testCCompiler.c.o -c /Users/svet/git/misc/rust/rust_modbus_relay/target/armv7-unknown-linux-musleabihf/release/build/paho-mqtt-sys-10e4ea6c9756035b/out/build/CMakeFiles/CMakeTmp/testCCompiler.c
      arm-linux-musleabihf-gcc: error: arm64: No such file or directory
      arm-linux-musleabihf-gcc: error: unrecognized command-line option '-arch'; did you mean '-march='?
      make[1]: *** [CMakeFiles/cmTC_0ae71.dir/testCCompiler.c.o] Error 1
      make: *** [cmTC_0ae71/fast] Error 2

    CMake will not be able to correctly generate this project.
  Call Stack (most recent call first):
    CMakeLists.txt:21 (PROJECT)

  thread 'main' panicked at '
  command did not execute successfully, got: exit status: 1

  build script failed, must exit now', /Users/svet/.cargo/registry/src/github.com-1ecc6299db9ec823/cmake-0.1.45/src/lib.rs:894:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fpagliughi commented 2 years ago

Hmmm. By default the Rust build for the '-sys' crate tries to compile the Paho C library from a Git submodule. For the most part it just runs CMake on the library with a few definitions appropriate for the Rust wrapper.

To do this we use the Rust 'cmake' crate, which largely takes care of dealing with cross-compilation.

With just a quick look, I'm not sure which is injecting the '-arch' option. My guess is probably the Rust CMake crate, but it needs some investigation.

I don't have an M1 Mac, but I don't think this is a problem with the build host, so I can run some tests with other hosts. But unfortunately this is likely an issue that needs to be fixed in an upstream dependency.

svet-b commented 2 years ago

Thanks for taking a look @fpagliughi! As I said I'm not so familiar with the overall build process, but inclined to agree that it's an upstream issue. I guess most other crates don't build C code, which is why this is the first time that I'm encountering the issue.

And indeed, based on the symptoms I think it's probably a general Mac issue rather than M1-specific.

fpagliughi commented 2 years ago

Yeah, I had hoped to rewrite the library by now to be 100% Rust, but there's never enough time to get it done! Most of the reported issues for this library revolve around build and portability issues involving the C lib. I'll have some time to take a closer look in the next few days.

appelgriebsch commented 2 years ago

Any updates on this? I'm running into the same issue. Other crates with -sys dependencies build fine though -e.g. openssl itself.

natdan commented 2 years ago

I'm stuck with the same as well (on M1 mac, of course). Is there anything someone would like me to start looking into? I'm semi-useless as I have just now started playing with this side of rust and cross compilation for Raspberry Pi...