xref: /aosp_15_r20/external/bazelbuild-rules_python/examples/bzlmod_build_file_generation/BUILD.bazel (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Load various rules so that we can have bazel download
2# various rulesets and dependencies.
3# The `load` statement imports the symbol for the rule, in the defined
4# ruleset. When the symbol is loaded you can use the rule.
5
6# The following code loads the base python requirements and gazelle
7# requirements.
8load("@bazel_gazelle//:def.bzl", "gazelle")
9load("@pip//:requirements.bzl", "all_whl_requirements")
10load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
11load("@rules_python//python:pip.bzl", "compile_pip_requirements")
12load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
13load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
14
15# This stanza calls a rule that generates targets for managing pip dependencies
16# with pip-compile.
17compile_pip_requirements(
18    name = "requirements",
19    src = "requirements.in",
20    requirements_txt = "requirements_lock.txt",
21    requirements_windows = "requirements_windows.txt",
22)
23
24# This repository rule fetches the metadata for python packages we
25# depend on. That data is required for the gazelle_python_manifest
26# rule to update our manifest file.
27modules_mapping(
28    name = "modules_map",
29    exclude_patterns = [
30        "^_|(\\._)+",  # This is the default.
31        "(\\.tests)+",  # Add a custom one to get rid of the psutil tests.
32        "^colorama",  # Get rid of colorama on Windows.
33        "^lazy_object_proxy\\.cext$",  # Get rid of this on Linux because it isn't included on Windows.
34    ],
35    wheels = all_whl_requirements,
36)
37
38# Gazelle python extension needs a manifest file mapping from
39# an import to the installed package that provides it.
40# This macro produces two targets:
41# - //:gazelle_python_manifest.update can be used with `bazel run`
42#   to recalculate the manifest
43# - //:gazelle_python_manifest.test is a test target ensuring that
44#   the manifest doesn't need to be updated
45# This target updates a file called gazelle_python.yaml, and
46# requires that file exist before the target is run.
47# When you are using gazelle you need to run this target first.
48gazelle_python_manifest(
49    name = "gazelle_python_manifest",
50    modules_mapping = ":modules_map",
51    pip_repository_name = "pip",
52    tags = ["exclusive"],
53)
54
55# Our gazelle target points to the python gazelle binary.
56# This is the simple case where we only need one language supported.
57# If you also had proto, go, or other gazelle-supported languages,
58# you would also need a gazelle_binary rule.
59# See https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.rst#example
60# This is the primary gazelle target to run, so that you can update BUILD.bazel files.
61# You can execute:
62# - bazel run //:gazelle update
63# - bazel run //:gazelle fix
64# See: https://github.com/bazelbuild/bazel-gazelle#fix-and-update
65gazelle(
66    name = "gazelle",
67    gazelle = "@rules_python_gazelle_plugin//python:gazelle_binary",
68)
69
70# The following targets are created and maintained by gazelle
71py_library(
72    name = "bzlmod_build_file_generation",
73    srcs = ["lib.py"],
74    visibility = ["//:__subpackages__"],
75    deps = ["@pip//tabulate"],
76)
77
78py_binary(
79    name = "bzlmod_build_file_generation_bin",
80    srcs = ["__main__.py"],
81    main = "__main__.py",
82    visibility = ["//:__subpackages__"],
83    deps = [":bzlmod_build_file_generation"],
84)
85
86py_test(
87    name = "bzlmod_build_file_generation_test",
88    srcs = ["__test__.py"],
89    main = "__test__.py",
90    deps = [":bzlmod_build_file_generation"],
91)
92