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