Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide capability to rename archives based on configuration values #198

Merged
merged 30 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a0b9b27
PackageNamingInfo PoC: start
aiuto Jun 27, 2020
0f89ade
WTF
aiuto Jun 28, 2020
6216efd
Merge branch 'master' into naming
aiuto Jul 3, 2020
b54d187
Merge branch 'master' into naming
aiuto Jul 9, 2020
ce1f647
Merge branch 'main' into naming
aiuto Jul 15, 2020
ec1ae22
Merge branch 'main' into naming
aiuto Jul 15, 2020
2171ca6
Merge branch 'main' into naming
aiuto Jul 31, 2020
41865c9
Merge branch 'main' into naming
aiuto Aug 7, 2020
11106ad
Merge branch 'main' into naming
aiuto Aug 7, 2020
02f7d96
Merge branch 'main' into naming
aiuto Aug 7, 2020
1c93629
Merge branch 'main' into naming
aiuto Aug 11, 2020
e6996d3
Merge branch 'main' into naming
aiuto Aug 27, 2020
15fb7a0
Add attribute strip_prefix to pkg_zip (#221)
davschne Aug 27, 2020
d528b79
Merge branch 'main' into naming
aiuto Sep 10, 2020
57fa497
Merge branch 'main' into naming
aiuto Sep 24, 2020
d01c5cc
always write output
aiuto Sep 24, 2020
861c72f
checkpoing
aiuto Sep 24, 2020
0a5f868
Merge remote-tracking branch 'upstream/main'
aiuto Sep 24, 2020
b01761a
Merge branch 'master' into naming
aiuto Sep 24, 2020
6d6dd7d
Merge remote-tracking branch 'upstream/main'
aiuto Sep 24, 2020
d95ecac
Merge branch 'master' into naming
aiuto Sep 24, 2020
4d1a22a
buff for review
aiuto Sep 24, 2020
3e48005
docs
aiuto Sep 24, 2020
eb0d591
only require package_variables if there is a substitution
aiuto Sep 24, 2020
d0376c2
put the renamed pkg_tar file in the tar test
aiuto Sep 25, 2020
5394c05
remove pointless comments
aiuto Sep 25, 2020
0712ea9
fix path naming in ArtifactInfo
aiuto Sep 25, 2020
7e02855
Merge branch 'main' into naming
aiuto Sep 25, 2020
4d9efcb
review fixes
aiuto Sep 28, 2020
ac2e47c
review fixes
aiuto Sep 28, 2020
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
2 changes: 2 additions & 0 deletions pkg/distro/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ genrule(
bzl_library(
name = "rules_pkg_lib",
srcs = [
"//:package_variables.bzl",
"//:path.bzl",
"//:pkg.bzl",
"//:providers.bzl",
"//:rpm.bzl",
"//:version.bzl",
],
Expand Down
25 changes: 25 additions & 0 deletions pkg/package_variables.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.

"""Utility methods to populate PackageVariablesInfo instances."""

load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
aiuto marked this conversation as resolved.
Show resolved Hide resolved

def add_cpp_variables(ctx, values):
cc_toolchain = find_cpp_toolchain(ctx)
# TODO(aiuto): Expand this to include target OS. Maybe also compilation
# mode, ABI and libc version, since they are sometimes used in package file
# names.
values['cpu'] = cc_toolchain.cpu
nacl marked this conversation as resolved.
Show resolved Hide resolved
return values
48 changes: 40 additions & 8 deletions pkg/pkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
"""Rules for manipulation of various packaging."""

load(":path.bzl", "compute_data_path", "dest_path")
load(":providers.bzl", "PackageArtifactInfo", "PackageVariablesInfo")

# Filetype to restrict inputs
tar_filetype = [".tar", ".tar.gz", ".tgz", ".tar.xz", ".tar.bz2"]
deb_filetype = [".deb", ".udeb"]
_DEFAULT_MTIME = -1


def _remap(remap_paths, path):
"""If path starts with a key in remap_paths, rewrite it."""
for prefix, replacement in remap_paths.items():
Expand All @@ -31,15 +33,32 @@ def _quote(filename, protect = "="):
"""Quote the filename, by escaping = by \\= and \\ by \\\\"""
return filename.replace("\\", "\\\\").replace(protect, "\\" + protect)


def _pkg_tar_impl(ctx):
"""Implementation of the pkg_tar rule."""

# Files needed by rule implementation at runtime
files = []

if ctx.attr.package_file_name:
output_name = ctx.attr.package_file_name
package_variables = ctx.attr.package_variables[PackageVariablesInfo]
if package_variables:
output_name = output_name.format(**package_variables.values)
nacl marked this conversation as resolved.
Show resolved Hide resolved
elif ctx.attr.package_file_name.find('{') >= 0:
fail('attribute package_file_name requires package_variables')
nacl marked this conversation as resolved.
Show resolved Hide resolved
output_file = ctx.actions.declare_file(output_name)
ctx.actions.symlink(
output = ctx.outputs.out,
target_file = output_file
)
nacl marked this conversation as resolved.
Show resolved Hide resolved
else:
output_file = ctx.outputs.out
output_name = ctx.outputs.out.basename

# Compute the relative path
data_path = compute_data_path(ctx.outputs.out, ctx.attr.strip_prefix)
data_path_without_prefix = compute_data_path(ctx.outputs.out, '.')
data_path = compute_data_path(output_file, ctx.attr.strip_prefix)
data_path_without_prefix = compute_data_path(output_file, '.')

# Find a list of path remappings to apply.
remap_paths = ctx.attr.remap_paths
Expand All @@ -55,7 +74,7 @@ def _pkg_tar_impl(ctx):

# Start building the arguments.
args = [
"--output=" + ctx.outputs.out.path,
"--output=" + output_file.path,
package_dir_arg,
"--mode=" + ctx.attr.mode,
"--owner=" + ctx.attr.owner,
Expand Down Expand Up @@ -130,10 +149,11 @@ def _pkg_tar_impl(ctx):

ctx.actions.run(
mnemonic = "PackageTar",
progress_message = "Writing: %s" % output_file.path,
inputs = file_inputs + ctx.files.deps + files,
executable = ctx.executable.build_tar,
arguments = ["@" + arg_file.path],
outputs = [ctx.outputs.out],
outputs = [output_file],
env = {
"LANG": "en_US.UTF-8",
"LC_CTYPE": "UTF-8",
Expand All @@ -142,7 +162,16 @@ def _pkg_tar_impl(ctx):
},
use_default_shell_env = True,
)
return OutputGroupInfo(out = [ctx.outputs.out])
return [
DefaultInfo(
files = depset([output_file]),
runfiles = ctx.runfiles(files = [output_file]),
),
PackageArtifactInfo(
label = ctx.label.name,
file_name = output_name,
)
nacl marked this conversation as resolved.
Show resolved Hide resolved
]

def _pkg_deb_impl(ctx):
"""The implementation for the pkg_deb rule."""
Expand Down Expand Up @@ -287,6 +316,10 @@ pkg_tar_impl = rule(
"include_runfiles": attr.bool(),
"empty_dirs": attr.string_list(),
"remap_paths": attr.string_dict(),
"package_file_name": attr.string(),
"package_variables": attr.label(
providers = [PackageVariablesInfo],
),

# Outputs
"out": attr.output(),
Expand Down Expand Up @@ -316,11 +349,10 @@ def pkg_tar(name, **kwargs):
kwargs["srcs"] = kwargs.pop("files")
archive_name = kwargs.get("archive_name") or name
extension = kwargs.get("extension") or "tar"

pkg_tar_impl(
name = name,
out = archive_name + "." + extension,
**kwargs
out = kwargs.get("out") or (archive_name + "." + extension),
**kwargs,
)

# A rule for creating a deb file, see README.md
Expand Down
31 changes: 31 additions & 0 deletions pkg/providers.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.

"""Packaging related providers."""


PackageArtifactInfo = provider(
doc = """Metadata about a package artifact.""",
fields = {
"label": "Label which produced it",
"file_name": "The file name of the artifact.",
},
)

PackageVariablesInfo = provider(
doc = """Variables which may be substituted into package names and content.""",
fields = {
"values": "Dict of name/value pairs",
},
)
32 changes: 31 additions & 1 deletion pkg/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ load("//:rpm.bzl", "pkg_rpm")
load("@rules_python//python:defs.bzl", "py_test")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")

load(":my_package_name.bzl", "my_package_naming")

# Exposes the value of the compilation mode to the package naming.
config_setting(
name = "debug_build",
values = {"compilation_mode": "dbg"},
)

my_package_naming(
name = "my_package_variables",
label = "-alpha",
compilation_mode = select({
":debug_build": "-debug",
"//conditions:default": "",
}),
)

genrule(
name = "generate_files",
outs = [
Expand All @@ -22,6 +39,15 @@ copy_file(
out = "zipcontent/loremipsum.txt",
)

pkg_tar(
name = "test_tar_naming",
srcs = [
":BUILD",
],
package_file_name = "test_naming-{cpu}{compilation_mode}{label}.tar",
package_variables = ":my_package_variables",
)

pkg_tar(
name = "test_tar_package_dir",
srcs = [
Expand Down Expand Up @@ -272,6 +298,7 @@ pkg_rpm(
":usr/titi",
],
extension = "tar%s" % ext,
# extension = ext[1:],
mode = "0644",
modes = {"usr/titi": "0755"},
owner = "42.24",
Expand All @@ -291,7 +318,10 @@ pkg_rpm(
[pkg_tar(
name = "test-tar-inclusion-%s" % ext,
build_tar = "//:build_tar",
deps = [":test-tar-basic-%s" % ext],
deps = [
":test-tar-basic-%s" % ext,
":test_tar_naming",
],
) for ext in [
"",
"gz",
Expand Down
42 changes: 42 additions & 0 deletions pkg/tests/my_package_name.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.

"""Sample rule to show package naming."""

load("//:providers.bzl", "PackageVariablesInfo")
load("//:package_variables.bzl", "add_cpp_variables")

def _my_package_naming_impl(ctx):
values = {}
# get things from the platform and toolchain
add_cpp_variables(ctx, values)
# then add in my own custom values
values['label'] = ctx.attr.label
values['compilation_mode'] = ctx.attr.compilation_mode
return PackageVariablesInfo(values = values)

my_package_naming = rule(
implementation = _my_package_naming_impl,
# must ask for cpp fragement to use add_cpp_variables().
fragments = ["cpp"],
attrs = {
"label": attr.string(doc = "A label that matters to me."),
"compilation_mode": attr.string(
doc = "Another label for the sake of the sample."
),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")
),
}
)
1 change: 1 addition & 0 deletions pkg/tests/pkg_tar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def test_file_inclusion(self):
{'name': './usr/titi', 'mode': 0o755, 'uid': 42, 'gid': 24},
{'name': './usr/bin'},
{'name': './usr/bin/java', 'linkname': '/path/to/bin/java'},
{'name': './BUILD'},
]
for ext in ('', '.gz', '.bz2', '.xz'):
with self.subTest(ext=ext):
Expand Down