forked from widelands/widelands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.sh
executable file
·688 lines (639 loc) · 20.9 KB
/
compile.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#!/bin/sh
print_help () {
# Print help for our options
echo "###########################################################"
echo " "
echo " Because of the many different systems Widelands"
echo " might be compiled on, we unfortunally can not"
echo " provide a simple way to prepare your system for"
echo " compilation. To ensure your system is ready, best"
echo " check https://www.widelands.org/wiki/BuildingWidelands"
echo " "
echo " You will often find helpful hands at our"
echo " * IRC Chat: https://www.widelands.org/webchat/"
echo " * Forums: https://www.widelands.org/forum/"
echo " * Mailinglist: https://www.widelands.org/wiki/MailLists/"
echo " "
echo " Please post your bug reports and feature requests at:"
echo " https://github.com/widelands/widelands/issues"
echo " "
echo "###########################################################"
echo " "
echo "Per default, this script will create a full debug build."
echo "Unless explicitly switched off, AddressSanitizer will"
echo "be used as well with debug builds."
echo " "
echo "You can override the defaults locally by creating a file"
echo "called '.compile_defaults' listing your desired command"
echo "line options in a single line. Only the first line of the"
echo "file is read, further lines can be used as comments."
echo "The 'COMPILE_DEFAULTS' environment variable can be set to"
echo "the name of a different file to use, or if set to a"
echo "non-existent name, to use the built-in defaults."
echo " "
echo "The following options are available:"
echo " "
echo "-h or --help Print this help."
echo "-q or --quiet Suppress most of compile.sh's output."
echo "-v or --verbose Make compile.sh's output verbose."
echo "-n or --dry-run Only print commands, do NOT run cmake nor compile"
echo " anything."
echo " "
echo "Omission options and their overrides:"
echo " "
echo "-w or --no-website Omit building of website binaries."
echo "+w or --with-website Enable building of website binaries."
echo " "
echo "-s or --skip-tests Skip linking and executing the tests."
echo "+s or --do-tests Link and execute the tests."
echo " "
echo "-a or --no-asan If in debug mode, switch off the AddressSanitizer."
echo " Release builds are created without AddressSanitizer"
echo " by default."
echo "+a or --with-asan If in release mode, switch on the AddressSanitizer."
echo " Debug builds are created with AddressSanitizer by"
echo " default."
echo " "
echo "-m or --no-tsan Switch off the ThreadSanitizer (default)."
echo "+m or --with-tsan Switch on the ThreadSanitizer."
echo " Can only be used with --no-asan, because AddressSanitizer"
echo " cannot be enabled at the same time."
echo " "
echo "-x or --without-xdg Disable support for the XDG Base Directory Specification."
echo "+x or --with-xdg Enable support for the XDG Base Directory Specification."
echo " "
echo "Compiler options:"
echo " "
echo "-j <number> or --cores <number>"
echo " Set the number of processor cores to use for"
echo " compiling and linking. Default is to leave 1 core"
echo " free."
echo " "
echo "-r or --release Create a release build. If this is not set,"
echo " a debug build will be created."
echo "-d or --debug Create a debug build. This is the default,"
echo " unless overridden locally."
echo "-e or --rel-with-dbg Create a release build with debugging symbols."
echo " "
if which g++ >/dev/null; then # gcc specific
echo "-c or --no-cross-opt Do not use cross compile unit optimization,"
echo " even if available."
echo "+c or --with-cross-opt"
echo " Use cross compile unit optimization if available."
echo " "
fi
echo "--gcc Try to build with GCC rather than the system default."
echo " If you built with Clang before, you will have to clean"
echo " your build directory before switching compilers."
echo " Expects that the compiler is in '/usr/bin/'."
echo " "
echo "--clang Try to build with Clang rather than the system default."
echo " If you built with GCC before, you will have to clean"
echo " your build directory before switching compilers."
echo " Expects that the compiler is in '/usr/bin/'."
echo " "
echo "--default-compiler Use the system default compiler."
echo " If you built with a different compiler before, you will"
echo " have to clean your build directory before switching"
echo " compilers."
echo " "
echo "For the AddressSanitizer output to be useful, some systems (e.g. Ubuntu Linux)"
echo "require that you set a symlink to the symbolizer. For example:"
echo " "
echo " sudo ln -s /usr/bin/llvm-symbolizer-3.8 /usr/bin/llvm-symbolizer"
echo " "
echo "More info about AddressSanitizer at:"
echo " "
echo " https://clang.llvm.org/docs/AddressSanitizer.html"
echo " "
echo " "
echo "CMake options:"
echo " "
echo "-D... Any option starting with -D will be passed to cmake"
echo " unchanged before the above options."
echo " "
return
}
## Options to control the build.
BUILD_WEBSITE="ON"
BUILD_TESTS="ON"
BUILD_TYPE="Debug"
USE_FLTO="yes"
USE_ASAN="default"
USE_ASAN_DEFAULT="ON"
USE_TSAN="OFF"
COMPILER="default"
USE_XDG="ON"
EXTRA_OPTS=""
# Option for this script itself
QUIET=0
RUN=""
if [ -z "$COMPILE_DEFAULTS" ]; then
COMPILE_DEFAULTS=.compile_defaults
fi
# try to set default number of cores automatically
# nproc is a GNU tool, may not be available everywhere, but it is preferable,
# when it is available
if hash nproc 2>/dev/null ; then
MAXCORES="$(nproc)"
else
# try BSD / OS X
if hash sysctl 2>/dev/null ; then
MAXCORES="$(sysctl -n hw.ncpu)" 2>/dev/null
# we will check result later
fi
fi
# this has the marginally useful side-effect that MAXCORES can be set in the
# environment when neither method works...
if [ $MAXCORES -gt 0 ] 2>/dev/null ; then
if [ $MAXCORES -gt 1 ]; then
CORES=$((MAXCORES - 1))
else
CORES=1
fi
else
echo "Cannot determine number of available CPU cores. Only one core will be"
echo "used by default. User provided -j will be trusted."
echo " "
MAXCORES=0
CORES=1
fi
OLD_CLI_ARGS="$@"
if [ -f "$COMPILE_DEFAULTS" -a -r "$COMPILE_DEFAULTS" ]; then
read LOCAL_DEFAULTS <"$COMPILE_DEFAULTS"
# We want $LOCAL_DEFAULTS to be split, so no "" for it
set -- $LOCAL_DEFAULTS "$@"
fi
while [ $# -gt 0 ]
do
case $1 in
-a|--no-asan)
USE_ASAN="OFF"
shift
;;
+a|--with-asan)
USE_ASAN="ON"
shift
;;
-m|--no-tsan)
USE_TSAN="OFF"
shift
;;
+m|--with-tsan)
USE_TSAN="ON"
shift
;;
-h|--help)
print_help
exit 0
shift
;;
-j|--cores)
case $2 in
*[!0-9]*|'')
echo "Call -j/--cores with a number, e.g. '-j $MAXCORES'"
exit 1
;;
esac
if [ "$2" -lt 1 ]; then
echo "Invalid number of cores was specified: $2"
exit 1
fi
if [ "$MAXCORES" -ge "$2" -o "$MAXCORES" -eq 0 ]; then
CORES="$2"
else
echo "Cannot set number of cores to $2, because the maximum number"
echo "of supported cores is $MAXCORES."
exit 1
fi
shift 2 # past argument and value
;;
-r|--release)
BUILD_TYPE="Release"
if [ "${USE_ASAN}" = "default" ]; then
USE_ASAN="OFF"
fi
shift
;;
-d|--debug)
BUILD_TYPE="Debug"
if [ "${USE_ASAN}" = "default" ] && [ "${USE_TSAN}" = "OFF" ]; then
USE_ASAN="ON"
fi
shift
;;
-e|--rel-with-dbg)
BUILD_TYPE="RelWithDebInfo"
if [ "${USE_ASAN}" = "default" ]; then
USE_ASAN="OFF"
fi
shift
;;
-c|--no-cross-opt)
USE_FLTO="no"
shift
;;
+c|--with-cross-opt)
USE_FLTO="yes"
shift
;;
-s|--skip-tests)
BUILD_TESTS="OFF"
shift
;;
+s|--do-tests)
BUILD_TESTS="ON"
shift
;;
-w|--no-website)
BUILD_WEBSITE="OFF"
shift
;;
+w|--with-website)
BUILD_WEBSITE="ON"
shift
;;
-q|--quiet)
QUIET=1
shift
;;
-v|--verbose)
QUIET=0
shift
;;
-n|--dry-run)
RUN=echo
shift
;;
--gcc)
if [ -f /usr/bin/gcc -a -f /usr/bin/g++ ]; then
COMPILER=gcc
export CC=/usr/bin/gcc
export CXX=/usr/bin/g++
fi
shift
;;
--clang)
if [ -f /usr/bin/clang -a -f /usr/bin/clang++ ]; then
COMPILER=clang
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
fi
shift
;;
--default-compiler)
COMPILER=default
unset CC
unset CXX
shift
;;
-x|--without-xdg)
USE_XDG="OFF"
shift
;;
+x|--with-xdg)
USE_XDG="ON"
shift
;;
-D*)
EXTRA_OPTS="$EXTRA_OPTS $1"
shift
;;
*)
# unknown option
echo "Unknown option:" $1
echo "Use -h or --help for list of available options"
exit 1
;;
esac
done
if [ "${USE_ASAN}" = "default" ]; then
if [ "${USE_TSAN}" = "ON" ]; then
USE_ASAN="OFF"
else
USE_ASAN="${USE_ASAN_DEFAULT}"
fi
fi
if [ "${USE_ASAN}" = "ON" ] && [ "${USE_TSAN}" = "ON" ]; then
echo " "
echo "Cannot compile with both Address and Thread Sanitizer enabled!"
exit 1
fi
if [ $QUIET -eq 0 ]; then
echo " "
echo "###########################################################"
echo "# Script to simplify the compilation of Widelands #"
echo "###########################################################"
echo " "
if [ -n "$LOCAL_DEFAULTS" ]; then
echo "Using default compile options from '$COMPILE_DEFAULTS':"
echo " $LOCAL_DEFAULTS"
echo " "
echo "Command line options:"
echo " $OLD_CLI_ARGS"
echo " "
fi
fi
if [ -n "$RUN" ]; then
if [ $QUIET -eq 0 ]; then
echo " "
echo "#################################################################"
echo "# Dry run: Nothing will be changed, commands will be printed. #"
echo "#################################################################"
echo " "
else
echo "### Dry run: Nothing will be changed, commands will be printed."
fi
fi
## Get command and options to use in update.sh
COMMANDLINE="$0"
CMD_ADD () {
COMMANDLINE="$COMMANDLINE $@"
}
if [ $QUIET -ne 0 ]; then
CMD_ADD "$LOCAL_DEFAULTS $OLD_CLI_ARGS"
else # Start of verbose output section
if [ -n "$EXTRA_OPTS" ]; then
echo "Extra CMake options used: $EXTRA_OPTS"
echo " "
CMD_ADD "$EXTRA_OPTS"
fi
echo "Using ${CORES} core(s)."
echo ""
CMD_ADD "--cores ${CORES}"
echo -n "Using compiler: "
if [ $COMPILER = default ]; then
echo "system default"
CMD_ADD "--default-compiler"
else
echo "$COMPILER"
CMD_ADD "--$COMPILER"
fi
echo " "
if [ $USE_FLTO = "yes" ]; then
echo "Using cross compile unit optimization if available."
CMD_ADD "--with-cross-opt"
else
echo "Not using cross compile unit optimization."
CMD_ADD "--no-cross-opt"
fi
echo " "
if [ $BUILD_WEBSITE = "ON" ]; then
echo "A complete build will be created."
echo "You can use -w or --no-website to omit building and"
echo "linking website-related executables."
CMD_ADD "--with-website"
else
echo "Any website-related code will be OMITTED in the build."
echo "Make sure that you have created and tested a full"
echo "build before submitting code to the repository!"
echo "You can use +w or --with-website to build and link"
echo "website-related executables."
CMD_ADD "--no-website"
fi
echo " "
if [ $BUILD_TESTS = "ON" ]; then
echo "Tests will be built."
echo "You can use -s or --skip-tests to omit building them."
CMD_ADD "--do-tests"
else
echo "Tests will not be built."
echo "You can use +s or --do-tests to build them."
CMD_ADD "--skip-tests"
fi
echo " "
echo "###########################################################"
echo " "
if [ $BUILD_TYPE = "Release" ]; then
echo "Creating a Release build. Use -d to create a Debug build."
CMD_ADD "--release"
elif [ $BUILD_TYPE = "Debug" ]; then
echo "Creating a Debug build. Use -r to create a Release build."
CMD_ADD "--debug"
elif [ $BUILD_TYPE = "RelWithDebInfo" ]; then
echo "Creating a RelWithDebInfo build."
CMD_ADD "--rel-with-dbg"
fi
echo " "
if [ $USE_ASAN = "ON" ]; then
echo "Will build with AddressSanitizer."
echo "https://clang.llvm.org/docs/AddressSanitizer.html"
echo "You can use -a or --no-asan to switch it off."
CMD_ADD "--with-asan"
else
echo "Will build without AddressSanitizer."
echo "You can use +a or --with-asan to switch it on."
CMD_ADD "--no-asan"
fi
if [ $USE_TSAN = "ON" ]; then
echo "Will build with ThreadSanitizer."
echo "https://clang.llvm.org/docs/ThreadSanitizer.html"
echo "You can use -m or --no-tsan to switch it off."
CMD_ADD "--with-tsan"
else
echo "Will build without ThreadSanitizer."
echo "You can use +m or --with-tsan to switch it on."
CMD_ADD "--no-tsan"
fi
if [ $USE_XDG = "ON" ]; then
echo " "
echo "Basic XDG Base Directory Specification will be used on Linux"
echo "if no existing \$HOME/.widelands folder is found."
echo "The widelands user data can be found in \$XDG_DATA_HOME/widelands"
echo "and defaults to \$HOME/.local/share/widelands."
echo "The widelands user configuration can be found in \$XDG_CONFIG_HOME/widelands"
echo "and defaults to \$HOME/.config/widelands."
echo "See https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html"
echo "for more information."
echo " "
CMD_ADD "--with-xdg"
else
CMD_ADD "--without-xdg"
fi
echo " "
echo "###########################################################"
echo " "
echo "Call 'compile.sh -h' or 'compile.sh --help' for help."
echo ""
echo "For instructions on how to adjust options and build with"
echo "CMake, please take a look at"
echo "https://www.widelands.org/wiki/BuildingWidelands/."
echo " "
echo "###########################################################"
echo " "
fi # End of verbose output section
######################################
# Definition of some local variables #
######################################
buildtool="" #Use ninja by default, fall back to make if that is not available.
######################################
######################################
# Definition of some functions #
######################################
# Check basic stuff
basic_check () {
# Check whether the script is run in a widelands main directory
if ! [ -f src/wlapplication.cc ] ; then
echo " This script must be run from the main directory of the widelands"
echo " source code."
exit 1
fi
return 0
}
set_buildtool () {
GENERATOR=""
#Defaults to ninja, but if that is not found, we use make instead
if [ `command -v ninja` ] ; then
buildtool="ninja"
GENERATOR="-G Ninja"
#On some systems (most notably Fedora), the binary is called ninja-build
elif [ `command -v ninja-build` ] ; then
buildtool="ninja-build"
GENERATOR="-G Ninja"
#... and some systems refer to GNU make as gmake
elif [ `command -v gmake` ] ; then
buildtool="gmake"
else
buildtool="make"
fi
}
# Compile Widelands
compile_widelands () {
$RUN cmake $GENERATOR .. $EXTRA_OPTS \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DOPTION_BUILD_WEBSITE_TOOLS=$BUILD_WEBSITE \
-DOPTION_BUILD_TESTS=$BUILD_TESTS \
-DOPTION_ASAN=$USE_ASAN \
-DOPTION_TSAN=$USE_TSAN \
-DUSE_XDG=$USE_XDG \
-DUSE_FLTO_IF_AVAILABLE=${USE_FLTO}
$RUN $buildtool -j $CORES
return 0
}
# Remove old and move newly compiled files
move_built_files () {
$RUN rm -f ../VERSION || true
$RUN rm -f ../widelands || true
$RUN rm -f ../wl_map_object_info || true
$RUN rm -f ../wl_map_info || true
$RUN cp VERSION ../VERSION
$RUN mv src/widelands ../widelands
if [ $BUILD_WEBSITE = "ON" ]; then
$RUN mv ../build/src/website/wl_create_spritesheet ../wl_create_spritesheet
$RUN mv ../build/src/website/wl_map_object_info ../wl_map_object_info
$RUN mv ../build/src/website/wl_map_info ../wl_map_info
fi
return 0
}
create_update_script () {
# First check if this is an git checkout at all - only in that case,
# creation of a script makes any sense.
if [ -n "$(git status -s)" ]; then
echo "You don't appear to be using Git, or your working tree is not clean."
echo "An update script will not be created."
if [ $QUIET -eq 0 ]; then
git status
fi
return 0
fi
$RUN rm -f update.sh || true
if [ -z "$RUN" ]; then
cat > update.sh << END_SCRIPT
#!/bin/sh
echo "################################################"
echo "# Widelands update script. #"
echo "################################################"
echo " "
set -e
if ! [ -f src/wlapplication.cc ] ; then
echo " This script must be run from the main directory of the widelands"
echo " source code."
exit 1
fi
# Checkout current branch and pull latest master
git checkout
git pull https://github.com/widelands/widelands.git master
$COMMANDLINE
echo " "
echo "################################################"
echo "# Widelands was updated successfully. #"
echo "# You should be able to run it via ./widelands #"
echo "################################################"
END_SCRIPT
else # dry run
echo 'cat > update.sh <<END_SCRIPT'
echo " # ..."
echo " $COMMANDLINE"
echo " # ..."
echo "END_SCRIPT"
fi
$RUN chmod +x ./update.sh
if [ $QUIET -eq 0 -a -z "$RUN" ]; then
echo "The update script has successfully been created."
fi
}
######################################
######################################
# Here is the "main" function #
######################################
set -e
basic_check
set_buildtool
# Dependency check doesn't work with ninja, so we do it manually here
if [ $BUILD_TYPE = "Debug" -a \( $buildtool = "ninja" -o $buildtool = "ninja-build" \) ]; then
$RUN utils/build_deps.py
fi
$RUN mkdir -p build
$RUN cd build
compile_widelands
move_built_files
$RUN cd ..
create_update_script
if [ $QUIET -eq 0 ]; then # Start of verbose output section
echo " "
echo "###########################################################"
if [ -z "$RUN" ]; then
echo "# Congratulations! Widelands has been built successfully #"
else
echo "# Widelands would have been built #"
fi
echo "# with the following settings: #"
echo "# #"
if [ $BUILD_TYPE = "Release" ]; then
echo "# - Release build #"
elif [ $BUILD_TYPE = "Debug" ]; then
echo "# - Debug build #"
elif [ $BUILD_TYPE = "RelWithDebInfo" ]; then
echo "# - RelWithDebInfo build #"
fi
if [ $BUILD_TESTS = "ON" ]; then
echo "# - Tests #"
else
echo "# - No tests #"
fi
if [ $USE_XDG = "ON" ]; then
echo "# - With support for the XDG Base Directory Specification #"
else
echo "# - Without support for the XDG Base Directory #"
echo "# Specification #"
fi
if [ $BUILD_WEBSITE = "ON" ]; then
echo "# - Website-related executables #"
else
echo "# - No website-related executables #"
fi
if [ -z "$RUN" ]; then
echo "# #"
echo "# You should now be able to run Widelands via #"
echo "# typing ./widelands + ENTER in your terminal #"
echo "# #"
echo "# You can update Widelands via running ./update.sh #"
echo "# in the same directory that you ran this script in. #"
fi
echo "###########################################################"
elif [ -z "$RUN" ]; then
echo "Widelands has been built successfully."
else
echo "### Dry run of compile.sh has completed successfully."
fi # End of verbose output section
######################################