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