Commit Diff


commit - 2f4986db7a675ecd1901c63c1023c92e5ff95bef
commit + 9c836066a157ed851db8cef72bcfdd47a2dfafa1
blob - 70acdc8191b7256df605aae4d6f6ac74fa4f4533
blob + 1919ea85b5986250356e8214c1ad812230ceb1d6
--- Makefile
+++ Makefile
@@ -1,13 +1,12 @@
 .SUFFIXES:
 .SUFFIXES: .c .S .o .elf
 
+TOP    != pwd
 PREFIX	= /usr/local
 TARGET	= riscv64-unknown-linux-musl
-HOSTOS != if test -e 'config.mk.local'; then echo 'local'; else uname; fi
 CROSS	= ./tools/bin/${TARGET}
-CONFIG = config.mk.${HOSTOS}
 
-include ${CONFIG}
+include config.mk
 
 COPT	= -g -O2
 CFLAGS	= ${CFLAGS_OS} ${COPT} -std=c2x
blob - e504615cd2b9d611ee9b1c2ec605747d563dbbaa
blob + ac5f4155a7f3f942b7b66e4a6c8e570a3dc9d8a4
--- README
+++ README
@@ -1,3 +1,13 @@
 # Resources
 - https://riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf
 - https://five-embeddev.com/riscv-isa-manual/latest/instr-table.html
+
+# Supported Host OSs
+- OpenBSD
+- FreeBSD
+- NetBSD
+- Linux
+
+# Non-standard Toolchain Build Dependencies
+- gsed (GNU sed)
+- gmake (GNU make)
blob - /dev/null
blob + eddf7f09c1dc13b4ead5be421bc86e8a8317d3be (mode 644)
--- /dev/null
+++ config.mk
@@ -0,0 +1,4 @@
+CONFIG != if test -f "${TOP}/config.mk.local"; then echo 'local'; else uname; fi
+
+
+include ${TOP}/config.mk.${CONFIG}
blob - /dev/null
blob + 30df880edd1caada6cc14f593eb926b6171ed0f4 (mode 644)
--- /dev/null
+++ config.mk.NetBSD
@@ -0,0 +1,6 @@
+CC		= clang
+CXX		= clang++
+SUDO		= sudo
+CFLAGS_OS	= 
+LDFLAGS_OS	= -static
+CHROOT		= ${SUDO} chroot
blob - 30acdf6f2f674d5b82c35047169180ac73b2d2c3
blob + 1651307592973e8436df79798a17fb41478e1e6c
--- src/ecall.c
+++ src/ecall.c
@@ -596,16 +596,36 @@ void ecall (void)
 		ret = map (setuid ((uid_t)a0));
 		break;
 	case SYS_setresuid:
+#ifdef __NetBSD__
+		if (a0 != a1 || a0 != a2)
+			ret = enosys ("setresuid");
+		ret = map (setuid ((uid_t)a0));
+#else
 		ret = map (setresuid ((uid_t)a0, (uid_t)a1, (uid_t)a2));
+#endif
 		break;
 	case SYS_getresuid:
+#ifdef __NetBSD__
+		ret = enosys ("getresuid");
+#else
 		ret = map (getresuid (ptr (uid_t, a0), ptr (uid_t, a1), ptr (uid_t, a2)));
+#endif
 		break;
 	case SYS_setresgid:
+#ifdef __NetBSD__
+		if (a0 != a1 || a0 != a2)
+			ret = enosys ("setresgid");
+		ret = map (setgid ((uid_t)a0));
+#else
 		ret = map (setresgid ((gid_t)a0, (gid_t)a1, (gid_t)a2));
+#endif
 		break;
 	case SYS_getresgid:
+#ifdef __NetBSD__
+		ret = -enosys ("getresgid");
+#else
 		ret = map (getresgid (ptr (gid_t, a0), ptr (gid_t, a1), ptr (gid_t, a2)));
+#endif
 		break;
 	case SYS_setfsuid:
 		ret = enosys ("setfsuid");
blob - 806fe955200df51d44ca1af6e05d2acd4d8fb7ce
blob + 314f42b3d9490c1bec8447fb6abf932979f01e38
--- tools/Makefile
+++ tools/Makefile
@@ -1,12 +1,13 @@
-.POSIX:
-
-TOP != pwd
-STAMPS = ${TOP}/build/.stamps
-PREFIX = ${TOP}
+TOOLS != pwd
+TOP = ${TOOLS}/..
+STAMPS = ${TOOLS}/build/.stamps
+PREFIX = ${TOOLS}
 TARGET = riscv64-unknown-linux-musl
 ARCH = rv64ia
-CC = cc
+U = ${TOOLS}/util
 
+include ${TOP}/config.mk
+
 BINUTILS_VER = 2.42
 GCC_VER = 13.2.0
 MUSL_VER = 1.2.4
@@ -16,8 +17,8 @@ GMP_VER = 6.3.0
 MPFR_VER = 4.2.1
 MPC_VER = 1.3.1
 
-GMAKE = ${TOP}/gmake
-#SUDO = doas
+GMAKE = $U/gmake
+SUDO =
 
 all: build
 
@@ -30,7 +31,7 @@ clean:
 	rm -rf bin build include lib libexec ${TARGET} share
 
 test:
-	${PREFIX}/bin/${TARGET}-gcc -o /dev/null test.c
+	${PREFIX}/bin/${TARGET}-gcc -o /dev/null $U/test.c
 
 download: src/binutils.tgz src/gcc.tgz src/musl.tgz src/linux.tgz src/gmp.tgz src/mpfr.tgz src/mpc.tgz
 extract: ${STAMPS}/binutils-extract ${STAMPS}/gcc-extract ${STAMPS}/musl-extract ${STAMPS}/linux-extract ${STAMPS}/gmp ${STAMPS}/mpfr ${STAMPS}/mpc
@@ -44,31 +45,31 @@ install-binutils: ${STAMPS}/binutils-install
 
 src/binutils.tgz:
 	mkdir -p src
-	./download $@ https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VER}.tar.gz
+	$U/download $@ https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VER}.tar.gz
 
 src/gcc.tgz:
 	mkdir -p src
-	./download $@ https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VER}/gcc-${GCC_VER}.tar.gz
+	$U/download $@ https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VER}/gcc-${GCC_VER}.tar.gz
 
 src/musl.tgz:
 	mkdir -p src
-	./download $@ http://musl.libc.org/releases/musl-${MUSL_VER}.tar.gz
+	$U/download $@ http://musl.libc.org/releases/musl-${MUSL_VER}.tar.gz
 
 src/linux.tgz:
 	mkdir -p src
-	./download $@ https://mirrors.edge.kernel.org/pub/linux/kernel/v${LINUX_MAJOR}.x/linux-${LINUX_VER}.tar.gz
+	$U/download $@ https://mirrors.edge.kernel.org/pub/linux/kernel/v${LINUX_MAJOR}.x/linux-${LINUX_VER}.tar.gz
 
 src/gmp.tgz:
 	mkdir -p src
-	./download $@ https://gmplib.org/download/gmp/gmp-${GMP_VER}.tar.gz
+	$U/download $@ https://gmplib.org/download/gmp/gmp-${GMP_VER}.tar.gz
 
 src/mpfr.tgz:
 	mkdir -p src
-	./download $@ https://www.mpfr.org/mpfr-current/mpfr-${MPFR_VER}.tar.gz
+	$U/download $@ https://www.mpfr.org/mpfr-current/mpfr-${MPFR_VER}.tar.gz
 
 src/mpc.tgz:
 	mkdir -p src
-	./download $@ https://ftp.gnu.org/gnu/mpc/mpc-${MPC_VER}.tar.gz
+	$U/download $@ https://ftp.gnu.org/gnu/mpc/mpc-${MPC_VER}.tar.gz
 
 # EXTRACT
 
@@ -85,6 +86,7 @@ ${STAMPS}/gcc-extract: src/gcc.tgz
 	tar -C build -xzf src/gcc.tgz
 	mv build/gcc-* build/gcc
 	cd build/gcc && ln -sf ../gmp ../mpfr ../mpc .
+	$U/fix-gcc ${TOOLS}/build/gcc
 	touch $@
 
 ${STAMPS}/musl-extract: src/musl.tgz
@@ -99,7 +101,7 @@ ${STAMPS}/linux-extract: src/linux.tgz
 	rm -rf build/linux
 	tar -C build -xzf src/linux.tgz
 	mv build/linux-* build/linux
-	./fix-headers build/linux/scripts/headers_install.sh
+	$U/fix-headers ${TOOLS}/build/linux/scripts/headers_install.sh
 	touch $@
 
 ${STAMPS}/gmp: src/gmp.tgz
@@ -124,43 +126,46 @@ ${STAMPS}/mpc: src/mpc.tgz
 # KERNEL HEADERS
 
 ${STAMPS}/linux-headers: ${STAMPS}/linux-extract
-	cd ${TOP}/build/linux && ${GMAKE} ARCH=riscv HOSTCC=${CC} headers
-	rm -f ${TOP}/build/linux/usr/include/Makefile
-	rm -f ${TOP}/build/linux/usr/include/headers_check.pl
+	(cd ${TOOLS}/build/linux && ${GMAKE} ARCH=riscv HOSTCC="${CC}" headers)
+	rm -f ${TOOLS}/build/linux/usr/include/Makefile
+	rm -f ${TOOLS}/build/linux/usr/include/headers_check.pl
 	touch $@
 
 ${STAMPS}/linux-hdrinst: ${STAMPS}/linux-headers
 	mkdir -p ${PREFIX}/${TARGET}
-	cd ${TOP}/build/linux && ${SUDO} cp -rf ${TOP}/build/linux/usr/include ${PREFIX}/${TARGET}/
+	cd ${TOOLS}/build/linux && ${SUDO} cp -rf ${TOOLS}/build/linux/usr/include ${PREFIX}/${TARGET}/
 	touch $@
 
 # BINUTILS
 
 ${STAMPS}/binutils-configure: ${STAMPS}/binutils-extract
-	mkdir -p ${TOP}/build/binutils/build
-	cd ${TOP}/build/binutils/build && ../configure	\
+	mkdir -p ${TOOLS}/build/binutils/build
+	cd ${TOOLS}/build/binutils/build		\
+	&& CC="${CC}" CXX="${CXX}" ../configure		\
 		--prefix=${PREFIX}			\
 		--target=${TARGET}			\
 		--with-system-zlib			\
 		--with-arch=${ARCH}			\
+		--without-zstd				\
 		--disable-nls				\
 		--disable-werror			\
 		--disable-multilib
 	touch $@
 
 ${STAMPS}/binutils-build: ${STAMPS}/binutils-configure
-	cd ${TOP}/build/binutils/build && ${GMAKE}
+	(cd ${TOOLS}/build/binutils/build && ${GMAKE})
 	touch $@
 
 ${STAMPS}/binutils-install: ${STAMPS}/binutils-build
-	cd ${TOP}/build/binutils/build && ${SUDO} ${GMAKE} install
+	(cd ${TOOLS}/build/binutils/build && ${SUDO} ${GMAKE} install)
 	touch $@
 
 # GCC
 
 ${STAMPS}/gcc-configure: ${STAMPS}/gcc-extract ${STAMPS}/binutils-install ${STAMPS}/gmp ${STAMPS}/mpfr ${STAMPS}/mpc
-	mkdir -p ${TOP}/build/gcc/build
-	cd ${TOP}/build/gcc/build && ../configure	\
+	mkdir -p ${TOOLS}/build/gcc/build
+	cd ${TOOLS}/build/gcc/build			\
+	&& CC="${CC}" CXX="${CXX}" ../configure		\
 		--prefix=${PREFIX}			\
 		--target=${TARGET}			\
 		--with-system-zlib			\
@@ -182,18 +187,18 @@ ${STAMPS}/gcc-configure: ${STAMPS}/gcc-extract ${STAMP
 	touch $@
 
 ${STAMPS}/gcc-build: ${STAMPS}/gcc-configure
-	cd ${TOP}/build/gcc/build && ${GMAKE} all-gcc
+	(cd ${TOOLS}/build/gcc/build && ${GMAKE} all-gcc)
 	touch $@
 
 ${STAMPS}/gcc-install: ${STAMPS}/gcc-build
-	cd ${TOP}/build/gcc/build && ${SUDO} ${GMAKE} install-gcc
+	(cd ${TOOLS}/build/gcc/build && ${SUDO} ${GMAKE} install-gcc)
 	touch $@
 
 # MUSL
 
 ${STAMPS}/musl-configure: ${STAMPS}/musl-extract ${STAMPS}/gcc-install
-	mkdir -p ${TOP}/build/musl/build
-	cd ${TOP}/build/musl/build &&			\
+	mkdir -p ${TOOLS}/build/musl/build
+	cd ${TOOLS}/build/musl/build &&			\
 		CROSS_COMPILE=${PREFIX}/bin/${TARGET}-	\
 		../configure				\
 		--prefix=${PREFIX}/${TARGET}		\
@@ -204,19 +209,19 @@ ${STAMPS}/musl-configure: ${STAMPS}/musl-extract ${STA
 	touch $@
 
 ${STAMPS}/musl-build: ${STAMPS}/musl-configure ${STAMPS}/linux-hdrinst
-	cd ${TOP}/build/musl/build && ${GMAKE}
+	(cd ${TOOLS}/build/musl/build && ${GMAKE})
 	touch $@
 
 ${STAMPS}/musl-install: ${STAMPS}/musl-build
-	cd ${TOP}/build/musl/build && ${SUDO} ${GMAKE} install
+	(cd ${TOOLS}/build/musl/build && ${SUDO} ${GMAKE} install)
 	touch $@
 
 # LIBGCC
 
 ${STAMPS}/libgcc-build: ${STAMPS}/gcc-install ${STAMPS}/musl-install
-	cd ${TOP}/build/gcc/build && ${GMAKE} all-target-libgcc
+	(cd ${TOOLS}/build/gcc/build && ${GMAKE} all-target-libgcc)
 	touch $@
 
 ${STAMPS}/libgcc-install: ${STAMPS}/libgcc-build
-	cd ${TOP}/build/gcc/build && ${SUDO} ${GMAKE} install-target-libgcc
+	(cd ${TOOLS}/build/gcc/build && ${SUDO} ${GMAKE} install-target-libgcc)
 	touch $@
blob - 0b2fcbd58e1b0e3015360614c9ba5cfa3a9f31d8 (mode 755)
blob + /dev/null
--- tools/download
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/bin/sh
-
-has() {
-	command -v "$1" >/dev/null 2>&1
-}
-
-case "$(uname)" in
-OpenBSD)
-	ftp -o "$1" "$2"
-	;;
-FreeBSD)
-	fetch -o "$1" "$2"
-	;;
-*)
-	if has 'wget'; then
-		wget -qO "$1" "$2"
-	elif has 'curl'; then
-		curl -sLo "$1" "$2"
-	else
-		echo "No download program found for $(uname)" >&2
-		exit 1
-	fi
-	;;
-esac	
blob - /dev/null
blob + 25dda49b779bd6928fa9b6330ecd2a3fbf677fed (mode 755)
--- /dev/null
+++ tools/util/download
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+has() {
+	command -v "$1" >/dev/null 2>&1
+}
+
+case "$(uname)" in
+OpenBSD|NetBSD)
+	ftp -o "$1" "$2"
+	;;
+FreeBSD)
+	fetch -o "$1" "$2"
+	;;
+*)
+	if has 'wget'; then
+		wget -qO "$1" "$2"
+	elif has 'curl'; then
+		curl -sLo "$1" "$2"
+	else
+		echo "No download program found for $(uname)" >&2
+		exit 1
+	fi
+	;;
+esac	
blob - /dev/null
blob + ca9433ccaa3075d23764dd7d93cc0af0f5af2172 (mode 755)
--- /dev/null
+++ tools/util/fix-gcc
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+case "$(uname)" in
+NetBSD)
+	sed -i 's/#include <math.h>/#include <cmath>/' "$1/gcc/config/riscv/genrvv-type-indexer.cc"
+	;;
+*)
+	;;
+esac
blob - /dev/null
blob + eda9290c5b5e071d6c54732c6f8660347dc9b59e (mode 755)
--- /dev/null
+++ tools/util/fix-headers
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+case "$(uname)" in
+Linux)
+	;;
+*BSD)
+	sed -ibu 's/sed/gsed/g' "$1"
+	;;
+esac
blob - /dev/null
blob + 392b307b7a35390f6d8f226b248c98c414e06788 (mode 755)
--- /dev/null
+++ tools/util/gmake
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+case "$(uname)" in
+Linux)
+	exec make "$@"
+	;;
+*BSD)
+	ncpu=$(echo "$MAKEFLAGS" | grep -oE -- '-j [0-9]+')
+	keep=$(echo "$MAKEFLAGS" | grep -o -- '-k')
+	unset MAKEFLAGS
+	exec gmake $ncpu $keep "$@"
+	;;
+*)
+	echo "Unsupported OS: $(uname)" >&2
+	exit 1
+	;;
+esac	
blob - /dev/null
blob + 3fe11fdc6a9a70f00cbb50cb8bdbf5aa6c0c0481 (mode 644)
--- /dev/null
+++ tools/util/test.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+int main (void)
+{
+	puts ("Hello World");
+	return 0;
+}
blob - eda9290c5b5e071d6c54732c6f8660347dc9b59e (mode 755)
blob + /dev/null
--- tools/fix-headers
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-case "$(uname)" in
-Linux)
-	;;
-*BSD)
-	sed -ibu 's/sed/gsed/g' "$1"
-	;;
-esac
blob - 351d45dfc5b81b06bdd2cce947359246cec2e42b (mode 755)
blob + /dev/null
--- tools/gmake
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/sh
-
-case "$(uname)" in
-Linux)
-	make "$@"
-	;;
-*BSD)
-	gmake "$@"
-	;;
-*)
-	echo "Unsupported OS: $(uname)" >&2
-	exit 1
-	;;
-esac	
blob - 3fe11fdc6a9a70f00cbb50cb8bdbf5aa6c0c0481 (mode 644)
blob + /dev/null
--- tools/test.c
+++ /dev/null
@@ -1,7 +0,0 @@
-#include <stdio.h>
-
-int main (void)
-{
-	puts ("Hello World");
-	return 0;
-}