]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - tools/expo.py
2 # SPDX-License-Identifier: GPL-2.0+
5 Expo utility - used for testing of expo features
7 Copyright 2023 Google LLC
8 Written by Simon Glass <sjg@chromium.org>
18 #from u_boot_pylib import cros_subprocess
19 from u_boot_pylib
import tools
24 RE_ENUM
= re
.compile(r
'(\S*)(\s*= (\d))?,')
26 # Parse #define <name> "string"
27 RE_DEF
= re
.compile(r
'#define (\S*)\s*"(.*)"')
30 """Figure out the value of the enums in a C file
33 fname (str): Filename to parse
40 Value of #define, if string
42 vals
= collections
.OrderedDict()
43 with
open(fname
, 'r', encoding
='utf-8') as inf
:
46 for line
in inf
.readlines():
51 if in_enum
and line
== '};':
55 if not line
or line
.startswith('/*'):
57 m_enum
= RE_ENUM
.match(line
)
59 cur_id
= int(m_enum
.group(3))
60 vals
[m_enum
.group(1)] = cur_id
63 m_def
= RE_DEF
.match(line
)
65 vals
[m_def
.group(1)] = tools
.to_bytes(m_def
.group(2))
71 """Run the expo program"""
72 fname
= args
.enum_fname
or args
.layout
75 print(f
"Warning: No enum ID values found in file '{fname}'")
77 indata
= tools
.read_file(args
.layout
)
81 for name
, val
in ids
.items():
82 if isinstance(val
, int):
85 outval
= b
'"%s"' % val
86 find_str
= r
'\b%s\b' % name
87 indata
= re
.sub(tools
.to_bytes(find_str
), outval
, indata
)
90 data
= outf
.getvalue()
92 with
open('/tmp/asc', 'wb') as outf
:
94 proc
= subprocess
.run('dtc', input=data
, capture_output
=True)
97 print(f
"Devicetree compiler error:\n{proc.stderr.decode('utf-8')}")
99 tools
.write_file(args
.outfile
, edtb
)
103 def parse_args(argv
):
104 """Parse the command-line arguments
107 argv (list of str): List of string arguments
110 tuple: (options, args) with the command-line options and arugments.
111 options provides access to the options (e.g. option.debug)
112 args is a list of string arguments
114 parser
= argparse
.ArgumentParser()
115 parser
.add_argument('-D', '--debug', action
='store_true',
116 help='Enable full debug traceback')
117 parser
.add_argument('-e', '--enum-fname', type=str,
118 help='.dts or C file containing enum declaration for expo items')
119 parser
.add_argument('-l', '--layout', type=str, required
=True,
120 help='Devicetree file source .dts for expo layout (and perhaps enums)')
121 parser
.add_argument('-o', '--outfile', type=str, required
=True,
122 help='Filename to write expo layout dtb')
124 return parser
.parse_args(argv
)
127 """Start the expo program"""
128 args
= parse_args(sys
.argv
[1:])
131 sys
.tracebacklimit
= 0
133 ret_code
= run_expo(args
)
137 if __name__
== "__main__":