aboutsummaryrefslogtreecommitdiff
path: root/toolchains/rpm
diff options
context:
space:
mode:
Diffstat (limited to 'toolchains/rpm')
-rw-r--r--toolchains/rpm/BUILD78
-rw-r--r--toolchains/rpm/BUILD.tpl14
-rw-r--r--toolchains/rpm/rpmbuild.bzl74
-rw-r--r--toolchains/rpm/rpmbuild_configure.bzl63
4 files changed, 229 insertions, 0 deletions
diff --git a/toolchains/rpm/BUILD b/toolchains/rpm/BUILD
new file mode 100644
index 0000000..58ec5db
--- /dev/null
+++ b/toolchains/rpm/BUILD
@@ -0,0 +1,78 @@
+# Copyright 2020 The Bazel Authors. All rights reserved.
+#
+# 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.
+
+"""toolchain to wrap an rpmbuild binary.
+
+Type: @rules_pkg//toolchains/rpm:rpmbuild_toolchain_type
+
+Toolchains:
+- rpmbuild_missing_toolchain: provides a fallback toolchain for exec platforms
+ where rpmbuild might not be available.
+
+- rpmbuild_auto_toolchain: a toolchain that uses the installed rpmbuild. See
+ rpmbuild_configure.bzl%find_system_rpmbuild for usage.
+"""
+
+load("//toolchains/rpm:rpmbuild.bzl", "is_rpmbuild_available", "rpmbuild_toolchain")
+
+package(default_applicable_licenses = ["//:license"])
+
+filegroup(
+ name = "standard_package",
+ srcs = glob([
+ "*",
+ ]),
+ visibility = ["//distro:__pkg__"],
+)
+
+exports_files(
+ glob(["*"]),
+ visibility = ["//visibility:public"],
+)
+
+# Expose the availability of an actual rpmbuild as a config_setting, so we can
+# select() on it.
+config_setting(
+ name = "have_rpmbuild",
+ flag_values = {
+ ":is_rpmbuild_available": "1",
+ },
+ visibility = ["//visibility:public"],
+)
+
+# Expose the availability of an actual rpmbuild as a feature flag, so we can
+# create a config_setting from it.
+is_rpmbuild_available(
+ name = "is_rpmbuild_available",
+ visibility = ["//:__subpackages__"],
+)
+
+toolchain_type(
+ name = "rpmbuild_toolchain_type",
+ visibility = ["//visibility:public"],
+)
+
+# rpmbuild_missing_toolchain provides a fallback toolchain so that toolchain
+# resolution can succeed even on platforms that do not have a working rpmbuild.
+# If this toolchain is selected, the constraint ":have_rpmbuild" will not be
+# satistifed.
+rpmbuild_toolchain(
+ name = "no_rpmbuild",
+)
+
+toolchain(
+ name = "rpmbuild_missing_toolchain",
+ toolchain = ":no_rpmbuild",
+ toolchain_type = ":rpmbuild_toolchain_type",
+)
diff --git a/toolchains/rpm/BUILD.tpl b/toolchains/rpm/BUILD.tpl
new file mode 100644
index 0000000..ca4b218
--- /dev/null
+++ b/toolchains/rpm/BUILD.tpl
@@ -0,0 +1,14 @@
+# This content is generated by {GENERATOR}
+load("@rules_pkg//toolchains/rpm:rpmbuild.bzl", "rpmbuild_toolchain")
+
+rpmbuild_toolchain(
+ name = "rpmbuild_auto",
+ path = "{RPMBUILD_PATH}",
+ version = "{RPMBUILD_VERSION}",
+)
+
+toolchain(
+ name = "rpmbuild_auto_toolchain",
+ toolchain = ":rpmbuild_auto",
+ toolchain_type = "@rules_pkg//toolchains/rpm:rpmbuild_toolchain_type",
+)
diff --git a/toolchains/rpm/rpmbuild.bzl b/toolchains/rpm/rpmbuild.bzl
new file mode 100644
index 0000000..3aa0ea0
--- /dev/null
+++ b/toolchains/rpm/rpmbuild.bzl
@@ -0,0 +1,74 @@
+# Copyright 2020 The Bazel Authors. All rights reserved.
+#
+# 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.
+"""toolchain to provide an rpmbuild binary."""
+
+RpmbuildInfo = provider(
+ doc = """Platform inde artifact.""",
+ fields = {
+ "name": "The name of the toolchain",
+ "valid": "Is this toolchain valid and usable?",
+ "label": "The path to a target I will build",
+ "path": "The path to a pre-built rpmbuild",
+ "version": "The version string of rpmbuild",
+ },
+)
+
+def _rpmbuild_toolchain_impl(ctx):
+ if ctx.attr.label and ctx.attr.path:
+ fail("rpmbuild_toolchain must not specify both label and path.")
+ valid = bool(ctx.attr.label) or bool(ctx.attr.path)
+ toolchain_info = platform_common.ToolchainInfo(
+ rpmbuild = RpmbuildInfo(
+ name = str(ctx.label),
+ valid = valid,
+ label = ctx.attr.label,
+ path = ctx.attr.path,
+ version = ctx.attr.version,
+ ),
+ )
+ return [toolchain_info]
+
+rpmbuild_toolchain = rule(
+ implementation = _rpmbuild_toolchain_impl,
+ attrs = {
+ "label": attr.label(
+ doc = "A valid label of a target to build or a prebuilt binary. Mutually exclusive with path.",
+ cfg = "exec",
+ executable = True,
+ allow_files = True,
+ ),
+ "path": attr.string(
+ doc = "The path to the rpmbuild executable. Mutually exclusive with label.",
+ ),
+ "version": attr.string(
+ doc = "The version string of the rpmbuild executable. This should be manually set.",
+ ),
+ },
+)
+
+# Expose the presence of an rpmbuild in the resolved toolchain as a flag.
+def _is_rpmbuild_available_impl(ctx):
+ toolchain = ctx.toolchains["@rules_pkg//toolchains/rpm:rpmbuild_toolchain_type"].rpmbuild
+ return [config_common.FeatureFlagInfo(
+ value = ("1" if toolchain.valid else "0"),
+ )]
+
+is_rpmbuild_available = rule(
+ implementation = _is_rpmbuild_available_impl,
+ attrs = {},
+ toolchains = ["@rules_pkg//toolchains/rpm:rpmbuild_toolchain_type"],
+)
+
+def rpmbuild_register_toolchains():
+ native.register_toolchains("@rules_pkg//toolchains/rpm:rpmbuild_missing_toolchain")
diff --git a/toolchains/rpm/rpmbuild_configure.bzl b/toolchains/rpm/rpmbuild_configure.bzl
new file mode 100644
index 0000000..f50a9ad
--- /dev/null
+++ b/toolchains/rpm/rpmbuild_configure.bzl
@@ -0,0 +1,63 @@
+# Copyright 2020 The Bazel Authors. All rights reserved.
+#
+# 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.
+"""Repository rule to autoconfigure a toolchain using the system rpmbuild."""
+
+def _write_build(rctx, path, version):
+ if not path:
+ path = ""
+ rctx.template(
+ "BUILD",
+ Label("//toolchains/rpm:BUILD.tpl"),
+ substitutions = {
+ "{GENERATOR}": "@rules_pkg//toolchains/rpm/rpmbuild_configure.bzl%find_system_rpmbuild",
+ "{RPMBUILD_PATH}": str(path),
+ "{RPMBUILD_VERSION}": version,
+ },
+ executable = False,
+ )
+
+def _find_system_rpmbuild_impl(rctx):
+ rpmbuild_path = rctx.which("rpmbuild")
+ if rctx.attr.verbose:
+ if rpmbuild_path:
+ print("Found rpmbuild at '%s'" % rpmbuild_path) # buildifier: disable=print
+ else:
+ print("No system rpmbuild found.") # buildifier: disable=print
+ version = "unknown"
+ if rpmbuild_path:
+ res = rctx.execute([rpmbuild_path, "--version"])
+ if res.return_code == 0:
+ # expect stdout like: RPM version 4.16.1.2
+ parts = res.stdout.strip().split(" ")
+ if parts[0] == "RPM" and parts[1] == "version":
+ version = parts[2]
+ _write_build(rctx = rctx, path = rpmbuild_path, version = version)
+
+_find_system_rpmbuild = repository_rule(
+ implementation = _find_system_rpmbuild_impl,
+ doc = """Create a repository that defines an rpmbuild toolchain based on the system rpmbuild.""",
+ local = True,
+ environ = ["PATH"],
+ attrs = {
+ "verbose": attr.bool(
+ doc = "If true, print status messages.",
+ ),
+ },
+)
+
+def find_system_rpmbuild(name, verbose=False):
+ _find_system_rpmbuild(name=name, verbose=verbose)
+ native.register_toolchains(
+ "@%s//:rpmbuild_auto_toolchain" % name,
+ "@rules_pkg//toolchains/rpm:rpmbuild_missing_toolchain")