xref: /aosp_15_r20/external/bazelbuild-rules_python/examples/bzlmod/MODULE.bazel (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1module(
2    name = "example_bzlmod",
3    version = "0.0.0",
4    compatibility_level = 1,
5)
6
7bazel_dep(name = "bazel_skylib", version = "1.4.1")
8bazel_dep(name = "rules_python", version = "0.0.0")
9local_path_override(
10    module_name = "rules_python",
11    path = "../..",
12)
13
14# (py_proto_library specific) We are using rules_proto to define rules_proto targets to be consumed by py_proto_library.
15bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
16
17# (py_proto_library specific) Add the protobuf library for well-known types (e.g. `Any`, `Timestamp`, etc)
18bazel_dep(name = "protobuf", version = "24.4", repo_name = "com_google_protobuf")
19
20# We next initialize the python toolchain using the extension.
21# You can set different Python versions in this block.
22python = use_extension("@rules_python//python/extensions:python.bzl", "python")
23python.toolchain(
24    configure_coverage_tool = True,
25    # Only set when you have multiple toolchain versions.
26    is_default = True,
27    python_version = "3.9",
28)
29
30# We are also using a second version of Python in this project.
31# Typically you will only need a single version of Python, but
32# If you need a different vesion we support more than one.
33# Note: we do not supporting using multiple pip extensions, this is
34# work in progress.
35python.toolchain(
36    configure_coverage_tool = True,
37    python_version = "3.10",
38)
39
40# One can override the actual toolchain versions that are available, which can be useful
41# when optimizing what gets downloaded and when.
42python.override(
43    available_python_versions = [
44        "3.10.9",
45        "3.9.19",
46        # The following is used by the `other_module` and we need to include it here
47        # as well.
48        "3.11.8",
49    ],
50    # Also override the `minor_mapping` so that the root module,
51    # instead of rules_python's defaults, controls what full version
52    # is used when `3.x` is requested.
53    minor_mapping = {
54        "3.10": "3.10.9",
55        "3.11": "3.11.8",
56        "3.9": "3.9.19",
57    },
58)
59
60# Or the sources that the toolchains come from for all platforms
61python.single_version_override(
62    patch_strip = 1,
63    # The user can specify patches to be applied to all interpreters.
64    patches = [],
65    python_version = "3.10.2",
66    sha256 = {
67        "aarch64-apple-darwin": "1409acd9a506e2d1d3b65c1488db4e40d8f19d09a7df099667c87a506f71c0ef",
68        "aarch64-unknown-linux-gnu": "8f351a8cc348bb45c0f95b8634c8345ec6e749e483384188ad865b7428342703",
69        "x86_64-apple-darwin": "8146ad4390710ec69b316a5649912df0247d35f4a42e2aa9615bffd87b3e235a",
70        "x86_64-pc-windows-msvc": "a1d9a594cd3103baa24937ad9150c1a389544b4350e859200b3e5c036ac352bd",
71        "x86_64-unknown-linux-gnu": "9b64eca2a94f7aff9409ad70bdaa7fbbf8148692662e764401883957943620dd",
72    },
73    urls = ["20220227/cpython-{python_version}+20220227-{platform}-{build}.tar.gz"],
74)
75
76# Or a single platform. This can be used in combination with the
77# `single_version_override` and `single_version_platform_override` will be
78# applied after `single_version_override`. Any values present in this override
79# will overwrite the values set by the `single_version_override`
80python.single_version_platform_override(
81    patch_strip = 1,
82    patches = [],
83    platform = "aarch64-apple-darwin",
84    python_version = "3.10.2",
85    sha256 = "1409acd9a506e2d1d3b65c1488db4e40d8f19d09a7df099667c87a506f71c0ef",
86    urls = ["20220227/cpython-{python_version}+20220227-{platform}-{build}.tar.gz"],
87)
88
89# You only need to load this repositories if you are using multiple Python versions.
90# See the tests folder for various examples on using multiple Python versions.
91# The names "python_3_9" and "python_3_10" are autmatically created by the repo
92# rules based on the `python_version` arg values.
93use_repo(python, "python_3_10", "python_3_9", "python_versions")
94
95# EXPERIMENTAL: This is experimental and may be removed without notice
96uv = use_extension("@rules_python//python/uv:extensions.bzl", "uv")
97uv.toolchain(uv_version = "0.2.23")
98use_repo(uv, "uv_toolchains")
99
100register_toolchains("@uv_toolchains//:all")
101
102# This extension allows a user to create modifications to how rules_python
103# creates different wheel repositories.  Different attributes allow the user
104# to modify the BUILD file, and copy files.
105# See @rules_python//python/extensions:whl_mods.bzl attributes for more information
106# on each of the attributes.
107# You are able to set a hub name, so that you can have different modifications of the same
108# wheel in different pip hubs.
109pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
110
111# Call whl_mods.create for the requests package.
112pip.whl_mods(
113    # we are using the appended_build_content.BUILD file
114    # to add content to the request wheel BUILD file.
115    additive_build_content_file = "//whl_mods:appended_build_content.BUILD",
116    data = [":generated_file"],
117    hub_name = "whl_mods_hub",
118    whl_name = "requests",
119)
120
121ADDITIVE_BUILD_CONTENT = """\
122load("@bazel_skylib//rules:write_file.bzl", "write_file")
123write_file(
124    name = "generated_file",
125    out = "generated_file.txt",
126    content = ["Hello world from build content file"],
127)
128"""
129
130# Call whl_mods.create for the wheel package.
131pip.whl_mods(
132    additive_build_content = ADDITIVE_BUILD_CONTENT,
133    copy_executables = {
134        "@@//whl_mods:data/copy_executable.py": "copied_content/executable.py",
135    },
136    copy_files = {
137        "@@//whl_mods:data/copy_file.txt": "copied_content/file.txt",
138    },
139    data = [":generated_file"],
140    data_exclude_glob = ["site-packages/*.dist-info/WHEEL"],
141    hub_name = "whl_mods_hub",
142    whl_name = "wheel",
143)
144use_repo(pip, "whl_mods_hub")
145
146# To fetch pip dependencies, use pip.parse. We can pass in various options,
147# but typically we pass requirements and the Python version. The Python
148# version must have been configured by a corresponding `python.toolchain()`
149# call.
150# Alternatively, `python_interpreter_target` can be used to directly specify
151# the Python interpreter to run to resolve dependencies.
152pip.parse(
153    # We can use `envsubst in the above
154    envsubst = ["PIP_INDEX_URL"],
155    # Use the bazel downloader to query the simple API for downloading the sources
156    # Note, that we can use envsubst for this value.
157    experimental_index_url = "${PIP_INDEX_URL:-https://pypi.org/simple}",
158    # One can also select a particular index for a particular package.
159    # This ensures that the setup is resistant against confusion attacks.
160    # experimental_index_url_overrides = {
161    #    "my_package": "https://different-index-url.com",
162    # },
163    # Or you can specify extra indexes like with `pip`:
164    # experimental_extra_index_urls = [
165    #    "https://different-index-url.com",
166    # ],
167    experimental_requirement_cycles = {
168        "sphinx": [
169            "sphinx",
170            "sphinxcontrib-qthelp",
171            "sphinxcontrib-htmlhelp",
172            "sphinxcontrib-devhelp",
173            "sphinxcontrib-applehelp",
174            "sphinxcontrib-serializinghtml",
175        ],
176    },
177    # You can use one of the values below to specify the target platform
178    # to generate the dependency graph for.
179    experimental_target_platforms = [
180        # Specifying the target platforms explicitly
181        "cp39_linux_x86_64",
182        "cp39_linux_*",
183        "cp39_*",
184    ],
185    hub_name = "pip",
186    python_version = "3.9",
187    requirements_lock = "requirements_lock_3_9.txt",
188    # These modifications were created above and we
189    # are providing pip.parse with the label of the mod
190    # and the name of the wheel.
191    whl_modifications = {
192        "@whl_mods_hub//:requests.json": "requests",
193        "@whl_mods_hub//:wheel.json": "wheel",
194    },
195)
196pip.parse(
197    experimental_requirement_cycles = {
198        "sphinx": [
199            "sphinx",
200            "sphinxcontrib-qthelp",
201            "sphinxcontrib-htmlhelp",
202            "sphinxcontrib-devhelp",
203            "sphinxcontrib-applehelp",
204            "sphinxcontrib-serializinghtml",
205        ],
206    },
207    # You can use one of the values below to specify the target platform
208    # to generate the dependency graph for.
209    experimental_target_platforms = [
210        # Using host python version
211        "linux_*",
212        "osx_*",
213        "windows_*",
214        # Or specifying an exact platform
215        "linux_x86_64",
216        # Or the following to get the `host` platform only
217        "host",
218    ],
219    hub_name = "pip",
220    python_version = "3.10",
221    # The requirements files for each platform that we want to support.
222    requirements_by_platform = {
223        # Default requirements file for needs to explicitly provide the platforms
224        "//:requirements_lock_3_10.txt": "linux_*,osx_*",
225        # This API allows one to specify additional platforms that the users
226        # configure the toolchains for themselves. In this example we add
227        # `windows_aarch64` to illustrate that `rules_python` won't fail to
228        # process the value, but it does not mean that this example will work
229        # on Windows ARM.
230        "//:requirements_windows_3_10.txt": "windows_x86_64,windows_aarch64",
231    },
232    # These modifications were created above and we
233    # are providing pip.parse with the label of the mod
234    # and the name of the wheel.
235    whl_modifications = {
236        "@whl_mods_hub//:requests.json": "requests",
237        "@whl_mods_hub//:wheel.json": "wheel",
238    },
239)
240
241# You can add patches that will be applied on the whl contents.
242#
243# The patches have to be in the unified-diff format.
244pip.override(
245    file = "requests-2.25.1-py2.py3-none-any.whl",
246    patch_strip = 1,
247    patches = [
248        "@//patches:empty.patch",
249        "@//patches:requests_metadata.patch",
250        "@//patches:requests_record.patch",
251    ],
252)
253use_repo(pip, "pip")
254
255bazel_dep(name = "other_module", version = "", repo_name = "our_other_module")
256local_path_override(
257    module_name = "other_module",
258    path = "other_module",
259)
260