2017-11-20 20:03:55 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright (c) 2017 Intel Corporation
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2019-03-12 05:45:43 +08:00
|
|
|
"""
|
2020-03-01 06:51:42 +08:00
|
|
|
Script to scan Zephyr include directories and emit system call and subsystem metadata
|
2019-03-12 05:45:43 +08:00
|
|
|
|
|
|
|
System calls require a great deal of boilerplate code in order to implement
|
|
|
|
completely. This script is the first step in the build system's process of
|
|
|
|
auto-generating this code by doing a text scan of directories containing
|
2020-05-30 04:24:51 +08:00
|
|
|
C or header files, and building up a database of system calls and their
|
2019-03-12 05:45:43 +08:00
|
|
|
function call prototypes. This information is emitted to a generated
|
|
|
|
JSON file for further processing.
|
|
|
|
|
2020-05-30 04:24:51 +08:00
|
|
|
This script also scans for struct definitions such as __subsystem and
|
|
|
|
__net_socket, emitting a JSON dictionary mapping tags to all the struct
|
|
|
|
declarations found that were tagged with them.
|
|
|
|
|
2019-03-12 05:45:43 +08:00
|
|
|
If the output JSON file already exists, its contents are checked against
|
|
|
|
what information this script would have outputted; if the result is that the
|
|
|
|
file would be unchanged, it is not modified to prevent unnecessary
|
|
|
|
incremental builds.
|
|
|
|
"""
|
|
|
|
|
2017-11-20 20:03:55 +08:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
|
2020-05-30 04:24:51 +08:00
|
|
|
regex_flags = re.MULTILINE | re.VERBOSE
|
|
|
|
|
2020-03-01 06:51:42 +08:00
|
|
|
syscall_regex = re.compile(r'''
|
2017-11-20 20:03:55 +08:00
|
|
|
__syscall\s+ # __syscall attribute, must be first
|
|
|
|
([^(]+) # type and name of system call (split later)
|
|
|
|
[(] # Function opening parenthesis
|
|
|
|
([^)]*) # Arg list (split later)
|
|
|
|
[)] # Closing parenthesis
|
2020-05-30 04:24:51 +08:00
|
|
|
''', regex_flags)
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2020-05-30 04:30:19 +08:00
|
|
|
struct_tags = ["__subsystem", "__net_socket"]
|
2020-05-30 04:24:51 +08:00
|
|
|
|
|
|
|
tagged_struct_decl_template = r'''
|
|
|
|
%s\s+ # tag, must be first
|
2020-03-01 06:51:42 +08:00
|
|
|
struct\s+ # struct keyword is next
|
|
|
|
([^{]+) # name of subsystem
|
|
|
|
[{] # Open curly bracket
|
2020-05-30 04:24:51 +08:00
|
|
|
'''
|
|
|
|
|
|
|
|
def tagged_struct_update(target_list, tag, contents):
|
|
|
|
regex = re.compile(tagged_struct_decl_template % tag, regex_flags)
|
|
|
|
items = [mo.groups()[0].strip() for mo in regex.finditer(contents)]
|
|
|
|
target_list.extend(items)
|
|
|
|
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2018-07-02 17:29:19 +08:00
|
|
|
def analyze_headers(multiple_directories):
|
2020-03-01 06:51:42 +08:00
|
|
|
syscall_ret = []
|
2020-05-30 04:24:51 +08:00
|
|
|
tagged_ret = {}
|
|
|
|
|
|
|
|
for tag in struct_tags:
|
|
|
|
tagged_ret[tag] = []
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2018-07-02 17:29:19 +08:00
|
|
|
for base_path in multiple_directories:
|
2019-02-16 10:56:57 +08:00
|
|
|
for root, dirs, files in os.walk(base_path, topdown=True):
|
|
|
|
dirs.sort()
|
|
|
|
files.sort()
|
2018-07-02 17:29:19 +08:00
|
|
|
for fn in files:
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2022-03-17 05:07:43 +08:00
|
|
|
# toolchain/common.h has the definitions of these tags which we
|
2018-07-02 17:29:19 +08:00
|
|
|
# don't want to trip over
|
|
|
|
path = os.path.join(root, fn)
|
2020-05-30 04:24:51 +08:00
|
|
|
if (not (path.endswith(".h") or path.endswith(".c")) or
|
|
|
|
path.endswith(os.path.join(os.sep, 'toolchain',
|
|
|
|
'common.h'))):
|
2018-07-02 17:29:19 +08:00
|
|
|
continue
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2018-07-02 17:29:19 +08:00
|
|
|
with open(path, "r", encoding="utf-8") as fp:
|
2020-03-01 06:51:42 +08:00
|
|
|
contents = fp.read()
|
|
|
|
|
|
|
|
try:
|
|
|
|
syscall_result = [(mo.groups(), fn)
|
|
|
|
for mo in syscall_regex.finditer(contents)]
|
2020-05-30 04:24:51 +08:00
|
|
|
for tag in struct_tags:
|
|
|
|
tagged_struct_update(tagged_ret[tag], tag, contents)
|
2020-03-01 06:51:42 +08:00
|
|
|
except Exception:
|
|
|
|
sys.stderr.write("While parsing %s\n" % fn)
|
|
|
|
raise
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2020-03-01 06:51:42 +08:00
|
|
|
syscall_ret.extend(syscall_result)
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2020-05-30 04:24:51 +08:00
|
|
|
return syscall_ret, tagged_ret
|
2020-03-01 06:51:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
def update_file_if_changed(path, new):
|
|
|
|
if os.path.exists(path):
|
|
|
|
with open(path, 'r') as fp:
|
|
|
|
old = fp.read()
|
|
|
|
|
|
|
|
if new != old:
|
|
|
|
with open(path, 'w') as fp:
|
|
|
|
fp.write(new)
|
|
|
|
else:
|
|
|
|
with open(path, 'w') as fp:
|
|
|
|
fp.write(new)
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
|
2017-11-20 20:03:55 +08:00
|
|
|
def parse_args():
|
|
|
|
global args
|
2017-12-12 21:19:25 +08:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description=__doc__,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2018-07-02 17:29:19 +08:00
|
|
|
parser.add_argument("-i", "--include", required=True, action='append',
|
2019-02-16 10:56:57 +08:00
|
|
|
help='''include directories recursively scanned
|
|
|
|
for .h files. Can be specified multiple times:
|
|
|
|
-i topdir1 -i topdir2 ...''')
|
2017-12-12 21:19:25 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"-j", "--json-file", required=True,
|
|
|
|
help="Write system call prototype information as json to file")
|
2020-03-01 06:51:42 +08:00
|
|
|
parser.add_argument(
|
2020-05-30 04:24:51 +08:00
|
|
|
"-t", "--tag-struct-file", required=True,
|
|
|
|
help="Write tagged struct name information as json to file")
|
|
|
|
|
2017-11-20 20:03:55 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
|
2017-11-20 20:03:55 +08:00
|
|
|
def main():
|
|
|
|
parse_args()
|
|
|
|
|
2020-05-30 04:24:51 +08:00
|
|
|
syscalls, tagged = analyze_headers(args.include)
|
2020-03-01 06:51:42 +08:00
|
|
|
|
|
|
|
# Only write json files if they don't exist or have changes since
|
2020-05-30 04:24:51 +08:00
|
|
|
# they will force an incremental rebuild.
|
2017-11-20 20:03:55 +08:00
|
|
|
|
|
|
|
syscalls_in_json = json.dumps(
|
|
|
|
syscalls,
|
|
|
|
indent=4,
|
|
|
|
sort_keys=True
|
|
|
|
)
|
2020-03-01 06:51:42 +08:00
|
|
|
update_file_if_changed(args.json_file, syscalls_in_json)
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2020-05-30 04:24:51 +08:00
|
|
|
tagged_struct_in_json = json.dumps(
|
|
|
|
tagged,
|
2020-03-01 06:51:42 +08:00
|
|
|
indent=4,
|
|
|
|
sort_keys=True
|
|
|
|
)
|
2020-05-30 04:24:51 +08:00
|
|
|
update_file_if_changed(args.tag_struct_file, tagged_struct_in_json)
|
2017-11-20 20:03:55 +08:00
|
|
|
|
2017-12-12 21:19:25 +08:00
|
|
|
|
2017-11-20 20:03:55 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|