1#!/usr/bin/env bash
2#===-- test-release.sh - Test the LLVM release candidates ------------------===#
3#
4#                     The LLVM Compiler Infrastructure
5#
6# This file is distributed under the University of Illinois Open Source
7# License.
8#
9#===------------------------------------------------------------------------===#
10#
11# Download, build, and test the release candidate for an LLVM release.
12#
13#===------------------------------------------------------------------------===#
14
15if [ `uname -s` = "FreeBSD" ]; then
16    MAKE=gmake
17else
18    MAKE=make
19fi
20
21projects="llvm cfe dragonegg compiler-rt libcxx libcxxabi test-suite clang-tools-extra"
22
23# Base SVN URL for the sources.
24Base_url="http://llvm.org/svn/llvm-project"
25
26Release=""
27Release_no_dot=""
28RC=""
29Triple=""
30use_gzip="no"
31do_checkout="yes"
32do_ada="no"
33do_clang="yes"
34do_dragonegg="no"
35do_fortran="no"
36do_objc="yes"
37do_64bit="yes"
38do_debug="no"
39do_asserts="no"
40do_compare="yes"
41BuildDir="`pwd`"
42BuildTriple=""
43
44function usage() {
45    echo "usage: `basename $0` -release X.Y -rc NUM [OPTIONS]"
46    echo ""
47    echo " -release X.Y         The release number to test."
48    echo " -rc NUM              The pre-release candidate number."
49    echo " -final               The final release candidate."
50    echo " -triple TRIPLE       The target triple for this machine."
51    echo " -j NUM               Number of compile jobs to run. [default: 3]"
52    echo " -build-dir DIR       Directory to perform testing in. [default: pwd]"
53    echo " -no-checkout         Don't checkout the sources from SVN."
54    echo " -no-64bit            Don't test the 64-bit version. [default: yes]"
55    echo " -enable-ada          Build Ada. [default: disable]"
56    echo " -disable-clang       Do not test clang. [default: enable]"
57    echo " -enable-dragonegg    Test dragonegg. [default: disable]"
58    echo " -enable-fortran      Enable Fortran build. [default: disable]"
59    echo " -disable-objc        Disable ObjC build. [default: enable]"
60    echo " -test-debug          Test the debug build. [default: no]"
61    echo " -test-asserts        Test with asserts on. [default: no]"
62    echo " -no-compare-files    Don't test that phase 2 and 3 files are identical."
63    echo " -use-gzip            Use gzip instead of xz."
64    echo " -build-triple TRIPLE The build triple for this machine"
65    echo "                      [default: use config.guess]"
66}
67
68while [ $# -gt 0 ]; do
69    case $1 in
70        -release | --release )
71            shift
72            Release="$1"
73            Release_no_dot="`echo $1 | sed -e 's,\.,,g'`"
74            ;;
75        -rc | --rc | -RC | --RC )
76            shift
77            RC="rc$1"
78            ;;
79        -final | --final )
80            RC=final
81            ;;
82        -triple | --triple )
83            shift
84            Triple="$1"
85            ;;
86        -build-triple | --build-triple )
87            shift
88            BuildTriple="$1"
89            ;;
90        -j* )
91            NumJobs="`echo $1 | sed -e 's,-j\([0-9]*\),\1,g'`"
92            if [ -z "$NumJobs" ]; then
93                shift
94                NumJobs="$1"
95            fi
96            ;;
97        -build-dir | --build-dir | -builddir | --builddir )
98            shift
99            BuildDir="$1"
100            ;;
101        -no-checkout | --no-checkout )
102            do_checkout="no"
103            ;;
104        -no-64bit | --no-64bit )
105            do_64bit="no"
106            ;;
107        -enable-ada | --enable-ada )
108            do_ada="yes"
109            ;;
110        -disable-clang | --disable-clang )
111            do_clang="no"
112            ;;
113        -enable-dragonegg | --enable-dragonegg )
114            do_dragonegg="yes"
115            ;;
116        -enable-fortran | --enable-fortran )
117            do_fortran="yes"
118            ;;
119        -disable-objc | --disable-objc )
120            do_objc="no"
121            ;;
122        -test-debug | --test-debug )
123            do_debug="yes"
124            ;;
125        -test-asserts | --test-asserts )
126            do_asserts="yes"
127            ;;
128        -no-compare-files | --no-compare-files )
129            do_compare="no"
130            ;;
131        -use-gzip | --use-gzip )
132            use_gzip="yes"
133            ;;
134        -help | --help | -h | --h | -\? )
135            usage
136            exit 0
137            ;;
138        * )
139            echo "unknown option: $1"
140            usage
141            exit 1
142            ;;
143    esac
144    shift
145done
146
147# Check required arguments.
148if [ -z "$Release" ]; then
149    echo "error: no release number specified"
150    exit 1
151fi
152if [ -z "$RC" ]; then
153    echo "error: no release candidate number specified"
154    exit 1
155fi
156if [ -z "$Triple" ]; then
157    echo "error: no target triple specified"
158    exit 1
159fi
160
161# Figure out how many make processes to run.
162if [ -z "$NumJobs" ]; then
163    NumJobs=`sysctl -n hw.activecpu 2> /dev/null || true`
164fi
165if [ -z "$NumJobs" ]; then
166    NumJobs=`sysctl -n hw.ncpu 2> /dev/null || true`
167fi
168if [ -z "$NumJobs" ]; then
169    NumJobs=`grep -c processor /proc/cpuinfo 2> /dev/null || true`
170fi
171if [ -z "$NumJobs" ]; then
172    NumJobs=3
173fi
174
175# Go to the build directory (may be different from CWD)
176BuildDir=$BuildDir/$RC
177mkdir -p $BuildDir
178cd $BuildDir
179
180# Location of log files.
181LogDir=$BuildDir/logs
182mkdir -p $LogDir
183
184# Final package name.
185Package=clang+llvm-$Release
186if [ $RC != "final" ]; then
187  Package=$Package-$RC
188fi
189Package=$Package-$Triple
190
191# Find compilers.
192if [ "$do_dragonegg" = "yes" ]; then
193    gcc_compiler="$GCC"
194    if [ -z "$gcc_compiler" ]; then
195        gcc_compiler="`which gcc`"
196        if [ -z "$gcc_compiler" ]; then
197            echo "error: cannot find gcc to use with dragonegg"
198            exit 1
199        fi
200    fi
201
202    gxx_compiler="$GXX"
203    if [ -z "$gxx_compiler" ]; then
204        gxx_compiler="`which g++`"
205        if [ -z "$gxx_compiler" ]; then
206            echo "error: cannot find g++ to use with dragonegg"
207            exit 1
208        fi
209    fi
210fi
211
212# Make sure that a required program is available
213function check_program_exists() {
214  local program="$1"
215  if ! type -P $program > /dev/null 2>&1 ; then
216    echo "program '$1' not found !"
217    exit 1
218  fi
219}
220
221if [ `uname -s` != "Darwin" ]; then
222  check_program_exists 'chrpath'
223  check_program_exists 'file'
224  check_program_exists 'objdump'
225fi
226
227# Make sure that the URLs are valid.
228function check_valid_urls() {
229    for proj in $projects ; do
230        echo "# Validating $proj SVN URL"
231
232        if ! svn ls $Base_url/$proj/tags/RELEASE_$Release_no_dot/$RC > /dev/null 2>&1 ; then
233            echo "$proj $Release release candidate $RC doesn't exist!"
234            exit 1
235        fi
236    done
237}
238
239# Export sources to the build directory.
240function export_sources() {
241    check_valid_urls
242
243    for proj in $projects ; do
244        echo "# Exporting $proj $Release-$RC sources"
245        if ! svn export -q $Base_url/$proj/tags/RELEASE_$Release_no_dot/$RC $proj.src ; then
246            echo "error: failed to export $proj project"
247            exit 1
248        fi
249    done
250
251    echo "# Creating symlinks"
252    cd $BuildDir/llvm.src/tools
253    if [ ! -h clang ]; then
254        ln -s ../../cfe.src clang
255    fi
256    cd $BuildDir/llvm.src/tools/clang/tools
257    if [ ! -h clang-tools-extra ]; then
258        ln -s ../../../../clang-tools-extra.src extra
259    fi
260    cd $BuildDir/llvm.src/projects
261    if [ ! -h test-suite ]; then
262        ln -s ../../test-suite.src test-suite
263    fi
264    if [ ! -h compiler-rt ]; then
265        ln -s ../../compiler-rt.src compiler-rt
266    fi
267    if [ ! -h libcxx ]; then
268        ln -s ../../libcxx.src libcxx
269    fi
270    if [ ! -h libcxxabi ]; then
271        ln -s ../../libcxxabi.src libcxxabi
272    fi
273    cd $BuildDir
274}
275
276function configure_llvmCore() {
277    Phase="$1"
278    Flavor="$2"
279    ObjDir="$3"
280    InstallDir="$4"
281
282    case $Flavor in
283        Release | Release-64 )
284            Optimized="yes"
285            Assertions="no"
286            ;;
287        Release+Asserts )
288            Optimized="yes"
289            Assertions="yes"
290            ;;
291        Debug )
292            Optimized="no"
293            Assertions="yes"
294            ;;
295        * )
296            echo "# Invalid flavor '$Flavor'"
297            echo ""
298            return
299            ;;
300    esac
301
302    echo "# Using C compiler: $c_compiler"
303    echo "# Using C++ compiler: $cxx_compiler"
304
305    build_triple_option="${BuildTriple:+--build=$BuildTriple}"
306
307    cd $ObjDir
308    echo "# Configuring llvm $Release-$RC $Flavor"
309    echo "# $BuildDir/llvm.src/configure --prefix=$InstallDir \
310        --enable-optimized=$Optimized \
311        --enable-assertions=$Assertions \
312        --disable-timestamps \
313        $build_triple_option"
314    env CC="$c_compiler" CXX="$cxx_compiler" \
315    $BuildDir/llvm.src/configure --prefix=$InstallDir \
316        --enable-optimized=$Optimized \
317        --enable-assertions=$Assertions \
318        --disable-timestamps \
319        $build_triple_option \
320        2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
321    cd $BuildDir
322}
323
324function build_llvmCore() {
325    Phase="$1"
326    Flavor="$2"
327    ObjDir="$3"
328    ExtraOpts=""
329
330    if [ "$Flavor" = "Release-64" ]; then
331        ExtraOpts="EXTRA_OPTIONS=-m64"
332    fi
333
334    cd $ObjDir
335    echo "# Compiling llvm $Release-$RC $Flavor"
336    echo "# ${MAKE} -j $NumJobs VERBOSE=1 $ExtraOpts"
337    ${MAKE} -j $NumJobs VERBOSE=1 $ExtraOpts \
338        2>&1 | tee $LogDir/llvm.make-Phase$Phase-$Flavor.log
339
340    echo "# Installing llvm $Release-$RC $Flavor"
341    echo "# ${MAKE} install"
342    ${MAKE} install \
343        2>&1 | tee $LogDir/llvm.install-Phase$Phase-$Flavor.log
344    cd $BuildDir
345}
346
347function build_dragonegg() {
348    Phase="$1"
349    Flavor="$2"
350    LLVMInstallDir="$3"
351    DragonEggObjDir="$4"
352    LLVM_CONFIG=$LLVMInstallDir/bin/llvm-config
353    TOP_DIR=$BuildDir/dragonegg.src
354
355    echo "# Targeted compiler: $gcc_compiler"
356
357    cd $DragonEggObjDir
358    echo "# Compiling phase $Phase dragonegg $Release-$RC $Flavor"
359    echo -n "# CXX=$cxx_compiler TOP_DIR=$TOP_DIR GCC=$gcc_compiler "
360    echo -n "LLVM_CONFIG=$LLVM_CONFIG ${MAKE} -f $TOP_DIR/Makefile "
361    echo "-j $NumJobs VERBOSE=1"
362    CXX="$cxx_compiler" TOP_DIR="$TOP_DIR" GCC="$gcc_compiler" \
363    LLVM_CONFIG="$LLVM_CONFIG" ${MAKE} -f $TOP_DIR/Makefile \
364        -j $NumJobs VERBOSE=1 \
365            2>&1 | tee $LogDir/dragonegg-Phase$Phase-$Flavor.log
366    cd $BuildDir
367}
368
369function test_llvmCore() {
370    Phase="$1"
371    Flavor="$2"
372    ObjDir="$3"
373
374    cd $ObjDir
375    ${MAKE} -k check-all \
376        2>&1 | tee $LogDir/llvm.check-Phase$Phase-$Flavor.log
377    ${MAKE} -k unittests \
378        2>&1 | tee $LogDir/llvm.unittests-Phase$Phase-$Flavor.log
379    cd $BuildDir
380}
381
382# Clean RPATH. Libtool adds the build directory to the search path, which is
383# not necessary --- and even harmful --- for the binary packages we release.
384function clean_RPATH() {
385  if [ `uname -s` = "Darwin" ]; then
386    return
387  fi
388  local InstallPath="$1"
389  for Candidate in `find $InstallPath/{bin,lib} -type f`; do
390    if file $Candidate | grep ELF | egrep 'executable|shared object' > /dev/null 2>&1 ; then
391      rpath=`objdump -x $Candidate | grep 'RPATH' | sed -e's/^ *RPATH *//'`
392      if [ -n "$rpath" ]; then
393        newrpath=`echo $rpath | sed -e's/.*\(\$ORIGIN[^:]*\).*/\1/'`
394        chrpath -r $newrpath $Candidate 2>&1 > /dev/null 2>&1
395      fi
396    fi
397  done
398}
399
400# Create a package of the release binaries.
401function package_release() {
402    cwd=`pwd`
403    cd $BuildDir/Phase3/Release
404    mv llvmCore-$Release-$RC.install $Package
405    if [ "$use_gzip" = "yes" ]; then
406      tar cfz $BuildDir/$Package.tar.gz $Package
407    else
408      tar cfJ $BuildDir/$Package.tar.xz $Package
409    fi
410    mv $Package llvmCore-$Release-$RC.install
411    cd $cwd
412}
413
414set -e                          # Exit if any command fails
415
416if [ "$do_checkout" = "yes" ]; then
417    export_sources
418fi
419
420(
421Flavors="Release"
422if [ "$do_debug" = "yes" ]; then
423    Flavors="Debug $Flavors"
424fi
425if [ "$do_asserts" = "yes" ]; then
426    Flavors="$Flavors Release+Asserts"
427fi
428if [ "$do_64bit" = "yes" ]; then
429    Flavors="$Flavors Release-64"
430fi
431
432for Flavor in $Flavors ; do
433    echo ""
434    echo ""
435    echo "********************************************************************************"
436    echo "  Release:     $Release-$RC"
437    echo "  Build:       $Flavor"
438    echo "  System Info: "
439    echo "    `uname -a`"
440    echo "********************************************************************************"
441    echo ""
442
443    c_compiler="$CC"
444    cxx_compiler="$CXX"
445
446    llvmCore_phase1_objdir=$BuildDir/Phase1/$Flavor/llvmCore-$Release-$RC.obj
447    llvmCore_phase1_installdir=$BuildDir/Phase1/$Flavor/llvmCore-$Release-$RC.install
448    dragonegg_phase1_objdir=$BuildDir/Phase1/$Flavor/DragonEgg-$Release-$RC.obj
449
450    llvmCore_phase2_objdir=$BuildDir/Phase2/$Flavor/llvmCore-$Release-$RC.obj
451    llvmCore_phase2_installdir=$BuildDir/Phase2/$Flavor/llvmCore-$Release-$RC.install
452    llvmCore_de_phase2_objdir=$BuildDir/Phase2/$Flavor/llvmCore-DragonEgg-$Release-$RC.obj
453    llvmCore_de_phase2_installdir=$BuildDir/Phase2/$Flavor/llvmCore-DragonEgg-$Release-$RC.install
454    dragonegg_phase2_objdir=$BuildDir/Phase2/$Flavor/DragonEgg-$Release-$RC.obj
455
456    llvmCore_phase3_objdir=$BuildDir/Phase3/$Flavor/llvmCore-$Release-$RC.obj
457    llvmCore_phase3_installdir=$BuildDir/Phase3/$Flavor/llvmCore-$Release-$RC.install
458    llvmCore_de_phase3_objdir=$BuildDir/Phase3/$Flavor/llvmCore-DragonEgg-$Release-$RC.obj
459    llvmCore_de_phase3_installdir=$BuildDir/Phase3/$Flavor/llvmCore-DragonEgg-$Release-$RC.install
460    dragonegg_phase3_objdir=$BuildDir/Phase3/$Flavor/DragonEgg-$Release-$RC.obj
461
462    rm -rf $llvmCore_phase1_objdir
463    rm -rf $llvmCore_phase1_installdir
464    rm -rf $dragonegg_phase1_objdir
465
466    rm -rf $llvmCore_phase2_objdir
467    rm -rf $llvmCore_phase2_installdir
468    rm -rf $llvmCore_de_phase2_objdir
469    rm -rf $llvmCore_de_phase2_installdir
470    rm -rf $dragonegg_phase2_objdir
471
472    rm -rf $llvmCore_phase3_objdir
473    rm -rf $llvmCore_phase3_installdir
474    rm -rf $llvmCore_de_phase3_objdir
475    rm -rf $llvmCore_de_phase3_installdir
476    rm -rf $dragonegg_phase3_objdir
477
478    mkdir -p $llvmCore_phase1_objdir
479    mkdir -p $llvmCore_phase1_installdir
480    mkdir -p $dragonegg_phase1_objdir
481
482    mkdir -p $llvmCore_phase2_objdir
483    mkdir -p $llvmCore_phase2_installdir
484    mkdir -p $llvmCore_de_phase2_objdir
485    mkdir -p $llvmCore_de_phase2_installdir
486    mkdir -p $dragonegg_phase2_objdir
487
488    mkdir -p $llvmCore_phase3_objdir
489    mkdir -p $llvmCore_phase3_installdir
490    mkdir -p $llvmCore_de_phase3_objdir
491    mkdir -p $llvmCore_de_phase3_installdir
492    mkdir -p $dragonegg_phase3_objdir
493
494    ############################################################################
495    # Phase 1: Build llvmCore and clang
496    echo "# Phase 1: Building llvmCore"
497    configure_llvmCore 1 $Flavor \
498        $llvmCore_phase1_objdir $llvmCore_phase1_installdir
499    build_llvmCore 1 $Flavor \
500        $llvmCore_phase1_objdir
501    clean_RPATH $llvmCore_phase1_installdir
502
503    # Test clang
504    if [ "$do_clang" = "yes" ]; then
505        ########################################################################
506        # Phase 2: Build llvmCore with newly built clang from phase 1.
507        c_compiler=$llvmCore_phase1_installdir/bin/clang
508        cxx_compiler=$llvmCore_phase1_installdir/bin/clang++
509        echo "# Phase 2: Building llvmCore"
510        configure_llvmCore 2 $Flavor \
511            $llvmCore_phase2_objdir $llvmCore_phase2_installdir
512        build_llvmCore 2 $Flavor \
513            $llvmCore_phase2_objdir
514        clean_RPATH $llvmCore_phase2_installdir
515
516        ########################################################################
517        # Phase 3: Build llvmCore with newly built clang from phase 2.
518        c_compiler=$llvmCore_phase2_installdir/bin/clang
519        cxx_compiler=$llvmCore_phase2_installdir/bin/clang++
520        echo "# Phase 3: Building llvmCore"
521        configure_llvmCore 3 $Flavor \
522            $llvmCore_phase3_objdir $llvmCore_phase3_installdir
523        build_llvmCore 3 $Flavor \
524            $llvmCore_phase3_objdir
525        clean_RPATH $llvmCore_phase3_installdir
526
527        ########################################################################
528        # Testing: Test phase 3
529        echo "# Testing - built with clang"
530        test_llvmCore 3 $Flavor $llvmCore_phase3_objdir
531
532        ########################################################################
533        # Compare .o files between Phase2 and Phase3 and report which ones
534        # differ.
535        if [ "$do_compare" = "yes" ]; then
536            echo
537            echo "# Comparing Phase 2 and Phase 3 files"
538            for o in `find $llvmCore_phase2_objdir -name '*.o'` ; do
539                p3=`echo $o | sed -e 's,Phase2,Phase3,'`
540                if ! cmp --ignore-initial=16 $o $p3 > /dev/null 2>&1 ; then
541                    echo "file `basename $o` differs between phase 2 and phase 3"
542                fi
543            done
544        fi
545    fi
546
547    # Test dragonegg
548    if [ "$do_dragonegg" = "yes" ]; then
549        # Build dragonegg using the targeted gcc.  This isn't necessary, but
550        # helps avoid using broken versions of gcc (which are legion), tests
551        # that the targeted gcc is basically sane and is consistent with the
552        # later phases in which the targeted gcc + dragonegg are used.
553        c_compiler="$gcc_compiler"
554        cxx_compiler="$gxx_compiler"
555        build_dragonegg 1 $Flavor $llvmCore_phase1_installdir $dragonegg_phase1_objdir
556
557        ########################################################################
558        # Phase 2: Build llvmCore with newly built dragonegg from phase 1.
559        c_compiler="$gcc_compiler -fplugin=$dragonegg_phase1_objdir/dragonegg.so"
560        cxx_compiler="$gxx_compiler -fplugin=$dragonegg_phase1_objdir/dragonegg.so"
561        echo "# Phase 2: Building llvmCore with dragonegg"
562        configure_llvmCore 2 $Flavor \
563            $llvmCore_de_phase2_objdir $llvmCore_de_phase2_installdir
564        build_llvmCore 2 $Flavor \
565            $llvmCore_de_phase2_objdir
566        build_dragonegg 2 $Flavor $llvmCore_de_phase2_installdir $dragonegg_phase2_objdir
567        clean_RPATH $llvmCore_de_phase2_installdir
568
569        ########################################################################
570        # Phase 3: Build llvmCore with newly built dragonegg from phase 2.
571        c_compiler="$gcc_compiler -fplugin=$dragonegg_phase2_objdir/dragonegg.so"
572        cxx_compiler="$gxx_compiler -fplugin=$dragonegg_phase2_objdir/dragonegg.so"
573        echo "# Phase 3: Building llvmCore with dragonegg"
574        configure_llvmCore 3 $Flavor \
575            $llvmCore_de_phase3_objdir $llvmCore_de_phase3_installdir
576        build_llvmCore 3 $Flavor \
577            $llvmCore_de_phase3_objdir
578        build_dragonegg 3 $Flavor $llvmCore_de_phase3_installdir $dragonegg_phase3_objdir
579        clean_RPATH $llvmCore_de_phase3_installdir
580
581        ########################################################################
582        # Testing: Test phase 3
583        c_compiler="$gcc_compiler -fplugin=$dragonegg_phase3_objdir/dragonegg.so"
584        cxx_compiler="$gxx_compiler -fplugin=$dragonegg_phase3_objdir/dragonegg.so"
585        echo "# Testing - built with dragonegg"
586        test_llvmCore 3 $Flavor $llvmCore_de_phase3_objdir
587
588        ########################################################################
589        # Compare .o files between Phase2 and Phase3 and report which ones differ.
590        echo
591        echo "# Comparing Phase 2 and Phase 3 files"
592        for o in `find $llvmCore_de_phase2_objdir -name '*.o'` \
593          `find $dragonegg_phase2_objdir -name '*.o'` ; do
594            p3=`echo $o | sed -e 's,Phase2,Phase3,'`
595            if ! cmp --ignore-initial=16 $o $p3 > /dev/null 2>&1 ; then
596                echo "file `basename $o` differs between dragonegg phase 2 and phase 3"
597            fi
598        done
599    fi
600
601    # Otherwise just test the core.
602    if [ "$do_clang" != "yes" -a "$do_dragonegg" != "yes" ]; then
603        echo "# Testing - built with system compiler"
604        test_llvmCore 1 $Flavor $llvmCore_phase1_objdir
605    fi
606done
607) 2>&1 | tee $LogDir/testing.$Release-$RC.log
608
609package_release
610
611set +e
612
613# Woo hoo!
614echo "### Testing Finished ###"
615if [ "$use_gzip" = "yes" ]; then
616  echo "### Package: $Package.tar.gz"
617else
618  echo "### Package: $Package.tar.xz"
619fi
620echo "### Logs: $LogDir"
621exit 0
622