Skip to content

Commit

Permalink
Fix configure.py issue with --clang_path.
Browse files Browse the repository at this point in the history
If --clang_path is passed a symlink, an error would occur when building C++ files that had includes from the standard library. E.g., a possible error was:

ERROR: /root/.cache/bazel/_bazel_root/e4ab50d61a21943a819d1e092972a817/external/zlib/BUILD.bazel:5:11: Compiling zutil.c [for tool] failed: undeclared inclusion(s) in rule '@zlib//:zlib':
this rule is missing dependency declarations for the following files included by 'zutil.c':
  '/usr/lib/clang/17/include/stddef.h'
  '/usr/lib/clang/17/include/__stddef_max_align_t.h'
  '/usr/lib/clang/17/include/limits.h'
  '/usr/lib/clang/17/include/stdarg.h'

The clang on the PATH, such as /usr/bin/clang-17, is usually symlinked to a file such as /usr/lib/llvm-17/bin/clang, so this would typically cause issues when building with --clang_path.

The issue is that bazel gives such errors when compiled with a symlinked clang. Without --clang_path, we resolved symlinks, but not with --clang_path. This change also resolves symlinks with --clang_path.

PiperOrigin-RevId: 636004628
  • Loading branch information
reedwm authored and copybara-github committed May 22, 2024
1 parent 8919ee3 commit 7d30432
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
25 changes: 15 additions & 10 deletions build_tools/configure/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,32 @@ def _find_executable(executable: str) -> Optional[str]:
return None


def _find_executable_or_die(executable: str) -> str:
def _find_executable_or_die(
executable_name: str, executable_path: Optional[str] = None
) -> str:
"""Finds executable and resolves symlinks or raises RuntimeError.
Resolving symlinks is sometimes necessary for finding system headers.
Args:
executable: The name of the executable that we want to find.
executable_name: The name of the executable that we want to find.
executable_path: If not None, the path to the executable.
Returns:
The path to the executable we are looking for.
The path to the executable we are looking for, after symlinks are resolved.
Raises:
RuntimeError: if path to the executable cannot be found.
"""
resolved_path_to_exe = _find_executable(executable)
if executable_path:
return str(pathlib.Path(executable_path).resolve(strict=True))
resolved_path_to_exe = _find_executable(executable_name)
if resolved_path_to_exe is None:
raise RuntimeError(
f"Could not find executable `{executable}`! "
f"Could not find executable `{executable_name}`! "
"Please change your $PATH or pass the path directly like"
f"`--{executable}_path=path/to/executable."
f"`--{executable_name}_path=path/to/executable."
)
logging.info("Found path to %s at %s", executable, resolved_path_to_exe)
logging.info("Found path to %s at %s", executable_name, resolved_path_to_exe)

return resolved_path_to_exe

Expand Down Expand Up @@ -236,7 +241,7 @@ def get_relevant_paths_and_versions(self, config: "XLAConfigOptions"):
self.ld_library_path = os.environ.get("LD_LIBRARY_PATH", None)

if config.host_compiler == HostCompiler.CLANG:
self.clang_path = self.clang_path or _find_executable_or_die("clang")
self.clang_path = _find_executable_or_die("clang", self.clang_path)
self.clang_major_version = (
self.clang_major_version or _get_clang_major_version(self.clang_path)
)
Expand All @@ -247,11 +252,11 @@ def get_relevant_paths_and_versions(self, config: "XLAConfigOptions"):
# directly.
self.lld_path = self.lld_path or shutil.which("ld.lld")
elif config.host_compiler == HostCompiler.GCC:
self.gcc_path = self.gcc_path or _find_executable_or_die("gcc")
self.gcc_path = _find_executable_or_die("gcc", self.gcc_path)

if config.backend == Backend.CUDA:
if config.cuda_compiler == CudaCompiler.CLANG:
self.clang_path = self.clang_path or _find_executable_or_die("clang")
self.clang_path = _find_executable_or_die("clang", self.clang_path)

if not self.cuda_compute_capabilities:
self.cuda_compute_capabilities = _get_cuda_compute_capabilities_or_die()
Expand Down
14 changes: 12 additions & 2 deletions build_tools/configure/configure_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import os

from absl.testing import absltest

from xla.build_tools import test_utils
Expand Down Expand Up @@ -55,7 +57,11 @@ def setUpClass(cls):
cls.clang_bazelrc_lines = [line.strip() for line in f.readlines()]

with (testdata / "gcc.bazelrc").open() as f:
cls.gcc_bazelrc_lines = [line.strip() for line in f.readlines()]
resolved_gcc_path = os.path.realpath(_GCC_PATH)
cls.gcc_bazelrc_lines = [
line.strip().replace(_GCC_PATH, resolved_gcc_path)
for line in f.readlines()
]

with (testdata / "cuda_clang.bazelrc").open() as f:
cls.cuda_clang_bazelrc_lines = [line.strip() for line in f.readlines()]
Expand All @@ -64,7 +70,11 @@ def setUpClass(cls):
cls.nvcc_clang_bazelrc_lines = [line.strip() for line in f.readlines()]

with (testdata / "nvcc_gcc.bazelrc").open() as f:
cls.nvcc_gcc_bazelrc_lines = [line.strip() for line in f.readlines()]
resolved_gcc_path = os.path.realpath(_GCC_PATH)
cls.nvcc_gcc_bazelrc_lines = [
line.strip().replace(_GCC_PATH, resolved_gcc_path)
for line in f.readlines()
]

def test_clang_bazelrc(self):
config = XLAConfigOptions(
Expand Down

0 comments on commit 7d30432

Please sign in to comment.