]> git.lizzy.rs Git - rust.git/blob - configure
Refactor how the parser looks for sub-modules
[rust.git] / configure
1 #!/bin/sh
2
3 msg() {
4     echo "configure: $*"
5 }
6
7 step_msg() {
8     msg
9     msg "$1"
10     msg
11 }
12
13 warn() {
14     echo "configure: WARNING: $1"
15 }
16
17 err() {
18     echo "configure: error: $1"
19     exit 1
20 }
21
22 run() {
23     msg "$@"
24     "$@"
25 }
26
27 need_ok() {
28     if [ $? -ne 0 ]
29     then
30         err "$1"
31     fi
32 }
33
34 need_cmd() {
35     if command -v $1 >/dev/null 2>&1
36     then msg "found program '$1'"
37     else err "program '$1' is missing, please install it"
38     fi
39 }
40
41 make_dir() {
42     if [ ! -d $1 ]
43     then
44         run mkdir -p $1
45     fi
46 }
47
48 copy_if_changed() {
49     if cmp -s $1 $2
50     then
51         msg "leaving $2 unchanged"
52     else
53         run cp -f $1 $2
54         chmod u-w $2 # make copied artifact read-only
55     fi
56 }
57
58 move_if_changed() {
59     if cmp -s $1 $2
60     then
61         msg "leaving $2 unchanged"
62     else
63         run mv -f $1 $2
64         chmod u-w $2 # make moved artifact read-only
65     fi
66 }
67
68 putvar() {
69     local T
70     eval T=\$$1
71     eval TLEN=\${#$1}
72     if [ $TLEN -gt 35 ]
73     then
74         printf "configure: %-20s := %.35s ...\n" $1 "$T"
75     else
76         printf "configure: %-20s := %s %s\n" $1 "$T" "$2"
77     fi
78     printf "%-20s := %s\n" $1 "$T" >>config.tmp
79 }
80
81 putpathvar() {
82     local T
83     eval T=\$$1
84     eval TLEN=\${#$1}
85     if [ $TLEN -gt 35 ]
86     then
87         printf "configure: %-20s := %.35s ...\n" $1 "$T"
88     else
89         printf "configure: %-20s := %s %s\n" $1 "$T" "$2"
90     fi
91     if [ -z "$T" ]
92     then
93         printf "%-20s := \n" $1 >>config.tmp
94     else
95         printf "%-20s := \"%s\"\n" $1 "$T" >>config.tmp
96     fi
97 }
98
99 probe() {
100     local V=$1
101     shift
102     local P
103     local T
104     for P
105     do
106         T=$(command -v $P 2>&1)
107         if [ $? -eq 0 ]
108         then
109             VER0=$($P --version 2>/dev/null \
110                 |  grep -o '[vV]\?[0-9][0-9.][a-z0-9.-]*' | head -1 )
111             if [ $? -eq 0 -a "x${VER0}" != "x" ]
112             then
113               VER="($VER0)"
114             else
115               VER=""
116             fi
117             break
118         else
119             VER=""
120             T=""
121         fi
122     done
123     eval $V=\$T
124     putpathvar $V "$VER"
125 }
126
127 probe_need() {
128     local V=$1
129     probe $*
130     eval VV=\$$V
131     if [ -z "$VV" ]
132     then
133         err "needed, but unable to find any of: $*"
134     fi
135 }
136
137 validate_opt () {
138     for arg in $CFG_CONFIGURE_ARGS
139     do
140         isArgValid=0
141         for option in $BOOL_OPTIONS
142         do
143             if test --disable-$option = $arg
144             then
145                 isArgValid=1
146             fi
147             if test --enable-$option = $arg
148             then
149                 isArgValid=1
150             fi
151         done
152         for option in $VAL_OPTIONS
153         do
154             if echo "$arg" | grep -q -- "--$option="
155             then
156                 isArgValid=1
157             fi
158         done
159         if [ "$arg" = "--help" ]
160         then
161             echo
162             echo "No more help available for Configure options,"
163             echo "check the Wiki or join our IRC channel"
164             break
165         else
166             if test $isArgValid -eq 0
167             then
168                 err "Option '$arg' is not recognized"
169             fi
170         fi
171     done
172 }
173
174 # `valopt OPTION_NAME DEFAULT DOC` extracts a string-valued option
175 # from command line, using provided default value for the option if
176 # not present, and saves it to the generated config.mk.
177 #
178 # `valopt_nosave` is much the same, except that it does not save the
179 # result to config.mk (instead the script should use `putvar` itself
180 # later on to save it).  `valopt_core` is the core upon which the
181 # other two are built.
182
183 valopt_core() {
184     VAL_OPTIONS="$VAL_OPTIONS $2"
185
186     local SAVE=$1
187     local OP=$2
188     local DEFAULT=$3
189     shift
190     shift
191     shift
192     local DOC="$*"
193     if [ $HELP -eq 0 ]
194     then
195         local UOP=$(echo $OP | tr '[:lower:]' '[:upper:]' | tr '\-' '\_')
196         local V="CFG_${UOP}"
197         local V_PROVIDED="${V}_PROVIDED"
198         eval $V="$DEFAULT"
199         for arg in $CFG_CONFIGURE_ARGS
200         do
201             if echo "$arg" | grep -q -- "--$OP="
202             then
203                 val=$(echo "$arg" | cut -f2 -d=)
204                 eval $V=$val
205                 eval $V_PROVIDED=1
206             fi
207         done
208         if [ "$SAVE" = "save" ]
209         then
210             putvar $V
211         fi
212     else
213         if [ -z "$DEFAULT" ]
214         then
215             DEFAULT="<none>"
216         fi
217         OP="${OP}=[${DEFAULT}]"
218         printf "    --%-30s %s\n" "$OP" "$DOC"
219     fi
220 }
221
222 valopt_nosave() {
223     valopt_core nosave "$@"
224 }
225
226 valopt() {
227     valopt_core save "$@"
228 }
229
230 # `opt OPTION_NAME DEFAULT DOC` extracts a boolean-valued option from
231 # command line, using the provided default value (0/1) for the option
232 # if not present, and saves it to the generated config.mk.
233 #
234 # `opt_nosave` is much the same, except that it does not save the
235 # result to config.mk (instead the script should use `putvar` itself
236 # later on to save it).  `opt_core` is the core upon which the other
237 # two are built.
238
239 opt_core() {
240     BOOL_OPTIONS="$BOOL_OPTIONS $2"
241
242     local SAVE=$1
243     local OP=$2
244     local DEFAULT=$3
245     shift
246     shift
247     shift
248     local DOC="$*"
249     local FLAG=""
250
251     if [ $DEFAULT -eq 0 ]
252     then
253         FLAG="enable"
254         DEFAULT_FLAG="disable"
255     else
256         FLAG="disable"
257         DEFAULT_FLAG="enable"
258         DOC="don't $DOC"
259     fi
260
261     if [ $HELP -eq 0 ]
262     then
263         for arg in $CFG_CONFIGURE_ARGS
264         do
265             if [ "$arg" = "--${FLAG}-${OP}" ]
266             then
267                 OP=$(echo $OP | tr 'a-z-' 'A-Z_')
268                 FLAG=$(echo $FLAG | tr 'a-z' 'A-Z')
269                 local V="CFG_${FLAG}_${OP}"
270                 local V_PROVIDED="CFG_${FLAG}_${OP}_PROVIDED"
271                 eval $V=1
272                 eval $V_PROVIDED=1
273                 if [ "$SAVE" = "save" ]
274                 then
275                    putvar $V
276                 fi
277             elif [ "$arg" = "--${DEFAULT_FLAG}-${OP}" ]
278             then
279                 OP=$(echo $OP | tr 'a-z-' 'A-Z_')
280                 DEFAULT_FLAG=$(echo $DEFAULT_FLAG | tr 'a-z' 'A-Z')
281                 local V_PROVIDED="CFG_${DEFAULT_FLAG}_${OP}_PROVIDED"
282                 eval $V_PROVIDED=1
283             fi
284         done
285     else
286         if [ ! -z "$META" ]
287         then
288             OP="$OP=<$META>"
289         fi
290         printf "    --%-30s %s\n" "$FLAG-$OP" "$DOC"
291      fi
292 }
293
294 opt_nosave() {
295     opt_core nosave "$@"
296 }
297
298 opt() {
299     opt_core save "$@"
300 }
301
302 envopt() {
303     local NAME=$1
304     local V="CFG_${NAME}"
305     eval VV=\$$V
306
307     # If configure didn't set a value already, then check environment.
308     #
309     # (It is recommended that the configure script always check the
310     # environment before setting any values to envopt variables; see
311     # e.g.  how CFG_CC is handled, where it first checks `-z "$CC"`,
312     # and issues msg if it ends up employing that provided value.)
313     if [ -z "$VV" ]
314     then
315         eval $V=\$$NAME
316         eval VV=\$$V
317     fi
318
319     # If script or environment provided a value, save it.
320     if [ ! -z "$VV" ]
321     then
322         putvar $V
323     fi
324 }
325
326 to_llvm_triple() {
327     case $1 in
328         i686-w64-mingw32) echo i686-pc-windows-gnu ;;
329         x86_64-w64-mingw32) echo x86_64-pc-windows-gnu ;;
330         *) echo $1 ;;
331     esac
332 }
333
334 to_gnu_triple() {
335     case $1 in
336         i686-pc-windows-gnu) echo i686-w64-mingw32 ;;
337         x86_64-pc-windows-gnu) echo x86_64-w64-mingw32 ;;
338         *) echo $1 ;;
339     esac
340 }
341
342 # Prints the absolute path of a directory to stdout
343 abs_path() {
344     local _path="$1"
345     # Unset CDPATH because it causes havok: it makes the destination unpredictable
346     # and triggers 'cd' to print the path to stdout. Route `cd`'s output to /dev/null
347     # for good measure.
348     (unset CDPATH && cd "$_path" > /dev/null && pwd)
349 }
350
351 msg "looking for configure programs"
352 need_cmd cmp
353 need_cmd mkdir
354 need_cmd printf
355 need_cmd cut
356 need_cmd head
357 need_cmd grep
358 need_cmd xargs
359 need_cmd cp
360 need_cmd find
361 need_cmd uname
362 need_cmd date
363 need_cmd tr
364 need_cmd sed
365 need_cmd file
366 need_cmd make
367
368 msg "inspecting environment"
369
370 CFG_OSTYPE=$(uname -s)
371 CFG_CPUTYPE=$(uname -m)
372
373 if [ $CFG_OSTYPE = Darwin -a $CFG_CPUTYPE = i386 ]
374 then
375     # Darwin's `uname -s` lies and always returns i386. We have to use sysctl
376     # instead.
377     if sysctl hw.optional.x86_64 | grep -q ': 1'
378     then
379         CFG_CPUTYPE=x86_64
380     fi
381 fi
382
383 # The goal here is to come up with the same triple as LLVM would,
384 # at least for the subset of platforms we're willing to target.
385
386 case $CFG_OSTYPE in
387
388     Linux)
389         CFG_OSTYPE=unknown-linux-gnu
390         ;;
391
392     FreeBSD)
393         CFG_OSTYPE=unknown-freebsd
394         ;;
395
396     DragonFly)
397         CFG_OSTYPE=unknown-dragonfly
398         ;;
399
400     Bitrig)
401         CFG_OSTYPE=unknown-bitrig
402         ;;
403
404     OpenBSD)
405         CFG_OSTYPE=unknown-openbsd
406        ;;
407
408     Darwin)
409         CFG_OSTYPE=apple-darwin
410         ;;
411
412     MINGW*)
413         # msys' `uname` does not print gcc configuration, but prints msys
414         # configuration. so we cannot believe `uname -m`:
415         # msys1 is always i686 and msys2 is always x86_64.
416         # instead, msys defines $MSYSTEM which is MINGW32 on i686 and
417         # MINGW64 on x86_64.
418         CFG_CPUTYPE=i686
419         CFG_OSTYPE=pc-windows-gnu
420         if [ "$MSYSTEM" = MINGW64 ]
421         then
422             CFG_CPUTYPE=x86_64
423         fi
424         ;;
425
426     MSYS*)
427         CFG_OSTYPE=pc-windows-gnu
428         ;;
429
430 # Thad's Cygwin identifiers below
431
432 #   Vista 32 bit
433     CYGWIN_NT-6.0)
434         CFG_OSTYPE=pc-windows-gnu
435         CFG_CPUTYPE=i686
436         ;;
437
438 #   Vista 64 bit
439     CYGWIN_NT-6.0-WOW64)
440         CFG_OSTYPE=pc-windows-gnu
441         CFG_CPUTYPE=x86_64
442         ;;
443
444 #   Win 7 32 bit
445     CYGWIN_NT-6.1)
446         CFG_OSTYPE=pc-windows-gnu
447         CFG_CPUTYPE=i686
448         ;;
449
450 #   Win 7 64 bit
451     CYGWIN_NT-6.1-WOW64)
452         CFG_OSTYPE=pc-windows-gnu
453         CFG_CPUTYPE=x86_64
454         ;;
455
456 #   Win 8 # uname -s on 64-bit cygwin does not contain WOW64, so simply use uname -m to detect arch (works in my install)
457     CYGWIN_NT-6.3)
458         CFG_OSTYPE=pc-windows-gnu
459         ;;
460 # We do not detect other OS such as XP/2003 using 64 bit using uname.
461 # If we want to in the future, we will need to use Cygwin - Chuck's csih helper in /usr/lib/csih/winProductName.exe or alternative.
462     *)
463         err "unknown OS type: $CFG_OSTYPE"
464         ;;
465 esac
466
467
468 case $CFG_CPUTYPE in
469
470     i386 | i486 | i686 | i786 | x86)
471         CFG_CPUTYPE=i686
472         ;;
473
474     xscale | arm)
475         CFG_CPUTYPE=arm
476         ;;
477
478     armv7l)
479         CFG_CPUTYPE=arm
480         CFG_OSTYPE="${CFG_OSTYPE}eabihf"
481         ;;
482
483     aarch64)
484         CFG_CPUTYPE=aarch64
485         ;;
486
487     # At some point, when ppc64[le] support happens, this will need to do
488     # something clever. For now it's safe to assume that we're only ever
489     # interested in building 32 bit.
490     powerpc | ppc | ppc64)
491         CFG_CPUTYPE=powerpc
492         ;;
493
494     x86_64 | x86-64 | x64 | amd64)
495         CFG_CPUTYPE=x86_64
496         ;;
497
498     *)
499         err "unknown CPU type: $CFG_CPUTYPE"
500 esac
501
502 # Detect 64 bit linux systems with 32 bit userland and force 32 bit compilation
503 if [ $CFG_OSTYPE = unknown-linux-gnu -a $CFG_CPUTYPE = x86_64 ]
504 then
505     # $SHELL does not exist in standard 'sh', so probably only exists
506     # if configure is running in an interactive bash shell. /usr/bin/env
507     # exists *everywhere*.
508     BIN_TO_PROBE="$SHELL"
509     if [ -z "$BIN_TO_PROBE" -a -e "/usr/bin/env" ]; then
510        BIN_TO_PROBE="/usr/bin/env"
511     fi
512     if [ -n "$BIN_TO_PROBE" ]; then
513        file -L "$BIN_TO_PROBE" | grep -q "x86[_-]64"
514        if [ $? != 0 ]; then
515             CFG_CPUTYPE=i686
516        fi
517      fi
518 fi
519
520
521 DEFAULT_BUILD="${CFG_CPUTYPE}-${CFG_OSTYPE}"
522
523 CFG_SRC_DIR="$(abs_path $(dirname $0))/"
524 CFG_SRC_DIR_RELATIVE="$(dirname $0)/"
525 CFG_BUILD_DIR="$(pwd)/"
526 CFG_SELF="$0"
527 CFG_CONFIGURE_ARGS="$@"
528
529 OPTIONS=""
530 HELP=0
531 if [ "$1" = "--help" ]
532 then
533     HELP=1
534     shift
535     echo
536     echo "Usage: $CFG_SELF [options]"
537     echo
538     echo "Options:"
539     echo
540 else
541     msg "recreating config.tmp"
542     echo '' >config.tmp
543
544     step_msg "processing $CFG_SELF args"
545 fi
546
547 BOOL_OPTIONS=""
548 VAL_OPTIONS=""
549
550 opt debug 0 "debug mode; disables optimization unless \`--enable-optimize\` given"
551 opt valgrind 0 "run tests with valgrind (memcheck by default)"
552 opt helgrind 0 "run tests with helgrind instead of memcheck"
553 opt valgrind-rpass 1 "run rpass-valgrind tests with valgrind"
554 opt docs     1 "build standard library documentation"
555 opt compiler-docs     0 "build compiler documentation"
556 opt optimize-tests 1 "build tests with optimizations"
557 opt debuginfo-tests 0 "build tests with debugger metadata"
558 opt libcpp 1 "build with llvm with libc++ instead of libstdc++ when using clang"
559 opt llvm-assertions 0 "build LLVM with assertions"
560 opt debug-assertions 0 "build with debugging assertions"
561 opt fast-make 0 "use .gitmodules as timestamp for submodule deps"
562 opt ccache 0 "invoke gcc/clang via ccache to reuse object files between builds"
563 opt local-rust 0 "use an installed rustc rather than downloading a snapshot"
564 opt llvm-static-stdcpp 0 "statically link to libstdc++ for LLVM"
565 opt rpath 0 "build rpaths into rustc itself"
566 # This is used by the automation to produce single-target nightlies
567 opt dist-host-only 0 "only install bins for the host architecture"
568 opt inject-std-version 1 "inject the current compiler version of libstd into programs"
569 opt llvm-version-check 1 "check if the LLVM version is supported, build anyway"
570
571 # Optimization and debugging options. These may be overridden by the release channel, etc.
572 opt_nosave optimize 1 "build optimized rust code"
573 opt_nosave optimize-cxx 1 "build optimized C++ code"
574 opt_nosave optimize-llvm 1 "build optimized LLVM"
575 opt_nosave llvm-assertions 0 "build LLVM with assertions"
576 opt_nosave debug-assertions 0 "build with debugging assertions"
577 opt_nosave debuginfo 0 "build with debugger metadata"
578 opt_nosave debug-jemalloc 0 "build jemalloc with --enable-debug --enable-fill"
579
580 valopt localstatedir "/var/lib" "local state directory"
581 valopt sysconfdir "/etc" "install system configuration files"
582
583 valopt datadir "${CFG_PREFIX}/share" "install data"
584 valopt infodir "${CFG_PREFIX}/share/info" "install additional info"
585 valopt llvm-root "" "set LLVM root"
586 valopt python "" "set path to python"
587 valopt jemalloc-root "" "set directory where libjemalloc_pic.a is located"
588 valopt build "${DEFAULT_BUILD}" "GNUs ./configure syntax LLVM build triple"
589 valopt android-cross-path "/opt/ndk_standalone" "Android NDK standalone path"
590 valopt release-channel "dev" "the name of the release channel to build"
591 valopt musl-root "/usr/local" "MUSL root installation directory"
592
593 # Many of these are saved below during the "writing configuration" step
594 # (others are conditionally saved).
595 opt_nosave manage-submodules 1 "let the build manage the git submodules"
596 opt_nosave clang 0 "prefer clang to gcc for building the runtime"
597 opt_nosave jemalloc 1 "build liballoc with jemalloc"
598 opt elf-tls 1 "elf thread local storage on platforms where supported"
599
600 valopt_nosave prefix "/usr/local" "set installation prefix"
601 valopt_nosave local-rust-root "/usr/local" "set prefix for local rust binary"
602 valopt_nosave host "${CFG_BUILD}" "GNUs ./configure syntax LLVM host triples"
603 valopt_nosave target "${CFG_HOST}" "GNUs ./configure syntax LLVM target triples"
604 valopt_nosave mandir "${CFG_PREFIX}/share/man" "install man pages in PATH"
605
606 # Temporarily support old triples until buildbots get updated
607 CFG_BUILD=$(to_llvm_triple $CFG_BUILD)
608 putvar CFG_BUILD # Yes, this creates a duplicate entry, but the last one wins.
609 CFG_HOST=$(to_llvm_triple $CFG_HOST)
610 CFG_TARGET=$(to_llvm_triple $CFG_TARGET)
611
612 # On windows we just store the libraries in the bin directory because
613 # there's no rpath. This is where the build system itself puts libraries;
614 # --libdir is used to configure the installation directory.
615 # FIXME: This needs to parameterized over target triples. Do it in platform.mk
616 if [ "$CFG_OSTYPE" = "pc-windows-gnu" ] || [ "$CFG_OSTYPE" = "pc-windows-msvc" ]
617 then
618     CFG_LIBDIR_RELATIVE=bin
619 else
620     CFG_LIBDIR_RELATIVE=lib
621 fi
622
623 valopt libdir "${CFG_PREFIX}/${CFG_LIBDIR_RELATIVE}" "install libraries (do not set it on windows platform)"
624
625 case "$CFG_LIBDIR" in
626     "$CFG_PREFIX"/*) CAT_INC=2;;
627     "$CFG_PREFIX"*)  CAT_INC=1;;
628     *)
629         err "libdir must begin with the prefix. Use --prefix to set it accordingly.";;
630 esac
631
632 CFG_LIBDIR_RELATIVE=`echo ${CFG_LIBDIR} | cut -c$((${#CFG_PREFIX}+${CAT_INC}))-`
633
634 if ( [ "$CFG_OSTYPE" = "pc-windows-gnu" ] || [ "$CFG_OSTYPE" = "pc-windows-msvc" ] ) \
635         && [ "$CFG_LIBDIR_RELATIVE" != "bin" ]; then
636     err "libdir on windows should be set to 'bin'"
637 fi
638
639 if [ $HELP -eq 1 ]
640 then
641     echo
642     exit 0
643 fi
644
645 # Validate Options
646 step_msg "validating $CFG_SELF args"
647 validate_opt
648
649 # Validate the release channel, and configure options
650 case "$CFG_RELEASE_CHANNEL" in
651     nightly )
652         msg "overriding settings for $CFG_RELEASE_CHANNEL"
653         CFG_ENABLE_LLVM_ASSERTIONS=1
654         ;;
655     dev | beta | stable)
656         ;;
657     *)
658         err "release channel must be 'dev', 'nightly', 'beta' or 'stable'"
659         ;;
660 esac
661
662 # Adjust perf and debug options for debug mode
663 if [ -n "$CFG_ENABLE_DEBUG" ]; then
664     msg "debug mode enabled, setting performance options"
665     if [ -z "$CFG_ENABLE_OPTIMIZE_PROVIDED" ]; then
666         msg "optimization not explicitly enabled, disabling optimization"
667         CFG_DISABLE_OPTIMIZE=1
668         CFG_DISABLE_OPTIMIZE_CXX=1
669     fi
670     CFG_ENABLE_DEBUG_ASSERTIONS=1
671     CFG_ENABLE_DEBUG_JEMALLOC=1
672     CFG_ENABLE_DEBUGINFO=1
673     CFG_ENABLE_LLVM_ASSERTIONS=1
674 fi
675
676 # OK, now write the debugging options
677 if [ -n "$CFG_DISABLE_OPTIMIZE" ]; then putvar CFG_DISABLE_OPTIMIZE; fi
678 if [ -n "$CFG_DISABLE_OPTIMIZE_CXX" ]; then putvar CFG_DISABLE_OPTIMIZE_CXX; fi
679 if [ -n "$CFG_DISABLE_OPTIMIZE_LLVM" ]; then putvar CFG_DISABLE_OPTIMIZE_LLVM; fi
680 if [ -n "$CFG_ENABLE_LLVM_ASSERTIONS" ]; then putvar CFG_ENABLE_LLVM_ASSERTIONS; fi
681 if [ -n "$CFG_ENABLE_DEBUG_ASSERTIONS" ]; then putvar CFG_ENABLE_DEBUG_ASSERTIONS; fi
682 if [ -n "$CFG_ENABLE_DEBUGINFO" ]; then putvar CFG_ENABLE_DEBUGINFO; fi
683 if [ -n "$CFG_ENABLE_DEBUG_JEMALLOC" ]; then putvar CFG_ENABLE_DEBUG_JEMALLOC; fi
684
685 # A magic value that allows the compiler to use unstable features
686 # during the bootstrap even when doing so would normally be an error
687 # because of feature staging or because the build turns on
688 # warnings-as-errors and unstable features default to warnings.  The
689 # build has to match this key in an env var. Meant to be a mild
690 # deterrent from users just turning on unstable features on the stable
691 # channel.
692 # Basing CFG_BOOTSTRAP_KEY on CFG_BOOTSTRAP_KEY lets it get picked up
693 # during a Makefile reconfig.
694 CFG_BOOTSTRAP_KEY="${CFG_BOOTSTRAP_KEY-`date +%H:%M:%S`}"
695 putvar CFG_BOOTSTRAP_KEY
696
697 step_msg "looking for build programs"
698
699 probe_need CFG_CURLORWGET  curl wget
700 if [ -z "$CFG_PYTHON_PROVIDED" ]; then
701     probe_need CFG_PYTHON      python2.7 python2.6 python2 python
702 fi
703
704 python_version=$($CFG_PYTHON -V 2>&1)
705 if [ $(echo $python_version | grep -c '^Python 2\.[4567]') -ne 1 ]; then
706     err "Found $python_version, but LLVM requires Python 2.4-2.7"
707 fi
708
709 # If we have no git directory then we are probably a tarball distribution
710 # and shouldn't attempt to load submodules
711 if [ ! -e ${CFG_SRC_DIR}.git ]
712 then
713     probe CFG_GIT          git
714     msg "git: no git directory. disabling submodules"
715     CFG_DISABLE_MANAGE_SUBMODULES=1
716 else
717     probe_need CFG_GIT     git
718 fi
719
720 # Use `md5sum` on GNU platforms, or `md5 -q` on BSD
721 probe CFG_MD5              md5
722 probe CFG_MD5SUM           md5sum
723 if [ -n "$CFG_MD5" ]
724 then
725     CFG_HASH_COMMAND="$CFG_MD5 -q | cut -c 1-8"
726 elif [ -n "$CFG_MD5SUM" ]
727 then
728     CFG_HASH_COMMAND="$CFG_MD5SUM | cut -c 1-8"
729 else
730     err 'could not find one of: md5 md5sum'
731 fi
732 putvar CFG_HASH_COMMAND
733
734 probe CFG_CLANG            clang++
735 probe CFG_CCACHE           ccache
736 probe CFG_GCC              gcc
737 probe CFG_LD               ld
738 probe CFG_VALGRIND         valgrind
739 probe CFG_PERF             perf
740 probe CFG_ISCC             iscc
741 probe CFG_ANTLR4           antlr4
742 probe CFG_GRUN             grun
743 probe CFG_FLEX             flex
744 probe CFG_BISON            bison
745 probe CFG_PANDOC           pandoc
746 probe CFG_XELATEX          xelatex
747 probe CFG_GDB              gdb
748 probe CFG_LLDB             lldb
749
750 # On MacOS X, invoking `javac` pops up a dialog if the JDK is not
751 # installed. Since `javac` is only used if `antlr4` is available,
752 # probe for it only in this case.
753 if [ ! -z "$CFG_ANTLR4" ]
754 then
755    probe CFG_JAVAC            javac
756 fi
757
758 # the valgrind rpass tests will fail if you don't have a valgrind, but they're
759 # only disabled if you opt out.
760 if [ -z "$CFG_VALGRIND" ]
761 then
762     # If the user has explicitly asked for valgrind tests, then fail
763     if [ -n "$CFG_ENABLE_VALGRIND" ] && [ -n "$CFG_ENABLE_VALGRIND_PROVIDED" ]
764     then
765         err "No valgrind present, but valgrind tests explicitly requested"
766     else
767         CFG_DISABLE_VALGRIND_RPASS=1
768         putvar CFG_DISABLE_VALGRIND_RPASS
769     fi
770 fi
771
772 if [ ! -z "$CFG_GDB" ]
773 then
774     # Store GDB's version
775     CFG_GDB_VERSION=$($CFG_GDB --version 2>/dev/null | head -1)
776     putvar CFG_GDB_VERSION
777 fi
778
779 if [ ! -z "$CFG_LLDB" ]
780 then
781     # Store LLDB's version
782     CFG_LLDB_VERSION=$($CFG_LLDB --version 2>/dev/null | head -1)
783     putvar CFG_LLDB_VERSION
784
785     # If CFG_LLDB_PYTHON_DIR is not already set from the outside and valid, try to read it from
786     # LLDB via the -P commandline options.
787     if [ -z "$CFG_LLDB_PYTHON_DIR" ] || [ ! -d "$CFG_LLDB_PYTHON_DIR" ]
788     then
789         CFG_LLDB_PYTHON_DIR=$($CFG_LLDB -P)
790
791         # If CFG_LLDB_PYTHON_DIR is not a valid directory, set it to something more readable
792         if [ ! -d "$CFG_LLDB_PYTHON_DIR" ]
793         then
794             CFG_LLDB_PYTHON_DIR="LLDB_PYTHON_DIRECTORY_NOT_FOUND"
795         fi
796
797         putvar CFG_LLDB_PYTHON_DIR
798     fi
799 fi
800
801 step_msg "looking for target specific programs"
802
803 probe CFG_ADB        adb
804
805 if [ ! -z "$CFG_PANDOC" ]
806 then
807     # Extract "MAJOR MINOR" from Pandoc's version number
808     PV_MAJOR_MINOR=$(pandoc --version | grep '^pandoc' |
809         sed -E 's/pandoc(.exe)? ([0-9]+)\.([0-9]+).*/\2 \3/')
810
811     MIN_PV_MAJOR="1"
812     MIN_PV_MINOR="9"
813
814     # these patterns are shell globs, *not* regexps
815     PV_MAJOR=${PV_MAJOR_MINOR% *}
816     PV_MINOR=${PV_MAJOR_MINOR#* }
817
818     if [ "$PV_MAJOR" -lt "$MIN_PV_MAJOR" ] || [ "$PV_MINOR" -lt "$MIN_PV_MINOR" ]
819     then
820         step_msg "pandoc $PV_MAJOR.$PV_MINOR is too old. Need at least $MIN_PV_MAJOR.$MIN_PV_MINOR. Disabling"
821         BAD_PANDOC=1
822     fi
823 fi
824
825 BIN_SUF=
826 if [ "$CFG_OSTYPE" = "pc-windows-gnu" ] || [ "$CFG_OSTYPE" = "pc-windows-msvc" ]
827 then
828     BIN_SUF=.exe
829 fi
830
831 if [ ! -z "$CFG_ENABLE_LOCAL_RUST" ]
832 then
833     system_rustc=$(which rustc)
834     if [ -f ${CFG_LOCAL_RUST_ROOT}/bin/rustc${BIN_SUF} ]
835     then
836         : # everything already configured
837     elif [ -n "$system_rustc" ]
838     then
839         # we assume that rustc is in a /bin directory
840         CFG_LOCAL_RUST_ROOT=${system_rustc%/bin/rustc}
841     else
842         err "no local rust to use"
843     fi
844
845     CMD="${CFG_LOCAL_RUST_ROOT}/bin/rustc${BIN_SUF}"
846     LRV=`$CMD --version`
847     if [ $? -ne 0 ]
848     then
849         step_msg "failure while running $CMD --version"
850         exit 1
851     fi
852     step_msg "using rustc at: ${CFG_LOCAL_RUST_ROOT} with version: $LRV"
853     putvar CFG_LOCAL_RUST_ROOT
854 fi
855
856 # Force bitrig to build with clang; gcc doesn't like us there
857 if [ $CFG_OSTYPE = unknown-bitrig ]
858 then
859     step_msg "on Bitrig, forcing use of clang, disabling jemalloc"
860     CFG_ENABLE_CLANG=1
861     CFG_DISABLE_JEMALLOC=1
862 fi
863
864 # OS X 10.9, gcc is actually clang. This can cause some confusion in the build
865 # system, so if we find that gcc is clang, we should just use clang directly.
866 if [ $CFG_OSTYPE = apple-darwin -a -z "$CFG_ENABLE_CLANG" ]
867 then
868     CFG_OSX_GCC_VERSION=$("$CFG_GCC" --version 2>&1 | grep "Apple LLVM version")
869     if [ $? -eq 0 ]
870     then
871         step_msg "on OS X >=10.9, forcing use of clang"
872         CFG_ENABLE_CLANG=1
873     else
874         if [ $("$CFG_GCC" --version 2>&1 | grep -c ' 4\.[0-6]') -ne 0 ]; then
875             step_msg "older GCC found, using clang instead"
876             CFG_ENABLE_CLANG=1
877         else
878             # on OS X, with xcode 5 and newer, certain developers may have
879             # cc, gcc and g++ point to a  mixture of clang and gcc
880             # if so, this will create very strange build errors
881             # this last stanza is to detect some such problems and save the future rust
882             # contributor some time solving that issue.
883             # this detection could be generalized to other OSes aside from OS X
884             # but the issue seems most likely to happen on OS X
885
886             chk_cc () {
887                 $1 --version 2> /dev/null | grep -q $2
888             }
889             # check that gcc, cc and g++ all point to the same compiler.
890             # note that for xcode 5, g++ points to clang, not clang++
891             if !((chk_cc gcc clang  && chk_cc g++ clang) ||
892                 (chk_cc gcc gcc  &&( chk_cc g++ g++ || chk g++ gcc))); then
893                 err "the gcc and g++ in your path point to different compilers.
894     Check which versions are in your path with gcc --version and g++ --version.
895     To resolve this problem, either fix your PATH  or run configure with --enable-clang"
896             fi
897
898         fi
899     fi
900 fi
901
902 # If the clang isn't already enabled, check for GCC, and if it is missing, turn
903 # on clang as a backup.
904 if [ -z "$CFG_ENABLE_CLANG" ]
905 then
906   CFG_GCC_VERSION=$("$CFG_GCC" --version 2>&1)
907   if [ $? -ne 0 ]
908   then
909     step_msg "GCC not installed, will try using Clang"
910     CFG_ENABLE_CLANG=1
911   fi
912 fi
913
914 # Okay, at this point, we have made up our minds about whether we are
915 # going to force CFG_ENABLE_CLANG or not; save the setting if so.
916 if [ ! -z "$CFG_ENABLE_CLANG" ]
917 then
918     putvar CFG_ENABLE_CLANG
919 fi
920
921 # Same with jemalloc.  save the setting here.
922 if [ ! -z "$CFG_DISABLE_JEMALLOC" ]
923 then
924     putvar CFG_DISABLE_JEMALLOC
925 fi
926
927 if [ ! -z "$CFG_LLVM_ROOT" -a -z "$CFG_DISABLE_LLVM_VERSION_CHECK" -a -e "$CFG_LLVM_ROOT/bin/llvm-config" ]
928 then
929     step_msg "using custom LLVM at $CFG_LLVM_ROOT"
930
931     LLVM_CONFIG="$CFG_LLVM_ROOT/bin/llvm-config"
932     LLVM_VERSION=$($LLVM_CONFIG --version)
933
934     case $LLVM_VERSION in
935         (3.[5-7]*)
936             msg "found ok version of LLVM: $LLVM_VERSION"
937             ;;
938         (*)
939             err "bad LLVM version: $LLVM_VERSION, need >=3.5"
940             ;;
941     esac
942 fi
943
944 # Even when the user overrides the choice of CC, still try to detect
945 # clang to disable some clang-specific warnings.  We here draw a
946 # distinction between:
947 #
948 #  CFG_ENABLE_CLANG : passed --enable-clang, or host "requires" clang,
949 #  CFG_USING_CLANG : compiler (clang / gcc / $CC) looks like clang.
950 #
951 # This distinction is important because there are some safeguards we
952 # would prefer to skip when merely CFG_USING_CLANG is set; but when
953 # CFG_ENABLE_CLANG is set, that indicates that we are opting into
954 # running such safeguards.
955
956 if [ ! -z "$CC" ]
957 then
958     msg "skipping compiler inference steps; using provided CC=$CC"
959     CFG_CC="$CC"
960
961     CFG_OSX_CC_VERSION=$("$CFG_CC" --version 2>&1 | grep "clang")
962     if [ $? -eq 0 ]
963     then
964         step_msg "note, user-provided CC looks like clang; CC=$CC."
965         CFG_USING_CLANG=1
966         putvar CFG_USING_CLANG
967     fi
968 else
969     if [ ! -z "$CFG_ENABLE_CLANG" ]
970     then
971         if [ -z "$CFG_CLANG" ]
972         then
973             err "clang requested but not found"
974         fi
975         CFG_CC="$CFG_CLANG"
976         CFG_USING_CLANG=1
977         putvar CFG_USING_CLANG
978     else
979         CFG_CC="gcc"
980     fi
981 fi
982
983 if [ ! -z "$CFG_ENABLE_CLANG" ]
984 then
985     case "$CC" in
986         (''|*clang)
987         CFG_CLANG_VERSION=$($CFG_CC \
988             --version \
989             | grep version \
990             | sed 's/.*\(version .*\)/\1/; s/.*based on \(LLVM .*\))/\1/' \
991             | cut -d ' ' -f 2)
992
993         case $CFG_CLANG_VERSION in
994             (3.2* | 3.3* | 3.4* | 3.5* | 3.6* | 3.7*)
995             step_msg "found ok version of CLANG: $CFG_CLANG_VERSION"
996             if [ -z "$CC" ]
997             then
998                 CFG_CC="clang"
999                 CFG_CXX="clang++"
1000             fi
1001             ;;
1002             (*)
1003             err "bad CLANG version: $CFG_CLANG_VERSION, need >=3.0svn"
1004             ;;
1005         esac
1006         ;;
1007         (*)
1008         msg "skipping CFG_ENABLE_CLANG version check; provided CC=$CC"
1009         ;;
1010     esac
1011 fi
1012
1013 if [ ! -z "$CFG_ENABLE_CCACHE" ]
1014 then
1015     if [ -z "$CC" ]
1016     then
1017         if [ -z "$CFG_CCACHE" ]
1018         then
1019             err "ccache requested but not found"
1020         fi
1021
1022         CFG_CC="ccache $CFG_CC"
1023     fi
1024 fi
1025
1026 if [ -z "$CC" -a -z "$CFG_ENABLE_CLANG" -a -z "$CFG_GCC" ]
1027 then
1028     err "either clang or gcc is required"
1029 fi
1030
1031 # All safeguards based on $CFG_ENABLE_CLANG should occur before this
1032 # point in the script; after this point, script logic should inspect
1033 # $CFG_USING_CLANG rather than $CFG_ENABLE_CLANG.
1034
1035 # Set CFG_{CC,CXX,CPP,CFLAGS,CXXFLAGS}
1036 envopt CC
1037 envopt CXX
1038 envopt CPP
1039 envopt CFLAGS
1040 envopt CXXFLAGS
1041
1042 # a little post-processing of various config values
1043 CFG_PREFIX=${CFG_PREFIX%/}
1044 CFG_MANDIR=${CFG_MANDIR%/}
1045 CFG_HOST="$(echo $CFG_HOST | tr ',' ' ')"
1046 CFG_TARGET="$(echo $CFG_TARGET | tr ',' ' ')"
1047 CFG_SUPPORTED_TARGET=""
1048 for target_file in ${CFG_SRC_DIR}mk/cfg/*.mk; do
1049   CFG_SUPPORTED_TARGET="${CFG_SUPPORTED_TARGET} $(basename "$target_file" .mk)"
1050 done
1051
1052 # copy build-triples to host-triples so that builds are a subset of hosts
1053 V_TEMP=""
1054 for i in $CFG_BUILD $CFG_HOST;
1055 do
1056    echo "$V_TEMP" | grep -qF $i || V_TEMP="$V_TEMP${V_TEMP:+ }$i"
1057 done
1058 CFG_HOST=$V_TEMP
1059
1060 # copy host-triples to target-triples so that hosts are a subset of targets
1061 V_TEMP=""
1062 for i in $CFG_HOST $CFG_TARGET;
1063 do
1064    echo "$V_TEMP" | grep -qF $i || V_TEMP="$V_TEMP${V_TEMP:+ }$i"
1065 done
1066 CFG_TARGET=$V_TEMP
1067
1068 # check target-specific tool-chains
1069 for i in $CFG_TARGET
1070 do
1071     L_CHECK=false
1072     for j in $CFG_SUPPORTED_TARGET
1073     do
1074         if [ $i = $j ]
1075         then
1076             L_CHECK=true
1077         fi
1078     done
1079
1080     if [ $L_CHECK = false ]
1081     then
1082         err "unsupported target triples \"$i\" found"
1083     fi
1084
1085     case $i in
1086         arm-linux-androideabi)
1087
1088             if [ ! -f $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-gcc ]
1089             then
1090                 err "NDK $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-gcc not found"
1091             fi
1092             if [ ! -f $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-g++ ]
1093             then
1094                 err "NDK $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-g++ not found"
1095             fi
1096             if [ ! -f $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-ar ]
1097             then
1098                 err "NDK $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-ar not found"
1099             fi
1100             ;;
1101
1102         arm-apple-darwin)
1103             if [ $CFG_OSTYPE != apple-darwin ]
1104             then
1105                 err "The iOS target is only supported on Mac OS X"
1106             fi
1107             ;;
1108
1109
1110         *-musl)
1111             if [ ! -f $CFG_MUSL_ROOT/lib/libc.a ]
1112             then
1113                 err "musl libc $CFG_MUSL_ROOT/lib/libc.a not found"
1114             fi
1115             ;;
1116
1117         *-msvc)
1118             # Currently the build system is not configured to build jemalloc
1119             # with MSVC, so we omit this optional dependency.
1120             step_msg "targeting MSVC, disabling jemalloc"
1121             CFG_DISABLE_JEMALLOC=1
1122             putvar CFG_DISABLE_JEMALLOC
1123
1124             # There are some MSYS python builds which will auto-translate
1125             # windows-style paths to MSYS-style paths in Python itself.
1126             # Unfortunately this breaks LLVM's build system as somewhere along
1127             # the line LLVM prints a path into a file from Python and then CMake
1128             # later tries to interpret that path. If Python prints a MSYS path
1129             # and CMake tries to use it as a Windows path, you're gonna have a
1130             # Bad Time.
1131             #
1132             # Consequently here we try to detect when that happens and print an
1133             # error if it does.
1134             if $CFG_PYTHON -c 'import sys; print sys.argv[1]' `pwd` | grep '^/'
1135             then
1136                 err "python is silently translating windows paths to MSYS paths \
1137                      and the build will fail if this python is used.\n\n \
1138                      Either an official python install must be used or an \
1139                      alternative python package in MinGW must be used."
1140             fi
1141
1142             # MSVC requires cmake because that's how we're going to build LLVM
1143             probe_need CFG_CMAKE cmake
1144
1145             # Use the REG program to figure out where VS is installed
1146             # We need to figure out where cl.exe and link.exe are, so we do some
1147             # munging and some probing here. We also look for the default
1148             # INCLUDE and LIB variables for MSVC so we can set those in the
1149             # build system as well.
1150             install=$(reg QUERY \
1151                        'HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\12.0' \
1152                        -v InstallDir)
1153             need_ok "couldn't find visual studio install root"
1154             CFG_MSVC_ROOT=$(echo "$install" | grep InstallDir | sed 's/.*REG_SZ[ ]*//')
1155             CFG_MSVC_ROOT=$(dirname "$CFG_MSVC_ROOT")
1156             CFG_MSVC_ROOT=$(dirname "$CFG_MSVC_ROOT")
1157             putvar CFG_MSVC_ROOT
1158
1159             case $i in
1160                 x86_64-*)
1161                     bits=x86_64
1162                     msvc_part=amd64
1163                     ;;
1164                 i686-*)
1165                     bits=i386
1166                     msvc_part=
1167                     ;;
1168                 *)
1169                     err "can only target x86 targets for MSVC"
1170                     ;;
1171             esac
1172             bindir="${CFG_MSVC_ROOT}/VC/bin"
1173             if [ ! -z "$msvc_part" ]; then
1174                 bindir="$bindir/$msvc_part"
1175             fi
1176             eval CFG_MSVC_BINDIR_$bits="\"$bindir\""
1177             eval CFG_MSVC_CL_$bits="\"$bindir/cl.exe\""
1178             eval CFG_MSVC_LIB_$bits="\"$bindir/lib.exe\""
1179             eval CFG_MSVC_LINK_$bits="\"$bindir/link.exe\""
1180
1181             vcvarsall="${CFG_MSVC_ROOT}/VC/vcvarsall.bat"
1182             include_path=$(cmd /c "\"$vcvarsall\" $msvc_part && cmd /c echo %INCLUDE%")
1183             need_ok "failed to learn about MSVC's INCLUDE"
1184             lib_path=$(cmd /c "\"$vcvarsall\" $msvc_part && cmd /c echo %LIB%")
1185             need_ok "failed to learn about MSVC's LIB"
1186
1187             eval CFG_MSVC_INCLUDE_PATH_${bits}="\"$include_path\""
1188             eval CFG_MSVC_LIB_PATH_${bits}="\"$lib_path\""
1189
1190             putvar CFG_MSVC_BINDIR_${bits}
1191             putvar CFG_MSVC_CL_${bits}
1192             putvar CFG_MSVC_LIB_${bits}
1193             putvar CFG_MSVC_LINK_${bits}
1194             putvar CFG_MSVC_INCLUDE_PATH_${bits}
1195             putvar CFG_MSVC_LIB_PATH_${bits}
1196             ;;
1197
1198         *)
1199             ;;
1200     esac
1201 done
1202
1203 if [ ! -z "$CFG_PERF" ]
1204 then
1205     HAVE_PERF_LOGFD=`$CFG_PERF stat --log-fd 2>&1 | grep 'unknown option'`
1206     if [ -z "$HAVE_PERF_LOGFD" ];
1207     then
1208         CFG_PERF_WITH_LOGFD=1
1209         putvar CFG_PERF_WITH_LOGFD
1210     fi
1211 fi
1212
1213 step_msg "making directories"
1214
1215 for i in \
1216     doc doc/std doc/extra \
1217     dl tmp dist
1218 do
1219     make_dir $i
1220 done
1221
1222 for t in $CFG_HOST
1223 do
1224     make_dir $t/llvm
1225 done
1226
1227 for t in $CFG_HOST
1228 do
1229     make_dir $t/rustllvm
1230 done
1231
1232 for t in $CFG_TARGET
1233 do
1234   make_dir $t/rt
1235   for s in 0 1 2 3
1236   do
1237     make_dir $t/rt/stage$s
1238     make_dir $t/rt/jemalloc
1239     make_dir $t/rt/compiler-rt
1240     for i in                                          \
1241       isaac sync test \
1242       arch/i386 arch/x86_64 arch/arm arch/aarch64 arch/mips arch/powerpc
1243     do
1244       make_dir $t/rt/stage$s/$i
1245     done
1246   done
1247 done
1248
1249 for h in $CFG_HOST
1250 do
1251     for t in $CFG_TARGET
1252     do
1253         # host lib dir stage0
1254         make_dir $h/stage0/lib
1255
1256         # target bin dir stage0
1257         make_dir $h/stage0/lib/rustlib/$t/bin
1258
1259         # target lib dir stage0
1260         make_dir $h/stage0/lib/rustlib/$t/lib
1261
1262         for i in 0 1 2 3
1263         do
1264             # host bin dir
1265             make_dir $h/stage$i/bin
1266
1267             # host lib dir
1268             make_dir $h/stage$i/$CFG_LIBDIR_RELATIVE
1269
1270             # host test dir
1271             make_dir $h/stage$i/test
1272
1273             # target bin dir
1274             make_dir $h/stage$i/$CFG_LIBDIR_RELATIVE/rustlib/$t/bin
1275
1276             # target lib dir
1277             make_dir $h/stage$i/$CFG_LIBDIR_RELATIVE/rustlib/$t/lib
1278         done
1279     done
1280
1281     make_dir $h/test/run-pass
1282     make_dir $h/test/run-pass-valgrind
1283     make_dir $h/test/run-pass-fulldeps
1284     make_dir $h/test/run-fail
1285     make_dir $h/test/run-fail-fulldeps
1286     make_dir $h/test/compile-fail
1287     make_dir $h/test/parse-fail
1288     make_dir $h/test/compile-fail-fulldeps
1289     make_dir $h/test/bench
1290     make_dir $h/test/perf
1291     make_dir $h/test/pretty
1292     make_dir $h/test/debuginfo-gdb
1293     make_dir $h/test/debuginfo-lldb
1294     make_dir $h/test/codegen
1295     make_dir $h/test/rustdoc
1296 done
1297
1298 # Configure submodules
1299 step_msg "configuring submodules"
1300
1301 # Have to be in the top of src directory for this
1302 if [ -z $CFG_DISABLE_MANAGE_SUBMODULES ]
1303 then
1304     cd ${CFG_SRC_DIR}
1305
1306     msg "git: submodule sync"
1307     "${CFG_GIT}" submodule sync
1308
1309     msg "git: submodule init"
1310     "${CFG_GIT}" submodule init
1311
1312     # Disable submodules that we're not using
1313     if [ ! -z "${CFG_LLVM_ROOT}" ]; then
1314         msg "git: submodule deinit src/llvm"
1315         "${CFG_GIT}" submodule deinit src/llvm
1316     fi
1317     if [ ! -z "${CFG_JEMALLOC_ROOT}" ]; then
1318         msg "git: submodule deinit src/jemalloc"
1319         "${CFG_GIT}" submodule deinit src/jemalloc
1320     fi
1321
1322     msg "git: submodule update"
1323     "${CFG_GIT}" submodule update
1324     need_ok "git failed"
1325
1326     msg "git: submodule foreach sync"
1327     "${CFG_GIT}" submodule foreach --recursive 'if test -e .gitmodules; then git submodule sync; fi'
1328     need_ok "git failed"
1329
1330     msg "git: submodule foreach update"
1331     "${CFG_GIT}" submodule update --recursive
1332     need_ok "git failed"
1333
1334     # NB: this is just for the sake of getting the submodule SHA1 values
1335     # and status written into the build log.
1336     msg "git: submodule status"
1337     "${CFG_GIT}" submodule status --recursive
1338
1339     msg "git: submodule clobber"
1340     "${CFG_GIT}" submodule foreach --recursive git clean -dxf
1341     need_ok "git failed"
1342     "${CFG_GIT}" submodule foreach --recursive git checkout .
1343     need_ok "git failed"
1344
1345     cd ${CFG_BUILD_DIR}
1346 fi
1347
1348 # Configure llvm, only if necessary
1349 step_msg "looking at LLVM"
1350 CFG_LLVM_SRC_DIR=${CFG_SRC_DIR}src/llvm/
1351 for t in $CFG_HOST
1352 do
1353     do_reconfigure=1
1354     is_msvc=0
1355     case "$t" in
1356         (*-msvc)
1357         is_msvc=1
1358         ;;
1359     esac
1360
1361     if [ -z $CFG_LLVM_ROOT ]
1362     then
1363         LLVM_BUILD_DIR=${CFG_BUILD_DIR}$t/llvm
1364         if [ ! -z "$CFG_DISABLE_OPTIMIZE_LLVM" ]
1365         then
1366             LLVM_DBG_OPTS="--enable-debug-symbols --disable-optimized"
1367             # Just use LLVM straight from its build directory to
1368             # avoid 'make install' time
1369             LLVM_INST_DIR=$LLVM_BUILD_DIR/Debug
1370         else
1371             LLVM_DBG_OPTS="--enable-optimized"
1372             LLVM_INST_DIR=$LLVM_BUILD_DIR/Release
1373         fi
1374         if [ -z "$CFG_ENABLE_LLVM_ASSERTIONS" ]
1375         then
1376             LLVM_ASSERTION_OPTS="--disable-assertions"
1377         else
1378             LLVM_ASSERTION_OPTS="--enable-assertions"
1379
1380             # Apparently even if we request assertions be enabled for MSVC,
1381             # LLVM's CMake build system ignore this and outputs in `Release`
1382             # anyway.
1383             if [ ${is_msvc} -eq 0 ]; then
1384                 LLVM_INST_DIR=${LLVM_INST_DIR}+Asserts
1385             fi
1386         fi
1387     else
1388         msg "not reconfiguring LLVM, external LLVM root"
1389         # The user is using their own LLVM
1390         LLVM_BUILD_DIR=
1391         LLVM_INST_DIR=$CFG_LLVM_ROOT
1392         do_reconfigure=0
1393     fi
1394
1395
1396     if [ ${do_reconfigure} -ne 0 ]
1397     then
1398     # because git is hilarious, it might have put the module index
1399     # in a couple places.
1400         index1="${CFG_SRC_DIR}.git/modules/src/llvm/index"
1401         index2="${CFG_SRC_DIR}src/llvm/.git/index"
1402         for index in ${index1} ${index2}
1403         do
1404             config_status="${LLVM_BUILD_DIR}/config.status"
1405             if test -e ${index} -a \
1406                     -e ${config_status} -a \
1407                     ${config_status} -nt ${index}
1408             then
1409                 msg "not reconfiguring LLVM, config.status is fresh"
1410                 do_reconfigure=0
1411             fi
1412         done
1413     fi
1414
1415     if [ ${do_reconfigure} -ne 0 ] && [ ${is_msvc} -ne 0 ]
1416     then
1417         msg "configuring LLVM for $t with cmake"
1418
1419         CMAKE_ARGS="-DLLVM_INCLUDE_TESTS=OFF"
1420         if [ ! -z "$CFG_DISABLE_OPTIMIZE_LLVM" ]; then
1421             CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_BUILD_TYPE=Debug"
1422         else
1423             CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release"
1424         fi
1425         if [ -z "$CFG_ENABLE_LLVM_ASSERTIONS" ]
1426         then
1427             CMAKE_ARGS="$CMAKE_ARGS -DLLVM_ENABLE_ASSERTIONS=OFF"
1428         else
1429             CMAKE_ARGS="$CMAKE_ARGS -DLLVM_ENABLE_ASSERTIONS=ON"
1430         fi
1431
1432         msg "configuring LLVM with:"
1433         msg "$CMAKE_ARGS"
1434         case "$t" in
1435             x86_64-*)
1436                 generator="Visual Studio 12 2013 Win64"
1437                 ;;
1438             i686-*)
1439                 generator="Visual Studio 12 2013"
1440                 ;;
1441             *)
1442                 err "can only build LLVM for x86 platforms"
1443                 ;;
1444         esac
1445         (cd $LLVM_BUILD_DIR && "$CFG_CMAKE" $CFG_LLVM_SRC_DIR \
1446                                             -G "$generator" \
1447                                             $CMAKE_ARGS)
1448         need_ok "LLVM cmake configure failed"
1449     fi
1450
1451     if [ ${do_reconfigure} -ne 0 ] && [ ${is_msvc} -eq 0 ]
1452     then
1453         # LLVM's configure doesn't recognize the new Windows triples yet
1454         gnu_t=$(to_gnu_triple $t)
1455
1456         msg "configuring LLVM for $gnu_t"
1457
1458         LLVM_TARGETS="--enable-targets=x86,x86_64,arm,aarch64,mips,powerpc"
1459         LLVM_BUILD="--build=$gnu_t"
1460         LLVM_HOST="--host=$gnu_t"
1461         LLVM_TARGET="--target=$gnu_t"
1462
1463         # Disable unused LLVM features
1464         LLVM_OPTS="$LLVM_DBG_OPTS $LLVM_ASSERTION_OPTS --disable-docs --enable-bindings=none"
1465         # Disable term-info, linkage of which comes in multiple forms,
1466         # making our snapshots incompatible (#9334)
1467         LLVM_OPTS="$LLVM_OPTS --disable-terminfo"
1468         # Try to have LLVM pull in as few dependencies as possible (#9397)
1469         LLVM_OPTS="$LLVM_OPTS --disable-zlib --disable-libffi"
1470
1471         # Use win32 native thread/lock apis instead of pthread wrapper.
1472         # (llvm's configure tries to find pthread first, so we have to disable it explicitly.)
1473         # Also note that pthreads works badly on mingw-w64 systems: #8996
1474         case "$CFG_BUILD" in
1475             (*-windows-gnu)
1476             LLVM_OPTS="$LLVM_OPTS --disable-pthreads"
1477             ;;
1478         esac
1479
1480         case "$CFG_CC" in
1481             ("ccache clang")
1482             LLVM_CXX_32="ccache clang++ -Qunused-arguments"
1483             LLVM_CC_32="ccache clang -Qunused-arguments"
1484
1485             LLVM_CXX_64="ccache clang++ -Qunused-arguments"
1486             LLVM_CC_64="ccache clang -Qunused-arguments"
1487             ;;
1488             ("clang")
1489             LLVM_CXX_32="clang++ -Qunused-arguments"
1490             LLVM_CC_32="clang -Qunused-arguments"
1491
1492             LLVM_CXX_64="clang++ -Qunused-arguments"
1493             LLVM_CC_64="clang -Qunused-arguments"
1494             ;;
1495             ("ccache gcc")
1496             LLVM_CXX_32="ccache g++"
1497             LLVM_CC_32="ccache gcc"
1498
1499             LLVM_CXX_64="ccache g++"
1500             LLVM_CC_64="ccache gcc"
1501             ;;
1502             ("gcc")
1503             LLVM_CXX_32="g++"
1504             LLVM_CC_32="gcc"
1505
1506             LLVM_CXX_64="g++"
1507             LLVM_CC_64="gcc"
1508             ;;
1509
1510             (*)
1511             msg "inferring LLVM_CXX/CC from CXX/CC = $CXX/$CC"
1512             LLVM_CXX_32="$CXX"
1513             LLVM_CC_32="$CC"
1514
1515             LLVM_CXX_64="$CXX"
1516             LLVM_CC_64="$CC"
1517             ;;
1518         esac
1519
1520         case "$CFG_CPUTYPE" in
1521             (x86*)
1522                 LLVM_CXX_32="$LLVM_CXX_32 -m32"
1523                 LLVM_CC_32="$LLVM_CC_32 -m32"
1524
1525                 LLVM_CFLAGS_32="-m32"
1526                 LLVM_CXXFLAGS_32="-m32"
1527                 LLVM_LDFLAGS_32="-m32"
1528
1529                 LLVM_CFLAGS_64=""
1530                 LLVM_CXXFLAGS_64=""
1531                 LLVM_LDFLAGS_64=""
1532
1533                 LLVM_CXX_32="$LLVM_CXX_32 -m32"
1534                 LLVM_CC_32="$LLVM_CC_32 -m32"
1535                 ;;
1536
1537             (*)
1538                 LLVM_CFLAGS_32=""
1539                 LLVM_CXXFLAGS_32=""
1540                 LLVM_LDFLAGS_32=""
1541
1542                 LLVM_CFLAGS_64=""
1543                 LLVM_CXXFLAGS_64=""
1544                 LLVM_LDFLAGS_64=""
1545                 ;;
1546         esac
1547
1548         if echo $t | grep -q x86_64
1549         then
1550             LLVM_CXX=$LLVM_CXX_64
1551             LLVM_CC=$LLVM_CC_64
1552             LLVM_CFLAGS=$LLVM_CFLAGS_64
1553             LLVM_CXXFLAGS=$LLVM_CXXFLAGS_64
1554             LLVM_LDFLAGS=$LLVM_LDFLAGS_64
1555         else
1556             LLVM_CXX=$LLVM_CXX_32
1557             LLVM_CC=$LLVM_CC_32
1558             LLVM_CFLAGS=$LLVM_CFLAGS_32
1559             LLVM_CXXFLAGS=$LLVM_CXXFLAGS_32
1560             LLVM_LDFLAGS=$LLVM_LDFLAGS_32
1561         fi
1562
1563         CXX=$LLVM_CXX
1564         CC=$LLVM_CC
1565         CFLAGS=$LLVM_CFLAGS
1566         CXXFLAGS=$LLVM_CXXFLAGS
1567         LDFLAGS=$LLVM_LDFLAGS
1568
1569         if [ -z "$CFG_DISABLE_LIBCPP" ] && [ -n "$CFG_USING_CLANG" ]; then
1570             LLVM_OPTS="$LLVM_OPTS --enable-libcpp"
1571         fi
1572
1573         LLVM_FLAGS="$LLVM_TARGETS $LLVM_OPTS $LLVM_BUILD \
1574                         $LLVM_HOST $LLVM_TARGET --with-python=$CFG_PYTHON"
1575
1576         msg "configuring LLVM with:"
1577         msg "$LLVM_FLAGS"
1578
1579         export CXX
1580         export CC
1581         export CFLAGS
1582         export CXXFLAGS
1583         export LDFLAGS
1584
1585         cd $LLVM_BUILD_DIR
1586         case $CFG_SRC_DIR in
1587             /* | [a-z]:* | [A-Z]:*)
1588                 ${CFG_LLVM_SRC_DIR}configure $LLVM_FLAGS
1589                 ;;
1590             *)
1591                 ${CFG_BUILD_DIR}${CFG_LLVM_SRC_DIR}configure \
1592                     $LLVM_FLAGS
1593                 ;;
1594         esac
1595         need_ok "LLVM configure failed"
1596
1597         cd $CFG_BUILD_DIR
1598     fi
1599
1600     # Construct variables for LLVM build and install directories for
1601     # each target. These will be named
1602     # CFG_LLVM_BUILD_DIR_${target_triple} but all the hyphens in
1603     # target_triple will be converted to underscore, because bash
1604     # variables can't contain hyphens. The makefile will then have to
1605     # convert back.
1606     CFG_LLVM_BUILD_DIR=$(echo CFG_LLVM_BUILD_DIR_${t} | tr - _)
1607     CFG_LLVM_INST_DIR=$(echo CFG_LLVM_INST_DIR_${t} | tr - _)
1608     eval ${CFG_LLVM_BUILD_DIR}="'$LLVM_BUILD_DIR'"
1609     eval ${CFG_LLVM_INST_DIR}="'$LLVM_INST_DIR'"
1610 done
1611
1612
1613 step_msg "writing configuration"
1614
1615 putvar CFG_SRC_DIR
1616 putvar CFG_SRC_DIR_RELATIVE
1617 putvar CFG_BUILD_DIR
1618 putvar CFG_OSTYPE
1619 putvar CFG_CPUTYPE
1620 putvar CFG_CONFIGURE_ARGS
1621 putvar CFG_PREFIX
1622 putvar CFG_HOST
1623 putvar CFG_TARGET
1624 putvar CFG_LIBDIR_RELATIVE
1625 putvar CFG_DISABLE_MANAGE_SUBMODULES
1626 putvar CFG_ANDROID_CROSS_PATH
1627 putvar CFG_MANDIR
1628
1629 # Avoid spurious warnings from clang by feeding it original source on
1630 # ccache-miss rather than preprocessed input.
1631 if [ ! -z "$CFG_ENABLE_CCACHE" ] && [ ! -z "$CFG_USING_CLANG" ]
1632 then
1633     CFG_CCACHE_CPP2=1
1634     putvar CFG_CCACHE_CPP2
1635 fi
1636
1637 if [ ! -z "$CFG_ENABLE_CCACHE" ]
1638 then
1639     CFG_CCACHE_BASEDIR=${CFG_SRC_DIR}
1640     putvar CFG_CCACHE_BASEDIR
1641 fi
1642
1643
1644 if [ ! -z $BAD_PANDOC ]
1645 then
1646     CFG_PANDOC=
1647     putvar CFG_PANDOC
1648 fi
1649
1650 putvar CFG_LLVM_SRC_DIR
1651
1652 for t in $CFG_HOST
1653 do
1654     CFG_LLVM_BUILD_DIR=$(echo CFG_LLVM_BUILD_DIR_${t} | tr - _)
1655     CFG_LLVM_INST_DIR=$(echo CFG_LLVM_INST_DIR_${t} | tr - _)
1656     putvar $CFG_LLVM_BUILD_DIR
1657     putvar $CFG_LLVM_INST_DIR
1658 done
1659
1660 msg
1661 copy_if_changed ${CFG_SRC_DIR}Makefile.in ./Makefile
1662 move_if_changed config.tmp config.mk
1663 rm -f config.tmp
1664 touch config.stamp
1665
1666 if [ -z "$CFG_ENABLE_DEBUG" ]; then
1667     step_msg "configured in release mode. for development consider --enable-debug"
1668 else
1669     step_msg "complete"
1670 fi
1671
1672 msg "run \`make help\`"
1673 msg