Skip to content

Commit

Permalink
chore(build): refine the locking of Rust dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ADD-SP committed Nov 11, 2024
1 parent fe0df2d commit 3e1b9b3
Show file tree
Hide file tree
Showing 16 changed files with 9,625 additions and 4,216 deletions.
6 changes: 1 addition & 5 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ openresty_repositories()
# [[ BEGIN: must happen after any Rust repositories are loaded
load("//build/kong_crate:deps.bzl", "kong_crate_repositories")

kong_crate_repositories(
cargo_home_isolated = False,
cargo_lockfile = "//:Cargo.Bazel.lock",
lockfile = "//:Cargo.Bazel.lock.json",
)
kong_crate_repositories(cargo_home_isolated = False)

load("//build/kong_crate:crates.bzl", "kong_crates")

Expand Down
8 changes: 6 additions & 2 deletions build/kong_crate/crates.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Setup Crates repostories """

load("@kong_crate_index//:defs.bzl", "crate_repositories")
load("@atc_router_crate_index//:defs.bzl", atc_router_crate_repositories = "crate_repositories")
load("@json_threat_protection_crate_index//:defs.bzl", json_threat_protection_crate_repositories = "crate_repositories")
load("@jsonschema_crate_index//:defs.bzl", jsonschema_crate_repositories = "crate_repositories")

def kong_crates():
crate_repositories()
atc_router_crate_repositories()
json_threat_protection_crate_repositories()
jsonschema_crate_repositories()
38 changes: 22 additions & 16 deletions build/kong_crate/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@ load("@rules_rust//crate_universe:defs.bzl", "crates_repository")
load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")
load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains", "rust_repository_set")

def kong_crate_repositories(cargo_lockfile, lockfile, cargo_home_isolated = True):
def kong_crate_repositories(cargo_home_isolated = True):
"""
Setup Kong Crates repostories
Args:
cargo_lockfile (label): Label to the Cargo.Bazel.lock file,
the document of the crate_universe says that this is to make sure that
Bazel and Cargo are using the same crate versions.
However, we just need the source of the Rust dependencies, we don't need the
`Cargo.lock` file, but this is a mandatory argument, so we just pass the path
to the `Cargo.Bazel.lock` file to make it happy.
lockfile (label): Label to the Cargo.Bazel.lock.json file,
this is the lockfile for reproducible builds.
cargo_home_isolated (bool): `False` to reuse system CARGO_HOME
for faster builds. `True` is default and will use isolated
Cargo home, which takes about 2 minutes to bootstrap.
Expand All @@ -42,15 +32,31 @@ def kong_crate_repositories(cargo_lockfile, lockfile, cargo_home_isolated = True
crate_universe_dependencies()

crates_repository(
name = "kong_crate_index",
cargo_lockfile = cargo_lockfile,
name = "atc_router_crate_index",
cargo_lockfile = "//:crate_locks/atc_router.Cargo.lock",
isolated = cargo_home_isolated,
lockfile = lockfile,
lockfile = "//:crate_locks/atc_router.lock",
manifests = [
"@atc_router//:Cargo.toml",
# [[ BEGIN EE ONLY
],
)

crates_repository(
name = "json_threat_protection_crate_index",
cargo_lockfile = "//:crate_locks/json_threat_protection.Cargo.lock",
isolated = cargo_home_isolated,
lockfile = "//:crate_locks/json_threat_protection.lock",
manifests = [
"@json_threat_protection//:Cargo.toml",
],
)

crates_repository(
name = "jsonschema_crate_index",
cargo_lockfile = "//:crate_locks/jsonschema.Cargo.lock",
isolated = cargo_home_isolated,
lockfile = "//:crate_locks/jsonschema.lock",
manifests = [
"@jsonschema//:Cargo.toml",
# END EE ONLY ]]
],
)
4 changes: 1 addition & 3 deletions build/openresty/atc_router/BUILD.atc_router.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@kong_crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@atc_router_crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_shared_library")

filegroup(
Expand Down Expand Up @@ -26,15 +26,13 @@ rust_shared_library(
"ffi",
],
proc_macro_deps = all_crate_deps(
package_name = "atc-router",
proc_macro = True,
),
rustc_flags = [
"--codegen=strip=symbols",
],
visibility = ["//visibility:public"],
deps = all_crate_deps(
package_name = "atc-router",
normal = True,
),
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@kong_crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@json_threat_protection_crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_shared_library")

filegroup(
Expand All @@ -25,15 +25,13 @@ rust_shared_library(
"ffi",
],
proc_macro_deps = all_crate_deps(
package_name = "json-threat-protection",
proc_macro = True,
),
rustc_flags = [
"--codegen=strip=symbols",
],
visibility = ["//visibility:public"],
deps = all_crate_deps(
package_name = "json-threat-protection",
normal = True,
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ def json_threat_protection_repositories():
# so we'd better use the same protocol instead of HTTPS
# for private repositories.
remote = "[email protected]:Kong/json-threat-protection.rs.git",
build_file = "//build/openresty/json-threat-protection:BUILD.json_threat_proection.bazel",
build_file = "//build/openresty/json_threat_protection:BUILD.json_threat_proection.bazel",
)
4 changes: 1 addition & 3 deletions build/openresty/jsonschema/BUILD.jsonschema.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@kong_crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@jsonschema_crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_shared_library")

filegroup(
Expand All @@ -21,15 +21,13 @@ rust_shared_library(
srcs = [":rust_srcs"],
aliases = aliases(),
proc_macro_deps = all_crate_deps(
package_name = "lua-resty-jsonschema-rs",
proc_macro = True,
),
rustc_flags = [
"--codegen=strip=symbols",
],
visibility = ["//visibility:public"],
deps = all_crate_deps(
package_name = "lua-resty-jsonschema-rs",
normal = True,
),
)
2 changes: 1 addition & 1 deletion build/openresty/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load("//build:build_system.bzl", "git_or_local_repository")
load("//build/openresty/ada:ada_repositories.bzl", "ada_repositories")
load("//build/openresty/atc_router:atc_router_repositories.bzl", "atc_router_repositories")
load("//build/openresty/brotli:brotli_repositories.bzl", "brotli_repositories")
load("//build/openresty/json-threat-protection:json_threat_protection_repositories.bzl", "json_threat_protection_repositories")
load("//build/openresty/json_threat_protection:json_threat_protection_repositories.bzl", "json_threat_protection_repositories")
load("//build/openresty/jsonschema:jsonschema_repositories.bzl", "jsonschema_repositories")
load("//build/openresty/msgpack_c:msgpack_c_repositories.bzl", "msgpack_c_repositories")
load("//build/openresty/openssl:openssl_repositories.bzl", "openssl_repositories")
Expand Down
30 changes: 30 additions & 0 deletions crate_locks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Crate Locks

This directory contains the lock files for the Rust dependencies.

* `xxx.Cargo.lock`: This file is generated by `cargo`, but we generate it with `bazel` to make the `rules_rust` happy.
* `xxx.lock`: This file is generated by `bazel` and is used by `rules_rust` for reproducibility.

## Repin

For more information on how to repin the dependencies,
please check out the [rules_rust](https://github.com/bazelbuild/rules_rust).

### Bash

```bash
crates="atc_router_crate_index,json_threat_protection_crate_index,jsonschema_crate_index"
CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=$scrates bazel sync --only=$crates
unset crates
```

### Fish

```fish
set -l crates \
atc_router_crate_index \
json_threat_protection_crate_index\
jsonschema_crate_index
CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=$(string join ',' $crates) bazel sync --only=$(string join ',' $crates)
set -e crates
```
Loading

0 comments on commit 3e1b9b3

Please sign in to comment.