Skip to content

Commit

Permalink
Merge pull request #94 from tweag/cb/upgrade-bazel
Browse files Browse the repository at this point in the history
Upgrade Bazel versions, remove Bazel 5
  • Loading branch information
mergify[bot] authored Dec 12, 2024
2 parents 9d16f12 + 97d4ce7 commit 8192e41
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .bazeliskrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
USE_BAZEL_VERSION=7.0.0
USE_BAZEL_VERSION=7.4.1
4 changes: 1 addition & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Compatibility options (per bazel version)
common:bazel8 --noincompatible_disallow_ctx_resolve_tools
common:bazel8 --config=common

common:bazel7 --config=common

common:bazel6 --config=common

common:bazel5 --config=common

common:common --noverbose_failures

# Remote Cache Configuration
Expand Down
16 changes: 2 additions & 14 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,7 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
bazel_mode: [workspace, module]
version: ["5.4.1", "6.4.0", "7.0.0", "8.0.0"]
include:
# Bazel 5.4.1 does not find Visual Studio on windows-2022. So, we
# test it on windows-2019.
- version: 5.4.1
bazel_mode: workspace
os: windows-2019
exclude:
- version: 5.4.1
bazel_mode: module
# Bazel 5.4.1 does not find Visual Studio on windows-2022.
- version: 5.4.1
os: windows-latest
version: ["6.5.0", "7.4.1", "8.0.0"]
defaults:
run:
shell: bash
Expand Down Expand Up @@ -62,7 +50,7 @@ jobs:
uses: tweag/write-bazelrc@v0
if: ${{ matrix.bazel_mode == 'module' }}
with:
content: build --experimental_enable_bzlmod
content: build --enable_bzlmod
- name: Configure the Bazel version
run: |
echo "USE_BAZEL_VERSION=${{ matrix.version }}" > .bazeliskrc
Expand Down
5 changes: 1 addition & 4 deletions sh/experimental/posix_hermetic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,10 @@ def _custom_rule_impl(ctx):
"RUNFILES_DIR": toolchain.tool[DefaultInfo].files_to_run.runfiles_manifest.dirname,
"RUNFILES_MANIFEST_FILE": toolchain.tool[DefaultInfo].files_to_run.runfiles_manifest.path,
}
(tools_inputs, tools_manifest) = ctx.resolve_tools(tools = [toolchain.tool])
ctx.actions.run(
executable = sh_binaries_info.executables["grep"],
executable = sh_binaries_info.files_to_run["grep"],
env = tools_env, # Pass the environment into the action.
inputs = tools_inputs,
input_manifests = tools_manifest,
...
)
```
Expand Down
37 changes: 20 additions & 17 deletions sh/sh.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ShBinariesInfo = provider(
doc = "The description of a sh_binaries target.",
fields = {
"executables": "dict of File, The executables included in the bundle by name.",
"files_to_run": "dict of FilesToRunProvider, to be passed to ctx.action.run / ctx.action.run_shell.",
"paths": "depset of string, The directories under which the binaries can be found.",
},
)
Expand All @@ -21,13 +22,15 @@ def _sh_binaries_from_srcs(ctx, srcs, is_windows):
executable_files = []
runfiles = ctx.runfiles()
executables_dict = dict()
files_to_run_dict = dict()
executable_paths = []

for src in srcs:
if src[DefaultInfo].files_to_run == None or src[DefaultInfo].files_to_run.executable == None:
fail("srcs must be executable, but '{}' is not.".format(src.label))

executable = src[DefaultInfo].files_to_run.executable
files_to_run = src[DefaultInfo].files_to_run
executable = files_to_run.executable
name = executable.basename
if is_windows:
(noext, ext) = paths.split_extension(executable.basename)
Expand All @@ -44,18 +47,21 @@ def _sh_binaries_from_srcs(ctx, srcs, is_windows):
executable_files.append(executable)
runfiles = runfiles.merge(src[DefaultInfo].default_runfiles)
executables_dict[name] = executable
files_to_run_dict[name] = files_to_run
executable_paths.append(executable.dirname)

return struct(
executable_files = executable_files,
runfiles = runfiles,
executables_dict = executables_dict,
executable_paths = executable_paths,
files_to_run_dict = files_to_run_dict,
)

def _sh_binaries_from_deps(ctx, deps):
executable_files = []
runfiles = ctx.runfiles()
files_to_run_dict = dict()
executables_dict = dict()
executable_paths = []

Expand All @@ -66,13 +72,15 @@ def _sh_binaries_from_deps(ctx, deps):
executable_files.append(dep[DefaultInfo].files)
runfiles = runfiles.merge(dep[DefaultInfo].default_runfiles)
executables_dict.update(dep[ShBinariesInfo].executables)
files_to_run_dict.update(dep[ShBinariesInfo].files_to_run)
executable_paths.append(dep[ShBinariesInfo].paths)

return struct(
executable_files = executable_files,
runfiles = runfiles,
executables_dict = executables_dict,
executable_paths = executable_paths,
files_to_run_dict = files_to_run_dict,
)

def _runfiles_from_data(ctx, data):
Expand All @@ -91,6 +99,11 @@ def _mk_sh_binaries_info(direct, transitive):
transitive.executables_dict,
direct.executables_dict,
),
files_to_run = dicts.add(
# The order is important so that srcs take precedence over deps on collision.
transitive.files_to_run_dict,
direct.files_to_run_dict,
),
paths = depset(
direct = direct.executable_paths,
# Reverse the order so that later deps take precedence.
Expand Down Expand Up @@ -244,13 +257,10 @@ load("@rules_sh//sh:sh.bzl", "ShBinariesInfo")
def _custom_rule_impl(ctx):
tools = ctx.attr.tools[ShBinariesInfo]
(tools_inputs, tools_manifest) = ctx.resolve_tools(tools = [ctx.attr.tools])
# Use binary-a in a `run` action.
ctx.actions.run(
executable = tools.executables["binary-a"], # Invoke binary-a
inputs = tools_inputs,
input_manifests = tools_manifest,
executable = tools.files_to_run["binary-a"], # Invoke binary-a
...
)
Expand All @@ -261,11 +271,9 @@ def _custom_rule_impl(ctx):
binary_b = tools.executables["binary-b"].path, # Path to binary-b
]),
tools = [
tools.executables["binary-a"],
tools.executables["binary-b"],
tools.files_to_run["binary-a"],
tools.files_to_run["binary-b"],
],
inputs = tools_inputs,
input_manifests = tools_manifest,
...
)
Expand All @@ -275,11 +283,9 @@ def _custom_rule_impl(ctx):
path = ":".join(tools.paths.to_list()),
),
tools = [
tools.executables["binary-a"],
tools.executables["binary-b"],
tools.files_to_run["binary-a"],
tools.files_to_run["binary-b"],
],
inputs = tools_inputs,
input_manifests = tools_manifest,
...
)
```
Expand Down Expand Up @@ -318,7 +324,6 @@ And in a custom rule as follows:
```bzl
def _custom_rule_impl(ctx):
tools = ctx.attr.tools[ShBinariesInfo]
(tools_inputs, tools_manifest) = ctx.resolve_tools(tools = [ctx.attr.tools])
# The explicit RUNFILES_DIR/RUNFILES_MANIFEST_FILE is a workaround for
# https://github.com/bazelbuild/bazel/issues/15486
tools_env = {
Expand All @@ -327,10 +332,8 @@ def _custom_rule_impl(ctx):
}
ctx.actions.run(
executable = tools.executables["binary-a"],
executable = tools.files_to_run["binary-a"],
env = tools_env, # Pass the environment into the action.
inputs = tools_inputs,
input_manifests = tools_manifest,
...
)
```
Expand Down
2 changes: 1 addition & 1 deletion tests/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
try-import %workspace%/../.bazelrc
import %workspace%/../.bazelrc
try-import %workspace%/../.bazelrc.local
6 changes: 1 addition & 5 deletions tests/posix_hermetic/posix_hermetic_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,6 @@ def _test_runfiles_toolchain():
def _runfiles_custom_rule_impl(ctx):
toolchain = ctx.toolchains["@rules_sh//sh/posix:toolchain_type"]

(tools_inputs, tools_manifest) = ctx.resolve_tools(tools = [toolchain.tool])

# Override the argv[0] relative runfiles tree or manifest by the bundle's.
# This is a workaround for https://github.com/bazelbuild/bazel/issues/15486
tools_env = {
Expand All @@ -620,11 +618,9 @@ def _runfiles_custom_rule_impl(ctx):
output = ctx.actions.declare_file("{}.txt".format(ctx.label.name))
ctx.actions.run_shell(
outputs = [output],
inputs = tools_inputs,
input_manifests = tools_manifest,
env = tools_env,
tools = [
toolchain.sh_binaries_info.executables["echo"],
toolchain.sh_binaries_info.files_to_run["echo"],
],
arguments = [
toolchain.sh_binaries_info.executables["echo"].path,
Expand Down
35 changes: 27 additions & 8 deletions tests/sh_binaries/sh_binaries_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ def _bundle_two_binaries_test_impl(ctx):
bundle_binaries_info.executables["hello_world"],
)

asserts.equals(
env,
bundle_binaries_info.executables["hello_world"],
bundle_binaries_info.files_to_run["hello_world"].executable,
)

asserts.true(
env,
"hello_data" in bundle_binaries_info.executables,
Expand All @@ -268,6 +274,12 @@ def _bundle_two_binaries_test_impl(ctx):
bundle_binaries_info.executables["hello_data"],
)

asserts.equals(
env,
bundle_binaries_info.executables["hello_data"],
bundle_binaries_info.files_to_run["hello_data"].executable,
)

asserts.equals(
env,
1,
Expand Down Expand Up @@ -380,6 +392,12 @@ def _merge_bundles_test_impl(ctx):
bundle_binaries_info.executables["hello_world"],
)

asserts.equals(
env,
bundle_binaries_info.executables["hello_world"],
bundle_binaries_info.files_to_run["hello_world"].executable,
)

asserts.true(
env,
"hello_data" in bundle_binaries_info.executables,
Expand All @@ -392,6 +410,12 @@ def _merge_bundles_test_impl(ctx):
bundle_binaries_info.executables["hello_data"],
)

asserts.equals(
env,
bundle_binaries_info.executables["hello_data"],
bundle_binaries_info.files_to_run["hello_data"].executable,
)

asserts.equals(
env,
1,
Expand Down Expand Up @@ -472,7 +496,6 @@ def _test_merge_bundles():

def _custom_rule_impl(ctx):
tools = ctx.attr.tools[ShBinariesInfo]
(tools_inputs, tools_manifest) = ctx.resolve_tools(tools = [ctx.attr.tools])

# Override the argv[0] relative runfiles tree or manifest by the bundle's.
# This is a workaround for https://github.com/bazelbuild/bazel/issues/15486
Expand All @@ -484,22 +507,19 @@ def _custom_rule_impl(ctx):
output_run = ctx.actions.declare_file("custom_rule_run_output")
ctx.actions.run(
outputs = [output_run],
inputs = tools_inputs,
executable = tools.executables["hello_data"],
executable = tools.files_to_run["hello_data"],
arguments = [output_run.path],
mnemonic = "RunExecutableWithBundle",
progress_message = "Running hello_data",
env = tools_env,
input_manifests = tools_manifest,
)

output_run_shell = ctx.actions.declare_file("custom_rule_run_shell_output")
ctx.actions.run_shell(
outputs = [output_run_shell],
inputs = tools_inputs,
tools = [
tools.executables["hello_world"],
tools.executables["hello_data"],
tools.files_to_run["hello_world"],
tools.files_to_run["hello_data"],
],
arguments = [
tools.executables["hello_world"].path,
Expand All @@ -510,7 +530,6 @@ def _custom_rule_impl(ctx):
command = "$1 > $3 && $2 >> $3",
progress_message = "Running hello_world and hello_data",
env = tools_env,
input_manifests = tools_manifest,
)

default_info = DefaultInfo(
Expand Down

0 comments on commit 8192e41

Please sign in to comment.