Skip to content

Commit

Permalink
Provide more examples for pkg_rpm() (bazelbuild#823)
Browse files Browse the repository at this point in the history
* Move prebuilt_rpmbuild example into rpm subdirectory

This will help us be a bit more organized as we add a few more
RPM-based examples for rules_pkg.

* Fix WORKSPACE file to account for subdirectory move

One we moved this into a subdirectory it was broke because of how it
uses rules_pkg as a local repository.

* s/readme.md/README.md/

The latter seems to be more the norm, so renaming for consistency and
visibility.

* Add more detail to README.md for prebuilt_rpmbuild example

This adds a bit more detail the description of the `prebuilt_rpmbuild`
example so that someone can understand it a bit more.

* Add an example of using system rpmbuild for pkg_rpm()

This example is essentially the same as the prebuilt_rpmbuild example
except that it uses find_system_rpmbuild() to register the local
version of `rpmbuild` as the toolchain to use.

* Add an example with bzlmod using system rpmbuild

This is very similar to the non-bzlmod version except we've replaced
the WORKSPACE file with a MODULE.bazel file.

* Add an example that doesn't use a specfile for pkg_rpm()

This example uses pkg_rpm() to generate the full rpm in lieu of using
a standalone specfile.
  • Loading branch information
kellyma2 authored Feb 28, 2024
1 parent d7ff3e6 commit 5607259
Show file tree
Hide file tree
Showing 19 changed files with 343 additions and 15 deletions.
10 changes: 0 additions & 10 deletions examples/prebuilt_rpmbuild/readme.md

This file was deleted.

54 changes: 54 additions & 0 deletions examples/rpm/nospecfile/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2024 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.
# -*- coding: utf-8 -*-

load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("@rules_pkg//pkg:rpm.bzl", "pkg_rpm")

pkg_files(
name = "rpm_files",
srcs = [
"BUILD",
"MODULE.bazel",
"README.md",
],
)

pkg_rpm(
name = "test-rpm",
srcs = [
":rpm_files",
],
release = "0",
version = "1",
summary = "rules_pkg example RPM",
description = "This is a package description.",
license = "Apache License, v2.0",
architecture = "x86_64",
requires = [
"somerpm",
],
provides = [
"somefile",
],
)

# If you have rpmbuild, you probably have rpm2cpio too.
# Feature idea: Add rpm2cpio and cpio to the rpmbuild toolchain
genrule(
name = "inspect_content",
srcs = [":test-rpm"],
outs = ["content.txt"],
cmd = "rpm2cpio $(locations :test-rpm) | cpio -ivt >$@",
)
32 changes: 32 additions & 0 deletions examples/rpm/nospecfile/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2024 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.

module(name = "rules_pkg_example_rpm_system_rpmbuild_bzlmod")

bazel_dep(name = "rules_pkg")

local_path_override(
module_name = "rules_pkg",
path = "../../..",
)

find_rpmbuild = use_extension(
"@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl",
"find_system_rpmbuild_bzlmod",
)
use_repo(find_rpmbuild, "rules_pkg_rpmbuild")

register_toolchains(
"@rules_pkg_rpmbuild//:all",
)
14 changes: 14 additions & 0 deletions examples/rpm/nospecfile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Using system rpmbuild with bzlmod

## Summary

This example builds an RPM using the system `rpmbuild` from a pure bazel
`pkg_rpm()` definition instead of using a separate specfile.

## To use

```
bazel build :*
rpm2cpio bazel-bin/test-rpm.rpm | cpio -ivt
cat bazel-bin/content.txt
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pkg_rpm(
data = [
"BUILD",
"WORKSPACE",
"readme.md",
"README.md",
"test_rpm.spec",
],
release = "0",
Expand Down
18 changes: 18 additions & 0 deletions examples/rpm/prebuilt_rpmbuild/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Using a prebuilt rpmbuild instead of the system one.

## Summary

This example defines a rpmbuild toolchain in `local` that can be used
by rules_pkg. This must be copied into place as `local/rpmbuild_binary`
for use by `register_toolchains()`.

The RPM itself is based on a user provided spec file.

## To use

```
cp /usr/bin/rpmbuild local/rpmbuild_binary
bazel build :*
rpm2cpio bazel-bin/test-rpm.rpm | cpio -ivt
cat bazel-bin/content.txt
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(name = "rules_pkg_example_prebuilt_rpmbuild")
workspace(name = "rules_pkg_example_rpm_prebuilt_rpmbuild")

local_repository(
name = "rules_pkg",
path = "../..",
path = "../../..",
)

load("@rules_pkg//pkg:deps.bzl", "rules_pkg_dependencies")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ This is a package description.
%build

%install
cp WORKSPACE BUILD readme.md test_rpm.spec %{buildroot}/
cp WORKSPACE BUILD README.md test_rpm.spec %{buildroot}/

%files
/WORKSPACE
/BUILD
/readme.md
/README.md
/test_rpm.spec
38 changes: 38 additions & 0 deletions examples/rpm/system_rpmbuild/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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.
# -*- coding: utf-8 -*-

load("@rules_pkg//pkg:rpm.bzl", "pkg_rpm")

pkg_rpm(
name = "test-rpm",
data = [
"BUILD",
"WORKSPACE",
"README.md",
"test_rpm.spec",
],
release = "0",
spec_file = "test_rpm.spec",
version = "1",
)

# If you have rpmbuild, you probably have rpm2cpio too.
# Feature idea: Add rpm2cpio and cpio to the rpmbuild toolchain
genrule(
name = "inspect_content",
srcs = [":test-rpm"],
outs = ["content.txt"],
cmd = "rpm2cpio $(locations :test-rpm) | cpio -ivt >$@",
)
17 changes: 17 additions & 0 deletions examples/rpm/system_rpmbuild/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Using system rpmbuild

## Summary

This example uses the `find_system_rpmbuild()` macro built into `rules_pkg`
to search for `rpmbuild` in on the local system and use that to drive the
packaging process.

The RPM itself is based on a user provided spec file.

## To use

```
bazel build :*
rpm2cpio bazel-bin/test-rpm.rpm | cpio -ivt
cat bazel-bin/content.txt
```
31 changes: 31 additions & 0 deletions examples/rpm/system_rpmbuild/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2024 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.

workspace(name = "rules_pkg_example_rpm_system_rpmbuild")

local_repository(
name = "rules_pkg",
path = "../../..",
)

load("@rules_pkg//pkg:deps.bzl", "rules_pkg_dependencies")

rules_pkg_dependencies()

load(
"@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl",
"find_system_rpmbuild",
)

find_system_rpmbuild(name="my_rpmbuild")
24 changes: 24 additions & 0 deletions examples/rpm/system_rpmbuild/test_rpm.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Name: example
Version: 0
Release: 1
Summary: Example .spec file
License: Apache License, v2.0

# Do not try to use magic to determine file types
%define __spec_install_post %{nil}
# Do not die because we give it more input files than are in the files section
%define _unpackaged_files_terminate_build 0

%description
This is a package description.

%build

%install
cp WORKSPACE BUILD README.md test_rpm.spec %{buildroot}/

%files
/WORKSPACE
/BUILD
/README.md
/test_rpm.spec
38 changes: 38 additions & 0 deletions examples/rpm/system_rpmbuild_bzlmod/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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.
# -*- coding: utf-8 -*-

load("@rules_pkg//pkg:rpm.bzl", "pkg_rpm")

pkg_rpm(
name = "test-rpm",
data = [
"BUILD",
"MODULE.bazel",
"README.md",
"test_rpm.spec",
],
release = "0",
spec_file = "test_rpm.spec",
version = "1",
)

# If you have rpmbuild, you probably have rpm2cpio too.
# Feature idea: Add rpm2cpio and cpio to the rpmbuild toolchain
genrule(
name = "inspect_content",
srcs = [":test-rpm"],
outs = ["content.txt"],
cmd = "rpm2cpio $(locations :test-rpm) | cpio -ivt >$@",
)
32 changes: 32 additions & 0 deletions examples/rpm/system_rpmbuild_bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2024 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.

module(name = "rules_pkg_example_rpm_system_rpmbuild_bzlmod")

bazel_dep(name = "rules_pkg")

local_path_override(
module_name = "rules_pkg",
path = "../../..",
)

find_rpmbuild = use_extension(
"@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl",
"find_system_rpmbuild_bzlmod",
)
use_repo(find_rpmbuild, "rules_pkg_rpmbuild")

register_toolchains(
"@rules_pkg_rpmbuild//:all",
)
16 changes: 16 additions & 0 deletions examples/rpm/system_rpmbuild_bzlmod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Using system rpmbuild with bzlmod

## Summary

This example uses the `find_system_rpmbuild_bzlmod` module extension to help
us register the system rpmbuild as a toolchain in a bzlmod environment.

The RPM itself is based on a user provided spec file.

## To use

```
bazel build :*
rpm2cpio bazel-bin/test-rpm.rpm | cpio -ivt
cat bazel-bin/content.txt
```
24 changes: 24 additions & 0 deletions examples/rpm/system_rpmbuild_bzlmod/test_rpm.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Name: example
Version: 0
Release: 1
Summary: Example .spec file
License: Apache License, v2.0

# Do not try to use magic to determine file types
%define __spec_install_post %{nil}
# Do not die because we give it more input files than are in the files section
%define _unpackaged_files_terminate_build 0

%description
This is a package description.

%build

%install
cp MODULE.bazel BUILD README.md test_rpm.spec %{buildroot}/

%files
/MODULE.bazel
/BUILD
/README.md
/test_rpm.spec

0 comments on commit 5607259

Please sign in to comment.