Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/update_llvm_libc.py
4150 views
1
#!/usr/bin/env python3
2
# Copyright 2019 The Emscripten Authors. All rights reserved.
3
# Emscripten is available under two separate licenses, the MIT license and the
4
# University of Illinois/NCSA Open Source License. Both these licenses can be
5
# found in the LICENSE file.
6
7
import os
8
import sys
9
import shutil
10
import glob
11
12
script_dir = os.path.abspath(os.path.dirname(__file__))
13
emscripten_root = os.path.dirname(os.path.dirname(script_dir))
14
default_llvm_dir = os.path.join(os.path.dirname(emscripten_root), 'llvm-project')
15
16
preserve_files = ('readme.txt', '__assertion_handler', '__config_site')
17
# ryu_long_double_constants.h from libc is unused (and very large)
18
excludes = ('CMakeLists.txt', 'ryu_long_double_constants.h')
19
20
libc_copy_dirs = [
21
'hdr',
22
'include/llvm-libc-macros',
23
'include/llvm-libc-types',
24
'shared',
25
'config',
26
'src/__support',
27
'src/assert',
28
'src/complex',
29
'src/errno',
30
'src/inttypes',
31
'src/math',
32
'src/setjmp',
33
'src/stdio/printf_core',
34
'src/stdlib',
35
'src/string',
36
'src/strings',
37
'src/wchar',
38
]
39
40
libc_exclusion_patterns = [
41
# float16 is unsupported in Emscripten.
42
'src/complex/**/*f16*',
43
'src/math/generic/*f16*',
44
45
'src/setjmp/**/*', # setjmp in Emscripten is implemented by the clang backend.
46
'src/strings/str*casecmp_l*', # locale_t is unsupported in Overlay Mode.
47
]
48
49
def clean_dir(dirname):
50
if not os.path.exists(dirname):
51
return
52
for f in os.listdir(dirname):
53
if f in preserve_files:
54
continue
55
full = os.path.join(dirname, f)
56
if os.path.isdir(full):
57
shutil.rmtree(full)
58
else:
59
os.remove(full)
60
61
62
def copy_tree(upstream_dir, local_dir):
63
if not os.path.exists(local_dir):
64
os.makedirs(local_dir)
65
for f in os.listdir(upstream_dir):
66
full = os.path.join(upstream_dir, f)
67
if os.path.isdir(full):
68
shutil.copytree(full, os.path.join(local_dir, f))
69
elif f not in excludes:
70
shutil.copy2(full, os.path.join(local_dir, f))
71
for root, dirs, files in os.walk(local_dir):
72
for f in files:
73
if f in excludes:
74
full = os.path.join(root, f)
75
os.remove(full)
76
77
78
def main():
79
if len(sys.argv) > 1:
80
llvm_dir = os.path.abspath(sys.argv[1])
81
else:
82
llvm_dir = default_llvm_dir
83
libc_upstream_dir = os.path.join(llvm_dir, 'libc')
84
assert os.path.exists(libc_upstream_dir)
85
libc_local_dir = os.path.join(script_dir, 'llvm-libc')
86
87
for dirname in libc_copy_dirs:
88
local_dir = os.path.join(libc_local_dir, dirname)
89
clean_dir(local_dir)
90
91
for dirname in libc_copy_dirs:
92
upstream_dir = os.path.join(libc_upstream_dir, dirname)
93
local_dir = os.path.join(libc_local_dir, dirname)
94
copy_tree(upstream_dir, local_dir)
95
96
# Certain llvm-libc files that are incompatible in Emscripten
97
for excludsion_pattern in libc_exclusion_patterns:
98
files_to_exclude = glob.glob(os.path.join(libc_local_dir, excludsion_pattern))
99
for file in files_to_exclude:
100
os.remove(file)
101
102
if __name__ == '__main__':
103
main()
104