-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·370 lines (308 loc) · 8.85 KB
/
build.sh
File metadata and controls
executable file
·370 lines (308 loc) · 8.85 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/sh
# vi: set et ft=sh ts=2 sw=2 fenc=utf-8 :vi
export LC_ALL=C
export TZ=UTC
IsBuildDebug=1
IsBuildEnabled=1
IsTestsEnabled=0
IsToolEnabled_hh_record_read=0
IsToolEnabled_hh_asset_builder=0
# Possible values: stbtt, freetype
TruetypeBackend='stbtt'
IsTruetypeBackendSTBTT=1
IsTruetypeBackendFreetype=0
usage() {
cat <<EOF
NAME
build.sh [OPTIONS]
DESCRIPTION
Build script of handmadehero.
OPTIONS
--debug
Build with debugging information.
--build-directory=path
Build executables in this folder. If directory not exists, one will be
created.
--disable-handmadehero
Do not build handmadehero binary.
--truetype-backend=stbtt|freetype
Which library used for font rendering.
Relevant for hh_asset_builder.
test
Run tests.
hh_record_read
Build hh_record_read tool.
hh_asset_builder
Build hh_asset_builder tool.
-h, --help
Display help page.
EXAMPLES
$ ./build.sh
Build only the handmadehero
$ ./build.sh --disable-handmadehero hh_asset_builder
Build only the hh_asset_builder tool.
EOF
}
for i in "$@"; do
case $i in
--debug)
IsBuildDebug=1
;;
--build-directory=*)
OutputDir="${i#*=}"
;;
--truetype-backend=*)
TruetypeBackend="${i#*=}"
IsTruetypeBackendSTBTT=0
IsTruetypeBackendFreetype=0
if [ "$TruetypeBackend" = 'freetype' ]; then
IsTruetypeBackendFreetype=1
elif [ "$TruetypeBackend" = 'stbtt' ]; then
IsTruetypeBackendSTBTT=1
fi
if [ $IsTruetypeBackendFreetype -eq 0 ] && [ $IsTruetypeBackendSTBTT -eq 0 ]; then
echo "truetype backend '$TruetypeBackend' is not recognized"
echo "possible values:"
echo " - stbtt"
echo " - freetype"
exit 1
fi
;;
--disable-handmadehero)
IsBuildEnabled=0
;;
test|tests)
IsBuildEnabled=0
IsTestsEnabled=1
;;
tool/hh_record_read/|tool/hh_record_read|hh_record_read)
IsToolEnabled_hh_record_read=1
;;
tool/hh_asset_builder/|tool/hh_asset_builder|hh_asset_builder)
IsToolEnabled_hh_asset_builder=1
;;
-h|-help|--help)
usage
exit 0
;;
*)
echo "argument $i not recognized"
usage
exit 1
;;
esac
done
################################################################
# TEXT FUNCTIONS
################################################################
# [0,1] StringContains(string, search)
StringContains() {
string="$1"
search="$2"
[ "${string##*$search}" != "$string" ] && echo 1 || echo 0
}
# [0,1] StringStartsWith(string, search)
StringStartsWith() {
string="$1"
search="$2"
[ "${string#$search}" != "$string" ] && echo 1 || echo 0
}
# [0,1] StringEndsWith(string, search)
StringEndsWith() {
string="$1"
search="$2"
[ "${string%$search}" != "$string" ] && echo 1 || echo 0
}
# string Basename(path)
Basename() {
path="$1"
echo "${path##*/}"
}
# string BasenameWithoutExtension(path)
BasenameWithoutExtension() {
path="$1"
basename="$(Basename "$path")"
echo ${basename%.*}
}
# string Dirname(path)
Dirname() {
path="$1"
if [ $(StringStartsWith "$path" '/') ]; then
dirname="${path%/*}"
if [ -z "$dirname" ]; then
echo '/'
else
echo $dirname
fi
else
echo '.'
fi
}
################################################################
# TIME FUNCTIONS
################################################################
StartTimer() {
startedAt=$(date +%s)
}
StopTimer() {
echo $(( $(date +%s) - $startedAt ))
}
################################################################
# LOG FUNCTIONS
################################################################
Timestamp="$(date +%Y%m%dT%H%M%S)"
Log() {
string=$1
output="$OutputDir/logs/build-$Timestamp.log"
if [ ! -e "$(Dirname "$output")" ]; then
mkdir "$(Dirname "$output")"
fi
echo "$string" >> "$output"
}
Debug() {
string=$1
output="$OutputDir/logs/build-$Timestamp.log"
if [ ! -e "$(Dirname "$output")" ]; then
mkdir "$(Dirname "$output")"
fi
echo "[DEBUG] $string" >> "$output"
}
################################################################
ProjectRoot="$(Dirname $(realpath "$0"))"
if [ "$(pwd)" != "$ProjectRoot" ]; then
echo "Must be call from project root!"
echo " $ProjectRoot"
exit 1
fi
OutputDir="${OutputDir:-$ProjectRoot/build}"
if [ ! -e "$OutputDir" ]; then
mkdir "$OutputDir"
# version control ignore
echo '*' > "$OutputDir/.gitignore"
echo 'syntax: glob' > "$OutputDir/.hgignore"
echo '**/*' > "$OutputDir/.hgignore"
fi
IsOSLinux=$(StringEndsWith "$(uname)" 'Linux')
cc="${CC:-clang}"
IsCompilerGCC=$(StringStartsWith "$("$cc" --version | head -n 1 -c 32)" "gcc")
IsCompilerClang=$(StringStartsWith "$("$cc" --version | head -n 1 -c 32)" "clang")
if [ $IsCompilerGCC -eq 0 ] && [ $IsCompilerClang -eq 0 ]; then
echo "unsupported compiler $cc. continue (y/n)?"
read input
if [ "$input" != 'y' ] && [ "$input" != 'Y' ]; then
exit 1
fi
echo "Assuming $cc as GCC"
IsCompilerGCC=1
fi
cflags="$CFLAGS"
# standard
cflags="$cflags -std=c99"
# performance
cflags="$cflags -O3"
if [ $(StringContains "$cflags" '-march=') -eq 0 ]; then
cflags="$cflags -march=x86-64-v3"
fi
cflags="$cflags -funroll-loops"
cflags="$cflags -fomit-frame-pointer"
# warnings
cflags="$cflags -Wall -Werror"
cflags="$cflags -Wconversion"
cflags="$cflags -Wno-unused-parameter"
cflags="$cflags -Wno-unused-result"
cflags="$cflags -Wno-missing-braces"
cflags="$cflags -DCOMPILER_GCC=$IsCompilerGCC"
cflags="$cflags -DCOMPILER_CLANG=$IsCompilerClang"
if [ $IsBuildDebug -eq 1 ]; then
#cflags="$cflags -g -O0"
cflags="$cflags -DHANDMADEHERO_DEBUG=1"
cflags="$cflags -DHANDMADEHERO_INTERNAL=1"
cflags="$cflags -Wno-unused-but-set-variable"
cflags="$cflags -Wno-unused-function"
cflags="$cflags -Wno-unused-variable"
fi
if [ $IsOSLinux -eq 1 ]; then
# needed by c libraries
cflags="$cflags -D_GNU_SOURCE=1"
cflags="$cflags -D_XOPEN_SOURCE=700"
fi
ldflags="${LDFLAGS}"
ldflags="$ldflags -Wl,--as-needed"
ldflags="${ldflags# }"
Log "Started at $(date '+%Y-%m-%d %H:%M:%S')"
Log "================================================================"
Log "root: $ProjectRoot"
Log "build: $OutputDir"
Log "os: $(uname)"
Log "compiler: $cc"
Log "cflags: $cflags"
if [ ! -z "$CFLAGS" ]; then
Log "from your env: $CFLAGS"
fi
Log "ldflags: $ldflags"
if [ ! -z "$LDFLAGS" ]; then
Log "from your env: $LDFLAGS"
fi
Log "================================================================"
LIB_M='-lm'
if [ $IsBuildEnabled -eq 1 ]; then
if [ $IsOSLinux -eq 0 ]; then
echo "Do not know how to compile on this OS"
echo " OS: $(uname)"
exit 1
elif [ $IsOSLinux -eq 1 ]; then
################################################################
# LINUX BUILD
# .--.
# |o_o |
# |:_/ |
# // \ \
# (| | )
# /'\_ _/`\
# \___)=(___/
################################################################
LIB_PTHREAD='-lpthread'
INC_LIBURING=$(pkg-config --cflags liburing)
LIB_LIBURING=$(pkg-config --libs liburing)
INC_LIBEVDEV=$(pkg-config --cflags libevdev)
LIB_LIBEVDEV=$(pkg-config --libs libevdev)
INC_WAYLAND_CLIENT=$(pkg-config --cflags wayland-client)
LIB_WAYLAND_CLIENT=$(pkg-config --libs wayland-client)
INC_XKBCOMMON=$(pkg-config --cflags xkbcommon)
LIB_XKBCOMMON=$(pkg-config --libs xkbcommon)
INC_LIBPIPEWIRE=$(pkg-config --cflags libpipewire-0.3)
LIB_LIBPIPEWIRE=$(pkg-config --libs libpipewire-0.3)
### libhandmadehero
src="$ProjectRoot/src/handmadehero.c"
output="$OutputDir/libhandmadehero.so"
inc="-I$ProjectRoot/include"
lib="$LIB_M"
StartTimer
"$cc" $cflags $ldflags $inc -fPIC -shared -o "$output" $src $lib
[ $? -eq 0 ] && echo "libhandmadehero compiled in $(StopTimer) seconds."
### handmadehero
# WaylandProtocolsInc= WaylandProtocolsSrc=
. $ProjectRoot/protocol/build.sh
src=""
src="$src $WaylandProtocolsSrc"
src="$src $ProjectRoot/src/handmadehero_linux.c"
src="${src# }"
output="$OutputDir/handmadehero"
inc="-I$ProjectRoot/include $INC_LIBURING $INC_LIBEVDEV $INC_WAYLAND_CLIENT $INC_XKBCOMMON $INC_LIBPIPEWIRE $WaylandProtocolsInc"
lib="$LIB_M $LIB_PTHREAD $LIB_LIBURING $LIB_LIBEVDEV $LIB_WAYLAND_CLIENT $LIB_XKBCOMMON $LIB_LIBPIPEWIRE"
StartTimer
"$cc" $cflags $ldflags $inc -o "$output" $src $lib
[ $? -eq 0 ] && echo "handmadehero compiled in $(StopTimer) seconds."
fi
fi
if [ $IsTestsEnabled -eq 1 ]; then
. "$ProjectRoot/test/build.sh"
fi
if [ $IsToolEnabled_hh_record_read -eq 1 ]; then
. "$ProjectRoot/tool/hh_record_read/build.sh"
fi
if [ $IsToolEnabled_hh_asset_builder -eq 1 ]; then
. "$ProjectRoot/tool/hh_asset_builder/build.sh"
fi
Log "================================================================"
Log "Finished at $(date '+%Y-%m-%d %H:%M:%S')"