Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed May 2, 2024
2 parents 6e4ec47 + bf4609b commit d18022b
Show file tree
Hide file tree
Showing 33 changed files with 573 additions and 95 deletions.
3 changes: 0 additions & 3 deletions .bazelci/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,4 @@ tasks:
integration:
name: rolling_distro
platform: ubuntu1804
bazel: rolling
build_flags:
- "--noenable_bzlmod"
<<: *common
6 changes: 3 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ module(

# Do not update to newer versions until you need a specific new feature.
bazel_dep(name = "rules_license", version = "0.0.4")
bazel_dep(name = "rules_python", version = "0.24.0")
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "bazel_skylib", version = "1.2.0")

# Only for development
bazel_dep(name = "platforms", version = "0.0.5", dev_dependency = True)
bazel_dep(name = "stardoc", version = "0.5.3", dev_dependency = True)
bazel_dep(name = "platforms", version = "0.0.9", dev_dependency = True)
bazel_dep(name = "rules_cc", version = "0.0.9", dev_dependency = True)
bazel_dep(name = "stardoc", version = "0.6.2", dev_dependency = True)

# Find the system rpmbuild if one is available.
find_rpm = use_extension("//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild_bzlmod", dev_dependency = True)
Expand Down
5 changes: 2 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "platforms",
sha256 = "3a561c99e7bdbe9173aa653fd579fe849f1d8d67395780ab4770b1f381431d51",
sha256 = "5eda539c841265031c2f82d8ae7a3a6490bd62176e0c038fc469eabf91f6149b",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.7/platforms-0.0.7.tar.gz",
"https://github.com/bazelbuild/platforms/releases/download/0.0.7/platforms-0.0.7.tar.gz",
"https://github.com/bazelbuild/platforms/releases/download/0.0.9/platforms-0.0.9.tar.gz",
],
)

Expand Down
2 changes: 1 addition & 1 deletion doc_build/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ How to:
"""

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_stardoc//stardoc:stardoc.bzl", "stardoc")
load("@stardoc//stardoc:stardoc.bzl", "stardoc")
load("//:version.bzl", "version")

package(default_applicable_licenses = ["//:license"])
Expand Down
7 changes: 4 additions & 3 deletions doc_build/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import typing


ID_RE = re.compile(r'<a id="#(.*)">')
ID_RE = re.compile(r'<a id="(.*)">')
WRAPS_RE = re.compile(r'@wraps\((.*)\)')
SINCE_RE = re.compile(r'@since\(([^)]*)\)')
CENTER_RE = re.compile(r'<p align="center">([^<]*)</p>')
Expand Down Expand Up @@ -78,9 +78,10 @@ def main(argv: typing.Sequence[str]) -> None:
for file in argv[1:]:
merge_file(file, sys.stdout, wrapper_map)
if wrapper_map:
print("We didn't use all the @wraps()", wrapper_map)
print("We didn't use all the @wraps()", wrapper_map, file=sys.stderr)
sys.exit(1)
return 0


if __name__ == '__main__':
main(sys.argv)
sys.exit(main(sys.argv))
Empty file.
55 changes: 55 additions & 0 deletions examples/rpm/debuginfo/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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:mappings.bzl", "pkg_files")
load("@rules_pkg//pkg:rpm.bzl", "pkg_rpm")

cc_binary(
name = "test",
copts = ["-g"],
srcs = [
"test.c",
],
)

pkg_files(
name = "rpm_files",
srcs = [
":test",
],
)

pkg_rpm(
name = "test-rpm",
srcs = [
":rpm_files",
],
release = "0",
version = "1",
license = "Some license",
summary = "Summary",
description = "Description",
debuginfo = True,
)

# 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/debuginfo/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",
)
17 changes: 17 additions & 0 deletions examples/rpm/debuginfo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Using system rpmbuild with bzlmod and generating debuginfo

## 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.

It configures the system toolchain to be aware of which debuginfo configuration
to use (defaults to "none", the example uses "centos7").

## To use

```
bazel build :*
rpm2cpio bazel-bin/test-rpm.rpm | cpio -ivt
cat bazel-bin/content.txt
```
3 changes: 3 additions & 0 deletions examples/rpm/debuginfo/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main() {
return 0;
}
Empty file.
Empty file.
44 changes: 34 additions & 10 deletions pkg/make_rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class RpmBuilder(object):

SOURCE_DIR = 'SOURCES'
BUILD_DIR = 'BUILD'
BUILD_SUBDIR = 'BUILD_SUB'
BUILDROOT_DIR = 'BUILDROOT'
TEMP_DIR = 'TMP'
RPMS_DIR = 'RPMS'
Expand Down Expand Up @@ -345,7 +346,7 @@ def SetupWorkdir(self,
shutil.copy(os.path.join(original_dir, file_list_path), RpmBuilder.BUILD_DIR)
self.file_list_path = os.path.join(RpmBuilder.BUILD_DIR, os.path.basename(file_list_path))

def CallRpmBuild(self, dirname, rpmbuild_args):
def CallRpmBuild(self, dirname, rpmbuild_args, debuginfo_type):
"""Call rpmbuild with the correct arguments."""

buildroot = os.path.join(dirname, RpmBuilder.BUILDROOT_DIR)
Expand All @@ -361,16 +362,31 @@ def CallRpmBuild(self, dirname, rpmbuild_args):
if self.debug:
args.append('-vv')

if debuginfo_type == "fedora40":
os.makedirs(f'{dirname}/{RpmBuilder.BUILD_DIR}/{RpmBuilder.BUILD_SUBDIR}')

# Common options
# NOTE: There may be a need to add '--define', 'buildsubdir .' for some
# rpmbuild versions. But that breaks other rpmbuild versions, so before
# adding it back in, add extensive tests.
args += [
'--define', '_topdir %s' % dirname,
'--define', '_tmppath %s/TMP' % dirname,
'--define', '_builddir %s/BUILD' % dirname,
'--bb',
'--buildroot=%s' % buildroot,
'--define', '_topdir %s' % dirname,
'--define', '_tmppath %s/TMP' % dirname,
'--define', '_builddir %s/BUILD' % dirname,
]

if debuginfo_type in ["fedora40", "centos7", "centos9"]:
args += ['--undefine', '_debugsource_packages']

if debuginfo_type in ["centos7", "centos9"]:
args += ['--define', 'buildsubdir .']

if debuginfo_type == "fedora40":
args += ['--define', f'buildsubdir {RpmBuilder.BUILD_SUBDIR}']

args += [
'--bb',
'--buildroot=%s' % buildroot,
] # yapf: disable

# Macro-based RPM parameter substitution, if necessary inputs provided.
Expand All @@ -382,7 +398,11 @@ def CallRpmBuild(self, dirname, rpmbuild_args):
args += ['--define', 'build_rpm_install %s' % self.install_script_file]
if self.file_list_path:
# %files -f is taken relative to the package root
args += ['--define', 'build_rpm_files %s' % os.path.basename(self.file_list_path)]
base_path = os.path.basename(self.file_list_path)
if debuginfo_type == "fedora40":
base_path = os.path.join("..", base_path)

args += ['--define', 'build_rpm_files %s' % base_path]

args.extend(rpmbuild_args)

Expand Down Expand Up @@ -459,7 +479,8 @@ def Build(self, spec_file, out_file, subrpm_out_files=None,
posttrans_scriptlet_path=None,
file_list_path=None,
changelog_file=None,
rpmbuild_args=None):
rpmbuild_args=None,
debuginfo_type=None):
"""Build the RPM described by the spec_file, with other metadata in keyword arguments"""

if self.debug:
Expand Down Expand Up @@ -490,7 +511,7 @@ def Build(self, spec_file, out_file, subrpm_out_files=None,
postun_scriptlet_path=postun_scriptlet_path,
posttrans_scriptlet_path=posttrans_scriptlet_path,
changelog_file=changelog_file)
status = self.CallRpmBuild(dirname, rpmbuild_args or [])
status = self.CallRpmBuild(dirname, rpmbuild_args or [], debuginfo_type)
self.SaveResult(out_file, subrpm_out_files)

return status
Expand Down Expand Up @@ -550,6 +571,8 @@ def main(argv):

parser.add_argument('--rpmbuild_arg', dest='rpmbuild_args', action='append',
help='Any additional arguments to pass to rpmbuild')
parser.add_argument('--debuginfo_type', dest='debuginfo_type', default='none',
help='debuginfo type to use (centos7, fedora40, or none)')
parser.add_argument('files', nargs='*')

options = parser.parse_args(argv or ())
Expand All @@ -574,7 +597,8 @@ def main(argv):
postun_scriptlet_path=options.postun_scriptlet,
posttrans_scriptlet_path=options.posttrans_scriptlet,
changelog_file=options.changelog,
rpmbuild_args=options.rpmbuild_args)
rpmbuild_args=options.rpmbuild_args,
debuginfo_type=options.debuginfo_type)
except NoRpmbuildFoundError:
print('ERROR: rpmbuild is required but is not present in PATH')
return 1
Expand Down
2 changes: 1 addition & 1 deletion pkg/mappings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def _pkg_files_impl(ctx):
target = file_to_target[src]
runfiles = target[DefaultInfo].default_runfiles
if runfiles:
base_path = src_dest_paths_map[src] + ".runfiles"
base_path = src_dest_paths_map[src] + ".runfiles/" + ctx.workspace_name
for rf in runfiles.files.to_list():
dest_path = paths.join(base_path, rf.short_path)

Expand Down
12 changes: 8 additions & 4 deletions pkg/private/pkg_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ _MappingContext = provider(
# Behaviors
"allow_duplicates_with_different_content": "bool: don't fail when you double mapped files",
"include_runfiles": "bool: include runfiles",
"workspace_name": "string: name of the main workspace",
"strip_prefix": "strip_prefix",

"path_mapper": "function to map destination paths",
Expand Down Expand Up @@ -119,6 +120,7 @@ def create_mapping_context_from_ctx(
allow_duplicates_with_different_content = allow_duplicates_with_different_content,
strip_prefix = strip_prefix,
include_runfiles = include_runfiles,
workspace_name = ctx.workspace_name,
default_mode = default_mode,
path_mapper = path_mapper or (lambda x: x),
# TODO(aiuto): allow these to be passed in as needed. But, before doing
Expand Down Expand Up @@ -370,15 +372,17 @@ def add_label_list(mapping_context, srcs):
src,
data_path,
data_path_without_prefix,
include_runfiles = mapping_context.include_runfiles,
mapping_context.include_runfiles,
mapping_context.workspace_name,
)

def add_from_default_info(
mapping_context,
src,
data_path,
data_path_without_prefix,
include_runfiles = False):
include_runfiles,
workspace_name):
"""Helper method to add the DefaultInfo of a target to a content_map.
Args:
Expand Down Expand Up @@ -428,15 +432,15 @@ def add_from_default_info(
# the first file of DefaultInfo.files should be the right target.
base_file = the_executable or all_files[0]
base_file_path = dest_path(base_file, data_path, data_path_without_prefix)
base_path = base_file_path + ".runfiles"
base_path = base_file_path + ".runfiles/" + workspace_name

for rf in runfiles.files.to_list():
d_path = mapping_context.path_mapper(base_path + "/" + rf.short_path)
fmode = "0755" if rf == the_executable else mapping_context.default_mode
_check_dest(mapping_context.content_map, d_path, rf, src.label, mapping_context.allow_duplicates_with_different_content)
mapping_context.content_map[d_path] = _DestFile(
src = rf,
entry_type = ENTRY_IS_FILE,
entry_type = ENTRY_IS_TREE if rf.is_directory else ENTRY_IS_FILE,
origin = src.label,
mode = fmode,
user = mapping_context.default_user,
Expand Down
2 changes: 0 additions & 2 deletions pkg/private/tar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ package(
default_applicable_licenses = ["//:license"],
)

licenses(["notice"])

filegroup(
name = "standard_package",
srcs = [
Expand Down
5 changes: 4 additions & 1 deletion pkg/private/tar/build_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ def add_link(self, symlink, destination, mode=None, ids=None, names=None):
names: (username, groupname) for the file to set ownership. An empty
file will be created as `destfile` in the layer.
"""
dest = self.normalize_path(symlink)
if not symlink.startswith("./"):
dest = self.normalize_path(symlink)
else:
dest = symlink
self.tarfile.add_file(
dest,
tarfile.SYMTYPE,
Expand Down
6 changes: 3 additions & 3 deletions pkg/rpm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ def pkg_rpm(name, srcs = None, spec_file = None, subrpms = None, **kwargs):
depending on mode
"""
if srcs and spec_file:
if srcs != None and spec_file:
fail("Cannot determine which pkg_rpm rule to use. `srcs` and `spec_file` are mutually exclusive")

if subrpms and spec_file:
fail("Cannot build sub RPMs with a specfile. `subrpms` and `spec_file` are mutually exclusive")

if not srcs and not spec_file:
if srcs == None and not spec_file:
fail("Either `srcs` or `spec_file` must be provided.")

if srcs:
if srcs != None:
pkg_rpm_pfg(
name = name,
srcs = srcs,
Expand Down
Loading

0 comments on commit d18022b

Please sign in to comment.