Debian Codegraph @

bazel/cpp_modules.bzl

raw
1 """
2 This module extension creates git_repositories loaded from a json file.
3
4 It is important to separate the implementation from the actual data because
5 the MODULE.bazel.lock file has a checksum of the .bzl files
6
7 """
8
9 load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
10
11 # https://bazel.build/rules/lib/globals/bzl.html#tag_class
12 _from_file_tag = tag_class(
13 doc = "A JSON file containing attributes for git_repository",
14 attrs = {
15 "deps_json": attr.label(
16 doc = "The deps.json file to read and create repos from.",
17 ),
18 },
19 )
20
21 # ctx is https://bazel.build/rules/lib/builtins/module_ctx
22 def _cpp_modules_impl(ctx):
23 all_deps = set()
24 direct_deps = []
25
26 # https://bazel.build/rules/lib/builtins/bazel_module.html
27 for module in ctx.modules:
28 #https://bazel.build/rules/lib/builtins/bazel_module_tags
29 if len(module.tags.from_file) != 1:
30 fail("Must have exactly one from_file attribute set")
31 json_file_label = module.tags.from_file[0].deps_json
32
33 # https://bazel.build/rules/lib/builtins/module_ctx#read
34 json_content = ctx.read(ctx.path(json_file_label))
35 data = json.decode(json_content)
36
37 if "direct" not in data:
38 fail("JSON file must contain a 'direct' list: {}".format(json_file_label))
39 if "indirect" not in data:
40 fail("JSON file must contain a 'indirect' list (even if it's empty): {}".format(json_file_label))
41
42 for repo_data in data["direct"]:
43 name = repo_data.get("name")
44 if name not in all_deps:
45 git_repository(
46 build_file = repo_data.get("build_file"),
47 commit = repo_data.get("commit"),
48 name = name,
49 patch_cmds = repo_data.get("patch_cmds"),
50 patch_cmds_win = repo_data.get("patch_cmds_win"),
51 patches = repo_data.get("patches"),
52 remote = repo_data.get("remote"),
53 )
54 direct_deps.append(name)
55 all_deps.add(name)
56
57 for repo_data in data["indirect"]:
58 name = repo_data.get("name")
59 if name not in all_deps:
60 git_repository(
61 build_file = repo_data.get("build_file"),
62 commit = repo_data.get("commit"),
63 name = name,
64 patch_cmds = repo_data.get("patch_cmds"),
65 patch_cmds_win = repo_data.get("patch_cmds_win"),
66 patches = repo_data.get("patches"),
67 remote = repo_data.get("remote"),
68 )
69 all_deps.add(name)
70
71 # https://bazel.build/rules/lib/builtins/module_ctx#extension_metadata
72 return ctx.extension_metadata(
73 # By specifying the direct dependencies, bazel mod tidy will automatically
74 # update the use_repo call to add or remove dependencies to the list.
75 root_module_direct_deps = direct_deps,
76 root_module_direct_dev_deps = [],
77 # By setting this line, we are telling Bazel that the generated rules are
78 # hermetic all on their own and that Bazel doesn't need to track the deps.json
79 # file (e.g. via a sha256 checksum hash). This *is* the case because we are
80 # generating git_repository rules, which include a specific commit, which is
81 # effectively a hash of all the input contents.
82 # This is a big deal because it means our autorollers can update only the deps.json
83 # file and don't have to update anything in the MODULE.bazel.lock file after.
84 reproducible = True,
85 )
86
87 cpp_modules = module_extension(
88 implementation = _cpp_modules_impl,
89 tag_classes = {
90 "from_file": _from_file_tag,
91 },
92 )