Skip to content

Added patch to resolve compiler from inside cc_wrapper.sh script. #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions refresh.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,60 @@ def get_workspace_root(path_from_execroot: pathlib.PurePath):
END_ARGS_MARKER = '===HEDRON_COMPILE_COMMANDS_END_ARGS==='


def _unwrap_cc_wrapper(script_path: str):
# Validate input script path
if not os.path.isfile(script_path):
raise ValueError(
f"Invalid script path when unwrapping cc_wrapper.sh: {script_path}"
)

# Get absolute path of script_path
script_path = os.path.abspath(script_path)

# Define bash commands to extract the compiler command
commands = " && ".join(
[
"set -T", # DEBUG & Traps need to be inherited by subshells
"""trap 'if [[ $BASH_COMMAND =~ ^[[:space:]]*(/[^[:space:]]*/)?(clang|clang\+\+|gcc|g\+\+|emcc|em\+\+)([[:space:]]|$|[^-]) ]]; then
echo $(read -ra arr <<< "$BASH_COMMAND" && echo "${arr[0]}") # Extract first element (compiler path)
exit 0 # Exit after capturing compiler
fi' DEBUG""", # A trap to capture the compiler command
f"source {script_path}", # Run the wrapper script within this script's context
]
)

try:
# Run the bash command to capture the compiler path
result = subprocess.run(
["bash", "-c", commands],
capture_output=True,
text=True,
check=False,
)

# Check for stderr output and raise an error if present
if result.stderr:
raise RuntimeError(
f"Failed to unwrap cc_wrapper.sh: {result.stderr.strip()}"
)

# Get the output (compiler path)
compiler_path = os.path.realpath(result.stdout.strip())

# Validate the output is a single, valid executable path
if not compiler_path:
raise ValueError("No compiler path extracted from script")
if not os.path.isfile(compiler_path):
raise ValueError(f"Invalid compiler path: {compiler_path}")
if not os.access(compiler_path, os.X_OK):
raise ValueError(f"Compiler path is not executable: {compiler_path}")

return compiler_path

except subprocess.SubprocessError as e:
raise RuntimeError(f"Subprocess error while unwrapping cc_wrapper.sh: {str(e)}")


def _all_platform_patch(compile_args: typing.List[str]):
"""Apply de-Bazeling fixes to the compile command that are shared across target platforms."""
# clangd writes module cache files to the wrong place
Expand Down Expand Up @@ -891,6 +945,9 @@ def _all_platform_patch(compile_args: typing.List[str]):
real_compiler_path = shutil.which(compiler)
if real_compiler_path:
compile_args[0] = real_compiler_path

if compile_args[0].endswith("cc_wrapper.sh"):
compile_args[0] = _unwrap_cc_wrapper(compile_args[0])

# Any other general fixes would go here...

Expand Down