11123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#!/usr/bin/env python2.7
21123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
31123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# Copyright 2014, ARM Limited
41123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# All rights reserved.
51123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#
61123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# Redistribution and use in source and binary forms, with or without
71123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# modification, are permitted provided that the following conditions are met:
81123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#
91123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#   * Redistributions of source code must retain the above copyright notice,
101123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#     this list of conditions and the following disclaimer.
111123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#   * Redistributions in binary form must reproduce the above copyright notice,
121123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#     this list of conditions and the following disclaimer in the documentation
131123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#     and/or other materials provided with the distribution.
141123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#   * Neither the name of ARM Limited nor the names of its contributors may be
151123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#     used to endorse or promote products derived from this software without
161123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#     specific prior written permission.
171123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl#
181123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
191123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
201123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
211123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
221123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
241123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
251123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
261123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
271123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
291123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixlimport os
301123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixlimport sys
311123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixlimport argparse
321123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixlimport re
331123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixlimport util
341123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
351123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixldef BuildOptions(root):
361123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  result = argparse.ArgumentParser(description = 'Simulator test generator.')
371123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  result.add_argument('--cctest', action='store', default=root+'/cctest',
381123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl                      help='The cctest executable to run.')
391123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  result.add_argument('--out', action='store',
401123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl                      default='test/test-simulator-traces-a64.h')
411123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  return result.parse_args()
421123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
431123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
441123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixlif __name__ == '__main__':
451123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  # $ROOT/tools/generate_simulator_traces.py
461123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  root_dir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
471123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  os.chdir(root_dir)
481123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
491123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  args = BuildOptions(root_dir)
501123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
511123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  # Run each simulator test (SIM_*) with the --sim_test_trace option, and use
521123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  # the output to update the traces header (from --out). The existing traces are
531123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  # wholly replaced, but some boilerplate code exists in the header, so we find
541123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  # the start of the traces, truncate the file to that point, then add the new
551123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  # (updated) trace data.
561123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
571123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  # Find the output section of the traces file.
581123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  marker = [
591123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    '// ---------------------------------------------------------------------',
601123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    '// Expected outputs.',
611123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    '// Everything below this point is automatically generated.',
621123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    '//',
631123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    '// PLEASE DO NOT EDIT ANYTHING BELOW THIS COMMENT.',
641123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    '// ---------------------------------------------------------------------',
651123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    ]
661123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  matched = 0
671123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  f = open(args.out, 'r+')
681123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  # Use readline (rather than for..in) so we can truncate at the right place.
691123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  line = f.readline();
701123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  while line:
711123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    if line.strip() == marker[matched]:
721123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl      matched = matched + 1
731123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl      if matched == len(marker):
741123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl        f.truncate()
751123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl        break
761123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    else:
771123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl      matched = 0
781123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    line = f.readline()
791123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
801123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  if matched != len(marker):
811123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    util.abort('Failed to find output section in ' + args.out + '.')
821123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
831123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  # Find the simulator tests.
841123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  status, output = util.getstatusoutput(args.cctest + ' --list')
851123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  if status != 0: util.abort('Failed to list all tests')
861123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  tests = filter(lambda t: 'SIM_' in t, output.split())
87ae093bfb93c7d6b6cc143546a4211995a9db4ebfarmvixl  tests.sort()
881123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
891123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  # Run each test.
901123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  for test in tests:
911123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    cmd = ' '.join([args.cctest, '--sim_test_trace', test])
921123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    status, output = util.getstatusoutput(cmd)
931123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    if status != 0: util.abort('Failed to run ' + cmd + '.')
941123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
951123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl    f.write('\n\n' + output)
961123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl
971123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  f.write('\n\n')
981123fee00a9cef7f1b448eab3c2ca333dbd426d7armvixl  f.close()
99