diff --git a/extension/download_tool.bzl b/extension/download_tool.bzl index d7b3827..cf858c8 100644 --- a/extension/download_tool.bzl +++ b/extension/download_tool.bzl @@ -25,6 +25,17 @@ def _get_platform(rctx): return "{}-{}".format(os_key, arch_key) +def _binary_path_for_platform(binary_subpath, platform): + """Return the binary path as packaged for the selected platform.""" + if platform.startswith("windows-") and not binary_subpath.endswith(".exe"): + return binary_subpath + ".exe" + return binary_subpath + +# Exposed only so the path resolution can be covered without downloading LLVM. +download_tool_test_utils = struct( + binary_path_for_platform = _binary_path_for_platform, +) + def _download_tool_impl(rctx): """Download and extract a tool for the current platform.""" platform = _get_platform(rctx) @@ -56,7 +67,7 @@ def _download_tool_impl(rctx): ) # Create BUILD.bazel that exports the tool binary - binary_path = rctx.attr.binary_subpath + binary_path = _binary_path_for_platform(rctx.attr.binary_subpath, platform) rctx.file("BUILD.bazel", content = """ package(default_visibility = ["//visibility:public"]) diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index dd5bf71..b613744 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -1,6 +1,9 @@ """Unit tests for the devtools runtime scripts.""" load("@rules_python//python:py_test.bzl", "py_test") +load(":download_tool_test.bzl", "download_tool_test_suite") + +download_tool_test_suite(name = "download_tool_tests") py_test( name = "compile_commands_test", diff --git a/tests/download_tool_test.bzl b/tests/download_tool_test.bzl new file mode 100644 index 0000000..6772637 --- /dev/null +++ b/tests/download_tool_test.bzl @@ -0,0 +1,34 @@ +"""Tests for platform-specific LLVM binary paths.""" + +load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") +load("//extension:download_tool.bzl", "download_tool_test_utils") + +def _windows_binary_path_test_impl(ctx): + env = unittest.begin(ctx) + binary_path = download_tool_test_utils.binary_path_for_platform + + asserts.equals(env, "bin/clang-format.exe", binary_path("bin/clang-format", "windows-x86_64")) + asserts.equals(env, "bin/clang-tidy.exe", binary_path("bin/clang-tidy", "windows-x86_64")) + asserts.equals(env, "bin/custom-tool.exe", binary_path("bin/custom-tool.exe", "windows-x86_64")) + + return unittest.end(env) + +windows_binary_path_test = unittest.make(_windows_binary_path_test_impl) + +def _unix_binary_path_test_impl(ctx): + env = unittest.begin(ctx) + binary_path = download_tool_test_utils.binary_path_for_platform + + asserts.equals(env, "bin/clang-format", binary_path("bin/clang-format", "linux-x86_64")) + asserts.equals(env, "bin/clang-tidy", binary_path("bin/clang-tidy", "darwin-aarch64")) + + return unittest.end(env) + +unix_binary_path_test = unittest.make(_unix_binary_path_test_impl) + +def download_tool_test_suite(name): + unittest.suite( + name, + windows_binary_path_test, + unix_binary_path_test, + )