From fbfa948027bae105d2f5dc027d268e9b9728a842 Mon Sep 17 00:00:00 2001
From: Luna <git@l4.pm>
Date: Mon, 7 Jun 2021 00:20:16 -0300
Subject: [PATCH 1/6] set '-u' and '-x' for better safety and debugging

---
 build | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/build b/build
index 69cdfa6457..6d930417de 100755
--- a/build
+++ b/build
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 set -e
+set -u
+set -x
 
 JOBS="$1"
 TARGET="$2" # Example: riscv64-linux-gnu

From 919da8d0394056628f8fee4b9917cd5fd540ccfa Mon Sep 17 00:00:00 2001
From: Luna <git@l4.pm>
Date: Mon, 7 Jun 2021 00:20:33 -0300
Subject: [PATCH 2/6] add NetBSD support to TARGET_OS_CMAKE variable

---
 build | 1 +
 1 file changed, 1 insertion(+)

diff --git a/build b/build
index 6d930417de..15efaa6b27 100755
--- a/build
+++ b/build
@@ -18,6 +18,7 @@ TARGET_OS_CMAKE=${TARGET_OS_AND_ABI%-*} # Example: linux
 case $TARGET_OS_CMAKE in
   macos) TARGET_OS_CMAKE="Darwin";;
   freebsd) TARGET_OS_CMAKE="FreeBSD";;
+  netbsd) TARGET_OS_CMAKE="NetBSD";;
   windows) TARGET_OS_CMAKE="Windows";;
   linux) TARGET_OS_CMAKE="Linux";;
   native) TARGET_OS_CMAKE="";;

From cf3539d2d152ee7fc882e8032364273848d99036 Mon Sep 17 00:00:00 2001
From: Luna <git@l4.pm>
Date: Mon, 7 Jun 2021 00:21:23 -0300
Subject: [PATCH 3/6] keep CC and CXX variables for final zig build

---
 build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/build b/build
index 15efaa6b27..bc4f6d9220 100755
--- a/build
+++ b/build
@@ -132,6 +132,6 @@ cmake "$ROOTDIR/zig" \
   -DZIG_VERSION="$ZIG_VERSION" \
   -DZIG_USE_LLVM_CONFIG=OFF \
   -DZIG_STATIC_ZLIB=ON
+make "$JOBS" install
 unset CC
 unset CXX
-make "$JOBS" install

From 6067ec27f3a923c73cbec287746c55689c4d1995 Mon Sep 17 00:00:00 2001
From: Luna <git@l4.pm>
Date: Mon, 7 Jun 2021 00:22:49 -0300
Subject: [PATCH 4/6] add sincos_netbsd_compat.c

this is required for NetBSD as a code path in the AMDGPU backend is
optimized to sincos(x), which is not provided by NetBSD libc.

a compatibility wrapper file provides the missing symbol.
---
 llvm/lib/Target/AMDGPU/CMakeLists.txt         |  1 +
 llvm/lib/Target/AMDGPU/sincos_netbsd_compat.c | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)
 create mode 100644 llvm/lib/Target/AMDGPU/sincos_netbsd_compat.c

diff --git a/llvm/lib/Target/AMDGPU/CMakeLists.txt b/llvm/lib/Target/AMDGPU/CMakeLists.txt
index 71f2026c33..043c62d68e 100644
--- a/llvm/lib/Target/AMDGPU/CMakeLists.txt
+++ b/llvm/lib/Target/AMDGPU/CMakeLists.txt
@@ -144,6 +144,7 @@ add_llvm_target(AMDGPUCodeGen
   GCNNSAReassign.cpp
   GCNDPPCombine.cpp
   SIModeRegister.cpp
+  sincos_netbsd_compat.c
 
   LINK_COMPONENTS
   Analysis
diff --git a/llvm/lib/Target/AMDGPU/sincos_netbsd_compat.c b/llvm/lib/Target/AMDGPU/sincos_netbsd_compat.c
new file mode 100644
index 0000000000..44db15af99
--- /dev/null
+++ b/llvm/lib/Target/AMDGPU/sincos_netbsd_compat.c
@@ -0,0 +1,16 @@
+#include <math.h>
+
+void sincos(double x, double *sin_result, double *cos_result) {
+	*sin_result = sin(x);
+	*cos_result = cos(x);
+}
+
+void sincosf(float x, float *sin_result, float *cos_result) {
+	*sin_result = sinf(x);
+	*cos_result = cosf(x);
+}
+
+void sincosl(long double x, long double *sin_result, long double *cos_result) {
+	*sin_result = sinl(x);
+	*cos_result = cosl(x);
+}

From 51e891e2fa8f29b8ea97d6538048db0edb9f0fd5 Mon Sep 17 00:00:00 2001
From: Luna <git@l4.pm>
Date: Mon, 7 Jun 2021 00:28:55 -0300
Subject: [PATCH 5/6] create miniscripts for CC and CXX

this is inspired by the proposed solution in https://github.com/ziglang/zig/issues/8973

cmake was not accepting the rest of the arguments for CC and CXX, those
wrapper scripts take care of that.

changes beyond the script:
 - use CC=clang because -mcpu=baseline is not accepted by gcc.
 - remove '-target' as i'm targeting native,
    but it should be brought back, this happened before the CC=clang change.
---
 build | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/build b/build
index bc4f6d9220..c538e91aeb 100755
--- a/build
+++ b/build
@@ -56,8 +56,16 @@ make "$JOBS" install
 
 # Now we have Zig as a cross compiler.
 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"
+export CC="$ZIG cc -fno-sanitize=all -mcpu=$MCPU"
+export CXX="$ZIG c++ -fno-sanitize=all -mcpu=$MCPU"
+echo "#!/bin/sh
+env CC=\"clang\" $CC \"\$@\"" > $ROOTDIR/out/host/bin/zigcc
+echo "#!/bin/sh
+env CC=\"clang\" $CXX \"\$@\"" > $ROOTDIR/out/host/bin/zigcxx
+chmod +x $ROOTDIR/out/host/bin/zigcc
+chmod +x $ROOTDIR/out/host/bin/zigcxx
+export CC="$ROOTDIR/out/host/bin/zigcc"
+export CXX="$ROOTDIR/out/host/bin/zigcxx"
 
 # First cross compile zlib for the target, as we need the LLVM linked into
 # the finaly zig binary to have zlib support enabled.

From 051c5d9e0573c4dd8b52138019ca9a1487302c31 Mon Sep 17 00:00:00 2001
From: Luna <git@l4.pm>
Date: Mon, 7 Jun 2021 00:29:51 -0300
Subject: [PATCH 6/6] use clang on zig cmake install script

somehow the $CC variable was missing by the time the install lib step
was running. force it back to make it work.
---
 zig/cmake/install.cmake | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/zig/cmake/install.cmake b/zig/cmake/install.cmake
index 386773e30c..f7d6a57bb1 100644
--- a/zig/cmake/install.cmake
+++ b/zig/cmake/install.cmake
@@ -10,7 +10,7 @@ if(NOT EXISTS ${zig_EXE})
     message(FATAL_ERROR)
 endif()
 
-execute_process(COMMAND ${zig_EXE} ${ZIG_INSTALL_ARGS}
+execute_process(COMMAND env CC=clang ${zig_EXE} ${ZIG_INSTALL_ARGS}
     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
     RESULT_VARIABLE _result
 )