From c13803de99c929183a0d09534ee67c6bbc4c2ef1 Mon Sep 17 00:00:00 2001 From: jkamins7 Date: Sat, 30 Jul 2022 16:48:38 -0400 Subject: [PATCH] Fix for CC being sometimes a path and sometimes a file on PATH (#1724) Possible fix for #1713 Co-authored-by: Jon Ross-Perkins --- bazel/cc_toolchains/clang_configuration.bzl | 23 +++++++++++---------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/bazel/cc_toolchains/clang_configuration.bzl b/bazel/cc_toolchains/clang_configuration.bzl index e6c2439e7ff6..ec9f826e2143 100644 --- a/bazel/cc_toolchains/clang_configuration.bzl +++ b/bazel/cc_toolchains/clang_configuration.bzl @@ -26,18 +26,19 @@ def _detect_system_clang(repository_ctx): # If the user provides an explicit `CC` environment variable, use that as # the compiler. This should point at the `clang` executable to use. cc = repository_ctx.os.environ.get("CC") + cc_path = None if cc: - version_output = _run(repository_ctx, [cc, "--version"]).stdout - if not "clang" in version_output: - fail("The `CC` environment variable is not a Clang compiler.") - return repository_ctx.path(cc) - - # Try looking on the path. We only check for the normal name. - system_clang = repository_ctx.which("clang") - if not system_clang: - fail("Unable to find a `clang` executable on the system path.") - - return system_clang + cc_path = repository_ctx.path(cc) + if not cc_path.exists: + cc_path = repository_ctx.which(cc) + if not cc_path: + cc_path = repository_ctx.which("clang") + if not cc_path: + fail("Cannot find clang or CC (%s); either correct your path or set the CC environment variable" % cc) + version_output = _run(repository_ctx, [cc_path, "--version"]).stdout + if "clang" not in version_output: + fail("Searching for clang or CC (%s), and found (%s), which is not a Clang compiler" % (cc, cc_path)) + return cc_path def _compute_clang_resource_dir(repository_ctx, clang): """Runs the `clang` binary to get its resource dir."""