Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2017 Intel Corporation |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
| 7 | import sys |
| 8 | import re |
| 9 | import argparse |
| 10 | import os |
| 11 | import json |
| 12 | |
| 13 | api_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øe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 21 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 22 | def analyze_headers(multiple_directories): |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 23 | ret = [] |
| 24 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 25 | for base_path in multiple_directories: |
| 26 | for root, dirs, files in os.walk(base_path): |
| 27 | for fn in files: |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 28 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 29 | # 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øe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 34 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 35 | with open(path, "r", encoding="utf-8") as fp: |
| 36 | try: |
Andrew Boie | a698e84 | 2018-07-23 17:27:57 -0700 | [diff] [blame] | 37 | result = [(mo.groups(), fn) |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 38 | for mo in api_regex.finditer(fp.read())] |
| 39 | except Exception: |
| 40 | sys.stderr.write("While parsing %s\n" % fn) |
| 41 | raise |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 42 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 43 | ret.extend(result) |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 44 | |
| 45 | return ret |
| 46 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 47 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 48 | def parse_args(): |
| 49 | global args |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 50 | parser = argparse.ArgumentParser( |
| 51 | description=__doc__, |
| 52 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 53 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 54 | parser.add_argument("-i", "--include", required=True, action='append', |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 55 | 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øe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 59 | args = parser.parse_args() |
| 60 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 61 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 62 | def 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 Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 88 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 89 | if __name__ == "__main__": |
| 90 | main() |