Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions bazel/rules/dd_packaging/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "dd_packaging_info_bzl",
srcs = ["dd_packaging_info.bzl"],
visibility = ["//visibility:public"],
)

bzl_library(
name = "dd_collect_dependencies_bzl",
srcs = ["dd_collect_dependencies.bzl"],
visibility = ["//visibility:public"],
deps = [
":dd_packaging_info_bzl",
"@rules_pkg//pkg:providers.bzl",
],
)

bzl_library(
name = "dd_cc_packaged_bzl",
srcs = ["dd_cc_packaged.bzl"],
visibility = ["//visibility:public"],
deps = [
":dd_packaging_info_bzl",
"//bazel/rules:so_symlink_bzl",
"//bazel/rules/rewrite_rpath:rewrite_rpath_bzl",
"@rules_pkg//pkg:mappings.bzl",
"@rules_pkg//pkg:providers.bzl",
],
)
93 changes: 93 additions & 0 deletions bazel/rules/dd_packaging/dd_cc_packaged.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""dd_cc_packaged — packaging-aware wrapper around cc_shared_library or cc_binary."""

load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("@rules_cc//cc/common:cc_shared_library_info.bzl", "CcSharedLibraryInfo")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo")
load("//bazel/rules:so_symlink.bzl", "so_symlink")
load("//bazel/rules/dd_packaging:dd_packaging_info.bzl", "DdPackagingInfo")
load("//bazel/rules/rewrite_rpath:rewrite_rpath.bzl", "rewrite_rpath")

def _dd_cc_packaged_rule_impl(ctx):
installed = []
for dep in ctx.attr.installed_files:
if PackageFilegroupInfo in dep:
installed.append(dep[PackageFilegroupInfo])
elif PackageFilesInfo in dep:
installed.append(PackageFilegroupInfo(
pkg_files = [(dep[PackageFilesInfo], dep.label)],
pkg_dirs = [],
pkg_symlinks = [],
))
providers = [
DdPackagingInfo(installed_files = installed),
DefaultInfo(files = depset([ctx.file.patched])),
]
if CcSharedLibraryInfo in ctx.attr.input:
providers.append(ctx.attr.input[CcSharedLibraryInfo])
return providers

_dd_cc_packaged_rule = rule(
implementation = _dd_cc_packaged_rule_impl,
attrs = {
"input": attr.label(
mandatory = True,
providers = [[CcInfo], [CcSharedLibraryInfo]],
),
"patched": attr.label(
mandatory = True,
allow_single_file = True,
),
"installed_files": attr.label_list(providers = [[PackageFilesInfo], [PackageFilegroupInfo]]),
},
)

def _dd_cc_packaged_impl(name, input, version = "", installed_files = [], visibility = None, **kwargs):
patched_name = "{}_patched".format(name)
rewrite_rpath(
name = patched_name,
inputs = [input],
)
rule_installed_files = list(installed_files)
packaged_lib = "{}_packaged".format(name)
if version:
so_symlink(
name = packaged_lib,
src = ":{}".format(patched_name),
libname = "lib" + input.name,
version = version,
visibility = visibility,
)
else:
pkg_files(
name = packaged_lib,
srcs = [":{}".format(patched_name)],
prefix = "lib",
visibility = visibility,
)
rule_installed_files.append(":{}".format(packaged_lib))
_dd_cc_packaged_rule(
name = name,
input = input,
patched = ":{}".format(patched_name),
installed_files = rule_installed_files,
visibility = visibility,
**kwargs
)

dd_cc_packaged = macro(
attrs = {
"input": attr.label(
mandatory = True,
configurable = False,
),
"version": attr.string(
default = "",
configurable = False,
),
"installed_files": attr.label_list(
configurable = False,
),
},
implementation = _dd_cc_packaged_impl,
)
84 changes: 84 additions & 0 deletions bazel/rules/dd_packaging/dd_collect_dependencies.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""dd_collect_dependencies — collect transitive DdPackagingInfo for installation."""

load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo")
load("//bazel/rules/dd_packaging:dd_packaging_info.bzl", "DdPackagingInfo")

_CollectedPackagingInfo = provider(
doc = "Internal provider used by _collect_dd_packaging_aspect to accumulate PackageFilegroupInfo instances.",
fields = {
# Flat list of PackageFilegroupInfo gathered from this node and all
# nodes reachable through dynamic_deps / input edges.
"pkg_filegroups": "list of PackageFilegroupInfo accumulated transitively",
},
)

def _get_deps(ctx, attr_names):
deps = []
for attr_name in attr_names:
val = getattr(ctx.rule.attr, attr_name, None)
if val == None:
continue
if type(val) == "list":
deps.extend(val)
else:
deps.append(val)
return deps

def _collect_dd_packaging_aspect_impl(target, ctx):
pkg_filegroups = []

if DdPackagingInfo in target:
pkg_filegroups.extend(target[DdPackagingInfo].installed_files)

for dep in _get_deps(ctx, ["dynamic_deps", "input"]):
if _CollectedPackagingInfo in dep:
pkg_filegroups.extend(dep[_CollectedPackagingInfo].pkg_filegroups)

return [_CollectedPackagingInfo(pkg_filegroups = pkg_filegroups)]

_collect_dd_packaging_aspect = aspect(
implementation = _collect_dd_packaging_aspect_impl,
doc = """
Traverses two edge types to walk the full CC dependency graph:
- dynamic_deps: cc_shared_library -> cc_shared_library edges
- input: _dd_cc_packaged_rule -> cc_shared_library edges (bridges a
packaged target back to its underlying cc_shared_library)
""",
attr_aspects = ["dynamic_deps", "input"],
)

def _dd_collect_dependencies_impl(ctx):
pkg_files = []
pkg_dirs = []
pkg_symlinks = []

for src in ctx.attr.srcs:
if _CollectedPackagingInfo in src:
for fg in src[_CollectedPackagingInfo].pkg_filegroups:
pkg_files.extend(fg.pkg_files)
pkg_dirs.extend(fg.pkg_dirs)
pkg_symlinks.extend(fg.pkg_symlinks)

all_files = depset([
f
for pkg_files_info, _ in pkg_files
for f in pkg_files_info.dest_src_map.values()
])

return [
PackageFilegroupInfo(
pkg_files = pkg_files,
pkg_dirs = pkg_dirs,
pkg_symlinks = pkg_symlinks,
),
DefaultInfo(files = all_files),
]

dd_collect_dependencies = rule(
implementation = _dd_collect_dependencies_impl,
attrs = {
"srcs": attr.label_list(
aspects = [_collect_dd_packaging_aspect],
),
},
)
7 changes: 7 additions & 0 deletions bazel/rules/dd_packaging/dd_packaging_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""DdPackagingInfo — common provider for the DD packaging decorator chain."""

DdPackagingInfo = provider(
fields = {
"installed_files": "list of PackageFilesInfo or PackageFilegroupInfo",
},
)
7 changes: 7 additions & 0 deletions bazel/rules/rewrite_rpath/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

package(default_visibility = ["//visibility:public"])

exports_files(["macos.sh"])

bzl_library(
name = "rewrite_rpath_bzl",
srcs = ["rewrite_rpath.bzl"],
)
4 changes: 3 additions & 1 deletion bazel/rules/so_symlink.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _gen_targets(base_name, src, libname, version, prefix, spec, attributes):
pkg_filegroup(name = name, srcs = targets, target_compatible_with = [platform])
return platform, ":{}".format(name)

def so_symlink(name, src, libname = None, version = None, prefix = "", attributes = None):
def so_symlink(name, src, libname = None, version = None, prefix = "", attributes = None, visibility = None):
"""Creates shared library symlink chain following Unix conventions.

Unix (Linux/macOS): Generates the common multilevel symlink hierarchy for shared libraries:
Expand All @@ -71,8 +71,10 @@ def so_symlink(name, src, libname = None, version = None, prefix = "", attribute
prefix: Installation directory prefix (default: "")
version: Full version string (e.g., "3.0", ignored on Windows)
attributes: pkg_attributes
visibility: Bazel visibility for the generated alias target
"""
native.alias(
name = name,
actual = select(dict([_gen_targets(name, src, libname, version, prefix, spec, attributes) for spec in _SPECS])),
visibility = visibility,
)
2 changes: 1 addition & 1 deletion deps/cpython.BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ configure_make(
"@bzip2//:bz2",
"@sqlite3//:sqlite3",
"@xz//:lzma",
"@zlib//:z",
"@zlib//:z_so",
],
includes = ["python{}".format(VERSION_STR)],
targets = [
Expand Down
4 changes: 2 additions & 2 deletions deps/curl/overlay/overlay.BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ cc_binary(
":curl",
"@nghttp2//:nghttp2_shared",
"@openssl//:openssl_shared",
"@zlib//:z",
"@zlib//:z_so",
],
includes = [
".",
Expand Down Expand Up @@ -222,7 +222,7 @@ cc_shared_library(
dynamic_deps = [
"@nghttp2//:nghttp2_shared",
"@openssl//:openssl_shared",
"@zlib//:z",
"@zlib//:z_so",
],
visibility = ["//visibility:public"],
deps = [
Expand Down
2 changes: 1 addition & 1 deletion deps/libxml2/overlay.BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ cc_shared_library(
deps = [":libxml2"],
visibility = ["//visibility:public"],
dynamic_deps = [
"@zlib//:z",
"@zlib//:z_so",
"@xz//:lzma",
],
)
Expand Down
1 change: 0 additions & 1 deletion deps/openscap/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pkg_filegroup(
"@libyaml//:all_files",
"@pcre2//:all_files",
"@popt//:all_files",
"@rpm//:all_files",
"@util-linux//:all_files",
"@xmlsec//:all_files",
],
Expand Down
23 changes: 15 additions & 8 deletions deps/openscap/openscap.BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

load("@@//bazel/rules:dd_agent_expand_template.bzl", "dd_agent_expand_template")
load("@@//bazel/rules:so_symlink.bzl", "so_symlink")
load("@@//bazel/rules/dd_packaging:dd_collect_dependencies.bzl", "dd_collect_dependencies")
load("@@//compliance/rules:purl.bzl", "purl_for_generic")
load("@@//compliance/rules:ship_source_offer.bzl", "ship_source_offer")
load("@package_metadata//rules:package_metadata.bzl", "package_metadata")
Expand Down Expand Up @@ -503,8 +504,8 @@ cc_shared_library(
"@libyaml//:yaml",
"@pcre2//:pcre2-8",
"@popt//:popt",
"@rpm//:rpm",
"@rpm//:rpmio",
"@rpm//:rpm_so",
"@rpm//:rpmio_so",
"@util-linux//:blkid",
"@libxml2//:xml2",
"@xmlsec//:xmlsec1",
Expand Down Expand Up @@ -550,8 +551,8 @@ cc_binary(
"@libyaml//:yaml",
"@pcre2//:pcre2-8",
"@popt//:popt",
"@rpm//:rpm",
"@rpm//:rpmio",
"@rpm//:rpm_so",
"@rpm//:rpmio_so",
"@util-linux//:blkid",
"@libxml2//:xml2",
"@xmlsec//:xmlsec1",
Expand All @@ -560,7 +561,7 @@ cc_binary(
"@sqlite3//:sqlite3",
"@openssl//:openssl_shared",
"@xz//:lzma",
"@zlib//:z",
"@zlib//:z_so",
"@nghttp2//:nghttp2_shared",
],
includes = COMMON_INCLUDE_DIRS + PUBLIC_HEADERS_DIRS,
Expand Down Expand Up @@ -595,8 +596,8 @@ cc_binary(
"@libyaml//:yaml",
"@pcre2//:pcre2-8",
"@popt//:popt",
"@rpm//:rpm",
"@rpm//:rpmio",
"@rpm//:rpm_so",
"@rpm//:rpmio_so",
"@util-linux//:blkid",
"@libxml2//:xml2",
"@xmlsec//:xmlsec1",
Expand All @@ -605,7 +606,7 @@ cc_binary(
"@sqlite3//:sqlite3",
"@openssl//:openssl_shared",
"@xz//:lzma",
"@zlib//:z",
"@zlib//:z_so",
"@nghttp2//:nghttp2_shared",
],
includes = COMMON_INCLUDE_DIRS + PUBLIC_HEADERS_DIRS,
Expand Down Expand Up @@ -658,13 +659,19 @@ pkg_files(
attributes = pkg_attributes("0644"),
)

dd_collect_dependencies(
name = "collected_files",
srcs = [":oscap"],
)

pkg_filegroup(
name = "all_files",
srcs = [
":bin_files",
":lib_files",
":xccdf-resources",
":cpe_files",
":collected_files",
],
prefix = "embedded",
)
Expand Down
Loading
Loading