Skip to content

Commit 3cf87f6

Browse files
committed
Construct environment variables necessary to run emcc process when they're not supplied from aquery output
In Bazel < 6.1.0, aquery doesn't provide environment variables. See #154 for more context.
1 parent f7e54ab commit 3cf87f6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

refresh.template.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,19 @@ def _apple_platform_patch(compile_args: typing.List[str]):
776776
return compile_args
777777

778778

779+
def _get_sysroot(args: typing.List[str]):
780+
"""Get path to sysroot from command line arguments."""
781+
for idx, arg in enumerate(args):
782+
if arg == '--sysroot' or arg == '-isysroot':
783+
if idx + 1 < len(args):
784+
return pathlib.PurePath(args[idx + 1])
785+
elif arg.startswith('--sysroot='):
786+
return pathlib.PurePath(arg[len('--sysroot='):])
787+
elif arg.startswith('-isysroot'):
788+
return pathlib.PurePath(arg[len('-isysroot'):])
789+
return None
790+
791+
779792
def _emscripten_platform_patch(compile_action):
780793
"""De-Bazel the command into something clangd can parse.
781794
@@ -786,13 +799,24 @@ def _emscripten_platform_patch(compile_action):
786799
return compile_action.arguments
787800

788801
workspace_absolute = pathlib.PurePath(os.environ["BUILD_WORKSPACE_DIRECTORY"])
802+
sysroot = _get_sysroot(compile_action.arguments)
803+
assert sysroot, f'Emscripten sysroot not detected in CMD: {compile_action.arguments}'
804+
805+
def get_workspace_root(path_from_execroot: pathlib.PurePath):
806+
if path_from_execroot.parts[0] != 'external':
807+
return pathlib.PurePath('.')
808+
return pathlib.PurePath('external') / path_from_execroot.parts[1]
789809

790810
environment = compile_action.environmentVariables.copy()
791811
environment['EXT_BUILD_ROOT'] = str(workspace_absolute)
792812
environment['EMCC_SKIP_SANITY_CHECK'] = '1'
793813
environment['EM_COMPILER_WRAPPER'] = str(pathlib.PurePath({print_args_executable}))
794814
if 'PATH' not in environment:
795815
environment['PATH'] = os.environ['PATH']
816+
if 'EM_BIN_PATH' not in environment:
817+
environment['EM_BIN_PATH'] = str(get_workspace_root(sysroot))
818+
if 'EM_CONFIG_PATH' not in environment:
819+
environment['EM_CONFIG_PATH'] = str(get_workspace_root(emcc_driver) / 'emscripten_toolchain' / 'emscripten_config')
796820

797821
# We run the emcc process with the environment variable EM_COMPILER_WRAPPER to intercept the command line arguments passed to `clang`.
798822
emcc_process = subprocess.run(

0 commit comments

Comments
 (0)