From 7725b3e697de8847511646437670f050dd6aea82 Mon Sep 17 00:00:00 2001 From: Tony Aiuto Date: Tue, 1 Feb 2022 15:57:43 -0500 Subject: [PATCH 1/2] Make the .bzl files available as input to a bzl_library, without having to resort to the invasivness of a shadow tree of just the .bzl files without the other distributable files. --- doc_build/BUILD | 15 ++---------- pkg/BUILD | 21 ++++++++++++++-- pkg/private/BUILD | 6 ++++- pkg/private/deb/BUILD | 6 ++++- pkg/private/make_starlark_library.bzl | 35 +++++++++++++++++++++++++++ pkg/private/tar/BUILD | 5 +++- pkg/private/zip/BUILD | 6 ++++- pkg/releasing/BUILD | 5 +++- 8 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 pkg/private/make_starlark_library.bzl diff --git a/doc_build/BUILD b/doc_build/BUILD index 12420b94..2b1f7729 100644 --- a/doc_build/BUILD +++ b/doc_build/BUILD @@ -115,21 +115,10 @@ bzl_library( name = "rules_pkg_lib", srcs = [ "//:version.bzl", - "//pkg:mappings.bzl", - "//pkg:package_variables.bzl", - "//pkg:path.bzl", - "//pkg:providers.bzl", - "//pkg:rpm.bzl", - "//pkg:rpm_pfg.bzl", - "//pkg/legacy:rpm.bzl", - "//pkg/private:pkg_files.bzl", - "//pkg/private:util.bzl", - "//pkg/private/deb:deb.bzl", - "//pkg/private/tar:tar.bzl", - "//pkg/private/zip:zip.bzl", + "//pkg:bzl_srcs", "@bazel_skylib//lib:paths", ], - visibility = ["//visibility:private"], + visibility = ["//visibility:public"], ) py_binary( diff --git a/pkg/BUILD b/pkg/BUILD index cba70010..bfe4ef61 100644 --- a/pkg/BUILD +++ b/pkg/BUILD @@ -14,6 +14,7 @@ # -*- coding: utf-8 -*- load("@rules_python//python:defs.bzl", "py_binary", "py_library") +load("@rules_pkg//pkg/private:make_starlark_library.bzl", "starlark_library") package(default_applicable_licenses = ["//:license"]) @@ -42,7 +43,23 @@ filegroup( "//pkg/legacy:standard_package", "//pkg/rpm:standard_package", ], - visibility = ["//distro:__pkg__"], + visibility = [ + "//distro:__pkg__", + "//pkg:__pkg__", + ], +) + +starlark_library( + name = "bzl_srcs", + srcs = [ + ":standard_package", + "//pkg/private:standard_package", + "//pkg/private/deb:standard_package", + "//pkg/private/tar:standard_package", + "//pkg/private/zip:standard_package", + "//pkg/releasing:standard_package", + ], + visibility = ["//visibility:public"], ) # Used by pkg_rpm in rpm.bzl. @@ -57,8 +74,8 @@ py_binary( }), visibility = ["//visibility:public"], deps = [ - "//pkg/private:archive", "//pkg:make_rpm_lib", + "//pkg/private:archive", ], ) diff --git a/pkg/private/BUILD b/pkg/private/BUILD index 87ac3690..f782d16d 100644 --- a/pkg/private/BUILD +++ b/pkg/private/BUILD @@ -29,7 +29,10 @@ filegroup( "*.bzl", "*.py", ]), - visibility = ["//distro:__pkg__"], + visibility = [ + "//distro:__pkg__", + "//pkg:__pkg__", + ], ) exports_files( @@ -39,6 +42,7 @@ exports_files( visibility = [ "//distro:__pkg__", "//doc_build:__pkg__", + "//pkg:__pkg__", ], ) diff --git a/pkg/private/deb/BUILD b/pkg/private/deb/BUILD index 43e26be2..7c14cdba 100644 --- a/pkg/private/deb/BUILD +++ b/pkg/private/deb/BUILD @@ -28,7 +28,10 @@ filegroup( "*.bzl", "*.py", ]), - visibility = ["//distro:__pkg__"], + visibility = [ + "//distro:__pkg__", + "//pkg:__pkg__", + ], ) exports_files( @@ -38,6 +41,7 @@ exports_files( visibility = [ "//distro:__pkg__", "//doc_build:__pkg__", + "//pkg:__pkg__", ], ) diff --git a/pkg/private/make_starlark_library.bzl b/pkg/private/make_starlark_library.bzl new file mode 100644 index 00000000..cad4264c --- /dev/null +++ b/pkg/private/make_starlark_library.bzl @@ -0,0 +1,35 @@ +"""Turn a label_list of mixed sources and bzl_library's into a bzl_library. + +The sources can be anything. Only the ones that end in ".bzl" will be added. +""" + +load("@bazel_skylib//:bzl_library.bzl", "StarlarkLibraryInfo") + +def _make_starlark_library(ctx): + direct = [] + transitive = [] + for src in ctx.attr.srcs: + if StarlarkLibraryInfo in src: + transitive.append(src[StarlarkLibraryInfo]) + else: + for file in src[DefaultInfo].files.to_list(): + if file.path.endswith(".bzl"): + print(file.path) + direct.append(file) + all_files = depset(direct, transitive = transitive) + return [ + DefaultInfo(files = all_files, runfiles = ctx.runfiles(transitive_files = all_files)), + StarlarkLibraryInfo(srcs = direct, transitive_srcs = all_files), + ] + +starlark_library = rule( + implementation = _make_starlark_library, + attrs = { + "srcs": attr.label_list( + doc = "Any mix of source files. Only .bzl files will be used.", + allow_files = True, + cfg = "exec", + mandatory = True, + ), + }, +) diff --git a/pkg/private/tar/BUILD b/pkg/private/tar/BUILD index 0717eae2..fb9ab50a 100644 --- a/pkg/private/tar/BUILD +++ b/pkg/private/tar/BUILD @@ -28,7 +28,10 @@ filegroup( "*.bzl", "*.py", ]), - visibility = ["//distro:__pkg__"], + visibility = [ + "//distro:__pkg__", + "//pkg:__pkg__", + ], ) exports_files( diff --git a/pkg/private/zip/BUILD b/pkg/private/zip/BUILD index 0ff8d5cb..eba47091 100644 --- a/pkg/private/zip/BUILD +++ b/pkg/private/zip/BUILD @@ -28,7 +28,10 @@ filegroup( "*.bzl", "*.py", ]), - visibility = ["//distro:__pkg__"], + visibility = [ + "//distro:__pkg__", + "//pkg:__pkg__", + ], ) exports_files( @@ -38,6 +41,7 @@ exports_files( visibility = [ "//distro:__pkg__", "//doc_build:__pkg__", + "//pkg:__pkg__", ], ) diff --git a/pkg/releasing/BUILD b/pkg/releasing/BUILD index b2c3738a..2a38667a 100644 --- a/pkg/releasing/BUILD +++ b/pkg/releasing/BUILD @@ -24,7 +24,10 @@ filegroup( "*.bzl", "*.py", ]), - visibility = ["//distro:__pkg__"], + visibility = [ + "//distro:__pkg__", + "//pkg:__pkg__", + ], ) py_library( From 7a55a501363051f291265a17cc5e465bf0d6e19b Mon Sep 17 00:00:00 2001 From: Tony Aiuto Date: Wed, 6 Apr 2022 23:43:02 -0400 Subject: [PATCH 2/2] revie --- pkg/BUILD | 1 + pkg/private/make_starlark_library.bzl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/BUILD b/pkg/BUILD index bfe4ef61..8b834403 100644 --- a/pkg/BUILD +++ b/pkg/BUILD @@ -53,6 +53,7 @@ starlark_library( name = "bzl_srcs", srcs = [ ":standard_package", + "//pkg:pkg.bzl", "//pkg/private:standard_package", "//pkg/private/deb:standard_package", "//pkg/private/tar:standard_package", diff --git a/pkg/private/make_starlark_library.bzl b/pkg/private/make_starlark_library.bzl index cad4264c..c6b0dee9 100644 --- a/pkg/private/make_starlark_library.bzl +++ b/pkg/private/make_starlark_library.bzl @@ -14,7 +14,7 @@ def _make_starlark_library(ctx): else: for file in src[DefaultInfo].files.to_list(): if file.path.endswith(".bzl"): - print(file.path) + # print(file.path) direct.append(file) all_files = depset(direct, transitive = transitive) return [