From 17b822f31afe87de1c1e97bfc603abaf511a01c6 Mon Sep 17 00:00:00 2001 From: James Wainwright Date: Fri, 23 Aug 2024 16:26:51 +0100 Subject: [PATCH] Fix path to licence-checker on Bazel 7 --- licence-checker/BUILD | 1 + rules/licence-checker-runner.template.sh | 6 +++++- rules/rules.bzl | 20 +++----------------- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/licence-checker/BUILD b/licence-checker/BUILD index 4ced674..a85c7f3 100644 --- a/licence-checker/BUILD +++ b/licence-checker/BUILD @@ -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", diff --git a/rules/licence-checker-runner.template.sh b/rules/licence-checker-runner.template.sh index 015a5c4..a62ddd6 100644 --- a/rules/licence-checker-runner.template.sh +++ b/rules/licence-checker-runner.template.sh @@ -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 @@ -16,4 +20,4 @@ else exit 1 fi -"@@LICENCE_CHECKER@@" --config="@@CONFIG@@" "$@" +"${RUNFILES_DIR}/@@LICENCE_CHECKER@@" --config="${RUNFILES_DIR}/@@CONFIG@@" "$@" diff --git a/rules/rules.bzl b/rules/rules.bzl index 39858b7..f87c557 100644 --- a/rules/rules.bzl +++ b/rules/rules.bzl @@ -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)