• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996",
24    urls = [
25        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
26        "https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip",
27    ],
28)
29
30# Download the bazel_gazelle ruleset.
31
32http_archive(
33    name = "bazel_gazelle",
34    sha256 = "727f3e4edd96ea20c29e8c2ca9e8d2af724d8c7778e7923a854b2c80952bc405",
35    urls = [
36        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
37        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
38    ],
39)
40
41# Load rules_go ruleset and expose the toolchain and dep rules.
42load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
43load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
44
45# go_rules_dependencies is a function that registers external dependencies
46# needed by the Go rules.
47# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
48go_rules_dependencies()
49
50# go_rules_dependencies is a function that registers external dependencies
51# needed by the Go rules.
52# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
53go_register_toolchains(version = "1.19.4")
54
55# The following call configured the gazelle dependencies, Go environment and Go SDK.
56gazelle_dependencies()
57
58# Remaining setup is for rules_python.
59
60# DON'T COPY_PASTE THIS.
61# Our example uses `local_repository` to point to the HEAD version of rules_python.
62# Users should instead use the installation instructions from the release they use.
63# See https://github.com/bazelbuild/rules_python/releases
64local_repository(
65    name = "rules_python",
66    path = "../..",
67)
68
69local_repository(
70    name = "rules_python_gazelle_plugin",
71    path = "../../gazelle",
72)
73
74# Next we load the toolchain from rules_python.
75load("@rules_python//python:repositories.bzl", "python_register_toolchains")
76
77# We now register a hermetic Python interpreter rather than relying on a system-installed interpreter.
78# This toolchain will allow bazel to download a specific python version, and use that version
79# for compilation.
80python_register_toolchains(
81    name = "python39",
82    python_version = "3.9",
83)
84
85# Load the interpreter and pip_parse rules.
86load("@python39//:defs.bzl", "interpreter")
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    # Generate user friendly alias labels for each dependency that we have.
96    incompatible_generate_aliases = True,
97    # (Optional) You can provide a python_interpreter (path) or a python_interpreter_target (a Bazel target, that
98    # acts as an executable). The latter can be anything that could be used as Python interpreter. E.g.:
99    # 1. Python interpreter that you compile in the build file.
100    # 2. Pre-compiled python interpreter included with http_archive.
101    # 3. Wrapper script, like in the autodetecting python toolchain.
102    #
103    # Here, we use the interpreter constant that resolves to the host interpreter from the default Python toolchain.
104    python_interpreter_target = interpreter,
105    # Set the location of the lock file.
106    requirements_lock = "//:requirements_lock.txt",
107    requirements_windows = "//:requirements_windows.txt",
108)
109
110# Load the install_deps macro.
111load("@pip//:requirements.bzl", "install_deps")
112
113# Initialize repositories for all packages in requirements_lock.txt.
114install_deps()
115
116# The rules_python gazelle extension has some third-party go dependencies
117# which we need to fetch in order to compile it.
118load("@rules_python_gazelle_plugin//:deps.bzl", _py_gazelle_deps = "gazelle_deps")
119
120# See: https://github.com/bazelbuild/rules_python/blob/main/gazelle/README.md
121# This rule loads and compiles various go dependencies that running gazelle
122# for python requirements.
123_py_gazelle_deps()
124