xref: /aosp_15_r20/external/bazelbuild-rules_python/examples/build_file_generation/WORKSPACE (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Set the name of the bazel workspace.
2workspace(name = "build_file_generation_example")
3
4# Load the http_archive rule so that we can have bazel download
5# various rulesets and dependencies.
6# The `load` statement imports the symbol for http_archive from the http.bzl
7# file.  When the symbol is loaded you can use the rule.
8load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
9
10######################################################################
11# We need rules_go and bazel_gazelle, to build the gazelle plugin from source.
12# Setup instructions for this section are at
13# https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
14# You may need to update the version of the rule, which is listed in the above
15# documentation.
16######################################################################
17
18# Define an http_archive rule that will download the below ruleset,
19# test the sha, and extract the ruleset to you local bazel cache.
20
21http_archive(
22    name = "io_bazel_rules_go",
23    sha256 = "278b7ff5a826f3dc10f04feaf0b70d48b68748ccd512d7f98bf442077f043fe3",
24    urls = [
25        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
26        "https://github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
27    ],
28)
29
30# Download the bazel_gazelle ruleset.
31http_archive(
32    name = "bazel_gazelle",
33    sha256 = "d3fa66a39028e97d76f9e2db8f1b0c11c099e8e01bf363a923074784e451f809",
34    urls = [
35        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.33.0/bazel-gazelle-v0.33.0.tar.gz",
36        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.33.0/bazel-gazelle-v0.33.0.tar.gz",
37    ],
38)
39
40# Load rules_go ruleset and expose the toolchain and dep rules.
41load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
42load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
43
44# go_rules_dependencies is a function that registers external dependencies
45# needed by the Go rules.
46# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
47go_rules_dependencies()
48
49# go_rules_dependencies is a function that registers external dependencies
50# needed by the Go rules.
51# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
52go_register_toolchains(version = "1.19.4")
53
54# The following call configured the gazelle dependencies, Go environment and Go SDK.
55gazelle_dependencies()
56
57# Remaining setup is for rules_python.
58
59# DON'T COPY_PASTE THIS.
60# Our example uses `local_repository` to point to the HEAD version of rules_python.
61# Users should instead use the installation instructions from the release they use.
62# See https://github.com/bazelbuild/rules_python/releases
63local_repository(
64    name = "rules_python",
65    path = "../..",
66)
67
68local_repository(
69    name = "rules_python_gazelle_plugin",
70    path = "../../gazelle",
71)
72
73# Next we load the setup and toolchain from rules_python.
74load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
75
76# Perform general setup
77py_repositories()
78
79# We now register a hermetic Python interpreter rather than relying on a system-installed interpreter.
80# This toolchain will allow bazel to download a specific python version, and use that version
81# for compilation.
82python_register_toolchains(
83    name = "python39",
84    python_version = "3.9",
85)
86
87load("@rules_python//python:pip.bzl", "pip_parse")
88
89# This macro wraps the `pip_repository` rule that invokes `pip`, with `incremental` set.
90# Accepts a locked/compiled requirements file and installs the dependencies listed within.
91# Those dependencies become available in a generated `requirements.bzl` file.
92# You can instead check this `requirements.bzl` file into your repo.
93pip_parse(
94    name = "pip",
95
96    # Requirement groups allow Bazel to tolerate PyPi cycles by putting dependencies
97    # which are known to form cycles into groups together.
98    experimental_requirement_cycles = {
99        "sphinx": [
100            "sphinx",
101            "sphinxcontrib-qthelp",
102            "sphinxcontrib-htmlhelp",
103            "sphinxcontrib-devhelp",
104            "sphinxcontrib-applehelp",
105            "sphinxcontrib-serializinghtml",
106        ],
107    },
108    # (Optional) You can provide a python_interpreter (path) or a python_interpreter_target (a Bazel target, that
109    # acts as an executable). The latter can be anything that could be used as Python interpreter. E.g.:
110    # 1. Python interpreter that you compile in the build file.
111    # 2. Pre-compiled python interpreter included with http_archive.
112    # 3. Wrapper script, like in the autodetecting python toolchain.
113    #
114    # Here, we use the interpreter constant that resolves to the host interpreter from the default Python toolchain.
115    python_interpreter_target = "@python39_host//:python",
116    # Set the location of the lock file.
117    requirements_lock = "//:requirements_lock.txt",
118    requirements_windows = "//:requirements_windows.txt",
119)
120
121# Load the install_deps macro.
122load("@pip//:requirements.bzl", "install_deps")
123
124# Initialize repositories for all packages in requirements_lock.txt.
125install_deps()
126
127# The rules_python gazelle extension has some third-party go dependencies
128# which we need to fetch in order to compile it.
129load("@rules_python_gazelle_plugin//:deps.bzl", _py_gazelle_deps = "gazelle_deps")
130
131# See: https://github.com/bazelbuild/rules_python/blob/main/gazelle/README.md
132# This rule loads and compiles various go dependencies that running gazelle
133# for python requirements.
134_py_gazelle_deps()
135