Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / native_client / pnacl / test.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 #@                   PNaCl Test Helper
7 #@-------------------------------------------------------------------
8 #@
9 #@ SCons Test Usage:
10 #@
11 #@   test.sh test-<arch>[-sbtc] [extra_arguments_to_scons]
12 #@
13 #@      Runs the SCons tests with selected arch (optionally with sandboxed tc)
14 #@      Valid arches:
15 #@          x86-32
16 #@          x86-64
17 #@          arm
18 #@
19 #@      Examples:
20 #@          test.sh test-x86-32
21 #@          test.sh test-arm-sbtc
22 #@
23 #@ The env variables: PNACL_CONCURRENCY, PNACL_BUILDBOT, PNACL_DEBUG
24 #@ control behavior of this script
25 #@
26 #@
27
28 ######################################################################
29 # Config
30 ######################################################################
31
32 set -o nounset
33 set -o errexit
34
35 # The script is located in "pnacl/".
36 # Set pwd to native_client/
37 cd "$(dirname "$0")"/..
38 if [[ $(basename "$(pwd)") != "native_client" ]] ; then
39   echo "ERROR: cannot find native_client/ directory"
40   exit -1
41 fi
42 readonly NACL_ROOT="$(pwd)"
43
44 readonly DRYRUN=${DRYRUN:-false}
45
46 source pnacl/scripts/common-tools.sh
47 SetScriptPath "${NACL_ROOT}/pnacl/test.sh"
48 SetLogDirectory "${NACL_ROOT}/toolchain/test-log"
49
50 # For different levels of make parallelism change this in your env
51 readonly PNACL_CONCURRENCY=${PNACL_CONCURRENCY:-8}
52
53 # This needs to be kept in sync with the var of the same name in build.sh
54 readonly TC_BUILD_LLVM="$(pwd)/pnacl/build/llvm_${HOST_ARCH}"
55
56 ######################################################################
57 ######################################################################
58 #
59 #   < TESTING >
60 #
61 ######################################################################
62 ######################################################################
63
64 # TODO(robertm): figure out what to do about concurrency in debug mode.
65 # Perhaps it is fine just tweaking that via PNACL_CONCURRENCY.
66 if ${PNACL_DEBUG} || ${PNACL_BUILDBOT}; then
67   readonly SCONS_ARGS=(MODE=nacl,opt-host
68                        bitcode=1
69                        --verbose
70                        -j${PNACL_CONCURRENCY})
71 else
72   readonly SCONS_ARGS=(MODE=nacl,opt-host
73                        bitcode=1
74                        naclsdk_validate=0
75                        sysinfo=0
76                        -j${PNACL_CONCURRENCY})
77 fi
78
79 #@ show-tests            - see what tests can be run
80 show-tests() {
81   StepBanner "SHOWING TESTS"
82   cat $(find tests -name nacl.scons) | grep -o 'run_[A-Za-z_-]*' | sort | uniq
83 }
84
85 Run() {
86   echo "$@"
87   if ! ${DRYRUN}; then
88     "$@"
89   fi
90 }
91
92 RunScons() {
93   local arch="$1"
94   shift 1
95   Run ./scons "${SCONS_ARGS[@]}" platform=${arch} "$@"
96 }
97
98 # Returns true if the arguments specify a test
99 # or target name to SCons.
100 has-target-name() {
101   while [ $# -gt 0 ]; do
102     # Skip arguments of the form -foo, --foo, or foo=bar.
103     if [[ "$1" =~ ^-.* ]] || [[ "$1" =~ = ]]; then
104       shift 1
105       continue
106     fi
107     return 0
108   done
109   return 1
110 }
111
112 scons-clean () {
113   local arch=$1
114   local mode=$2
115   local frontend=clang
116   # Clear both the pexe and the nonpexe scons-out directory, since we run
117   # a mix of both tests.
118   if [ "${mode}" == "native" ] ; then
119     Run rm -rf scons-out/nacl-${arch}-pnacl-${frontend}
120     Run rm -rf scons-out/nacl-${arch}-pnacl-pexe-${frontend}
121   else
122     Run rm -rf scons-out/nacl-${arch}-pnacl-${mode}-${frontend}
123     Run rm -rf scons-out/nacl-${arch}-pnacl-${mode}-pexe-${frontend}
124   fi
125 }
126
127 build-sbtc-prerequisites() {
128   local arch=$1
129   # Sandboxed translators currently only require irt_core since they do not
130   # use PPAPI.
131   RunScons ${arch} sel_ldr sel_universal irt_core
132 }
133
134 #+ Run scons test under a certain configuration
135 #+ scons-tests <arch> <mode={sbtc|}> [optional list of test names]
136 #+ If no optional tests are listed, we will build all the tests then
137 #+ run the "smoke_tests" test suite.
138 scons-tests () {
139   local arch="$1"
140   local mode="$2"
141   shift 2
142   scons-clean ${arch} ${mode}
143
144   local modeflags=""
145   if [ ${mode} == "sbtc" ]; then
146     build-sbtc-prerequisites "${arch}"
147     modeflags="use_sandboxed_translator=1"
148   fi
149
150   if has-target-name "$@" ; then
151     # By default this uses pexe mode (except where not supported) but this
152     # can be overridden
153     RunScons ${arch} ${modeflags} "$@"
154   else
155     # For now, mostly just duplicate mode-buildbot-x86 in buildbot_pnacl.sh
156     # (but don't bother separating build/run for now) until we
157     # converge on exactly what we want
158     RunScons ${arch} ${modeflags} "$@" smoke_tests
159     if [ ${mode} != "sbtc" ]; then
160       # nonpexe tests
161       RunScons ${arch} ${modeflags} pnacl_generate_pexe=0 "$@" nonpexe_tests
162     fi
163   fi
164 }
165
166 test-driver() {
167   ${NACL_ROOT}/pnacl/driver/tests/driver_tests.py --platform="$1"
168 }
169 test-arm()        { test-driver arm && scons-tests arm native "$@" ; }
170 test-x86-32()     { test-driver x86-32 && scons-tests x86-32 native "$@" ; }
171 test-x86-64()     { test-driver x86-64 && scons-tests x86-64 native "$@" ; }
172
173 test-arm-sbtc()    { scons-tests arm sbtc "$@" ; }
174 test-x86-32-sbtc() { scons-tests x86-32 sbtc "$@" ; }
175 test-x86-64-sbtc() { scons-tests x86-64 sbtc "$@" ; }
176
177 #@
178 #@ test-spec <official-spec-dir> <setup> [ref|train] [<benchmarks>]*
179 #@                       - run spec tests
180 test-spec() {
181   if [[ $# -lt 2 ]]; then
182     echo "not enough arguments for test-spec"
183     exit 1
184   fi;
185   official=$(GetAbsolutePath $1)
186   setup=$2
187   shift 2
188   spushd tests/spec2k
189   ./run_all.sh BuildPrerequisitesSetupBased ${setup}
190   ./run_all.sh CleanBenchmarks "$@"
191   ./run_all.sh PopulateFromSpecHarness ${official} "$@"
192   ./run_all.sh BuildAndRunBenchmarks ${setup} "$@"
193   spopd
194 }
195
196 #+ CollectTimingInfo <directory> <timing_result_file> <tagtype...>
197 #+  CD's into the directory in a subshell and collects all the
198 #+  relevant timed run info
199 #+  tagtype just gets printed out.
200 CollectTimingInfo() {
201   wd=$1
202   result_file=$2
203   setup=$3
204   (cd ${wd};
205    mkdir -p $(dirname ${result_file})
206    echo "##################################################" >>${result_file}
207    date +"# Completed at %F %H:%M:%S %A ${result_file}" >> ${result_file}
208    echo "# " ${wd}
209    echo "#" $(uname -a) >> ${result_file}
210    echo "# SETUP: ${setup}" >>${result_file}
211    echo "##################################################" >>${result_file}
212    echo "# COMPILE " >> ${result_file}
213    for ff in $(find . -name "*.compile_time"); do
214      cat ${ff} >> ${result_file}
215    done
216    echo "# RUN " >> ${result_file}
217    for ff in $(find . -name "*.run_time"); do
218      cat ${ff} >> ${result_file}
219    done
220    cat ${result_file}
221   )
222 }
223
224 #@
225 #@ timed-test-spec <result-file> <official-spec-dir> <setup> ... - run spec and
226 #@  measure time / size data. Data is emitted to stdout, but also collected
227 #@  in <result-file>. <result-file> is not cleared across runs (but temp files
228 #@  are cleared on each run).
229 #@  Note that the VERIFY variable effects the timing!
230 timed-test-spec() {
231   if ${BUILD_PLATFORM_MAC} ; then
232     echo "Timed-test-spec is not currently supported on MacOS"
233     echo "Namely, /usr/bin/time -f is not supported."
234     exit -1
235   fi
236   if [ "$#" -lt "3" ]; then
237     echo "timed-test-spec {result-file} {spec2krefdir} {setupfunc}" \
238          "[ref|train] [benchmark]*"
239     exit 1
240   fi
241   result_file=$1
242   official=$(GetAbsolutePath $2)
243   setup=$3
244   shift 3
245   spushd tests/spec2k
246   ./run_all.sh BuildPrerequisitesSetupBased ${setup}
247   ./run_all.sh CleanBenchmarks "$@"
248   ./run_all.sh PopulateFromSpecHarness ${official} "$@"
249   ./run_all.sh TimedBuildAndRunBenchmarks ${setup} "$@"
250   CollectTimingInfo $(pwd) ${result_file} ${setup}
251   spopd
252 }
253
254 #@ help                  - Usage information.
255 help() {
256   Usage
257 }
258
259 #@ help-full             - Usage information including internal functions.
260 help-full() {
261   Usage2
262 }
263
264 ######################################################################
265 ######################################################################
266 #
267 #                               < MAIN >
268 #
269 ######################################################################
270 ######################################################################
271
272 [ $# = 0 ] && set -- help  # Avoid reference to undefined $1.
273 if [ "$(type -t $1)" != "function" ]; then
274   #Usage
275   echo "ERROR: unknown function '$1'." >&2
276   echo "For help, try:"
277   echo "    $0 help"
278   exit 1
279 fi
280
281 "$@"