joseluisq / rust-linux-darwin-builder

Use the same Docker image to cross-compile Rust x86_64/ARM64 programs for Linux and macOS (osxcross).
Apache License 2.0
119 stars 15 forks source link

Building crates needing C++ fails #27

Open rjkroege opened 1 year ago

rjkroege commented 1 year ago

When I use the builder, crates that contain Rust-compiled C++ code fail to link. Example:

...much spew...
  = note: ld: archive has no table of contents file '/root/src/difft/target/aarch64-apple-darwin/release/build/difftastic-abae5b1432a24d5f/out/libtree-sitter-ada.a' for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
...much spew...

AFAIK: lib tree-sitter-ada.a is built from C++ code by the difftastic crate.

This is working as intended? Or am I being an idiot?

joseluisq commented 1 year ago

Can you share how are you compiling difftastic actually?

rjkroege commented 1 year ago

Using environment from the Dockerfile. I will note in passing that it failed for all of aarch64-apple-darwin, x86_64-apple-darwin, armv7-unknown-linux-musleabihf, x86_64-unknown-linux-musl. My exact build commands differed in each case. I'll use aarch64-apple-darwin as an example:

CC=aarch64-apple-darwin22.2-clang \
CXX=aarch64-apple-darwin22.2-clang++ \
cargo build \
    --release \
    --target aarch64-apple-darwin

I see from the spew that the C++ compiler has executed because it generated many compilation warnings.

The behaviour for x86_64-unknown-linux-musl differs (still doesn't work but different errors.) FWIW: I built like so:

CC=musl-gcc \
CXX=musl-g++ \
cargo build \
    --release \
    --target x86_64-unknown-linux-musl

It also failed in the link phase in a way that suggests a mismatch between libstdc++ (conceivably compiled against Gnu) and must. At least, thatI's what I think is happening. But see the "am I being an idiot?" in the original issue. 🙂

Here's the error:

  = note: /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/12/libstdc++.a(stdexcept.o): in function `std::logic_error::~logic_error()':
          (.text._ZNSt11logic_errorD2Ev+0x43): undefined reference to `__libc_single_threaded'
          collect2: error: ld returned 1 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

Maybe I need to add a full musl cross gcc to the Docker (something like richfelker/musl-cross-make: Simple makefile-based build for musl cross compiler) to build difftastic on Linux?

joseluisq commented 1 year ago

About musl, we probably have to do some clever stuff such as sysroot musl and add the necessary symlinks similar to cross. But definitely, this will need some work to be able to statically link libstdc++ when targeting musl.

And regarding Darwin, for me looks like a mismatch issue but I have no clue about it at the moment, so I will try to figure it out.

But in general, there are interesting ideas in the cross project (which BTW difftastic is using for the musl target). So feel free to help.

rjkroege commented 1 year ago

Since I want this to work... I'll see if I can make this work better over the next few weeks and update the bug with anything that I figure out.