Skip to content

Commit 33658ba

Browse files
committed
Header finding: inherit action environment
Attempt to address #169
1 parent d7d9efd commit 33658ba

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

refresh.template.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def _is_nvcc(path: str):
232232
return os.path.basename(path).startswith('nvcc')
233233

234234

235-
def _get_headers_gcc(compile_args: typing.List[str], source_path: str, action_key: str):
235+
def _get_headers_gcc(compile_action, source_path: str, action_key: str):
236236
"""Gets the headers used by a particular compile command that uses gcc arguments formatting (including clang.)
237237
238238
Relatively slow. Requires running the C preprocessor if we can't hit Bazel's cache.
@@ -241,12 +241,12 @@ def _get_headers_gcc(compile_args: typing.List[str], source_path: str, action_ke
241241

242242
# Check to see if Bazel has an (approximately) fresh cache of the included headers, and if so, use them to avoid a slow preprocessing step.
243243
if action_key in _get_bazel_cached_action_keys(): # Safe because Bazel only holds one cached action key per path, and the key contains the path.
244-
for i, arg in enumerate(compile_args):
244+
for i, arg in enumerate(compile_action.arguments):
245245
if arg.startswith('-MF'):
246246
if len(arg) > 3: # Either appended, like -MF<file>
247247
dep_file_path = arg[:3]
248248
else: # Or after as a separate arg, like -MF <file>
249-
dep_file_path = compile_args[i+1]
249+
dep_file_path = compile_action.arguments[i+1]
250250
if os.path.isfile(dep_file_path):
251251
dep_file_last_modified = os.path.getmtime(dep_file_path) # Do before opening just as a basic hedge against concurrent write, even though we won't handle the concurrent delete case perfectly.
252252
with open(dep_file_path) as dep_file:
@@ -262,7 +262,7 @@ def _get_headers_gcc(compile_args: typing.List[str], source_path: str, action_ke
262262
# Clang on Apple doesn't let later flags override earlier ones, unfortunately.
263263
# These flags are prefixed with M for "make", because that's their output format.
264264
# *-dependencies is the long form. And the output file is traditionally *.d
265-
header_cmd = (arg for arg in compile_args
265+
header_cmd = (arg for arg in compile_action.arguments
266266
if not arg.startswith('-M') and not arg.endswith(('-dependencies', '.d')))
267267

268268
# Strip output flags. Apple clang tries to do a full compile if you don't.
@@ -290,6 +290,7 @@ def _get_headers_gcc(compile_args: typing.List[str], source_path: str, action_ke
290290
# MIN_PY=3.7: Replace PIPEs with capture_output.
291291
stdout=subprocess.PIPE,
292292
stderr=subprocess.PIPE,
293+
env=compile_action.environmentVariables,
293294
encoding=locale.getpreferredencoding(),
294295
check=False, # We explicitly ignore errors and carry on.
295296
)
@@ -408,15 +409,15 @@ def _subprocess_run_spilling_over_to_param_file_if_needed(command: typing.List[s
408409
raise
409410

410411

411-
def _get_headers_msvc(compile_args: typing.List[str], source_path: str):
412+
def _get_headers_msvc(compile_action, source_path: str):
412413
"""Gets the headers used by a particular compile command that uses msvc argument formatting (including clang-cl.)
413414
414415
Relatively slow. Requires running the C preprocessor.
415416
"""
416417
# Flags reference here: https://docs.microsoft.com/en-us/cpp/build/reference/compiler-options
417418
# Relies on our having made the workspace directory simulate a complete version of the execroot with //external junction
418419

419-
header_cmd = list(compile_args) + [
420+
header_cmd = list(compile_action.arguments) + [
420421
'/showIncludes', # Print included headers to stderr. https://docs.microsoft.com/en-us/cpp/build/reference/showincludes-list-include-files
421422
'/EP', # Preprocess (only, no compilation for speed), writing to stdout where we can easily ignore it instead of a file. https://docs.microsoft.com/en-us/cpp/build/reference/ep-preprocess-to-stdout-without-hash-line-directives
422423
]
@@ -427,12 +428,13 @@ def _get_headers_msvc(compile_args: typing.List[str], source_path: str):
427428
# Bazel should have supplied the environment variables in aquery output but doesn't https://github.com/bazelbuild/bazel/issues/12852
428429
# Non-Bazel Windows users would normally configure these by calling vcvars
429430
# For more, see https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line
430-
environment = dict(os.environ)
431-
environment['INCLUDE'] = os.pathsep.join((
432-
# Begin: template filled by Bazel
433-
{windows_default_include_paths}
434-
# End: template filled by Bazel
435-
))
431+
environment = compile_action.environmentVariables.copy()
432+
if 'INCLUDE' not in environment:
433+
environment['INCLUDE'] = os.pathsep.join((
434+
# Begin: template filled by Bazel
435+
{windows_default_include_paths}
436+
# End: template filled by Bazel
437+
))
436438

437439
header_search_process = _subprocess_run_spilling_over_to_param_file_if_needed(
438440
header_cmd,
@@ -584,9 +586,9 @@ def _get_headers(compile_action, source_path: str):
584586
return set(cached_headers)
585587

586588
if compile_action.arguments[0].endswith('cl.exe'): # cl.exe and also clang-cl.exe
587-
headers, should_cache = _get_headers_msvc(compile_action.arguments, source_path)
589+
headers, should_cache = _get_headers_msvc(compile_action, source_path)
588590
else:
589-
headers, should_cache = _get_headers_gcc(compile_action.arguments, source_path, compile_action.actionKey)
591+
headers, should_cache = _get_headers_gcc(compile_action, source_path, compile_action.actionKey)
590592

591593
# Cache for future use
592594
if output_file and should_cache:
@@ -815,8 +817,6 @@ def get_workspace_root(path_from_execroot: pathlib.PurePath):
815817
environment['EXT_BUILD_ROOT'] = str(workspace_absolute)
816818
environment['EMCC_SKIP_SANITY_CHECK'] = '1'
817819
environment['EM_COMPILER_WRAPPER'] = str(pathlib.PurePath({print_args_executable}))
818-
if 'PATH' not in environment:
819-
environment['PATH'] = os.environ['PATH']
820820
if 'EM_BIN_PATH' not in environment:
821821
environment['EM_BIN_PATH'] = str(get_workspace_root(sysroot))
822822
if 'EM_CONFIG_PATH' not in environment:
@@ -1110,6 +1110,8 @@ def _get_cpp_command_for_files(compile_action):
11101110
"""
11111111
# Condense aquery's environment variables into a dictionary, the format you might expect.
11121112
compile_action.environmentVariables = {pair.key: pair.value for pair in getattr(compile_action, 'environmentVariables', [])}
1113+
if 'PATH' not in compile_action.environmentVariables: # Bazel only adds if --incompatible_strict_action_env is passed--and otherwise inherits.
1114+
compile_action.environmentVariables['PATH'] = os.environ['PATH']
11131115

11141116
# Patch command by platform, revealing any hidden arguments.
11151117
compile_action.arguments = _apple_platform_patch(compile_action.arguments)

0 commit comments

Comments
 (0)