diff --git a/libsel4/tools/__init__.py b/libsel4/tools/__init__.py index ec5f2ab36..86aeea432 100644 --- a/libsel4/tools/__init__.py +++ b/libsel4/tools/__init__.py @@ -8,4 +8,27 @@ # This file allows the the libsel4 tools directory to be imported as # a python module. +# The following import of `loader` avoids a python module loading error +# When this is used as a module in manual/tools/libsel4_tools. +# Traceback (most recent call last): +# File "/path/to/syscall_stub_gen.py", line 46, in +# from condition import condition_to_cpp +# ModuleNotFoundError: No module named 'condition' +# +# Imports for modules differ from imports for scripts +# Such that when run as a script in tools/ it includes the current +# directory in the search path. But not when imported as a module. +# Importing `loader` will add the current directory to the search path. +# +# It exists as a module so that autopep8 will not reorder the +# code on code formatting. So that the path modification +# needed before import syscall_stub_gen occurs after the import. +# +# Upgrading autopep8 to 1.5.4 would offer an alternate solution: +# fmt: off +# path modification code +# imports. +# fmt: on +from . import loader +from . import condition from . import syscall_stub_gen diff --git a/libsel4/tools/condition.py b/libsel4/tools/condition.py new file mode 120000 index 000000000..0eb8157af --- /dev/null +++ b/libsel4/tools/condition.py @@ -0,0 +1 @@ +../../tools/condition.py \ No newline at end of file diff --git a/libsel4/tools/loader/__init__.py b/libsel4/tools/loader/__init__.py new file mode 100644 index 000000000..abf00df55 --- /dev/null +++ b/libsel4/tools/loader/__init__.py @@ -0,0 +1,14 @@ + +# Copyright 2022, seL4 Project a Series of LF Projects, LLC +# +# SPDX-License-Identifier: BSD-2-Clause or GPL-2.0-only + +import os +import sys +import pathlib + +# Add the parent module to the sys.path, do so in this __init__.py +# so that the code producing the side-effect can occur after import +# statements. Otherwise autopep8 would want to reformat this code +# so the side-effect occurs after all import statements. +sys.path.append(str(pathlib.Path(os.path.dirname(os.path.realpath(__file__))).parent)) diff --git a/libsel4/tools/syscall_stub_gen.py b/libsel4/tools/syscall_stub_gen.py index 6ef606d2b..2be1c90c9 100644 --- a/libsel4/tools/syscall_stub_gen.py +++ b/libsel4/tools/syscall_stub_gen.py @@ -43,6 +43,7 @@ import xml.dom.minidom from argparse import ArgumentParser import sys from functools import reduce +from condition import condition_to_cpp # Number of bits in a standard word WORD_SIZE_BITS_ARCH = { @@ -847,7 +848,7 @@ def parse_xml_file(input_file, valid_types): for method in interface.getElementsByTagName("method"): method_name = method.getAttribute("name") method_id = method.getAttribute("id") - method_condition = method.getAttribute("condition") + method_condition = condition_to_cpp(method.getElementsByTagName("condition")) method_manual_name = method.getAttribute("manual_name") or method_name method_manual_label = method.getAttribute("manual_label")