1#!/bin/bash
2# Copyright 2017 the V8 project 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# Exit immediately if a command exits with a non-zero status.
7set -e
8
9# Treat unset variables as an error when substituting.
10set -u
11
12# return value of a pipeline is the status of the last command to exit with a
13# non-zero status, or zero if no command exited with a non-zero status
14set -o pipefail
15
16log_and_run() {
17  echo ">>" $*
18  if ! $*; then
19    echo "sub-command failed: $*"
20    exit
21  fi
22}
23
24###############################################################################
25# Setup directories.
26###############################################################################
27
28TOOLS_WASM_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
29V8_DIR="${TOOLS_WASM_DIR}/../.."
30SPEC_TEST_DIR=${V8_DIR}/test/wasm-spec-tests
31TMP_DIR=${SPEC_TEST_DIR}/tmp
32
33JS_API_TEST_DIR=${V8_DIR}/test/wasm-js
34
35log_and_run cd ${V8_DIR}
36
37log_and_run rm -rf ${SPEC_TEST_DIR}/tests
38log_and_run mkdir ${SPEC_TEST_DIR}/tests
39
40log_and_run mkdir ${SPEC_TEST_DIR}/tests/proposals
41
42log_and_run rm -rf ${TMP_DIR}
43log_and_run mkdir ${TMP_DIR}
44
45log_and_run rm -rf ${JS_API_TEST_DIR}/tests
46log_and_run mkdir ${JS_API_TEST_DIR}/tests
47log_and_run mkdir ${JS_API_TEST_DIR}/tests/wpt
48log_and_run mkdir ${JS_API_TEST_DIR}/tests/proposals
49
50###############################################################################
51# Generate the spec tests.
52###############################################################################
53
54echo Process spec
55log_and_run cd ${TMP_DIR}
56log_and_run git clone https://github.com/WebAssembly/spec
57log_and_run cd spec/interpreter
58
59# The next step requires that ocaml is installed. See the README.md in
60# https://github.com/WebAssembly/spec/tree/master/interpreter/.
61log_and_run make clean opt
62
63log_and_run cd ${TMP_DIR}/spec/test/core
64log_and_run cp *.wast ${SPEC_TEST_DIR}/tests/
65
66log_and_run ./run.py --wasm ${TMP_DIR}/spec/interpreter/wasm --out ${TMP_DIR}
67log_and_run cp ${TMP_DIR}/*.js ${SPEC_TEST_DIR}/tests/
68
69log_and_run cp -r ${TMP_DIR}/spec/test/js-api/* ${JS_API_TEST_DIR}/tests
70
71###############################################################################
72# Generate the wpt tests.
73###############################################################################
74
75echo Process wpt
76log_and_run cd ${TMP_DIR}
77log_and_run git clone https://github.com/web-platform-tests/wpt
78log_and_run cp -r wpt/wasm/jsapi/* ${JS_API_TEST_DIR}/tests/wpt
79
80log_and_run cd ${JS_API_TEST_DIR}/tests
81for spec_test_name in $(find ./ -name '*.any.js' -not -wholename '*/wpt/*'); do
82  wpt_test_name="wpt/${spec_test_name}"
83  if [ -f "$wpt_test_name" ] && cmp -s $spec_test_name $wpt_test_name ; then
84    log_and_run rm $wpt_test_name
85  elif [ -f "$wpt_test_name" ]; then
86    echo "keep" $wpt_test_name
87  fi
88done
89
90###############################################################################
91# Generate the proposal tests.
92###############################################################################
93
94repos='js-types tail-call simd memory64'
95
96for repo in ${repos}; do
97  echo "Process ${repo}"
98  echo ">> Process core tests"
99  log_and_run cd ${TMP_DIR}
100  log_and_run git clone https://github.com/WebAssembly/${repo}
101  # Compile the spec interpreter to generate the .js test cases later.
102  log_and_run cd ${repo}/interpreter
103  log_and_run make clean opt
104  log_and_run cd ../test/core
105  log_and_run mkdir ${SPEC_TEST_DIR}/tests/proposals/${repo}
106
107  # Iterate over all proposal tests. Those which differ from the spec tests are
108  # copied to the output directory and converted to .js tests.
109  for rel_filename in $(find . -name '*.wast'); do
110    abs_filename=$(realpath $rel_filename)
111    spec_filename=${TMP_DIR}/spec/test/core/${rel_filename}
112    if [ ! -f "$spec_filename" ] || ! cmp -s $abs_filename $spec_filename ; then
113      log_and_run cp ${rel_filename} ${SPEC_TEST_DIR}/tests/proposals/${repo}/
114      log_and_run ./run.py --wasm ../../interpreter/wasm ${rel_filename} --out _build 2> /dev/null
115    fi
116  done
117
118  if ls _build/*.js > /dev/null; then
119    log_and_run cp _build/*.js ${SPEC_TEST_DIR}/tests/proposals/${repo}/
120  fi
121
122  echo ">> Process js-api tests"
123  log_and_run mkdir ${JS_API_TEST_DIR}/tests/proposals/${repo}
124  log_and_run cp -r ${TMP_DIR}/${repo}/test/js-api/* ${JS_API_TEST_DIR}/tests/proposals/${repo}
125  # Delete duplicate tests
126  log_and_run cd ${JS_API_TEST_DIR}/tests
127  for spec_test_name in $(find ./ -name '*.any.js' -not -wholename '*/proposals/*'); do
128    proposal_test_name="proposals/${repo}/${spec_test_name}"
129    if [ -f "$proposal_test_name" ] && cmp -s $spec_test_name $proposal_test_name ; then
130      log_and_run rm $proposal_test_name
131    elif [ -f "$proposal_test_name" ]; then
132      echo "keep" $proposal_test_name
133    fi
134  done
135done
136
137###############################################################################
138# Report and cleanup.
139###############################################################################
140
141cd ${SPEC_TEST_DIR}
142echo
143echo "The following files will get uploaded:"
144ls -R tests
145echo
146
147cd ${JS_API_TEST_DIR}
148ls -R tests
149echo
150
151log_and_run rm -rf ${TMP_DIR}
152
153###############################################################################
154# Upload all spec tests.
155###############################################################################
156
157echo "****************************************************************************"
158echo "* For the following command you first have to authenticate with google cloud"
159echo "* storage. For that you have to execute"
160echo "*"
161echo "* > gsutil.py config"
162echo "*"
163echo "* When the script asks you for your project-id, use 0."
164echo "****************************************************************************"
165log_and_run cd ${SPEC_TEST_DIR}
166log_and_run upload_to_google_storage.py -a -b v8-wasm-spec-tests tests
167
168log_and_run cd ${JS_API_TEST_DIR}
169log_and_run upload_to_google_storage.py -a -b v8-wasm-spec-tests tests
170