Select Git revision
gen-modules.py

rahix authored
Previously, we left out the dependency of the QSTR header on `modules.h`. This was done to prevent rebuilds of the entire MicroPython sources whenever a Pycardium module is changed. This leads to issues where QSTRs got out of sync and weird errors like the following could happen: import foo_module Exception: No module `abc_def` (a different string than expected) Attempt to fix this by only updating the QSTR header when the module-header actually changes. For this, a few workarounds are needed: - Replace symlinks with actual copied files so timestamps change on updates. - Add a hack so meson picks up on the dependency of the file in genhdr/ - Rename the outer file so older meson versions don't complain about multiple targets with the same name. Co-authored-by:dx <dequis@dequis.org> Signed-off-by:
Rahix <rahix@rahix.de>
gen-modules.py 1.47 KiB
#!/usr/bin/env python3
# Usage: gen-modules.py <source path> <output.h> <input.c ...>
import sys
import os
import tempfile
import shutil
def main():
# Get 'official' makemoduledefs.py
path = sys.argv[1] + "/micropython/py"
sys.path.insert(0, path)
import makemoduledefs
modules = set()
for source in sys.argv[3:]:
modules |= makemoduledefs.find_module_registrations(source)
stdout = sys.stdout
with tempfile.TemporaryFile("w+") as temp:
sys.stdout = temp
makemoduledefs.generate_module_table_header(sorted(modules))
sys.stdout = stdout
# Read contents of existing file and compare
try:
with open(sys.argv[2], "r") as f:
old_content = f.read()
except FileNotFoundError:
old_content = ""
temp.seek(0)
new_content = temp.read()
if new_content == old_content:
# If both file contain the same content, exit early
sys.exit(0)
with open(sys.argv[2], "w") as f:
f.write(new_content)
try:
os.mkdir(os.path.dirname(sys.argv[2]) + "/genhdr")
except FileExistsError:
pass
linkname = (
os.path.dirname(sys.argv[2]) + "/genhdr/" + os.path.basename(sys.argv[2])
)
if os.path.exists(linkname):
os.unlink(linkname)
shutil.copy(sys.argv[2], linkname)
sys.stdout = stdout
if __name__ == "__main__":
main()