-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.sh
executable file
·270 lines (247 loc) · 7.78 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
#!/bin/bash
# Copyright (c) 2019-2019 The Authors.
# SPDX-License-Identifier: BSD-3-Clause
# saner programming env: these switches turn some bugs into errors
set -o errexit -o pipefail -o noclobber -o nounset
# Takes a path argument and returns it as an absolute path.
# No-op if the path is already absolute.
function toAbsolutePath {
local target="$1"
if [ "$target" == "." ]; then
pwd
elif [ "$target" == ".." ]; then
dirname "$(pwd)"
else
echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")"
fi
}
# -allow a command to fail with !’s side effect on errexit
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
cat << EOF;
I’m sorry, 'getopt --test' failed in this environment.
Enhanced getopt is available on most "bash-systems", including Cygwin;
on OS X try brew install gnu-getopt or sudo port install getopt
EOF
exit 1
fi
OPTIONS=d:vh
LONGOPTS=dist:,verbose,help
function usage() {
cat << EOF;
Usage:
build.sh [options] [target]
Build the module targets. By default the script will build all targets,
including: magic, libmagic, file and the binding.
Note that libmagic builds the magic.mgx file during native build, which is
required for the library to properly work. Therefore we have to do a 2-pass
build for the file sub-module, the first one to generate the magic file and
the second one to generate the emscripten artifacts. Once the magic file is
built, it is automatically saved to the dist directory.
Target:
magic native build of 'file' submodule to create the magic file
emlib emscripten build of libmagic static lib for use by binding
binding emscripten/embind build of the binding
all (default) clean build all targets
Options:
-d, --dist set the destination of build artifacts relative to the script
directory (default is dist)
-v, --verbose show all output from build commands (default is no output)
-h, --help display this help
EOF
}
# -regarding ! and PIPESTATUS see above
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
DIST_DIR="dist" VERBOSE=no TARGET=all
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--verbose)
VERBOSE=yes
shift
;;
-d|--dist)
DIST_DIR="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
# handle non-option arguments
if [ $# -ne 0 ]; then
if [ $# -ne 1 ]; then
echo "$0: A single target name can be specified."
exit 4
fi
TARGET=$1
if [ "$TARGET" != magic ] && [ "$TARGET" != emlib ] && [ "$TARGET" != binding ] && [ "$TARGET" != all ]; then
echo "$0: invalid target '$TARGET'"
usage
exit 4
fi
fi
ROOT_DIR=$(toAbsolutePath "$(dirname $0)")
[ $VERBOSE == "yes" ] && echo "-- Root directory is: $ROOT_DIR"
LIBMAGIC_SRC_DIR=$ROOT_DIR/node_modules/c-libmagic
[ $VERBOSE == "yes" ] && echo "-- Using libmagic in: $LIBMAGIC_SRC_DIR"
LIBMAGIC_BUILD_DIR=$ROOT_DIR/build/libmagic
! mkdir -p "$LIBMAGIC_BUILD_DIR"
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to setup the build dir for libmagic at $LIBMAGIC_BUILD_DIR"
exit 3
fi
[ $VERBOSE == "yes" ] && echo "-- Using libmagic in: $LIBMAGIC_SRC_DIR"
DIST_DIR=$ROOT_DIR/$DIST_DIR
[ $VERBOSE == "yes" ] && echo "-- Final build artifacts will be placed in: $DIST_DIR"
! mkdir -p "$DIST_DIR"
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to setup the dist dir at $DIST_DIR"
exit 3
fi
#
# Native magic file
#
if [ "$TARGET" == "all" ] || [ "$TARGET" == "magic" ]; then
echo "-- [native-magic] configure stage"
cd "$LIBMAGIC_BUILD_DIR"
if [ $VERBOSE == yes ]; then
! autoreconf -f -i "$LIBMAGIC_SRC_DIR"
else
! autoreconf -f -i "$LIBMAGIC_SRC_DIR" > /dev/null
fi
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to autoreconf"
exit 4
fi
if [ $VERBOSE == yes ]; then
! "$LIBMAGIC_SRC_DIR"/configure --prefix="$DIST_DIR"/file --enable-static=yes --enable-shared=no
else
! "$LIBMAGIC_SRC_DIR"/configure --prefix="$DIST_DIR"/file --enable-static=yes --enable-shared=no > /dev/null
fi
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to run configure"
exit 4
fi
echo "-- [native-magic] build stage"
! make
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to run make"
exit 4
fi
echo "-- [native-magic] copy magic.mgc to dist dir"
! cp magic/magic.mgc "$DIST_DIR"
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to copy magic file to dist dir"
exit 4
fi
cd "$ROOT_DIR"
fi
#
# Emscript build of libmagic
#
if [ "$TARGET" == "all" ] || [ "$TARGET" == "emlib" ]; then
cd "$LIBMAGIC_BUILD_DIR"
if [ -f Makefile ]; then
echo "-- [emscript-libmagic] clean any remaining things from previous builds"
if [ $VERBOSE == yes ]; then
! make clean
else
! make clean > /dev/null
fi
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to run make clean"
exit 4
fi
else
if [ $VERBOSE == yes ]; then
! autoreconf -f -i "$LIBMAGIC_BUILD_DIR"
else
! autoreconf -f -i "$LIBMAGIC_BUILD_DIR" > /dev/null
fi
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to autoreconf"
exit 4
fi
fi
echo "-- [emscript-libmagic] configure stage"
if [ $VERBOSE == yes ]; then
! emconfigure "$LIBMAGIC_SRC_DIR"/configure --enable-static=yes --enable-shared=no
else
! emconfigure "$LIBMAGIC_SRC_DIR"/configure --enable-static=yes --enable-shared=no > /dev/null
fi
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to run configure"
exit 4
fi
echo "-- [emscript-libmagic] build stage"
cd src
! emmake make
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to run make"
exit 4
fi
cd "$ROOT_DIR"
fi
#
# Emscripten binding
#
if [ "$TARGET" == "all" ] || [ "$TARGET" == "binding" ]; then
echo "-- [binding] generate js and wasm"
cd lib/binding
SRCS="magic-js.cpp"
# Compile the code
# We enable FS, using node rawfs and we force the environment to always
# be node, thus disabling the auto-detection at runtime
EMCC_OPTIONS="-O3 \
-fno-exceptions \
--bind \
-s ASSERTIONS=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s MALLOC=emmalloc \
-s MODULARIZE=1 \
-s ENVIRONMENT=node \
-s FILESYSTEM=1 \
-s NODERAWFS=1 \
-o ../magic-js.js \
-I $LIBMAGIC_BUILD_DIR/src \
-L $LIBMAGIC_BUILD_DIR/src/.libs \
-lmagic \
$SRCS"
echo "em++ $EMCC_OPTIONS"
# shellcheck disable=SC2086
! em++ $EMCC_OPTIONS
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to compile binding"
exit 4
fi
# Copy the generated files to dist
echo "-- [binding] copy js and wasm to dist"
! cp ../magic-js.js ../magic-js.wasm "$DIST_DIR"
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
echo "$0: failed to copy generated magic-js files to dist dir"
exit 4
fi
cd "$ROOT_DIR"
fi