blob: eab01d6732970ccedfa4fda1d2c7dc36d36d5b5d [file]
#!/usr/bin/env python3
# Copyright 2019 The Emscripten Authors. All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License. Both these licenses can be
# found in the LICENSE file.
# Run this after updating llvm-libc to the same version. libcxx is dependent on
# the following diretories from llvm-libc:
# llvm-libc/hdr
# llvm-libc/include/llvm-libc-macros
# llvm-libc/include/llvm-libc-types
# llvm-libc/shared
# llvm-libc/config
import os
import re
import shutil
import subprocess
import sys
from update_common import (
clean_dir,
copy_tree,
default_llvm_dir,
emscripten_root,
parse_args,
read_file,
script_dir,
update_readme,
write_file,
)
local_root = os.path.join(script_dir, 'libcxx')
local_src = os.path.join(local_root, 'src')
local_inc = os.path.join(local_root, 'include')
local_modules = os.path.join(local_root, 'modules')
preserve_files = ('readme.txt', '__assertion_handler', '__config_site')
# ryu_long_double_constants.h from libc is unused (and very large)
excludes = {'ryu_long_double_constants.h'}
def generate_modules(cmake_version: str):
dst = os.path.join(local_modules, 'prebuilt')
lib = 'lib/emscripten'
share = 'share/libc++/v1'
build_dir = f'{emscripten_root}/out/libcxx/modules'
binary_dir = f'{build_dir}/out'
dist = f'{binary_dir}/dist/'
clean_dir(build_dir, preserve_files)
os.makedirs(build_dir, exist_ok=True)
write_file(f'{build_dir}/CMakeLists.txt', f'''\
cmake_minimum_required(VERSION {cmake_version})
project(libcxx-modules)
add_subdirectory("{local_modules}" libcxx)
''')
vars = [
('LIBCXX_INSTALL_LIBRARY_DIR', lib),
('LIBCXX_INSTALL_MODULES_DIR', share),
('LIBCXX_LIBRARY_DIR', dist + lib),
('LIBCXX_GENERATED_MODULE_DIR', dist + share),
]
cmd = ['cmake', '-B', binary_dir, '-S', build_dir]
cmd += [f'-D{key}={val}' for key, val in vars]
subprocess.run(cmd, stdout=subprocess.DEVNULL)
subprocess.run(['cmake', '--build', binary_dir], stdout=subprocess.DEVNULL)
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)
except OSError:
print('CMake not found', file=sys.stderr)
sys.exit(1)
cmake_version = re.search(r'^cmake version (\d+(?:\.\d+)*)', output).group(1)
libcxx_dir = os.path.join(llvm_dir, 'libcxx')
upstream_src = os.path.join(libcxx_dir, 'src')
upstream_inc = os.path.join(libcxx_dir, 'include')
upstream_modules = os.path.join(libcxx_dir, 'modules')
assert os.path.exists(upstream_inc)
assert os.path.exists(upstream_src)
assert os.path.exists(upstream_modules)
# Remove old version
clean_dir(local_src, preserve_files)
clean_dir(local_inc, preserve_files)
clean_dir(local_modules, preserve_files)
copy_tree(upstream_src, local_src, excludes)
copy_tree(upstream_inc, local_inc, excludes)
copy_tree(upstream_modules, local_modules, excludes)
generate_modules(cmake_version)
shutil.copy2(os.path.join(libcxx_dir, 'CREDITS.TXT'), local_root)
shutil.copy2(os.path.join(libcxx_dir, 'LICENSE.TXT'), local_root)
# We don't use frozen c++03 headers for now
shutil.rmtree(os.path.join(local_inc, '__cxx03'))
# Copy module.modulemap.in to module.modulemap and replace __config_site
# string
modulemap = read_file(os.path.join(local_inc, 'module.modulemap.in'))
modulemap = modulemap.replace('@LIBCXX_CONFIG_SITE_MODULE_ENTRY@',
'textual header "__config_site"')
write_file(os.path.join(local_inc, 'module.modulemap'), modulemap)
# Copy default_assertion_handler.in to __assertion_handler
shutil.copy2(
os.path.join(libcxx_dir, 'vendor', 'llvm', 'default_assertion_handler.in'),
os.path.join(local_inc, '__assertion_handler'),
)
update_readme(local_root, llvm_dir)
if __name__ == '__main__':
main()