blob: 73f52455d3ce0d8857bec54e388be88d47acf63c [file] [log] [blame]
Mike Frysingera23e81f2019-07-01 11:55:50 -04001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2019 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Print errno table as markdown."""
8
9from __future__ import print_function
10
11import argparse
12import os
Mike Frysinger65de7442022-01-06 02:51:42 -050013from pathlib import Path
Mike Frysingera23e81f2019-07-01 11:55:50 -040014import subprocess
15import sys
16
17import constants
18
19
Mike Frysinger65de7442022-01-06 02:51:42 -050020# The directory where all the code & markdown files live.
21TOPDIR = Path(__file__).resolve().parent
22
Mike Frysingera23e81f2019-07-01 11:55:50 -040023# The C library header to find symbols.
24HEADER = 'errno.h'
25
26# The markdown file where we store this table.
27MARKDOWN = 'errnos.md'
28
29# The string we use to start the table header.
30START_OF_TABLE = '| number |'
31
32
33def find_symbols(target):
34 """Find all the symbols using |target|."""
35 cc = '%s-clang' % (target,)
36 ret = subprocess.run([cc, '-E', '-dD', '-P', '-'],
37 input='#include <%s>\n' % (HEADER,),
38 stdout=subprocess.PIPE,
39 encoding='utf-8')
40 table = {}
41 for line in ret.stdout.splitlines():
42 # If we need to make this smarter, signals.py handles things.
43 assert not line.startswith('#undef E'), '#undef not handled'
44 if line.startswith('#define E'):
45 sym, val = line.strip().split()[1:3]
46 assert sym not in table, 'sym %s already found' % (sym,)
47 table[sym] = val
48 return table
49
50
51def load_table():
52 """Return a table of all the symbol values (and aliases)."""
53 all_tables = {}
54 for target in constants.TARGETS:
55 all_tables[target] = find_symbols(target)
56
Mike Frysinger2db7b852020-09-10 04:37:44 -040057 # Check that all the tables are the same.
Mike Frysingera23e81f2019-07-01 11:55:50 -040058 basetarget = constants.TARGETS[0]
59 baseline = all_tables[basetarget]
60 for target, table in all_tables.items():
61 assert baseline == table
62
63 # Sometimes values have multiple names.
64 aliases = {}
65 for sym, val in baseline.items():
66 try:
67 int(val)
68 except ValueError:
69 aliases.setdefault(val, []).append(sym)
70
71 return (baseline, aliases)
72
73
74def sort_table(table):
75 """Return a sorted table."""
76 def sorter(element):
77 try:
78 num = int(element[1])
79 except ValueError:
80 num = 0
81 return (num, element[0])
82 return sorted(table.items(), key=sorter)
83
84
85def get_md_table(table, aliases):
86 """Return the table in markdown format."""
87 ret = []
88 last_num = 0
89 for sym, val in sort_table(table):
90 try:
91 num = int(val)
92 except ValueError:
93 continue
94
95 # Fill in holes in the table so it's obvious to the user when searching.
96 for stub in range(last_num + 1, num):
97 ret.append('| %i | 0x%02x | | *not implemented* ||' % (stub, stub))
98 last_num = num
99
100 desc = os.strerror(num)
101 ret.append('| %i | 0x%02x | %s | %s |' % (num, num, sym, desc))
102 for alias in aliases.get(sym, []):
103 ret.append('| %i | 0x%02x | %s | *(Same value as %s)* %s |' %
104 (num, num, alias, sym, desc))
105 return ret
106
107
108def get_parser():
109 """Return a command line parser."""
110 parser = argparse.ArgumentParser(description=__doc__)
111 parser.add_argument('-i', '--inplace', action='store_true',
112 help='Update the markdown file directly.')
113 return parser
114
115
116def main(argv):
117 """The main func!"""
118 parser = get_parser()
119 opts = parser.parse_args(argv)
120
121 baseline, aliases = load_table()
122 md_data = get_md_table(baseline, aliases)
123
124 if opts.inplace:
Mike Frysinger65de7442022-01-06 02:51:42 -0500125 md_file = TOPDIR / MARKDOWN
126 old_data = md_file.read_text().splitlines(keepends=True)
Mike Frysingera23e81f2019-07-01 11:55:50 -0400127
128 i = None
129 for i, line in enumerate(old_data):
130 if line.startswith(START_OF_TABLE):
131 break
132 else:
133 print('ERROR: could not find table in %s' % (md_file,),
134 file=sys.stderr)
135 sys.exit(1)
136
137 old_data = old_data[0:i + 2]
Mike Frysinger65de7442022-01-06 02:51:42 -0500138 with md_file.open('w') as fp:
Mike Frysingera23e81f2019-07-01 11:55:50 -0400139 fp.writelines(old_data)
140 fp.write('\n'.join(md_data) + '\n')
141 else:
142 print('\n'.join(md_data))
143
144
145if __name__ == '__main__':
146 sys.exit(main(sys.argv[1:]))