diff --git a/bazel/repositories.bzl b/bazel/repositories.bzl index c53d62da1bbb6..d7efdfdf1f55d 100644 --- a/bazel/repositories.bzl +++ b/bazel/repositories.bzl @@ -52,6 +52,54 @@ _default_envoy_build_config = repository_rule( }, ) +def _envoy_repo_impl(repository_ctx): + """This provides information about the Envoy repository + + You can access the current version and path to the repository in .bzl/BUILD + files as follows: + + ```starlark + load("@envoy_repo//:version.bzl", "VERSION") + ``` + + `VERSION` can be used to derive version-specific rules and can be passed + to the rules. + + The `VERSION` and also the local `PATH` to the repo can be accessed in + python libraries/binaries. By adding `@envoy_repo` to `deps` they become + importable through the `envoy_repo` namespace. + + As the `PATH` is local to the machine, it is generally only useful for + jobs that will run locally. + + This can be useful for example, for tooling that needs to check the + repository, or to run bazel queries that cannot be run within the + constraints of a `genquery`. + + """ + repo_path = repository_ctx.path(repository_ctx.attr.envoy_root).dirname + version = repository_ctx.read(repo_path.get_child("VERSION")).strip() + repository_ctx.file("version.bzl", "VERSION = '%s'" % version) + repository_ctx.file("__init__.py", "PATH = '%s'\nVERSION = '%s'" % (repo_path, version)) + repository_ctx.file("WORKSPACE", "") + repository_ctx.file("BUILD", """ +load("@rules_python//python:defs.bzl", "py_library") + +py_library(name = "envoy_repo", srcs = ["__init__.py"], visibility = ["//visibility:public"]) + +""") + +_envoy_repo = repository_rule( + implementation = _envoy_repo_impl, + attrs = { + "envoy_root": attr.label(default = "@envoy//:BUILD"), + }, +) + +def envoy_repo(): + if "envoy_repo" not in native.existing_rules().keys(): + _envoy_repo(name = "envoy_repo") + # Python dependencies. def _python_deps(): # TODO(htuch): convert these to pip3_import. @@ -100,6 +148,9 @@ def _rust_deps(): external_http_archive("rules_rust") def envoy_dependencies(skip_targets = []): + # Add a binding for repository variables. + envoy_repo() + # Setup Envoy developer tools. envoy_dev_binding()