Klaus Aehlig | 3c9cd82 | 2018-05-24 03:35:42 -0700 | [diff] [blame] | 1 | # Copyright 2018 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 | """Defines a repository rule that generates an archive consisting of the specified files to fetch""" |
| 15 | |
Tony Aiuto | 6a3d5f1 | 2021-01-12 13:38:32 -0800 | [diff] [blame] | 16 | load("//:distdir_deps.bzl", "DEPS_BY_NAME") |
| 17 | load("//tools/build_defs/repo:http.bzl", "http_archive", "http_file") |
Tony Aiuto | 3b2d310 | 2021-01-07 03:40:14 -0800 | [diff] [blame] | 18 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 19 | _BUILD = """ |
aiuto | 11462d1 | 2019-07-23 14:46:46 -0700 | [diff] [blame] | 20 | load("@rules_pkg//:pkg.bzl", "pkg_tar") |
Klaus Aehlig | 3c9cd82 | 2018-05-24 03:35:42 -0700 | [diff] [blame] | 21 | |
| 22 | pkg_tar( |
| 23 | name="archives", |
| 24 | srcs = {srcs}, |
| 25 | package_dir = "{dirname}", |
| 26 | visibility = ["//visibility:public"], |
| 27 | ) |
| 28 | |
| 29 | """ |
| 30 | |
| 31 | def _distdir_tar_impl(ctx): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 32 | for name in ctx.attr.archives: |
| 33 | ctx.download(ctx.attr.urls[name], name, ctx.attr.sha256[name], False) |
| 34 | ctx.file("WORKSPACE", "") |
| 35 | ctx.file( |
| 36 | "BUILD", |
| 37 | _BUILD.format(srcs = ctx.attr.archives, dirname = ctx.attr.dirname), |
| 38 | ) |
Klaus Aehlig | 3c9cd82 | 2018-05-24 03:35:42 -0700 | [diff] [blame] | 39 | |
| 40 | _distdir_tar_attrs = { |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 41 | "archives": attr.string_list(), |
| 42 | "sha256": attr.string_dict(), |
| 43 | "urls": attr.string_list_dict(), |
| 44 | "dirname": attr.string(default = "distdir"), |
Klaus Aehlig | 3c9cd82 | 2018-05-24 03:35:42 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Tony Aiuto | 337e717 | 2020-12-09 10:20:14 -0800 | [diff] [blame] | 47 | _distdir_tar = repository_rule( |
Klaus Aehlig | 3c9cd82 | 2018-05-24 03:35:42 -0700 | [diff] [blame] | 48 | implementation = _distdir_tar_impl, |
| 49 | attrs = _distdir_tar_attrs, |
| 50 | ) |
Tony Aiuto | 337e717 | 2020-12-09 10:20:14 -0800 | [diff] [blame] | 51 | |
| 52 | def distdir_tar(name, archives, sha256, urls, dirname, dist_deps = None): |
| 53 | """Creates a repository whose content is a set of tar files. |
| 54 | |
| 55 | Args: |
| 56 | name: repo name. |
| 57 | archives: list of tar file names. |
| 58 | sha256: map of tar file names to SHAs. |
| 59 | urls: map of tar file names to URL lists. |
| 60 | dirname: output directory in repo. |
| 61 | dist_deps: map of repo names to dict of archive, sha256, and urls. |
| 62 | """ |
| 63 | if dist_deps: |
| 64 | for dep, info in dist_deps.items(): |
| 65 | archive_file = info["archive"] |
| 66 | archives.append(archive_file) |
| 67 | sha256[archive_file] = info["sha256"] |
| 68 | urls[archive_file] = info["urls"] |
| 69 | _distdir_tar( |
| 70 | name = name, |
| 71 | archives = archives, |
| 72 | sha256 = sha256, |
| 73 | urls = urls, |
| 74 | dirname = dirname, |
| 75 | ) |
Tony Aiuto | 3b2d310 | 2021-01-07 03:40:14 -0800 | [diff] [blame] | 76 | |
| 77 | def dist_http_archive(name, **kwargs): |
Tony Aiuto | 6a3d5f1 | 2021-01-12 13:38:32 -0800 | [diff] [blame] | 78 | """Wraps http_archive, providing attributes like sha and urls from the central list. |
Tony Aiuto | 3b2d310 | 2021-01-07 03:40:14 -0800 | [diff] [blame] | 79 | |
Tony Aiuto | 6a3d5f1 | 2021-01-12 13:38:32 -0800 | [diff] [blame] | 80 | dist_http_archive wraps an http_archive invocation, but looks up relevant attributes |
| 81 | from distdir_deps.bzl so the user does not have to specify them. |
Tony Aiuto | 3b2d310 | 2021-01-07 03:40:14 -0800 | [diff] [blame] | 82 | |
| 83 | Args: |
| 84 | name: repo name |
| 85 | **kwargs: see http_archive for allowed args. |
| 86 | """ |
Tony Aiuto | 6a3d5f1 | 2021-01-12 13:38:32 -0800 | [diff] [blame] | 87 | info = DEPS_BY_NAME[name] |
Tony Aiuto | 3b2d310 | 2021-01-07 03:40:14 -0800 | [diff] [blame] | 88 | if "patch_args" not in kwargs: |
| 89 | kwargs["patch_args"] = info.get("patch_args") |
| 90 | if "patches" not in kwargs: |
| 91 | kwargs["patches"] = info.get("patches") |
| 92 | if "strip_prefix" not in kwargs: |
| 93 | kwargs["strip_prefix"] = info.get("strip_prefix") |
| 94 | http_archive( |
| 95 | name = name, |
| 96 | sha256 = info["sha256"], |
| 97 | urls = info["urls"], |
| 98 | **kwargs |
| 99 | ) |
Tony Aiuto | 6a3d5f1 | 2021-01-12 13:38:32 -0800 | [diff] [blame] | 100 | |
| 101 | def dist_http_file(name, **kwargs): |
| 102 | """Wraps http_file, providing attributes like sha and urls from the central list. |
| 103 | |
| 104 | dist_http_file wraps an http_file invocation, but looks up relevant attributes |
| 105 | from distdir_deps.bzl so the user does not have to specify them. |
| 106 | |
| 107 | Args: |
| 108 | name: repo name |
| 109 | **kwargs: see http_file for allowed args. |
| 110 | """ |
| 111 | info = DEPS_BY_NAME[name] |
| 112 | http_file( |
| 113 | name = name, |
| 114 | sha256 = info["sha256"], |
| 115 | urls = info["urls"], |
| 116 | **kwargs |
| 117 | ) |