aboutsummaryrefslogtreecommitdiff
path: root/examples/bzlmod/MODULE.bazel
blob: be9466d883c8707acded222b847eea86f9c53ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module(
    name = "example_bzlmod",
    version = "0.0.0",
    compatibility_level = 1,
)

bazel_dep(name = "bazel_skylib", version = "1.4.1")
bazel_dep(name = "rules_python", version = "0.0.0")
local_path_override(
    module_name = "rules_python",
    path = "../..",
)

# We next initialize the python toolchain using the extension.
# You can set different Python versions in this block.
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
    configure_coverage_tool = True,
    # Only set when you have mulitple toolchain versions.
    is_default = True,
    python_version = "3.9",
)

# We are also using a second version of Python in this project.
# Typically you will only need a single version of Python, but
# If you need a different vesion we support more than one.
# Note: we do not supporting using multiple pip extensions, this is
# work in progress.
python.toolchain(
    configure_coverage_tool = True,
    python_version = "3.10",
)

# You only need to load this repositories if you are using multiple Python versions.
# See the tests folder for various examples on using multiple Python versions.
# The names "python_3_9" and "python_3_10" are autmatically created by the repo
# rules based on the `python_version` arg values.
use_repo(python, "python_3_10", "python_3_9", "python_versions")

# This extension allows a user to create modifications to how rules_python
# creates different wheel repositories.  Different attributes allow the user
# to modify the BUILD file, and copy files.
# See @rules_python//python/extensions:whl_mods.bzl attributes for more information
# on each of the attributes.
# You are able to set a hub name, so that you can have different modifications of the same
# wheel in different pip hubs.
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")

# Call whl_mods.create for the requests package.
pip.whl_mods(
    # we are using the appended_build_content.BUILD file
    # to add content to the request wheel BUILD file.
    additive_build_content_file = "//whl_mods:appended_build_content.BUILD",
    data = [":generated_file"],
    hub_name = "whl_mods_hub",
    whl_name = "requests",
)

ADDITIVE_BUILD_CONTENT = """\
load("@bazel_skylib//rules:write_file.bzl", "write_file")
write_file(
    name = "generated_file",
    out = "generated_file.txt",
    content = ["Hello world from build content file"],
)
"""

# Call whl_mods.create for the wheel package.
pip.whl_mods(
    additive_build_content = ADDITIVE_BUILD_CONTENT,
    copy_executables = {
        "@@//whl_mods:data/copy_executable.py": "copied_content/executable.py",
    },
    copy_files = {
        "@@//whl_mods:data/copy_file.txt": "copied_content/file.txt",
    },
    data = [":generated_file"],
    data_exclude_glob = ["site-packages/*.dist-info/WHEEL"],
    hub_name = "whl_mods_hub",
    whl_name = "wheel",
)
use_repo(pip, "whl_mods_hub")

# To fetch pip dependencies, use pip.parse. We can pass in various options,
# but typically we pass requirements and the Python version. The Python
# version must have been configured by a corresponding `python.toolchain()`
# call.
# Alternatively, `python_interpreter_target` can be used to directly specify
# the Python interpreter to run to resolve dependencies.
pip.parse(
    hub_name = "pip",
    python_version = "3.9",
    requirements_lock = "//:requirements_lock_3_9.txt",
    requirements_windows = "//:requirements_windows_3_9.txt",
    # These modifications were created above and we
    # are providing pip.parse with the label of the mod
    # and the name of the wheel.
    whl_modifications = {
        "@whl_mods_hub//:requests.json": "requests",
        "@whl_mods_hub//:wheel.json": "wheel",
    },
)
pip.parse(
    hub_name = "pip",
    python_version = "3.10",
    requirements_lock = "//:requirements_lock_3_10.txt",
    requirements_windows = "//:requirements_windows_3_10.txt",
    # These modifications were created above and we
    # are providing pip.parse with the label of the mod
    # and the name of the wheel.
    whl_modifications = {
        "@whl_mods_hub//:requests.json": "requests",
        "@whl_mods_hub//:wheel.json": "wheel",
    },
)

# NOTE: The pip_39 repo is only used because the plain `@pip` repo doesn't
# yet support entry points; see https://github.com/bazelbuild/rules_python/issues/1262
use_repo(pip, "pip", "pip_39")

bazel_dep(name = "other_module", version = "", repo_name = "our_other_module")
local_path_override(
    module_name = "other_module",
    path = "other_module",
)