Skip to content

Commit

Permalink
--cflags option (emscripten-core#4776)
Browse files Browse the repository at this point in the history
Allows getting the flags emcc passes to clang.
  • Loading branch information
kripken authored Dec 15, 2016
1 parent 579bbd7 commit e101571
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
20 changes: 18 additions & 2 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def run():
if len(sys.argv) <= 1 or ('--help' not in sys.argv and len(sys.argv) >= 2 and sys.argv[1] != '--version'):
shared.check_sanity(force=DEBUG)

misc_temp_files = shared.configuration.get_temp_files()

# Handle some global flags

if len(sys.argv) == 1:
Expand Down Expand Up @@ -181,6 +183,22 @@ def run():
print shared.get_llvm_target()
exit(0)

elif '--cflags' in sys.argv:
# fake running the command, to see the full args we pass to clang
debug_env = os.environ.copy()
debug_env['EMCC_DEBUG'] = '1'
args = filter(lambda x: x != '--cflags', sys.argv)
with misc_temp_files.get_file(suffix='.o') as temp_target:
input_file = 'hello_world.c'
out, err = subprocess.Popen([shared.PYTHON] + args + [shared.path_from_root('tests', input_file), '-c', '-o', temp_target], stderr=subprocess.PIPE, env=debug_env).communicate()
lines = filter(lambda x: shared.CLANG_CC in x and input_file in x, err.split(os.linesep))
line = lines[0]
assert 'running:' in line
parts = line.split(' ')[2:]
parts = filter(lambda x: x != '-c' and x != '-o' and input_file not in x and temp_target not in x and '-emit-llvm' not in x, parts)
print ' '.join(parts)
exit(0)

def is_minus_s_for_emcc(newargs, i):
assert newargs[i] == '-s'
if i+1 < len(newargs) and '=' in newargs[i+1] and not newargs[i+1].startswith('-'): # -s OPT=VALUE is for us, -s by itself is a linker option
Expand Down Expand Up @@ -397,8 +415,6 @@ def log_time(name):

use_cxx = True

misc_temp_files = shared.configuration.get_temp_files()

try:
with ToolchainProfiler.profile_block('parse arguments and setup'):
## Parse args
Expand Down
3 changes: 3 additions & 0 deletions site/source/docs/tools_reference/emcc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ Options that are modified or new in *emcc* are listed below:
``--output-eol windows|linux``
Specifies the line ending to generate for the text files that are outputted. If "--output-eol windows" is passed, the final output files will have Windows \r\n line endings in them. With "--output-eol linux", the final generated files will be written with Unix \n line endings.

``--cflags``
Prints out the flags ``emcc`` would pass to ``clang`` to compile source code to object/bitcode form. You can use this to invoke clang yourself, and then run ``emcc`` on those outputs just for the final linking+conversion to JS.

.. _emcc-environment-variables:

Environment variables
Expand Down
11 changes: 11 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,17 @@ def test_emcc_cache_flag(self):
os.chdir(path_from_root('tests')) # Move away from the directory we are about to remove.
shutil.rmtree(tempdirname)

def test_emcc_cflags(self):
# see we print them out
output = Popen([PYTHON, EMCC, '--cflags'], stdout=PIPE, stderr=PIPE).communicate()
flags = output[0].strip()
self.assertContained(' '.join(COMPILER_OPTS), flags)
# check they work
cmd = [CLANG, path_from_root('tests', 'hello_world.cpp')] + flags.split(' ') + ['-c', '-emit-llvm', '-o', 'a.bc']
subprocess.check_call(cmd)
subprocess.check_call([PYTHON, EMCC, 'a.bc'])
self.assertContained('hello, world!', run_js(self.in_dir('a.out.js')))

def test_emar_em_config_flag(self):
# We expand this in case the EM_CONFIG is ~/.emscripten (default)
config = os.path.expanduser(EM_CONFIG)
Expand Down

0 comments on commit e101571

Please sign in to comment.