Revert accidental changes from r258805
[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   # See llvm.org/PR26146.
159   echo Skipping test-suite when using CMake.
160   do_test_suite="no"
161 fi
162
163 # Check required arguments.
164 if [ -z "$Release" ]; then
165     echo "error: no release number specified"
166     exit 1
167 fi
168 if [ -z "$RC" ]; then
169     echo "error: no release candidate number specified"
170     exit 1
171 fi
172 if [ -z "$ExportBranch" ]; then
173     ExportBranch="tags/RELEASE_$Release_no_dot/$RC"
174 fi
175 if [ -z "$Triple" ]; then
176     echo "error: no target triple specified"
177     exit 1
178 fi
179
180 # Figure out how many make processes to run.
181 if [ -z "$NumJobs" ]; then
182     NumJobs=`sysctl -n hw.activecpu 2> /dev/null || true`
183 fi
184 if [ -z "$NumJobs" ]; then
185     NumJobs=`sysctl -n hw.ncpu 2> /dev/null || true`
186 fi
187 if [ -z "$NumJobs" ]; then
188     NumJobs=`grep -c processor /proc/cpuinfo 2> /dev/null || true`
189 fi
190 if [ -z "$NumJobs" ]; then
191     NumJobs=3
192 fi
193
194 # Projects list
195 projects="llvm cfe clang-tools-extra"
196 if [ $do_rt = "yes" ]; then
197   projects="$projects compiler-rt"
198 fi
199 if [ $do_libs = "yes" ]; then
200   projects="$projects libcxx libcxxabi"
201   if [ $do_libunwind = "yes" ]; then
202     projects="$projects libunwind"
203   fi
204 fi
205 if [ $do_test_suite = "yes" ]; then
206   projects="$projects test-suite"
207 fi
208 if [ $do_openmp = "yes" ]; then
209   projects="$projects openmp"
210 fi
211
212 # Go to the build directory (may be different from CWD)
213 BuildDir=$BuildDir/$RC
214 mkdir -p $BuildDir
215 cd $BuildDir
216
217 # Location of log files.
218 LogDir=$BuildDir/logs
219 mkdir -p $LogDir
220
221 # Final package name.
222 Package=clang+llvm-$Release
223 if [ $RC != "final" ]; then
224   Package=$Package-$RC
225 fi
226 Package=$Package-$Triple
227
228 # Errors to be highlighted at the end are written to this file.
229 echo -n > $LogDir/deferred_errors.log
230
231 function deferred_error() {
232   Phase="$1"
233   Flavor="$2"
234   Msg="$3"
235   echo "[${Flavor} Phase${Phase}] ${Msg}" | tee -a $LogDir/deferred_errors.log
236 }
237
238 # Make sure that a required program is available
239 function check_program_exists() {
240   local program="$1"
241   if ! type -P $program > /dev/null 2>&1 ; then
242     echo "program '$1' not found !"
243     exit 1
244   fi
245 }
246
247 if [ `uname -s` != "Darwin" ]; then
248   check_program_exists 'chrpath'
249   check_program_exists 'file'
250   check_program_exists 'objdump'
251 fi
252
253 # Make sure that the URLs are valid.
254 function check_valid_urls() {
255     for proj in $projects ; do
256         echo "# Validating $proj SVN URL"
257
258         if ! svn ls $Base_url/$proj/$ExportBranch > /dev/null 2>&1 ; then
259             echo "$proj does not have a $ExportBranch branch/tag!"
260             exit 1
261         fi
262     done
263 }
264
265 # Export sources to the build directory.
266 function export_sources() {
267     check_valid_urls
268
269     for proj in $projects ; do
270         case $proj in
271         llvm)
272             projsrc=$proj.src
273             ;;
274         cfe)
275             projsrc=llvm.src/tools/clang
276             ;;
277         clang-tools-extra)
278             projsrc=llvm.src/tools/clang/tools/extra
279             ;;
280         compiler-rt|libcxx|libcxxabi|libunwind|openmp|test-suite)
281             projsrc=llvm.src/projects/$proj
282             ;;
283         *)
284             echo "error: unknown project $proj"
285             exit 1
286             ;;
287         esac
288
289         if [ -d $projsrc ]; then
290           echo "# Reusing $proj $Release-$RC sources in $projsrc"
291           continue
292         fi
293         echo "# Exporting $proj $Release-$RC sources to $projsrc"
294         if ! svn export -q $Base_url/$proj/$ExportBranch $projsrc ; then
295             echo "error: failed to export $proj project"
296             exit 1
297         fi
298     done
299
300     cd $BuildDir
301 }
302
303 function configure_llvmCore() {
304     Phase="$1"
305     Flavor="$2"
306     ObjDir="$3"
307
308     case $Flavor in
309         Release )
310             BuildType="Release"
311             Assertions="OFF"
312             ConfigureFlags="--enable-optimized --disable-assertions"
313             ;;
314         Release+Asserts )
315             BuildType="Release"
316             Assertions="ON"
317             ConfigureFlags="--enable-optimized --enable-assertions"
318             ;;
319         Debug )
320             BuildType="Debug"
321             Assertions="ON"
322             ConfigureFlags="--disable-optimized --enable-assertions"
323             ;;
324         * )
325             echo "# Invalid flavor '$Flavor'"
326             echo ""
327             return
328             ;;
329     esac
330
331     echo "# Using C compiler: $c_compiler"
332     echo "# Using C++ compiler: $cxx_compiler"
333
334     cd $ObjDir
335     echo "# Configuring llvm $Release-$RC $Flavor"
336
337     if [ "$use_autoconf" = "yes" ]; then
338         echo "#" env CC="$c_compiler" CXX="$cxx_compiler" \
339             $BuildDir/llvm.src/configure \
340             $ConfigureFlags --disable-timestamps $ExtraConfigureFlags \
341             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
342         env CC="$c_compiler" CXX="$cxx_compiler" \
343             $BuildDir/llvm.src/configure \
344             $ConfigureFlags --disable-timestamps $ExtraConfigureFlags \
345             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
346     else
347         echo "#" env CC="$c_compiler" CXX="$cxx_compiler" \
348             cmake -G "Unix Makefiles" \
349             -DCMAKE_BUILD_TYPE=$BuildType -DLLVM_ENABLE_ASSERTIONS=$Assertions \
350             -DLLVM_ENABLE_TIMESTAMPS=OFF -DLLVM_CONFIGTIME="(timestamp not enabled)" \
351             $ExtraConfigureFlags $BuildDir/llvm.src \
352             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
353         env CC="$c_compiler" CXX="$cxx_compiler" \
354             cmake -G "Unix Makefiles" \
355             -DCMAKE_BUILD_TYPE=$BuildType -DLLVM_ENABLE_ASSERTIONS=$Assertions \
356             -DLLVM_ENABLE_TIMESTAMPS=OFF -DLLVM_CONFIGTIME="(timestamp not enabled)" \
357             $ExtraConfigureFlags $BuildDir/llvm.src \
358             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
359     fi
360
361     cd $BuildDir
362 }
363
364 function build_llvmCore() {
365     Phase="$1"
366     Flavor="$2"
367     ObjDir="$3"
368     DestDir="$4"
369
370     cd $ObjDir
371     echo "# Compiling llvm $Release-$RC $Flavor"
372     echo "# ${MAKE} -j $NumJobs VERBOSE=1"
373     ${MAKE} -j $NumJobs VERBOSE=1 \
374         2>&1 | tee $LogDir/llvm.make-Phase$Phase-$Flavor.log
375
376     echo "# Installing llvm $Release-$RC $Flavor"
377     echo "# ${MAKE} install"
378     ${MAKE} install \
379         DESTDIR="${DestDir}" \
380         2>&1 | tee $LogDir/llvm.install-Phase$Phase-$Flavor.log
381     cd $BuildDir
382 }
383
384 function test_llvmCore() {
385     Phase="$1"
386     Flavor="$2"
387     ObjDir="$3"
388
389     cd $ObjDir
390     if ! ( ${MAKE} -j $NumJobs -k check-all \
391         2>&1 | tee $LogDir/llvm.check-Phase$Phase-$Flavor.log ) ; then
392       deferred_error $Phase $Flavor "check-all failed"
393     fi
394
395     if [ "$use_autoconf" = "yes" ]; then
396         # In the cmake build, unit tests are run as part of check-all.
397         if ! ( ${MAKE} -k unittests 2>&1 | \
398             tee $LogDir/llvm.unittests-Phase$Phase-$Flavor.log ) ; then
399           deferred_error $Phase $Flavor "unittests failed"
400         fi
401     fi
402
403     cd $BuildDir
404 }
405
406 # Clean RPATH. Libtool adds the build directory to the search path, which is
407 # not necessary --- and even harmful --- for the binary packages we release.
408 function clean_RPATH() {
409   if [ `uname -s` = "Darwin" ]; then
410     return
411   fi
412   local InstallPath="$1"
413   for Candidate in `find $InstallPath/{bin,lib} -type f`; do
414     if file $Candidate | grep ELF | egrep 'executable|shared object' > /dev/null 2>&1 ; then
415       if rpath=`objdump -x $Candidate | grep 'RPATH'` ; then
416         rpath=`echo $rpath | sed -e's/^ *RPATH *//'`
417         if [ -n "$rpath" ]; then
418           newrpath=`echo $rpath | sed -e's/.*\(\$ORIGIN[^:]*\).*/\1/'`
419           chrpath -r $newrpath $Candidate 2>&1 > /dev/null 2>&1
420         fi
421       fi
422     fi
423   done
424 }
425
426 # Create a package of the release binaries.
427 function package_release() {
428     cwd=`pwd`
429     cd $BuildDir/Phase3/Release
430     mv llvmCore-$Release-$RC.install/usr/local $Package
431     if [ "$use_gzip" = "yes" ]; then
432       tar cfz $BuildDir/$Package.tar.gz $Package
433     else
434       tar cfJ $BuildDir/$Package.tar.xz $Package
435     fi
436     mv $Package llvmCore-$Release-$RC.install/usr/local
437     cd $cwd
438 }
439
440 # Exit if any command fails
441 # Note: pipefail is necessary for running build commands through
442 # a pipe (i.e. it changes the output of ``false | tee /dev/null ; echo $?``)
443 set -e
444 set -o pipefail
445
446 if [ "$do_checkout" = "yes" ]; then
447     export_sources
448 fi
449
450 (
451 Flavors="Release"
452 if [ "$do_debug" = "yes" ]; then
453     Flavors="Debug $Flavors"
454 fi
455 if [ "$do_asserts" = "yes" ]; then
456     Flavors="$Flavors Release+Asserts"
457 fi
458
459 for Flavor in $Flavors ; do
460     echo ""
461     echo ""
462     echo "********************************************************************************"
463     echo "  Release:     $Release-$RC"
464     echo "  Build:       $Flavor"
465     echo "  System Info: "
466     echo "    `uname -a`"
467     echo "********************************************************************************"
468     echo ""
469
470     c_compiler="$CC"
471     cxx_compiler="$CXX"
472     llvmCore_phase1_objdir=$BuildDir/Phase1/$Flavor/llvmCore-$Release-$RC.obj
473     llvmCore_phase1_destdir=$BuildDir/Phase1/$Flavor/llvmCore-$Release-$RC.install
474
475     llvmCore_phase2_objdir=$BuildDir/Phase2/$Flavor/llvmCore-$Release-$RC.obj
476     llvmCore_phase2_destdir=$BuildDir/Phase2/$Flavor/llvmCore-$Release-$RC.install
477
478     llvmCore_phase3_objdir=$BuildDir/Phase3/$Flavor/llvmCore-$Release-$RC.obj
479     llvmCore_phase3_destdir=$BuildDir/Phase3/$Flavor/llvmCore-$Release-$RC.install
480
481     rm -rf $llvmCore_phase1_objdir
482     rm -rf $llvmCore_phase1_destdir
483
484     rm -rf $llvmCore_phase2_objdir
485     rm -rf $llvmCore_phase2_destdir
486
487     rm -rf $llvmCore_phase3_objdir
488     rm -rf $llvmCore_phase3_destdir
489
490     mkdir -p $llvmCore_phase1_objdir
491     mkdir -p $llvmCore_phase1_destdir
492
493     mkdir -p $llvmCore_phase2_objdir
494     mkdir -p $llvmCore_phase2_destdir
495
496     mkdir -p $llvmCore_phase3_objdir
497     mkdir -p $llvmCore_phase3_destdir
498
499     ############################################################################
500     # Phase 1: Build llvmCore and clang
501     echo "# Phase 1: Building llvmCore"
502     configure_llvmCore 1 $Flavor $llvmCore_phase1_objdir
503     build_llvmCore 1 $Flavor \
504         $llvmCore_phase1_objdir $llvmCore_phase1_destdir
505     clean_RPATH $llvmCore_phase1_destdir/usr/local
506
507     ########################################################################
508     # Phase 2: Build llvmCore with newly built clang from phase 1.
509     c_compiler=$llvmCore_phase1_destdir/usr/local/bin/clang
510     cxx_compiler=$llvmCore_phase1_destdir/usr/local/bin/clang++
511     echo "# Phase 2: Building llvmCore"
512     configure_llvmCore 2 $Flavor $llvmCore_phase2_objdir
513     build_llvmCore 2 $Flavor \
514         $llvmCore_phase2_objdir $llvmCore_phase2_destdir
515     clean_RPATH $llvmCore_phase2_destdir/usr/local
516
517     ########################################################################
518     # Phase 3: Build llvmCore with newly built clang from phase 2.
519     c_compiler=$llvmCore_phase2_destdir/usr/local/bin/clang
520     cxx_compiler=$llvmCore_phase2_destdir/usr/local/bin/clang++
521     echo "# Phase 3: Building llvmCore"
522     configure_llvmCore 3 $Flavor $llvmCore_phase3_objdir
523     build_llvmCore 3 $Flavor \
524         $llvmCore_phase3_objdir $llvmCore_phase3_destdir
525     clean_RPATH $llvmCore_phase3_destdir/usr/local
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 p2 in `find $llvmCore_phase2_objdir -name '*.o'` ; do
539             p3=`echo $p2 | sed -e 's,Phase2,Phase3,'`
540             # Substitute 'Phase2' for 'Phase3' in the Phase 2 object file in
541             # case there are build paths in the debug info. On some systems,
542             # sed adds a newline to the output, so pass $p3 through sed too.
543             if ! cmp -s <(sed -e 's,Phase2,Phase3,g' $p2) <(sed -e '' $p3) \
544                     16 16 ; then
545                 echo "file `basename $p2` differs between phase 2 and phase 3"
546             fi
547         done
548     fi
549 done
550
551 ) 2>&1 | tee $LogDir/testing.$Release-$RC.log
552
553 package_release
554
555 set +e
556
557 # Woo hoo!
558 echo "### Testing Finished ###"
559 if [ "$use_gzip" = "yes" ]; then
560   echo "### Package: $Package.tar.gz"
561 else
562   echo "### Package: $Package.tar.xz"
563 fi
564 echo "### Logs: $LogDir"
565
566 echo "### Errors:"
567 if [ -s "$LogDir/deferred_errors.log" ]; then
568   cat "$LogDir/deferred_errors.log"
569   exit 1
570 else
571   echo "None."
572 fi
573
574 exit 0