Skip to content

Instantly share code, notes, and snippets.

@matu3ba
Last active October 3, 2021 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matu3ba/f919bffa883faa4a6acc2e3e5a09610a to your computer and use it in GitHub Desktop.
Save matu3ba/f919bffa883faa4a6acc2e3e5a09610a to your computer and use it in GitHub Desktop.
cross compiling zig master to have C and C++ compiler working with master
#!/bin/sh
# The main problem with building zig with zig are build system changes.
# Further I had problems using C test suite, so it seems to me that not all
# necessary configs are being reused within the build system.
# Therefore this script cross compiles Zig with Zig to track upstream.
# However stage2 does not work with this and fails with
# error: unknown command: -print-file-name=libstdc++.a
# The following command exited with error code 1:
# $HOME/dev/git/zig/zig-bootstrap/out/host/bin/zig -print-file-name=libstdc++.a
# error: the following build command failed with exit code 1:
# $HOME/dev/git/zig/zig/zig-cache/o/ab99b768c960f81d9fcbfcd2b773ac6f/build $HOME/dev/git/zig/zig/build-x86_64-linux-gnu-native/zig $HOME/dev/git/zig/zig $HOME/dev/git/zig/zig/zig-cache $HOME/.cache/zig -Denable-llvm
# 1. Build zig-bootstrap: (https://github.com/ziglang/zig-bootstrap)
# 2. Select the following according `build` script from zig-bootstrap
TARGET="x86_64-linux-gnu" # Example: riscv64-linux-gnu
MCPU="native" # Examples: `baseline`, `native`, `generic+v7a`, or `arm1176jzf_s`
ROOTDIR="${HOME}/dev/git/zig/zig-bootstrap" ## ROOTDIR of zig-bootstrap
TARGET_OS_CMAKE="" # lookup switch case in script `build`
# 3. Select target build directory and install directory
DEV_ZIG="${HOME}/dev/git/zig/zig"
INSTALLDIR="${HOME}/.local/zig_stage1"
# Use zig generated by zig-bootstrap for cross-compiling
ZIG="$ROOTDIR/out/host/bin/zig"
export CC="$ZIG cc -fno-sanitize=all -target $TARGET -mcpu=$MCPU"
export CXX="$ZIG c++ -fno-sanitize=all -target $TARGET -mcpu=$MCPU"
# Finally, we can cross compile Zig itself, with Zig.
mkdir -p "${DEV_ZIG}/build-zig-$TARGET-$MCPU"
cd "${DEV_ZIG}/build-zig-$TARGET-$MCPU"
cmake "$ROOTDIR/zig" \
-DCMAKE_INSTALL_PREFIX="${INSTALLDIR}/zig-$TARGET-$MCPU" \
-DCMAKE_PREFIX_PATH="$ROOTDIR/out/$TARGET-$MCPU" \
-DCMAKE_CROSSCOMPILING=True \
-DCMAKE_SYSTEM_NAME="$TARGET_OS_CMAKE" \
-DCMAKE_AR="$ROOTDIR/out/host/bin/llvm-ar" \
-DCMAKE_RANLIB="$ROOTDIR/out/host/bin/llvm-ranlib" \
-DCMAKE_BUILD_TYPE=Release \
-DZIG_TARGET_TRIPLE="$TARGET" \
-DZIG_TARGET_MCPU="$MCPU" \
-DZIG_EXECUTABLE="$ZIG" \
-DZIG_USE_LLVM_CONFIG=OFF \
-DZIG_STATIC_ZLIB=ON \
-GNinja
unset CC
unset CXX
# Building
# you can also use mold (https://github.com/rui314/mold) with: mold -run ninja
${HOME}/dev/git/cpp/mold/mold -run ninja
# Behold that using `install` installs ~300MB
#ninja install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment