rust-cross / cargo-zigbuild

Compile Cargo project with zig as linker
MIT License
1.45k stars 52 forks source link

Fail to cross compile C++ dependencies from Linux to macOS #136

Closed Tpt closed 1 year ago

Tpt commented 1 year ago

Thank you so much for maturin and cargo zigbuild. They are amazing tool!

I am trying to cross-compile pyoxigraph from Linux to macOS using Maturin and Zig. Cross-compilation works perfectly targetting other Linux architectures. Pyoxigraph has some C++ dependencies.

However, when I try to cross-compile to MacOS, I get compilation errors from the libcxx version shipped with Zig. It seems that some context like the __cplusplus env variable used by libcxx are not properly setup. The same errors happen for both x86_64-apple-darwin and aarch64-apple-darwin.

What is strange is that when I call the build script zigcxx-x86_64-apple-darwin.sh manually with the same arguments as what the build script prints I get a successful compilation.

It seems that zig is not properly setting up some global environment when called by the cc crate. Here is the build script from Oxigraph for the C++ dependencies.

I am not sure if it is a known problem or if there is a better place to report this issue.

Example:

  running: ".cache/cargo-zigbuild/0.16.9/zigcxx-x86_64-apple-darwin.sh" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-2" "-fno-omit-frame-pointer" "-m64" "-arch" "x86_64" "-I" "rocksdb/include/" "-I" "rocksdb/" "-I" "lz4/lib/" "-Wall" "-Wextra" "-std=c++17" "-Wno-invalid-offsetof" "-msse2" "-DNDEBUG=1" "-DLZ4=1" "-DOS_MACOSX" "-DROCKSDB_PLATFORM_POSIX" "-DROCKSDB_LIB_IO_POSIX" "-DROCKSDB_SUPPORT_THREAD_LOCAL" "-DHAVE_UINT128_EXTENSION=1" "-o" "oxigraph/target/x86_64-apple-darwin/debug/build/oxrocksdb-sys-0c32ab08ef7397d8/out/rocksdb/cache/cache_helpers.o" "-c" "rocksdb/cache/cache_helpers.cc"
  cargo:warning=In file included from rocksdb/cache/cache_reservation_manager.cc:9:
  cargo:warning=In file included from rocksdb/cache/cache_reservation_manager.h:12:
  cargo:warning=In file included from oxigraph/venv/lib/python3.11/site-packages/ziglang/lib/libcxx/include/atomic:523:
  cargo:warning=In file included from oxigraph/venv/lib/python3.11/site-packages/ziglang/lib/libcxx/include/__chrono/duration.h:14:
  cargo:warning=In file included from oxigraph/venv/lib/python3.11/site-packages/ziglang/lib/libcxx/include/limits:107:
  cargo:warning=In file included from oxigraph/venv/lib/python3.11/site-packages/ziglang/lib/libcxx/include/type_traits:421:
  cargo:warning=In file included from oxigraph/venv/lib/python3.11/site-packages/ziglang/lib/libcxx/include/__functional/invoke.h:15:
  cargo:warning=In file included from oxigraph/venv/lib/python3.11/site-packages/ziglang/lib/libcxx/include/__type_traits/apply_cv.h:16:
  cargo:warning=In file included from oxigraph/venv/lib/python3.11/site-packages/ziglang/lib/libcxx/include/__type_traits/remove_reference.h:13:
  cargo:warning=oxigraph/venv/lib/python3.11/site-packages/ziglang/lib/libcxx/include/cstddef:50:9: error: no member named 'nullptr_t' in the global namespace

Environment variables as reported by cc:

  TARGET = Some("x86_64-apple-darwin")
  OPT_LEVEL = Some("0")
  HOST = Some("x86_64-unknown-linux-gnu")
  CXXFLAGS_x86_64-apple-darwin = None
  TARGET_CXXFLAGS = None
  CXXFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  CARGO_CFG_TARGET_FEATURE = Some("cmpxchg16b,fxsr,sse,sse2,sse3,ssse3")
  CXX_x86_64_apple_darwin = Some(".cache/cargo-zigbuild/0.16.9/zigcxx-x86_64-apple-darwin.sh")

To reproduce:

git clone https://github.com/oxigraph/oxigraph.git
cd oxigraph/python
pip install 'maturin[zig]'
rustup target add x86_64-apple-darwin
export SDKROOT=PATH_TO_MACOS_SDK
maturin build --zig --target x86_64-apple-darwin --features abi3

Sorry disturbing you with this bad bug report.

Tpt commented 1 year ago

I have found what triggered the error: zig c++ does not like when both include directories and SDKROOT are set. I unset SDKROOT and compilation works properly.