blob: 08c399a18a0af27be2ce3a45d09ba1b4fcdd96b0 [file] [log] [blame]
Raef Coles8cbeed52024-03-11 16:24:57 +00001#!/usr/bin/python3
2# -----------------------------------------------------------------------------
3# Copyright (c) 2024, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7# -----------------------------------------------------------------------------
8
9import argparse
10import logging
11import json
12from os import listdir
13from os.path import join, isfile, relpath
14import subprocess
15
16class Source:
17 def __init__(self, location : str):
18 self.type = "git"
19 self.location = location
20 self.refspec = ""
21
22 with open(join(location, ".git", "config"), "rt") as git_config_file:
23 url_line = [x for x in git_config_file.readlines() if "url" in x][0]
24 self.url = url_line.rstrip().replace("\turl = ", "").rstrip()
25
26 with open(join(location, ".git", "HEAD"), "rt") as git_HEAD_file:
27 self.commit = git_HEAD_file.read().rstrip()
28
29 self.location = relpath(location, args.source_dir)
30
31def get_tfm_dependencies(build_dir : str) -> [Source]:
32 dependencies = []
33
34 with open(join(build_dir, "CMakeCache.txt"), "rt") as cmakecache_file:
35 cmakecache = cmakecache_file.readlines()
36 variables = [x.rstrip().split("=") for x in cmakecache if "=" in x]
37 path_variables = [x for x in variables if "PATH" in x[0]]
38
39 for _,p in path_variables:
40 try:
41 dependencies.append(Source(p))
42 except (FileNotFoundError, NotADirectoryError):
43 continue
44
45 return dependencies
46
47parser = argparse.ArgumentParser()
48parser.add_argument("--build_dir", help="TF-M build directory", required=True)
49parser.add_argument("--source_dir", help="TF-M source directory", required=True)
50parser.add_argument("--tools_binary_dir", help="Binary dir in which objdump etc reside", required=False)
51parser.add_argument("--log_level", help="Log level", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], default="ERROR")
52parser.add_argument("--output_config_file", help="output JSON file", required=True)
53parser.add_argument("--output_intermediate_file", help="output intermediate file", required=True)
54parser.add_argument("trace_file", nargs="+", help="input trace log files")
55args = parser.parse_args()
56
57# logging setup
58logging.basicConfig(level=args.log_level)
59
60configuration = {
61 "remove_workspace": True,
62 "include_assembly": True,
63}
64
65if (args.tools_binary_dir):
66 tools_prefix = args.tools_binary_dir
67else:
68 tools_prefix = ""
69
70tfm_source = Source(args.source_dir)
71dependencies = get_tfm_dependencies(args.build_dir)
72
73parameters = {
74 "objdump" : join(tools_prefix, "arm-none-eabi-objdump"),
75 "readelf" : join(tools_prefix, "arm-none-eabi-readelf"),
76 "sources" : [
77 {
78 "type" : x.type,
79 "URL": x.url,
80 "COMMIT" : x.commit,
81 "REFSPEC" : x.refspec,
82 "LOCATION" : x.location,
83 } for x in [tfm_source] + dependencies],
84 "workspace": args.source_dir,
85 "output_file": args.output_intermediate_file,
86}
87
88bin_dir = join(args.build_dir, "bin")
89elf_files = [join(bin_dir, x) for x in listdir(bin_dir) if isfile(join(bin_dir, x)) and "elf" in x]
90
91elfs = [
92 {
93 "name": x,
94 "traces": args.trace_file,
95 } for x in elf_files]
96
97output = {
98 "configuration": configuration,
99 "parameters": parameters,
100 "elfs": elfs,
101}
102
103with open(args.output_config_file, "w+") as output_file:
104 json.dump(output, output_file)