Merging r257791:
[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 if [ `uname -s` = "Darwin" ]; then
69   # compiler-rt doesn't yet build with CMake on Darwin.
70   use_autoconf="yes"
71 fi
72
73 while [ $# -gt 0 ]; do
74     case $1 in
75         -release | --release )
76             shift
77             Release="$1"
78             Release_no_dot="`echo $1 | sed -e 's,\.,,g'`"
79             ;;
80         -rc | --rc | -RC | --RC )
81             shift
82             RC="rc$1"
83             ;;
84         -final | --final )
85             RC=final
86             ;;
87         -svn-path | --svn-path )
88             shift
89             Release="test"
90             Release_no_dot="test"
91             ExportBranch="$1"
92             RC="`echo $ExportBranch | sed -e 's,/,_,g'`"
93             echo "WARNING: Using the branch $ExportBranch instead of a release tag"
94             echo "         This is intended to aid new packagers in trialing "
95             echo "         builds without requiring a tag to be created first"
96             ;;
97         -triple | --triple )
98             shift
99             Triple="$1"
100             ;;
101         -configure-flags | --configure-flags )
102             shift
103             ExtraConfigureFlags="$1"
104             ;;
105         -j* )
106             NumJobs="`echo $1 | sed -e 's,-j\([0-9]*\),\1,g'`"
107             if [ -z "$NumJobs" ]; then
108                 shift
109                 NumJobs="$1"
110             fi
111             ;;
112         -build-dir | --build-dir | -builddir | --builddir )
113             shift
114             BuildDir="$1"
115             ;;
116         -no-checkout | --no-checkout )
117             do_checkout="no"
118             ;;
119         -test-debug | --test-debug )
120             do_debug="yes"
121             ;;
122         -test-asserts | --test-asserts )
123             do_asserts="yes"
124             ;;
125         -no-compare-files | --no-compare-files )
126             do_compare="no"
127             ;;
128         -use-gzip | --use-gzip )
129             use_gzip="yes"
130             ;;
131         -use-autoconf | --use-autoconf )
132             use_autoconf="yes"
133             ;;
134         -no-rt )
135             do_rt="no"
136             ;;
137         -no-libs )
138             do_libs="no"
139             ;;
140         -no-libunwind )
141             do_libunwind="no"
142             ;;
143         -no-test-suite )
144             do_test_suite="no"
145             ;;
146         -no-openmp )
147             do_openmp="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
160 done
161
162 if [ "$use_autoconf" = "no" ]; then
163   # See llvm.org/PR26146.
164   echo Skipping test-suite when using CMake.
165   do_test_suite="no"
166 fi
167
168 # Check required arguments.
169 if [ -z "$Release" ]; then
170     echo "error: no release number specified"
171     exit 1
172 fi
173 if [ -z "$RC" ]; then
174     echo "error: no release candidate number specified"
175     exit 1
176 fi
177 if [ -z "$ExportBranch" ]; then
178     ExportBranch="tags/RELEASE_$Release_no_dot/$RC"
179 fi
180 if [ -z "$Triple" ]; then
181     echo "error: no target triple specified"
182     exit 1
183 fi
184
185 # Figure out how many make processes to run.
186 if [ -z "$NumJobs" ]; then
187     NumJobs=`sysctl -n hw.activecpu 2> /dev/null || true`
188 fi
189 if [ -z "$NumJobs" ]; then
190     NumJobs=`sysctl -n hw.ncpu 2> /dev/null || true`
191 fi
192 if [ -z "$NumJobs" ]; then
193     NumJobs=`grep -c processor /proc/cpuinfo 2> /dev/null || true`
194 fi
195 if [ -z "$NumJobs" ]; then
196     NumJobs=3
197 fi
198
199 # Projects list
200 projects="llvm cfe clang-tools-extra"
201 if [ $do_rt = "yes" ]; then
202   projects="$projects compiler-rt"
203 fi
204 if [ $do_libs = "yes" ]; then
205   projects="$projects libcxx libcxxabi"
206   if [ $do_libunwind = "yes" ]; then
207     projects="$projects libunwind"
208   fi
209 fi
210 if [ $do_test_suite = "yes" ]; then
211   projects="$projects test-suite"
212 fi
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         if [ -d $proj.src ]; then
276           echo "# Reusing $proj $Release-$RC sources"
277           continue
278         fi
279         echo "# Exporting $proj $Release-$RC sources"
280         if ! svn export -q $Base_url/$proj/$ExportBranch $proj.src ; then
281             echo "error: failed to export $proj project"
282             exit 1
283         fi
284     done
285
286     echo "# Creating symlinks"
287     cd $BuildDir/llvm.src/tools
288     if [ ! -h clang ]; then
289         ln -s ../../cfe.src clang
290     fi
291     cd $BuildDir/llvm.src/tools/clang/tools
292     if [ ! -h extra ]; then
293         ln -s ../../../../clang-tools-extra.src extra
294     fi
295     cd $BuildDir/llvm.src/projects
296     if [ -d $BuildDir/test-suite.src ] && [ ! -h test-suite ]; then
297         ln -s ../../test-suite.src test-suite
298     fi
299     if [ -d $BuildDir/compiler-rt.src ] && [ ! -h compiler-rt ]; then
300         ln -s ../../compiler-rt.src compiler-rt
301     fi
302     if [ -d $BuildDir/openmp.src ] && [ ! -h openmp ]; then
303         ln -s ../../openmp.src openmp
304     fi
305     if [ -d $BuildDir/libcxx.src ] && [ ! -h libcxx ]; then
306         ln -s ../../libcxx.src libcxx
307     fi
308     if [ -d $BuildDir/libcxxabi.src ] && [ ! -h libcxxabi ]; then
309         ln -s ../../libcxxabi.src libcxxabi
310     fi
311     if [ -d $BuildDir/libunwind.src ] && [ ! -h libunwind ]; then
312         ln -s ../../libunwind.src libunwind
313     fi
314
315     cd $BuildDir
316 }
317
318 function configure_llvmCore() {
319     Phase="$1"
320     Flavor="$2"
321     ObjDir="$3"
322
323     case $Flavor in
324         Release )
325             BuildType="Release"
326             Assertions="OFF"
327             ConfigureFlags="--enable-optimized --disable-assertions"
328             ;;
329         Release+Asserts )
330             BuildType="Release"
331             Assertions="ON"
332             ConfigureFlags="--enable-optimized --enable-assertions"
333             ;;
334         Debug )
335             BuildType="Debug"
336             Assertions="ON"
337             ConfigureFlags="--disable-optimized --enable-assertions"
338             ;;
339         * )
340             echo "# Invalid flavor '$Flavor'"
341             echo ""
342             return
343             ;;
344     esac
345
346     echo "# Using C compiler: $c_compiler"
347     echo "# Using C++ compiler: $cxx_compiler"
348
349     cd $ObjDir
350     echo "# Configuring llvm $Release-$RC $Flavor"
351
352     if [ "$use_autoconf" = "yes" ]; then
353         echo "#" env CC="$c_compiler" CXX="$cxx_compiler" \
354             $BuildDir/llvm.src/configure \
355             $ConfigureFlags --disable-timestamps $ExtraConfigureFlags \
356             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
357         env CC="$c_compiler" CXX="$cxx_compiler" \
358             $BuildDir/llvm.src/configure \
359             $ConfigureFlags --disable-timestamps $ExtraConfigureFlags \
360             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
361     else
362         echo "#" env CC="$c_compiler" CXX="$cxx_compiler" \
363             cmake -G "Unix Makefiles" \
364             -DCMAKE_BUILD_TYPE=$BuildType -DLLVM_ENABLE_ASSERTIONS=$Assertions \
365             -DLLVM_ENABLE_TIMESTAMPS=OFF -DLLVM_CONFIGTIME="(timestamp not enabled)" \
366             $ExtraConfigureFlags $BuildDir/llvm.src \
367             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
368         env CC="$c_compiler" CXX="$cxx_compiler" \
369             cmake -G "Unix Makefiles" \
370             -DCMAKE_BUILD_TYPE=$BuildType -DLLVM_ENABLE_ASSERTIONS=$Assertions \
371             -DLLVM_ENABLE_TIMESTAMPS=OFF -DLLVM_CONFIGTIME="(timestamp not enabled)" \
372             $ExtraConfigureFlags $BuildDir/llvm.src \
373             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
374     fi
375
376     cd $BuildDir
377 }
378
379 function build_llvmCore() {
380     Phase="$1"
381     Flavor="$2"
382     ObjDir="$3"
383     DestDir="$4"
384
385     cd $ObjDir
386     echo "# Compiling llvm $Release-$RC $Flavor"
387     echo "# ${MAKE} -j $NumJobs VERBOSE=1"
388     ${MAKE} -j $NumJobs VERBOSE=1 \
389         2>&1 | tee $LogDir/llvm.make-Phase$Phase-$Flavor.log
390
391     echo "# Installing llvm $Release-$RC $Flavor"
392     echo "# ${MAKE} install"
393     ${MAKE} install \
394         DESTDIR="${DestDir}" \
395         2>&1 | tee $LogDir/llvm.install-Phase$Phase-$Flavor.log
396     cd $BuildDir
397 }
398
399 function test_llvmCore() {
400     Phase="$1"
401     Flavor="$2"
402     ObjDir="$3"
403
404     cd $ObjDir
405     if ! ( ${MAKE} -j $NumJobs -k check-all \
406         2>&1 | tee $LogDir/llvm.check-Phase$Phase-$Flavor.log ) ; then
407       deferred_error $Phase $Flavor "check-all failed"
408     fi
409
410     if [ "$use_autoconf" = "yes" ]; then
411         # In the cmake build, unit tests are run as part of check-all.
412         if ! ( ${MAKE} -k unittests 2>&1 | \
413             tee $LogDir/llvm.unittests-Phase$Phase-$Flavor.log ) ; then
414           deferred_error $Phase $Flavor "unittests failed"
415         fi
416     fi
417
418     cd $BuildDir
419 }
420
421 # Clean RPATH. Libtool adds the build directory to the search path, which is
422 # not necessary --- and even harmful --- for the binary packages we release.
423 function clean_RPATH() {
424   if [ `uname -s` = "Darwin" ]; then
425     return
426   fi
427   local InstallPath="$1"
428   for Candidate in `find $InstallPath/{bin,lib} -type f`; do
429     if file $Candidate | grep ELF | egrep 'executable|shared object' > /dev/null 2>&1 ; then
430       if rpath=`objdump -x $Candidate | grep 'RPATH'` ; then
431         rpath=`echo $rpath | sed -e's/^ *RPATH *//'`
432         if [ -n "$rpath" ]; then
433           newrpath=`echo $rpath | sed -e's/.*\(\$ORIGIN[^:]*\).*/\1/'`
434           chrpath -r $newrpath $Candidate 2>&1 > /dev/null 2>&1
435         fi
436       fi
437     fi
438   done
439 }
440
441 # Create a package of the release binaries.
442 function package_release() {
443     cwd=`pwd`
444     cd $BuildDir/Phase3/Release
445     mv llvmCore-$Release-$RC.install/usr/local $Package
446     if [ "$use_gzip" = "yes" ]; then
447       tar cfz $BuildDir/$Package.tar.gz $Package
448     else
449       tar cfJ $BuildDir/$Package.tar.xz $Package
450     fi
451     mv $Package llvmCore-$Release-$RC.install/usr/local
452     cd $cwd
453 }
454
455 # Exit if any command fails
456 # Note: pipefail is necessary for running build commands through
457 # a pipe (i.e. it changes the output of ``false | tee /dev/null ; echo $?``)
458 set -e
459 set -o pipefail
460
461 if [ "$do_checkout" = "yes" ]; then
462     export_sources
463 fi
464
465 (
466 Flavors="Release"
467 if [ "$do_debug" = "yes" ]; then
468     Flavors="Debug $Flavors"
469 fi
470 if [ "$do_asserts" = "yes" ]; then
471     Flavors="$Flavors Release+Asserts"
472 fi
473
474 for Flavor in $Flavors ; do
475     echo ""
476     echo ""
477     echo "********************************************************************************"
478     echo "  Release:     $Release-$RC"
479     echo "  Build:       $Flavor"
480     echo "  System Info: "
481     echo "    `uname -a`"
482     echo "********************************************************************************"
483     echo ""
484
485     c_compiler="$CC"
486     cxx_compiler="$CXX"
487     llvmCore_phase1_objdir=$BuildDir/Phase1/$Flavor/llvmCore-$Release-$RC.obj
488     llvmCore_phase1_destdir=$BuildDir/Phase1/$Flavor/llvmCore-$Release-$RC.install
489
490     llvmCore_phase2_objdir=$BuildDir/Phase2/$Flavor/llvmCore-$Release-$RC.obj
491     llvmCore_phase2_destdir=$BuildDir/Phase2/$Flavor/llvmCore-$Release-$RC.install
492
493     llvmCore_phase3_objdir=$BuildDir/Phase3/$Flavor/llvmCore-$Release-$RC.obj
494     llvmCore_phase3_destdir=$BuildDir/Phase3/$Flavor/llvmCore-$Release-$RC.install
495
496     rm -rf $llvmCore_phase1_objdir
497     rm -rf $llvmCore_phase1_destdir
498
499     rm -rf $llvmCore_phase2_objdir
500     rm -rf $llvmCore_phase2_destdir
501
502     rm -rf $llvmCore_phase3_objdir
503     rm -rf $llvmCore_phase3_destdir
504
505     mkdir -p $llvmCore_phase1_objdir
506     mkdir -p $llvmCore_phase1_destdir
507
508     mkdir -p $llvmCore_phase2_objdir
509     mkdir -p $llvmCore_phase2_destdir
510
511     mkdir -p $llvmCore_phase3_objdir
512     mkdir -p $llvmCore_phase3_destdir
513
514     ############################################################################
515     # Phase 1: Build llvmCore and clang
516     echo "# Phase 1: Building llvmCore"
517     configure_llvmCore 1 $Flavor $llvmCore_phase1_objdir
518     build_llvmCore 1 $Flavor \
519         $llvmCore_phase1_objdir $llvmCore_phase1_destdir
520     clean_RPATH $llvmCore_phase1_destdir/usr/local
521
522     ########################################################################
523     # Phase 2: Build llvmCore with newly built clang from phase 1.
524     c_compiler=$llvmCore_phase1_destdir/usr/local/bin/clang
525     cxx_compiler=$llvmCore_phase1_destdir/usr/local/bin/clang++
526     echo "# Phase 2: Building llvmCore"
527     configure_llvmCore 2 $Flavor $llvmCore_phase2_objdir
528     build_llvmCore 2 $Flavor \
529         $llvmCore_phase2_objdir $llvmCore_phase2_destdir
530     clean_RPATH $llvmCore_phase2_destdir/usr/local
531
532     ########################################################################
533     # Phase 3: Build llvmCore with newly built clang from phase 2.
534     c_compiler=$llvmCore_phase2_destdir/usr/local/bin/clang
535     cxx_compiler=$llvmCore_phase2_destdir/usr/local/bin/clang++
536     echo "# Phase 3: Building llvmCore"
537     configure_llvmCore 3 $Flavor $llvmCore_phase3_objdir
538     build_llvmCore 3 $Flavor \
539         $llvmCore_phase3_objdir $llvmCore_phase3_destdir
540     clean_RPATH $llvmCore_phase3_destdir/usr/local
541
542     ########################################################################
543     # Testing: Test phase 3
544     echo "# Testing - built with clang"
545     test_llvmCore 3 $Flavor $llvmCore_phase3_objdir
546
547     ########################################################################
548     # Compare .o files between Phase2 and Phase3 and report which ones
549     # differ.
550     if [ "$do_compare" = "yes" ]; then
551         echo
552         echo "# Comparing Phase 2 and Phase 3 files"
553         for p2 in `find $llvmCore_phase2_objdir -name '*.o'` ; do
554             p3=`echo $p2 | sed -e 's,Phase2,Phase3,'`
555             # Substitute 'Phase2' for 'Phase3' in the Phase 2 object file in
556             # case there are build paths in the debug info. On some systems,
557             # sed adds a newline to the output, so pass $p3 through sed too.
558             if ! cmp -s <(sed -e 's,Phase2,Phase3,g' $p2) <(sed -e '' $p3) \
559                     16 16 ; then
560                 echo "file `basename $p2` differs between phase 2 and phase 3"
561             fi
562         done
563     fi
564 done
565
566 ) 2>&1 | tee $LogDir/testing.$Release-$RC.log
567
568 package_release
569
570 set +e
571
572 # Woo hoo!
573 echo "### Testing Finished ###"
574 if [ "$use_gzip" = "yes" ]; then
575   echo "### Package: $Package.tar.gz"
576 else
577   echo "### Package: $Package.tar.xz"
578 fi
579 echo "### Logs: $LogDir"
580
581 echo "### Errors:"
582 if [ -s "$LogDir/deferred_errors.log" ]; then
583   cat "$LogDir/deferred_errors.log"
584   exit 1
585 else
586   echo "None."
587 fi
588
589 exit 0