-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·228 lines (178 loc) · 5.27 KB
/
build.sh
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
#!/usr/bin/env bash
# Build script for kanso engine
# Uncomment for debug
#set -x
OPTIONS=':hxsvg'
# Option-related
STRIP=0 # Becomes 1 if `-s` is passed
CLEAN=0 # Becomes 1 if `-x` is passed
VERBOSE=0 # Becomes 1 if `-v` is passed
GENERATE_ONLY=0 # Becomes 1 if `-g` is passed
# Project directories
OBJ_DIR="obj"
OUT_DIR="bin"
# Print args to STDERR
error() {
local TXT=("$@")
printf "%s\n" "${TXT[@]}" >&2
return 0
}
verb_error() {
if [[ $VERBOSE -eq 1 ]] && [[ $# -ge 1 ]]; then
error "$@"
fi
return 0
}
verb_log() {
if [[ $VERBOSE -eq 1 ]] && [[ $# -ge 1 ]]; then
local TXT=("$@")
printf "%s\n" "${TXT[@]}"
fi
return 0
}
# Terminate script execution
die() {
local EC=1
if [[ $# -ge 1 ]] && [[ $1 =~ ^(0|-?[1-9][0-9]*)$ ]]; then
EC=$1
shift
fi
if [[ $# -ge 1 ]]; then
local TXT=("$@")
if [[ $EC -eq 0 ]]; then
printf "%s\n" "${TXT[@]}"
else
error "${TXT[@]}"
fi
fi
# Make sure to kill debugging
set +x
exit "$EC"
}
# Determine if one or more commands exist in shell or not
_cmd() {
[[ $# -eq 0 ]] && return 127
local EC=0
while [[ $# -gt 0 ]]; do
if ! command -v "$1" &> /dev/null; then
EC=1
break
fi
shift
done
return "$EC"
}
# Print help message
usage() {
local EC=0
if [[ $# -ge 1 ]] && [[ $1 =~ ^(0|-?[1-9][0-9]*)$ ]]; then
EC="$1"
fi
local TXT=(
"build.sh: Kanso build script"
""
"Usage: build.sh -h"
" build.sh [-v] -x"
" build.sh [-v] -g"
" build.sh [-v] -s"
""
" -h prints this help message with exit code 0"
" -v verbose mode"
" -x cleans the autogenerated code and the output object(s)"
" -s strips the output binaries after compilation"
" -g only generate wayland code and exit"
""
)
die "$EC" "${TXT[@]}"
}
# Autogenerate code
__autogen() {
if _cmd 'wayland-scanner' && [[ -f /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml ]]; then
verb_log "Generating \`src/xdg-shell-client-protocol.h\`..."
wayland-scanner client-header /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml \
./src/xdg-shell-client-protocol.h
verb_log "Done"
verb_log "Generating \`src/xdg-shell-protocol.c\`..."
wayland-scanner private-code /usr/share/wayland-protocols/stable/xdg-shell/xdg-shell.xml \
./src/xdg-shell-protocol.c
verb_log "Done"
else
die 127 "No xdg-shell code to autogenerate"
fi
return 0
}
__clean_repo() {
local EC=0
local RM_COMMAND=""
if [[ $VERBOSE -eq 1 ]]; then
RM_COMMAND="rm -rvf"
else
RM_COMMAND="rm -rf"
fi
verb_log "Cleaning up..."
eval "$RM_COMMAND src/xdg-shell-client-protocol.h src/xdg-shell-protocol.c bin obj"
EC=$?
verb_log "" "Done"
die "$EC"
}
# If not in root of repository
! [[ -d ./src ]] && die 127 \
"Not on root of repository." \
"You must execute this script in the root of the repo."
while getopts "$OPTIONS" OPTION; do
case "$OPTION" in
h) usage 0;;
x) CLEAN=1;;
s) STRIP=1 ;;
v) VERBOSE=1 ;;
g) GENERATE_ONLY=1 ;;
*) usage 1;;
esac
done
# If `-x` is passed
[[ $CLEAN -eq 1 ]] && __clean_repo
# If SDK env not initialized
if [[ -z ${VULKAN_SDK+X} ]]; then
if [[ -f ./setup-env.sh ]];then
. ./setup-env.sh || die 127 "Failed to source \`./setup-env.sh\`"
else
error "WARNING: Vulkan environment not initialized" "Falling back to \`/usr\`"
export VULKAN_SDK='/usr'
fi
fi
__autogen
# If `-g`, stop here
[[ $GENERATE_ONLY -eq 1 ]] && die 0
COMPILER_FLAGS=("-std=gnu17" "-g" "-Og" "-fPIC" "-mtune=generic")
[[ $VERBOSE -eq 1 ]] && COMPILER_FLAGS+=("-Wall" "-pedantic" "-Wno-unused")
# COMPILER_FLAGS+=("-Wextra") # Uncomment if extra warnings are desired
# COMPILER_FLAGS+=("-Werror") # Uncomment if errors should terminate compilation
INCLUDE_FLAGS=("-I$VULKAN_SDK/include" "-Isrc")
LINKER_FLAGS=("-L$VULKAN_SDK/lib" "-lvulkan" "-lwayland-client" "-lm" "-lc")
O_FILENAMES=()
mkdir -p "$OBJ_DIR"
# Compile each source file into object file
for F in src/*.c; do
FILE="$(basename "$F")"
OFILE="${OBJ_DIR}/${FILE%.c}.o"
O_FILENAMES+=("$OFILE")
verb_log "$F ==> $OFILE"
gcc -c src/"$FILE" -o "$OBJ_DIR/${FILE%.c}.o" \
"${COMPILER_FLAGS[@]}" \
"${INCLUDE_FLAGS[@]}" || die 1 "Failed to compile \`$F\` into object file"
done
mkdir -p "$OUT_DIR"
verb_log "Generating \`libkansoengine.so\`..."
gcc "${O_FILENAMES[@]}" \
"${INCLUDE_FLAGS[@]}" \
"${COMPILER_FLAGS[@]}" \
-o "$OUT_DIR"/libkansoengine.so \
"${LINKER_FLAGS[@]}" \
-shared || die 1 "Compilation failed"
verb_log "Done" ""
if [[ $STRIP -eq 1 ]]; then
verb_log "Stripping \`libkansoengine.so\`..."
strip "$OUT_DIR"/libkansoengine.so \
&& verb_log "Done" "" || die 1 "Stripping failed"
fi
die 0