summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2025-02-28 16:33:04 -0700
committerLuke T. Shumaker <lukeshu@lukeshu.com>2025-02-28 19:39:29 -0700
commit58991986652e0a08f919cb30f5302bb1b977ef32 (patch)
tree5711e2637bd270087282d5a7cd610b17610f04a8
parent2de5915f7a06a75611b63507fe7ee123d127a870 (diff)
stack.c.gen: Fix filename hacks
-rwxr-xr-xbuild-aux/stack.c.gen106
-rw-r--r--lib9p/tests/test_server/config/config.h3
2 files changed, 69 insertions, 40 deletions
diff --git a/build-aux/stack.c.gen b/build-aux/stack.c.gen
index 42a8a62..968c760 100755
--- a/build-aux/stack.c.gen
+++ b/build-aux/stack.c.gen
@@ -328,6 +328,16 @@ def read_source(location: str) -> str:
return fh.readlines()[row][col:].rstrip()
+def get_zero_or_one(
+ pred: typing.Callable[[str], bool], fnames: typing.Collection[str]
+) -> str | None:
+ count = sum(1 for fname in fnames if pred(fname))
+ assert count < 2
+ if count:
+ return next(fname for fname in fnames if pred(fname))
+ return None
+
+
def main(
*,
arg_pico_platform: str,
@@ -347,6 +357,35 @@ def main(
# The sbc-harness codebase #######################################
+ def _is_config_h(fname: str) -> bool:
+ if not fname.startswith(arg_base_dir + "/"):
+ return False
+ suffix = fname[len(arg_base_dir) + 1 :]
+ if suffix.startswith("3rd-party/"):
+ return False
+ return suffix.endswith("/config.h")
+
+ config_h_fname = get_zero_or_one(_is_config_h, arg_c_fnames)
+
+ lib9p_srv_c_fname = get_zero_or_one(
+ lambda fname: fname.endswith("lib9p/srv.c"), arg_c_fnames
+ )
+
+ lib9p_generated_c_fname = get_zero_or_one(
+ lambda fname: fname.endswith("lib9p/9p.generated.c"), arg_c_fnames
+ )
+
+ def config_h_get(varname: str) -> int | None:
+ if config_h_fname:
+ with open(config_h_fname, "r") as fh:
+ for line in fh:
+ line = line.rstrip()
+ if line.startswith("#define"):
+ parts = line.split()
+ if parts[1] == varname:
+ return int(parts[2])
+ return None
+
objcalls: dict[str, set[str]] = {}
re_vtable_start = re.compile(r"_vtable\s*=\s*\{")
re_vtable_entry = re.compile(r"^\s+\.(?P<meth>\S+)\s*=\s*(?P<impl>\S+),.*")
@@ -370,25 +409,21 @@ def main(
in_vtable = True
tmessage_handlers: set[str] | None = None
- if any(fname.endswith("lib9p/srv.c") for fname in arg_c_fnames):
- srv_c = next(fname for fname in arg_c_fnames if fname.endswith("lib9p/srv.c"))
+ if lib9p_srv_c_fname:
re_tmessage_handler = re.compile(
r"^\s*\[LIB9P_TYP_T[^]]+\]\s*=\s*\(tmessage_handler\)\s*(?P<handler>\S+),\s*$"
)
tmessage_handlers = set()
- with open(srv_c, "r") as fh:
+ with open(lib9p_srv_c_fname, "r") as fh:
for line in fh:
line = line.rstrip()
if m := re_tmessage_handler.fullmatch(line):
tmessage_handlers.add(m.group("handler"))
lib9p_msgs: set[str] = set()
- if any(fname.endswith("lib9p/9p.c") for fname in arg_c_fnames):
- generated_c = next(
- fname for fname in arg_c_fnames if fname.endswith("lib9p/9p.generated.c")
- )
+ if lib9p_generated_c_fname:
re_lib9p_msg_entry = re.compile(r"^\s*_MSG_(?:[A-Z]+)\((?P<typ>\S+)\),$")
- with open(generated_c, "r") as fh:
+ with open(lib9p_generated_c_fname, "r") as fh:
for line in fh:
line = line.rstrip()
if m := re_lib9p_msg_entry.fullmatch(line):
@@ -429,12 +464,15 @@ def main(
hooks_indirect_callees += [sbc_indirect_callees]
+ _CONFIG_9P_NUM_SOCKS = config_h_get("_CONFIG_9P_NUM_SOCKS")
+ CONFIG_9P_SRV_MAX_REQS = config_h_get("CONFIG_9P_SRV_MAX_REQS")
+ CONFIG_9P_SRV_MAX_DEPTH = config_h_get("CONFIG_9P_SRV_MAX_DEPTH")
+
def sbc_is_thread(name: str) -> int:
if name.endswith("_cr") and name != "lib9p_srv_read_cr":
if "9p" in name:
- # TODO: FIXME: Sniff these numbers from config.h
- _CONFIG_9P_NUM_SOCKS = 3
- CONFIG_9P_SRV_MAX_REQS = 2
+ assert _CONFIG_9P_NUM_SOCKS
+ assert CONFIG_9P_SRV_MAX_REQS
if "read" in name:
return _CONFIG_9P_NUM_SOCKS
elif "write" in name:
@@ -458,15 +496,6 @@ def main(
"w5500_intrhandler",
]
- # 1=just root directory
- # 2=just files in root directory
- # 3=just 1 level of subdirectories
- # 4=just 2 levels of subdirectories
- # ...
- #
- # TODO: FIXME: Sniff this from config.h
- CONFIG_9P_SRV_MAX_DEPTH = 3
-
def sbc_skip_call(chain: list[str], call: str) -> bool:
if (
len(chain) > 1
@@ -475,15 +504,13 @@ def main(
and "__assert_msg_fail" in chain[:-1]
):
return True
- if (
- len(chain) >= CONFIG_9P_SRV_MAX_DEPTH
- and "/srv.c:srv_util_pathfree" in call
- and all(
- ("/srv.c:srv_util_pathfree" in c)
+ if "lib9p/srv.c:srv_util_pathfree" in call:
+ assert isinstance(CONFIG_9P_SRV_MAX_DEPTH, int)
+ if len(chain) >= CONFIG_9P_SRV_MAX_DEPTH and all(
+ ("lib9p/srv.c:srv_util_pathfree" in c)
for c in chain[-CONFIG_9P_SRV_MAX_DEPTH:]
- )
- ):
- return True
+ ):
+ return True
return False
hooks_skip_call += [sbc_skip_call]
@@ -643,15 +670,21 @@ def main(
# TinyUSB device #################################################
- if any(fname.endswith("/tinyusb/src/device/usbd.c") for fname in arg_c_fnames):
- tusb_config_fname = (
- arg_base_dir + "/cmd/sbc_harness/config/tusb_config.h"
- ) # TODO: FIXME
+ usbd_c_fname = get_zero_or_one(
+ lambda fname: fname.endswith("/tinyusb/src/device/usbd.c"), arg_c_fnames
+ )
+
+ tusb_config_h_fname = get_zero_or_one(
+ lambda fname: fname.endswith("/tusb_config.h"), arg_c_fnames
+ )
+
+ if usbd_c_fname:
+ assert tusb_config_h_fname
re_tud_class = re.compile(
r"^\s*#\s*define\s+(?P<k>CFG_TUD_(?:\S{3}|AUDIO|VIDEO|MIDI|VENDOR|USBTMC|DFU_RUNTIME|ECM_RNDIS))\s+(?P<v>\S+).*"
)
tusb_config: dict[str, bool] = {}
- with open(tusb_config_fname, "r") as fh:
+ with open(tusb_config_h_fname, "r") as fh:
in_table = False
for line in fh:
line = line.rstrip()
@@ -660,11 +693,6 @@ def main(
v = m.group("v")
tusb_config[k] = bool(int(v))
- usbd_fname = next(
- fname
- for fname in arg_c_fnames
- if fname.endswith("/tinyusb/src/device/usbd.c")
- )
tud_drivers: dict[str, set[str]] = {}
re_tud_entry = re.compile(
r"^\s+\.(?P<meth>\S+)\s*=\s*(?P<impl>[a-zA-Z0-9_]+)(?:,.*)?"
@@ -672,7 +700,7 @@ def main(
re_tud_if1 = re.compile(r"^\s*#\s*if (\S+)\s*")
re_tud_if2 = re.compile(r"^\s*#\s*if (\S+)\s*\|\|\s*(\S+)\s*")
re_tud_endif = re.compile(r"^\s*#\s*endif\s*")
- with open(usbd_fname, "r") as fh:
+ with open(usbd_c_fname, "r") as fh:
in_table = False
enabled = True
for line in fh:
diff --git a/lib9p/tests/test_server/config/config.h b/lib9p/tests/test_server/config/config.h
index 201cfd0..67960ca 100644
--- a/lib9p/tests/test_server/config/config.h
+++ b/lib9p/tests/test_server/config/config.h
@@ -7,7 +7,8 @@
#ifndef _CONFIG_H_
#define _CONFIG_H_
-#define CONFIG_SRV9P_NUM_CONNS 8
+#define _CONFIG_9P_NUM_SOCKS 8
+#define CONFIG_SRV9P_NUM_CONNS _CONFIG_9P_NUM_SOCKS
/* 9P *************************************************************************/