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
15System=`uname -s`
16if [ "$System" = "FreeBSD" ]; then
17    MAKE=gmake
18else
19    MAKE=make
20fi
21
22# Base SVN URL for the sources.
23Base_url="http://llvm.org/svn/llvm-project"
24
25Release=""
26Release_no_dot=""
27RC=""
28Triple=""
29use_gzip="no"
30do_checkout="yes"
31do_debug="no"
32do_asserts="no"
33do_compare="yes"
34do_rt="yes"
35do_libs="yes"
36do_libunwind="yes"
37do_test_suite="yes"
38do_openmp="yes"
39do_lldb="no"
40BuildDir="`pwd`"
41ExtraConfigureFlags=""
42ExportBranch=""
43
44function usage() {
45    echo "usage: `basename $0` -release X.Y.Z -rc NUM [OPTIONS]"
46    echo ""
47    echo " -release X.Y.Z       The release version 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 " -test-debug          Test the debug build. [default: no]"
55    echo " -test-asserts        Test with asserts on. [default: no]"
56    echo " -no-compare-files    Don't test that phase 2 and 3 files are identical."
57    echo " -use-gzip            Use gzip instead of xz."
58    echo " -configure-flags FLAGS  Extra flags to pass to the configure step."
59    echo " -svn-path DIR        Use the specified DIR instead of a release."
60    echo "                      For example -svn-path trunk or -svn-path branches/release_37"
61    echo " -no-rt               Disable check-out & build Compiler-RT"
62    echo " -no-libs             Disable check-out & build libcxx/libcxxabi/libunwind"
63    echo " -no-libunwind        Disable check-out & build libunwind"
64    echo " -no-test-suite       Disable check-out & build test-suite"
65    echo " -no-openmp           Disable check-out & build libomp"
66    echo " -lldb                Enable check-out & build lldb"
67    echo " -no-lldb             Disable check-out & build lldb (default)"
68}
69
70while [ $# -gt 0 ]; do
71    case $1 in
72        -release | --release )
73            shift
74            Release="$1"
75            Release_no_dot="`echo $1 | sed -e 's,\.,,g'`"
76            ;;
77        -rc | --rc | -RC | --RC )
78            shift
79            RC="rc$1"
80            ;;
81        -final | --final )
82            RC=final
83            ;;
84        -svn-path | --svn-path )
85            shift
86            Release="test"
87            Release_no_dot="test"
88            ExportBranch="$1"
89            RC="`echo $ExportBranch | sed -e 's,/,_,g'`"
90            echo "WARNING: Using the branch $ExportBranch instead of a release tag"
91            echo "         This is intended to aid new packagers in trialing "
92            echo "         builds without requiring a tag to be created first"
93            ;;
94        -triple | --triple )
95            shift
96            Triple="$1"
97            ;;
98        -configure-flags | --configure-flags )
99            shift
100            ExtraConfigureFlags="$1"
101            ;;
102        -j* )
103            NumJobs="`echo $1 | sed -e 's,-j\([0-9]*\),\1,g'`"
104            if [ -z "$NumJobs" ]; then
105                shift
106                NumJobs="$1"
107            fi
108            ;;
109        -build-dir | --build-dir | -builddir | --builddir )
110            shift
111            BuildDir="$1"
112            ;;
113        -no-checkout | --no-checkout )
114            do_checkout="no"
115            ;;
116        -test-debug | --test-debug )
117            do_debug="yes"
118            ;;
119        -test-asserts | --test-asserts )
120            do_asserts="yes"
121            ;;
122        -no-compare-files | --no-compare-files )
123            do_compare="no"
124            ;;
125        -use-gzip | --use-gzip )
126            use_gzip="yes"
127            ;;
128        -no-rt )
129            do_rt="no"
130            ;;
131        -no-libs )
132            do_libs="no"
133            ;;
134        -no-libunwind )
135            do_libunwind="no"
136            ;;
137        -no-test-suite )
138            do_test_suite="no"
139            ;;
140        -no-openmp )
141            do_openmp="no"
142            ;;
143        -lldb )
144            do_lldb="yes"
145            ;;
146        -no-lldb )
147            do_lldb="no"
148            ;;
149        -help | --help | -h | --h | -\? )
150            usage
151            exit 0
152            ;;
153        * )
154            echo "unknown option: $1"
155            usage
156            exit 1
157            ;;
158    esac
159    shift
160done
161
162if [ "$do_test_suite" = "yes" ]; then
163  # See llvm.org/PR26146.
164  echo Skipping test-suite build when using CMake.
165  echo It will still be exported.
166  do_test_suite="export-only"
167fi
168
169# Check required arguments.
170if [ -z "$Release" ]; then
171    echo "error: no release number specified"
172    exit 1
173fi
174if [ -z "$RC" ]; then
175    echo "error: no release candidate number specified"
176    exit 1
177fi
178if [ -z "$ExportBranch" ]; then
179    ExportBranch="tags/RELEASE_$Release_no_dot/$RC"
180fi
181if [ -z "$Triple" ]; then
182    echo "error: no target triple specified"
183    exit 1
184fi
185
186# Figure out how many make processes to run.
187if [ -z "$NumJobs" ]; then
188    NumJobs=`sysctl -n hw.activecpu 2> /dev/null || true`
189fi
190if [ -z "$NumJobs" ]; then
191    NumJobs=`sysctl -n hw.ncpu 2> /dev/null || true`
192fi
193if [ -z "$NumJobs" ]; then
194    NumJobs=`grep -c processor /proc/cpuinfo 2> /dev/null || true`
195fi
196if [ -z "$NumJobs" ]; then
197    NumJobs=3
198fi
199
200# Projects list
201projects="llvm cfe clang-tools-extra"
202if [ $do_rt = "yes" ]; then
203  projects="$projects compiler-rt"
204fi
205if [ $do_libs = "yes" ]; then
206  projects="$projects libcxx libcxxabi"
207  if [ $do_libunwind = "yes" ]; then
208    projects="$projects libunwind"
209  fi
210fi
211case $do_test_suite in
212  yes|export-only)
213    projects="$projects test-suite"
214    ;;
215esac
216if [ $do_openmp = "yes" ]; then
217  projects="$projects openmp"
218fi
219if [ $do_lldb = "yes" ]; then
220  projects="$projects lldb"
221fi
222
223# Go to the build directory (may be different from CWD)
224BuildDir=$BuildDir/$RC
225mkdir -p $BuildDir
226cd $BuildDir
227
228# Location of log files.
229LogDir=$BuildDir/logs
230mkdir -p $LogDir
231
232# Final package name.
233Package=clang+llvm-$Release
234if [ $RC != "final" ]; then
235  Package=$Package-$RC
236fi
237Package=$Package-$Triple
238
239# Errors to be highlighted at the end are written to this file.
240echo -n > $LogDir/deferred_errors.log
241
242function deferred_error() {
243  Phase="$1"
244  Flavor="$2"
245  Msg="$3"
246  echo "[${Flavor} Phase${Phase}] ${Msg}" | tee -a $LogDir/deferred_errors.log
247}
248
249# Make sure that a required program is available
250function check_program_exists() {
251  local program="$1"
252  if ! type -P $program > /dev/null 2>&1 ; then
253    echo "program '$1' not found !"
254    exit 1
255  fi
256}
257
258if [ "$System" != "Darwin" ]; then
259  check_program_exists 'chrpath'
260  check_program_exists 'file'
261  check_program_exists 'objdump'
262fi
263
264# Make sure that the URLs are valid.
265function check_valid_urls() {
266    for proj in $projects ; do
267        echo "# Validating $proj SVN URL"
268
269        if ! svn ls $Base_url/$proj/$ExportBranch > /dev/null 2>&1 ; then
270            echo "$proj does not have a $ExportBranch branch/tag!"
271            exit 1
272        fi
273    done
274}
275
276# Export sources to the build directory.
277function export_sources() {
278    check_valid_urls
279
280    for proj in $projects ; do
281        case $proj in
282        llvm)
283            projsrc=$proj.src
284            ;;
285        cfe)
286            projsrc=llvm.src/tools/clang
287            ;;
288        lldb)
289            projsrc=llvm.src/tools/$proj
290            ;;
291        clang-tools-extra)
292            projsrc=llvm.src/tools/clang/tools/extra
293            ;;
294        compiler-rt|libcxx|libcxxabi|libunwind|openmp)
295            projsrc=llvm.src/projects/$proj
296            ;;
297        test-suite)
298            if [ $do_test_suite = 'yes' ]; then
299              projsrc=llvm.src/projects/$proj
300            else
301              projsrc=$proj.src
302            fi
303            ;;
304        *)
305            echo "error: unknown project $proj"
306            exit 1
307            ;;
308        esac
309
310        if [ -d $projsrc ]; then
311          echo "# Reusing $proj $Release-$RC sources in $projsrc"
312          continue
313        fi
314        echo "# Exporting $proj $Release-$RC sources to $projsrc"
315        if ! svn export -q $Base_url/$proj/$ExportBranch $projsrc ; then
316            echo "error: failed to export $proj project"
317            exit 1
318        fi
319    done
320
321    cd $BuildDir
322}
323
324function configure_llvmCore() {
325    Phase="$1"
326    Flavor="$2"
327    ObjDir="$3"
328
329    case $Flavor in
330        Release )
331            BuildType="Release"
332            Assertions="OFF"
333            ;;
334        Release+Asserts )
335            BuildType="Release"
336            Assertions="ON"
337            ;;
338        Debug )
339            BuildType="Debug"
340            Assertions="ON"
341            ;;
342        * )
343            echo "# Invalid flavor '$Flavor'"
344            echo ""
345            return
346            ;;
347    esac
348
349    echo "# Using C compiler: $c_compiler"
350    echo "# Using C++ compiler: $cxx_compiler"
351
352    cd $ObjDir
353    echo "# Configuring llvm $Release-$RC $Flavor"
354
355    echo "#" env CC="$c_compiler" CXX="$cxx_compiler" \
356        cmake -G "Unix Makefiles" \
357        -DCMAKE_BUILD_TYPE=$BuildType -DLLVM_ENABLE_ASSERTIONS=$Assertions \
358        $ExtraConfigureFlags $BuildDir/llvm.src \
359        2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
360    env CC="$c_compiler" CXX="$cxx_compiler" \
361        cmake -G "Unix Makefiles" \
362        -DCMAKE_BUILD_TYPE=$BuildType -DLLVM_ENABLE_ASSERTIONS=$Assertions \
363        $ExtraConfigureFlags $BuildDir/llvm.src \
364        2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
365
366    cd $BuildDir
367}
368
369function build_llvmCore() {
370    Phase="$1"
371    Flavor="$2"
372    ObjDir="$3"
373    DestDir="$4"
374
375    cd $ObjDir
376    echo "# Compiling llvm $Release-$RC $Flavor"
377    echo "# ${MAKE} -j $NumJobs VERBOSE=1"
378    ${MAKE} -j $NumJobs VERBOSE=1 \
379        2>&1 | tee $LogDir/llvm.make-Phase$Phase-$Flavor.log
380
381    echo "# Installing llvm $Release-$RC $Flavor"
382    echo "# ${MAKE} install"
383    ${MAKE} install \
384        DESTDIR="${DestDir}" \
385        2>&1 | tee $LogDir/llvm.install-Phase$Phase-$Flavor.log
386    cd $BuildDir
387}
388
389function test_llvmCore() {
390    Phase="$1"
391    Flavor="$2"
392    ObjDir="$3"
393
394    cd $ObjDir
395    if ! ( ${MAKE} -j $NumJobs -k check-all \
396        2>&1 | tee $LogDir/llvm.check-Phase$Phase-$Flavor.log ) ; then
397      deferred_error $Phase $Flavor "check-all failed"
398    fi
399
400    cd $BuildDir
401}
402
403# Clean RPATH. Libtool adds the build directory to the search path, which is
404# not necessary --- and even harmful --- for the binary packages we release.
405function clean_RPATH() {
406  if [ "$System" = "Darwin" ]; then
407    return
408  fi
409  local InstallPath="$1"
410  for Candidate in `find $InstallPath/{bin,lib} -type f`; do
411    if file $Candidate | grep ELF | egrep 'executable|shared object' > /dev/null 2>&1 ; then
412      if rpath=`objdump -x $Candidate | grep 'RPATH'` ; then
413        rpath=`echo $rpath | sed -e's/^ *RPATH *//'`
414        if [ -n "$rpath" ]; then
415          newrpath=`echo $rpath | sed -e's/.*\(\$ORIGIN[^:]*\).*/\1/'`
416          chrpath -r $newrpath $Candidate 2>&1 > /dev/null 2>&1
417        fi
418      fi
419    fi
420  done
421}
422
423# Create a package of the release binaries.
424function package_release() {
425    cwd=`pwd`
426    cd $BuildDir/Phase3/Release
427    mv llvmCore-$Release-$RC.install/usr/local $Package
428    if [ "$use_gzip" = "yes" ]; then
429      tar cfz $BuildDir/$Package.tar.gz $Package
430    else
431      tar cfJ $BuildDir/$Package.tar.xz $Package
432    fi
433    mv $Package llvmCore-$Release-$RC.install/usr/local
434    cd $cwd
435}
436
437# Exit if any command fails
438# Note: pipefail is necessary for running build commands through
439# a pipe (i.e. it changes the output of ``false | tee /dev/null ; echo $?``)
440set -e
441set -o pipefail
442
443if [ "$do_checkout" = "yes" ]; then
444    export_sources
445fi
446
447(
448Flavors="Release"
449if [ "$do_debug" = "yes" ]; then
450    Flavors="Debug $Flavors"
451fi
452if [ "$do_asserts" = "yes" ]; then
453    Flavors="$Flavors Release+Asserts"
454fi
455
456for Flavor in $Flavors ; do
457    echo ""
458    echo ""
459    echo "********************************************************************************"
460    echo "  Release:     $Release-$RC"
461    echo "  Build:       $Flavor"
462    echo "  System Info: "
463    echo "    `uname -a`"
464    echo "********************************************************************************"
465    echo ""
466
467    c_compiler="$CC"
468    cxx_compiler="$CXX"
469    llvmCore_phase1_objdir=$BuildDir/Phase1/$Flavor/llvmCore-$Release-$RC.obj
470    llvmCore_phase1_destdir=$BuildDir/Phase1/$Flavor/llvmCore-$Release-$RC.install
471
472    llvmCore_phase2_objdir=$BuildDir/Phase2/$Flavor/llvmCore-$Release-$RC.obj
473    llvmCore_phase2_destdir=$BuildDir/Phase2/$Flavor/llvmCore-$Release-$RC.install
474
475    llvmCore_phase3_objdir=$BuildDir/Phase3/$Flavor/llvmCore-$Release-$RC.obj
476    llvmCore_phase3_destdir=$BuildDir/Phase3/$Flavor/llvmCore-$Release-$RC.install
477
478    rm -rf $llvmCore_phase1_objdir
479    rm -rf $llvmCore_phase1_destdir
480
481    rm -rf $llvmCore_phase2_objdir
482    rm -rf $llvmCore_phase2_destdir
483
484    rm -rf $llvmCore_phase3_objdir
485    rm -rf $llvmCore_phase3_destdir
486
487    mkdir -p $llvmCore_phase1_objdir
488    mkdir -p $llvmCore_phase1_destdir
489
490    mkdir -p $llvmCore_phase2_objdir
491    mkdir -p $llvmCore_phase2_destdir
492
493    mkdir -p $llvmCore_phase3_objdir
494    mkdir -p $llvmCore_phase3_destdir
495
496    ############################################################################
497    # Phase 1: Build llvmCore and clang
498    echo "# Phase 1: Building llvmCore"
499    configure_llvmCore 1 $Flavor $llvmCore_phase1_objdir
500    build_llvmCore 1 $Flavor \
501        $llvmCore_phase1_objdir $llvmCore_phase1_destdir
502    clean_RPATH $llvmCore_phase1_destdir/usr/local
503
504    ########################################################################
505    # Phase 2: Build llvmCore with newly built clang from phase 1.
506    c_compiler=$llvmCore_phase1_destdir/usr/local/bin/clang
507    cxx_compiler=$llvmCore_phase1_destdir/usr/local/bin/clang++
508    echo "# Phase 2: Building llvmCore"
509    configure_llvmCore 2 $Flavor $llvmCore_phase2_objdir
510    build_llvmCore 2 $Flavor \
511        $llvmCore_phase2_objdir $llvmCore_phase2_destdir
512    clean_RPATH $llvmCore_phase2_destdir/usr/local
513
514    ########################################################################
515    # Phase 3: Build llvmCore with newly built clang from phase 2.
516    c_compiler=$llvmCore_phase2_destdir/usr/local/bin/clang
517    cxx_compiler=$llvmCore_phase2_destdir/usr/local/bin/clang++
518    echo "# Phase 3: Building llvmCore"
519    configure_llvmCore 3 $Flavor $llvmCore_phase3_objdir
520    build_llvmCore 3 $Flavor \
521        $llvmCore_phase3_objdir $llvmCore_phase3_destdir
522    clean_RPATH $llvmCore_phase3_destdir/usr/local
523
524    ########################################################################
525    # Testing: Test phase 3
526    echo "# Testing - built with clang"
527    test_llvmCore 3 $Flavor $llvmCore_phase3_objdir
528
529    ########################################################################
530    # Compare .o files between Phase2 and Phase3 and report which ones
531    # differ.
532    if [ "$do_compare" = "yes" ]; then
533        echo
534        echo "# Comparing Phase 2 and Phase 3 files"
535        for p2 in `find $llvmCore_phase2_objdir -name '*.o'` ; do
536            p3=`echo $p2 | sed -e 's,Phase2,Phase3,'`
537            # Substitute 'Phase2' for 'Phase3' in the Phase 2 object file in
538            # case there are build paths in the debug info. On some systems,
539            # sed adds a newline to the output, so pass $p3 through sed too.
540            if ! cmp -s \
541                <(env LC_CTYPE=C sed -e 's,Phase2,Phase3,g' $p2) \
542                <(env LC_CTYPE=C sed -e '' $p3) 16 16; then
543                echo "file `basename $p2` differs between phase 2 and phase 3"
544            fi
545        done
546    fi
547done
548
549) 2>&1 | tee $LogDir/testing.$Release-$RC.log
550
551package_release
552
553set +e
554
555# Woo hoo!
556echo "### Testing Finished ###"
557if [ "$use_gzip" = "yes" ]; then
558  echo "### Package: $Package.tar.gz"
559else
560  echo "### Package: $Package.tar.xz"
561fi
562echo "### Logs: $LogDir"
563
564echo "### Errors:"
565if [ -s "$LogDir/deferred_errors.log" ]; then
566  cat "$LogDir/deferred_errors.log"
567  exit 1
568else
569  echo "None."
570fi
571
572exit 0
573