1b8021494Sopenharmony_ci#!/usr/bin/env python3
2b8021494Sopenharmony_ci
3b8021494Sopenharmony_ci# Copyright 2016, VIXL authors
4b8021494Sopenharmony_ci# All rights reserved.
5b8021494Sopenharmony_ci#
6b8021494Sopenharmony_ci# Redistribution and use in source and binary forms, with or without
7b8021494Sopenharmony_ci# modification, are permitted provided that the following conditions are met:
8b8021494Sopenharmony_ci#
9b8021494Sopenharmony_ci#   * Redistributions of source code must retain the above copyright notice,
10b8021494Sopenharmony_ci#     this list of conditions and the following disclaimer.
11b8021494Sopenharmony_ci#   * Redistributions in binary form must reproduce the above copyright notice,
12b8021494Sopenharmony_ci#     this list of conditions and the following disclaimer in the documentation
13b8021494Sopenharmony_ci#     and/or other materials provided with the distribution.
14b8021494Sopenharmony_ci#   * Neither the name of ARM Limited nor the names of its contributors may be
15b8021494Sopenharmony_ci#     used to endorse or promote products derived from this software without
16b8021494Sopenharmony_ci#     specific prior written permission.
17b8021494Sopenharmony_ci#
18b8021494Sopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
19b8021494Sopenharmony_ci# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20b8021494Sopenharmony_ci# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21b8021494Sopenharmony_ci# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22b8021494Sopenharmony_ci# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b8021494Sopenharmony_ci# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24b8021494Sopenharmony_ci# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25b8021494Sopenharmony_ci# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26b8021494Sopenharmony_ci# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27b8021494Sopenharmony_ci# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28b8021494Sopenharmony_ci
29b8021494Sopenharmony_ciimport os
30b8021494Sopenharmony_ciimport sys
31b8021494Sopenharmony_ciimport argparse
32b8021494Sopenharmony_ciimport re
33b8021494Sopenharmony_ciimport util
34b8021494Sopenharmony_ci
35b8021494Sopenharmony_ci
36b8021494Sopenharmony_cidef BuildOptions(root):
37b8021494Sopenharmony_ci  result = argparse.ArgumentParser(
38b8021494Sopenharmony_ci      description = 'Generate reference output for TRACE_* tests')
39b8021494Sopenharmony_ci  result.add_argument('--runner', action='store',
40b8021494Sopenharmony_ci                      default=os.path.join(root, 'obj/latest/test/test-runner'),
41b8021494Sopenharmony_ci                      help='The test executable to run.')
42b8021494Sopenharmony_ci  result.add_argument('--outdir', action='store',
43b8021494Sopenharmony_ci                      default='test/test-trace-reference/')
44b8021494Sopenharmony_ci  return result.parse_args()
45b8021494Sopenharmony_ci
46b8021494Sopenharmony_ciif __name__ == '__main__':
47b8021494Sopenharmony_ci  root_dir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
48b8021494Sopenharmony_ci  os.chdir(root_dir)
49b8021494Sopenharmony_ci
50b8021494Sopenharmony_ci  args = BuildOptions(root_dir)
51b8021494Sopenharmony_ci
52b8021494Sopenharmony_ci  # Run each trace test (TRACE_*) with the --generate_test_trace option, and
53b8021494Sopenharmony_ci  # use the output to create the reference traces (in --outdir).
54b8021494Sopenharmony_ci
55b8021494Sopenharmony_ci  # Find the trace tests.
56b8021494Sopenharmony_ci  status, output = util.getstatusoutput(args.runner + ' --list')
57b8021494Sopenharmony_ci  if status != 0: util.abort('Failed to list all tests')
58b8021494Sopenharmony_ci  tests = [t for t in output.split() if 'TRACE_' in t]
59b8021494Sopenharmony_ci  tests.sort()
60b8021494Sopenharmony_ci
61b8021494Sopenharmony_ci  if not os.path.exists(args.outdir):
62b8021494Sopenharmony_ci    os.makedirs(args.outdir)
63b8021494Sopenharmony_ci
64b8021494Sopenharmony_ci  for test in tests:
65b8021494Sopenharmony_ci    # Run each test.
66b8021494Sopenharmony_ci    print('Generating trace for ' + test);
67b8021494Sopenharmony_ci    cmd = ' '.join([args.runner, '--generate_test_trace', test])
68b8021494Sopenharmony_ci    status, output = util.getstatusoutput(cmd)
69b8021494Sopenharmony_ci    if status != 0: util.abort('Failed to run ' + cmd + '.')
70b8021494Sopenharmony_ci
71b8021494Sopenharmony_ci    # Create a new trace header file.
72b8021494Sopenharmony_ci    trace_filename = 'log-' + test.replace('TRACE_', '').lower().replace('_', '-')
73b8021494Sopenharmony_ci    trace_f =  open(os.path.join(args.outdir, trace_filename), 'w')
74b8021494Sopenharmony_ci    trace_f.write(output)
75b8021494Sopenharmony_ci    # `getstatusoutput` removes the trailing newline. Put it back, but only if
76b8021494Sopenharmony_ci    # the output was not empty.
77b8021494Sopenharmony_ci    if len(output) > 0: trace_f.write('\n')
78