Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/install.py
6162 views
1
#!/usr/bin/env python3
2
# Copyright 2020 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
"""Install the parts of emscripten needed for end users. This works like
8
a traditional `make dist` target but is written in python so it can be portable
9
and run on non-unix platforms (basically windows).
10
"""
11
12
import argparse
13
import fnmatch
14
import logging
15
import os
16
import shutil
17
import subprocess
18
import sys
19
20
WINDOWS = sys.platform.startswith('win')
21
MSYS2 = 'MSYSTEM' in os.environ
22
23
EXCLUDES = [os.path.normpath(x) for x in '''
24
test/third_party
25
tools/maint
26
tools/install.py
27
site
28
node_modules
29
Makefile
30
.git
31
.circleci
32
.github
33
.mypy_cache
34
.ruff_cache
35
cache
36
cache.lock
37
out
38
bootstrap.py
39
'''.split()]
40
41
LAUNCHER_BAT_SCRIPTS = '''
42
emcc.bat
43
em++.bat
44
bootstrap.bat
45
'''.split()
46
47
EXCLUDE_PATTERNS = '''
48
*.pyc
49
.*
50
__pycache__
51
'''.split()
52
53
logger = logging.getLogger('install')
54
55
56
def add_revision_file(target):
57
# text=True would be better than encoding here, but it's only supported in 3.7+
58
git_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'], encoding='utf-8').strip()
59
with open(os.path.join(target, 'emscripten-revision.txt'), 'w') as f:
60
f.write(git_hash + '\n')
61
62
63
def copy_emscripten(target):
64
script_dir = os.path.dirname(os.path.abspath(__file__))
65
emscripten_root = os.path.dirname(script_dir)
66
67
excludes = EXCLUDES
68
# We have a few launcher scripts that are checked into git still.
69
# Exclude the ones not designed for the current platforms.
70
if WINDOWS and not MSYS2:
71
excludes += [os.path.splitext(l)[0] for l in LAUNCHER_BAT_SCRIPTS]
72
elif not MSYS2:
73
excludes += LAUNCHER_BAT_SCRIPTS
74
75
os.chdir(emscripten_root)
76
for root, dirs, files in os.walk('.'):
77
# Handle the case where the target directory is underneath emscripten_root
78
if os.path.abspath(root) == os.path.abspath(target):
79
dirs.clear()
80
continue
81
82
remove_dirs = []
83
for d in dirs:
84
if d in EXCLUDE_PATTERNS:
85
remove_dirs.append(d)
86
continue
87
fulldir = os.path.normpath(os.path.join(root, d))
88
if fulldir in EXCLUDES:
89
remove_dirs.append(d)
90
continue
91
os.makedirs(os.path.join(target, fulldir))
92
93
for d in remove_dirs:
94
# Prevent recursion in excluded dirs
95
logger.debug('skipping dir: ' + os.path.join(root, d))
96
dirs.remove(d)
97
98
for f in files:
99
if any(fnmatch.fnmatch(f, pat) for pat in EXCLUDE_PATTERNS):
100
logger.debug('skipping file: ' + os.path.join(root, f))
101
continue
102
full = os.path.normpath(os.path.join(root, f))
103
if full in excludes:
104
logger.debug('skipping file: ' + os.path.join(root, f))
105
continue
106
logger.debug('installing file: ' + os.path.join(root, f))
107
shutil.copy2(full, os.path.join(target, root, f), follow_symlinks=False)
108
109
110
def npm_install(target):
111
subprocess.check_call([shutil.which('npm'), 'ci', '--omit=dev'], cwd=target)
112
113
114
def main():
115
parser = argparse.ArgumentParser(description=__doc__)
116
parser.add_argument('-v', '--verbose', action='store_true', help='verbose',
117
default=int(os.environ.get('EMCC_DEBUG', '0')))
118
parser.add_argument('target', help='target directory')
119
args = parser.parse_args()
120
target = os.path.abspath(args.target)
121
if os.path.exists(target):
122
print('target directory already exists: %s' % target)
123
return 1
124
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
125
os.makedirs(target)
126
copy_emscripten(target)
127
npm_install(target)
128
if os.path.isdir('.git'):
129
# Add revision flag only if the source directory is a Git repository
130
# and not a source archive
131
add_revision_file(target)
132
return 0
133
134
135
if __name__ == '__main__':
136
sys.exit(main())
137
138