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