]> git.lizzy.rs Git - rust.git/blob - configure
Auto merge of #31254 - tmiasko:macro-pretty-print-fix, r=sfackler
[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 [ -n "$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 [ -n "$VV" ]
321     then
322         putvar $V
323     fi
324 }
325
326 enable_if_not_disabled() {
327     local OP=$1
328     local UOP=$(echo $OP | tr '[:lower:]' '[:upper:]' | tr '\-' '\_')
329     local ENAB_V="CFG_ENABLE_$UOP"
330     local EXPLICITLY_DISABLED="CFG_DISABLE_${UOP}_PROVIDED"
331     eval VV=\$$EXPLICITLY_DISABLED
332     if [ -z "$VV" ]; then
333         eval $ENAB_V=1
334     fi
335 }
336
337 to_gnu_triple() {
338     case $1 in
339         i686-pc-windows-gnu) echo i686-w64-mingw32 ;;
340         x86_64-pc-windows-gnu) echo x86_64-w64-mingw32 ;;
341         *) echo $1 ;;
342     esac
343 }
344
345 # Prints the absolute path of a directory to stdout
346 abs_path() {
347     local _path="$1"
348     # Unset CDPATH because it causes havok: it makes the destination unpredictable
349     # and triggers 'cd' to print the path to stdout. Route `cd`'s output to /dev/null
350     # for good measure.
351     (unset CDPATH && cd "$_path" > /dev/null && pwd)
352 }
353
354 msg "looking for configure programs"
355 need_cmd cmp
356 need_cmd mkdir
357 need_cmd printf
358 need_cmd cut
359 need_cmd head
360 need_cmd grep
361 need_cmd xargs
362 need_cmd cp
363 need_cmd find
364 need_cmd uname
365 need_cmd date
366 need_cmd tr
367 need_cmd sed
368 need_cmd file
369 need_cmd make
370
371 msg "inspecting environment"
372
373 CFG_OSTYPE=$(uname -s)
374 CFG_CPUTYPE=$(uname -m)
375
376 if [ $CFG_OSTYPE = Darwin -a $CFG_CPUTYPE = i386 ]
377 then
378     # Darwin's `uname -s` lies and always returns i386. We have to use sysctl
379     # instead.
380     if sysctl hw.optional.x86_64 | grep -q ': 1'
381     then
382         CFG_CPUTYPE=x86_64
383     fi
384 fi
385
386 # The goal here is to come up with the same triple as LLVM would,
387 # at least for the subset of platforms we're willing to target.
388
389 case $CFG_OSTYPE in
390
391     Linux)
392         CFG_OSTYPE=unknown-linux-gnu
393         ;;
394
395     FreeBSD)
396         CFG_OSTYPE=unknown-freebsd
397         ;;
398
399     DragonFly)
400         CFG_OSTYPE=unknown-dragonfly
401         ;;
402
403     Bitrig)
404         CFG_OSTYPE=unknown-bitrig
405         ;;
406
407     OpenBSD)
408         CFG_OSTYPE=unknown-openbsd
409        ;;
410
411     NetBSD)
412             CFG_OSTYPE=unknown-netbsd
413             ;;
414
415     Darwin)
416         CFG_OSTYPE=apple-darwin
417         ;;
418
419     MINGW*)
420         # msys' `uname` does not print gcc configuration, but prints msys
421         # configuration. so we cannot believe `uname -m`:
422         # msys1 is always i686 and msys2 is always x86_64.
423         # instead, msys defines $MSYSTEM which is MINGW32 on i686 and
424         # MINGW64 on x86_64.
425         CFG_CPUTYPE=i686
426         CFG_OSTYPE=pc-windows-gnu
427         if [ "$MSYSTEM" = MINGW64 ]
428         then
429             CFG_CPUTYPE=x86_64
430         fi
431         ;;
432
433     MSYS*)
434         CFG_OSTYPE=pc-windows-gnu
435         ;;
436
437 # Thad's Cygwin identifiers below
438
439 #   Vista 32 bit
440     CYGWIN_NT-6.0)
441         CFG_OSTYPE=pc-windows-gnu
442         CFG_CPUTYPE=i686
443         ;;
444
445 #   Vista 64 bit
446     CYGWIN_NT-6.0-WOW64)
447         CFG_OSTYPE=pc-windows-gnu
448         CFG_CPUTYPE=x86_64
449         ;;
450
451 #   Win 7 32 bit
452     CYGWIN_NT-6.1)
453         CFG_OSTYPE=pc-windows-gnu
454         CFG_CPUTYPE=i686
455         ;;
456
457 #   Win 7 64 bit
458     CYGWIN_NT-6.1-WOW64)
459         CFG_OSTYPE=pc-windows-gnu
460         CFG_CPUTYPE=x86_64
461         ;;
462
463 #   Win 8 # uname -s on 64-bit cygwin does not contain WOW64, so simply use uname -m to detect arch (works in my install)
464     CYGWIN_NT-6.3)
465         CFG_OSTYPE=pc-windows-gnu
466         ;;
467 # We do not detect other OS such as XP/2003 using 64 bit using uname.
468 # 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.
469     *)
470         err "unknown OS type: $CFG_OSTYPE"
471         ;;
472 esac
473
474
475 case $CFG_CPUTYPE in
476
477     i386 | i486 | i686 | i786 | x86)
478         CFG_CPUTYPE=i686
479         ;;
480
481     xscale | arm)
482         CFG_CPUTYPE=arm
483         ;;
484
485     armv7l)
486         CFG_CPUTYPE=arm
487         CFG_OSTYPE="${CFG_OSTYPE}eabihf"
488         ;;
489
490     aarch64)
491         CFG_CPUTYPE=aarch64
492         ;;
493
494     powerpc | ppc)
495         CFG_CPUTYPE=powerpc
496         ;;
497
498     powerpc64 | ppc64)
499         CFG_CPUTYPE=powerpc64
500         ;;
501
502     powerpc64le | ppc64le)
503         CFG_CPUTYPE=powerpc64le
504         ;;
505
506     x86_64 | x86-64 | x64 | amd64)
507         CFG_CPUTYPE=x86_64
508         ;;
509
510     *)
511         err "unknown CPU type: $CFG_CPUTYPE"
512 esac
513
514 # Detect 64 bit linux systems with 32 bit userland and force 32 bit compilation
515 if [ $CFG_OSTYPE = unknown-linux-gnu -a $CFG_CPUTYPE = x86_64 ]
516 then
517     # $SHELL does not exist in standard 'sh', so probably only exists
518     # if configure is running in an interactive bash shell. /usr/bin/env
519     # exists *everywhere*.
520     BIN_TO_PROBE="$SHELL"
521     if [ ! -r "$BIN_TO_PROBE" ]; then
522         if [ -r "/usr/bin/env" ]; then
523             BIN_TO_PROBE="/usr/bin/env"
524         else
525             warn "Cannot check if the userland is i686 or x86_64"
526         fi
527     fi
528     file -L "$BIN_TO_PROBE" | grep -q "x86[_-]64"
529     if [ $? != 0 ]; then
530         msg "i686 userland on x86_64 Linux kernel"
531         CFG_CPUTYPE=i686
532     fi
533 fi
534
535
536 DEFAULT_BUILD="${CFG_CPUTYPE}-${CFG_OSTYPE}"
537
538 CFG_SRC_DIR="$(abs_path $(dirname $0))/"
539 CFG_SRC_DIR_RELATIVE="$(dirname $0)/"
540 CFG_BUILD_DIR="$(pwd)/"
541 CFG_SELF="$0"
542 CFG_CONFIGURE_ARGS="$@"
543
544
545 case "${CFG_SRC_DIR}" in
546     *\ * )
547         err "The path to the rust source directory contains spaces, which is not supported"
548         ;;
549     *)
550         ;;
551 esac
552
553
554 OPTIONS=""
555 HELP=0
556 if [ "$1" = "--help" ]
557 then
558     HELP=1
559     shift
560     echo
561     echo "Usage: $CFG_SELF [options]"
562     echo
563     echo "Options:"
564     echo
565 else
566     msg "recreating config.tmp"
567     echo '' >config.tmp
568
569     step_msg "processing $CFG_SELF args"
570 fi
571
572 BOOL_OPTIONS=""
573 VAL_OPTIONS=""
574
575 opt debug 0 "debug mode; disables optimization unless \`--enable-optimize\` given"
576 opt valgrind 0 "run tests with valgrind (memcheck by default)"
577 opt helgrind 0 "run tests with helgrind instead of memcheck"
578 opt valgrind-rpass 1 "run rpass-valgrind tests with valgrind"
579 opt docs     1 "build standard library documentation"
580 opt compiler-docs     0 "build compiler documentation"
581 opt optimize-tests 1 "build tests with optimizations"
582 opt debuginfo-tests 0 "build tests with debugger metadata"
583 opt libcpp 1 "build with llvm with libc++ instead of libstdc++ when using clang"
584 opt llvm-assertions 0 "build LLVM with assertions"
585 opt debug-assertions 0 "build with debugging assertions"
586 opt fast-make 0 "use .gitmodules as timestamp for submodule deps"
587 opt ccache 0 "invoke gcc/clang via ccache to reuse object files between builds"
588 opt local-rust 0 "use an installed rustc rather than downloading a snapshot"
589 opt llvm-static-stdcpp 0 "statically link to libstdc++ for LLVM"
590 opt rpath 1 "build rpaths into rustc itself"
591 opt stage0-landing-pads 1 "enable landing pads during bootstrap with stage0"
592 # This is used by the automation to produce single-target nightlies
593 opt dist-host-only 0 "only install bins for the host architecture"
594 opt inject-std-version 1 "inject the current compiler version of libstd into programs"
595 opt llvm-version-check 1 "check if the LLVM version is supported, build anyway"
596
597 # Optimization and debugging options. These may be overridden by the release channel, etc.
598 opt_nosave optimize 1 "build optimized rust code"
599 opt_nosave optimize-cxx 1 "build optimized C++ code"
600 opt_nosave optimize-llvm 1 "build optimized LLVM"
601 opt_nosave llvm-assertions 0 "build LLVM with assertions"
602 opt_nosave debug-assertions 0 "build with debugging assertions"
603 opt_nosave debuginfo 0 "build with debugger metadata"
604 opt_nosave debug-jemalloc 0 "build jemalloc with --enable-debug --enable-fill"
605
606 valopt localstatedir "/var/lib" "local state directory"
607 valopt sysconfdir "/etc" "install system configuration files"
608
609 valopt datadir "${CFG_PREFIX}/share" "install data"
610 valopt infodir "${CFG_PREFIX}/share/info" "install additional info"
611 valopt llvm-root "" "set LLVM root"
612 valopt python "" "set path to python"
613 valopt jemalloc-root "" "set directory where libjemalloc_pic.a is located"
614 valopt build "${DEFAULT_BUILD}" "GNUs ./configure syntax LLVM build triple"
615 valopt android-cross-path "/opt/ndk_standalone" "Android NDK standalone path (deprecated)"
616 valopt i686-linux-android-ndk "" "i686-linux-android NDK standalone path"
617 valopt arm-linux-androideabi-ndk "" "arm-linux-androideabi NDK standalone path"
618 valopt aarch64-linux-android-ndk "" "aarch64-linux-android NDK standalone path"
619 valopt nacl-cross-path  "" "NaCl SDK path (Pepper Canary is recommended). Must be absolute!"
620 valopt release-channel "dev" "the name of the release channel to build"
621 valopt musl-root "/usr/local" "MUSL root installation directory"
622 valopt extra-filename "" "Additional data that is hashed and passed to the -C extra-filename flag"
623
624 # Used on systems where "cc" and "ar" are unavailable
625 valopt default-linker "cc" "the default linker"
626 valopt default-ar     "ar" "the default ar"
627
628 # Many of these are saved below during the "writing configuration" step
629 # (others are conditionally saved).
630 opt_nosave manage-submodules 1 "let the build manage the git submodules"
631 opt_nosave clang 0 "prefer clang to gcc for building the runtime"
632 opt_nosave jemalloc 1 "build liballoc with jemalloc"
633 opt elf-tls 1 "elf thread local storage on platforms where supported"
634
635 valopt_nosave prefix "/usr/local" "set installation prefix"
636 valopt_nosave local-rust-root "/usr/local" "set prefix for local rust binary"
637 valopt_nosave host "${CFG_BUILD}" "GNUs ./configure syntax LLVM host triples"
638 valopt_nosave target "${CFG_HOST}" "GNUs ./configure syntax LLVM target triples"
639 valopt_nosave mandir "${CFG_PREFIX}/share/man" "install man pages in PATH"
640
641 # On Windows this determines root of the subtree for target libraries.
642 # Host runtime libs always go to 'bin'.
643 valopt libdir "${CFG_PREFIX}/lib" "install libraries"
644
645 case "$CFG_LIBDIR" in
646     "$CFG_PREFIX"/*) CAT_INC=2;;
647     "$CFG_PREFIX"*)  CAT_INC=1;;
648     *)
649         err "libdir must begin with the prefix. Use --prefix to set it accordingly.";;
650 esac
651
652 CFG_LIBDIR_RELATIVE=`echo ${CFG_LIBDIR} | cut -c$((${#CFG_PREFIX}+${CAT_INC}))-`
653
654 if [ $HELP -eq 1 ]
655 then
656     echo
657     exit 0
658 fi
659
660 # Validate Options
661 step_msg "validating $CFG_SELF args"
662 validate_opt
663
664 # Validate the release channel, and configure options
665 case "$CFG_RELEASE_CHANNEL" in
666     nightly )
667         msg "overriding settings for $CFG_RELEASE_CHANNEL"
668         CFG_ENABLE_LLVM_ASSERTIONS=1
669         ;;
670     dev | beta | stable)
671         ;;
672     *)
673         err "release channel must be 'dev', 'nightly', 'beta' or 'stable'"
674         ;;
675 esac
676
677 # Adjust perf and debug options for debug mode
678 if [ -n "$CFG_ENABLE_DEBUG" ]; then
679     msg "debug mode enabled, setting performance options"
680     if [ -z "$CFG_ENABLE_OPTIMIZE_PROVIDED" ]; then
681         msg "optimization not explicitly enabled, disabling optimization"
682         CFG_DISABLE_OPTIMIZE=1
683         CFG_DISABLE_OPTIMIZE_CXX=1
684     fi
685
686     # Set following variables to 1 unless setting already provided
687     enable_if_not_disabled debug-assertions
688     enable_if_not_disabled debug-jemalloc
689     enable_if_not_disabled debuginfo
690     enable_if_not_disabled llvm-assertions
691 fi
692
693 # OK, now write the debugging options
694 if [ -n "$CFG_DISABLE_OPTIMIZE" ]; then putvar CFG_DISABLE_OPTIMIZE; fi
695 if [ -n "$CFG_DISABLE_OPTIMIZE_CXX" ]; then putvar CFG_DISABLE_OPTIMIZE_CXX; fi
696 if [ -n "$CFG_DISABLE_OPTIMIZE_LLVM" ]; then putvar CFG_DISABLE_OPTIMIZE_LLVM; fi
697 if [ -n "$CFG_ENABLE_LLVM_ASSERTIONS" ]; then putvar CFG_ENABLE_LLVM_ASSERTIONS; fi
698 if [ -n "$CFG_ENABLE_DEBUG_ASSERTIONS" ]; then putvar CFG_ENABLE_DEBUG_ASSERTIONS; fi
699 if [ -n "$CFG_ENABLE_DEBUGINFO" ]; then putvar CFG_ENABLE_DEBUGINFO; fi
700 if [ -n "$CFG_ENABLE_DEBUG_JEMALLOC" ]; then putvar CFG_ENABLE_DEBUG_JEMALLOC; fi
701
702 # A magic value that allows the compiler to use unstable features
703 # during the bootstrap even when doing so would normally be an error
704 # because of feature staging or because the build turns on
705 # warnings-as-errors and unstable features default to warnings.  The
706 # build has to match this key in an env var. Meant to be a mild
707 # deterrent from users just turning on unstable features on the stable
708 # channel.
709 # Basing CFG_BOOTSTRAP_KEY on CFG_BOOTSTRAP_KEY lets it get picked up
710 # during a Makefile reconfig.
711 CFG_BOOTSTRAP_KEY="${CFG_BOOTSTRAP_KEY-`date +%H:%M:%S`}"
712 putvar CFG_BOOTSTRAP_KEY
713
714 step_msg "looking for build programs"
715
716 probe_need CFG_CURLORWGET  curl wget
717 if [ -z "$CFG_PYTHON_PROVIDED" ]; then
718     probe_need CFG_PYTHON      python2.7 python2.6 python2 python
719 fi
720
721 python_version=$($CFG_PYTHON -V 2>&1)
722 if [ $(echo $python_version | grep -c '^Python 2\.[4567]') -ne 1 ]; then
723     err "Found $python_version, but LLVM requires Python 2.4-2.7"
724 fi
725
726 # If we have no git directory then we are probably a tarball distribution
727 # and shouldn't attempt to load submodules
728 if [ ! -e ${CFG_SRC_DIR}.git ]
729 then
730     probe CFG_GIT          git
731     msg "git: no git directory. disabling submodules"
732     CFG_DISABLE_MANAGE_SUBMODULES=1
733 else
734     probe_need CFG_GIT     git
735 fi
736
737 # Use `md5sum` on GNU platforms, or `md5 -q` on BSD
738 probe CFG_MD5              md5
739 probe CFG_MD5SUM           md5sum
740 if [ -n "$CFG_MD5" ]
741 then
742     CFG_HASH_COMMAND="$CFG_MD5 -q | cut -c 1-8"
743 elif [ -n "$CFG_MD5SUM" ]
744 then
745     CFG_HASH_COMMAND="$CFG_MD5SUM | cut -c 1-8"
746 else
747     err 'could not find one of: md5 md5sum'
748 fi
749 putvar CFG_HASH_COMMAND
750
751 probe CFG_CLANG            clang++
752 probe CFG_CCACHE           ccache
753 probe CFG_GCC              gcc
754 probe CFG_LD               ld
755 probe CFG_VALGRIND         valgrind
756 probe CFG_PERF             perf
757 probe CFG_ISCC             iscc
758 probe CFG_ANTLR4           antlr4
759 probe CFG_GRUN             grun
760 probe CFG_FLEX             flex
761 probe CFG_BISON            bison
762 probe CFG_GDB              gdb
763 probe CFG_LLDB             lldb
764
765 # On MacOS X, invoking `javac` pops up a dialog if the JDK is not
766 # installed. Since `javac` is only used if `antlr4` is available,
767 # probe for it only in this case.
768 if [ -n "$CFG_ANTLR4" ]
769 then
770    probe CFG_JAVAC            javac
771 fi
772
773 # the valgrind rpass tests will fail if you don't have a valgrind, but they're
774 # only disabled if you opt out.
775 if [ -z "$CFG_VALGRIND" ]
776 then
777     # If the user has explicitly asked for valgrind tests, then fail
778     if [ -n "$CFG_ENABLE_VALGRIND" ] && [ -n "$CFG_ENABLE_VALGRIND_PROVIDED" ]
779     then
780         err "No valgrind present, but valgrind tests explicitly requested"
781     else
782         CFG_DISABLE_VALGRIND_RPASS=1
783         putvar CFG_DISABLE_VALGRIND_RPASS
784     fi
785 fi
786
787 if [ -n "$CFG_GDB" ]
788 then
789     # Store GDB's version
790     CFG_GDB_VERSION=$($CFG_GDB --version 2>/dev/null | head -1)
791     putvar CFG_GDB_VERSION
792 fi
793
794 if [ -n "$CFG_LLDB" ]
795 then
796     # Store LLDB's version
797     CFG_LLDB_VERSION=$($CFG_LLDB --version 2>/dev/null | head -1)
798     putvar CFG_LLDB_VERSION
799
800     # If CFG_LLDB_PYTHON_DIR is not already set from the outside and valid, try to read it from
801     # LLDB via the -P commandline options.
802     if [ -z "$CFG_LLDB_PYTHON_DIR" ] || [ ! -d "$CFG_LLDB_PYTHON_DIR" ]
803     then
804         CFG_LLDB_PYTHON_DIR=$($CFG_LLDB -P)
805
806         # If CFG_LLDB_PYTHON_DIR is not a valid directory, set it to something more readable
807         if [ ! -d "$CFG_LLDB_PYTHON_DIR" ]
808         then
809             CFG_LLDB_PYTHON_DIR="LLDB_PYTHON_DIRECTORY_NOT_FOUND"
810         fi
811
812         putvar CFG_LLDB_PYTHON_DIR
813     fi
814 fi
815
816 step_msg "looking for target specific programs"
817
818 probe CFG_ADB        adb
819
820 BIN_SUF=
821 if [ "$CFG_OSTYPE" = "pc-windows-gnu" ] || [ "$CFG_OSTYPE" = "pc-windows-msvc" ]
822 then
823     BIN_SUF=.exe
824 fi
825
826 if [ -n "$CFG_ENABLE_LOCAL_RUST" ]
827 then
828     system_rustc=$(which rustc)
829     if [ -f ${CFG_LOCAL_RUST_ROOT}/bin/rustc${BIN_SUF} ]
830     then
831         : # everything already configured
832     elif [ -n "$system_rustc" ]
833     then
834         # we assume that rustc is in a /bin directory
835         CFG_LOCAL_RUST_ROOT=${system_rustc%/bin/rustc}
836     else
837         err "no local rust to use"
838     fi
839
840     CMD="${CFG_LOCAL_RUST_ROOT}/bin/rustc${BIN_SUF}"
841     LRV=`$CMD --version`
842     if [ $? -ne 0 ]
843     then
844         step_msg "failure while running $CMD --version"
845         exit 1
846     fi
847     step_msg "using rustc at: ${CFG_LOCAL_RUST_ROOT} with version: $LRV"
848     putvar CFG_LOCAL_RUST_ROOT
849 fi
850
851 # Force bitrig to build with clang; gcc doesn't like us there
852 if [ $CFG_OSTYPE = unknown-bitrig ]
853 then
854     step_msg "on Bitrig, forcing use of clang, disabling jemalloc"
855     CFG_ENABLE_CLANG=1
856     CFG_DISABLE_JEMALLOC=1
857 fi
858
859 # default gcc version under OpenBSD maybe too old, try using egcc, which is a
860 # gcc version from ports
861 if [ $CFG_OSTYPE = unknown-openbsd ]
862 then
863     if [ $("$CFG_GCC" --version 2>&1 | grep -c ' 4\.[0-6]') -ne 0 ]; then
864         step_msg "older GCC found, try with egcc instead"
865
866         # probe again but using egcc
867         probe CFG_GCC egcc
868
869         # and use egcc/eg++ for CC/CXX too if it was found
870         # (but user setting has priority)
871         if [ -n "$CFG_GCC" ]; then
872             CC="${CC:-egcc}"
873             CXX="${CXX:-eg++}"
874         fi
875     fi
876
877     step_msg "on OpenBSD, disabling jemalloc"
878     CFG_DISABLE_JEMALLOC=1
879 fi
880
881 if [ $CFG_OSTYPE = pc-windows-gnu ]
882 then
883     # FIXME(#31030) - there's not a great reason to disable jemalloc here
884     step_msg "on Windows, disabling jemalloc"
885     CFG_DISABLE_JEMALLOC=1
886 fi
887
888 # OS X 10.9, gcc is actually clang. This can cause some confusion in the build
889 # system, so if we find that gcc is clang, we should just use clang directly.
890 if [ $CFG_OSTYPE = apple-darwin -a -z "$CFG_ENABLE_CLANG" ]
891 then
892     CFG_OSX_GCC_VERSION=$("$CFG_GCC" --version 2>&1 | grep "Apple LLVM version")
893     if [ $? -eq 0 ]
894     then
895         step_msg "on OS X >=10.9, forcing use of clang"
896         CFG_ENABLE_CLANG=1
897     else
898         if [ $("$CFG_GCC" --version 2>&1 | grep -c ' 4\.[0-6]') -ne 0 ]; then
899             step_msg "older GCC found, using clang instead"
900             CFG_ENABLE_CLANG=1
901         else
902             # on OS X, with xcode 5 and newer, certain developers may have
903             # cc, gcc and g++ point to a  mixture of clang and gcc
904             # if so, this will create very strange build errors
905             # this last stanza is to detect some such problems and save the future rust
906             # contributor some time solving that issue.
907             # this detection could be generalized to other OSes aside from OS X
908             # but the issue seems most likely to happen on OS X
909
910             chk_cc () {
911                 $1 --version 2> /dev/null | grep -q $2
912             }
913             # check that gcc, cc and g++ all point to the same compiler.
914             # note that for xcode 5, g++ points to clang, not clang++
915             if !((chk_cc gcc clang  && chk_cc g++ clang) ||
916                 (chk_cc gcc gcc  &&( chk_cc g++ g++ || chk g++ gcc))); then
917                 err "the gcc and g++ in your path point to different compilers.
918     Check which versions are in your path with gcc --version and g++ --version.
919     To resolve this problem, either fix your PATH  or run configure with --enable-clang"
920             fi
921
922         fi
923     fi
924 fi
925
926 # If the clang isn't already enabled, check for GCC, and if it is missing, turn
927 # on clang as a backup.
928 if [ -z "$CFG_ENABLE_CLANG" ]
929 then
930   CFG_GCC_VERSION=$("$CFG_GCC" --version 2>&1)
931   if [ $? -ne 0 ]
932   then
933     step_msg "GCC not installed, will try using Clang"
934     CFG_ENABLE_CLANG=1
935   fi
936 fi
937
938 # Okay, at this point, we have made up our minds about whether we are
939 # going to force CFG_ENABLE_CLANG or not; save the setting if so.
940 if [ -n "$CFG_ENABLE_CLANG" ]
941 then
942     putvar CFG_ENABLE_CLANG
943 fi
944
945 if [ -z "$CFG_DISABLE_LIBCPP" -a -n "$CFG_ENABLE_CLANG" ]
946 then
947     CFG_USING_LIBCPP="1"
948 else
949     CFG_USING_LIBCPP="0"
950 fi
951
952 # Same with jemalloc.  save the setting here.
953 if [ -n "$CFG_DISABLE_JEMALLOC" ]
954 then
955     putvar CFG_DISABLE_JEMALLOC
956 fi
957
958 if [ -n "$CFG_LLVM_ROOT" -a -z "$CFG_DISABLE_LLVM_VERSION_CHECK" -a -e "$CFG_LLVM_ROOT/bin/llvm-config" ]
959 then
960     step_msg "using custom LLVM at $CFG_LLVM_ROOT"
961
962     LLVM_CONFIG="$CFG_LLVM_ROOT/bin/llvm-config"
963     LLVM_VERSION=$($LLVM_CONFIG --version)
964
965     case $LLVM_VERSION in
966         (3.[5-8]*)
967             msg "found ok version of LLVM: $LLVM_VERSION"
968             ;;
969         (*)
970             err "bad LLVM version: $LLVM_VERSION, need >=3.5"
971             ;;
972     esac
973 fi
974
975 # Even when the user overrides the choice of CC, still try to detect
976 # clang to disable some clang-specific warnings.  We here draw a
977 # distinction between:
978 #
979 #  CFG_ENABLE_CLANG : passed --enable-clang, or host "requires" clang,
980 #  CFG_USING_CLANG : compiler (clang / gcc / $CC) looks like clang.
981 #
982 # This distinction is important because there are some safeguards we
983 # would prefer to skip when merely CFG_USING_CLANG is set; but when
984 # CFG_ENABLE_CLANG is set, that indicates that we are opting into
985 # running such safeguards.
986
987 if [ -n "$CC" ]
988 then
989     msg "skipping compiler inference steps; using provided CC=$CC"
990     CFG_CC="$CC"
991
992     CFG_OSX_CC_VERSION=$("$CFG_CC" --version 2>&1 | grep "clang")
993     if [ $? -eq 0 ]
994     then
995         step_msg "note, user-provided CC looks like clang; CC=$CC."
996         CFG_USING_CLANG=1
997         putvar CFG_USING_CLANG
998     fi
999 else
1000     if [ -n "$CFG_ENABLE_CLANG" ]
1001     then
1002         if [ -z "$CFG_CLANG" ]
1003         then
1004             err "clang requested but not found"
1005         fi
1006         CFG_CC="$CFG_CLANG"
1007         CFG_USING_CLANG=1
1008         putvar CFG_USING_CLANG
1009     else
1010         CFG_CC="gcc"
1011     fi
1012 fi
1013
1014 if [ -n "$CFG_ENABLE_CLANG" ]
1015 then
1016     case "$CC" in
1017         (''|*clang)
1018         CFG_CLANG_REPORTED_VERSION=$($CFG_CC --version | grep version)
1019
1020         if echo $CFG_CLANG_REPORTED_VERSION | grep -q "(based on LLVM "; then
1021             CFG_CLANG_VERSION=$(echo $CFG_CLANG_REPORTED_VERSION | sed 's/.*(based on LLVM \(.*\))/\1/')
1022         elif echo $CFG_CLANG_REPORTED_VERSION | grep -q "Apple LLVM"; then
1023             CFG_OSX_CLANG_VERSION=$(echo $CFG_CLANG_REPORTED_VERSION | sed 's/.*version \(.*\) .*/\1/')
1024         else
1025             CFG_CLANG_VERSION=$(echo $CFG_CLANG_REPORTED_VERSION | sed 's/.*version \(.*\) .*/\1/')
1026         fi
1027
1028         if [ -n "$CFG_OSX_CLANG_VERSION" ]
1029         then
1030             case $CFG_OSX_CLANG_VERSION in
1031                 (7.0* | 7.1* | 7.2*)
1032                 step_msg "found ok version of APPLE CLANG: $CFG_OSX_CLANG_VERSION"
1033                 ;;
1034                 (*)
1035                 err "bad APPLE CLANG version: $CFG_OSX_CLANG_VERSION, need >=7.0"
1036                 ;;
1037             esac
1038         else
1039             case $CFG_CLANG_VERSION in
1040                 (3.2* | 3.3* | 3.4* | 3.5* | 3.6* | 3.7* | 3.8* | 3.9*)
1041                 step_msg "found ok version of CLANG: $CFG_CLANG_VERSION"
1042                 ;;
1043                 (*)
1044                 err "bad CLANG version: $CFG_CLANG_VERSION, need >=3.0svn"
1045                 ;;
1046             esac
1047         fi
1048
1049         if [ -z "$CC" ]
1050         then
1051             CFG_CC="clang"
1052             CFG_CXX="clang++"
1053         fi
1054     esac
1055 fi
1056
1057 if [ -n "$CFG_ENABLE_CCACHE" ]
1058 then
1059     if [ -z "$CFG_CCACHE" ]
1060     then
1061         err "ccache requested but not found"
1062     fi
1063
1064     CFG_CC="ccache $CFG_CC"
1065 fi
1066
1067 if [ -z "$CC" -a -z "$CFG_ENABLE_CLANG" -a -z "$CFG_GCC" ]
1068 then
1069     err "either clang or gcc is required"
1070 fi
1071
1072 # All safeguards based on $CFG_ENABLE_CLANG should occur before this
1073 # point in the script; after this point, script logic should inspect
1074 # $CFG_USING_CLANG rather than $CFG_ENABLE_CLANG.
1075
1076 # Set CFG_{CC,CXX,CPP,CFLAGS,CXXFLAGS,LDFLAGS}
1077 envopt CC
1078 envopt CXX
1079 envopt CPP
1080 envopt CFLAGS
1081 envopt CXXFLAGS
1082 envopt LDFLAGS
1083
1084 # stdc++ name in use
1085 # used to manage non-standard name (on OpenBSD for example)
1086 program_transform_name=$($CFG_CC -v 2>&1 | sed -n "s/.*--program-transform-name='\([^']*\)'.*/\1/p")
1087 CFG_STDCPP_NAME=$(echo "stdc++" | sed "${program_transform_name}")
1088 putvar CFG_STDCPP_NAME
1089
1090 # a little post-processing of various config values
1091 CFG_PREFIX=${CFG_PREFIX%/}
1092 CFG_MANDIR=${CFG_MANDIR%/}
1093 CFG_HOST="$(echo $CFG_HOST | tr ',' ' ')"
1094 CFG_TARGET="$(echo $CFG_TARGET | tr ',' ' ')"
1095 CFG_SUPPORTED_TARGET=""
1096 for target_file in ${CFG_SRC_DIR}mk/cfg/*.mk; do
1097   CFG_SUPPORTED_TARGET="${CFG_SUPPORTED_TARGET} $(basename "$target_file" .mk)"
1098 done
1099
1100 # copy build-triples to host-triples so that builds are a subset of hosts
1101 V_TEMP=""
1102 for i in $CFG_BUILD $CFG_HOST;
1103 do
1104    echo "$V_TEMP" | grep -qF $i || V_TEMP="$V_TEMP${V_TEMP:+ }$i"
1105 done
1106 CFG_HOST=$V_TEMP
1107
1108 # copy host-triples to target-triples so that hosts are a subset of targets
1109 V_TEMP=""
1110 for i in $CFG_HOST $CFG_TARGET;
1111 do
1112    echo "$V_TEMP" | grep -qF $i || V_TEMP="$V_TEMP${V_TEMP:+ }$i"
1113 done
1114 CFG_TARGET=$V_TEMP
1115
1116 # check target-specific tool-chains
1117 for i in $CFG_TARGET
1118 do
1119     L_CHECK=false
1120     for j in $CFG_SUPPORTED_TARGET
1121     do
1122         if [ $i = $j ]
1123         then
1124             L_CHECK=true
1125         fi
1126     done
1127
1128     if [ $L_CHECK = false ]
1129     then
1130         err "unsupported target triples \"$i\" found"
1131     fi
1132
1133     case $i in
1134         *android*)
1135             upper_snake_target=$(echo "$i" | tr '[:lower:]' '[:upper:]' | tr '\-' '\_')
1136             eval ndk=\$"CFG_${upper_snake_target}_NDK"
1137             if [ -z "$ndk" ]
1138             then
1139                 ndk=$CFG_ANDROID_CROSS_PATH
1140                 eval "CFG_${upper_snake_target}_NDK"=$CFG_ANDROID_CROSS_PATH
1141                 warn "generic/default Android NDK option is deprecated (use --$i-ndk option instead)"
1142             fi
1143
1144             # Perform a basic sanity check of the NDK
1145             for android_ndk_tool in "$ndk/bin/$i-gcc" "$ndk/bin/$i-g++" "$ndk/bin/$i-ar"
1146             do
1147                 if [ ! -f $android_ndk_tool ]
1148                 then
1149                     err "NDK tool $android_ndk_tool not found (bad or missing --$i-ndk option?)"
1150                 fi
1151             done
1152             ;;
1153         *-unknown-nacl)
1154             if [ -z "$CFG_NACL_CROSS_PATH" ]
1155             then
1156                 err "I need the NaCl SDK path! (use --nacl-cross-path)"
1157             fi
1158             ;;
1159         arm-apple-darwin)
1160             if [ $CFG_OSTYPE != apple-darwin ]
1161             then
1162                 err "The iOS target is only supported on Mac OS X"
1163             fi
1164             ;;
1165
1166
1167         x86_64-*-musl)
1168             if [ ! -f $CFG_MUSL_ROOT/lib/libc.a ]
1169             then
1170                 err "musl libc $CFG_MUSL_ROOT/lib/libc.a not found"
1171             fi
1172             ;;
1173
1174         *-msvc)
1175             # Currently the build system is not configured to build jemalloc
1176             # with MSVC, so we omit this optional dependency.
1177             step_msg "targeting MSVC, disabling jemalloc"
1178             CFG_DISABLE_JEMALLOC=1
1179             putvar CFG_DISABLE_JEMALLOC
1180
1181             # There are some MSYS python builds which will auto-translate
1182             # windows-style paths to MSYS-style paths in Python itself.
1183             # Unfortunately this breaks LLVM's build system as somewhere along
1184             # the line LLVM prints a path into a file from Python and then CMake
1185             # later tries to interpret that path. If Python prints a MSYS path
1186             # and CMake tries to use it as a Windows path, you're gonna have a
1187             # Bad Time.
1188             #
1189             # Consequently here we try to detect when that happens and print an
1190             # error if it does.
1191             if $CFG_PYTHON -c 'import sys; print sys.argv[1]' `pwd` | grep '^/' > /dev/null
1192             then
1193                 err "
1194
1195 python is silently translating windows paths to MSYS paths \
1196 and the build will fail if this python is used.
1197
1198 Either an official python install must be used or an \
1199 alternative python package in MinGW must be used.
1200
1201 If you are building under msys2 try installing the mingw-w64-x86_64-python2 \
1202 package instead of python2:
1203
1204 $ pacman -R python2 && pacman -S mingw-w64-x86_64-python2
1205 "
1206             fi
1207
1208             # MSVC requires cmake because that's how we're going to build LLVM
1209             probe_need CFG_CMAKE cmake
1210
1211             # There are three builds of cmake on windows: MSVC, MinGW and Cygwin
1212             # The Cygwin build does not have generators for Visual Studio, so
1213             # detect that here and error.
1214             if ! "$CFG_CMAKE" --help | sed -n '/^Generators/,$p' | grep 'Visual Studio' > /dev/null
1215             then
1216                 err "
1217
1218 cmake does not support Visual Studio generators.
1219
1220 This is likely due to it being an msys/cygwin build of cmake, \
1221 rather than the required windows version, built using MinGW \
1222 or Visual Studio.
1223
1224 If you are building under msys2 try installing the mingw-w64-x86_64-cmake \
1225 package instead of cmake:
1226
1227 $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
1228 "
1229             fi
1230
1231             # Use the REG program to figure out where VS is installed
1232             # We need to figure out where cl.exe and link.exe are, so we do some
1233             # munging and some probing here. We also look for the default
1234             # INCLUDE and LIB variables for MSVC so we can set those in the
1235             # build system as well.
1236             install=$(cmd //c reg QUERY \
1237                        'HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0' \
1238                        -v InstallDir)
1239             if [ -z "$install" ]; then
1240               install=$(cmd //c reg QUERY \
1241                          'HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\12.0' \
1242                          -v InstallDir)
1243             fi
1244             need_ok "couldn't find visual studio install root"
1245             CFG_MSVC_ROOT=$(echo "$install" | grep InstallDir | sed 's/.*REG_SZ[ ]*//')
1246             CFG_MSVC_ROOT=$(dirname "$CFG_MSVC_ROOT")
1247             CFG_MSVC_ROOT=$(dirname "$CFG_MSVC_ROOT")
1248             putvar CFG_MSVC_ROOT
1249
1250             case $i in
1251                 x86_64-*)
1252                     bits=x86_64
1253                     msvc_part=amd64
1254                     ;;
1255                 i686-*)
1256                     bits=i386
1257                     msvc_part=
1258                     ;;
1259                 *)
1260                     err "can only target x86 targets for MSVC"
1261                     ;;
1262             esac
1263             bindir="${CFG_MSVC_ROOT}/VC/bin"
1264             if [ -n "$msvc_part" ]; then
1265                 bindir="$bindir/$msvc_part"
1266             fi
1267             eval CFG_MSVC_BINDIR_$bits="\"$bindir\""
1268             eval CFG_MSVC_CL_$bits="\"$bindir/cl.exe\""
1269             eval CFG_MSVC_LIB_$bits="\"$bindir/lib.exe\""
1270             eval CFG_MSVC_LINK_$bits="\"$bindir/link.exe\""
1271
1272             vcvarsall="${CFG_MSVC_ROOT}/VC/vcvarsall.bat"
1273             include_path=$(cmd //V:ON //c "$vcvarsall" $msvc_part \& echo !INCLUDE!)
1274             need_ok "failed to learn about MSVC's INCLUDE"
1275             lib_path=$(cmd //V:ON //c "$vcvarsall" $msvc_part \& echo !LIB!)
1276             need_ok "failed to learn about MSVC's LIB"
1277
1278             eval CFG_MSVC_INCLUDE_PATH_${bits}="\"$include_path\""
1279             eval CFG_MSVC_LIB_PATH_${bits}="\"$lib_path\""
1280
1281             putvar CFG_MSVC_BINDIR_${bits}
1282             putvar CFG_MSVC_CL_${bits}
1283             putvar CFG_MSVC_LIB_${bits}
1284             putvar CFG_MSVC_LINK_${bits}
1285             putvar CFG_MSVC_INCLUDE_PATH_${bits}
1286             putvar CFG_MSVC_LIB_PATH_${bits}
1287             ;;
1288
1289         *-rumprun-netbsd)
1290             step_msg "targeting rumprun-netbsd, disabling jemalloc"
1291             CFG_DISABLE_JEMALLOC=1
1292             putvar CFG_DISABLE_JEMALLOC
1293             ;;
1294
1295         *)
1296             ;;
1297     esac
1298 done
1299
1300 if [ -n "$CFG_PERF" ]
1301 then
1302     HAVE_PERF_LOGFD=`$CFG_PERF stat --log-fd 2>&1 | grep 'unknown option'`
1303     if [ -z "$HAVE_PERF_LOGFD" ];
1304     then
1305         CFG_PERF_WITH_LOGFD=1
1306         putvar CFG_PERF_WITH_LOGFD
1307     fi
1308 fi
1309
1310 step_msg "making directories"
1311
1312 for i in \
1313     doc doc/std doc/extra \
1314     dl tmp dist
1315 do
1316     make_dir $i
1317 done
1318
1319 for t in $CFG_HOST
1320 do
1321     make_dir $t/llvm
1322 done
1323
1324 for t in $CFG_HOST
1325 do
1326     make_dir $t/rustllvm
1327 done
1328
1329 for t in $CFG_TARGET
1330 do
1331   make_dir $t/rt
1332   for s in 0 1 2 3
1333   do
1334     make_dir $t/rt/stage$s
1335     make_dir $t/rt/jemalloc
1336     make_dir $t/rt/compiler-rt
1337     for i in                                          \
1338       isaac sync test \
1339       arch/i386 arch/x86_64 arch/arm arch/aarch64 arch/mips arch/powerpc
1340     do
1341       make_dir $t/rt/stage$s/$i
1342     done
1343   done
1344 done
1345
1346 for h in $CFG_HOST
1347 do
1348     for t in $CFG_TARGET
1349     do
1350         # host bin dir stage0
1351         make_dir $h/stage0/bin
1352
1353         # host lib dir stage0
1354         make_dir $h/stage0/lib
1355
1356         # host test dir stage0
1357         make_dir $h/stage0/test
1358
1359         # target bin dir stage0
1360         make_dir $h/stage0/lib/rustlib/$t/bin
1361
1362         # target lib dir stage0
1363         make_dir $h/stage0/lib/rustlib/$t/lib
1364
1365         for i in 1 2 3
1366         do
1367             # host bin dir
1368             make_dir $h/stage$i/bin
1369
1370             # host lib dir
1371             make_dir $h/stage$i/$CFG_LIBDIR_RELATIVE
1372
1373             # host test dir
1374             make_dir $h/stage$i/test
1375
1376             # target bin dir
1377             make_dir $h/stage$i/$CFG_LIBDIR_RELATIVE/rustlib/$t/bin
1378
1379             # target lib dir
1380             make_dir $h/stage$i/$CFG_LIBDIR_RELATIVE/rustlib/$t/lib
1381         done
1382     done
1383
1384     make_dir $h/test/run-pass
1385     make_dir $h/test/run-pass-valgrind
1386     make_dir $h/test/run-pass-fulldeps
1387     make_dir $h/test/run-fail
1388     make_dir $h/test/run-fail-fulldeps
1389     make_dir $h/test/compile-fail
1390     make_dir $h/test/parse-fail
1391     make_dir $h/test/compile-fail-fulldeps
1392     make_dir $h/test/bench
1393     make_dir $h/test/perf
1394     make_dir $h/test/pretty
1395     make_dir $h/test/debuginfo-gdb
1396     make_dir $h/test/debuginfo-lldb
1397     make_dir $h/test/codegen
1398     make_dir $h/test/codegen-units
1399     make_dir $h/test/rustdoc
1400 done
1401
1402 # Configure submodules
1403 step_msg "configuring submodules"
1404
1405 # Have to be in the top of src directory for this
1406 if [ -z $CFG_DISABLE_MANAGE_SUBMODULES ]
1407 then
1408     cd ${CFG_SRC_DIR}
1409
1410     msg "git: submodule sync"
1411     "${CFG_GIT}" submodule sync
1412
1413     msg "git: submodule init"
1414     "${CFG_GIT}" submodule init
1415
1416     # Disable submodules that we're not using
1417     if [ -n "${CFG_LLVM_ROOT}" ]; then
1418         msg "git: submodule deinit src/llvm"
1419         "${CFG_GIT}" submodule deinit src/llvm
1420     fi
1421     if [ -n "${CFG_JEMALLOC_ROOT}" ]; then
1422         msg "git: submodule deinit src/jemalloc"
1423         "${CFG_GIT}" submodule deinit src/jemalloc
1424     fi
1425
1426     msg "git: submodule update"
1427     "${CFG_GIT}" submodule update
1428     need_ok "git failed"
1429
1430     msg "git: submodule foreach sync"
1431     "${CFG_GIT}" submodule foreach --recursive 'if test -e .gitmodules; then git submodule sync; fi'
1432     need_ok "git failed"
1433
1434     msg "git: submodule foreach update"
1435     "${CFG_GIT}" submodule update --recursive
1436     need_ok "git failed"
1437
1438     # NB: this is just for the sake of getting the submodule SHA1 values
1439     # and status written into the build log.
1440     msg "git: submodule status"
1441     "${CFG_GIT}" submodule status --recursive
1442
1443     msg "git: submodule clobber"
1444     "${CFG_GIT}" submodule foreach --recursive git clean -dxf
1445     need_ok "git failed"
1446     "${CFG_GIT}" submodule foreach --recursive git checkout .
1447     need_ok "git failed"
1448
1449     cd ${CFG_BUILD_DIR}
1450 fi
1451
1452 # Configure llvm, only if necessary
1453 step_msg "looking at LLVM"
1454 CFG_LLVM_SRC_DIR=${CFG_SRC_DIR}src/llvm/
1455 for t in $CFG_HOST
1456 do
1457     do_reconfigure=1
1458     is_msvc=0
1459     case "$t" in
1460         (*-msvc)
1461         is_msvc=1
1462         ;;
1463     esac
1464
1465     if [ -z $CFG_LLVM_ROOT ]
1466     then
1467         LLVM_BUILD_DIR=${CFG_BUILD_DIR}$t/llvm
1468         if [ -n "$CFG_DISABLE_OPTIMIZE_LLVM" ]
1469         then
1470             LLVM_DBG_OPTS="--enable-debug-symbols --disable-optimized"
1471             # Just use LLVM straight from its build directory to
1472             # avoid 'make install' time
1473             LLVM_INST_DIR=$LLVM_BUILD_DIR/Debug
1474         else
1475             LLVM_DBG_OPTS="--enable-optimized"
1476             LLVM_INST_DIR=$LLVM_BUILD_DIR/Release
1477         fi
1478         if [ -z "$CFG_ENABLE_LLVM_ASSERTIONS" ]
1479         then
1480             LLVM_ASSERTION_OPTS="--disable-assertions"
1481         else
1482             LLVM_ASSERTION_OPTS="--enable-assertions"
1483
1484             # Apparently even if we request assertions be enabled for MSVC,
1485             # LLVM's CMake build system ignore this and outputs in `Release`
1486             # anyway.
1487             if [ ${is_msvc} -eq 0 ]; then
1488                 LLVM_INST_DIR=${LLVM_INST_DIR}+Asserts
1489             fi
1490         fi
1491     else
1492         msg "not reconfiguring LLVM, external LLVM root"
1493         # The user is using their own LLVM
1494         LLVM_BUILD_DIR=
1495         LLVM_INST_DIR=$CFG_LLVM_ROOT
1496         do_reconfigure=0
1497         # Check that LLVm FileCheck is available. Needed for the tests
1498         need_cmd $LLVM_INST_DIR/bin/FileCheck
1499     fi
1500
1501     if [ ${do_reconfigure} -ne 0 ]
1502     then
1503     # because git is hilarious, it might have put the module index
1504     # in a couple places.
1505         index1="${CFG_SRC_DIR}.git/modules/src/llvm/index"
1506         index2="${CFG_SRC_DIR}src/llvm/.git/index"
1507         for index in ${index1} ${index2}
1508         do
1509             config_status="${LLVM_BUILD_DIR}/config.status"
1510             if test -e ${index} -a \
1511                     -e ${config_status} -a \
1512                     ${config_status} -nt ${index}
1513             then
1514                 msg "not reconfiguring LLVM, config.status is fresh"
1515                 do_reconfigure=0
1516             fi
1517         done
1518     fi
1519
1520     # We need the generator later on for compiler-rt even if LLVM's not built
1521     if [ ${is_msvc} -ne 0 ]
1522     then
1523         case "$CFG_MSVC_ROOT" in
1524             *14.0*)
1525                 generator="Visual Studio 14 2015"
1526                 ;;
1527             *12.0*)
1528                 generator="Visual Studio 12 2013"
1529                 ;;
1530             *)
1531                 err "can't determine generator for LLVM cmake"
1532                 ;;
1533         esac
1534         case "$t" in
1535             x86_64-*)
1536                 generator="$generator Win64"
1537                 ;;
1538             i686-*)
1539                 ;;
1540             *)
1541                 err "can only build LLVM for x86 platforms"
1542                 ;;
1543         esac
1544         CFG_CMAKE_GENERATOR=$generator
1545         putvar CFG_CMAKE_GENERATOR
1546     fi
1547
1548     if [ ${do_reconfigure} -ne 0 ] && [ ${is_msvc} -ne 0 ]
1549     then
1550         msg "configuring LLVM for $t with cmake"
1551
1552         CMAKE_ARGS="-DLLVM_INCLUDE_TESTS=OFF"
1553         if [ -n "$CFG_DISABLE_OPTIMIZE_LLVM" ]; then
1554             CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_BUILD_TYPE=Debug"
1555         else
1556             CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release"
1557         fi
1558         if [ -z "$CFG_ENABLE_LLVM_ASSERTIONS" ]
1559         then
1560             CMAKE_ARGS="$CMAKE_ARGS -DLLVM_ENABLE_ASSERTIONS=OFF"
1561         else
1562             CMAKE_ARGS="$CMAKE_ARGS -DLLVM_ENABLE_ASSERTIONS=ON"
1563         fi
1564
1565         msg "configuring LLVM with:"
1566         msg "$CMAKE_ARGS"
1567
1568         (cd $LLVM_BUILD_DIR && "$CFG_CMAKE" $CFG_LLVM_SRC_DIR \
1569                                             -G "$CFG_CMAKE_GENERATOR" \
1570                                             $CMAKE_ARGS)
1571         need_ok "LLVM cmake configure failed"
1572     fi
1573
1574     if [ ${do_reconfigure} -ne 0 ] && [ ${is_msvc} -eq 0 ]
1575     then
1576         # LLVM's configure doesn't recognize the new Windows triples yet
1577         gnu_t=$(to_gnu_triple $t)
1578
1579         msg "configuring LLVM for $gnu_t"
1580
1581         LLVM_TARGETS="--enable-targets=x86,x86_64,arm,aarch64,mips,powerpc"
1582         LLVM_BUILD="--build=$gnu_t"
1583         LLVM_HOST="--host=$gnu_t"
1584         LLVM_TARGET="--target=$gnu_t"
1585
1586         # Disable unused LLVM features
1587         LLVM_OPTS="$LLVM_DBG_OPTS $LLVM_ASSERTION_OPTS --disable-docs --enable-bindings=none"
1588         # Disable term-info, linkage of which comes in multiple forms,
1589         # making our snapshots incompatible (#9334)
1590         LLVM_OPTS="$LLVM_OPTS --disable-terminfo"
1591         # Try to have LLVM pull in as few dependencies as possible (#9397)
1592         LLVM_OPTS="$LLVM_OPTS --disable-zlib --disable-libffi"
1593
1594         # Use win32 native thread/lock apis instead of pthread wrapper.
1595         # (llvm's configure tries to find pthread first, so we have to disable it explicitly.)
1596         # Also note that pthreads works badly on mingw-w64 systems: #8996
1597         case "$CFG_BUILD" in
1598             (*-windows-gnu)
1599             LLVM_OPTS="$LLVM_OPTS --disable-pthreads"
1600             ;;
1601         esac
1602
1603         case "$CFG_CC" in
1604             ("ccache clang")
1605             LLVM_CXX_32="ccache clang++ -Qunused-arguments"
1606             LLVM_CC_32="ccache clang -Qunused-arguments"
1607
1608             LLVM_CXX_64="ccache clang++ -Qunused-arguments"
1609             LLVM_CC_64="ccache clang -Qunused-arguments"
1610             ;;
1611             ("clang")
1612             LLVM_CXX_32="clang++ -Qunused-arguments"
1613             LLVM_CC_32="clang -Qunused-arguments"
1614
1615             LLVM_CXX_64="clang++ -Qunused-arguments"
1616             LLVM_CC_64="clang -Qunused-arguments"
1617             ;;
1618             ("ccache gcc")
1619             LLVM_CXX_32="ccache g++"
1620             LLVM_CC_32="ccache gcc"
1621
1622             LLVM_CXX_64="ccache g++"
1623             LLVM_CC_64="ccache gcc"
1624             ;;
1625             ("gcc")
1626             LLVM_CXX_32="g++"
1627             LLVM_CC_32="gcc"
1628
1629             LLVM_CXX_64="g++"
1630             LLVM_CC_64="gcc"
1631             ;;
1632
1633             (*)
1634             msg "inferring LLVM_CXX/CC from CXX/CC = $CXX/$CC"
1635             if [ -n "$CFG_ENABLE_CCACHE" ]
1636             then
1637                 if [ -z "$CFG_CCACHE" ]
1638                 then
1639                     err "ccache requested but not found"
1640                 fi
1641
1642                 LLVM_CXX_32="ccache $CXX"
1643                 LLVM_CC_32="ccache $CC"
1644
1645                 LLVM_CXX_64="ccache $CXX"
1646                 LLVM_CC_64="ccache $CC"
1647             else
1648                 LLVM_CXX_32="$CXX"
1649                 LLVM_CC_32="$CC"
1650
1651                 LLVM_CXX_64="$CXX"
1652                 LLVM_CC_64="$CC"
1653             fi
1654
1655             ;;
1656         esac
1657
1658         case "$CFG_CPUTYPE" in
1659             (x86*)
1660                 LLVM_CXX_32="$LLVM_CXX_32 -m32"
1661                 LLVM_CC_32="$LLVM_CC_32 -m32"
1662
1663                 LLVM_CFLAGS_32="-m32"
1664                 LLVM_CXXFLAGS_32="-m32"
1665                 LLVM_LDFLAGS_32="-m32"
1666
1667                 LLVM_CFLAGS_64=""
1668                 LLVM_CXXFLAGS_64=""
1669                 LLVM_LDFLAGS_64=""
1670
1671                 LLVM_CXX_32="$LLVM_CXX_32 -m32"
1672                 LLVM_CC_32="$LLVM_CC_32 -m32"
1673                 ;;
1674
1675             (*)
1676                 LLVM_CFLAGS_32=""
1677                 LLVM_CXXFLAGS_32=""
1678                 LLVM_LDFLAGS_32=""
1679
1680                 LLVM_CFLAGS_64=""
1681                 LLVM_CXXFLAGS_64=""
1682                 LLVM_LDFLAGS_64=""
1683                 ;;
1684         esac
1685
1686         if echo $t | grep -q x86_64
1687         then
1688             LLVM_CXX=$LLVM_CXX_64
1689             LLVM_CC=$LLVM_CC_64
1690             LLVM_CFLAGS=$LLVM_CFLAGS_64
1691             LLVM_CXXFLAGS=$LLVM_CXXFLAGS_64
1692             LLVM_LDFLAGS=$LLVM_LDFLAGS_64
1693         else
1694             LLVM_CXX=$LLVM_CXX_32
1695             LLVM_CC=$LLVM_CC_32
1696             LLVM_CFLAGS=$LLVM_CFLAGS_32
1697             LLVM_CXXFLAGS=$LLVM_CXXFLAGS_32
1698             LLVM_LDFLAGS=$LLVM_LDFLAGS_32
1699         fi
1700
1701         CXX=$LLVM_CXX
1702         CC=$LLVM_CC
1703         CFLAGS="$CFLAGS $LLVM_CFLAGS"
1704         CXXFLAGS="$CXXFLAGS $LLVM_CXXFLAGS"
1705         LDFLAGS="$LDFLAGS $LLVM_LDFLAGS"
1706
1707         if [ "$CFG_USING_LIBCPP" != "0" ]; then
1708             LLVM_OPTS="$LLVM_OPTS --enable-libcpp"
1709         fi
1710
1711         LLVM_FLAGS="$LLVM_TARGETS $LLVM_OPTS $LLVM_BUILD \
1712                         $LLVM_HOST $LLVM_TARGET --with-python=$CFG_PYTHON"
1713
1714         msg "configuring LLVM with:"
1715         msg "$LLVM_FLAGS"
1716
1717         export CXX
1718         export CC
1719         export CFLAGS
1720         export CXXFLAGS
1721         export LDFLAGS
1722
1723         cd $LLVM_BUILD_DIR
1724         case $CFG_SRC_DIR in
1725             /* | [a-z]:* | [A-Z]:*)
1726                 ${CFG_LLVM_SRC_DIR}configure $LLVM_FLAGS
1727                 ;;
1728             *)
1729                 ${CFG_BUILD_DIR}${CFG_LLVM_SRC_DIR}configure \
1730                     $LLVM_FLAGS
1731                 ;;
1732         esac
1733         need_ok "LLVM configure failed"
1734
1735         cd $CFG_BUILD_DIR
1736     fi
1737
1738     # Construct variables for LLVM build and install directories for
1739     # each target. These will be named
1740     # CFG_LLVM_BUILD_DIR_${target_triple} but all the hyphens in
1741     # target_triple will be converted to underscore, because bash
1742     # variables can't contain hyphens. The makefile will then have to
1743     # convert back.
1744     CFG_LLVM_BUILD_DIR=$(echo CFG_LLVM_BUILD_DIR_${t} | tr - _)
1745     CFG_LLVM_INST_DIR=$(echo CFG_LLVM_INST_DIR_${t} | tr - _)
1746     eval ${CFG_LLVM_BUILD_DIR}="'$LLVM_BUILD_DIR'"
1747     eval ${CFG_LLVM_INST_DIR}="'$LLVM_INST_DIR'"
1748 done
1749
1750
1751 step_msg "writing configuration"
1752
1753 putvar CFG_SRC_DIR
1754 putvar CFG_SRC_DIR_RELATIVE
1755 putvar CFG_BUILD_DIR
1756 putvar CFG_OSTYPE
1757 putvar CFG_CPUTYPE
1758 putvar CFG_CONFIGURE_ARGS
1759 putvar CFG_PREFIX
1760 putvar CFG_HOST
1761 putvar CFG_TARGET
1762 putvar CFG_LIBDIR_RELATIVE
1763 putvar CFG_DISABLE_MANAGE_SUBMODULES
1764 putvar CFG_AARCH64_LINUX_ANDROID_NDK
1765 putvar CFG_ARM_LINUX_ANDROIDEABI_NDK
1766 putvar CFG_I686_LINUX_ANDROID_NDK
1767 putvar CFG_NACL_CROSS_PATH
1768 putvar CFG_MANDIR
1769 putvar CFG_USING_LIBCPP
1770
1771 # Avoid spurious warnings from clang by feeding it original source on
1772 # ccache-miss rather than preprocessed input.
1773 if [ -n "$CFG_ENABLE_CCACHE" ] && [ -n "$CFG_USING_CLANG" ]
1774 then
1775     CFG_CCACHE_CPP2=1
1776     putvar CFG_CCACHE_CPP2
1777 fi
1778
1779 if [ -n "$CFG_ENABLE_CCACHE" ]
1780 then
1781     CFG_CCACHE_BASEDIR=${CFG_SRC_DIR}
1782     putvar CFG_CCACHE_BASEDIR
1783 fi
1784
1785
1786 putvar CFG_LLVM_SRC_DIR
1787
1788 for t in $CFG_HOST
1789 do
1790     CFG_LLVM_BUILD_DIR=$(echo CFG_LLVM_BUILD_DIR_${t} | tr - _)
1791     CFG_LLVM_INST_DIR=$(echo CFG_LLVM_INST_DIR_${t} | tr - _)
1792     putvar $CFG_LLVM_BUILD_DIR
1793     putvar $CFG_LLVM_INST_DIR
1794 done
1795
1796 msg
1797 copy_if_changed ${CFG_SRC_DIR}Makefile.in ./Makefile
1798 move_if_changed config.tmp config.mk
1799 rm -f config.tmp
1800 touch config.stamp
1801
1802 if [ -z "$CFG_ENABLE_DEBUG" ]; then
1803     step_msg "configured in release mode. for development consider --enable-debug"
1804 else
1805     step_msg "complete"
1806 fi
1807
1808 msg "run \`make help\`"
1809 msg