|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +def get_sdk_path(): |
| 9 | + result = subprocess.run(["xcrun", "--show-sdk-path"], capture_output=True, text=True) |
| 10 | + if result.returncode != 0: |
| 11 | + raise RuntimeError("Could not resolve SDK path") |
| 12 | + return os.path.realpath(result.stdout.strip()) |
| 13 | + |
| 14 | + |
| 15 | +def remove_ctype_module(content): |
| 16 | + # Break cyclic module dependencies |
| 17 | + # See: https://github.com/root-project/root/commit/8045591a17125b49c1007787c586868dea764479 |
| 18 | + pattern = re.compile(r"module\s+std_ctype_h\s+\[system\]\s*\{.*?\}", re.DOTALL) |
| 19 | + return pattern.sub("", content) |
| 20 | + |
| 21 | + |
| 22 | +def main(): |
| 23 | + if len(sys.argv) < 2: |
| 24 | + raise ValueError("Usage: std_modulemap_darwin_fix.py <output_path>") |
| 25 | + output_path = sys.argv[1] |
| 26 | + sdk = get_sdk_path() |
| 27 | + cpp_modulemap = os.path.join(sdk, "usr/include/c++/v1/module.modulemap") |
| 28 | + |
| 29 | + if not os.path.exists(cpp_modulemap): |
| 30 | + raise FileNotFoundError(f"Cannot find libc++ modulemap at {cpp_modulemap}") |
| 31 | + |
| 32 | + with open(cpp_modulemap, "r") as f: |
| 33 | + original_content = f.read() |
| 34 | + |
| 35 | + cleaned_content = remove_ctype_module(original_content) |
| 36 | + |
| 37 | + os.makedirs(os.path.dirname(output_path), exist_ok=True) |
| 38 | + with open(output_path, "w") as f: |
| 39 | + f.write("// Auto-generated flat modulemap for Cling\n") |
| 40 | + f.write("module std [system] {\n") |
| 41 | + f.write("export *\n\n") |
| 42 | + f.write("// Entire modulemap wrapped\n") |
| 43 | + f.write(cleaned_content) |
| 44 | + f.write("\n}\n") |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + main() |
0 commit comments