From 0d3f28031575c410a331a37dca049d472c67b4e7 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 10 Jun 2026 10:31:43 -0700 Subject: [PATCH] Improve support for clang information flags like -print-file-name If we setup clang correctly when we can just pass these flags through to clang rather than trying to re-implement them locally. --- emcc.py | 26 ++++++++++++++++++++------ test/test_other.py | 2 +- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/emcc.py b/emcc.py index 4c53876354533..4134a489bda3d 100644 --- a/emcc.py +++ b/emcc.py @@ -86,6 +86,19 @@ '--threadprofiler', '--use-preload-plugins', } +PASSTHROUGH_FLAGS = { + '-print-resource-dir', + '--print-resource-dir', + '-dumpmachine', + '-print-target-triple', + '--print-target-triple', +} + +PASSTHROUGH_PREFIXES = { + '-print-prog-name', + '--print-prog-name', +} + @unique class Mode(Enum): @@ -263,10 +276,9 @@ def main(args): print(utils.EMSCRIPTEN_VERSION) return 0 - if '-dumpmachine' in args or '-print-target-triple' in args or '--print-target-triple' in args: - print(shared.get_llvm_target()) - return 0 - + # Sadly we cannot rely on PASSTHROUGH_FLAGS for -print-search-dirs or -print-libgcc-file-name + # because there is no way to tell clang today about our custom library paths. + # TODO: Teach clang about emscripten's library layout so we can remove this code. if '-print-search-dirs' in args or '--print-search-dirs' in args: print(f'programs: ={config.LLVM_ROOT}') resource_dir = get_clang_resource_dir(args) @@ -307,8 +319,10 @@ def main(args): if 'EMCC_REPRODUCE' in os.environ: options.reproduce = os.environ['EMCC_REPRODUCE'] - if '-print-resource-dir' in args or any(a.startswith('--print-prog-name') for a in args): - shared.exec_process([clang, *compile.get_cflags(tuple(args)), *args]) + if any(a in PASSTHROUGH_FLAGS for a in args) or any(a.startswith(p) for p in PASSTHROUGH_PREFIXES for a in args): + # For several -print-xxx-name flags we just defer to clang rather than + # trying to re-implement the logic. + shared.exec_process([clang, *compile.get_cflags(tuple(args)), *newargs]) assert False, 'exec_process should not return' if '--cflags' in args: diff --git a/test/test_other.py b/test/test_other.py index a80e34bafa37e..397e57d412ac2 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -756,7 +756,7 @@ def test_print_resource_dir(self): @crossplatform def test_print_prog_name(self): - output = self.run_process([EMCC, '--print-prog-name=clang'], stdout=PIPE).stdout + output = self.run_process([EMCC, '-sASSERTIONS', '--print-prog-name=clang'], stdout=PIPE).stdout expected = CLANG_CC if WINDOWS: expected = os.path.normpath(utils.unsuffixed(CLANG_CC))