blob: 7a135c00e3e37f2be78f7e34d1742e587e7e1a94 [file] [log] [blame]
Klaus Aehlig3c9cd822018-05-24 03:35:42 -07001# 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 Aiuto6a3d5f12021-01-12 13:38:32 -080016load("//:distdir_deps.bzl", "DEPS_BY_NAME")
17load("//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
Tony Aiuto3b2d3102021-01-07 03:40:14 -080018
vladmos20a042f2018-06-01 04:51:21 -070019_BUILD = """
aiuto11462d12019-07-23 14:46:46 -070020load("@rules_pkg//:pkg.bzl", "pkg_tar")
Klaus Aehlig3c9cd822018-05-24 03:35:42 -070021
22pkg_tar(
23 name="archives",
24 srcs = {srcs},
25 package_dir = "{dirname}",
26 visibility = ["//visibility:public"],
27)
28
29"""
30
31def _distdir_tar_impl(ctx):
vladmos20a042f2018-06-01 04:51:21 -070032 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 Aehlig3c9cd822018-05-24 03:35:42 -070039
40_distdir_tar_attrs = {
vladmos20a042f2018-06-01 04:51:21 -070041 "archives": attr.string_list(),
42 "sha256": attr.string_dict(),
43 "urls": attr.string_list_dict(),
44 "dirname": attr.string(default = "distdir"),
Klaus Aehlig3c9cd822018-05-24 03:35:42 -070045}
46
Tony Aiuto337e7172020-12-09 10:20:14 -080047_distdir_tar = repository_rule(
Klaus Aehlig3c9cd822018-05-24 03:35:42 -070048 implementation = _distdir_tar_impl,
49 attrs = _distdir_tar_attrs,
50)
Tony Aiuto337e7172020-12-09 10:20:14 -080051
52def 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 Aiuto3b2d3102021-01-07 03:40:14 -080076
77def dist_http_archive(name, **kwargs):
Tony Aiuto6a3d5f12021-01-12 13:38:32 -080078 """Wraps http_archive, providing attributes like sha and urls from the central list.
Tony Aiuto3b2d3102021-01-07 03:40:14 -080079
Tony Aiuto6a3d5f12021-01-12 13:38:32 -080080 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 Aiuto3b2d3102021-01-07 03:40:14 -080082
83 Args:
84 name: repo name
85 **kwargs: see http_archive for allowed args.
86 """
Tony Aiuto6a3d5f12021-01-12 13:38:32 -080087 info = DEPS_BY_NAME[name]
Tony Aiuto3b2d3102021-01-07 03:40:14 -080088 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 Aiuto6a3d5f12021-01-12 13:38:32 -0800100
101def 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 )