blob: bcb205fd7ba6772bec66c82f799334c653dff2ad [file] [log] [blame]
Tim Windelschmidtf5c45102025-02-08 22:30:58 +00001load(
2 "@bazel_tools//tools/build_defs/repo:utils.bzl",
3 "patch",
4 "update_attrs",
5 "workspace_and_buildfile",
6)
7
8def _build_archive_url(owner, repo, ref):
9 return "https://github.com/{owner}/{repo}/archive/{ref}.tar.gz".format(
10 owner = owner,
11 repo = repo,
12 ref = ref,
13 ), "{repo}-{ref}".format(repo = repo, ref = ref)
14
15def build_submodule_info_url(owner, repo, submodule, ref):
16 return "https://api.github.com/repos/{owner}/{repo}/contents/{submodule}?ref={ref}".format(
17 owner = owner,
18 repo = repo,
19 submodule = submodule,
20 ref = ref,
21 )
22
23def parse_github_url(url):
24 url = url.removeprefix("https://github.com/")
25 url = url.removesuffix(".git")
26 (owner, repo) = url.split("/")
27 return owner, repo
28
29def _github_repository(ctx):
30 base_repo_archive_url, base_repo_archive_prefix = _build_archive_url(
31 owner = ctx.attr.owner,
32 repo = ctx.attr.repo,
33 ref = ctx.attr.ref,
34 )
35
36 base_repo_download_info = ctx.download_and_extract(
37 url = base_repo_archive_url,
38 stripPrefix = base_repo_archive_prefix,
39 integrity = ctx.attr.integrity,
40 type = "tar.gz",
41 )
42
43 for submodule, integrity in ctx.attr.submodules.items():
44 url = build_submodule_info_url(
45 owner = ctx.attr.owner,
46 repo = ctx.attr.repo,
47 ref = ctx.attr.ref,
48 submodule = submodule,
49 )
50
51 submodule_info_path = submodule + ".submodule_info"
52 ctx.download(
53 url = url,
54 headers = {
55 "Accept": "application/vnd.github+json",
56 "X-GitHub-Api-Version": "2022-11-28",
57 },
58 output = submodule_info_path,
59 )
60
61 submodule_info = json.decode(ctx.read(submodule_info_path))
62 if submodule_info["type"] != "submodule":
63 fail("provided submodule path is not a submodule")
64
65 submodule_owner, submodule_repo = parse_github_url(
66 url = submodule_info["submodule_git_url"],
67 )
68
69 submodule_url, submodule_strip_prefix = _build_archive_url(
70 owner = submodule_owner,
71 repo = submodule_repo,
72 ref = submodule_info["sha"],
73 )
74
75 download_info = ctx.download_and_extract(
76 url = submodule_url,
77 stripPrefix = submodule_strip_prefix,
78 integrity = integrity,
79 type = "tar.gz",
80 output = submodule_info["path"],
81 )
82 if integrity == "":
83 # buildifier: disable=print
84 print("Missing integrity for submodule \"{submodule}\": \"{sha256}\". Consider adding it.".format(
85 submodule = submodule,
86 integrity = download_info.integrity,
87 ))
88
89 workspace_and_buildfile(ctx)
90
91 patch(ctx)
92
93 return update_attrs(ctx.attr, _github_repository_attrs.keys(), {"integrity": base_repo_download_info.integrity})
94
95_github_repository_attrs = {
96 "owner": attr.string(
97 mandatory = True,
98 doc = "The Owner of the Github repository",
99 ),
100 "repo": attr.string(
101 mandatory = True,
102 doc = "The Name of Github repository",
103 ),
104 "submodules": attr.string_dict(
105 mandatory = False,
106 default = {},
107 doc = "The list of submodules with their integrity as value",
108 ),
109 "ref": attr.string(
110 default = "",
111 doc =
112 "The specific ref to be checked out.",
113 ),
114 "integrity": attr.string(
115 doc = """Expected checksum in Subresource Integrity format of the file downloaded.
116
117 This must match the checksum of the file downloaded. _It is a security risk
118 to omit the checksum as remote files can change._ At best omitting this
119 field will make your build non-hermetic. It is optional to make development
120 easier but either this attribute or `sha256` should be set before shipping.""",
121 ),
122 "patches": attr.label_list(
123 default = [],
124 doc =
125 "A list of files that are to be applied as patches after " +
126 "extracting the archive. By default, it uses the Bazel-native patch implementation " +
127 "which doesn't support fuzz match and binary patch, but Bazel will fall back to use " +
128 "patch command line tool if `patch_tool` attribute is specified or there are " +
129 "arguments other than `-p` in `patch_args` attribute.",
130 ),
131 "patch_tool": attr.string(
132 default = "",
133 doc = "The patch(1) utility to use. If this is specified, Bazel will use the specified " +
134 "patch tool instead of the Bazel-native patch implementation.",
135 ),
136 "patch_args": attr.string_list(
137 default = ["-p0"],
138 doc =
139 "The arguments given to the patch tool. Defaults to -p0, " +
140 "however -p1 will usually be needed for patches generated by " +
141 "git. If multiple -p arguments are specified, the last one will take effect." +
142 "If arguments other than -p are specified, Bazel will fall back to use patch " +
143 "command line tool instead of the Bazel-native patch implementation. When falling " +
144 "back to patch command line tool and patch_tool attribute is not specified, " +
145 "`patch` will be used. This only affects patch files in the `patches` attribute.",
146 ),
147 "patch_cmds": attr.string_list(
148 default = [],
149 doc = "Sequence of Bash commands to be applied on Linux/Macos after patches are applied.",
150 ),
151 "build_file": attr.label(
152 allow_single_file = True,
153 doc =
154 "The file to use as the BUILD file for this repository." +
155 "This attribute is an absolute label (use '@//' for the main " +
156 "repo). The file does not need to be named BUILD, but can " +
157 "be (something like BUILD.new-repo-name may work well for " +
158 "distinguishing it from the repository's actual BUILD files. " +
159 "Either build_file or build_file_content can be specified, but " +
160 "not both.",
161 ),
162 "build_file_content": attr.string(
163 doc =
164 "The content for the BUILD file for this repository. " +
165 "Either build_file or build_file_content can be specified, but " +
166 "not both.",
167 ),
168 "workspace_file": attr.label(
169 doc =
170 "The file to use as the `WORKSPACE` file for this repository. " +
171 "Either `workspace_file` or `workspace_file_content` can be " +
172 "specified, or neither, but not both.",
173 ),
174 "workspace_file_content": attr.string(
175 doc =
176 "The content for the WORKSPACE file for this repository. " +
177 "Either `workspace_file` or `workspace_file_content` can be " +
178 "specified, or neither, but not both.",
179 ),
180}
181
182github_repository = repository_rule(
183 implementation = _github_repository,
184 attrs = _github_repository_attrs,
185)