Skip to content

Commit 927fbd1

Browse files
committed
Use environment variables from aquery output
1 parent 24d81fc commit 927fbd1

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

refresh.template.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -745,19 +745,7 @@ def _apple_platform_patch(compile_args: typing.List[str]):
745745
return compile_args
746746

747747

748-
def _get_sysroot(args: typing.List[str]):
749-
for idx, arg in enumerate(args):
750-
if arg == '--sysroot' or arg == '-isysroot':
751-
if idx + 1 < len(args):
752-
return pathlib.PurePath(args[idx + 1])
753-
elif arg.startswith('--sysroot='):
754-
return pathlib.PurePath(arg[len('--sysroot='):])
755-
elif arg.startswith('-isysroot'):
756-
return pathlib.PurePath(arg[len('-isysroot'):])
757-
return None
758-
759-
760-
def _emscripten_platform_patch(compile_args: typing.List[str]):
748+
def _emscripten_platform_patch(compile_args: typing.List[str], action_env: typing.Dict[str, str]):
761749
"""De-Bazel the command into something clangd can parse.
762750
763751
This function has fixes specific to Emscripten platforms, but you should call it on all platforms. It'll determine whether the fixes should be applied or not
@@ -767,22 +755,13 @@ def _emscripten_platform_patch(compile_args: typing.List[str]):
767755
return compile_args
768756

769757
workspace_absolute = pathlib.PurePath(os.environ["BUILD_WORKSPACE_DIRECTORY"])
770-
sysroot = _get_sysroot(compile_args)
771-
assert sysroot, f'Emscripten sysroot not detected in CMD: {compile_args}'
772-
773-
def get_workspace_root(path_from_execroot: pathlib.PurePath):
774-
if path_from_execroot.parts[0] != 'external':
775-
return pathlib.PurePath('.')
776-
return pathlib.PurePath('external') / path_from_execroot.parts[1]
777-
778-
environment = {
779-
'EXT_BUILD_ROOT': str(workspace_absolute),
780-
'EM_BIN_PATH': str(get_workspace_root(sysroot)),
781-
'EM_CONFIG_PATH': str(get_workspace_root(emcc_driver) / 'emscripten_toolchain' / 'emscripten_config'),
782-
'EMCC_SKIP_SANITY_CHECK': '1',
783-
'EM_COMPILER_WRAPPER': str(pathlib.PurePath({print_args_executable})),
784-
'PATH': os.environ['PATH'],
785-
}
758+
759+
environment = dict(action_env)
760+
environment['EXT_BUILD_ROOT'] = str(workspace_absolute)
761+
environment['EMCC_SKIP_SANITY_CHECK'] = '1'
762+
environment['EM_COMPILER_WRAPPER'] = str(pathlib.PurePath({print_args_executable}))
763+
if 'PATH' not in environment:
764+
environment['PATH'] = os.environ['PATH']
786765

787766
# We run the emcc process with the environment variable EM_COMPILER_WRAPPER to intercept the command line arguments passed to `clang`.
788767
emcc_process = subprocess.run(
@@ -1064,9 +1043,14 @@ def _get_cpp_command_for_files(compile_action):
10641043
10651044
Undo Bazel-isms and figures out which files clangd should apply the command to.
10661045
"""
1046+
env_pairs = getattr(compile_action, 'environmentVariables', [])
1047+
env = {}
1048+
for pair in env_pairs:
1049+
env[pair.key] = pair.value
1050+
10671051
# Patch command by platform, revealing any hidden arguments.
10681052
compile_action.arguments = _apple_platform_patch(compile_action.arguments)
1069-
compile_action.arguments = _emscripten_platform_patch(compile_action.arguments)
1053+
compile_action.arguments = _emscripten_platform_patch(compile_action.arguments, action_env=env)
10701054
# Android and Linux and grailbio LLVM toolchains: Fine as is; no special patching needed.
10711055
compile_action.arguments = _all_platform_patch(compile_action.arguments)
10721056

0 commit comments

Comments
 (0)