• 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.4.1")
8bazel_dep(name = "rules_python", version = "0.0.0")
9local_path_override(
10    module_name = "rules_python",
11    path = "../..",
12)
13
14# We next initialize the python toolchain using the extension.
15# You can set different Python versions in this block.
16python = use_extension("@rules_python//python/extensions:python.bzl", "python")
17python.toolchain(
18    configure_coverage_tool = True,
19    # Only set when you have mulitple toolchain versions.
20    is_default = True,
21    python_version = "3.9",
22)
23
24# We are also using a second version of Python in this project.
25# Typically you will only need a single version of Python, but
26# If you need a different vesion we support more than one.
27# Note: we do not supporting using multiple pip extensions, this is
28# work in progress.
29python.toolchain(
30    configure_coverage_tool = True,
31    python_version = "3.10",
32)
33
34# You only need to load this repositories if you are using multiple Python versions.
35# See the tests folder for various examples on using multiple Python versions.
36# The names "python_3_9" and "python_3_10" are autmatically created by the repo
37# rules based on the `python_version` arg values.
38use_repo(python, "python_3_10", "python_3_9", "python_versions")
39
40# This extension allows a user to create modifications to how rules_python
41# creates different wheel repositories.  Different attributes allow the user
42# to modify the BUILD file, and copy files.
43# See @rules_python//python/extensions:whl_mods.bzl attributes for more information
44# on each of the attributes.
45# You are able to set a hub name, so that you can have different modifications of the same
46# wheel in different pip hubs.
47pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
48
49# Call whl_mods.create for the requests package.
50pip.whl_mods(
51    # we are using the appended_build_content.BUILD file
52    # to add content to the request wheel BUILD file.
53    additive_build_content_file = "//whl_mods:appended_build_content.BUILD",
54    data = [":generated_file"],
55    hub_name = "whl_mods_hub",
56    whl_name = "requests",
57)
58
59ADDITIVE_BUILD_CONTENT = """\
60load("@bazel_skylib//rules:write_file.bzl", "write_file")
61write_file(
62    name = "generated_file",
63    out = "generated_file.txt",
64    content = ["Hello world from build content file"],
65)
66"""
67
68# Call whl_mods.create for the wheel package.
69pip.whl_mods(
70    additive_build_content = ADDITIVE_BUILD_CONTENT,
71    copy_executables = {
72        "@@//whl_mods:data/copy_executable.py": "copied_content/executable.py",
73    },
74    copy_files = {
75        "@@//whl_mods:data/copy_file.txt": "copied_content/file.txt",
76    },
77    data = [":generated_file"],
78    data_exclude_glob = ["site-packages/*.dist-info/WHEEL"],
79    hub_name = "whl_mods_hub",
80    whl_name = "wheel",
81)
82use_repo(pip, "whl_mods_hub")
83
84# To fetch pip dependencies, use pip.parse. We can pass in various options,
85# but typically we pass requirements and the Python version. The Python
86# version must have been configured by a corresponding `python.toolchain()`
87# call.
88# Alternatively, `python_interpreter_target` can be used to directly specify
89# the Python interpreter to run to resolve dependencies.
90pip.parse(
91    hub_name = "pip",
92    python_version = "3.9",
93    requirements_lock = "//:requirements_lock_3_9.txt",
94    requirements_windows = "//:requirements_windows_3_9.txt",
95    # These modifications were created above and we
96    # are providing pip.parse with the label of the mod
97    # and the name of the wheel.
98    whl_modifications = {
99        "@whl_mods_hub//:requests.json": "requests",
100        "@whl_mods_hub//:wheel.json": "wheel",
101    },
102)
103pip.parse(
104    hub_name = "pip",
105    python_version = "3.10",
106    requirements_lock = "//:requirements_lock_3_10.txt",
107    requirements_windows = "//:requirements_windows_3_10.txt",
108    # These modifications were created above and we
109    # are providing pip.parse with the label of the mod
110    # and the name of the wheel.
111    whl_modifications = {
112        "@whl_mods_hub//:requests.json": "requests",
113        "@whl_mods_hub//:wheel.json": "wheel",
114    },
115)
116
117# NOTE: The pip_39 repo is only used because the plain `@pip` repo doesn't
118# yet support entry points; see https://github.com/bazelbuild/rules_python/issues/1262
119use_repo(pip, "pip", "pip_39")
120
121bazel_dep(name = "other_module", version = "", repo_name = "our_other_module")
122local_path_override(
123    module_name = "other_module",
124    path = "other_module",
125)
126