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