From e569dfb9f0af2c62e614e6a8b299920f96ded570 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Tue, 16 Jun 2026 12:51:45 +0200 Subject: [PATCH] py-mod.mk: Support PY_INIT_SUBMODULES Add support for PY_INIT_SUBMODULES to py-mod.mk. If it is defined in a Makefile including py-mod.mk, the listed submodules will be added to __init__.py and thus included in the list of things that can be imported from a module. This commit also adds support for --submodules to python-tools.sh for that to happen. Signed-off-by: Jan Lindemann --- make/py-mod.mk | 5 +++++ scripts/python-tools.sh | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/make/py-mod.mk b/make/py-mod.mk index d61856ac..cf8ae627 100644 --- a/make/py-mod.mk +++ b/make/py-mod.mk @@ -14,6 +14,11 @@ ifneq ($(PY_SYMBOL_FILTER),) PY_GENERATE_INIT_PY += --symbol-filter "$(PY_SYMBOL_FILTER)" endif +PY_INIT_SUBMODULES ?= +ifneq ($(PY_INIT_SUBMODULES),) + PY_GENERATE_INIT_PY += --submodules "$(PY_INIT_SUBMODULES)" +endif + #leftparen := ( #PY_EXPORT ?= $(shell sed '/\(class\|def\) ..*\# *export/ !d; s/\(class\|def\) *//; s/[$(leftparen):].*//' $(PY_SRC_PY)) diff --git a/scripts/python-tools.sh b/scripts/python-tools.sh index 657204da..8d2e593d 100644 --- a/scripts/python-tools.sh +++ b/scripts/python-tools.sh @@ -68,6 +68,13 @@ cmd_create_init() done done fi + + local submodule + for submodule in $submodules; do + echo "from . import $submodule as $submodule" + __add_seen $submodule + done + if [ "$import_submodules" = 1 ]; then for f in $files; do [ -f $f/__init__.py ] || continue @@ -90,7 +97,8 @@ cmd_create_init() myname=`basename $0` -eval set -- `getopt -l 'symbol-filter:,extract-filter:,module:' -o 'he:m:' "$@"` +submodules="" +eval set -- `getopt -l 'symbol-filter:,extract-filter:,module:,submodules:' -o 'he:m:' "$@"` while [ "$1" != -- ]; do case $1 in -e|--extract-filter) @@ -105,6 +113,10 @@ case $1 in module=$2 shift ;; + -m|--submodules) + submodules="$submodules $2" + shift + ;; -h|--help) usage 0 ;;