Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix licence checker paths in Bazel 7 onwards #39

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests
4 changes: 4 additions & 0 deletions .github/workflows/licence-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ jobs:
run: bazel run :format.check
- name: Run Licence Checker
run: bazel run //:licence-check
- name: Test Licence Checker
run: |
cd tests
bazel test :licence_check
1 change: 1 addition & 0 deletions licence-checker/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package(default_visibility = ["//visibility:public"])

load("@lowrisc_misc_linters_pip//:requirements.bzl", "requirement")
load("@rules_python//python:py_binary.bzl", "py_binary")

py_binary(
name = "licence-checker",
Expand Down
6 changes: 5 additions & 1 deletion rules/licence-checker-runner.template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

# Remember the runfiles dir since this is what paths like "@@LICENCE_CHECKER@@"
# and "@@CONFIG@@" are relative to once we `cd` elsewhere.
RUNFILES_DIR="$PWD"

WORKSPACE="@@WORKSPACE@@"

if [[ ! -z "${WORKSPACE}" ]]; then
Expand All @@ -16,4 +20,4 @@ else
exit 1
fi

"@@LICENCE_CHECKER@@" --config="@@CONFIG@@" "$@"
"${RUNFILES_DIR}/@@LICENCE_CHECKER@@" --config="${RUNFILES_DIR}/@@CONFIG@@" "$@"
20 changes: 3 additions & 17 deletions rules/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,20 @@ def _licence_check_impl(ctx):
},
)

# Hack to make Bazel build the checker correctly.
#
# Bazel py_binaries require a .runfiles directory to be present, but for
# some reason or another it does not provide a good way to extract those
# for building as a dependency from a PyInfo provider.
#
# https://github.com/bazelbuild/bazel/issues/7357
checker = ctx.actions.declare_file(ctx.label.name + ".checker-witness")
ctx.actions.run_shell(
tools = [ctx.executable.licence_check],
outputs = [checker],
command = 'touch "{}"'.format(checker.path),
)

workspace = ctx.file.workspace.path if ctx.file.workspace else ""
script = ctx.actions.declare_file(ctx.label.name + ".bash")
ctx.actions.expand_template(
template = ctx.file._runner,
output = script,
substitutions = {
"@@LICENCE_CHECKER@@": ctx.executable.licence_check.path,
"@@CONFIG@@": config.path,
"@@LICENCE_CHECKER@@": ctx.executable.licence_check.short_path,
"@@CONFIG@@": config.short_path,
"@@WORKSPACE@@": workspace,
},
is_executable = True,
)

files = [config, checker]
files = [config]
if ctx.file.workspace:
files.append(ctx.file.workspace)

Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MODULE.bazel.lock
16 changes: 16 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

load("@lowrisc_misc_linters//rules:rules.bzl", "licence_test")

licence_test(
name = "licence_check",
exclude_patterns = ["file_without_licence.txt"],
licence = """
Copyright lowRISC contributors.
Licensed under the Apache License, Version 2.0, see LICENSE for details.
SPDX-License-Identifier: Apache-2.0
""",
workspace = ":MODULE.bazel",
)
13 changes: 13 additions & 0 deletions tests/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

module(name = "lowrisc_misc_linters_tests")

bazel_dep(name = "lowrisc_misc_linters")
local_path_override(module_name = "lowrisc_misc_linters", path = "../")

bazel_dep(name = "rules_python", version = "0.34.0")
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.9")

17 changes: 17 additions & 0 deletions tests/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

local_repository(
name = "lowrisc_misc_linters",
path = "..",
)

load("@lowrisc_misc_linters//rules:repos.bzl", "lowrisc_misc_linters_repos")
lowrisc_misc_linters_repos()
load("@lowrisc_misc_linters//rules:deps.bzl", "lowrisc_misc_linters_dependencies")
lowrisc_misc_linters_dependencies()
load("@lowrisc_misc_linters//rules:pip.bzl", "lowrisc_misc_linters_pip_dependencies")
lowrisc_misc_linters_pip_dependencies()
load("@lowrisc_misc_linters_pip//:requirements.bzl", "install_deps")
install_deps()
5 changes: 5 additions & 0 deletions tests/file_with_licence.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

This file has a licence!
1 change: 1 addition & 0 deletions tests/file_without_licence.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file intentionally has no licence.