summaryrefslogtreecommitdiff
path: root/build.sh
blob: 36aa12d8225097d5afc8e22948d8b4774ce70a29 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash

# Use project-local GCC if available, otherwise system GCC
TOOLCHAIN_GCC="./toolchain/gcc-15.2.0/bin/gcc"
if [ -f "${TOOLCHAIN_GCC}" ]; then
    CC="${TOOLCHAIN_GCC}"
else
    CC=gcc
fi

# Set the project name here
PROJECT_NAME="mknes"  # Change this for each new project

WIN_CC=x86_64-w64-mingw32-gcc

# Base configuration common to all builds
CFLAGS="-std=gnu11 -mtune=generic "
# -fdump-tree-alias "
CFLAGS+="-mbmi -fno-argument-alias "
CFLAGS+="-mfunction-return=keep -mindirect-branch=keep "
CFLAGS+="-fwrapv -ffast-math -fno-trapping-math -fvisibility=hidden "
CFLAGS+="-fno-stack-protector -fno-PIE -no-pie -fcf-protection=none -ffunction-sections -fdata-sections "
CFLAGS+="-fno-non-call-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables "
CFLAGS+="-Wall -Wextra -Wstrict-aliasing=3 -Wstrict-overflow=5 "
CFLAGS+="-Wno-unused-parameter -Wno-sign-compare -Wno-trigraphs -Wno-maybe-uninitialized "
CFLAGS+="-Wno-unused-variable -Wno-unused-const-variable -Wno-unused-function -Wno-write-strings -Wno-missing-field-initializers "
CFLAGS+="-U_FORTIFY_SOURCE -fno-pic "

# Enable occationally to check for errors..
# CFLAGS+="-fanalyzer "

LDFLAGS="-Wl,--gc-sections -Wl,--as-needed "

# Base include paths (common for all platforms)
INCLUDE_PATHS="-Ibase -I../mkfw "

# Linux-specific includes and libraries
LINUX_INCLUDE=" "
LINUX_LIBS="-lXi -lX11 -lGL -lm -ldl -pthread -larchive "

# Windows-specific includes and libraries
WINDOWS_INCLUDE=""
WINDOWS_LIBS="-lwinmm -lgdi32 -lopengl32 -lavrt "
# To enable ZIP support via libarchive, uncomment the line below and comment the line above:
# WINDOWS_LIBS="-static -larchive -lz -lbz2 -llzma -lbcrypt -lwinmm -lgdi32 -lopengl32"
# You'll also need to add -DUSE_LIBARCHIVE to CFLAGS

# Determine build type-specific flags
BUILD_TYPE=$1

if [ -z "$BUILD_TYPE" ]; then
	BUILD_TYPE="normal"
fi

case "$BUILD_TYPE" in
	"normal")
		CFLAGS+="-ggdb -fno-omit-frame-pointer -O2 -DDEBUG_INTERNAL "
		# CFLAGS+="-fsanitize=address,undefined,alignment,object-size,unreachable "
#  -pg # for gprof
		;;
	"release")
		CFLAGS+="-s -Wl,--strip-all -O2 "
		;;
	"profile")
		CFLAGS+="-O2 -fprofile-generate -ftest-coverage -DBENCHMARK "
		;;
	"profile_release")
		# CFLAGS+="-s -Wl,--strip-all -O2 -fprofile-use "
		CFLAGS+="-g -O2 -fprofile-use -DBENCHMARK "
		;;
	"debug")
		CFLAGS+="-g -O0 "
# -DTIMER_DEBUG "
		LDFLAGS+="-fno-pie -no-pie "
		;;
	"coverage")
		gcov -b -c *.c
		exit 0
		;;

	"clean")
		rm -f *.gcda *.gcno *.gcov perf.data* *.alias mknes
		exit 0
		;;
	*)
		echo "Unknown build type: $BUILD_TYPE"
		exit 1
		;;
esac

# Make sure the compilation stops if any error happens.
set -e

# Build Linux version
(
	$CC $CFLAGS ${PROJECT_NAME}.c -o ${PROJECT_NAME} $INCLUDE_PATHS $LINUX_INCLUDE $LDFLAGS $LINUX_LIBS
	objdump -d -Mintel mknes > mknes.s
) &

# Build Windows version
#(
#	$WIN_CC $CFLAGS ${PROJECT_NAME}.c -o ${PROJECT_NAME}.exe $INCLUDE_PATHS $WINDOWS_INCLUDE $LDFLAGS $WINDOWS_LIBS
#) &

wait