blob: 462989d89ee80a4007176206ca4cbf52fa9c80fc [file] [log] [blame]
Sebastian Bøe13a68402017-11-20 13:03:55 +01001#!/usr/bin/env python3
2#
3# Copyright (c) 2017 Intel Corporation
4#
5# SPDX-License-Identifier: Apache-2.0
6
7import sys
8import re
9import argparse
10import os
11import json
12
13api_regex = re.compile(r'''
14__syscall\s+ # __syscall attribute, must be first
15([^(]+) # type and name of system call (split later)
16[(] # Function opening parenthesis
17([^)]*) # Arg list (split later)
18[)] # Closing parenthesis
19''', re.MULTILINE | re.VERBOSE)
20
Sebastian Bøe13a68402017-11-20 13:03:55 +010021
Adithya Baglodye67720b2018-07-02 14:59:19 +053022def analyze_headers(multiple_directories):
Sebastian Bøe13a68402017-11-20 13:03:55 +010023 ret = []
24
Adithya Baglodye67720b2018-07-02 14:59:19 +053025 for base_path in multiple_directories:
26 for root, dirs, files in os.walk(base_path):
27 for fn in files:
Sebastian Bøe13a68402017-11-20 13:03:55 +010028
Adithya Baglodye67720b2018-07-02 14:59:19 +053029 # toolchain/common.h has the definition of __syscall which we
30 # don't want to trip over
31 path = os.path.join(root, fn)
32 if not fn.endswith(".h") or path.endswith(os.path.join(os.sep, 'toolchain', 'common.h')):
33 continue
Sebastian Bøe13a68402017-11-20 13:03:55 +010034
Adithya Baglodye67720b2018-07-02 14:59:19 +053035 with open(path, "r", encoding="utf-8") as fp:
36 try:
Andrew Boiea698e842018-07-23 17:27:57 -070037 result = [(mo.groups(), fn)
Adithya Baglodye67720b2018-07-02 14:59:19 +053038 for mo in api_regex.finditer(fp.read())]
39 except Exception:
40 sys.stderr.write("While parsing %s\n" % fn)
41 raise
Sebastian Bøe13a68402017-11-20 13:03:55 +010042
Adithya Baglodye67720b2018-07-02 14:59:19 +053043 ret.extend(result)
Sebastian Bøe13a68402017-11-20 13:03:55 +010044
45 return ret
46
Anas Nashif72565532017-12-12 08:19:25 -050047
Sebastian Bøe13a68402017-11-20 13:03:55 +010048def parse_args():
49 global args
Anas Nashif72565532017-12-12 08:19:25 -050050 parser = argparse.ArgumentParser(
51 description=__doc__,
52 formatter_class=argparse.RawDescriptionHelpFormatter)
Sebastian Bøe13a68402017-11-20 13:03:55 +010053
Adithya Baglodye67720b2018-07-02 14:59:19 +053054 parser.add_argument("-i", "--include", required=True, action='append',
Anas Nashif72565532017-12-12 08:19:25 -050055 help="Base include directory")
56 parser.add_argument(
57 "-j", "--json-file", required=True,
58 help="Write system call prototype information as json to file")
Sebastian Bøe13a68402017-11-20 13:03:55 +010059 args = parser.parse_args()
60
Anas Nashif72565532017-12-12 08:19:25 -050061
Sebastian Bøe13a68402017-11-20 13:03:55 +010062def main():
63 parse_args()
64
65 syscalls = analyze_headers(args.include)
66
67 syscalls_in_json = json.dumps(
68 syscalls,
69 indent=4,
70 sort_keys=True
71 )
72
73 # Check if the file already exists, and if there are no changes,
74 # don't touch it since that will force an incremental rebuild
75 path = args.json_file
76 new = syscalls_in_json
77 if os.path.exists(path):
78 with open(path, 'r') as fp:
79 old = fp.read()
80
81 if new != old:
82 with open(path, 'w') as fp:
83 fp.write(new)
84 else:
85 with open(path, 'w') as fp:
86 fp.write(new)
87
Anas Nashif72565532017-12-12 08:19:25 -050088
Sebastian Bøe13a68402017-11-20 13:03:55 +010089if __name__ == "__main__":
90 main()