Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/emcmake.py
4091 views
1
#!/usr/bin/env python3
2
# Copyright 2016 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 shlex
9
import shutil
10
import sys
11
from tools import shared
12
from tools import config
13
from tools import utils
14
from subprocess import CalledProcessError
15
16
17
#
18
# Main run() function
19
#
20
def run():
21
if len(sys.argv) < 2 or sys.argv[1] in ('--version', '--help'):
22
print('''\
23
emcmake is a helper for cmake, setting various environment
24
variables so that emcc etc. are used. Typical usage:
25
26
emcmake cmake [FLAGS]
27
''', file=sys.stderr)
28
return 1
29
30
args = sys.argv[1:]
31
32
def has_substr(args, substr):
33
return any(substr in s for s in args)
34
35
# Append the Emscripten toolchain file if the user didn't specify one.
36
if not has_substr(args, '-DCMAKE_TOOLCHAIN_FILE') and 'CMAKE_TOOLCHAIN_FILE' not in os.environ:
37
args.append('-DCMAKE_TOOLCHAIN_FILE=' + utils.path_from_root('cmake/Modules/Platform/Emscripten.cmake'))
38
39
if not has_substr(args, '-DCMAKE_CROSSCOMPILING_EMULATOR'):
40
node_js = [config.NODE_JS[0]]
41
# In order to allow cmake to run code built with pthreads we need to pass
42
# some extra flags to node.
43
node_js += shared.node_pthread_flags(config.NODE_JS)
44
node_js = ';'.join(node_js)
45
# See https://github.com/emscripten-core/emscripten/issues/15522
46
args.append(f'-DCMAKE_CROSSCOMPILING_EMULATOR={node_js}')
47
48
# Print a better error if we have no CMake executable on the PATH
49
if not os.path.dirname(args[0]) and not shutil.which(args[0]):
50
print(f'emcmake: cmake executable not found on PATH: `{args[0]}`', file=sys.stderr)
51
return 1
52
53
# On Windows specify MinGW Makefiles or ninja if we have them and no other
54
# toolchain was specified, to keep CMake from pulling in a native Visual
55
# Studio, or Unix Makefiles.
56
if utils.WINDOWS and not any(arg.startswith('-G') for arg in args):
57
if shutil.which('mingw32-make'):
58
args += ['-G', 'MinGW Makefiles']
59
elif shutil.which('ninja'):
60
args += ['-G', 'Ninja']
61
else:
62
print('emcmake: no compatible cmake generator found; Please install ninja or mingw32-make, or specify a generator explicitly using -G', file=sys.stderr)
63
return 1
64
65
print(f'configure: {shlex.join(args)}', file=sys.stderr)
66
try:
67
shared.check_call(args)
68
return 0
69
except CalledProcessError as e:
70
return e.returncode
71
72
73
if __name__ == '__main__':
74
sys.exit(run())
75
76