aboutsummaryrefslogtreecommitdiffstats
path: root/compile.sh
blob: 62399ceb4980721b0a85581b0ecc052fcbfe89a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# author: nasr
# Runs on Linux and macOS. macOS ships bash 3.2 and BSD tools: no cp -u, no date %N, no mold.
set -e

MODE=debug
if [ $# -gt 0 ] && [[ "$1" != -* ]]; then
	MODE=$1
	shift
fi

OS=$(uname -s)
BUILD_DIR=build
OFLAGS="-vet -vet-cast -strict-style -warnings-as-errors -error-pos-style:unix"
if [ "$OS" = "Linux" ]; then
	OFLAGS+=" -linker:mold"
fi

if [ "${ASAN:-0}" -eq 1 ]; then
	OFLAGS+=" -sanitize:address"
fi

now_ms() {
	if [ "$OS" = "Darwin" ]; then
		perl -MTime::HiRes=time -e 'printf "%d\n", time * 1000'
	else
		echo $(($(date +%s%N) / 1000000))
	fi
}

mkdir -p "$BUILD_DIR"
START=$(now_ms)

case "$MODE" in
	debug)
		if [ "$OS" = "Darwin" ]; then
			LIB_EXT=dylib
			RAYLIB_LIB="$(odin root)/vendor/raylib/macos/libraylib.620.dylib"
			RPATH='-Wl,-rpath,@loader_path'
		else
			LIB_EXT=so
			RAYLIB_LIB="$(odin root)/vendor/raylib/linux/libraylib.so.620"
			if [ "$(uname -m)" = "aarch64" ]; then
				RAYLIB_LIB="$(odin root)/vendor/raylib/linux-arm64/libraylib.so.620"
			fi
			RPATH='-Wl,-rpath=$ORIGIN'
		fi
		echo "Building ${BUILD_DIR}/drift + ${BUILD_DIR}/game.${LIB_EXT} [debug, hot reload]"
		# Copy only when newer: overwriting the raylib the running game has loaded crashes it.
		if [ "$RAYLIB_LIB" -nt "$BUILD_DIR/$(basename "$RAYLIB_LIB")" ]; then
			cp "$RAYLIB_LIB" "$BUILD_DIR/"
		fi
		odin build source/game $OFLAGS -debug -o:none -use-separate-modules \
			-build-mode:dll -define:RAYLIB_SHARED=true -define:DRIFT_DEBUG=true -extra-linker-flags:"$RPATH" \
			-out:"$BUILD_DIR/game_tmp.$LIB_EXT" "$@"
		mv "$BUILD_DIR/game_tmp.$LIB_EXT" "$BUILD_DIR/game.$LIB_EXT"
		odin build source/host $OFLAGS -debug -o:none -out:"$BUILD_DIR/drift_tmp" "$@"
		mv "$BUILD_DIR/drift_tmp" "$BUILD_DIR/drift"
		;;
	profile)
		echo "Building ${BUILD_DIR}/drift [profile, single exe]"
		odin build source/release $OFLAGS -debug -o:speed -define:DRIFT_PROFILE=true -out:"$BUILD_DIR/drift" "$@"
		;;
	release)
		echo "Building ${BUILD_DIR}/drift [release, single exe]"
		odin build source/release $OFLAGS -o:speed -disable-assert -out:"$BUILD_DIR/drift" "$@"
		;;
	*)
		echo "usage: $0 [debug|profile|release] [extra odin flags]"
		exit 1
		;;
esac

END=$(now_ms)
echo "==== BUILD SUCCESS in $((END - START)) ms ===="