eclipse / paho.mqtt.rust

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

Can not build Rust Project with paho-mqtt-sys library on Mac os Apple M1 Pro #174

Closed burak0206 closed 1 year ago

burak0206 commented 1 year ago

Hi all, I am developing a rust web project using the M1 pro chipset. I installed mosquitto with brew and paho.mqtt.c with macports. I added paho-mqtt-sys and paho-mqtt. Before that, application can be built. However if I add paho-mqtt libraries, It gives an error when I call cargo build --target x86_64-apple-darwin.

What should I do to fix the error?

Error:

Ekran Resmi 2022-11-20 18 27 11
appelgriebsch commented 1 year ago

Interestingly the generated libpaho-mqtt3as.a is a fat binary already....

target/release/build/paho-mqtt-sys-78ae08bf9e76abb2/out/lib/libpaho-mqtt3as.a: Mach-O universal binary with 2 architectures: [x86_64:current ar archive random librarycurrent ar archive random library] [arm64:current ar archive random librarycurrent ar archive random library]
target/release/build/paho-mqtt-sys-78ae08bf9e76abb2/out/lib/libpaho-mqtt3as.a (for architecture x86_64):    current ar archive random library
target/release/build/paho-mqtt-sys-78ae08bf9e76abb2/out/lib/libpaho-mqtt3as.a (for architecture arm64): current ar archive random library

Could that be the issue?

appelgriebsch commented 1 year ago

there was a recent PR merged in rust... https://github.com/rust-lang/rust/pull/98736. Looks like it is targeted for the 1.66 release of the rust toolchain

appelgriebsch commented 1 year ago

checked it with the beta toolchain just now. it builds for both architectures on my M1 :)

burak0206 commented 1 year ago

good news!, I will try it @appelgriebsch

burak0206 commented 1 year ago

I tried to these instructions set and I can compile. thank you @appelgriebsch

1) rustup update && rustup update beta && rustup install beta 2) rustup default beta-aarch64-apple-darwin 3) rustup target add x86_64-apple-darwin
4) cargo clean && cargo build --target x86_64-apple-darwin

appelgriebsch commented 1 year ago

I tried to these instructions set and I can compile. thank you @appelgriebsch

1. rustup update && rustup update beta && rustup install beta

2. rustup default beta-aarch64-apple-darwin

3. rustup target add x86_64-apple-darwin

4. cargo clean && cargo build --target x86_64-apple-darwin

instead of making the beta channel the default one you can also prefix the cargo commands with the channel to be used:

cargo +beta target add x86_64-apple-darwin
cargo +beta build --target x86_64-apple-darwin
appelgriebsch commented 1 year ago

You can then use the lipo command to combine both binaries into a single universal (fat) binary like so:

lipo -create -arch arm64 <path-to-aarch64-apple-darwin-binary> -arch x86_64 <path-to-x86_64-apple-darwin-binary> -o <path-to-universal-binary>

Benefit: this universal binary works for both architectures.

fpagliughi commented 1 year ago

Hey! Thanks for the work on this. I don't have access to an M1 to help or try any of it out, but do you think there is something here we should add to the README.md? Maybe make a Mac/M1 section?

burak0206 commented 1 year ago

Hi @fpagliughi ,

yes it can be nice, I can add if you want. but I don't know which one is the best. I think @appelgriebsch knows better than me.

appelgriebsch commented 1 year ago

Sorry for radio silence. Was a bit busy lately. Will provide a PR for the README update on the coming weekend.