Commit Diff


commit - 6bf04703743693934f0f5a10196d93242b519acc
commit + 3528385d9abd041019a7c8986562d60124b69b6f
blob - a152ce5ff7756db87c05a0c99728b86c63a18c0e
blob + f2274af0927a0ca27546fc37257d52fd72a17993
--- Makefile
+++ Makefile
@@ -2,13 +2,18 @@
 .SUFFIXES:
 .SUFFIXES: .c .S .o .elf
 
-SUDO	= doas
 PREFIX	= /usr/local
-
 TARGET	= riscv64-unknown-linux-musl
+HOSTOS != uname
 CROSS	= ./tools/bin/${TARGET}
-CFLAGS	= -std=c2x -fPIC -O0 -g
-LDFLAGS	= -pie -static -lpthread
+CONFIG = config.mk.${HOSTOS}
+
+include ${CONFIG}
+
+COPT	= -g -O2
+CFLAGS	= ${CFLAGS_OS} ${COPT} -std=c2x
+LDFLAGS	= ${LDFLAGS_OS} -lpthread
+
 OBJ	= src/rvemu.o src/ecall.o src/cpu.o src/exec.o
 T	= asm
 PROGS	= examples/test.elf	\
@@ -29,6 +34,9 @@ run: rvemu ${PROGS}
 	cp -f test.txt rootfs/
 	${SUDO} chroot rootfs /bin/rvemu /bin/$T.elf
 
+distclean: clean
+	(cd tools; ${MAKE} distclean)
+
 clean:
 	rm -f rvemu src/*.o examples/*.elf *.core src/syscalls.h
 	rm -rf rootfs
blob - /dev/null
blob + b1b92eef4c8ca5b2367f60027a2e806d3c5afd3f (mode 644)
--- /dev/null
+++ config.mk.FreeBSD
@@ -0,0 +1,3 @@
+SUDO		= sudo
+CFLAGS_OS	= 
+LDFLAGS_OS	= -static
blob - 66eb875ea57dc87a40bf8b6cb2d3386f35feeb06
blob + d173422d7269a87bd9bc461fb94115f0977498e0
--- src/exec.c
+++ src/exec.c
@@ -55,7 +55,19 @@ int my_execve (const char *path, char **argv, char **e
 	close (fd);
 
 	if (is_executable (&ehdr)) {
+#if defined(__FreeBSD__)
+		extern char **environ;
+		char **old_env;
+		int st;
+
+		old_env = environ;
+		environ = envp;
+		st = execvp ("rvemu", argv);
+		environ = old_env;
+		return st;
+#else
 		return execvpe ("rvemu", argv, envp);
+#endif
 	}
 
 	return execve (path, argv, envp);
blob - /dev/null
blob + fe3046e7f083f5a16ad5a213697b4accf19607bd (mode 644)
--- /dev/null
+++ config.mk.OpenBSD
@@ -0,0 +1,3 @@
+SUDO		= doas
+CFLAGS_OS	= -fPIC
+LDFLAGS_OS	= -pie -static
blob - e777f65d1defd61013642483c67df9e3258a9bb5
blob + c4a75737828d8f647530bcb710baf137d1f439a3
--- tools/Makefile
+++ tools/Makefile
@@ -5,6 +5,7 @@ STAMPS = ${TOP}/build/.stamps
 PREFIX = ${TOP}
 TARGET = riscv64-unknown-linux-musl
 ARCH = rv64ia
+CC = cc
 
 BINUTILS_VER = 2.42
 GCC_VER = 13.2.0
@@ -22,11 +23,15 @@ all: build
 
 build: ${STAMPS}/libgcc-install
 
+distclean: clean
+	rm -rf src
+
 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 test test.c
+	./test; rm -f test
 
 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
@@ -40,31 +45,31 @@ install-binutils: ${STAMPS}/binutils-install
 
 src/binutils.tgz:
 	mkdir -p src
-	ftp -o $@ https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VER}.tar.gz
+	./download $@ https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VER}.tar.gz
 
 src/gcc.tgz:
 	mkdir -p src
-	ftp -o $@ https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VER}/gcc-${GCC_VER}.tar.gz
+	./download $@ https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VER}/gcc-${GCC_VER}.tar.gz
 
 src/musl.tgz:
 	mkdir -p src
-	ftp -o $@ http://musl.libc.org/releases/musl-${MUSL_VER}.tar.gz
+	./download $@ http://musl.libc.org/releases/musl-${MUSL_VER}.tar.gz
 
 src/linux.tgz:
 	mkdir -p src
-	ftp -o $@ https://mirrors.edge.kernel.org/pub/linux/kernel/v${LINUX_MAJOR}.x/linux-${LINUX_VER}.tar.gz
+	./download $@ https://mirrors.edge.kernel.org/pub/linux/kernel/v${LINUX_MAJOR}.x/linux-${LINUX_VER}.tar.gz
 
 src/gmp.tgz:
 	mkdir -p src
-	ftp -o $@ https://gmplib.org/download/gmp/gmp-${GMP_VER}.tar.gz
+	./download $@ https://gmplib.org/download/gmp/gmp-${GMP_VER}.tar.gz
 
 src/mpfr.tgz:
 	mkdir -p src
-	ftp -o $@ https://www.mpfr.org/mpfr-current/mpfr-${MPFR_VER}.tar.gz
+	./download $@ https://www.mpfr.org/mpfr-current/mpfr-${MPFR_VER}.tar.gz
 
 src/mpc.tgz:
 	mkdir -p src
-	ftp -o $@ https://ftp.gnu.org/gnu/mpc/mpc-${MPC_VER}.tar.gz
+	./download $@ https://ftp.gnu.org/gnu/mpc/mpc-${MPC_VER}.tar.gz
 
 # EXTRACT
 
@@ -95,7 +100,7 @@ ${STAMPS}/linux-extract: src/linux.tgz
 	rm -rf build/linux
 	tar -C build -xzf src/linux.tgz
 	mv build/linux-* build/linux
-	sed -i 's/sed/gsed/g' build/linux/scripts/headers_install.sh
+	./fix-headers build/linux/scripts/headers_install.sh
 	touch $@
 
 ${STAMPS}/gmp: src/gmp.tgz
blob - /dev/null
blob + 3cb5f2afc731ad975f04efb3e472d119ca0e2226 (mode 755)
--- /dev/null
+++ tools/download
@@ -0,0 +1,24 @@
+#!/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 -O "$1" "$2"
+	elif has 'curl'; then
+		curl -Lo "$1" "$2"
+	else
+		echo "No download program found for $(uname)" >&2
+		exit 1
+	fi
+	;;
+esac	
blob - /dev/null
blob + eda9290c5b5e071d6c54732c6f8660347dc9b59e (mode 755)
--- /dev/null
+++ tools/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 + 3fe11fdc6a9a70f00cbb50cb8bdbf5aa6c0c0481 (mode 644)
--- /dev/null
+++ tools/test.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+int main (void)
+{
+	puts ("Hello World");
+	return 0;
+}