This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
forked from vittorioromeo/SSVOpenHexagon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·321 lines (270 loc) · 7.63 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
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
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
set -o posix
unset DESTDIR #TODO: handle DESTDIR set by the caller
function usage {
cat <<EOF
Build and install script for Open Hexagon and dependencies.
Parameters:
<Installation prefix>
The first argument that is not a valid option will be treated
as the install prefix path. It may appear only once. If not
specified, "$prefixDirDefault" will be used.
The actual files will be installed into the $projectName
subdirectory. The contents of that subdirectory will be erased.
-c, --clean
Delete the build directories and make everything from scratch.
-d, --install-dependencies
Install the dependencies of this project ("make install").
If this option is not specified, the dependencies will be used
from their (temporary) build directories.
-g, --debug
Build with debug symbols and do not strip resulting binary.
-j <N>
Use <N> build jobs instead of the default of $makeJobs.
-r, --standalone
Standalone mode: do not bake RPATH into binary on install.
-s, --use-sudo
Call installation commands (such as "make install") with sudo.
Note that without password caching you might have to type your
password quite a few times!
-v, --verbose
Show commands before executing them ("set -o xtrace").
-y, --yes-to-all
Answer "yes" to all questions instead of prompting the user.
Note that if using "-s", sudo will still prompt for password.
--help
Display this help message and exit.
Arguments may be given in any order.
EOF
}
function onExit {
[ $? -eq 0 ] || echo -e "\nScript finished unsuccessfully." 1>&2
}
function die {
local msg="$1"
local code="${2:-1}"
echo "$msg" 1>&2
exit "$code"
}
function askContinue {
[ "$flagYesToAll" = false ] || return 0
read -p "Continue? [Y/n] "
! [[ "$REPLY" =~ ^[nN]$ ]] || die
}
function run {
set +e
RUN_OUTPUT="$(set -e ; "$@")"
RUN_STATUS=$?
set -e
}
function listSoDeps {
ldd "$1" | grep -v 'linux-vdso.so' |
sed -rn 's/^\t(.+ => )?(.+) \(0x.+\)$/\2/p'
#TODO: OSX support
}
function absPath {
local relPath="$1"
[ -e "$relPath" ] || return 1
if [ -d "$relPath" ]; then
cd "$relPath"
echo "$PWD"
cd "$OLDPWD"
else
local relDir="$(dirname -- "$relPath")"
local relFile="$(basename -- "$relPath")"
cd "$relDir"
echo "$PWD/$relFile"
cd "$OLDPWD"
fi
}
trap onExit EXIT
GLOBIGNORE="${GLOBIGNORE:-.:..}" #this also makes .* visible in *
case "$OSTYPE" in
*linux*)
alias xargs='xargs -r'
soDepsToolName=ldd
;;
*bsd*)
soDepsToolName=ldd
;;
*darwin*)
die "OSX is not supported yet." #TODO
soDepsToolName=otool
;;
esac
projectName="SSVOpenHexagon"
binName="SSVOpenHexagon"
bootstrapName="OpenHexagon"
flagClean=false
flagInstallDeps=false
flagStandalone=false
flagYesToAll=false
buildType="Release"
do=""
makeJobs=2
prefixDir=""
prefixDirDefault="/usr/local/games"
while [ $# -ne 0 ]; do
case "$1" in
-c|--clean) flagClean=true ;;
-d|--install-dependencies) flagInstallDeps=true ;;
-g|--debug) buildType="Debug" ;;
-r|--standalone) flagStandalone=true ;;
-s|--use-sudo) do=sudo ;;
-v|--verbose) set -o xtrace ;;
-y|--yes-to-all) flagYesToAll=true ;;
--help) usage; exit 0 ;;
-j*)
makeJobs="${1:2}"
if [ -z "$makeJobs" ]; then
makeJobs="$2"
shift
fi
[[ "$makeJobs" =~ ^[1-9][0-9]*$ ]] || { usage; die; }
;;
*)
[ -z "$prefixDir" ] || { usage; die; }
prefixDir="$1"
;;
esac
shift
done
prefixDir="${prefixDir:-"$prefixDirDefault"}"
run absPath "$prefixDir"
prefixDirAbs="$RUN_OUTPUT"
if [ $RUN_STATUS -ne 0 ]; then
if ! [ "$flagYesToAll" = true ]; then
read -p "$prefixDir does not exist. Create it? [Y/n] "
! [[ "$REPLY" =~ ^[nN]$ ]] || die "Cannot continue."
fi
mkdir -p "$prefixDir"
prefixDirAbs="$(absPath "$prefixDir")"
fi
[ -d "$prefixDirAbs" ] || die "$prefixDir is not a directory."
$do [ -w "$prefixDirAbs" ] || die "Write permission denied on $prefixDir."
repoReleaseSubdir="$(absPath ./_RELEASE)"
if [ "${prefixDirAbs##"$repoReleaseSubdir"}" != "$prefixDirAbs" ]; then
die "Do not install into the _RELEASE subdir of your repo."
fi
destinationDir="$prefixDirAbs/$projectName"
cmakeFlags=("-DCMAKE_BUILD_TYPE=$buildType"
"-DCMAKE_INSTALL_PREFIX=$destinationDir")
if [ "$flagStandalone" = true ]; then
cmakeFlags+=("-DCMAKE_SKIP_BUILD_RPATH=True")
fi
echo "Build and installation of $projectName will start with these options:"
echo "Build Type: $buildType"
echo "Build Jobs: $makeJobs"
echo "Destination Directory: $destinationDir"
echo "Initial CMake Flags: ${cmakeFlags[@]}"
if [ "$flagStandalone" = true ]; then
echo "Standalone mode active: RPATH will NOT be baked into binaries."
echo "Needed libraries will be copied next to the output binary."
echo "$soDepsToolName needs to be present."
else
echo "Standalone mode NOT active: RPATH will be baked into binaries."
fi
if [ "$flagInstallDeps" = true ]; then
echo "Dependencies (SSV libraries) will be installed."
else
echo "Dependencies (SSV libraries) will be compiled in a temp dir."
fi
echo
echo "A C++14 capable compiler is required."
echo "The contents of $destinationDir WILL BE DELETED (if any)."
echo
askContinue
if [ -e "$destinationDir" ]; then
find "$destinationDir" -mindepth 1 -print0 | xargs -0 $do rm -rf
else
$do mkdir -p "$destinationDir"
fi
# List of extlibs to build in order
dependencies=(
"vrm_pp"
"SSVUtils"
"SSVMenuSystem"
"SSVEntitySystem"
"SSVLuaWrapper"
"SSVStart"
)
dependenciesIncludeDirs=()
function buildLib {
local libName="$1"
echo "Building $libName..."
cd "$libName"
[ "$flagClean" = false ] || ! [ -e build ] || rm -rf build
[ -d build ] || mkdir build
cd build
cmake .. "${cmakeFlags[@]}"
make "-j$makeJobs"
if [ "$flagInstallDeps" == true ]; then
$do make install "-j$makeJobs"
else
cd ..
dependenciesIncludeDirs+=("$PWD")
echo "Added $PWD to dependencies include directories."
cd ./include
dependenciesIncludeDirs+=("$PWD")
echo "Added $PWD to dependencies include directories."
fi
cd ../.. # Back to extlibs
echo "Finished building $libName."
}
cd extlibs
for lib in "${dependencies[@]}"; do
buildLib "$lib"
done
cd ..
echo
echo "Building $projectName..."
[ "$flagClean" = false ] || ! [ -e build ] || rm -rf build
[ -d build ] || mkdir build
cd build
if [ "$flagInstallDeps" = false ]; then
echo "Setting additional CMake flags..."
flag="-DCMAKE_INCLUDE_PATH="
for dir in "${dependenciesIncludeDirs[@]}"; do
flag+="$dir:"
done
cmakeFlags+=("$flag")
echo "Final CMake Flags:"
echo "${cmakeFlags[@]}"
echo
askContinue
fi
cmake .. "${cmakeFlags[@]}"
make "-j$makeJobs"
$do make install "-j$makeJobs"
cd ..
echo "Finished building $projectName."
if [ "$buildType" = Release ]; then
find "$destinationDir" -name 'SSV*.so' -print0 | xargs -0 strip -s
find "$destinationDir" -name 'SSV*.so' -print0 | xargs -0 upx -9
strip -s "$destinationDir/$binName"
upx -9 "$destinationDir/$binName"
fi
if [ "$flagStandalone" = true ]; then
echo "Copying system libraries..."
libDestDir="$destinationDir/lib"
[ -e "$libDestDir" ] || $do mkdir "$libDestDir"
listSoDeps "$destinationDir/$binName" | while read libPath; do
libName="$(basename "$libPath")"
$do cp "$libPath" "$libDestDir/$libName"
done
cat >"$destinationDir/$bootstrapName" <<-EOF
#!/bin/bash
set -o errexit
$(declare -f absPath)
myPath="\$(absPath "\$(dirname -- "\${BASH_SOURCE[0]}")")"
export LD_LIBRARY_PATH="\$myPath/lib:\$LD_LIBRARY_PATH"
cd "\$myPath"
./SSVOpenHexagon
EOF
chmod +x "$destinationDir/$bootstrapName"
fi
$do cp -Rv "$repoReleaseSubdir/" "$destinationDir"
echo "Successfully finished building $projectName."