diff options
author | Mike Frysinger <vapier@gentoo.org> | 2021-07-06 22:10:53 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2021-10-31 04:31:28 -0400 |
commit | a7e40a99318c46ec57a9c0a64c622b6ec0ed89ff (patch) | |
tree | 85e48ec389ff5f1d3b5a60289517a558cc2937e0 /sim/common/gennltvals.py | |
parent | f9cd2be59c1c1d56d95a191f82298ee92cf41231 (diff) | |
download | binutils-a7e40a99318c46ec57a9c0a64c622b6ec0ed89ff.zip binutils-a7e40a99318c46ec57a9c0a64c622b6ec0ed89ff.tar.gz binutils-a7e40a99318c46ec57a9c0a64c622b6ec0ed89ff.tar.bz2 |
sim: nltvals: pull target errno out into a dedicated source file
The current system maintains a list of target errno constants in the
nltvals.def file, then runs a build-time tool to turn that into a C
file. This list of errno values is the same for all arches, so we
don't need the arch-specific flexibility. Further, these are only
for newlib/libgloss environments, which makes it confusing to support
other userland runtimes (like Linux). Let's simplify to make this
easier to understand & build. We don't namespace the variables yet,
but sets up the framework for it.
Create a new target-newlib-errno.c template file. The template file
is hand written, but the inline map is still automatically generated.
This allows us to move it to the common set of objects so it's only
built once in a multi-target build.
Now we can remove the output from the gentmap build-time tool since
it's checked into the tree.
Then we stop including the errno lists in nltvals.def since nothing
uses it.
Diffstat (limited to 'sim/common/gennltvals.py')
-rwxr-xr-x | sim/common/gennltvals.py | 109 |
1 files changed, 74 insertions, 35 deletions
diff --git a/sim/common/gennltvals.py b/sim/common/gennltvals.py index 955ace34..3006f7f 100755 --- a/sim/common/gennltvals.py +++ b/sim/common/gennltvals.py @@ -63,8 +63,43 @@ FILE_HEADER = f"""\ /* This file is machine generated by {PROG}. */\ """ +# Used to update sections of files. +START_MARKER = 'gennltvals: START' +END_MARKER = 'gennltvals: END' -def gentvals(output: TextIO, cpp: str, srctype: str, srcdir: Path, + +def extract_syms(cpp: str, srcdir: Path, + headers: Iterable[str], + pattern: str, + filter: str = r'^$') -> dict: + """Extract all the symbols from |headers| matching |pattern| using |cpp|.""" + srcfile = ''.join(f'#include <{x}>\n' for x in headers) + syms = set() + define_pattern = re.compile(r'^#\s*define\s+(' + pattern + ')') + filter_pattern = re.compile(filter) + for header in headers: + with open(srcdir / header, 'r', encoding='utf-8') as fp: + data = fp.read() + for line in data.splitlines(): + m = define_pattern.match(line) + if m and not filter_pattern.search(line): + syms.add(m.group(1)) + for sym in syms: + srcfile += f'#ifdef {sym}\nDEFVAL "{sym}" {sym}\n#endif\n' + + result = subprocess.run( + f'{cpp} -E -I"{srcdir}" -', shell=True, check=True, encoding='utf-8', + input=srcfile, capture_output=True) + ret = {} + for line in result.stdout.splitlines(): + if line.startswith('DEFVAL '): + _, sym, val = line.split() + ret[sym.strip('"')] = val + return ret + + +def gentvals(output_dir: Path, output: TextIO, + cpp: str, srctype: str, srcdir: Path, headers: Iterable[str], pattern: str, filter: str = r'^$', @@ -80,6 +115,29 @@ def gentvals(output: TextIO, cpp: str, srctype: str, srcdir: Path, fullpath = srcdir / header assert fullpath.exists(), f'{fullpath} does not exist' + syms = extract_syms(cpp, srcdir, headers, pattern, filter) + + # If we have a map file, use it directly. + target_map = output_dir / f'target-newlib-{srctype}.c' + if target_map.exists(): + old_lines = target_map.read_text().splitlines() + start_i = end_i = None + for i, line in enumerate(old_lines): + if START_MARKER in line: + start_i = i + if END_MARKER in line: + end_i = i + assert start_i and end_i + new_lines = old_lines[0:start_i + 1] + new_lines.extend( + f'#ifdef {sym}\n' + f' {{ "{sym}", {sym}, {val} }},\n' + f'#endif' for sym, val in sorted(syms.items())) + new_lines.extend(old_lines[end_i:]) + target_map.write_text('\n'.join(new_lines) + '\n') + return + + # Fallback to classic nltvals.def. if target is not None: print(f'#ifdef NL_TARGET_{target}', file=output) print(f'#ifdef {srctype}_defs', file=output) @@ -91,27 +149,8 @@ def gentvals(output: TextIO, cpp: str, srctype: str, srcdir: Path, else: print(f'/* begin {target} {srctype} target macros */', file=output) - # Extract all the symbols. - srcfile = ''.join(f'#include <{x}>\n' for x in headers) - syms = set() - define_pattern = re.compile(r'^#\s*define\s+(' + pattern + ')') - filter_pattern = re.compile(filter) - for header in headers: - with open(srcdir / header, 'r', encoding='utf-8') as fp: - data = fp.read() - for line in data.splitlines(): - m = define_pattern.match(line) - if m and not filter_pattern.search(line): - syms.add(m.group(1)) - for sym in sorted(syms): - srcfile += f'#ifdef {sym}\nDEFVAL {{ "{sym}", {sym} }},\n#endif\n' - - result = subprocess.run( - f'{cpp} -E -I"{srcdir}" -', shell=True, check=True, encoding='utf-8', - input=srcfile, capture_output=True) - for line in result.stdout.splitlines(): - if line.startswith('DEFVAL '): - print(line[6:].rstrip(), file=output) + for sym, val in sorted(syms.items()): + print(f' {{ "{sym}", {val} }},', file=output) print(f'#undef {srctype}_defs', file=output) if target is None: @@ -122,37 +161,37 @@ def gentvals(output: TextIO, cpp: str, srctype: str, srcdir: Path, print('#endif', file=output) -def gen_common(output: TextIO, newlib: Path, cpp: str): +def gen_common(output_dir: Path, output: TextIO, newlib: Path, cpp: str): """Generate the common C library constants. No arch should override these. """ - gentvals(output, cpp, 'errno', newlib / 'newlib/libc/include', + gentvals(output_dir, output, cpp, 'errno', newlib / 'newlib/libc/include', ('errno.h', 'sys/errno.h'), 'E[A-Z0-9]*') - gentvals(output, cpp, 'signal', newlib / 'newlib/libc/include', + gentvals(output_dir, output, cpp, 'signal', newlib / 'newlib/libc/include', ('signal.h', 'sys/signal.h'), r'SIG[A-Z0-9]*', filter=r'SIGSTKSZ') - gentvals(output, cpp, 'open', newlib / 'newlib/libc/include', + gentvals(output_dir, output, cpp, 'open', newlib / 'newlib/libc/include', ('fcntl.h', 'sys/fcntl.h', 'sys/_default_fcntl.h'), r'O_[A-Z0-9]*') -def gen_targets(output: TextIO, newlib: Path, cpp: str): +def gen_targets(output_dir: Path, output: TextIO, newlib: Path, cpp: str): """Generate the target-specific lists.""" for target, subdir in sorted(TARGET_DIRS.items()): - gentvals(output, cpp, 'sys', newlib / subdir, ('syscall.h',), - r'SYS_[_a-zA-Z0-9]*', target=target) + gentvals(output_dir, output, cpp, 'sys', newlib / subdir, + ('syscall.h',), r'SYS_[_a-zA-Z0-9]*', target=target) # Then output the common syscall targets. - gentvals(output, cpp, 'sys', newlib / 'libgloss', ('syscall.h',), - r'SYS_[_a-zA-Z0-9]*') + gentvals(output_dir, output, cpp, 'sys', newlib / 'libgloss', + ('syscall.h',), r'SYS_[_a-zA-Z0-9]*') -def gen(output: TextIO, newlib: Path, cpp: str): +def gen(output_dir: Path, output: TextIO, newlib: Path, cpp: str): """Generate all the things!""" print(FILE_HEADER, file=output) - gen_common(output, newlib, cpp) - gen_targets(output, newlib, cpp) + gen_common(output_dir, output, newlib, cpp) + gen_targets(output_dir, output, newlib, cpp) def get_parser() -> argparse.ArgumentParser: @@ -212,7 +251,7 @@ def main(argv: List[str]) -> int: output = (opts.output / 'nltvals.def').open('w', encoding='utf-8') - gen(output, opts.newlib, opts.cpp) + gen(opts.output, output, opts.newlib, opts.cpp) return 0 |