# Set the name of the bazel workspace. workspace(name = "build_file_generation_example") # Load the http_archive rule so that we can have bazel download # various rulesets and dependencies. # The `load` statement imports the symbol for http_archive from the http.bzl # file. When the symbol is loaded you can use the rule. load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") ###################################################################### # We need rules_go and bazel_gazelle, to build the gazelle plugin from source. # Setup instructions for this section are at # https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel # You may need to update the version of the rule, which is listed in the above # documentation. ###################################################################### # Define an http_archive rule that will download the below ruleset, # test the sha, and extract the ruleset to you local bazel cache. http_archive( name = "io_bazel_rules_go", sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996", urls = [ "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip", "https://github.com/bazelbuild/rules_go/releases/download/v0.39.1/rules_go-v0.39.1.zip", ], ) # Download the bazel_gazelle ruleset. http_archive( name = "bazel_gazelle", sha256 = "727f3e4edd96ea20c29e8c2ca9e8d2af724d8c7778e7923a854b2c80952bc405", urls = [ "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz", "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz", ], ) # Load rules_go ruleset and expose the toolchain and dep rules. load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies") load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") # go_rules_dependencies is a function that registers external dependencies # needed by the Go rules. # See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies go_rules_dependencies() # go_rules_dependencies is a function that registers external dependencies # needed by the Go rules. # See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies go_register_toolchains(version = "1.19.4") # The following call configured the gazelle dependencies, Go environment and Go SDK. gazelle_dependencies() # Remaining setup is for rules_python. # DON'T COPY_PASTE THIS. # Our example uses `local_repository` to point to the HEAD version of rules_python. # Users should instead use the installation instructions from the release they use. # See https://github.com/bazelbuild/rules_python/releases local_repository( name = "rules_python", path = "../..", ) local_repository( name = "rules_python_gazelle_plugin", path = "../../gazelle", ) # Next we load the toolchain from rules_python. load("@rules_python//python:repositories.bzl", "python_register_toolchains") # We now register a hermetic Python interpreter rather than relying on a system-installed interpreter. # This toolchain will allow bazel to download a specific python version, and use that version # for compilation. python_register_toolchains( name = "python39", python_version = "3.9", ) # Load the interpreter and pip_parse rules. load("@python39//:defs.bzl", "interpreter") load("@rules_python//python:pip.bzl", "pip_parse") # This macro wraps the `pip_repository` rule that invokes `pip`, with `incremental` set. # Accepts a locked/compiled requirements file and installs the dependencies listed within. # Those dependencies become available in a generated `requirements.bzl` file. # You can instead check this `requirements.bzl` file into your repo. pip_parse( name = "pip", # Generate user friendly alias labels for each dependency that we have. incompatible_generate_aliases = True, # (Optional) You can provide a python_interpreter (path) or a python_interpreter_target (a Bazel target, that # acts as an executable). The latter can be anything that could be used as Python interpreter. E.g.: # 1. Python interpreter that you compile in the build file. # 2. Pre-compiled python interpreter included with http_archive. # 3. Wrapper script, like in the autodetecting python toolchain. # # Here, we use the interpreter constant that resolves to the host interpreter from the default Python toolchain. python_interpreter_target = interpreter, # Set the location of the lock file. requirements_lock = "//:requirements_lock.txt", requirements_windows = "//:requirements_windows.txt", ) # Load the install_deps macro. load("@pip//:requirements.bzl", "install_deps") # Initialize repositories for all packages in requirements_lock.txt. install_deps() # The rules_python gazelle extension has some third-party go dependencies # which we need to fetch in order to compile it. load("@rules_python_gazelle_plugin//:deps.bzl", _py_gazelle_deps = "gazelle_deps") # See: https://github.com/bazelbuild/rules_python/blob/main/gazelle/README.md # This rule loads and compiles various go dependencies that running gazelle # for python requirements. _py_gazelle_deps()