-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
303 lines (251 loc) · 7.55 KB
/
build.sh
File metadata and controls
303 lines (251 loc) · 7.55 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env bash
## build.sh
# This is the official build script for the SystemUP, for it to work you need to have the following tools installed:
# Git Bash, and GCC (GNU Compiler Collection).
##
## Usage:
# ./build.sh [OPTIONS]
# Options:
# -h, --help Show this help message
# -v, --version Show version information
# -c, --clean Clean the build directory before building
# -b, --build Build the project
# -t, --test Run tests after building
# -d, --debug Enable debug mode
# -o, --output Specify output directory (default: build)
__NAME__=$(basename "$0")
__VERSION__="0.0.1"
__AUTHOR__="h4rl"
__DESCRIPTION__="Build script for SystemUP"
__LICENSE__="BSD 3-Clause License"
# This script is free software: you can redistribute it and/or modify
# it under the terms of the BSD 3-Clause License.
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# BSD 3-Clause License for more details.
# You should have received a copy of the BSD 3-Clause License
# along with this script. If not, see <https://opensource.org/license/bsd-3-clause/>.
# This script is intended to be run from the root directory of the SystemUP project.
# It will build the project and output the results to the 'build' directory.
COLORS=true # set to false to disable colored output
OUTPUT_DIR="./build"
DEBUG=false
TEST=false
C_FLAGS="-Wall -Wextra -Werror -Wno-unused-parameter -Wno-unused-but-set-variable -Wno-unused-but-set-parameter -Wno-unused-variable -pedantic -std=c99 -O2"
INCLUDE_DIRS="-I./include"
LD_FLAGS="-mwindows -lpowrprof -lversion"
TEST_FILES=""
C_FILES=""
CC="gcc" # doesn't support address sanitizer, so we use clang if debug is enabled
if ${COLORS}; then
ESCAPE=$(printf "\e")
RED="${ESCAPE}[0;31m"
GREEN="${ESCAPE}[0;32m"
YELLOW="${ESCAPE}[1;33m"
BLUE="${ESCAPE}[0;34m"
CYAN="${ESCAPE}[0;36m"
CLEAR="${ESCAPE}[0m"
fi
print_line() {
level=$1
message=$2
if [ -z "$message" ]; then
message="No message provided"
fi
if [ -z "$level" ]; then
level="info"
fi
case $level in
"info")
echo -e "${GREEN}>${CLEAR} $message"
;;
"warn")
echo -e "${YELLOW}?${CLEAR} $message"
;;
"error")
echo -e "${RED}! $message${CLEAR}"
exit 1
;;
"success")
echo -e "${GREEN}✓${CLEAR} $message"
;;
*)
echo -e "${GREEN}>${CLEAR} $message"
;;
esac
}
usage() {
cat <<EOF
${BLUE}Usage${CLEAR}: $__NAME__ [OPTIONS] <args>
$__DESCRIPTION__
${GREEN}Author${CLEAR}: $__AUTHOR__
${GREEN}Version${CLEAR}: $__VERSION__
${GREEN}License${CLEAR}: $__LICENSE__
Options:
${RED} -h --help ${CLEAR} Show this help message
${RED} -v --version ${CLEAR} Show version information
${RED} -c --compile ${CLEAR} Compile the project
${RED} -l --link ${CLEAR} Link the object files
${RED} -b --build (no args) ${CLEAR} Build the project
${RED} -t --test ${CLEAR} Run tests after building
${RED} -d --debug ${CLEAR} Enable debug mode (enables verbose output, debugging symbols, and the address sanitizer)
${RED} -o --output ${CYAN}<dir>${CLEAR} Specify output directory (default: build)
${RED} -z --zip ${CLEAR} Create a zip file for release
EOF
exit 0
}
config_compile() {
local c_file_list
local test_file_list
local c_file
local test_file
if $TEST; then
test_file_list=$(find ./tests -type f -name "*.c")
else
c_file_list=$(find ./src -type f -name "*.c")
fi
if [ -z "$c_file_list" ]; then
print_line "error" "No C files found in ./src"
fi
if $TEST && [ -z "$test_file_list" ]; then
print_line "error" "No C test files found in ./tests"
fi
if $TEST; then
print_line "info" "Preparing tests for compilation..."
for test_file in $test_file_list; do
TEST_FILES+="$test_file "
done
fi
print_line "info" "Preparing source files for compilation..."
for c_file in $c_file_list; do
C_FILES+="$c_file "
done
if ${DEBUG}; then
print_line "info" "Building with debug enabled"
CC="clang"
C_FLAGS+=" -fsanitize=address -g"
LD_FLAGS+=" -fsanitize=address"
fi
print_line "info" "Building resource file..."
if ! windres -i ./assets/resource.rc -O coff -o "$OUTPUT_DIR/out/systemup_res.o"; then
print_line "error" "Failed to build resource file"
fi
print_line "info" "Making build directory"
mkdir -p "$OUTPUT_DIR/bin"
mkdir -p "$OUTPUT_DIR/out"
mkdir -p "$OUTPUT_DIR/tests/out"
mkdir -p "$OUTPUT_DIR/tests/bin"
}
compile() {
local source_file
local test_file
local source_index
local source_total
local test_index
local test_total
config_compile
source_total=$(echo "$C_FILES" | wc -w)
source_index=1
test_total=$(echo "$TEST_FILES" | wc -w)
test_index=1
for source_file in $C_FILES; do
print_line "info" "[${source_index}/${source_total}] - Compiling $source_file..."
if "$CC" -c $C_FLAGS $INCLUDE_DIRS $source_file -o "$OUTPUT_DIR/out/$(basename "$source_file" .c).o" $LD_FLAGS; then
print_line "success" "[${source_index}/${source_total}] - Compiled $source_file"
source_index=$((source_index + 1))
else
print_line "error" "Failed to compile $source_file"
fi
done
if $TEST; then
for test_file in $TEST_FILES; do
print_line "info" "[${test_index}/${test_total}] - Compiling test $test_file..."
if "$CC" -c $C_FLAGS $INCLUDE_DIRS $test_file -o "$OUTPUT_DIR/tests/out/$(basename "$test_file" .c).o" $LD_FLAGS; then
print_line "success" "[${test_index}/${test_total}] - Compiled test $test_file"
test_index=$((test_index + 1))
else
print_line "error" "Failed to compile test $test_file"
fi
done
fi
print_line "success" "Done compiling!"
}
link_() {
O_FILES=$(find "$OUTPUT_DIR/out" -name "*.o")
if [ -z "$O_FILES" ]; then
print_line "error" "No object files found in ./build/out"
fi
if "$CC" -o "${OUTPUT_DIR}/bin/systemup.exe" $O_FILES $LD_FLAGS; then
print_line "success" "Successfully linked object files to ./build/bin/systemup.exe"
else
print_line "error" "Failed to link object files to systemup.exe"
fi
exit 0
}
build() {
print_line "info" "Building SystemUP..."
compile
link_
}
zip_for_release() {
if [ ! -d "$OUTPUT_DIR" ]; then
print_line "error" "Output directory $OUTPUT_DIR does not exist. Please build first."
fi
if [ -f "$OUTPUT_DIR/systemup.zip" ]; then
rm "$OUTPUT_DIR/systemup.zip"
fi
print_line "info" "Zipping the build directory..."
if zip -j "$OUTPUT_DIR/systemup.zip" "$OUTPUT_DIR/bin/systemup.exe" ./assets/install.ps1 ./assets/readme.txt; then
print_line "success" "Successfully created systemup.zip in $OUTPUT_DIR"
else
print_line "error" "Failed to create systemup.zip"
fi
exit 0
}
if [ $# -eq 0 ]; then
build
fi
i=0
for arg in "$@"; do
case $arg in
-h|--help)
usage
;;
-v|--version)
print_line "info" "$__NAME__ version $__VERSION__"
exit 0
;;
-c|--compile)
compile
;;
-l|--link)
link_
;;
-b|--build)
build
;;
-t|--test)
TEST=true
print_line "info" "Will run tests after building"
;;
-d|--debug)
DEBUG=true
print_line "info" "Debug mode enabled"
;;
-o|--output)
OUTPUT_DIR_IDX=$((i + 1))
OUTPUT_DIR="${*:$OUTPUT_DIR_IDX:1}"
;;
-z|--zip)
zip_for_release
;;
*)
usage
;;
esac
i=$((i + 1))
if [ $i -eq $# ]; then
build
fi
done