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 | """ |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 8 | Script to scan Zephyr include directories and emit system call and subsystem metadata |
Andrew Boie | c78c5e6 | 2019-03-11 14:45:43 -0700 | [diff] [blame] | 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 |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 13 | C or header files, and building up a database of system calls and their |
Andrew Boie | c78c5e6 | 2019-03-11 14:45:43 -0700 | [diff] [blame] | 14 | function call prototypes. This information is emitted to a generated |
| 15 | JSON file for further processing. |
| 16 | |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 17 | This script also scans for struct definitions such as __subsystem and |
| 18 | __net_socket, emitting a JSON dictionary mapping tags to all the struct |
| 19 | declarations found that were tagged with them. |
| 20 | |
Andrew Boie | c78c5e6 | 2019-03-11 14:45:43 -0700 | [diff] [blame] | 21 | If the output JSON file already exists, its contents are checked against |
| 22 | what information this script would have outputted; if the result is that the |
| 23 | file would be unchanged, it is not modified to prevent unnecessary |
| 24 | incremental builds. |
| 25 | """ |
| 26 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 27 | import sys |
| 28 | import re |
| 29 | import argparse |
| 30 | import os |
| 31 | import json |
| 32 | |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 33 | regex_flags = re.MULTILINE | re.VERBOSE |
| 34 | |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 35 | syscall_regex = re.compile(r''' |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 36 | __syscall\s+ # __syscall attribute, must be first |
| 37 | ([^(]+) # type and name of system call (split later) |
| 38 | [(] # Function opening parenthesis |
| 39 | ([^)]*) # Arg list (split later) |
| 40 | [)] # Closing parenthesis |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 41 | ''', regex_flags) |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 42 | |
Andrew Boie | 299ec8f | 2020-05-29 13:30:19 -0700 | [diff] [blame] | 43 | struct_tags = ["__subsystem", "__net_socket"] |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 44 | |
| 45 | tagged_struct_decl_template = r''' |
| 46 | %s\s+ # tag, must be first |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 47 | struct\s+ # struct keyword is next |
| 48 | ([^{]+) # name of subsystem |
| 49 | [{] # Open curly bracket |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 50 | ''' |
| 51 | |
| 52 | def tagged_struct_update(target_list, tag, contents): |
| 53 | regex = re.compile(tagged_struct_decl_template % tag, regex_flags) |
| 54 | items = [mo.groups()[0].strip() for mo in regex.finditer(contents)] |
| 55 | target_list.extend(items) |
| 56 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 57 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 58 | def analyze_headers(multiple_directories): |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 59 | syscall_ret = [] |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 60 | tagged_ret = {} |
| 61 | |
| 62 | for tag in struct_tags: |
| 63 | tagged_ret[tag] = [] |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 64 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 65 | for base_path in multiple_directories: |
Marc Herbert | d5b2834 | 2019-02-15 18:56:57 -0800 | [diff] [blame] | 66 | for root, dirs, files in os.walk(base_path, topdown=True): |
| 67 | dirs.sort() |
| 68 | files.sort() |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 69 | for fn in files: |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 70 | |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 71 | # toolchain/common.h has the definitions of these tagswhich we |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 72 | # don't want to trip over |
| 73 | path = os.path.join(root, fn) |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 74 | if (not (path.endswith(".h") or path.endswith(".c")) or |
| 75 | path.endswith(os.path.join(os.sep, 'toolchain', |
| 76 | 'common.h'))): |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 77 | continue |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 78 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 79 | with open(path, "r", encoding="utf-8") as fp: |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 80 | contents = fp.read() |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 81 | |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 82 | try: |
| 83 | syscall_result = [(mo.groups(), fn) |
| 84 | for mo in syscall_regex.finditer(contents)] |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 85 | for tag in struct_tags: |
| 86 | tagged_struct_update(tagged_ret[tag], tag, contents) |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 87 | except Exception: |
| 88 | sys.stderr.write("While parsing %s\n" % fn) |
| 89 | raise |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 90 | |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 91 | syscall_ret.extend(syscall_result) |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 92 | |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 93 | return syscall_ret, tagged_ret |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 94 | |
| 95 | |
| 96 | def update_file_if_changed(path, new): |
| 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) |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 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 | def parse_args(): |
| 110 | global args |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 111 | parser = argparse.ArgumentParser( |
| 112 | description=__doc__, |
| 113 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 114 | |
Adithya Baglody | e67720b | 2018-07-02 14:59:19 +0530 | [diff] [blame] | 115 | parser.add_argument("-i", "--include", required=True, action='append', |
Marc Herbert | d5b2834 | 2019-02-15 18:56:57 -0800 | [diff] [blame] | 116 | help='''include directories recursively scanned |
| 117 | for .h files. Can be specified multiple times: |
| 118 | -i topdir1 -i topdir2 ...''') |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 119 | parser.add_argument( |
| 120 | "-j", "--json-file", required=True, |
| 121 | help="Write system call prototype information as json to file") |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 122 | parser.add_argument( |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 123 | "-t", "--tag-struct-file", required=True, |
| 124 | help="Write tagged struct name information as json to file") |
| 125 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 126 | args = parser.parse_args() |
| 127 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 128 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 129 | def main(): |
| 130 | parse_args() |
| 131 | |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 132 | syscalls, tagged = analyze_headers(args.include) |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 133 | |
| 134 | # Only write json files if they don't exist or have changes since |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 135 | # they will force an incremental rebuild. |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 136 | |
| 137 | syscalls_in_json = json.dumps( |
| 138 | syscalls, |
| 139 | indent=4, |
| 140 | sort_keys=True |
| 141 | ) |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 142 | update_file_if_changed(args.json_file, syscalls_in_json) |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 143 | |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 144 | tagged_struct_in_json = json.dumps( |
| 145 | tagged, |
Corey Wharton | ccd15df | 2020-02-29 14:51:42 -0800 | [diff] [blame] | 146 | indent=4, |
| 147 | sort_keys=True |
| 148 | ) |
Andrew Boie | 5960119 | 2020-05-29 13:24:51 -0700 | [diff] [blame] | 149 | update_file_if_changed(args.tag_struct_file, tagged_struct_in_json) |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 150 | |
Anas Nashif | 7256553 | 2017-12-12 08:19:25 -0500 | [diff] [blame] | 151 | |
Sebastian Bøe | 13a6840 | 2017-11-20 13:03:55 +0100 | [diff] [blame] | 152 | if __name__ == "__main__": |
| 153 | main() |