Yong Cong Sin | 335acf8 | 2023-07-11 01:14:25 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2023 Meta |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
| 7 | import argparse |
| 8 | import os |
| 9 | import re |
| 10 | |
| 11 | |
| 12 | def front_matter(): |
| 13 | return f''' |
| 14 | /* |
| 15 | * This file is generated by {__file__} |
| 16 | */ |
| 17 | |
| 18 | #include <zephyr/posix/signal.h> |
| 19 | ''' |
| 20 | |
| 21 | |
| 22 | def gen_strsignal_table(input, output): |
| 23 | with open(input, 'r') as inf: |
| 24 | |
| 25 | highest_signo = 0 |
| 26 | symbols = [] |
| 27 | msgs = {} |
| 28 | |
| 29 | for line in inf.readlines(): |
| 30 | # Select items of the form below (note: SIGNO is numeric) |
| 31 | # #define SYMBOL SIGNO /**< MSG */ |
| 32 | pat = r'^#define[\s]+(SIG[A-Z_]*)[\s]+([1-9][0-9]*)[\s]+/\*\*<[\s]+(.*)[\s]+\*/[\s]*$' |
| 33 | match = re.match(pat, line) |
| 34 | |
| 35 | if not match: |
| 36 | continue |
| 37 | |
| 38 | symbol = match[1] |
| 39 | signo = int(match[2]) |
| 40 | msg = match[3] |
| 41 | |
| 42 | symbols.append(symbol) |
| 43 | msgs[symbol] = msg |
| 44 | |
| 45 | highest_signo = max(int(signo), highest_signo) |
| 46 | |
| 47 | try: |
| 48 | os.makedirs(os.path.dirname(output)) |
| 49 | except BaseException: |
| 50 | # directory already present |
| 51 | pass |
| 52 | |
| 53 | with open(output, 'w') as outf: |
| 54 | |
| 55 | print(front_matter(), file=outf) |
| 56 | |
| 57 | # Generate string table |
| 58 | print( |
| 59 | f'static const char *const strsignal_list[{highest_signo + 1}] = {{', file=outf) |
| 60 | for symbol in symbols: |
| 61 | print(f'\t[{symbol}] = "{msgs[symbol]}",', file=outf) |
| 62 | |
| 63 | print('};', file=outf) |
| 64 | |
| 65 | |
| 66 | def parse_args(): |
| 67 | parser = argparse.ArgumentParser(allow_abbrev=False) |
| 68 | parser.add_argument( |
| 69 | '-i', |
| 70 | '--input', |
| 71 | dest='input', |
| 72 | required=True, |
| 73 | help='input file (e.g. include/zephyr/posix/signal.h)') |
| 74 | parser.add_argument( |
| 75 | '-o', |
| 76 | '--output', |
| 77 | dest='output', |
| 78 | required=True, |
| 79 | help='output file (e.g. build/zephyr/misc/generated/lib/posix/strsignal_table.h)') |
| 80 | |
| 81 | args = parser.parse_args() |
| 82 | |
| 83 | return args |
| 84 | |
| 85 | |
| 86 | def main(): |
| 87 | args = parse_args() |
| 88 | gen_strsignal_table(args.input, args.output) |
| 89 | |
| 90 | |
| 91 | if __name__ == '__main__': |
| 92 | main() |