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 | |
Andrew Boie | c78c5e6 | 2019-03-11 14:45:43 -0700 | [diff] [blame] | 7 | """ |
| 8 | Script to scan Zephyr include directories and emit system call metadata |
| 9 | |
| 10 | System calls require a great deal of boilerplate code in order to implement |
| 11 | completely. This script is the first step in the build system's process of |
| 12 | auto-generating this code by doing a text scan of directories containing |
| 13 | header files, and building up a database of system calls and their |
| 14 | function call prototypes. This information is emitted to a generated |
| 15 | JSON file for further processing. |
| 16 | |
| 17 | If the output JSON file already exists, its contents are checked against |
| 18 | what information this script would have outputted; if the result is that the |
| 19 | file would be unchanged, it is not modified to prevent unnecessary |
| 20 | incremental builds. |
| 21 | """ |
| 22 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 23 | import sys |
| 24 | import re |
| 25 | import argparse |
| 26 | import os |
| 27 | import json |
| 28 | |
| 29 | api_regex = re.compile(r''' |
| 30 | __syscall\s+ # __syscall attribute, must be first |
| 31 | ([^(]+) # type and name of system call (split later) |
| 32 | [(] # Function opening parenthesis |
| 33 | ([^)]*) # Arg list (split later) |
| 34 | [)] # Closing parenthesis |
| 35 | ''', re.MULTILINE | re.VERBOSE) |
| 36 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 37 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 38 | def analyze_headers(multiple_directories): |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 39 | ret = [] |
| 40 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 41 | for base_path in multiple_directories: |
Marc Herbert | d5b2834 | 2019-02-15 18:56:57 -0800 | [diff] [blame] | 42 | for root, dirs, files in os.walk(base_path, topdown=True): |
| 43 | dirs.sort() |
| 44 | files.sort() |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 45 | for fn in files: |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 46 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 47 | # toolchain/common.h has the definition of __syscall which we |
| 48 | # don't want to trip over |
| 49 | path = os.path.join(root, fn) |
| 50 | if not fn.endswith(".h") or path.endswith(os.path.join(os.sep, 'toolchain', 'common.h')): |
| 51 | continue |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 52 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 53 | with open(path, "r", encoding="utf-8") as fp: |
| 54 | try: |
Andrew Boie | a698e84 | 2018-07-23 17:27:57 -0700 | [diff] [blame] | 55 | result = [(mo.groups(), fn) |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 56 | for mo in api_regex.finditer(fp.read())] |
| 57 | except Exception: |
| 58 | sys.stderr.write("While parsing %s\n" % fn) |
| 59 | raise |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 60 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 61 | ret.extend(result) |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 62 | |
| 63 | return ret |
| 64 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 65 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 66 | def parse_args(): |
| 67 | global args |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 68 | parser = argparse.ArgumentParser( |
| 69 | description=__doc__, |
| 70 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 71 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 72 | parser.add_argument("-i", "--include", required=True, action='append', |
Marc Herbert | d5b2834 | 2019-02-15 18:56:57 -0800 | [diff] [blame] | 73 | help='''include directories recursively scanned |
| 74 | for .h files. Can be specified multiple times: |
| 75 | -i topdir1 -i topdir2 ...''') |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 76 | parser.add_argument( |
| 77 | "-j", "--json-file", required=True, |
| 78 | help="Write system call prototype information as json to file") |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 79 | args = parser.parse_args() |
| 80 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 81 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 82 | def main(): |
| 83 | parse_args() |
| 84 | |
| 85 | syscalls = analyze_headers(args.include) |
| 86 | |
| 87 | syscalls_in_json = json.dumps( |
| 88 | syscalls, |
| 89 | indent=4, |
| 90 | sort_keys=True |
| 91 | ) |
| 92 | |
| 93 | # Check if the file already exists, and if there are no changes, |
| 94 | # don't touch it since that will force an incremental rebuild |
| 95 | path = args.json_file |
| 96 | new = syscalls_in_json |
| 97 | if os.path.exists(path): |
| 98 | with open(path, 'r') as fp: |
| 99 | old = fp.read() |
| 100 | |
| 101 | if new != old: |
| 102 | with open(path, 'w') as fp: |
| 103 | fp.write(new) |
| 104 | else: |
| 105 | with open(path, 'w') as fp: |
| 106 | fp.write(new) |
| 107 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 108 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 109 | if __name__ == "__main__": |
| 110 | main() |