Skip to content

Commit

Permalink
Add support for py_binary tools (#10)
Browse files Browse the repository at this point in the history
Also adds tests for Python toolchains and their `python3` binary.
  • Loading branch information
fmeum authored Jun 6, 2024
1 parent 5edbd07 commit 7b2b12a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bazel_env.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ _tool = rule(
implementation = _tool_impl,
attrs = {
"target": attr.label(
allow_single_file = True,
allow_files = True,
cfg = _flip_output_dir,
executable = True,
),
Expand Down
15 changes: 15 additions & 0 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
load("@buildozer//:buildozer.bzl", "BUILDOZER_LABEL")
load("@bazel_env.bzl", "bazel_env")
load("@rules_python//python:py_binary.bzl", "py_binary")

# `bazel run //:bazel_env` prints a summary and setup steps.
bazel_env(
name = "bazel_env",
toolchains = {
"cc_toolchain": "@bazel_tools//tools/cpp:current_cc_toolchain",
"jdk": "@rules_java//toolchains:current_host_java_runtime",
"python": "@rules_python//python:current_py_toolchain",
},
tools = {
# Tool paths can reference the Make variables provided by toolchains.
Expand All @@ -18,6 +20,8 @@ bazel_env(
"jar": "$(JAVABASE)/bin/jar",
"java": "$(JAVA)",
"jq": ":jq",
"python": "$(PYTHON3)",
"python_tool": ":python_tool",
},
)

Expand All @@ -30,6 +34,17 @@ alias(
}),
)

py_binary(
name = "python_tool",
srcs = ["python_tool.py"],
deps = [":python_tool_lib"],
)

py_library(
name = "python_tool_lib",
srcs = ["python_tool_lib.py"],
)

# Lines below are not part of the example, they are used to test the example.

sh_test(
Expand Down
7 changes: 7 additions & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ bazel_dep(name = "buildifier_prebuilt", version = "6.4.0")
bazel_dep(name = "buildozer", version = "7.1.2")
bazel_dep(name = "rules_go", version = "0.47.1")
bazel_dep(name = "rules_java", version = "7.5.0")
bazel_dep(name = "rules_python", version = "0.32.2")
bazel_dep(name = "platforms", version = "0.0.10")

# Don't update the versions below, they are only used to verify the hermeticity of bazel_env.

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.20.14")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
is_default = True,
python_version = "3.11.8",
)

http_file = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")

http_file(
Expand Down
26 changes: 17 additions & 9 deletions examples/bazel_env_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ build_workspace_directory="$(dirname "$(readlink -f MODULE.bazel)")"
function assert_cmd_output() {
local -r cmd="$1"
local -r expected_output="$2"
local -r extra_path="${3:-}"

local -r bazel_env="$build_workspace_directory/bazel-out/bazel_env-opt/bin/bazel_env/bin"
local -r actual_output="$(PATH="$bazel_env:/bin" $cmd 2>&1 | head -n 1 || true)"
local -r actual_output="$(PATH="$bazel_env:/bin$extra_path" $cmd 2>&1 | head -n 1 || true)"

# Allow for wildcard matching first.
if [[ $actual_output == $expected_output ]]; then
Expand Down Expand Up @@ -55,17 +56,20 @@ expected_output='
✅ direnv added bazel-out/bazel_env-opt/bin/bazel_env/bin to PATH
Tools available in PATH:
* bazel-cc: $(CC)
* buildifier: @buildifier_prebuilt//:buildifier
* buildozer: @@buildozer~~buildozer_binary~buildozer_binary//:buildozer.exe
* go: @rules_go//go
* jar: $(JAVABASE)/bin/jar
* java: $(JAVA)
* jq: :jq
* bazel-cc: $(CC)
* buildifier: @buildifier_prebuilt//:buildifier
* buildozer: @@buildozer~~buildozer_binary~buildozer_binary//:buildozer.exe
* go: @rules_go//go
* jar: $(JAVABASE)/bin/jar
* java: $(JAVA)
* jq: :jq
* python: $(PYTHON3)
* python_tool: :python_tool
Toolchains available at stable relative paths:
* cc_toolchain: bazel-out/bazel_env-opt/bin/bazel_env/toolchains/cc_toolchain
* jdk: bazel-out/bazel_env-opt/bin/bazel_env/toolchains/jdk'
* jdk: bazel-out/bazel_env-opt/bin/bazel_env/toolchains/jdk
* python: bazel-out/bazel_env-opt/bin/bazel_env/toolchains/python'

diff <(echo "$expected_output") <(echo "$status_out") || exit 1

Expand All @@ -82,8 +86,12 @@ assert_cmd_output "go version" "go version go1.20.14 $(uname|tr '[:upper:]' '[:l
assert_cmd_output "jar --version" "jar 17.0.8.1"
assert_cmd_output "java --version" "openjdk 17.0.8.1 2023-08-24 LTS"
assert_cmd_output "jq --version" "jq-1.5"
assert_cmd_output "python --version" "Python 3.11.8"
# Bazel's Python launcher requires a system installation of python3.
assert_cmd_output "python_tool" "python_tool version 0.0.1" ":$(dirname "$(which python3)")"

#### Toolchains ####

[[ -d "$build_workspace_directory/bazel-out/bazel_env-opt/bin/bazel_env/toolchains/cc_toolchain" ]]
assert_cmd_output "$build_workspace_directory/bazel-out/bazel_env-opt/bin/bazel_env/toolchains/jdk/bin/java --version" "openjdk 17.0.8.1 2023-08-24 LTS"
assert_cmd_output "$build_workspace_directory/bazel-out/bazel_env-opt/bin/bazel_env/toolchains/python/bin/python3 --version" "Python 3.11.8"
4 changes: 4 additions & 0 deletions examples/python_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from python_tool_lib import print_version

if __name__ == "__main__":
print_version()
2 changes: 2 additions & 0 deletions examples/python_tool_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def print_version():
print("python_tool version 0.0.1")

0 comments on commit 7b2b12a

Please sign in to comment.