- add third_party src.
[platform/framework/web/crosswalk.git] / src / native_client / buildbot / buildbot_pnacl_toolchain_tests.sh
1 #!/bin/bash
2 # Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 # Run toolchain torture tests and llvm testsuite tests.
7 # For now, run on linux64, build and run unsandboxed newlib tests
8 # for all 3 architectures.
9 # Note: This script builds the toolchain from scratch but does
10 #       not build the translators and hence the translators
11 #       are from an older revision, see comment below.
12
13 set -o xtrace
14 set -o nounset
15 set -o errexit
16
17 # NOTE:
18 # The pexes which are referred to below, and the pexes generated by the
19 # archived frontend below will be translated with translators from DEPS.
20 # The motivation is to ensure that newer translators can still handle
21 # older pexes.
22
23 # This hopefully needs to be updated rarely, it contains pexe from
24 # the sandboxed llc/gold builds
25 ARCHIVED_PEXE_TRANSLATOR_REV=12177
26
27 # The frontend from this rev will generate pexes for the archived frontend
28 # test. The toolchain downloader expects this information in a specially
29 # formatted file. We generate that file in this script from this information,
30 # to keep all our versions in one place
31 ARCHIVED_TOOLCHAIN_REV=12276
32
33 readonly PNACL_BUILD="pnacl/build.sh"
34 readonly UP_DOWN_LOAD="buildbot/file_up_down_load.sh"
35 readonly TORTURE_TEST="tools/toolchain_tester/torture_test.py"
36 readonly LLVM_TESTSUITE="pnacl/scripts/llvm-test.py"
37
38 # build.sh, llvm test suite and torture tests all use this value
39 export PNACL_CONCURRENCY=${PNACL_CONCURRENCY:-4}
40
41 # Change the  toolchain build script (PNACL_BUILD) behavior slightly
42 # wrt to error logging and mecurial retry delays.
43 # TODO(robertm): if this special casing is still needed,
44 #                make this into separate vars
45 export PNACL_BUILDBOT=true
46 # Make the toolchain build script (PNACL_BUILD) more verbose.
47 # This will also prevent bot timeouts which otherwise gets triggered
48 # by long periods without console output.
49 export PNACL_VERBOSE=true
50
51 # For now this script runs on linux x86-64.
52 # It is possible to force the PNACL_BUILD to build host binaries with "-m32",
53 # by uncommenting below:
54 # export BUILD_ARCH="x86_32"
55 # export HOST_ARCH="x86_32"
56 # TODO(pnacl-team): Figure out what to do about this.
57 # Export this so that the test scripts know where to find the toolchain.
58 export PNACL_TOOLCHAIN_LABEL=pnacl_linux_x86
59 # This picks the TC which we just built, even if scons doesn't know
60 # how to find a 64-bit host toolchain.
61 readonly SCONS_PICK_TC="pnaclsdk_mode=custom:toolchain/${PNACL_TOOLCHAIN_LABEL}"
62
63 # download-old-tc -
64 # Download the archived frontend toolchain, if we haven't already
65 download-old-tc() {
66   local dst=$1
67
68   if [[ -f "${dst}/${ARCHIVED_TOOLCHAIN_REV}.stamp" ]]; then
69     echo "Using existing tarball for archived frontend"
70   else
71     mkdir -p "${dst}"
72     rm -rf "${dst}/*"
73     ${UP_DOWN_LOAD} DownloadPnaclToolchains ${ARCHIVED_TOOLCHAIN_REV} \
74       ${PNACL_TOOLCHAIN_LABEL} \
75       ${dst}/${PNACL_TOOLCHAIN_LABEL}.tgz
76     mkdir -p ${dst}/${PNACL_TOOLCHAIN_LABEL}
77     tar xz -C ${dst}/${PNACL_TOOLCHAIN_LABEL} \
78       -f ${dst}/${PNACL_TOOLCHAIN_LABEL}.tgz
79     touch "${dst}/${ARCHIVED_TOOLCHAIN_REV}.stamp"
80   fi
81 }
82
83
84 clobber() {
85   echo @@@BUILD_STEP clobber@@@
86   rm -rf scons-out
87   # Don't clobber toolchain/pnacl_translator; these bots currently don't build
88   # it, but they use the DEPSed-in version.
89   rm -rf toolchain/pnacl_linux* toolchain/pnacl_mac* toolchain/pnacl_win*
90 }
91
92 handle-error() {
93   echo "@@@STEP_FAILURE@@@"
94 }
95
96 ignore-error() {
97   echo "@==  IGNORING AN ERROR  ==@"
98 }
99
100 #### Support for running arm sbtc tests on this bot, since we have
101 # less coverage on the main waterfall now:
102 # http://code.google.com/p/nativeclient/issues/detail?id=2581
103 readonly SCONS_COMMON="./scons --verbose bitcode=1 -j${PNACL_CONCURRENCY}"
104 readonly SCONS_COMMON_SLOW="./scons --verbose bitcode=1 -j2"
105
106 build-sbtc-prerequisites() {
107   local platform=$1
108   ${SCONS_COMMON} ${SCONS_PICK_TC} platform=${platform} \
109     sel_ldr sel_universal irt_core
110 }
111
112
113 scons-tests-translator() {
114   local platform=$1
115
116   echo "@@@BUILD_STEP scons-sb-trans [${platform}] [prereq]@@@"
117   build-sbtc-prerequisites ${platform}
118
119   local flags="--mode=opt-host,nacl use_sandboxed_translator=1 \
120                platform=${platform} -k"
121   local targets="small_tests medium_tests large_tests"
122
123   # ROUND 1: regular builds
124   # generate pexes with full parallelism
125   echo "@@@BUILD_STEP scons-sb-trans-pexe [${platform}] [${targets}]@@@"
126   ${SCONS_COMMON} ${SCONS_PICK_TC} ${flags} ${targets} \
127       translate_in_build_step=0 do_not_run_tests=1 || handle-error
128
129   # translate pexes
130   echo "@@@BUILD_STEP scons-sb-trans-trans [${platform}] [${targets}]@@@"
131   if [[ ${platform} = arm ]] ; then
132       # For ARM we use less parallelism to avoid mysterious QEMU crashes.
133       # We also force a timeout for translation only.
134       export QEMU_PREFIX_HOOK="timeout 120"
135       # Run sb translation twice in case we failed to translate some of the
136       # pexes.  If there was an error in the first run this shouldn't
137       # trigger a buildbot error.  Only the second run can make the bot red.
138       ${SCONS_COMMON_SLOW} ${SCONS_PICK_TC} ${flags} ${targets} \
139           do_not_run_tests=1 || ignore-error
140       ${SCONS_COMMON_SLOW} ${SCONS_PICK_TC} ${flags} ${targets} \
141           do_not_run_tests=1 || handle-error
142       # Do not use the prefix hook for running actual tests as
143       # it will break some of them due to exit code sign inversion.
144       unset QEMU_PREFIX_HOOK
145   else
146       ${SCONS_COMMON} ${SCONS_PICK_TC} ${flags} ${targets} \
147           do_not_run_tests=1 || handle-error
148   fi
149   # finally run the tests
150   echo "@@@BUILD_STEP scons-sb-trans-run [${platform}] [${targets}]@@@"
151   ${SCONS_COMMON_SLOW} ${SCONS_PICK_TC} ${flags} ${targets} || handle-error
152
153   # ROUND 2: builds with "fast translation"
154   flags="${flags} translate_fast=1"
155   echo "@@@BUILD_STEP scons-sb-trans-pexe [fast] [${platform}] [${targets}]@@@"
156   ${SCONS_COMMON} ${SCONS_PICK_TC} ${flags} ${targets} \
157       translate_in_build_step=0 do_not_run_tests=1 || handle-error
158
159   echo "@@@BUILD_STEP scons-sb-trans-trans [fast] [${platform}] [${targets}]@@@"
160   if [[ ${platform} = arm ]] ; then
161       # For ARM we use less parallelism to avoid mysterious QEMU crashes.
162       # We also force a timeout for translation only.
163       export QEMU_PREFIX_HOOK="timeout 120"
164       # Run sb translation twice in case we failed to translate some of the
165       # pexes.  If there was an error in the first run this shouldn't
166       # trigger a buildbot error.  Only the second run can make the bot red.
167       ${SCONS_COMMON_SLOW} ${SCONS_PICK_TC} ${flags} ${targets} \
168           do_not_run_tests=1 || ignore-error
169       ${SCONS_COMMON_SLOW} ${SCONS_PICK_TC} ${flags} ${targets} \
170           do_not_run_tests=1 || handle-error
171       # Do not use the prefix hook for running actual tests as
172       # it will break some of them due to exit code sign inversion.
173       unset QEMU_PREFIX_HOOK
174   else
175       ${SCONS_COMMON} ${SCONS_PICK_TC} ${flags} ${targets} \
176           do_not_run_tests=1 || handle-error
177   fi
178   echo "@@@BUILD_STEP scons-sb-trans-run [fast] [${platform}] [${targets}]@@@"
179   ${SCONS_COMMON_SLOW} ${SCONS_PICK_TC} ${flags} ${targets} || handle-error
180 }
181
182 scons-tests-x86-64-zero-based-sandbox() {
183   echo "@@@BUILD_STEP hello_world (x86-64 zero-based sandbox)@@@"
184   local flags="--mode=opt-host,nacl platform=x86-64 \
185                x86_64_zero_based_sandbox=1"
186   ${SCONS_COMMON} ${SCONS_PICK_TC} ${flags} "run_hello_world_test"
187 }
188
189 # This test is a bitcode stability test, which builds pexes for all the tests
190 # using an old version of the toolchain frontend, and then translates those
191 # pexes using the current version of the translator. It's simpler than using
192 # archived pexes, because archived pexes for old scons tests may not match the
193 # current scons tests (e.g. if the expected output changes or if a new test
194 # is added). The only thing that would break this approach is if a new test
195 # is added that is incompatible with the old frontend. For this case there
196 # simply needs to be a mechanism to disable that test (which could be as simple
197 # as using disable_tests here on the scons command line).
198 # Note: If this test is manually interrupted or killed during the run, the
199 # toolchain install might end up missing or replaced with the old one.
200 # To fix, copy the current one from toolchains/current_tc or blow it away
201 # and re-run gclient runhooks.
202 archived-frontend-test() {
203   local arch=$1
204   # Build the IRT with the latest toolchain before building user
205   # pexes with the archived toolchain.
206   echo "@@@BUILD_STEP archived_frontend [${arch}]\
207         rev ${ARCHIVED_TOOLCHAIN_REV} BUILD IRT@@@"
208   ${SCONS_COMMON} ${SCONS_PICK_TC} --mode=opt-host,nacl platform=${arch} \
209     irt_core || handle-error
210
211
212   echo "@@@BUILD_STEP archived_frontend [${arch}]\
213         rev ${ARCHIVED_TOOLCHAIN_REV} BUILD@@@"
214   local targets="small_tests medium_tests large_tests"
215   local flags="--mode=opt-host,nacl platform=${arch} \
216                translate_in_build_step=0 skip_trusted_tests=1 \
217                skip_nonstable_bitcode=1"
218
219   rm -rf scons-out/nacl-${arch}*
220
221   # Get the archived frontend.
222   # If the correct cached frontend is in place, the hash will match and the
223   # download will be a no-op. Otherwise the downloader will fix it.
224   download-old-tc toolchain/archived_tc
225
226   # Save the current toolchain.
227   mkdir -p toolchain/current_tc
228   rm -rf toolchain/current_tc/*
229   mv toolchain/${PNACL_TOOLCHAIN_LABEL} \
230     toolchain/current_tc/${PNACL_TOOLCHAIN_LABEL}
231
232   # Link the old frontend into place. If we just use pnaclsdk_mode to select a
233   # different toolchain, SCons will attempt to rebuild the IRT.
234   ln -s archived_tc/${PNACL_TOOLCHAIN_LABEL} toolchain/${PNACL_TOOLCHAIN_LABEL}
235
236   # Build the pexes with the old frontend.
237   ${SCONS_COMMON} ${SCONS_PICK_TC} \
238     do_not_run_tests=1 ${flags} ${targets} || handle-error
239
240   # Put the current toolchain back in place.
241   rm toolchain/${PNACL_TOOLCHAIN_LABEL}
242   mv toolchain/current_tc/${PNACL_TOOLCHAIN_LABEL} \
243     toolchain/${PNACL_TOOLCHAIN_LABEL}
244
245   # Translate them with the new translator, and run the tests.
246   # Use the sandboxed translator, which runs the newer ABI verifier
247   # (building w/ the old frontend only runs the old ABI verifier).
248   flags="${flags} use_sandboxed_translator=1"
249   # The pexes for sandboxed and fast translation tests are identical but scons
250   # uses a different directory.
251   local pexe_dir="scons-out/nacl-${arch}-pnacl-pexe-clang"
252   cp -a ${pexe_dir} "scons-out/nacl-${arch}-pnacl-sbtc-pexe-clang"
253   cp -a ${pexe_dir} "scons-out/nacl-${arch}-pnacl-fast-sbtc-pexe-clang"
254
255   echo "@@@BUILD_STEP archived_frontend [${arch}]\
256         rev ${ARCHIVED_TOOLCHAIN_REV} RUN@@@"
257   # For QEMU limit parallelism to avoid flake.
258   if [[ ${arch} = arm ]] ; then
259     ${SCONS_COMMON_SLOW} ${SCONS_PICK_TC} \
260       ${flags} ${targets} built_elsewhere=1 || handle-error
261     # Also test the fast-translation option
262     echo "@@@BUILD_STEP archived_frontend [${arch} translate-fast]\
263         rev ${ARCHIVED_TOOLCHAIN_REV} RUN@@@"
264     ${SCONS_COMMON_SLOW} ${SCONS_PICK_TC} ${flags} translate_fast=1 \
265       built_elsewhere=1 ${targets} || handle-error
266   else
267     ${SCONS_COMMON} ${SCONS_PICK_TC} \
268       ${flags} ${targets} built_elsewhere=1 || handle-error
269     # Also test the fast-translation option
270     echo "@@@BUILD_STEP archived_frontend [${arch} translate-fast]\
271         rev ${ARCHIVED_TOOLCHAIN_REV} RUN@@@"
272     ${SCONS_COMMON} ${SCONS_PICK_TC} ${flags} translate_fast=1 \
273       built_elsewhere=1 ${targets} || handle-error
274   fi
275 }
276
277 archived-pexe-translator-test() {
278   local arch=$1
279   echo "@@@BUILD_STEP archived_pexe_translator \
280         $arch rev ${ARCHIVED_PEXE_TRANSLATOR_REV} @@@"
281   local dir="$(pwd)/pexe_archive"
282   local tarball="${dir}/pexes.tar.bz2"
283   local measure_cmd="/usr/bin/time -v"
284   local sb_translator="${measure_cmd} \
285                        toolchain/pnacl_translator/bin/pnacl-translate"
286   rm -rf ${dir}
287   mkdir -p ${dir}
288
289   ${UP_DOWN_LOAD} DownloadArchivedPexesTranslator \
290       ${ARCHIVED_PEXE_TRANSLATOR_REV} ${tarball}
291   tar jxf ${tarball} --directory ${dir}
292
293   local ld_ext=""
294   local llc_ext=""
295   # Pexes are arch specific.
296   case ${arch} in
297     arm)
298       ld_ext=".armv7.final.pexe"
299       llc_ext=".armv7.final.pexe"
300       ;;
301     x86-32)
302       ld_ext=".i686.final.pexe"
303       llc_ext=".i686.final.pexe"
304       ;;
305     x86-64)
306       ld_ext=".x86_64.final.pexe"
307       llc_ext=".i686.final.pexe" # One llc pexe handles both x86-32 and x86-64.
308       ;;
309     *) echo "unknown arch!" && handle-error ;;
310   esac
311
312   # Note, that the arch flag has two functions:
313   # 1) it selects the target arch for the translator
314   # 2) combined with --pnacl-sb it selects the host arch for the
315   #    sandboxed translators
316   local flags="-arch ${arch} --pnacl-sb --pnacl-driver-verbose \
317       -pnaclabi-allow-dev-intrinsics"
318   if [[ ${arch} = arm ]] ; then
319     # We need to enable qemu magic for arm
320     flags="${flags} --pnacl-use-emulator"
321   fi
322   local fast_trans_flags="${flags} -translate-fast"
323
324   # Driver flags for overriding *just* the LLC and LD from
325   # the translator, to test that the LLC and LD generated
326   # from archived pexes may work.  Note that this does not override the
327   # libaries or the driver that are part of the translator,
328   # so it is not a full override and will not work if the interface
329   # has changed.
330   local override_flags="\
331     --pnacl-driver-set-LLC_SB=${dir}/pnacl-llc-${arch}.nexe \
332     --pnacl-driver-set-LD_SB=${dir}/ld-${arch}.nexe"
333   local fast_override_flags="\
334     --pnacl-driver-set-LLC_SB=${dir}/pnacl-llc-${arch}.fast_trans.nexe \
335     --pnacl-driver-set-LD_SB=${dir}/ld-${arch}.fast_trans.nexe"
336
337   echo "=== Translating the archived translator."
338   echo "=== Compiling Old Gold (normal mode) ==="
339   ${sb_translator} ${flags} ${dir}/ld${ld_ext} \
340       -o ${dir}/ld-${arch}.nexe
341   echo "=== Compiling Old Gold (fast mode) ==="
342   ${sb_translator} ${fast_trans_flags} ${dir}/ld${ld_ext} \
343       -o ${dir}/ld-${arch}.fast_trans.nexe
344
345   # Yikes: This takes about 17min on arm with qemu
346   echo "=== Compiling Old pnacl-llc (normal mode) ==="
347   ${sb_translator} ${flags} ${dir}/pnacl-llc${llc_ext} \
348       -o ${dir}/pnacl-llc-${arch}.nexe
349   echo "=== Compiling Old pnacl-llc (fast mode) ==="
350   ${sb_translator} ${fast_trans_flags} ${dir}/pnacl-llc${llc_ext} \
351       -o ${dir}/pnacl-llc-${arch}.fast_trans.nexe
352
353   ls -l ${dir}
354   file ${dir}/*
355
356   echo "=== Running the translated archived translator to test."
357   ${sb_translator} ${flags} ${override_flags} ${dir}/ld${ld_ext} \
358       -o ${dir}/ld-${arch}.2.nexe
359   ${sb_translator} ${flags} ${fast_override_flags} ${dir}/ld${ld_ext} \
360       -o ${dir}/ld-${arch}.3.nexe
361
362   # TODO(robertm): Ideally we would compare the result of translation like so
363   # ${dir}/ld-${arch}.2.nexe == ${dir}/ld-${arch}.3.nexe
364   # but this requires the translator to be deterministic which is not
365   # quite true at the moment - probably due to due hashing inside of
366   # llc based on pointer values.
367 }
368
369
370 tc-test-bot() {
371   local archset="$1"
372   clobber
373
374   # Only build MIPS stuff on mips bots
375   if [[ ${archset} == "mips" ]]; then
376     export PNACL_BUILD_MIPS=true
377     # Don't run any of the tests yet
378     echo "MIPS bot: Only running build, and not tests"
379     archset=
380   fi
381
382   echo "@@@BUILD_STEP show-config@@@"
383   ${PNACL_BUILD} show-config
384
385   # Build the un-sandboxed toolchain
386   echo "@@@BUILD_STEP compile_toolchain@@@"
387   ${PNACL_BUILD} clean
388   HOST_ARCH=x86_32 ${PNACL_BUILD} all
389   # Make 64-bit versions of the build tools such as fpcmp (used for llvm
390   # test suite and for some reason it matters that they match the build machine)
391   ${PNACL_BUILD} llvm-configure
392   PNACL_MAKE_OPTS=BUILD_DIRS_ONLY=1 ${PNACL_BUILD} llvm-make
393
394   # run the torture tests. the "trybot" phases take care of prerequisites
395   # for both test sets
396   for arch in ${archset}; do
397     if [[ "${arch}" == "x86-32" ]]; then
398       # Torture tests on x86-32 are covered by tc-tests-all in
399       # buildbot_pnacl.sh.
400       continue
401     fi
402     echo "@@@BUILD_STEP torture_tests $arch @@@"
403     ${TORTURE_TEST} pnacl ${arch} --verbose \
404       --concurrency=${PNACL_CONCURRENCY} || handle-error
405   done
406
407   # llvm-test-suite below requires the SDK libraries to be installed.
408   # torture_tests above do this as a side effect, but we do it
409   # explicitly here for cases in which torture_tests are not run.
410   echo "@@@BUILD_STEP install sdk libraries @@@"
411   ${PNACL_BUILD} sdk
412
413   local optset
414   optset[1]="--opt O3f --opt O2b"
415   for arch in ${archset}; do
416     # Run all appropriate frontend/backend optimization combinations.
417     # For now, this means running 2 combinations for x86 since each
418     # takes about 20 minutes on the bots, and making a single run
419     # elsewhere since e.g. arm takes about 75 minutes.  In a perfect
420     # world, all 4 combinations would be run.
421     if [[ ${archset} =~ x86 ]]; then
422       optset[2]="--opt O3f --opt O0b"
423     fi
424     for opt in "${optset[@]}"; do
425       echo "@@@BUILD_STEP llvm-test-suite ${arch} ${opt} @@@"
426       python ${LLVM_TESTSUITE} --testsuite-prereq --arch ${arch}
427       python ${LLVM_TESTSUITE} --testsuite-clean
428       python ${LLVM_TESTSUITE} \
429         --testsuite-configure --testsuite-run --testsuite-report \
430         --arch ${arch} ${opt} -v -c || handle-error
431     done
432
433     archived-frontend-test ${arch}
434
435     # Note: we do not build the sandboxed translator on this bot
436     # because this would add another 20min to the build time.
437     # The upshot of this is that we are using the sandboxed
438     # toolchain which is currently deps'ed in.
439     # There is a small upside here: we will notice that bitcode has
440     # changed in a way that is incompatible with older translators.
441     # TODO(pnacl-team): rethink this.
442     # Note: the tests which use sandboxed translation are at the end,
443     # because they can sometimes hang on arm, causing buildbot to kill the
444     # script without running any more tests.
445     scons-tests-translator ${arch}
446
447     archived-pexe-translator-test ${arch}
448
449     if [[ ${arch} = x86-64 ]] ; then
450       scons-tests-x86-64-zero-based-sandbox
451     fi
452
453   done
454 }
455
456
457 if [ $# = 0 ]; then
458   # NOTE: this is used for manual testing only
459   tc-test-bot "x86-64 x86-32 arm"
460 else
461   "$@"
462 fi