]> git.lizzy.rs Git - rust.git/blob - configure
e5106f93d3587a66e0651f8db0db1a33fb792d47
[rust.git] / configure
1 #!/bin/sh
2
3 msg() {
4     echo "configure: $1"
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 need_ok() {
23     if [ $? -ne 0 ]
24     then
25         err "$1"
26     fi
27 }
28
29 need_cmd() {
30     if command -v $1 >/dev/null 2>&1
31     then msg "found $1"
32     else err "need $1"
33     fi
34 }
35
36 make_dir() {
37     if [ ! -d $1 ]
38     then
39         msg "mkdir -p $1"
40         mkdir -p $1
41     fi
42 }
43
44 copy_if_changed() {
45     if cmp -s $1 $2
46     then
47         msg "leaving $2 unchanged"
48     else
49         msg "cp $1 $2"
50         cp -f $1 $2
51         chmod u-w $2 # make copied artifact read-only
52     fi
53 }
54
55 move_if_changed() {
56     if cmp -s $1 $2
57     then
58         msg "leaving $2 unchanged"
59     else
60         msg "mv $1 $2"
61         mv -f $1 $2
62         chmod u-w $2 # make moved artifact read-only
63     fi
64 }
65
66 putvar() {
67     local T
68     eval T=\$$1
69     eval TLEN=\${#$1}
70     if [ $TLEN -gt 35 ]
71     then
72         printf "configure: %-20s := %.35s ...\n" $1 "$T"
73     else
74         printf "configure: %-20s := %s %s\n" $1 "$T" "$2"
75     fi
76     printf "%-20s := %s\n" $1 "$T" >>config.tmp
77 }
78
79 probe() {
80     local V=$1
81     shift
82     local P
83     local T
84     for P
85     do
86         T=$(command -v $P 2>&1)
87         if [ $? -eq 0 ]
88         then
89             VER0=$($P --version 2>/dev/null | head -1 \
90                 |  sed -e 's/[^0-9]*\([vV]\?[0-9.]\+[^ ]*\).*/\1/' )
91             if [ $? -eq 0 -a "x${VER0}" != "x" ]
92             then
93               VER="($VER0)"
94             else
95               VER=""
96             fi
97             break
98         else
99             VER=""
100             T=""
101         fi
102     done
103     eval $V=\$T
104     putvar $V "$VER"
105 }
106
107 probe_need() {
108     local V=$1
109     probe $*
110     eval VV=\$$V
111     if [ -z "$VV" ]
112     then
113         err "needed, but unable to find any of: $*"
114     fi
115 }
116
117 validate_opt () {
118     for arg in $CFG_CONFIGURE_ARGS
119     do
120         isArgValid=0
121         for option in $BOOL_OPTIONS
122         do
123             if test --disable-$option = $arg
124             then
125                 isArgValid=1
126             fi
127             if test --enable-$option = $arg
128             then
129                 isArgValid=1
130             fi
131         done
132         for option in $VAL_OPTIONS
133         do
134             if echo "$arg" | grep -q -- "--$option="
135             then
136                 isArgValid=1
137             fi
138         done
139         if [ "$arg" = "--help" ]
140         then
141             echo
142             echo "No more help available for Configure options,"
143             echo "check the Wiki or join our IRC channel"
144             break
145         else
146             if test $isArgValid -eq 0
147             then
148                 err "Option '$arg' is not recognized"
149             fi
150         fi
151     done
152 }
153
154 valopt() {
155     VAL_OPTIONS="$VAL_OPTIONS $1"
156
157     local OP=$1
158     local DEFAULT=$2
159     shift
160     shift
161     local DOC="$*"
162     if [ $HELP -eq 0 ]
163     then
164         local UOP=$(echo $OP | tr '[:lower:]' '[:upper:]' | tr '\-' '\_')
165         local V="CFG_${UOP}"
166         eval $V="$DEFAULT"
167         for arg in $CFG_CONFIGURE_ARGS
168         do
169             if echo "$arg" | grep -q -- "--$OP="
170             then
171                 val=$(echo "$arg" | cut -f2 -d=)
172                 eval $V=$val
173             fi
174         done
175         putvar $V
176     else
177         if [ -z "$DEFAULT" ]
178         then
179             DEFAULT="<none>"
180         fi
181         OP="${OP}=[${DEFAULT}]"
182         printf "    --%-30s %s\n" "$OP" "$DOC"
183     fi
184 }
185
186 opt() {
187     BOOL_OPTIONS="$BOOL_OPTIONS $1"
188
189     local OP=$1
190     local DEFAULT=$2
191     shift
192     shift
193     local DOC="$*"
194     local FLAG=""
195
196     if [ $DEFAULT -eq 0 ]
197     then
198         FLAG="enable"
199     else
200         FLAG="disable"
201         DOC="don't $DOC"
202     fi
203
204     if [ $HELP -eq 0 ]
205     then
206         for arg in $CFG_CONFIGURE_ARGS
207         do
208             if [ "$arg" = "--${FLAG}-${OP}" ]
209             then
210                 OP=$(echo $OP | tr 'a-z-' 'A-Z_')
211                 FLAG=$(echo $FLAG | tr 'a-z' 'A-Z')
212                 local V="CFG_${FLAG}_${OP}"
213                 eval $V=1
214                 putvar $V
215             fi
216         done
217     else
218         if [ ! -z "$META" ]
219         then
220             OP="$OP=<$META>"
221         fi
222         printf "    --%-30s %s\n" "$FLAG-$OP" "$DOC"
223      fi
224 }
225
226 envopt() {
227     local NAME=$1
228     local V="CFG_${NAME}"
229     eval VV=\$$V
230
231     # If configure didn't set a value already, then check environment.
232     #
233     # (It is recommended that the configure script always check the
234     # environment before setting any values to envopt variables; see
235     # e.g.  how CFG_CC is handled, where it first checks `-z "$CC"`,
236     # and issues msg if it ends up employing that provided value.)
237     if [ -z "$VV" ]
238     then
239         eval $V=\$$NAME
240         eval VV=\$$V
241     fi
242
243     # If script or environment provided a value, save it.
244     if [ ! -z "$VV" ]
245     then
246         putvar $V
247     fi
248 }
249
250 msg "looking for configure programs"
251 need_cmd cmp
252 need_cmd mkdir
253 need_cmd printf
254 need_cmd cut
255 need_cmd head
256 need_cmd grep
257 need_cmd xargs
258 need_cmd cp
259 need_cmd find
260 need_cmd uname
261 need_cmd date
262 need_cmd tr
263 need_cmd sed
264 need_cmd file
265
266 msg "inspecting environment"
267
268 CFG_OSTYPE=$(uname -s)
269 CFG_CPUTYPE=$(uname -m)
270
271 if [ $CFG_OSTYPE = Darwin -a $CFG_CPUTYPE = i386 ]
272 then
273     # Darwin's `uname -s` lies and always returns i386. We have to use sysctl
274     # instead.
275     if sysctl hw.optional.x86_64 | grep -q ': 1'
276     then
277         CFG_CPUTYPE=x86_64
278     fi
279 fi
280
281 # The goal here is to come up with the same triple as LLVM would,
282 # at least for the subset of platforms we're willing to target.
283
284 case $CFG_OSTYPE in
285
286     Linux)
287         CFG_OSTYPE=unknown-linux-gnu
288         ;;
289
290     FreeBSD)
291         CFG_OSTYPE=unknown-freebsd
292         ;;
293
294     Darwin)
295         CFG_OSTYPE=apple-darwin
296         ;;
297
298     MINGW32*)
299         CFG_OSTYPE=pc-mingw32
300         ;;
301
302     MINGW64*)
303         # msys2, MSYSTEM=MINGW64
304         CFG_OSTYPE=w64-mingw32
305         ;;
306
307 # Thad's Cygwin identifers below
308
309 #   Vista 32 bit
310     CYGWIN_NT-6.0)
311         CFG_OSTYPE=pc-mingw32
312         CFG_CPUTYPE=i686
313         ;;
314
315 #   Vista 64 bit
316     CYGWIN_NT-6.0-WOW64)
317         CFG_OSTYPE=w64-mingw32
318         CFG_CPUTYPE=x86_64
319         ;;
320
321 #   Win 7 32 bit
322     CYGWIN_NT-6.1)
323         CFG_OSTYPE=pc-mingw32
324         CFG_CPUTYPE=i686
325         ;;
326
327 #   Win 7 64 bit
328     CYGWIN_NT-6.1-WOW64)
329         CFG_OSTYPE=w64-mingw32
330         CFG_CPUTYPE=x86_64
331         ;;
332
333 # We do not detect other OS such as XP/2003 using 64 bit using uname.
334 # 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.
335     *)
336         err "unknown OS type: $CFG_OSTYPE"
337         ;;
338 esac
339
340
341 case $CFG_CPUTYPE in
342
343     i386 | i486 | i686 | i786 | x86)
344         CFG_CPUTYPE=i686
345         ;;
346
347     xscale | arm)
348         CFG_CPUTYPE=arm
349         ;;
350
351     armv7l)
352         CFG_CPUTYPE=arm
353         CFG_OSTYPE="${CFG_OSTYPE}eabihf"
354         ;;
355
356     x86_64 | x86-64 | x64 | amd64)
357         CFG_CPUTYPE=x86_64
358         ;;
359
360     *)
361         err "unknown CPU type: $CFG_CPUTYPE"
362 esac
363
364 # Detect 64 bit linux systems with 32 bit userland and force 32 bit compilation
365 if [ $CFG_OSTYPE = unknown-linux-gnu -a $CFG_CPUTYPE = x86_64 ]
366 then
367     file -L "$SHELL" | grep -q "x86[_-]64"
368     if [ $? != 0 ]; then
369         CFG_CPUTYPE=i686
370     fi
371 fi
372
373
374 DEFAULT_BUILD="${CFG_CPUTYPE}-${CFG_OSTYPE}"
375
376 CFG_SRC_DIR="$(cd $(dirname $0) && pwd)/"
377 CFG_BUILD_DIR="$(pwd)/"
378 CFG_SELF="$0"
379 CFG_CONFIGURE_ARGS="$@"
380
381 OPTIONS=""
382 HELP=0
383 if [ "$1" = "--help" ]
384 then
385     HELP=1
386     shift
387     echo
388     echo "Usage: $CFG_SELF [options]"
389     echo
390     echo "Options:"
391     echo
392 else
393     msg "recreating config.tmp"
394     echo '' >config.tmp
395
396     step_msg "processing $CFG_SELF args"
397 fi
398
399 BOOL_OPTIONS=""
400 VAL_OPTIONS=""
401
402 opt valgrind 0 "run tests with valgrind (memcheck by default)"
403 opt helgrind 0 "run tests with helgrind instead of memcheck"
404 opt docs     1 "build documentation"
405 opt optimize 1 "build optimized rust code"
406 opt optimize-cxx 1 "build optimized C++ code"
407 opt optimize-llvm 1 "build optimized LLVM"
408 opt optimize-tests 1 "build tests with optimizations"
409 opt libcpp 1 "build with llvm with libc++ instead of libstdc++ when using clang"
410 opt llvm-assertions 1 "build LLVM with assertions"
411 opt debug 1 "build with extra debug fun"
412 opt ratchet-bench 0 "ratchet benchmarks"
413 opt fast-make 0 "use .gitmodules as timestamp for submodule deps"
414 opt manage-submodules 1 "let the build manage the git submodules"
415 opt mingw-cross 0 "cross-compile for win32 using mingw"
416 opt clang 0 "prefer clang to gcc for building the runtime"
417 opt ccache 0 "invoke gcc/clang via ccache to reuse object files between builds"
418 opt local-rust 0 "use an installed rustc rather than downloading a snapshot"
419 opt pax-flags 0 "apply PaX flags to rustc binaries (required for GRSecurity/PaX-patched kernels)"
420 opt inject-std-version 1 "inject the current compiler version of libstd into programs"
421 opt llvm-static-stdcpp 0 "statically link to libstdc++ for LLVM"
422 opt rpath 1 "build rpaths into rustc itself"
423 opt nightly 0 "build nightly packages"
424 opt verify-install 1 "verify installed binaries work"
425 valopt prefix "/usr/local" "set installation prefix"
426 valopt local-rust-root "/usr/local" "set prefix for local rust binary"
427 valopt llvm-root "" "set LLVM root"
428 valopt jemalloc-root "" "set directory where libjemalloc_pic.a is located"
429 valopt libuv-root "" "set directory where libuv.a is located"
430 valopt android-cross-path "/opt/ndk_standalone" "Android NDK standalone path"
431 valopt mingw32-cross-path "" "MinGW32 cross compiler path"
432
433 valopt build "${DEFAULT_BUILD}" "GNUs ./configure syntax LLVM build triple"
434 valopt host "${CFG_BUILD}" "GNUs ./configure syntax LLVM host triples"
435 valopt target "${CFG_HOST}" "GNUs ./configure syntax LLVM target triples"
436
437 valopt localstatedir "/var/lib" "local state directory"
438 valopt sysconfdir "/etc" "install system configuration files"
439
440 valopt datadir "${CFG_PREFIX}/share" "install data"
441 valopt infodir "${CFG_PREFIX}/share/info" "install additional info"
442 valopt mandir "${CFG_PREFIX}/share/man" "install man pages in PATH"
443
444 # On windows we just store the libraries in the bin directory because
445 # there's no rpath. This is where the build system itself puts libraries;
446 # --libdir is used to configure the installation directory.
447 # FIXME: Thise needs to parameterized over target triples. Do it in platform.mk
448 CFG_LIBDIR_RELATIVE=lib
449 if [ "$CFG_OSTYPE" = "pc-mingw32" ] || [ "$CFG_OSTYPE" = "w64-mingw32" ]
450 then
451     CFG_LIBDIR_RELATIVE=bin
452 fi
453
454 valopt libdir "${CFG_PREFIX}/${CFG_LIBDIR_RELATIVE}" "install libraries"
455
456 if [ $HELP -eq 1 ]
457 then
458     echo
459     exit 0
460 fi
461
462 # Validate Options
463 step_msg "validating $CFG_SELF args"
464 validate_opt
465
466 step_msg "looking for build programs"
467
468 probe_need CFG_PERL        perl
469 probe_need CFG_CURLORWGET  curl wget
470 probe_need CFG_PYTHON      python2.7 python2.6 python2 python
471
472 python_version=$($CFG_PYTHON -V 2>&1)
473 if [ $(echo $python_version | grep -c '^Python 2\.[4567]') -ne 1 ]; then
474     err "Found $python_version, but LLVM requires Python 2.4-2.7"
475 fi
476
477 # If we have no git directory then we are probably a tarball distribution
478 # and shouldn't attempt to load submodules
479 if [ ! -e ${CFG_SRC_DIR}.git ]
480 then
481     probe CFG_GIT          git
482     msg "git: no git directory. disabling submodules"
483     CFG_DISABLE_MANAGE_SUBMODULES=1
484 else
485     probe_need CFG_GIT     git
486 fi
487
488 probe CFG_CLANG            clang++
489 probe CFG_CCACHE           ccache
490 probe CFG_GCC              gcc
491 probe CFG_LD               ld
492 probe CFG_VALGRIND         valgrind
493 probe CFG_PERF             perf
494 probe CFG_ISCC             iscc
495 probe CFG_LLNEXTGEN        LLnextgen
496 probe CFG_PANDOC           pandoc
497 probe CFG_PDFLATEX         pdflatex
498 probe CFG_XELATEX          xelatex
499 probe CFG_LUALATEX         lualatex
500 probe CFG_GDB              gdb
501 probe CFG_LLDB             lldb
502
503 if [ ! -z "$CFG_LLDB" ]
504 then
505     # If CFG_LLDB_PYTHON_DIR is not already set from the outside and valid, try to read it from
506     # LLDB via the -P commandline options.
507     if [ -z "$CFG_LLDB_PYTHON_DIR" ] || [ ! -d "$CFG_LLDB_PYTHON_DIR" ]
508     then
509         CFG_LLDB_PYTHON_DIR=$($CFG_LLDB -P)
510
511         # If CFG_LLDB_PYTHON_DIR is not a valid directory, set it to something more readable
512         if [ ! -d "$CFG_LLDB_PYTHON_DIR" ]
513         then
514             CFG_LLDB_PYTHON_DIR="LLDB_PYTHON_DIRECTORY_NOT_FOUND"
515         fi
516
517         putvar CFG_LLDB_PYTHON_DIR
518     fi
519 fi
520
521 if [ "$CFG_OSTYPE" = "unknown-linux-gnu" ]
522 then
523     probe CFG_PAXCTL           paxctl /sbin/paxctl
524     probe CFG_ZCAT             zcat
525 fi
526
527 step_msg "looking for target specific programs"
528
529 probe CFG_ADB        adb
530
531 if [ ! -z "$CFG_PANDOC" ]
532 then
533     PV_MAJOR_MINOR=$(pandoc --version | grep '^pandoc ' |
534         # extract the first 2 version fields, ignore everything else
535         sed 's/pandoc \([0-9]*\)\.\([0-9]*\).*/\1 \2/')
536
537     MIN_PV_MAJOR="1"
538     MIN_PV_MINOR="9"
539     # these patterns are shell globs, *not* regexps
540     PV_MAJOR=${PV_MAJOR_MINOR% *}
541     PV_MINOR=${PV_MAJOR_MINOR#* }
542     if [ "$PV_MAJOR" -lt "$MIN_PV_MAJOR" ] || [ "$PV_MINOR" -lt "$MIN_PV_MINOR" ]
543     then
544         step_msg "pandoc $PV_MAJOR.$PV_MINOR is too old. Need at least $MIN_PV_MAJOR.$MIN_PV_MINOR. Disabling"
545         BAD_PANDOC=1
546     fi
547 fi
548
549 if [ "$CFG_OSTYPE" = "unknown-linux-gnu" ]
550 then
551     if [ ! -z "$CFG_ENABLE_PAX_FLAGS" -a -z "$CFG_PAXCTL" ]
552     then
553         err "enabled PaX markings but no paxctl binary found"
554     fi
555
556     if [ -z "$CFG_DISABLE_PAX_FLAGS" ]
557     then
558         # GRSecurity/PaX detection. This can be very flaky.
559         GRSEC_DETECTED=
560
561         # /dev/grsec only exists if CONFIG_GRKERNSEC_NO_RBAC is not set.
562         # /proc/sys/kernel/grsecurity is not available if Ã‡ONFIG_GRKERNSEC_SYSCTL is not set.
563         if [ -e /dev/grsec -o -d /proc/sys/kernel/grsecurity ]
564         then
565             GRSEC_DETECTED=1
566         # /proc/config.gz is normally only available to root, and only if CONFIG_IKCONFIG_PROC has been set.
567         elif [ -r /proc/config.gz -a ! -z "$CFG_ZCAT" ]
568         then
569             if "$CFG_ZCAT" /proc/config.gz | grep --quiet "CONFIG_GRKERNSEC=y"
570             then
571                 GRSEC_DETECTED=1
572             fi
573         # Flaky.
574         elif grep --quiet grsec /proc/version
575         then
576             GRSEC_DETECTED=1
577         fi
578
579         if [ ! -z "$GRSEC_DETECTED" ]
580         then
581             step_msg "GRSecurity: yes"
582             if [ ! -z "$CFG_PAXCTL" ]
583             then
584                 CFG_ENABLE_PAX_FLAGS=1
585             else
586                 warn "GRSecurity kernel detected but no paxctl binary found: not setting CFG_ENABLE_PAX_FLAGS"
587             fi
588         else
589             step_msg "GRSecurity: no"
590         fi
591     fi
592 fi
593
594 BIN_SUF=
595 if [ "$CFG_OSTYPE" = "pc-mingw32" ] || [ "$CFG_OSTYPE" = "w64-mingw32" ]
596 then
597     BIN_SUF=.exe
598 fi
599
600 if [ ! -z "$CFG_ENABLE_LOCAL_RUST" ]
601 then
602     if [ ! -f ${CFG_LOCAL_RUST_ROOT}/bin/rustc${BIN_SUF} ]
603     then
604         err "no local rust to use"
605     else
606         LRV=`${CFG_LOCAL_RUST_ROOT}/bin/rustc${BIN_SUF} --version`
607         step_msg "using rustc at: ${CFG_LOCAL_RUST_ROOT} with version: $LRV"
608     fi
609 fi
610
611 # Force freebsd to build with clang; gcc doesn't like us there
612 if [ $CFG_OSTYPE = unknown-freebsd ]
613 then
614     step_msg "on FreeBSD, forcing use of clang"
615     CFG_ENABLE_CLANG=1
616     putvar CFG_ENABLE_CLANG
617 fi
618
619 if [ -z "$CFG_ENABLE_CLANG" -a -z "$CFG_GCC" ]
620 then
621     err "either clang or gcc is required"
622 fi
623
624 # OS X 10.9, gcc is actually clang. This can cause some confusion in the build
625 # system, so if we find that gcc is clang, we should just use clang directly.
626 if [ $CFG_OSTYPE = apple-darwin -a -z "$CFG_ENABLE_CLANG" ]
627 then
628     CFG_OSX_GCC_VERSION=$("$CFG_GCC" --version 2>&1 | grep "Apple LLVM version")
629     if [ $? -eq 0 ]
630     then
631         step_msg "on OS X 10.9, forcing use of clang"
632         CFG_ENABLE_CLANG=1
633         putvar CFG_ENABLE_CLANG
634     else
635         if [ $("$CFG_GCC" --version 2>&1 | grep -c ' 4\.[0-6]') -ne 0 ]; then
636             step_msg "older GCC found, using clang instead"
637             CFG_ENABLE_CLANG=1
638             putvar CFG_ENABLE_CLANG
639         else
640             # on OS X, with xcode 5 and newer, certain developers may have
641             # cc, gcc and g++ point to a  mixture of clang and gcc
642             # if so, this will create very strange build errors
643             # this last stanza is to detect some such problems and save the future rust
644             # contributor some time solving that issue.
645             # this detection could be generalized to other OSes aside from OS X
646             # but the issue seems most likely to happen on OS X
647
648             chk_cc () {
649                 $1 --version 2> /dev/null | grep -q $2
650             }
651             # check that gcc, cc and g++ all point to the same compiler.
652             # note that for xcode 5, g++ points to clang, not clang++
653             if !((chk_cc gcc clang  && chk_cc g++ clang) ||
654                 (chk_cc gcc gcc  &&( chk_cc g++ g++ || chk g++ gcc))) then
655                 err "the gcc and g++ in your path point to different compilers.
656     Check which versions are in your path with gcc --version and g++ --version.
657     To resolve this problem, either fix your PATH  or run configure with --enable-clang"
658             fi
659
660         fi
661     fi
662 fi
663
664 if [ ! -z "$CFG_LLVM_ROOT" -a -e "$CFG_LLVM_ROOT/bin/llvm-config" ]
665 then
666     step_msg "using custom LLVM at $CFG_LLVM_ROOT"
667
668     LLVM_CONFIG="$CFG_LLVM_ROOT/bin/llvm-config"
669     LLVM_VERSION=$($LLVM_CONFIG --version)
670
671     case $LLVM_VERSION in
672         (3.[2-5]*)
673             msg "found ok version of LLVM: $LLVM_VERSION"
674             ;;
675         (*)
676             err "bad LLVM version: $LLVM_VERSION, need >=3.0svn"
677             ;;
678     esac
679 fi
680
681 # Even when the user overrides the choice of CC, still try to detect
682 # clang to disable some clang-specific warnings.  We here draw a
683 # distinction between:
684 #
685 #  CFG_ENABLE_CLANG : passed --enable-clang, or host "requires" clang,
686 #  CFG_USING_CLANG : compiler (clang / gcc / $CC) looks like clang.
687 #
688 # This distinction is important because there are some safeguards we
689 # would prefer to skip when merely CFG_USING_CLANG is set; but when
690 # CFG_ENABLE_CLANG is set, that indicates that we are opting into
691 # running such safeguards.
692
693 if [ ! -z "$CC" ]
694 then
695     msg "skipping compiler inference steps; using provided CC=$CC"
696     CFG_CC="$CC"
697
698     CFG_OSX_CC_VERSION=$("$CFG_CC" --version 2>&1 | grep "clang")
699     if [ $? -eq 0 ]
700     then
701         step_msg "note, user-provided CC looks like clang; CC=$CC."
702         CFG_USING_CLANG=1
703         putvar CFG_USING_CLANG
704     fi
705 else
706     if [ ! -z "$CFG_ENABLE_CLANG" ]
707     then
708         if [ -z "$CFG_CLANG" ]
709         then
710             err "clang requested but not found"
711         fi
712         CFG_CC="$CFG_CLANG"
713         CFG_USING_CLANG=1
714         putvar CFG_USING_CLANG
715     else
716         CFG_CC="gcc"
717     fi
718 fi
719
720 if [ ! -z "$CFG_ENABLE_CLANG" ]
721 then
722     if [ -z "$CC" ] || [[ $CC == *clang ]]
723     then
724         CFG_CLANG_VERSION=$($CFG_CC \
725             --version \
726             | grep version \
727             | sed 's/.*\(version .*\)/\1/; s/.*based on \(LLVM .*\))/\1/' \
728             | cut -d ' ' -f 2)
729
730         case $CFG_CLANG_VERSION in
731             (3.0svn | 3.0 | 3.1* | 3.2* | 3.3* | 3.4* | 3.5* )
732             step_msg "found ok version of CLANG: $CFG_CLANG_VERSION"
733             if [ -z "$CC" ]
734             then
735                 CFG_CC="clang"
736                 CFG_CXX="clang++"
737             fi
738             ;;
739             (*)
740             err "bad CLANG version: $CFG_CLANG_VERSION, need >=3.0svn"
741             ;;
742         esac
743     else
744         msg "skipping CFG_ENABLE_CLANG version check; provided CC=$CC"
745     fi
746 fi
747
748 if [ ! -z "$CFG_ENABLE_CCACHE" ]
749 then
750     if [ -z "$CC" ]
751     then
752         if [ -z "$CFG_CCACHE" ]
753         then
754             err "ccache requested but not found"
755         fi
756
757         CFG_CC="ccache $CFG_CC"
758     fi
759 fi
760
761 if [ -z "$CC" -a -z "$CFG_ENABLE_CLANG" -a -z "$CFG_GCC" ]
762 then
763     err "either clang or gcc is required"
764 fi
765
766 # All safeguards based on $CFG_ENABLE_CLANG should occur before this
767 # point in the script; after this point, script logic should inspect
768 # $CFG_USING_CLANG rather than $CFG_ENABLE_CLANG.
769
770 # Set CFG_{CC,CXX,CPP,CFLAGS,CXXFLAGS}
771 envopt CC
772 envopt CXX
773 envopt CPP
774 envopt CFLAGS
775 envopt CXXFLAGS
776
777 # a little post-processing of various config values
778 CFG_PREFIX=${CFG_PREFIX%/}
779 CFG_MANDIR=${CFG_MANDIR%/}
780 CFG_HOST="$(echo $CFG_HOST | tr ',' ' ')"
781 CFG_TARGET="$(echo $CFG_TARGET | tr ',' ' ')"
782 CFG_SUPPORTED_TARGET="$(grep ^CC_*=* ${CFG_SRC_DIR}mk/platform.mk | sed -e 's/^CC_//' -e 's/\([^=]*\).*/\1/' | xargs)"
783
784 # copy host-triples to target-triples so that hosts are a subset of targets
785 V_TEMP=""
786 for i in $CFG_HOST $CFG_TARGET;
787 do
788    echo "$V_TEMP" | grep -qF $i || V_TEMP="$V_TEMP${V_TEMP:+ }$i"
789 done
790 CFG_TARGET=$V_TEMP
791
792 # check target-specific tool-chains
793 for i in $CFG_TARGET
794 do
795     L_CHECK=false
796     for j in $CFG_SUPPORTED_TARGET
797     do
798         if [ $i = $j ]
799         then
800             L_CHECK=true
801         fi
802     done
803
804     if [ $L_CHECK = false ]
805     then
806         err "unsupported target triples \"$i\" found"
807     fi
808
809     case $i in
810         arm-linux-androideabi)
811
812             if [ ! -f $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-gcc ]
813             then
814                 err "NDK $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-gcc not found"
815             fi
816             if [ ! -f $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-g++ ]
817             then
818                 err "NDK $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-g++ not found"
819             fi
820             if [ ! -f $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-ar ]
821             then
822                 err "NDK $CFG_ANDROID_CROSS_PATH/bin/arm-linux-androideabi-ar not found"
823             fi
824             ;;
825
826         arm-apple-darwin)
827             if [ $CFG_OSTYPE != apple-darwin ]
828             then
829                 err "The iOS target is only supported on Mac OS X"
830             fi
831             ;;
832
833         *)
834             ;;
835     esac
836 done
837
838 if [ ! -z "$CFG_PERF" ]
839 then
840     HAVE_PERF_LOGFD=`$CFG_PERF stat --log-fd 2>&1 | grep 'unknown option'`
841     if [ -z "$HAVE_PERF_LOGFD" ];
842     then
843         CFG_PERF_WITH_LOGFD=1
844         putvar CFG_PERF_WITH_LOGFD
845     fi
846 fi
847
848 step_msg "making directories"
849
850 for i in \
851     doc doc/std doc/extra \
852     dl tmp dist
853 do
854     make_dir $i
855 done
856
857 for t in $CFG_HOST
858 do
859     make_dir $t/llvm
860 done
861
862 for t in $CFG_HOST
863 do
864     make_dir $t/rustllvm
865 done
866
867 for t in $CFG_TARGET
868 do
869   make_dir $t/rt
870   for s in 0 1 2 3
871   do
872     make_dir $t/rt/stage$s
873     make_dir $t/rt/jemalloc
874     make_dir $t/rt/libuv
875     make_dir $t/rt/libuv/src/ares
876     make_dir $t/rt/libuv/src/eio
877     make_dir $t/rt/libuv/src/ev
878     for i in                                          \
879       isaac sync test \
880       arch/i386 arch/x86_64 arch/arm arch/mips  \
881       sundown/src sundown/html
882     do
883       make_dir $t/rt/stage$s/$i
884     done
885   done
886 done
887
888 for h in $CFG_HOST
889 do
890     for t in $CFG_TARGET
891     do
892         for i in 0 1 2 3
893         do
894             # host bin dir
895             make_dir $h/stage$i/bin
896
897             # host lib dir
898             make_dir $h/stage$i/$CFG_LIBDIR_RELATIVE
899
900             # host test dir
901             make_dir $h/stage$i/test
902
903             # target bin dir
904             make_dir $h/stage$i/$CFG_LIBDIR_RELATIVE/rustlib/$t/bin
905
906             # target lib dir
907             make_dir $h/stage$i/$CFG_LIBDIR_RELATIVE/rustlib/$t/lib
908         done
909     done
910
911     make_dir $h/test/run-pass
912     make_dir $h/test/run-pass-fulldeps
913     make_dir $h/test/run-fail
914     make_dir $h/test/compile-fail
915     make_dir $h/test/compile-fail-fulldeps
916     make_dir $h/test/bench
917     make_dir $h/test/perf
918     make_dir $h/test/pretty
919     make_dir $h/test/debuginfo-gdb
920     make_dir $h/test/debuginfo-lldb
921     make_dir $h/test/codegen
922     make_dir $h/test/doc-tutorial
923     make_dir $h/test/doc-guide-ffi
924     make_dir $h/test/doc-guide-runtime
925     make_dir $h/test/doc-guide-macros
926     make_dir $h/test/doc-guide-lifetimes
927     make_dir $h/test/doc-guide-pointers
928     make_dir $h/test/doc-guide-container
929     make_dir $h/test/doc-guide-tasks
930     make_dir $h/test/doc-complement-cheatsheet
931     make_dir $h/test/doc-rust
932 done
933
934 # Configure submodules
935 step_msg "configuring submodules"
936
937 # Have to be in the top of src directory for this
938 if [ -z $CFG_DISABLE_MANAGE_SUBMODULES ]
939 then
940     cd ${CFG_SRC_DIR}
941
942     msg "git: submodule sync"
943     "${CFG_GIT}" submodule sync
944
945     msg "git: submodule init"
946     "${CFG_GIT}" submodule init
947
948     # Disable submodules that we're not using
949     if [ ! -z "${CFG_LLVM_ROOT}" ]; then
950         msg "git: submodule deinit src/llvm"
951         "${CFG_GIT}" submodule deinit src/llvm
952     fi
953     if [ ! -z "${CFG_JEMALLOC_ROOT}" ]; then
954         msg "git: submodule deinit src/jemalloc"
955         "${CFG_GIT}" submodule deinit src/jemalloc
956     fi
957     if [ ! -z "${CFG_LIBUV_ROOT}" ]; then
958         msg "git: submodule deinit src/libuv"
959         "${CFG_GIT}" submodule deinit src/libuv
960     fi
961
962     msg "git: submodule update"
963     "${CFG_GIT}" submodule update
964     need_ok "git failed"
965
966     msg "git: submodule foreach sync"
967     "${CFG_GIT}" submodule foreach --recursive 'if test -e .gitmodules; then git submodule sync; fi'
968     need_ok "git failed"
969
970     msg "git: submodule foreach update"
971     "${CFG_GIT}" submodule update --recursive
972     need_ok "git failed"
973
974     # NB: this is just for the sake of getting the submodule SHA1 values
975     # and status written into the build log.
976     msg "git: submodule status"
977     "${CFG_GIT}" submodule status --recursive
978
979     msg "git: submodule clobber"
980     "${CFG_GIT}" submodule foreach --recursive git clean -dxf
981     need_ok "git failed"
982     "${CFG_GIT}" submodule foreach --recursive git checkout .
983     need_ok "git failed"
984
985     cd ${CFG_BUILD_DIR}
986 fi
987
988 # Configure llvm, only if necessary
989 step_msg "looking at LLVM"
990 CFG_LLVM_SRC_DIR=${CFG_SRC_DIR}src/llvm/
991 for t in $CFG_HOST
992 do
993     do_reconfigure=1
994
995     if [ -z $CFG_LLVM_ROOT ]
996     then
997         LLVM_BUILD_DIR=${CFG_BUILD_DIR}$t/llvm
998         if [ ! -z "$CFG_DISABLE_OPTIMIZE_LLVM" ]
999         then
1000             LLVM_DBG_OPTS="--enable-debug-symbols --disable-optimized"
1001             # Just use LLVM straight from its build directory to
1002             # avoid 'make install' time
1003             LLVM_INST_DIR=$LLVM_BUILD_DIR/Debug
1004         else
1005             LLVM_DBG_OPTS="--enable-optimized"
1006             LLVM_INST_DIR=$LLVM_BUILD_DIR/Release
1007         fi
1008         if [ ! -z "$CFG_DISABLE_LLVM_ASSERTIONS" ]
1009         then
1010             LLVM_ASSERTION_OPTS="--disable-assertions"
1011         else
1012             LLVM_ASSERTION_OPTS="--enable-assertions"
1013             LLVM_INST_DIR=${LLVM_INST_DIR}+Asserts
1014         fi
1015     else
1016         msg "not reconfiguring LLVM, external LLVM root"
1017         # The user is using their own LLVM
1018         LLVM_BUILD_DIR=
1019         LLVM_INST_DIR=$CFG_LLVM_ROOT
1020         do_reconfigure=0
1021     fi
1022
1023
1024     if [ ${do_reconfigure} -ne 0 ]
1025     then
1026     # because git is hilarious, it might have put the module index
1027     # in a couple places.
1028         index1="${CFG_SRC_DIR}.git/modules/src/llvm/index"
1029         index2="${CFG_SRC_DIR}src/llvm/.git/index"
1030         for index in ${index1} ${index2}
1031         do
1032             config_status="${LLVM_BUILD_DIR}/config.status"
1033             if test -e ${index} -a \
1034                     -e ${config_status} -a \
1035                     ${config_status} -nt ${index}
1036             then
1037                 msg "not reconfiguring LLVM, config.status is fresh"
1038                 do_reconfigure=0
1039             fi
1040         done
1041     fi
1042
1043     if [ ${do_reconfigure} -ne 0 ]
1044     then
1045         msg "configuring LLVM for $t"
1046
1047         LLVM_TARGETS="--enable-targets=x86,x86_64,arm,mips"
1048         LLVM_BUILD="--build=$t"
1049         LLVM_HOST="--host=$t"
1050         LLVM_TARGET="--target=$t"
1051
1052         # Disable unused LLVM features
1053         LLVM_OPTS="$LLVM_DBG_OPTS $LLVM_ASSERTION_OPTS --disable-docs --enable-bindings=none"
1054         # Disable term-info, linkage of which comes in multiple forms,
1055         # making our snapshots incompatible (#9334)
1056         LLVM_OPTS="$LLVM_OPTS --disable-terminfo"
1057         # Try to have LLVM pull in as few dependencies as possible (#9397)
1058         LLVM_OPTS="$LLVM_OPTS --disable-zlib --disable-libffi"
1059
1060         # Use win32 native thread/lock apis instead of pthread wrapper.
1061         # (llvm's configure tries to find pthread first, so we have to disable it explicitly.)
1062         # Also note that pthreads works badly on mingw-w64 systems: #8996
1063         case "$CFG_BUILD" in
1064             (*-mingw32)
1065             LLVM_OPTS="$LLVM_OPTS --disable-pthreads"
1066             ;;
1067         esac
1068
1069         case "$CFG_CC" in
1070             ("ccache clang")
1071             LLVM_CXX_32="ccache clang++ -Qunused-arguments"
1072             LLVM_CC_32="ccache clang -Qunused-arguments"
1073
1074             LLVM_CXX_64="ccache clang++ -Qunused-arguments"
1075             LLVM_CC_64="ccache clang -Qunused-arguments"
1076             ;;
1077             ("clang")
1078             LLVM_CXX_32="clang++ -Qunused-arguments"
1079             LLVM_CC_32="clang -Qunused-arguments"
1080
1081             LLVM_CXX_64="clang++ -Qunused-arguments"
1082             LLVM_CC_64="clang -Qunused-arguments"
1083             ;;
1084             ("ccache gcc")
1085             LLVM_CXX_32="ccache g++"
1086             LLVM_CC_32="ccache gcc"
1087
1088             LLVM_CXX_64="ccache g++"
1089             LLVM_CC_64="ccache gcc"
1090             ;;
1091             ("gcc")
1092             LLVM_CXX_32="g++"
1093             LLVM_CC_32="gcc"
1094
1095             LLVM_CXX_64="g++"
1096             LLVM_CC_64="gcc"
1097             ;;
1098
1099             (*)
1100             msg "inferring LLVM_CXX/CC from CXX/CC = $CXX/$CC"
1101             LLVM_CXX_32="$CXX"
1102             LLVM_CC_32="$CC"
1103
1104             LLVM_CXX_64="$CXX"
1105             LLVM_CC_64="$CC"
1106             ;;
1107         esac
1108
1109         case "$CFG_CPUTYPE" in
1110             (x86*)
1111                 LLVM_CXX_32="$LLVM_CXX_32 -m32"
1112                 LLVM_CC_32="$LLVM_CC_32 -m32"
1113
1114                 LLVM_CFLAGS_32="-m32"
1115                 LLVM_CXXFLAGS_32="-m32"
1116                 LLVM_LDFLAGS_32="-m32"
1117
1118                 LLVM_CFLAGS_64=""
1119                 LLVM_CXXFLAGS_64=""
1120                 LLVM_LDFLAGS_64=""
1121
1122                 LLVM_CXX_32="$LLVM_CXX_32 -m32"
1123                 LLVM_CC_32="$LLVM_CC_32 -m32"
1124                 ;;
1125
1126             (*)
1127                 LLVM_CFLAGS_32=""
1128                 LLVM_CXXFLAGS_32=""
1129                 LLVM_LDFLAGS_32=""
1130
1131                 LLVM_CFLAGS_64=""
1132                 LLVM_CXXFLAGS_64=""
1133                 LLVM_LDFLAGS_64=""
1134                 ;;
1135         esac
1136
1137         if echo $t | grep -q x86_64
1138         then
1139             LLVM_CXX=$LLVM_CXX_64
1140             LLVM_CC=$LLVM_CC_64
1141             LLVM_CFLAGS=$LLVM_CFLAGS_64
1142             LLVM_CXXFLAGS=$LLVM_CXXFLAGS_64
1143             LLVM_LDFLAGS=$LLVM_LDFLAGS_64
1144         else
1145             LLVM_CXX=$LLVM_CXX_32
1146             LLVM_CC=$LLVM_CC_32
1147             LLVM_CFLAGS=$LLVM_CFLAGS_32
1148             LLVM_CXXFLAGS=$LLVM_CXXFLAGS_32
1149             LLVM_LDFLAGS=$LLVM_LDFLAGS_32
1150         fi
1151
1152         CXX=$LLVM_CXX
1153         CC=$LLVM_CC
1154         CFLAGS=$LLVM_CFLAGS
1155         CXXFLAGS=$LLVM_CXXFLAGS
1156         LDFLAGS=$LLVM_LDFLAGS
1157
1158         if [ -z "$CFG_DISABLE_LIBCPP" ] && [ -n "$CFG_USING_CLANG" ]; then
1159             LLVM_OPTS="$LLVM_OPTS --enable-libcpp"
1160         fi
1161
1162         LLVM_FLAGS="$LLVM_TARGETS $LLVM_OPTS $LLVM_BUILD \
1163                         $LLVM_HOST $LLVM_TARGET --with-python=$CFG_PYTHON"
1164
1165         msg "configuring LLVM with:"
1166         msg "$LLVM_FLAGS"
1167
1168         export CXX
1169         export CC
1170         export CFLAGS
1171         export CXXFLAGS
1172         export LDFLAGS
1173
1174         cd $LLVM_BUILD_DIR
1175         case $CFG_SRC_DIR in
1176             /* | [a-z]:* | [A-Z]:*)
1177                 ${CFG_LLVM_SRC_DIR}configure $LLVM_FLAGS
1178                 ;;
1179             *)
1180                 ${CFG_BUILD_DIR}${CFG_LLVM_SRC_DIR}configure \
1181                     $LLVM_FLAGS
1182                 ;;
1183         esac
1184         need_ok "LLVM configure failed"
1185
1186         cd $CFG_BUILD_DIR
1187     fi
1188
1189     # Construct variables for LLVM build and install directories for
1190     # each target. These will be named
1191     # CFG_LLVM_BUILD_DIR_${target_triple} but all the hyphens in
1192     # target_triple will be converted to underscore, because bash
1193     # variables can't contain hyphens. The makefile will then have to
1194     # convert back.
1195     CFG_LLVM_BUILD_DIR=$(echo CFG_LLVM_BUILD_DIR_${t} | tr - _)
1196     CFG_LLVM_INST_DIR=$(echo CFG_LLVM_INST_DIR_${t} | tr - _)
1197     eval ${CFG_LLVM_BUILD_DIR}="'$LLVM_BUILD_DIR'"
1198     eval ${CFG_LLVM_INST_DIR}="'$LLVM_INST_DIR'"
1199 done
1200
1201
1202 step_msg "writing configuration"
1203
1204 putvar CFG_SRC_DIR
1205 putvar CFG_BUILD_DIR
1206 putvar CFG_OSTYPE
1207 putvar CFG_CPUTYPE
1208 putvar CFG_CONFIGURE_ARGS
1209 putvar CFG_PREFIX
1210 putvar CFG_BUILD
1211 putvar CFG_HOST
1212 putvar CFG_TARGET
1213 putvar CFG_LIBDIR
1214 putvar CFG_LIBDIR_RELATIVE
1215 putvar CFG_DISABLE_MANAGE_SUBMODULES
1216 putvar CFG_ANDROID_CROSS_PATH
1217 putvar CFG_MINGW32_CROSS_PATH
1218 putvar CFG_MANDIR
1219 putvar CFG_DISABLE_INJECT_STD_VERSION
1220 putvar CFG_JEMALLOC_ROOT
1221 putvar CFG_LIBUV_ROOT
1222
1223 # Avoid spurious warnings from clang by feeding it original source on
1224 # ccache-miss rather than preprocessed input.
1225 if [ ! -z "$CFG_ENABLE_CCACHE" ] && [ ! -z "$CFG_USING_CLANG" ]
1226 then
1227     CFG_CCACHE_CPP2=1
1228     putvar CFG_CCACHE_CPP2
1229 fi
1230
1231 if [ ! -z "$CFG_ENABLE_CCACHE" ]
1232 then
1233     CFG_CCACHE_BASEDIR=${CFG_SRC_DIR}
1234     putvar CFG_CCACHE_BASEDIR
1235 fi
1236
1237
1238 if [ ! -z $BAD_PANDOC ]
1239 then
1240     CFG_PANDOC=
1241     putvar CFG_PANDOC
1242 fi
1243
1244 # Valgrind is only reliable on Linux. On Windows it doesn't work at all, and
1245 # on the Mac the dynamic linker causes Valgrind to emit a huge stream of
1246 # errors.
1247 if [ $CFG_OSTYPE != unknown-linux-gnu ] && [ $CFG_OSTYPE != apple-darwin ]
1248 then
1249     CFG_BAD_VALGRIND=1
1250     putvar CFG_BAD_VALGRIND
1251 fi
1252
1253 putvar CFG_LLVM_ROOT
1254 putvar CFG_LLVM_SRC_DIR
1255
1256 for t in $CFG_HOST
1257 do
1258     CFG_LLVM_BUILD_DIR=$(echo CFG_LLVM_BUILD_DIR_${t} | tr - _)
1259     CFG_LLVM_INST_DIR=$(echo CFG_LLVM_INST_DIR_${t} | tr - _)
1260     putvar $CFG_LLVM_BUILD_DIR
1261     putvar $CFG_LLVM_INST_DIR
1262 done
1263
1264 # Munge any paths that appear in config.mk back to posix-y
1265 perl -i.bak -p -e 's@ ([a-zA-Z]):[/\\]@ /\1/@go;' \
1266                -e 's@\\@/@go;' config.tmp
1267 rm -f config.tmp.bak
1268
1269 msg
1270 copy_if_changed ${CFG_SRC_DIR}Makefile.in ./Makefile
1271 move_if_changed config.tmp config.mk
1272 rm -f config.tmp
1273 touch config.stamp
1274
1275 step_msg "complete"
1276 msg "run \`make help\`"
1277 msg