c3884ba31756af71edd1b872415f68f546d9c87d
[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         if [ -d $proj.src ]; then
271           echo "# Reusing $proj $Release-$RC sources"
272           continue
273         fi
274         echo "# Exporting $proj $Release-$RC sources"
275         if ! svn export -q $Base_url/$proj/$ExportBranch $proj.src ; then
276             echo "error: failed to export $proj project"
277             exit 1
278         fi
279     done
280
281     echo "# Creating symlinks"
282     cd $BuildDir/llvm.src/tools
283     if [ ! -h clang ]; then
284         ln -s ../../cfe.src clang
285     fi
286
287     # The autoconf and CMake builds want different symlinks here:
288     if [ "$use_autoconf" = "yes" ]; then
289       cd $BuildDir/llvm.src/tools/clang/tools
290       if [ ! -h extra ]; then
291           ln -s ../../../../clang-tools-extra.src extra
292       fi
293     else
294       cd $BuildDir/cfe.src/tools
295       if [ ! -h extra ]; then
296           ln -s ../../clang-tools-extra.src extra
297       fi
298     fi
299
300     cd $BuildDir/llvm.src/projects
301     if [ -d $BuildDir/test-suite.src ] && [ ! -h test-suite ]; then
302         ln -s ../../test-suite.src test-suite
303     fi
304     if [ -d $BuildDir/compiler-rt.src ] && [ ! -h compiler-rt ]; then
305         ln -s ../../compiler-rt.src compiler-rt
306     fi
307     if [ -d $BuildDir/openmp.src ] && [ ! -h openmp ]; then
308         ln -s ../../openmp.src openmp
309     fi
310     if [ -d $BuildDir/libcxx.src ] && [ ! -h libcxx ]; then
311         ln -s ../../libcxx.src libcxx
312     fi
313     if [ -d $BuildDir/libcxxabi.src ] && [ ! -h libcxxabi ]; then
314         ln -s ../../libcxxabi.src libcxxabi
315     fi
316     if [ -d $BuildDir/libunwind.src ] && [ ! -h libunwind ]; then
317         ln -s ../../libunwind.src libunwind
318     fi
319
320     cd $BuildDir
321 }
322
323 function configure_llvmCore() {
324     Phase="$1"
325     Flavor="$2"
326     ObjDir="$3"
327
328     case $Flavor in
329         Release )
330             BuildType="Release"
331             Assertions="OFF"
332             ConfigureFlags="--enable-optimized --disable-assertions"
333             ;;
334         Release+Asserts )
335             BuildType="Release"
336             Assertions="ON"
337             ConfigureFlags="--enable-optimized --enable-assertions"
338             ;;
339         Debug )
340             BuildType="Debug"
341             Assertions="ON"
342             ConfigureFlags="--disable-optimized --enable-assertions"
343             ;;
344         * )
345             echo "# Invalid flavor '$Flavor'"
346             echo ""
347             return
348             ;;
349     esac
350
351     echo "# Using C compiler: $c_compiler"
352     echo "# Using C++ compiler: $cxx_compiler"
353
354     cd $ObjDir
355     echo "# Configuring llvm $Release-$RC $Flavor"
356
357     if [ "$use_autoconf" = "yes" ]; then
358         echo "#" env CC="$c_compiler" CXX="$cxx_compiler" \
359             $BuildDir/llvm.src/configure \
360             $ConfigureFlags --disable-timestamps $ExtraConfigureFlags \
361             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
362         env CC="$c_compiler" CXX="$cxx_compiler" \
363             $BuildDir/llvm.src/configure \
364             $ConfigureFlags --disable-timestamps $ExtraConfigureFlags \
365             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
366     else
367         echo "#" env CC="$c_compiler" CXX="$cxx_compiler" \
368             cmake -G "Unix Makefiles" \
369             -DCMAKE_BUILD_TYPE=$BuildType -DLLVM_ENABLE_ASSERTIONS=$Assertions \
370             -DLLVM_ENABLE_TIMESTAMPS=OFF -DLLVM_CONFIGTIME="(timestamp not enabled)" \
371             $ExtraConfigureFlags $BuildDir/llvm.src \
372             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
373         env CC="$c_compiler" CXX="$cxx_compiler" \
374             cmake -G "Unix Makefiles" \
375             -DCMAKE_BUILD_TYPE=$BuildType -DLLVM_ENABLE_ASSERTIONS=$Assertions \
376             -DLLVM_ENABLE_TIMESTAMPS=OFF -DLLVM_CONFIGTIME="(timestamp not enabled)" \
377             $ExtraConfigureFlags $BuildDir/llvm.src \
378             2>&1 | tee $LogDir/llvm.configure-Phase$Phase-$Flavor.log
379     fi
380
381     cd $BuildDir
382 }
383
384 function build_llvmCore() {
385     Phase="$1"
386     Flavor="$2"
387     ObjDir="$3"
388     DestDir="$4"
389
390     cd $ObjDir
391     echo "# Compiling llvm $Release-$RC $Flavor"
392     echo "# ${MAKE} -j $NumJobs VERBOSE=1"
393     ${MAKE} -j $NumJobs VERBOSE=1 \
394         2>&1 | tee $LogDir/llvm.make-Phase$Phase-$Flavor.log
395
396     echo "# Installing llvm $Release-$RC $Flavor"
397     echo "# ${MAKE} install"
398     ${MAKE} install \
399         DESTDIR="${DestDir}" \
400         2>&1 | tee $LogDir/llvm.install-Phase$Phase-$Flavor.log
401     cd $BuildDir
402 }
403
404 function test_llvmCore() {
405     Phase="$1"
406     Flavor="$2"
407     ObjDir="$3"
408
409     cd $ObjDir
410     if ! ( ${MAKE} -j $NumJobs -k check-all \
411         2>&1 | tee $LogDir/llvm.check-Phase$Phase-$Flavor.log ) ; then
412       deferred_error $Phase $Flavor "check-all failed"
413     fi
414
415     if [ "$use_autoconf" = "yes" ]; then
416         # In the cmake build, unit tests are run as part of check-all.
417         if ! ( ${MAKE} -k unittests 2>&1 | \
418             tee $LogDir/llvm.unittests-Phase$Phase-$Flavor.log ) ; then
419           deferred_error $Phase $Flavor "unittests failed"
420         fi
421     fi
422
423     cd $BuildDir
424 }
425
426 # Clean RPATH. Libtool adds the build directory to the search path, which is
427 # not necessary --- and even harmful --- for the binary packages we release.
428 function clean_RPATH() {
429   if [ `uname -s` = "Darwin" ]; then
430     return
431   fi
432   local InstallPath="$1"
433   for Candidate in `find $InstallPath/{bin,lib} -type f`; do
434     if file $Candidate | grep ELF | egrep 'executable|shared object' > /dev/null 2>&1 ; then
435       if rpath=`objdump -x $Candidate | grep 'RPATH'` ; then
436         rpath=`echo $rpath | sed -e's/^ *RPATH *//'`
437         if [ -n "$rpath" ]; then
438           newrpath=`echo $rpath | sed -e's/.*\(\$ORIGIN[^:]*\).*/\1/'`
439           chrpath -r $newrpath $Candidate 2>&1 > /dev/null 2>&1
440         fi
441       fi
442     fi
443   done
444 }
445
446 # Create a package of the release binaries.
447 function package_release() {
448     cwd=`pwd`
449     cd $BuildDir/Phase3/Release
450     mv llvmCore-$Release-$RC.install/usr/local $Package
451     if [ "$use_gzip" = "yes" ]; then
452       tar cfz $BuildDir/$Package.tar.gz $Package
453     else
454       tar cfJ $BuildDir/$Package.tar.xz $Package
455     fi
456     mv $Package llvmCore-$Release-$RC.install/usr/local
457     cd $cwd
458 }
459
460 # Exit if any command fails
461 # Note: pipefail is necessary for running build commands through
462 # a pipe (i.e. it changes the output of ``false | tee /dev/null ; echo $?``)
463 set -e
464 set -o pipefail
465
466 if [ "$do_checkout" = "yes" ]; then
467     export_sources
468 fi
469
470 (
471 Flavors="Release"
472 if [ "$do_debug" = "yes" ]; then
473     Flavors="Debug $Flavors"
474 fi
475 if [ "$do_asserts" = "yes" ]; then
476     Flavors="$Flavors Release+Asserts"
477 fi
478
479 for Flavor in $Flavors ; do
480     echo ""
481     echo ""
482     echo "********************************************************************************"
483     echo "  Release:     $Release-$RC"
484     echo "  Build:       $Flavor"
485     echo "  System Info: "
486     echo "    `uname -a`"
487     echo "********************************************************************************"
488     echo ""
489
490     c_compiler="$CC"
491     cxx_compiler="$CXX"
492     llvmCore_phase1_objdir=$BuildDir/Phase1/$Flavor/llvmCore-$Release-$RC.obj
493     llvmCore_phase1_destdir=$BuildDir/Phase1/$Flavor/llvmCore-$Release-$RC.install
494
495     llvmCore_phase2_objdir=$BuildDir/Phase2/$Flavor/llvmCore-$Release-$RC.obj
496     llvmCore_phase2_destdir=$BuildDir/Phase2/$Flavor/llvmCore-$Release-$RC.install
497
498     llvmCore_phase3_objdir=$BuildDir/Phase3/$Flavor/llvmCore-$Release-$RC.obj
499     llvmCore_phase3_destdir=$BuildDir/Phase3/$Flavor/llvmCore-$Release-$RC.install
500
501     rm -rf $llvmCore_phase1_objdir
502     rm -rf $llvmCore_phase1_destdir
503
504     rm -rf $llvmCore_phase2_objdir
505     rm -rf $llvmCore_phase2_destdir
506
507     rm -rf $llvmCore_phase3_objdir
508     rm -rf $llvmCore_phase3_destdir
509
510     mkdir -p $llvmCore_phase1_objdir
511     mkdir -p $llvmCore_phase1_destdir
512
513     mkdir -p $llvmCore_phase2_objdir
514     mkdir -p $llvmCore_phase2_destdir
515
516     mkdir -p $llvmCore_phase3_objdir
517     mkdir -p $llvmCore_phase3_destdir
518
519     ############################################################################
520     # Phase 1: Build llvmCore and clang
521     echo "# Phase 1: Building llvmCore"
522     configure_llvmCore 1 $Flavor $llvmCore_phase1_objdir
523     build_llvmCore 1 $Flavor \
524         $llvmCore_phase1_objdir $llvmCore_phase1_destdir
525     clean_RPATH $llvmCore_phase1_destdir/usr/local
526
527     ########################################################################
528     # Phase 2: Build llvmCore with newly built clang from phase 1.
529     c_compiler=$llvmCore_phase1_destdir/usr/local/bin/clang
530     cxx_compiler=$llvmCore_phase1_destdir/usr/local/bin/clang++
531     echo "# Phase 2: Building llvmCore"
532     configure_llvmCore 2 $Flavor $llvmCore_phase2_objdir
533     build_llvmCore 2 $Flavor \
534         $llvmCore_phase2_objdir $llvmCore_phase2_destdir
535     clean_RPATH $llvmCore_phase2_destdir/usr/local
536
537     ########################################################################
538     # Phase 3: Build llvmCore with newly built clang from phase 2.
539     c_compiler=$llvmCore_phase2_destdir/usr/local/bin/clang
540     cxx_compiler=$llvmCore_phase2_destdir/usr/local/bin/clang++
541     echo "# Phase 3: Building llvmCore"
542     configure_llvmCore 3 $Flavor $llvmCore_phase3_objdir
543     build_llvmCore 3 $Flavor \
544         $llvmCore_phase3_objdir $llvmCore_phase3_destdir
545     clean_RPATH $llvmCore_phase3_destdir/usr/local
546
547     ########################################################################
548     # Testing: Test phase 3
549     echo "# Testing - built with clang"
550     test_llvmCore 3 $Flavor $llvmCore_phase3_objdir
551
552     ########################################################################
553     # Compare .o files between Phase2 and Phase3 and report which ones
554     # differ.
555     if [ "$do_compare" = "yes" ]; then
556         echo
557         echo "# Comparing Phase 2 and Phase 3 files"
558         for p2 in `find $llvmCore_phase2_objdir -name '*.o'` ; do
559             p3=`echo $p2 | sed -e 's,Phase2,Phase3,'`
560             # Substitute 'Phase2' for 'Phase3' in the Phase 2 object file in
561             # case there are build paths in the debug info. On some systems,
562             # sed adds a newline to the output, so pass $p3 through sed too.
563             if ! cmp -s <(sed -e 's,Phase2,Phase3,g' $p2) <(sed -e '' $p3) \
564                     16 16 ; then
565                 echo "file `basename $p2` differs between phase 2 and phase 3"
566             fi
567         done
568     fi
569 done
570
571 ) 2>&1 | tee $LogDir/testing.$Release-$RC.log
572
573 package_release
574
575 set +e
576
577 # Woo hoo!
578 echo "### Testing Finished ###"
579 if [ "$use_gzip" = "yes" ]; then
580   echo "### Package: $Package.tar.gz"
581 else
582   echo "### Package: $Package.tar.xz"
583 fi
584 echo "### Logs: $LogDir"
585
586 echo "### Errors:"
587 if [ -s "$LogDir/deferred_errors.log" ]; then
588   cat "$LogDir/deferred_errors.log"
589   exit 1
590 else
591   echo "None."
592 fi
593
594 exit 0