1
+ # Set the name of the bazel workspace.
1
2
workspace (name = "build_file_generation_example" )
2
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.
3
8
load ("@bazel_tools//tools/build_defs/repo:http.bzl" , "http_archive" )
4
9
5
10
######################################################################
6
11
# We need rules_go and bazel_gazelle, to build the gazelle plugin from source.
7
12
# Setup instructions for this section are at
8
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
+ ######################################################################
9
17
10
- # Note, you could omit the rules_go dependency, if you have some way to statically
11
- # compile the gazelle binary for your workspace and distribute it to users on all
12
- # needed platforms.
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.
13
20
http_archive (
14
21
name = "io_bazel_rules_go" ,
15
22
sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa" ,
@@ -19,6 +26,7 @@ http_archive(
19
26
],
20
27
)
21
28
29
+ # Download the bazel_gazelle ruleset.
22
30
http_archive (
23
31
name = "bazel_gazelle" ,
24
32
sha256 = "efbbba6ac1a4fd342d5122cbdfdb82aeb2cf2862e35022c752eaddffada7c3f3" ,
@@ -28,45 +36,96 @@ http_archive(
28
36
],
29
37
)
30
38
39
+ # Load rules_go ruleset and expose the toolchain and dep rules.
31
40
load ("@bazel_gazelle//:deps.bzl" , "gazelle_dependencies" )
32
41
load ("@io_bazel_rules_go//go:deps.bzl" , "go_register_toolchains" , "go_rules_dependencies" )
33
42
43
+ # go_rules_dependencies is a function that registers external dependencies
44
+ # needed by the Go rules.
45
+ # See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
34
46
go_rules_dependencies ()
35
47
48
+ # go_rules_dependencies is a function that registers external dependencies
49
+ # needed by the Go rules.
50
+ # See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
36
51
go_register_toolchains (version = "1.18.3" )
37
52
53
+ # The following call configured the gazelle dependencies, Go environment and Go SDK.
38
54
gazelle_dependencies ()
39
55
40
- ######################################################################
41
- # Remaining setup is for rules_python
56
+ # Remaining setup is for rules_python.
42
57
58
+ # You do not want to use the following command when you are using a WORKSPACE file
59
+ # that is outside of rules_python repository.
60
+ # This command allows targets from a local directory to be bound.
61
+ # Which allows bazel to use targets defined in base rules_python directory.
62
+ # If you are using this example outside of the rules_python git repo,
63
+ # use the http_archive command that is commented out below.
64
+ # https://bazel.build/reference/be/workspace#local_repository
43
65
local_repository (
44
66
name = "rules_python" ,
45
67
path = "../.." ,
46
68
)
47
69
70
+ # When not using this example in the rules_python git repo you would load the python
71
+ # ruleset using the following StarLark.
72
+ # See https://github.com/bazelbuild/rules_python#getting-started for the latest
73
+ # ruleset version.
74
+ #
75
+ # The following StarLark would replace the `local_repository` rule mentioned above.
76
+ #
77
+ # load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
78
+ # http_archive(
79
+ # name = "rules_python",
80
+ # sha256 = "497ca47374f48c8b067d786b512ac10a276211810f4a580178ee9b9ad139323a",
81
+ # strip_prefix = "rules_python-0.16.1",
82
+ # url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.16.1.tar.gz",
83
+ # )
84
+
85
+ # Next we load the toolchain from rules_python.
48
86
load ("@rules_python//python:repositories.bzl" , "python_register_toolchains" )
49
87
88
+ # We now register a hermetic Python interpreter rather than relying on a system-installed interpreter.
89
+ # This toolchain will allow bazel to download a specific python version, and use that version
90
+ # for compilation.
50
91
python_register_toolchains (
51
92
name = "python39" ,
52
93
python_version = "3.9" ,
53
94
)
54
95
96
+ # Load the interpreter and pip_parse rules.
55
97
load ("@python39//:defs.bzl" , "interpreter" )
56
98
load ("@rules_python//python:pip.bzl" , "pip_parse" )
57
99
100
+ # This macro wraps the `pip_repository` rule that invokes `pip`, with `incremental` set.
101
+ # Accepts a locked/compiled requirements file and installs the dependencies listed within.
102
+ # Those dependencies become available in a generated `requirements.bzl` file.
103
+ # You can instead check this `requirements.bzl` file into your repo.
58
104
pip_parse (
59
105
name = "pip" ,
106
+ # (Optional) You can provide a python_interpreter (path) or a python_interpreter_target (a Bazel target, that
107
+ # acts as an executable). The latter can be anything that could be used as Python interpreter. E.g.:
108
+ # 1. Python interpreter that you compile in the build file.
109
+ # 2. Pre-compiled python interpreter included with http_archive.
110
+ # 3. Wrapper script, like in the autodetecting python toolchain.
111
+ #
112
+ # Here, we use the interpreter constant that resolves to the host interpreter from the default Python toolchain.
60
113
python_interpreter_target = interpreter ,
114
+ # Set the location of the lock file.
61
115
requirements_lock = "//:requirements_lock.txt" ,
62
116
)
63
117
118
+ # Load the install_deps macro.
64
119
load ("@pip//:requirements.bzl" , "install_deps" )
65
120
121
+ # Initialize repositories for all packages in requirements_lock.txt.
66
122
install_deps ()
67
123
68
124
# The rules_python gazelle extension has some third-party go dependencies
69
125
# which we need to fetch in order to compile it.
70
126
load ("@rules_python//gazelle:deps.bzl" , _py_gazelle_deps = "gazelle_deps" )
71
127
128
+ # See: https://github.com/bazelbuild/rules_python/blob/main/gazelle/README.md
129
+ # This rule loads and compiles various go dependencies that running gazelle
130
+ # for python requirements.
72
131
_py_gazelle_deps ()
0 commit comments