1#!/usr/bin/env python2.7
2
3# Copyright 2014, ARM Limited
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8#
9#   * Redistributions of source code must retain the above copyright notice,
10#     this list of conditions and the following disclaimer.
11#   * Redistributions in binary form must reproduce the above copyright notice,
12#     this list of conditions and the following disclaimer in the documentation
13#     and/or other materials provided with the distribution.
14#   * Neither the name of ARM Limited nor the names of its contributors may be
15#     used to endorse or promote products derived from this software without
16#     specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29import os
30import sys
31import argparse
32import re
33import util
34
35def BuildOptions(root):
36  result = argparse.ArgumentParser(description = 'Simulator test generator.')
37  result.add_argument('--cctest', action='store', default=root+'/cctest',
38                      help='The cctest executable to run.')
39  result.add_argument('--out', action='store',
40                      default='test/test-simulator-traces-a64.h')
41  return result.parse_args()
42
43
44if __name__ == '__main__':
45  # $ROOT/tools/generate_simulator_traces.py
46  root_dir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
47  os.chdir(root_dir)
48
49  args = BuildOptions(root_dir)
50
51  # Run each simulator test (SIM_*) with the --sim_test_trace option, and use
52  # the output to update the traces header (from --out). The existing traces are
53  # wholly replaced, but some boilerplate code exists in the header, so we find
54  # the start of the traces, truncate the file to that point, then add the new
55  # (updated) trace data.
56
57  # Find the output section of the traces file.
58  marker = [
59    '// ---------------------------------------------------------------------',
60    '// Expected outputs.',
61    '// Everything below this point is automatically generated.',
62    '//',
63    '// PLEASE DO NOT EDIT ANYTHING BELOW THIS COMMENT.',
64    '// ---------------------------------------------------------------------',
65    ]
66  matched = 0
67  f = open(args.out, 'r+')
68  # Use readline (rather than for..in) so we can truncate at the right place.
69  line = f.readline();
70  while line:
71    if line.strip() == marker[matched]:
72      matched = matched + 1
73      if matched == len(marker):
74        f.truncate()
75        break
76    else:
77      matched = 0
78    line = f.readline()
79
80  if matched != len(marker):
81    util.abort('Failed to find output section in ' + args.out + '.')
82
83  # Find the simulator tests.
84  status, output = util.getstatusoutput(args.cctest + ' --list')
85  if status != 0: util.abort('Failed to list all tests')
86  tests = filter(lambda t: 'SIM_' in t, output.split())
87  tests.sort()
88
89  # Run each test.
90  for test in tests:
91    cmd = ' '.join([args.cctest, '--sim_test_trace', test])
92    status, output = util.getstatusoutput(cmd)
93    if status != 0: util.abort('Failed to run ' + cmd + '.')
94
95    f.write('\n\n' + output)
96
97  f.write('\n\n')
98  f.close()
99