Skip to content

Commit

Permalink
add emcc option --generate-config (#6810)
Browse files Browse the repository at this point in the history
fixes #6299
  • Loading branch information
VENIAMIN PETRENKO authored and kripken committed Jul 16, 2018
1 parent 1c100d9 commit 676a351
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 39 deletions.
8 changes: 8 additions & 0 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,14 @@ def parse_args(newargs):
exit_with_error('Invalid value "' + newargs[i + 1] + '" to --output_eol!')
newargs[i] = ''
newargs[i + 1] = ''
elif newargs[i] == '--generate-config':
optarg = newargs[i + 1]
path = os.path.expanduser(optarg)
if os.path.exists(path):
exit_with_error('File ' + optarg + ' passed to --generate-config already exists!')
else:
shared.generate_config(optarg)
should_exit = True

if should_exit:
sys.exit(0)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ def test_emcc_python_version(self):
assert expected_call not in output
assert output == '', output

def test_emcc_generate_config(self):
for compiler in [EMCC, EMXX]:
config_path = './emscripten_config'
run_process([PYTHON, compiler, '--generate-config', config_path])
assert os.path.exists(config_path), 'A config file should have been created at %s' % config_path
config_contents = open(config_path).read()
self.assertContained('EMSCRIPTEN_ROOT', config_contents)
self.assertContained('LLVM_ROOT', config_contents)
os.remove(config_path)

def test_emcc_1(self):
for compiler in [EMCC, EMXX]:
shortcompiler = os.path.basename(compiler)
Expand Down
84 changes: 45 additions & 39 deletions tools/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,50 @@ def check_call(cmd, *args, **kw):
raise FatalError("'%s' failed" % " ".join(cmd))


def generate_config(path, first_time=False):
# Note: repr is used to ensure the paths are escaped correctly on Windows.
# The full string is replaced so that the template stays valid Python.
config_file = open(path_from_root('tools', 'settings_template_readonly.py')).read().split('\n')
config_file = config_file[1:] # remove "this file will be copied..."
config_file = '\n'.join(config_file)
# autodetect some default paths
config_file = config_file.replace('\'{{{ EMSCRIPTEN_ROOT }}}\'', repr(__rootpath__))
llvm_root = os.path.dirname(find_executable('llvm-dis') or '/usr/bin/llvm-dis')
config_file = config_file.replace('\'{{{ LLVM_ROOT }}}\'', repr(llvm_root))

node = find_executable('nodejs') or find_executable('node') or 'node'
config_file = config_file.replace('\'{{{ NODE }}}\'', repr(node))
if WINDOWS:
tempdir = os.environ.get('TEMP') or os.environ.get('TMP') or 'c:\\temp'
else:
tempdir = '/tmp'
config_file = config_file.replace('\'{{{ TEMP }}}\'', repr(tempdir))

abspath = os.path.abspath(os.path.expanduser(path))
# write
open(abspath, 'w').write(config_file)
if first_time:
print('''
==============================================================================
Welcome to Emscripten!
This is the first time any of the Emscripten tools has been run.
A settings file has been copied to %s, at absolute path: %s
It contains our best guesses for the important paths, which are:
LLVM_ROOT = %s
NODE_JS = %s
EMSCRIPTEN_ROOT = %s
Please edit the file if any of those are incorrect.
This command will now exit. When you are done editing those paths, re-run it.
==============================================================================
''' % (path, abspath, llvm_root, node, __rootpath__), file=sys.stderr)


# Emscripten configuration is done through the --em-config command line option or
# the EM_CONFIG environment variable. If the specified string value contains newline
# or semicolon-separated definitions, then these definitions will be used to configure
Expand Down Expand Up @@ -220,45 +264,7 @@ def check_call(cmd, *args, **kw):
CONFIG_FILE = os.path.expanduser(EM_CONFIG)
logging.debug('EM_CONFIG is located in ' + CONFIG_FILE)
if not os.path.exists(CONFIG_FILE):
# Note: repr is used to ensure the paths are escaped correctly on Windows.
# The full string is replaced so that the template stays valid Python.
config_file = open(path_from_root('tools', 'settings_template_readonly.py')).read().split('\n')
config_file = config_file[1:] # remove "this file will be copied..."
config_file = '\n'.join(config_file)
# autodetect some default paths
config_file = config_file.replace('\'{{{ EMSCRIPTEN_ROOT }}}\'', repr(__rootpath__))
llvm_root = os.path.dirname(find_executable('llvm-dis') or '/usr/bin/llvm-dis')
config_file = config_file.replace('\'{{{ LLVM_ROOT }}}\'', repr(llvm_root))

node = find_executable('nodejs') or find_executable('node') or 'node'
config_file = config_file.replace('\'{{{ NODE }}}\'', repr(node))
if WINDOWS:
tempdir = os.environ.get('TEMP') or os.environ.get('TMP') or 'c:\\temp'
else:
tempdir = '/tmp'
config_file = config_file.replace('\'{{{ TEMP }}}\'', repr(tempdir))

# write
open(CONFIG_FILE, 'w').write(config_file)
print('''
==============================================================================
Welcome to Emscripten!
This is the first time any of the Emscripten tools has been run.
A settings file has been copied to %s, at absolute path: %s
It contains our best guesses for the important paths, which are:
LLVM_ROOT = %s
NODE_JS = %s
EMSCRIPTEN_ROOT = %s
Please edit the file if any of those are incorrect.
This command will now exit. When you are done editing those paths, re-run it.
==============================================================================
''' % (EM_CONFIG, CONFIG_FILE, llvm_root, node, __rootpath__), file=sys.stderr)
generate_config(EM_CONFIG, first_time=True)
sys.exit(0)

# The following globals can be overridden by the config file.
Expand Down

0 comments on commit 676a351

Please sign in to comment.