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