Newer
Older
minerva / Ports / zig / patches / 0013-build-Adjust-build-process-for-Minerva.patch
@minerva minerva on 13 Jul 3 KB Initial commit
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: sin-ack <sin-ack@users.noreply.github.com>
Date: Sun, 11 Dec 2022 17:22:27 +0000
Subject: [PATCH] build: Adjust build process for Minerva

---
 build | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)

diff --git a/build b/build
index b1917f6bb18a587d9b5e98171c9f789d9a29aff6..e7d809893e46e247a579050ac61091a78c6b90a5 100755
--- a/build
+++ b/build
@@ -17,6 +17,7 @@ case $TARGET_OS_CMAKE in
   freebsd) TARGET_OS_CMAKE="FreeBSD";;
   windows) TARGET_OS_CMAKE="Windows";;
   linux) TARGET_OS_CMAKE="Linux";;
+  minerva) TARGET_OS_CMAKE="Minerva";;
   native) TARGET_OS_CMAKE="";;
 esac
 
@@ -62,10 +63,58 @@ cmake "$ROOTDIR/zig" \
   -DCMAKE_BUILD_TYPE=Release \
   -DZIG_VERSION="$ZIG_VERSION"
 cmake --build . --target install
+ 
+# Create a libc installation file so Zig knows where to look for headers and
+# libraries while compiling for the Minerva target.
+cat <<EOF > "$ROOTDIR/out/libc_installation.txt"
+include_dir=$MINERVA_INSTALL_ROOT/usr/include
+sys_include_dir=$MINERVA_INSTALL_ROOT/usr/include
+crt_dir=$MINERVA_INSTALL_ROOT/usr/lib
+msvc_lib_dir=
+kernel32_lib_dir=
+gcc_dir=
+EOF
+
+export ZIG_LIBC="$ROOTDIR/out/libc_installation.txt"
+
+# Create a CMakeToolchain.txt file to tell CMake about the Minerva platform.
+# Adapted from Toolchain/CMake/GNUToolchain.txt.in, with the tool settings
+# removed as zig-bootstrap overrides them manually with the cmake command.
+cat <<EOF > "$ROOTDIR/out/CMakeToolchain.txt"
+if (\${CMAKE_VERSION} VERSION_LESS "3.25.0")
+  list(APPEND CMAKE_MODULE_PATH "$MINERVA_SOURCE_DIR/Toolchain/CMake")
+endif()
+
+set(CMAKE_SYSTEM_NAME Minerva)
+set(CMAKE_SYSTEM_PROCESSOR "$MINERVA_ARCH")
+
+set(CMAKE_C_COMPILER_WORKS TRUE)
+set(CMAKE_CXX_COMPILER_WORKS TRUE)
+
+set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
+EOF
+
 
 # Now we have Zig as a cross compiler.
 ZIG="$ROOTDIR/out/host/bin/zig"
 
+# Create a minerva/constants.zig file. This package will overwrite the
+# constants.zig file in the upstream Zig in order to provide values for POSIX
+# constants.
+$ZIG run "$ROOTDIR/out/scripts/generate-minerva-constants.zig" \
+  -D__minerva__ \
+  -I"$MINERVA_INSTALL_ROOT/usr/include" \
+  > "$ROOTDIR/zig/lib/std/c/minerva/constants.zig"
+# HACK: Also copy this file over to the host Zig compiler in the prefix. It has
+#       to be done this way because we can't generate the constants until we
+#       have a Zig compiler, and by the time the host Zig compiler is built
+#       the standard library sources have been copied into the prefix already.
+cp "$ROOTDIR/zig/lib/std/c/minerva/constants.zig" \
+  "$ROOTDIR/out/host/lib/zig/std/c/minerva/constants.zig"
+
 # First cross compile zlib for the target, as we need the LLVM linked into
 # the final zig binary to have zlib support enabled.
 mkdir -p "$ROOTDIR/out/build-zlib-$TARGET-$MCPU"
@@ -81,7 +130,8 @@ cmake "$ROOTDIR/zlib" \
   -DCMAKE_ASM_COMPILER="$ZIG;cc;-fno-sanitize=all;-s;-target;$TARGET;-mcpu=$MCPU" \
   -DCMAKE_RC_COMPILER="$ROOTDIR/out/host/bin/llvm-rc" \
   -DCMAKE_AR="$ROOTDIR/out/host/bin/llvm-ar" \
-  -DCMAKE_RANLIB="$ROOTDIR/out/host/bin/llvm-ranlib"
+  -DCMAKE_RANLIB="$ROOTDIR/out/host/bin/llvm-ranlib" \
+  -DCMAKE_TOOLCHAIN_FILE="$ROOTDIR/out/CMakeToolchain.txt"
 cmake --build . --target install
 
 # Same deal for zstd.
@@ -144,6 +194,7 @@ cmake "$ROOTDIR/llvm" \
   -DCMAKE_RC_COMPILER="$ROOTDIR/out/host/bin/llvm-rc" \
   -DCMAKE_AR="$ROOTDIR/out/host/bin/llvm-ar" \
   -DCMAKE_RANLIB="$ROOTDIR/out/host/bin/llvm-ranlib" \
+  -DCMAKE_TOOLCHAIN_FILE="$ROOTDIR/out/CMakeToolchain.txt" \
   -DLLVM_ENABLE_BACKTRACES=OFF \
   -DLLVM_ENABLE_BINDINGS=OFF \
   -DLLVM_ENABLE_CRASH_OVERRIDES=OFF \