aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWeijia He <hwj@google.com>2023-10-03 21:13:10 +0000
committerWeijia He <hwj@google.com>2023-10-11 18:46:23 +0000
commitca30f52024dc1ffc23818e2cb733108543b1a654 (patch)
treec2b02d19f53dda40b0fb0828eccefa5752ae91f0
parentc00b963bd9311e1877da090fb230cfe3939b156b (diff)
downloadbazel_common_rules-ca30f52024dc1ffc23818e2cb733108543b1a654.tar.gz
Add a zip bazel rule, to be used to generate arbitrary zip file.
Bug: 291147200 Change-Id: I43fc24599f69f110757874aaf6b2d2964b5b6904
-rw-r--r--zip/BUILD13
-rw-r--r--zip/zip.bzl53
2 files changed, 66 insertions, 0 deletions
diff --git a/zip/BUILD b/zip/BUILD
new file mode 100644
index 0000000..3b1f000
--- /dev/null
+++ b/zip/BUILD
@@ -0,0 +1,13 @@
+# Copyright (C) 2023 The Android Open Source Project
+#
+# 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.
diff --git a/zip/zip.bzl b/zip/zip.bzl
new file mode 100644
index 0000000..3dee689
--- /dev/null
+++ b/zip/zip.bzl
@@ -0,0 +1,53 @@
+# Copyright (C) 2023 The Android Open Source Project
+#
+# 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.
+
+"""Creates a zip archive from the specified targets."""
+
+def _zip_archive_impl(ctx):
+ out_filename = ctx.attr.out if ctx.attr.out else ctx.attr.name + ".zip"
+ out = ctx.actions.declare_file(out_filename)
+ srcs_depset = depset(transitive = [target.files for target in ctx.attr.srcs])
+
+ args = ctx.actions.args()
+ args.add("-symlinks=false")
+ args.add("-o", out)
+ args.add_all(srcs_depset, before_each = "-f")
+
+ ctx.actions.run(
+ inputs = srcs_depset,
+ outputs = [out],
+ executable = ctx.executable._soong_zip,
+ arguments = [args],
+ mnemonic = "Zip",
+ progress_message = "Zipping %s" % (out_filename),
+ )
+ return [DefaultInfo(files = depset([out]))]
+
+zip_archive = rule(
+ implementation = _zip_archive_impl,
+ doc = "A rule to create a zip archive from specified targets",
+ attrs = {
+ "srcs": attr.label_list(
+ mandatory = True,
+ allow_files = True,
+ doc = "the targets with outputs to put in the generated archive",
+ ),
+ "out": attr.string(doc = "the output file name. Will use basename+\".zip\" if not specified"),
+ "_soong_zip": attr.label(
+ default = "//prebuilts/build-tools:soong_zip",
+ executable = True,
+ cfg = "exec",
+ ),
+ },
+)