xref: /aosp_15_r20/external/pytorch/tools/rules/workspace.bzl (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1def _impl(repository_ctx):
2    archive = repository_ctx.attr.name + ".tar"
3    reference = Label("@%s_unpatched//:README" % repository_ctx.attr.name)
4    dirname = repository_ctx.path(reference).dirname
5    repository_ctx.execute(["tar", "hcf", archive, "-C", dirname, "."])
6    repository_ctx.extract(archive)
7    for patch in repository_ctx.attr.patches:
8        repository_ctx.patch(repository_ctx.path(patch), repository_ctx.attr.patch_strip)
9    build_file = repository_ctx.path(repository_ctx.attr.build_file)
10    repository_ctx.execute(["cp", build_file, "BUILD.bazel"])
11
12_patched_rule = repository_rule(
13    implementation = _impl,
14    attrs = {
15        "build_file": attr.label(),
16        "patch_strip": attr.int(),
17        "patches": attr.label_list(),
18    },
19)
20
21def new_patched_local_repository(name, path, **kwargs):
22    native.new_local_repository(
23        name = name + "_unpatched",
24        build_file_content = """
25pkg_tar(name = "content", srcs = glob(["**"]))
26""",
27        path = path,
28    )
29    _patched_rule(name = name, **kwargs)
30
31def _new_empty_repository_impl(repo_ctx):
32    build_file = repo_ctx.attr.build_file
33    build_file_content = repo_ctx.attr.build_file_content
34    if not (bool(build_file) != bool(build_file_content)):
35        fail("Exactly one of 'build_file' or 'build_file_content' is required")
36
37    if build_file_content:
38        repo_ctx.file("BUILD", build_file_content)
39    elif build_file:
40        repo_ctx.template("BUILD", repo_ctx.attr.build_file, {})
41
42new_empty_repository = repository_rule(
43    attrs = {
44        "build_file": attr.label(allow_files = True),
45        "build_file_content": attr.string(),
46    },
47    implementation = _new_empty_repository_impl,
48)
49
50"""Create an empty repository with the supplied BUILD file.
51
52This is mostly useful to create wrappers for specific target that we want
53to be used with the '@' syntax.
54"""
55