-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·192 lines (158 loc) · 4.59 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·192 lines (158 loc) · 4.59 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env bash
DIR_ROOT="$(dirname "$(readlink -f "$0")")"
cd "$DIR_ROOT"
BUILD_SCRIPT_NAME="$(basename "$0")"
NAME_PROJECT="$(basename "$(pwd)")"
BIN_NAME=$NAME_PROJECT
DIR_BIN="bin"
mkdir -p "$DIR_BIN"
DIR_SRC_MAIN="src"
DIR_SRC_EXTERNAL="src/external"
DIR_SRC_TESTS="tests"
BUILD_TYPE="debug"
ARG_VERBOSE=false
NO_RUN=false
LEAKS=false
compilation_flags_constant=(
"-Wno-unused-function"
"-std=c99"
)
compilation_flags_debug=(
"-O0"
"-DDEBUG"
"-pedantic"
"-pedantic-errors"
"-g"
"-Wall"
"-Wextra"
"-Wshadow"
"-Werror"
)
compilation_flags_release=(
"-flto"
"-O2"
"-DNDEBUG"
)
log_details() {
if [ $ARG_VERBOSE = true ]; then
echo "[INFO] $1"
fi
}
err() {
echo "[ERROR] $1"
exit 1
}
help() {
echo "Usage: $BUILD_SCRIPT_NAME [options|commands]"
echo "Execute without options to compile and run a debug build."
echo "Program arguments are passed after a divider '--'."
echo ""
echo "Commands:"
echo "(NONE) Build (and run) with debug flags."
echo "release Build (and run) with release flags."
echo "test Build (and run) tests with debug flags."
echo "leaks Build with debug flags and check for leaks."
echo ""
echo "Options:"
echo "-n, --no-run Don't run program after build."
echo "-v, --verbose Enable verbose build details."
echo "-h, --help Display help."
}
PASS_THROUGH_ARGS=()
arg_idx=0
for arg in "$@"
do
case "$arg" in
--)
# Capture everything after this index
((idx++))
PASS_THROUGH_ARGS=("${@:$((idx+1))}")
break
;;
release)
BUILD_TYPE="release"
BIN_NAME="${BIN_NAME}-release"
;;
test)
BUILD_TYPE="test"
BIN_NAME="${BIN_NAME}-test"
compilation_flags_debug+=("-DTEST")
;;
leaks) LEAKS=true ;;
-v|--verbose) ARG_VERBOSE=true ;;
-h|--help) help; exit ;;
-n|--no-run) NO_RUN=true ;;
esac
((idx++))
done
if [[ "$BUILD_TYPE" == "debug" ]]; then
if [[ "$LEAKS" == false ]]; then
compilation_flags_debug+=("-fsanitize=address")
compilation_flags_debug+=("-fsanitize=undefined")
compilation_flags_debug+=("-fno-omit-frame-pointer")
compilation_flags_debug+=("-fno-omit-frame-pointer")
fi
BIN_NAME="${BIN_NAME}-debug"
fi
if commit=$(git rev-parse --short=7 HEAD 2>/dev/null); then
BIN_NAME="${BIN_NAME}-${commit}"
fi
if tag=$(git describe --tags --abbrev=0 2>/dev/null); then
BIN_NAME="${BIN_NAME}-${tag}"
fi
src_files_str_src=$(find "$DIR_SRC_MAIN" "$DIR_SRC_EXTERNAL" | sed -n '/\.c/p')
src_files_str_tests=$(find "$DIR_SRC_TESTS" | sed -n '/\.c/p')
declare -a SRC_FILES=($src_files_str_src $src_files_str_tests)
log_details "source files: $SRC_FILES"
log_details "build type: $BUILD_TYPE"
log_details "bin name: $BIN_NAME"
compile() {
local compiler=""
if $(cc -v 2>/dev/null); then
compiler="cc"
elif $(gcc -v 2>/dev/null); then
compiler="gcc"
elif $(clang -v 2>/dev/null); then
compiler="clang"
elif $(tcc -v 2>/dev/null); then
compiler="tcc"
else
err "no valid compiler could be found"
fi
log_details "compiler: $compiler"
set -e
if [[ "$BUILD_TYPE" == "release" ]]; then
log_details "flags: ${compilation_flags_constant[*]} ${compilation_flags_release[*]}"
$compiler "${compilation_flags_release[@]}" \
"${compilation_flags_constant[@]}" \
"${SRC_FILES[@]}" \
-o "$DIR_BIN"/"$BIN_NAME"
elif [[ "$BUILD_TYPE" == "test" ]]; then
log_details "flags: ${compilation_flags_debug[*]} ${compilation_flags_constant[*]}"
$compiler "${compilation_flags_debug[@]}" \
"${compilation_flags_constant[@]}" \
"${SRC_FILES[@]}" \
-o "$DIR_BIN"/"$BIN_NAME"
else # debug
log_details "flags: ${compilation_flags_constant[*]} ${compilation_flags_debug[*]}"
$compiler "${compilation_flags_debug[@]}" \
"${compilation_flags_constant[@]}" \
"${SRC_FILES[@]}" \
-o "$DIR_BIN"/"$BIN_NAME"
fi
set +e
}
run() {
log_details "destination: ./${DIR_BIN}/${BIN_NAME}"
if [[ $LEAKS == true ]]; then
MallocStackLogging=1 leaks -atExit -- \
"$DIR_BIN"/"$BIN_NAME" \
"${PASS_THROUGH_ARGS[@]}"
exit
fi
if [[ $NO_RUN == false ]]; then
"$DIR_BIN"/"$BIN_NAME" "${PASS_THROUGH_ARGS[@]}"
fi
}
compile
run