-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild
executable file
·305 lines (274 loc) · 6.47 KB
/
build
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
#!/bin/bash
if [ "$CC" == "" ]; then
CC="gcc"
fi
if ! command -v $CC 2>&1 > /dev/null; then
echo "Unable to find the specified compiler ($CC)"
exit 1
fi
if [ "$LNK" = "" ]; then
LNK="$CC"
else
if ! command -v $LNK 2>&1 > /dev/null; then
echo "Unable to find the specified linker ($LNK)"
exit 1
fi
fi
if [ "$STRIP" == "" ]; then
STRIP="strip"
fi
NPROC="nproc"
SHLIB_EXT="so"
case "$(uname -s)" in
Linux)
HOST_OS="linux"
;;
Darwin)
HOST_OS="darwin"
NPROC="sysctl -n hw.physicalcpu"
;;
CYGWIN*|MINGW*|MSYS*|MINGW32*)
HOST_OS="win"
;;
*)
HOST_OS="unknown"
;;
esac
if [[ "$HOST_OS" != "win" && "$CC" == *"mingw"* ]]; then
TARGET_OS="win"
else
TARGET_OS="$HOST_OS"
fi
MAXWORKERS=$($NPROC)
COMPILE=1
GITOK=0
GITUPD=0
RUN=0
SAN=0
DEBUG=0
OPT="-O2"
WARN=""
ROOT=$(cd "$(dirname "$0")"; pwd)
if [ "$CSERVER_BUILD_TARGET" == "" ]; then
MACH=$($CC -dumpmachine)
if [ $MACH = "" ]; then
MACH="unknown"
fi
else
MACH="$CSERVER_BUILD_TARGET"
CC="$CSERVER_BUILD_TARGET-$CC"
LNK="$CSERVER_BUILD_TARGET-$LNK"
STRIP="$CSERVER_BUILD_TARGET-$STRIP"
fi
if [ "$TARGET_OS" != "win" ]; then
LNK="$LNK -rdynamic"
if [ "$TARGET_OS" != "darwin" ]; then
CFLAGS="$CFLAGS -fno-semantic-interposition"
else
LNK="$LNK -undefined dynamic_lookup"
SHLIB_EXT="dylib"
fi
else
SHLIB_EXT="dll"
fi
CFLAGS="-pipe -fpic"
LIBS=""
OUTBIN="server"
if git --version >/dev/null 2>&1; then
GITOK=1
fi
PLUGIN_ARGS=""
PLUGIN_BUILD=0
PLUGIN_INSTALL=0
SERVER_OUTROOT="$ROOT/out/$MACH"
notaplugin () {
echo "Looks like the specified directory is not a plugin."
exit 1
}
waitfor () {
if ! wait $1; then
echo -ne "error\nCompiler errors:\n$(cat $STDERR)\n"
exit 1
fi
}
for a in "$@"
do
if [ $PLUGIN_BUILD -eq 1 ]; then
if [ -n "$PLUGIN_NAME" ]; then
if [ "$a" == "install" ]; then
PLUGIN_INSTALL=1
else
PLUGIN_ARGS="$PLUGIN_ARGS$a "
fi
continue
else
if [ ! -d "../cs-$a" ]; then notaplugin; fi
if [ ! -d "../cs-$a/src" ]; then notaplugin; fi
PLUGIN_NAME="$a"
CFLAGS="$CFLAGS -DCORE_BUILD_PLUGIN -Isrc"
LNK="$LNK -shared"
OUTBIN="$a.$SHLIB_EXT"
ROOT="../cs-$a"
fi
fi
if [ "$a" == "cls" ]; then clear; fi
if [ "$a" == "pb" ]; then PLUGIN_BUILD=1; fi
if [ "$a" == "wall" ]; then WARN="-Wall -Wextra"; fi
if [ "$a" == "wx" ]; then CFLAGS="$CFLAGS -Werror"; fi
if [ "$a" == "w0" ]; then WARN="-W0"; fi
if [ "$a" == "dbg" ]; then DEBUG=1; fi
if [ "$a" == "od" ]; then OPT="-O0"; fi
if [ "$a" == "san" ]; then SAN=1; fi
if [ "$a" == "run" ]; then RUN=1; fi
if [ "$a" == "upd" ] && [ $GITOK -eq 1 ]; then GITUPD=1; fi
done
OUTDIR="$ROOT/out/$MACH"
if [ $DEBUG -eq 1 ]; then
OUTDIR="$OUTDIR-dbg"
SERVER_OUTROOT="$SERVER_OUTROOT-dbg"
fi
PLUGINS_DIR="$SERVER_OUTROOT/plugins"
if [ $GITUPD -eq 1 ]; then
if [ $PLUGIN_BUILD -eq 1 ]; then
pushd "../cs-$PLUGIN_NAME"
if ! git fetch origin; then
echo "!!! Failed to fetch $PLUGIN_NAME repo"
exit 1
fi
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ -f "$PLUGINS_DIR/$OUTBIN" ] && [ "$CSBUILD_NOUPDATE" != "ignore" ]; then
if [ $(git rev-list HEAD..origin/$BRANCH --count) -eq 0 ]; then
echo "Nothing updated"
COMPILE=0
fi
fi
if ! git merge --ff-only; then
echo "!!! Failed to update $PLUGIN_NAME"
popd
exit 1
fi
popd
else
if ! git fetch origin; then
echo "!!! Failed to fetch main repo"
exit 1
fi
BUILDALERTED=0
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ -f "$SERVER_OUTROOT/$OUTBIN" ] && [ "$CSBUILD_NOUPDATE" != "ignore" ]; then
if [ $(git rev-list HEAD..origin/$BRANCH --count) -eq 0 ]; then
echo "Nothing updated"
COMPILE=0
fi
for file in $(git diff --stat --name-only HEAD origin/$BRANCH); do
if [ "$file" = "build" ]; then
BUILDALERTED=1
break
fi
done
fi
if ! git merge --ff-only; then
echo "!!! Failed to update server core"
exit 1
elif [ $BUILDALERTED -eq 1 ]; then
echo "Bash script has been updated, restarting..."
CSBUILD_NOUPDATE=ignore exec $0 $@
fi
fi
fi
if [ $COMPILE -eq 1 ]; then
if [ -f "$ROOT/vars.sh" ]; then
if ! . "$ROOT/vars.sh"; then
echo "Failed to add plugin variables"
exit 1
fi
fi
if [ $DEBUG -eq 1 ]; then
CFLAGS="$CFLAGS -g -DCORE_BUILD_DEBUG"
CFLAGS="$CFLAGS -fno-omit-frame-pointer"
OPT="-O0"
fi
if [ $GITOK -eq 1 ]; then
VER="$(git -C "$ROOT" describe --tags HEAD 2> /dev/null)"
if [ "$VER" == "" ]; then
VER="$(git -C "$ROOT" rev-parse --short HEAD 2> /dev/null)"
fi
CFLAGS="$CFLAGS -DGIT_COMMIT_TAG=\"$VER\""
fi
STDERR="$ROOT/out/stderr"
OBJDIR="$ROOT/out/.objs/$MACH"
if [ $SAN -eq 1 ]; then
LIBS="-lasan -lubsan $LIBS"
CFLAGS="-fsanitize=undefined,address -fstack-protector-all -fno-sanitize=alignment -fno-sanitize-recover=all $CFLAGS"
fi
if [ ! -d "$OUTDIR" ]; then mkdir -p $OUTDIR; fi
if [ ! -d "$OBJDIR" ]; then mkdir -p $OBJDIR; fi
echo -n "Compiling..."
pids=()
OBJS=""
> $STDERR
for cpath in $(find "$ROOT/src/" -maxdepth 1 -type f -name "*.c"); do
objpath="$OBJDIR/$(basename $cpath .c).o"
OBJS="$OBJS $objpath"
$CC -fdiagnostics-color=always -c $CFLAGS $WARN $OPT -I$ROOT/src $cpath -o$objpath 2>> $STDERR &
pids+=($!)
while test $(jobs -p | wc -w) -ge "$MAXWORKERS"; do
wait ${pids[0]}
pids=("${pids[@]:1}")
done
done
for pid in ${pids[@]}; do
waitfor $pid
done
echo -ne "done\nLinking..."
if [ $PLUGIN_BUILD -eq 0 ]; then
if [ "$TARGET_OS" == "win" ]; then
LIBS="$LIBS -lkernel32 -ldbghelp -lws2_32"
LIBS="$LIBS -Wl,--out-implib,$OUTDIR/lib$OUTBIN.a"
else
LIBS="$LIBS -lpthread -ldl"
fi
else
if [ "$TARGET_OS" == "win" ]; then
LIBS="$LIBS -L$SERVER_OUTROOT -lserver"
fi
fi
$LNK$OBJS -o$OUTDIR/$OUTBIN $LIBS 2>> $STDERR &
waitfor $!
if [ $DEBUG -eq 0 ]; then
echo -ne "done\nStripping..."
$STRIP $OUTDIR/$OUTBIN
fi
echo -e "done\nCompiled binary: $OUTDIR/$OUTBIN"
fi
if [ $PLUGIN_INSTALL -eq 1 ]; then
echo -ne "Copying to the plugins directory..."
if [ ! -d "$PLUGINS_DIR" ]; then mkdir -p $PLUGINS_DIR; fi
cp $OUTDIR/$OUTBIN $PLUGINS_DIR/$OUTBIN
echo "done"
fi
if [ $COMPILE -eq 1 ]; then
stderrlen=$(wc -c < "$STDERR")
if [ $stderrlen -gt 0 ]; then
echo -e "Compiler stderr:\n$(cat $STDERR)"
if grep $STDERR -qe 'error:'; then
exit 1
fi
fi
fi
if [ $RUN -eq 1 ]; then
echo -ne "Running..."
if [[ "$TARGET_OS" != "$HOST_OS" ]]; then
echo "error, $HOST_OS does not support $TARGET_OS binaries"
exit 1
fi
pushd $SERVER_OUTROOT
if [ $SAN -eq 1 ]; then
set -- /usr/lib/$MACH/libasan.$SHLIB_EXT* /lib/$MACH/libasan.$SHLIB_EXT*
LD_PRELOAD=$1 ./server
else
./server
fi
popd
fi
exit 0