-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcommon.sh
182 lines (164 loc) · 4.72 KB
/
common.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
BASE_DIR="$(dirname -- "$(readlink -f -- "$0")")"
cd "$BASE_DIR"
# Debug / Release
export BUILD_KIND="${BUILD_KIND:-release}"
# Setup emscripten (if not already)
export EMSDK="${EMSDK:-use_local_install}"
if [ "$EMSDK" == "use_local_install" ]; then
if [ ! -d emsdk ]; then
set +x
echo "-------------------------------------------------------"
echo "Emscripten is not installed. (EMSDK not set)"
echo "Press ENTER to install it into emsdk/. Ctrl-C to abort."
echo "-------------------------------------------------------"
read unused_var
if [ "$unused_var" != "" ]; then
echo "Aborting"
exit 1
fi
set -x
./install_emsdk.sh
fi
pushd emsdk
source ./emsdk_env.sh
popd
fi
case $BUILD_KIND in
debug)
export MINETEST_BUILD_TYPE="Debug"
export COMMON_CFLAGS="-O0 -g -gsource-map"
export COMMON_LDFLAGS="-sSAFE_HEAP=1 -sASSERTIONS=2 -sDEMANGLE_SUPPORT=1"
export BUILD_SUFFIX="-debug"
;;
profile)
export MINETEST_BUILD_TYPE="Release"
export COMMON_CFLAGS="--profiling -O2 -g -gsource-map"
export COMMON_LDFLAGS=""
export BUILD_SUFFIX="-profile"
;;
release)
export MINETEST_BUILD_TYPE="Release"
export COMMON_CFLAGS="-O2"
export COMMON_LDFLAGS=""
export BUILD_SUFFIX=""
;;
*)
echo "Unknown build kind: $BUILD_KIND"
exit 1
esac
SOURCES_DIR="$BASE_DIR/sources"
BUILD_DIR="$BASE_DIR/build$BUILD_SUFFIX"
INSTALL_DIR="$BUILD_DIR/install"
WWW_DIR="$BASE_DIR/www"
mkdir -p "$SOURCES_DIR" "$BUILD_DIR" "$INSTALL_DIR"
export MAKEFLAGS="-j$(nproc)"
export CFLAGS="$COMMON_CFLAGS -pthread -sUSE_PTHREADS=1 -fexceptions"
export CXXFLAGS="$COMMON_CFLAGS -pthread -sUSE_PTHREADS=1 -fexceptions"
export LDFLAGS="$COMMON_LDFLAGS -pthread -sUSE_PTHREADS=1 -fexceptions"
export EMSDK_ROOT="$EMSDK"
export EMSDK_SYSLIB="${EMSDK_ROOT}/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten"
export EMSDK_SYSINCLUDE="${EMSDK_ROOT}/upstream/emscripten/cache/sysroot/include"
export MINETEST_REPO="$SOURCES_DIR/minetest"
export IRRLICHT_REPO="$SOURCES_DIR/minetest/lib/irrlichtmt"
function getsource() {
local url="$1"
local hsh="$2"
local filename="$(basename -- "${url%%\?*}")"
pushd "$SOURCES_DIR"
if [ ! -f "$filename" ]; then
wget "$url" -O "$filename"
fi
if ! sha256sum "$filename" | grep -q "$hsh"; then
echo "Wrong sha256 checksum for $filename"
exit 1
fi
popd
}
function do_unpack() {
local filename="$1"
case "$filename" in
*.tar.gz|*.tar.bz2|*.tar.xz|*.tgz)
tar xvf "$filename"
;;
*.zip)
unzip "$filename"
;;
*)
echo "Not sure how to unpack: $filename"
exit 1
;;
esac
}
function strip_ext() {
local filename="$1"
case "$filename" in
*.tar.gz) echo "${filename%.tar.gz}" ;;
*.tar.xz) echo "${filename%.tar.xz}" ;;
*.tar.bz2) echo "${filename%.tar.bz2}" ;;
*.zip) echo "${filename%.zip}" ;;
*) echo "$filename" ;;
esac
}
# Usage:
#
# unpack_source $prefix
#
# Finds the tar/zip file in sources/ with a specific prefix,
# and untars/unzips it to the build directory, renamed as $prefix.
function unpack_source() {
local prefix="$1"
pushd "$SOURCES_DIR"
shopt -s nullglob
local matches=("$prefix"*)
shopt -u nullglob
local count="${#matches[@]}"
popd
if [ $count -eq 0 ] ; then
echo "unpack_source $prefix: Could not find source tar/zip file"
exit 1
elif [ $count -ne 1 ]; then
echo "unpack_source $prefix: Ambiguous prefix (count=$count)"
exit 1
fi
local filename="${matches[0]}"
local dirname="$(strip_ext "$filename")"
pushd "$BUILD_DIR"
rm -rf "$prefix" "$dirname"
do_unpack "$SOURCES_DIR/$filename"
if [ ! -d "$dirname" ]; then
echo "Unpacking $filename did not produce the expected directory"
exit 1
fi
if [ "$dirname" != "$prefix" ]; then
mv "$dirname" "$prefix"
fi
popd
}
function getrepo() {
local dirname="$1"
local url="$2"
local rev="$3"
pushd "$SOURCES_DIR"
if [ ! -d "$dirname" ]; then
git clone "$url" "$dirname"
pushd "$dirname"
git checkout "$rev"
popd
fi
popd
pushd "$SOURCES_DIR/$dirname"
local oldrev=`git rev-parse HEAD`
if [ "$oldrev" != "$rev" ]; then
set +x
echo "---------------------------------------------------------------"
echo "ERROR: sources/$dirname is on wrong revision"
echo "--------------------------------------------------------------"
echo "Detected revision: $oldrev"
echo "Expected revision: $rev"
echo "---------------------------------------------------------------"
echo "Please pull/checkout to the correct revision, or delete repo"
echo "before proceeding (it will be re-cloned)"
exit 1
fi
popd
}