diff --git a/system/lib/update_common.py b/system/lib/update_common.py index 54f861f03fa55..a71991ad1a1e4 100644 --- a/system/lib/update_common.py +++ b/system/lib/update_common.py @@ -1,22 +1,26 @@ import os import sys import shutil +import re +import argparse script_dir = os.path.abspath(os.path.dirname(__file__)) emscripten_root = os.path.dirname(os.path.dirname(script_dir)) default_llvm_dir = os.path.join(os.path.dirname(emscripten_root), 'llvm-project') -def get_llvm_dir(): - if len(sys.argv) > 1: - llvm_dir = os.path.abspath(sys.argv[1]) - else: - llvm_dir = default_llvm_dir - if not os.path.isdir(llvm_dir): - print(f'LLVM directory not found: {llvm_dir}', file=sys.stderr) - print(f'Usage: {sys.argv[0]} [llvm_dir]', file=sys.stderr) +def parse_args(default_dir, dir_name='llvm_dir'): + parser = argparse.ArgumentParser(description=f'Update library from {dir_name}') + parser.add_argument('src_dir', nargs='?', default=default_dir, help=f'Path to {dir_name}') + args = parser.parse_args() + + src_dir = os.path.abspath(args.src_dir) + if not os.path.isdir(src_dir): + print(f'{dir_name} directory not found: {src_dir}', file=sys.stderr) + parser.print_help(sys.stderr) sys.exit(1) - return llvm_dir + + return src_dir def clean_dir(dirname, preserve_files=()): @@ -47,3 +51,38 @@ def copy_tree(upstream_dir, local_dir, excludes=()): if f in excludes: full = os.path.join(root, f) os.remove(full) + + +def get_llvm_version(upstream_dir): + cmake_file = os.path.join(upstream_dir, 'cmake', 'Modules', 'LLVMVersion.cmake') + if not os.path.exists(cmake_file): + return None, None + with open(cmake_file, 'r') as f: + content = f.read() + major = re.search(r'set\(LLVM_VERSION_MAJOR\s+(\d+)\)', content) + minor = re.search(r'set\(LLVM_VERSION_MINOR\s+(\d+)\)', content) + patch = re.search(r'set\(LLVM_VERSION_PATCH\s+(\d+)\)', content) + if major and minor and patch: + return major.group(1), f'{major.group(1)}.{minor.group(1)}.{patch.group(1)}' + return None, None + + +def update_readme(local_dir, llvm_dir): + readme_path = os.path.join(local_dir, 'readme.txt') + if not os.path.exists(readme_path): + readme_path = os.path.join(local_dir, 'README.txt') + if not os.path.exists(readme_path): + return + + with open(readme_path, 'r') as f: + content = f.read() + + major, full_version = get_llvm_version(llvm_dir) + if major and full_version: + # Find full version numbers in the form of x.y.z and update them + content = re.sub(r'\b\d+\.\d+\.\d+\b', full_version, content) + # Find 'emscripten-libs-NN' strings and update them with the major version + content = re.sub(r'emscripten-libs-\d+', f'emscripten-libs-{major}', content) + + with open(readme_path, 'w') as f: + f.write(content) diff --git a/system/lib/update_compiler_rt.py b/system/lib/update_compiler_rt.py index b0057e86547fc..5036eb4b5e3d2 100755 --- a/system/lib/update_compiler_rt.py +++ b/system/lib/update_compiler_rt.py @@ -30,7 +30,7 @@ def main(): - llvm_dir = get_llvm_dir() + llvm_dir = parse_args(default_llvm_dir, 'llvm_dir') upstream_dir = os.path.join(llvm_dir, 'compiler-rt') assert os.path.exists(upstream_dir) upstream_src = os.path.join(upstream_dir, 'lib', 'builtins') @@ -53,6 +53,7 @@ def main(): shutil.copy2(os.path.join(upstream_dir, 'CREDITS.TXT'), local_src) shutil.copy2(os.path.join(upstream_dir, 'LICENSE.TXT'), local_src) + update_readme(local_src, llvm_dir) if __name__ == '__main__': diff --git a/system/lib/update_libcxx.py b/system/lib/update_libcxx.py index 35d69c92bf266..a3a99bc277a4d 100755 --- a/system/lib/update_libcxx.py +++ b/system/lib/update_libcxx.py @@ -63,6 +63,7 @@ def generate_modules(cmake_version: str): shutil.copytree(dist, dst, dirs_exist_ok=True) def main(): + llvm_dir = parse_args(default_llvm_dir, 'llvm_dir') # Exit early if cmake is not found try: output= subprocess.check_output(['cmake', '--version'], text=True) @@ -112,6 +113,7 @@ def main(): copy_tree(upstream_dir, local_dir, excludes) shutil.copy2(os.path.join(libc_upstream_dir, 'LICENSE.TXT'), libc_local_dir) + update_readme(local_root, llvm_dir) if __name__ == '__main__': main() diff --git a/system/lib/update_libcxxabi.py b/system/lib/update_libcxxabi.py index dd8cfc1acd9ce..11719e5d0ea88 100755 --- a/system/lib/update_libcxxabi.py +++ b/system/lib/update_libcxxabi.py @@ -19,7 +19,7 @@ def main(): - llvm_dir = get_llvm_dir() + llvm_dir = parse_args(default_llvm_dir, 'llvm_dir') upstream_root = os.path.join(llvm_dir, 'libcxxabi') upstream_src = os.path.join(upstream_root, 'src') upstream_inc = os.path.join(upstream_root, 'include') @@ -34,6 +34,7 @@ def main(): copy_tree(upstream_inc, local_inc, excludes) shutil.copy2(os.path.join(upstream_root, 'CREDITS.TXT'), local_root) shutil.copy2(os.path.join(upstream_root, 'LICENSE.TXT'), local_root) + update_readme(local_root, llvm_dir) if __name__ == '__main__': diff --git a/system/lib/update_libunwind.py b/system/lib/update_libunwind.py index 7bd85738542aa..bd32bbe951379 100755 --- a/system/lib/update_libunwind.py +++ b/system/lib/update_libunwind.py @@ -19,7 +19,7 @@ def main(): - llvm_dir = get_llvm_dir() + llvm_dir = parse_args(default_llvm_dir, 'llvm_dir') upstream_root = os.path.join(llvm_dir, 'libunwind') upstream_src = os.path.join(upstream_root, 'src') upstream_inc = os.path.join(upstream_root, 'include') @@ -33,6 +33,7 @@ def main(): copy_tree(upstream_src, local_src, excludes) copy_tree(upstream_inc, local_inc, excludes) shutil.copy2(os.path.join(upstream_root, 'LICENSE.TXT'), local_root) + update_readme(local_root, llvm_dir) if __name__ == '__main__': diff --git a/system/lib/update_llvm_libc.py b/system/lib/update_llvm_libc.py index 481eebc4d146c..5227d2691bc8d 100755 --- a/system/lib/update_llvm_libc.py +++ b/system/lib/update_llvm_libc.py @@ -46,7 +46,7 @@ def main(): - llvm_dir = get_llvm_dir() + llvm_dir = parse_args(default_llvm_dir, 'llvm_dir') libc_upstream_dir = os.path.join(llvm_dir, 'libc') assert os.path.exists(libc_upstream_dir) libc_local_dir = os.path.join(script_dir, 'llvm-libc') @@ -66,5 +66,7 @@ def main(): for file in files_to_exclude: os.remove(file) + update_readme(libc_local_dir, llvm_dir) + if __name__ == '__main__': main() diff --git a/system/lib/update_musl.py b/system/lib/update_musl.py index 927fff74c0e97..a9f91cf49a944 100755 --- a/system/lib/update_musl.py +++ b/system/lib/update_musl.py @@ -69,16 +69,6 @@ ) -if len(sys.argv) > 1: - musl_dir = os.path.abspath(sys.argv[1]) -else: - musl_dir = default_musl_dir -if not os.path.isdir(musl_dir): - print(f'musl directory not found: {musl_dir}', file=sys.stderr) - print(f'Usage: {sys.argv[0]} [musl_dir]', file=sys.stderr) - sys.exit(1) - - def make_ignore(root): root = Path(root).resolve() @@ -102,6 +92,7 @@ def ignore(directory, contents): def main(): + musl_dir = parse_args(default_musl_dir, 'musl_dir') assert os.path.exists(musl_dir) # Remove old version