summaryrefslogtreecommitdiff
path: root/miniany/build.sh
diff options
context:
space:
mode:
Diffstat (limited to 'miniany/build.sh')
-rwxr-xr-xminiany/build.sh82
1 files changed, 82 insertions, 0 deletions
diff --git a/miniany/build.sh b/miniany/build.sh
new file mode 100755
index 0000000..0db0d13
--- /dev/null
+++ b/miniany/build.sh
@@ -0,0 +1,82 @@
+#!/bin/bash
+
+BINARY=${1:-cc}
+COMPILER=${2:-gcc}
+MODE=${3:-freestanding}
+LEVEL=${4:-d}
+
+DEBUG=0
+
+declare -a MODULES
+MODULES+=("libc-${MODE}.c")
+MODULES+=("$BINARY.c")
+
+case "${COMPILER}" in
+ gcc)
+ CFLAGS="-m32 -march=i386 -Wall -pedantic -Werror -std=c89 -x c"
+ ;;
+ clang)
+ CFLAGS="-m32 -march=i386 -Werror -Wall -pedantic -std=c89 -x c"
+ ;;
+ pcc)
+ CFLAGS="-march=i386 -Werror -Wall -std=c89 -x c"
+ ;;
+ tcc)
+ CFLAGS="-m32 -march=i386 -Werror -Wall -std=c89"
+ ;;
+ *)
+ echo "ERROR: Unknown compiler '${COMPILER}' (use gcc, clang, pcc or tcc)" 1>&2
+ exit 1
+ ;;
+esac
+
+case "${LEVEL}" in
+ 0|1|2|3)
+ CFLAGS+=" -O${LEVEL}"
+ ;;
+ d)
+ CFLAGS+=" -g -O0"
+ DEBUG=1
+ ;;
+ *)
+ echo "ERROR: Unknown compilation level '${LEVEL}' (use one of 0123 for -O<n>, or d for -O0 and debugging)" 1>&2
+ exit 1
+ ;;
+esac
+
+case "${MODE}" in
+ freestanding|hosted)
+ ;;
+ *)
+ echo "ERROR: Unknown environment '${MODE}' (use 'freestanding' or 'hosted')" 1>&2
+ exit 1
+ ;;
+esac
+
+case "${COMPILER}:${MODE}" in
+ gcc:freestanding)
+ CFLAGS+=" -ffreestanding -fno-stack-protector -nostdlib -emain -fno-omit-frame-pointer"
+ ;;
+ clang:freestanding)
+ CFLAGS+=" -ffreestanding -fno-stack-protector -nostdlib -Wl,-emain -fno-omit-frame-pointer"
+ ;;
+ pcc:freestanding)
+ CFLAGS+=" -ffreestanding -nostdlib -Wl,-emain"
+ ;;
+ tcc:freestanding)
+ CFLAGS+=" -fno-bultin -nostdlib"
+ MODULES+=("_start-stub.c")
+ ;;
+ *:hosted)
+ #~ CFLAGS+=" -lbsd"
+ ;;
+esac
+
+echo "${COMPILER} ${CFLAGS} -o ${BINARY} ${MODULES[@]}"
+if [ "${DEBUG}" = 1 ]; then
+ cat ${MODULES[@]} > "${BINARY}_tmp.c"
+ ${COMPILER} ${CFLAGS} -o ${BINARY} "${BINARY}_tmp.c"
+else
+ cat ${MODULES[@]} | ${COMPILER} ${CFLAGS} -o ${BINARY} -
+fi
+