Skip to content

Commit

Permalink
bazel: Shift @envoy_repo to separate file (envoyproxy#35718)
Browse files Browse the repository at this point in the history
For external repositories that want to access information about the
envoy repo - eg current/stable/archived versions - this allows them to
do so without having to load all of envoy's dependency chain

Signed-off-by: Ryan Northey <[email protected]>
Signed-off-by: duxin40 <[email protected]>
  • Loading branch information
phlax authored and duxin40 committed Oct 15, 2024
1 parent 563c18e commit 275f906
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 154 deletions.
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ load("//bazel:api_repositories.bzl", "envoy_api_dependencies")

envoy_api_dependencies()

load("//bazel:repo.bzl", "envoy_repo")

envoy_repo()

load("//bazel:repositories.bzl", "envoy_dependencies")

envoy_dependencies()
Expand Down
152 changes: 152 additions & 0 deletions bazel/repo.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# `@envoy_repo` repository rule for managing the repo and querying its metadata.

def _envoy_repo_impl(repository_ctx):
"""This provides information about the Envoy repository
You can access the current project and api versions and the path to the repository in
.bzl/BUILD files as follows:
```starlark
load("@envoy_repo//:version.bzl", "VERSION", "API_VERSION")
```
`*VERSION` can be used to derive version-specific rules and can be passed
to the rules.
The `VERSION`s 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 bazel run jobs to run bazel queries that cannot be run
within the constraints of a `genquery`, or that otherwise need access to the repository
files.
Project and repo data can be accessed in JSON format using `@envoy_repo//:project`, eg:
```starlark
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
jq(
name = "project_version",
srcs = ["@envoy_repo//:data"],
out = "version.txt",
args = ["-r"],
filter = ".version",
)
```
"""
repo_version_path = repository_ctx.path(repository_ctx.attr.envoy_version)
api_version_path = repository_ctx.path(repository_ctx.attr.envoy_api_version)
version = repository_ctx.read(repo_version_path).strip()
api_version = repository_ctx.read(api_version_path).strip()
repository_ctx.file("version.bzl", "VERSION = '%s'\nAPI_VERSION = '%s'" % (version, api_version))
repository_ctx.file("path.bzl", "PATH = '%s'" % repo_version_path.dirname)
repository_ctx.file("__init__.py", "PATH = '%s'\nVERSION = '%s'\nAPI_VERSION = '%s'" % (repo_version_path.dirname, version, api_version))
repository_ctx.file("WORKSPACE", "")
repository_ctx.file("BUILD", '''
load("@rules_python//python:defs.bzl", "py_library")
load("@envoy//tools/base:envoy_python.bzl", "envoy_entry_point")
load("//:path.bzl", "PATH")
py_library(
name = "envoy_repo",
srcs = ["__init__.py"],
visibility = ["//visibility:public"],
)
envoy_entry_point(
name = "get_project_json",
pkg = "envoy.base.utils",
script = "envoy.project_data",
init_data = [":__init__.py"],
)
genrule(
name = "project",
outs = ["project.json"],
cmd = """
$(location :get_project_json) $$(dirname $(location @envoy//:VERSION.txt)) > $@
""",
tools = [
":get_project_json",
"@envoy//:VERSION.txt",
"@envoy//changelogs",
],
visibility = ["//visibility:public"],
)
envoy_entry_point(
name = "release",
args = [
"release",
PATH,
"--release-message-path=$(location @envoy//changelogs:summary)",
],
data = ["@envoy//changelogs:summary"],
pkg = "envoy.base.utils",
script = "envoy.project",
init_data = [":__init__.py"],
)
envoy_entry_point(
name = "dev",
args = [
"dev",
PATH,
],
pkg = "envoy.base.utils",
script = "envoy.project",
init_data = [":__init__.py"],
)
envoy_entry_point(
name = "sync",
args = [
"sync",
PATH,
],
pkg = "envoy.base.utils",
script = "envoy.project",
init_data = [":__init__.py"],
)
envoy_entry_point(
name = "publish",
args = [
"publish",
PATH,
],
pkg = "envoy.base.utils",
script = "envoy.project",
init_data = [":__init__.py"],
)
envoy_entry_point(
name = "trigger",
args = [
"trigger",
PATH,
],
pkg = "envoy.base.utils",
script = "envoy.project",
init_data = [":__init__.py"],
)
''')

_envoy_repo = repository_rule(
implementation = _envoy_repo_impl,
attrs = {
"envoy_version": attr.label(default = "@envoy//:VERSION.txt"),
"envoy_api_version": attr.label(default = "@envoy//:API_VERSION.txt"),
},
)

def envoy_repo():
if "envoy_repo" not in native.existing_rules().keys():
_envoy_repo(name = "envoy_repo")
154 changes: 0 additions & 154 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,157 +70,6 @@ _default_envoy_build_config = repository_rule(
},
)

def _envoy_repo_impl(repository_ctx):
"""This provides information about the Envoy repository
You can access the current project and api versions and the path to the repository in
.bzl/BUILD files as follows:
```starlark
load("@envoy_repo//:version.bzl", "VERSION", "API_VERSION")
```
`*VERSION` can be used to derive version-specific rules and can be passed
to the rules.
The `VERSION`s 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 bazel run jobs to run bazel queries that cannot be run
within the constraints of a `genquery`, or that otherwise need access to the repository
files.
Project and repo data can be accessed in JSON format using `@envoy_repo//:project`, eg:
```starlark
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
jq(
name = "project_version",
srcs = ["@envoy_repo//:data"],
out = "version.txt",
args = ["-r"],
filter = ".version",
)
```
"""
repo_version_path = repository_ctx.path(repository_ctx.attr.envoy_version)
api_version_path = repository_ctx.path(repository_ctx.attr.envoy_api_version)
version = repository_ctx.read(repo_version_path).strip()
api_version = repository_ctx.read(api_version_path).strip()
repository_ctx.file("version.bzl", "VERSION = '%s'\nAPI_VERSION = '%s'" % (version, api_version))
repository_ctx.file("path.bzl", "PATH = '%s'" % repo_version_path.dirname)
repository_ctx.file("__init__.py", "PATH = '%s'\nVERSION = '%s'\nAPI_VERSION = '%s'" % (repo_version_path.dirname, version, api_version))
repository_ctx.file("WORKSPACE", "")
repository_ctx.file("BUILD", '''
load("@rules_python//python:defs.bzl", "py_library")
load("@envoy//tools/base:envoy_python.bzl", "envoy_entry_point")
load("//:path.bzl", "PATH")
py_library(
name = "envoy_repo",
srcs = ["__init__.py"],
visibility = ["//visibility:public"],
)
envoy_entry_point(
name = "get_project_json",
pkg = "envoy.base.utils",
script = "envoy.project_data",
init_data = [":__init__.py"],
)
genrule(
name = "project",
outs = ["project.json"],
cmd = """
$(location :get_project_json) $$(dirname $(location @envoy//:VERSION.txt)) > $@
""",
tools = [
":get_project_json",
"@envoy//:VERSION.txt",
"@envoy//changelogs",
],
visibility = ["//visibility:public"],
)
envoy_entry_point(
name = "release",
args = [
"release",
PATH,
"--release-message-path=$(location @envoy//changelogs:summary)",
],
data = ["@envoy//changelogs:summary"],
pkg = "envoy.base.utils",
script = "envoy.project",
init_data = [":__init__.py"],
)
envoy_entry_point(
name = "dev",
args = [
"dev",
PATH,
],
pkg = "envoy.base.utils",
script = "envoy.project",
init_data = [":__init__.py"],
)
envoy_entry_point(
name = "sync",
args = [
"sync",
PATH,
],
pkg = "envoy.base.utils",
script = "envoy.project",
init_data = [":__init__.py"],
)
envoy_entry_point(
name = "publish",
args = [
"publish",
PATH,
],
pkg = "envoy.base.utils",
script = "envoy.project",
init_data = [":__init__.py"],
)
envoy_entry_point(
name = "trigger",
args = [
"trigger",
PATH,
],
pkg = "envoy.base.utils",
script = "envoy.project",
init_data = [":__init__.py"],
)
''')

_envoy_repo = repository_rule(
implementation = _envoy_repo_impl,
attrs = {
"envoy_version": attr.label(default = "@envoy//:VERSION.txt"),
"envoy_api_version": attr.label(default = "@envoy//:API_VERSION.txt"),
},
)

def envoy_repo():
if "envoy_repo" not in native.existing_rules().keys():
_envoy_repo(name = "envoy_repo")

# Bazel native C++ dependencies. For the dependencies that doesn't provide autoconf/automake builds.
def _cc_deps():
external_http_archive("grpc_httpjson_transcoding")
Expand Down Expand Up @@ -255,9 +104,6 @@ def _rust_deps():
)

def envoy_dependencies(skip_targets = []):
# Add a binding for repository variables.
envoy_repo()

# Setup Envoy developer tools.
envoy_dev_binding()

Expand Down

0 comments on commit 275f906

Please sign in to comment.