From 2c0116280bf741c3cbe89bfcda39bde10dc99d39 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 10 Jun 2026 12:16:05 -0700 Subject: [PATCH] Honor clang resource dir in `-print-file-name` + `-print-search-dirs`. NFC --- emcc.py | 29 ++++++++++++++++++++--------- test/test_other.py | 22 ++++++++++++++-------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/emcc.py b/emcc.py index db64d9b046fd7..b3fcb00321127 100644 --- a/emcc.py +++ b/emcc.py @@ -29,6 +29,7 @@ import tarfile from dataclasses import dataclass from enum import Enum, auto, unique +from subprocess import PIPE # This assert needs to happen early, before any too-recent python syntax is used. # In particular it needs to happen before we import any python file that uses the @@ -172,6 +173,15 @@ def make_relative(filename): reproduce_file.add(rsp_name, os.path.join(root, 'response.txt')) +def get_clang_resource_dir(args): + resource_dir = [a for a in args if a.startswith(('-resource-dir=', '--resource-dir='))] + if resource_dir: + return resource_dir[-1].split('=')[1] + else: + output = utils.run_process([shared.CLANG_CC, '-print-resource-dir'], stdout=PIPE).stdout + return output.strip() + + @ToolchainProfiler.profile() def main(args): if shared.run_via_emxx: @@ -253,7 +263,9 @@ def main(args): if '-print-search-dirs' in args or '--print-search-dirs' in args: print(f'programs: ={config.LLVM_ROOT}') - print(f'libraries: ={cache.get_lib_dir(absolute=True)}') + resource_dir = get_clang_resource_dir(args) + libdir = cache.get_lib_dir(absolute=True) + print(f'libraries: ={resource_dir}{os.pathsep}{libdir}') return 0 if '-print-libgcc-file-name' in args or '--print-libgcc-file-name' in args: @@ -265,14 +277,13 @@ def main(args): print_file_name = [a for a in args if a.startswith(('-print-file-name=', '--print-file-name='))] if print_file_name: libname = print_file_name[-1].split('=')[1] - resource_dir = [a for a in args if a.startswith(('-resource-dir=', '--resource-dir='))] - if resource_dir: - system_libpath = resource_dir[-1].split('=')[1] - else: - system_libpath = cache.get_lib_dir(absolute=True) - fullpath = os.path.join(system_libpath, libname) - if os.path.isfile(fullpath): - print(fullpath) + resource_dir = get_clang_resource_dir(args) + system_libpath = cache.get_lib_dir(absolute=True) + for dirname in (resource_dir, system_libpath): + fullpath = os.path.join(dirname, libname) + if os.path.isfile(fullpath): + print(fullpath) + break else: print(libname) return 0 diff --git a/test/test_other.py b/test/test_other.py index 725c3b3fb5e0e..9718f62e9a30b 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -768,8 +768,8 @@ def test_print_prog_name(self): def test_print_file_name(self, args): # make sure the corresponding version of libc exists in the cache self.run_process([EMCC, test_file('hello_world.c'), '-O2'] + args) - output = self.run_process([EMCC, '-print-file-name=libc.a'] + args, stdout=PIPE).stdout - output2 = self.run_process([EMCC, '--print-file-name=libc.a'] + args, stdout=PIPE).stdout + output = self.run_process([EMCC, '-print-file-name=libc.a'] + args, stdout=PIPE).stdout.rstrip() + output2 = self.run_process([EMCC, '--print-file-name=libc.a'] + args, stdout=PIPE).stdout.rstrip() self.assertEqual(output, output2) filename = Path(output) settings.LTO = '-flto' in args @@ -777,12 +777,18 @@ def test_print_file_name(self, args): self.assertContained(cache.get_lib_name('libc.a'), str(filename)) @crossplatform - def test_print_file_name_with_resource_dir(self): - file = Path(EMCC).name - output = self.run_process([EMCC, f'-print-file-name={file}'], stdout=PIPE).stdout - output_relative = self.run_process([EMCC, '-resource-dir=' + path_from_root(), f'-print-file-name={file}'], stdout=PIPE).stdout - self.assertNotExists(output.rstrip()) - self.assertExists(output_relative.rstrip()) + def test_print_file_name_resdir(self): + output = self.run_process([EMCC, '-print-file-name=include/wmmintrin.h'], stdout=PIPE).stdout.rstrip() + print('print-file-name: ' + output) + self.assertExists(output) + + @crossplatform + def test_print_file_name_custom_resdir(self): + output = self.run_process([EMCC, '-print-file-name=emcc.py'], stdout=PIPE).stdout.rstrip() + self.assertNotExists(output) + + output_relative = self.run_process([EMCC, '-resource-dir=' + path_from_root(), '-print-file-name=emcc.py'], stdout=PIPE).stdout.rstrip() + self.assertExists(output_relative) def test_emar_em_config_flag(self): # Test that the --em-config flag is accepted but not passed down do llvm-ar.