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