diff options
author | Yifan Hong <elsk@google.com> | 2024-03-04 17:07:42 -0800 |
---|---|---|
committer | Yifan Hong <elsk@google.com> | 2024-03-04 17:07:42 -0800 |
commit | dd37d255392cf7a7fd1bb6aaa94c855315614267 (patch) | |
tree | eae201fbe944f01b5b5bff045e78a029239847dd | |
parent | 6746ab1a241ad0900bbe459635b8846f51b08c3a (diff) | |
download | build-dd37d255392cf7a7fd1bb6aaa94c855315614267.tar.gz |
kleaf: Move bison() to bison_wrapper.bzl.
... and rename it to bison_wrapper. This avoids
accidental usage outside Kleaf.
Test: TH
Bug: 291816237
Change-Id: Ib21c224ea0bbb4d0a412a4e1d6c7e29e9ae3857e
-rw-r--r-- | BUILD.bazel | 6 | ||||
-rw-r--r-- | kleaf/hermetic_tools.bzl | 55 | ||||
-rw-r--r-- | kleaf/impl/BUILD.bazel | 1 | ||||
-rw-r--r-- | kleaf/impl/bison_wrapper.bzl | 75 |
4 files changed, 80 insertions, 57 deletions
diff --git a/BUILD.bazel b/BUILD.bazel index a823216..f569f2b 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -14,7 +14,8 @@ load("@bazel_skylib//lib:paths.bzl", "paths") load("@kernel_toolchain_info//:dict.bzl", "VARS") -load("//build/kernel/kleaf:hermetic_tools.bzl", "bison", "hermetic_genrule", "hermetic_tools") +load("//build/kernel/kleaf:hermetic_tools.bzl", "hermetic_genrule", "hermetic_tools") +load("//build/kernel/kleaf/impl:bison_wrapper.bzl", "bison_wrapper") filegroup( name = "sysroot", @@ -28,11 +29,12 @@ filegroup( ], ) -bison( +bison_wrapper( name = "bison", actual = "//prebuilts/build-tools:linux-x86/bin/bison", pkgdata_dir = "//prebuilts/build-tools:bison.runfiles_dir", pkgdata_files = "//prebuilts/build-tools:bison.runfiles", + visibility = ["//visibility:private"], ) _HERMETIC_TOOLS = [ diff --git a/kleaf/hermetic_tools.bzl b/kleaf/hermetic_tools.bzl index 660038d..294eebd 100644 --- a/kleaf/hermetic_tools.bzl +++ b/kleaf/hermetic_tools.bzl @@ -231,58 +231,3 @@ Use hermetic_toolchain or hermetic_genrule for the full hermetic toolchain. name = name + "/" + alias, **alias_kwargs ) - -def _bison_impl(ctx): - file = ctx.actions.declare_file("{}/bison".format(ctx.attr.name)) - root_from_base = "/".join([".."] * len(paths.dirname(file.path).split("/"))) - short_root_from_base = "/".join([".."] * len(paths.dirname(file.short_path).split("/"))) - - content = """\ -#!/bin/sh - -if [ -n "${{BUILD_WORKSPACE_DIRECTORY}}" ]; then - # When bazel run, I am at short_path - KLEAF_REPO_DIR=${{0%/*}}/{short_root_from_base} -else - # When bazel build, I am at path - KLEAF_REPO_DIR=${{0%/*}}/{root_from_base} -fi - -export BISON_PKGDATADIR=${{KLEAF_REPO_DIR}}/{pkgdata_dir} -export M4=$(which m4) - -if [ -z "${{M4}}" ]; then - echo "ERROR: m4 is not found!" >&2 - exit 1 -fi - -if [ ! -d "${{BISON_PKGDATADIR}}" ]; then - echo "ERROR: BISON_PKGDATADIR ${{BISON_PKGDATADIR}} is empty!" >&2 - exit 1 -fi - -${{KLEAF_REPO_DIR}}/{actual} $* -""".format( - pkgdata_dir = ctx.file.pkgdata_dir.path, - actual = ctx.file.actual.path, - root_from_base = root_from_base, - short_root_from_base = short_root_from_base, - ) - ctx.actions.write(file, content, is_executable = True) - - return DefaultInfo( - files = depset([file]), - runfiles = ctx.runfiles( - files = [ctx.file.actual], - transitive_files = ctx.attr.pkgdata_files.files, - ), - ) - -bison = rule( - implementation = _bison_impl, - attrs = { - "actual": attr.label(allow_single_file = True), - "pkgdata_dir": attr.label(allow_single_file = True), - "pkgdata_files": attr.label(allow_files = True), - }, -) diff --git a/kleaf/impl/BUILD.bazel b/kleaf/impl/BUILD.bazel index c1715ee..285928f 100644 --- a/kleaf/impl/BUILD.bazel +++ b/kleaf/impl/BUILD.bazel @@ -42,6 +42,7 @@ bzl_library( "abi/protected_exports.bzl", "abi/trim_nonlisted_kmi_utils.bzl", "android_filegroup.bzl", + "bison_wrapper.bzl", "btf.bzl", "cache_dir.bzl", "checkpatch.bzl", diff --git a/kleaf/impl/bison_wrapper.bzl b/kleaf/impl/bison_wrapper.bzl new file mode 100644 index 0000000..bfe4444 --- /dev/null +++ b/kleaf/impl/bison_wrapper.bzl @@ -0,0 +1,75 @@ +# Copyright (C) 2024 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""`bison` wrapper.""" + +load("@bazel_skylib//lib:paths.bzl", "paths") + +visibility("//build/kernel/...") + +def _bison_wrapper_impl(ctx): + file = ctx.actions.declare_file("{}/bison".format(ctx.attr.name)) + root_from_base = "/".join([".."] * len(paths.dirname(file.path).split("/"))) + short_root_from_base = "/".join([".."] * len(paths.dirname(file.short_path).split("/"))) + + content = """\ +#!/bin/sh + +if [ -n "${{BUILD_WORKSPACE_DIRECTORY}}" ]; then + # When bazel run, I am at short_path + KLEAF_REPO_DIR=${{0%/*}}/{short_root_from_base} +else + # When bazel build, I am at path + KLEAF_REPO_DIR=${{0%/*}}/{root_from_base} +fi + +export BISON_PKGDATADIR=${{KLEAF_REPO_DIR}}/{pkgdata_dir} +export M4=$(which m4) + +if [ -z "${{M4}}" ]; then + echo "ERROR: m4 is not found!" >&2 + exit 1 +fi + +if [ ! -d "${{BISON_PKGDATADIR}}" ]; then + echo "ERROR: BISON_PKGDATADIR ${{BISON_PKGDATADIR}} is empty!" >&2 + exit 1 +fi + +${{KLEAF_REPO_DIR}}/{actual} $* +""".format( + pkgdata_dir = ctx.file.pkgdata_dir.path, + actual = ctx.file.actual.path, + root_from_base = root_from_base, + short_root_from_base = short_root_from_base, + ) + ctx.actions.write(file, content, is_executable = True) + + return DefaultInfo( + files = depset([file]), + runfiles = ctx.runfiles( + files = [ctx.file.actual], + transitive_files = ctx.attr.pkgdata_files.files, + ), + ) + +bison_wrapper = rule( + implementation = _bison_wrapper_impl, + doc = "Creates a wrapper script over real `bison` binary.", + attrs = { + "actual": attr.label(allow_single_file = True), + "pkgdata_dir": attr.label(allow_single_file = True), + "pkgdata_files": attr.label(allow_files = True), + }, +) |