blob: 32395abcf0a9ac8749d22409453748f2e1344bb8 [file] [log] [blame]
Ivo Listc0809f72020-12-04 06:21:44 -08001# Copyright 2020 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Rules to create a release archive"""
16
17def release_archive(name, srcs = [], src_map = {}, package_dir = "-", deps = [], **kwargs):
18 """ Creates an zip of the srcs, and renamed label artifacts.
19
20 Usage:
21 //:BUILD
22 load("//src:release_archive.bzl", "release_archive")
23 release_archive(
24 name = "release_archive",
25 src_map = {
26 "BUILD.release.bazel.bazel": "BUILD.bazel",
27 "WORKSPACE.release.bazel": "WORKSPACE",
28 },
29 deps = [
30 "//dep:pkg"
31 ],
32 )
33 //dep:BUILD
34 load("//src:release_archive.bzl", "release_archive")
35 release_archive(
36 name = "pkg",
37 srcs = [
38 ":label_of_artifact",
39 ],
40 )
41 Args:
42 name: target identifier, points to a pkg_tar target.
43 package_dir: directory to place the srcs, src_map, and dist_files under. Defaults to the current directory.
44 src_map: dict of <label>:<name string> for labels to be renamed and included in the distribution.
45 srcs: files to include in the distribution.
46 deps: release_archives to be included.
47 **kwargs: other arguments added to the final genrule (for example visibility)
48 """
49 srcs = list(srcs)
50 for source, target in src_map.items():
51 rename_name = name + "_" + target
52 _rename(
53 name = rename_name,
54 source = source,
55 target = target,
56 )
57 srcs.append(rename_name)
58
59 if srcs != []:
60 native.genrule(
61 name = name + "_srcs",
62 srcs = srcs,
63 outs = [name + "_srcs.zip"],
64 cmd = "zip -qjX $@ $$(echo $(SRCS) | sort)",
65 visibility = ["//visibility:private"],
66 )
67 deps = [name + "_srcs.zip"] + deps
68
69 native.genrule(
70 name = name,
71 srcs = deps,
72 outs = [(name[:-len("_zip")] if name.endswith("_zip") else name) + ".zip"],
73 cmd = "$(location //src:merge_zip_files) %s $@ $(SRCS)" % package_dir,
74 output_to_bindir = 1,
75 tools = ["//src:merge_zip_files"],
76 **kwargs
77 )
78
79def _rename_impl(ctx):
80 out_file = ctx.actions.declare_file(ctx.label.name + "/" + ctx.attr.target)
81 in_file = ctx.file.source
82 ctx.actions.run_shell(
83 inputs = [in_file],
84 outputs = [out_file],
85 progress_message = "%s -> %s" % (in_file, ctx.attr.target),
86 command = "mkdir -p {dir} && cp {in_file} {out_file}".format(
87 dir = out_file.dirname,
88 in_file = in_file.path,
89 out_file = out_file.path,
90 ),
91 )
92 return [DefaultInfo(files = depset([out_file]))]
93
94_rename = rule(
95 implementation = _rename_impl,
96 attrs = {
97 "source": attr.label(allow_single_file = True, mandatory = True),
98 "target": attr.string(mandatory = True),
99 },
100)