OSDN Git Service

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