Skip to content

Commit

Permalink
Remove unnecessary external dependencies (#3088)
Browse files Browse the repository at this point in the history
Addresses some failures identified in
#3077
  • Loading branch information
UebelAndre authored Dec 13, 2024
1 parent 1352e43 commit ba49599
Show file tree
Hide file tree
Showing 14 changed files with 2,040 additions and 84 deletions.
24 changes: 12 additions & 12 deletions crate_universe/docs_bzlmod.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ There are some examples of using crate_universe with bzlmod in the [example fold
To use rules_rust in a project using bzlmod, add the following to your MODULE.bazel file:
```starlark
```python
bazel_dep(name = "rules_rust", version = "0.49.3")
```
Expand All @@ -34,7 +34,7 @@ You find the latest version on the [release page](https://github.com/bazelbuild/
After adding `rules_rust` in your MODULE.bazel, set the following to begin using `crate_universe`:
```starlark
```python
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
// # ... Dependencies
use_repo(crate, "crates")
Expand All @@ -54,7 +54,7 @@ One of the simpler ways to wire up dependencies would be to first structure your
The crates_repository rule can ingest a root Cargo.toml file and generate Bazel dependencies from there.
You find a complete example in the in the [example folder](../examples/bzlmod/all_crate_deps).
```starlark
```python
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
crate.from_cargo(
Expand All @@ -73,7 +73,7 @@ Since these macros come from the generated repository, the dependencies and alia
they return will automatically update BUILD targets. In your BUILD files,
you use these macros for a Rust library as shown below:
```starlark
```python
load("@crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
Expand Down Expand Up @@ -107,7 +107,7 @@ rust_test(
For a Rust binary that does not depend on any macro, use the following configuration
in your build file:
```starlark
```python
rust_binary(
name = "bin",
srcs = ["src/main.rs"],
Expand Down Expand Up @@ -142,7 +142,7 @@ In situations like this, it may be desirable to have a “Cargo free” setup. Y
crates_repository supports this through the packages attribute,
as shown below.
```starlark
```python
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
crate.spec(package = "serde", features = ["derive"], version = "1.0")
Expand All @@ -156,7 +156,7 @@ use_repo(crate, "crates")
Consuming dependencies may be more ergonomic in this case through the aliases defined in the new repository.
In your BUILD files, you use direct dependencies as shown below:
```starlark
```python
rust_binary(
name = "bin",
crate_root = "src/main.rs",
Expand Down Expand Up @@ -185,7 +185,7 @@ You find a complete example in the in the [example folder](../examples/bzlmod/al
For the setup, you need to add the skylib in addition to the rust rules to your MODUE.bazel.
```starlark
```python
module(
name = "deps_vendored",
version = "0.0.0"
Expand Down Expand Up @@ -230,7 +230,7 @@ but by convention, its either thirdparty or 3rdparty to indicate vendored depend
In the 3rdparty folder, you add a target crates_vendor to declare your dependencies to vendor.
In the example, we vendor a specific version of bzip2.
```starlark
```python
load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_vendor")
crates_vendor(
Expand Down Expand Up @@ -284,7 +284,7 @@ that depends on a vendored dependency. You find a list of all available vendored
in the BUILD file of the generated folder: `basic/3rdparty/crates/BUILD.bazel`
You declare a vendored dependency in you target as following:
```starlark
```python
load("@rules_rust//rust:defs.bzl", "rust_binary")
rust_binary(
Expand All @@ -298,7 +298,7 @@ Note, the vendored dependency is not yet accessible because you have to define f
how to load the vendored dependencies. For that, you first create a file `sys_deps.bzl`
and add the following content:
```starlark
```python
# rename the default name "crate_repositories" in case you import multiple vendored folders.
load("//basic/3rdparty/crates:defs.bzl", basic_crate_repositories = "crate_repositories")
Expand All @@ -314,7 +314,7 @@ just load the vendored dependencies.
In a WORKSPACE configuration, you would just load and call sys_deps(), but in a MODULE configuration, you cannot do that.
Instead, you create a new file `WORKSPACE.bzlmod` and add the following content.
```starlark
```python
load("//:sys_deps.bzl", "sys_deps")
sys_deps()
```
Expand Down
24 changes: 12 additions & 12 deletions docs/src/crate_universe_bzlmod.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ There are some examples of using crate_universe with bzlmod in the [example fold

To use rules_rust in a project using bzlmod, add the following to your MODULE.bazel file:

```starlark
```python
bazel_dep(name = "rules_rust", version = "0.49.3")
```

Expand All @@ -35,7 +35,7 @@ You find the latest version on the [release page](https://github.com/bazelbuild/

After adding `rules_rust` in your MODULE.bazel, set the following to begin using `crate_universe`:

```starlark
```python
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
// # ... Dependencies
use_repo(crate, "crates")
Expand All @@ -55,7 +55,7 @@ One of the simpler ways to wire up dependencies would be to first structure your
The crates_repository rule can ingest a root Cargo.toml file and generate Bazel dependencies from there.
You find a complete example in the in the [example folder](../examples/bzlmod/all_crate_deps).

```starlark
```python
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")

crate.from_cargo(
Expand All @@ -74,7 +74,7 @@ Since these macros come from the generated repository, the dependencies and alia
they return will automatically update BUILD targets. In your BUILD files,
you use these macros for a Rust library as shown below:

```starlark
```python
load("@crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

Expand Down Expand Up @@ -108,7 +108,7 @@ rust_test(
For a Rust binary that does not depend on any macro, use the following configuration
in your build file:

```starlark
```python
rust_binary(
name = "bin",
srcs = ["src/main.rs"],
Expand Down Expand Up @@ -143,7 +143,7 @@ In situations like this, it may be desirable to have a “Cargo free” se
crates_repository supports this through the packages attribute,
as shown below.

```starlark
```python
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")

crate.spec(package = "serde", features = ["derive"], version = "1.0")
Expand All @@ -157,7 +157,7 @@ use_repo(crate, "crates")
Consuming dependencies may be more ergonomic in this case through the aliases defined in the new repository.
In your BUILD files, you use direct dependencies as shown below:

```starlark
```python
rust_binary(
name = "bin",
crate_root = "src/main.rs",
Expand Down Expand Up @@ -186,7 +186,7 @@ You find a complete example in the in the [example folder](../examples/bzlmod/al

For the setup, you need to add the skylib in addition to the rust rules to your MODUE.bazel.

```starlark
```python
module(
name = "deps_vendored",
version = "0.0.0"
Expand Down Expand Up @@ -231,7 +231,7 @@ but by convention, its either thirdparty or 3rdparty to indicate vendored depend
In the 3rdparty folder, you add a target crates_vendor to declare your dependencies to vendor.
In the example, we vendor a specific version of bzip2.

```starlark
```python
load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_vendor")

crates_vendor(
Expand Down Expand Up @@ -285,7 +285,7 @@ that depends on a vendored dependency. You find a list of all available vendored
in the BUILD file of the generated folder: `basic/3rdparty/crates/BUILD.bazel`
You declare a vendored dependency in you target as following:

```starlark
```python
load("@rules_rust//rust:defs.bzl", "rust_binary")

rust_binary(
Expand All @@ -299,7 +299,7 @@ Note, the vendored dependency is not yet accessible because you have to define f
how to load the vendored dependencies. For that, you first create a file `sys_deps.bzl`
and add the following content:

```starlark
```python
# rename the default name "crate_repositories" in case you import multiple vendored folders.
load("//basic/3rdparty/crates:defs.bzl", basic_crate_repositories = "crate_repositories")

Expand All @@ -315,7 +315,7 @@ just load the vendored dependencies.
In a WORKSPACE configuration, you would just load and call sys_deps(), but in a MODULE configuration, you cannot do that.
Instead, you create a new file `WORKSPACE.bzlmod` and add the following content.

```starlark
```python
load("//:sys_deps.bzl", "sys_deps")
sys_deps()
```
Expand Down
19 changes: 2 additions & 17 deletions examples/crate_universe/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -630,31 +630,16 @@ load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_depende
rules_foreign_cc_dependencies()

http_archive(
name = "aspect_bazel_lib",
sha256 = "f5ea76682b209cc0bd90d0f5a3b26d2f7a6a2885f0c5f615e72913f4805dbb0d",
strip_prefix = "bazel-lib-2.5.0",
url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.5.0/bazel-lib-v2.5.0.tar.gz",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "aspect_bazel_lib_register_toolchains")

aspect_bazel_lib_dependencies()

aspect_bazel_lib_register_toolchains()

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
name = "boringssl",
commit = "44b3df6f03d85c901767250329c571db405122d5",
patch_args = ["-p1"],
patches = [
"//complicated_dependencies:boringssl-filegroup.patch",
# On the macOS bazelci builders, there's a system-installed openssl, and that takes priority over -isystem flags, which is what cc_library.includes uses.
# This forces our local system-includes to be chosen with higher priority, which avoids conflicts.
"//complicated_dependencies:boringssl-system-includes.patch",
],
remote = "https://github.com/google/boringssl.git",
strip_prefix = "boringssl-44b3df6f03d85c901767250329c571db405122d5",
urls = ["https://github.com/google/boringssl/archive/44b3df6f03d85c901767250329c571db405122d5.zip"],
)

crates_repository(
Expand Down
22 changes: 11 additions & 11 deletions examples/crate_universe/complicated_dependencies/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@rules_rust//rust:defs.bzl", "rust_binary")
load(":boringssl_utils.bzl", "boringssl_build_script_dir")

rust_binary(
name = "build_script_dir_maker",
srcs = ["build_script_dir_maker.rs"],
edition = "2021",
)

# This target lays out the output needed from boringssl in the directory structure needed by the boring-sys build script.
copy_to_directory(
boringssl_build_script_dir(
name = "boringssl_gen_dir",
srcs = [
"@boringssl//:crypto",
"@boringssl//:ssl",
],
out = "boringssl_gen_dir_out",
include_external_repositories = ["*"],
replace_prefixes = {
"libcrypto.a": "build/libcrypto.a",
"libssl.a": "build/libssl.a",
},
crypto = "@boringssl//:crypto",
ssl = "@boringssl//:ssl",
visibility = ["//visibility:public"],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""BoringSSL Utils"""

def _boringssl_build_script_dir_impl(ctx):
output = ctx.actions.declare_directory(ctx.attr.out)

ssl = ctx.files.ssl[0]
crypto = ctx.files.crypto[0]

inputs = depset([ssl, crypto])

ctx.actions.run(
executable = ctx.executable._maker,
outputs = [output],
inputs = inputs,
env = {
"ARG_CRYPTO": crypto.path,
"ARG_OUTPUT": output.path,
"ARG_SSL": ssl.path,
},
)

return [DefaultInfo(
files = depset([output]),
runfiles = ctx.runfiles([output]),
)]

boringssl_build_script_dir = rule(
doc = "A utility rule for building directories compatible with its `cargo_build_script` target.",
implementation = _boringssl_build_script_dir_impl,
attrs = {
"crypto": attr.label(
doc = "The `crypto`/`libcrypto` library.",
allow_files = True,
mandatory = True,
),
"out": attr.string(
doc = "The name of the output directory.",
mandatory = True,
),
"ssl": attr.label(
doc = "The `ssl`/`libssl` library.",
allow_files = True,
mandatory = True,
),
"_maker": attr.label(
cfg = "exec",
executable = True,
default = Label("//complicated_dependencies:build_script_dir_maker"),
),
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! A utility script for the "complicated dependencies" example.
use std::path::PathBuf;
use std::{env, fs};

fn main() {
let ssl = PathBuf::from(env::var("ARG_SSL").unwrap());
let crypto = PathBuf::from(env::var("ARG_CRYPTO").unwrap());
let output = PathBuf::from(env::var("ARG_OUTPUT").unwrap());

let build_dir = output.join("build");

fs::create_dir_all(&build_dir).unwrap();

fs::copy(&ssl, build_dir.join(ssl.file_name().unwrap())).unwrap();
fs::copy(&crypto, build_dir.join(crypto.file_name().unwrap())).unwrap();
}
8 changes: 4 additions & 4 deletions examples/musl_cross_compiling/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_binary")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@rules_rust//rust:defs.bzl", "rust_binary")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
load(":musl_utils.bzl", "platform_transition_binary")

rust_binary(
name = "hello",
Expand All @@ -12,7 +12,7 @@ rust_binary(
platform_transition_binary(
name = "hello_linux_x86_64_musl",
binary = ":hello",
target_platform = "//platforms:linux_x86_64_musl",
platform = "//platforms:linux_x86_64_musl",
)

sh_test(
Expand All @@ -28,7 +28,7 @@ sh_test(
platform_transition_binary(
name = "hello_linux_arm64_musl",
binary = ":hello",
target_platform = "//platforms:linux_arm64_musl",
platform = "//platforms:linux_arm64_musl",
)

sh_test(
Expand All @@ -51,7 +51,7 @@ rust_binary(
platform_transition_binary(
name = "keyring_linux_x86_64_musl",
binary = ":keyring",
target_platform = "//platforms:linux_x86_64_musl",
platform = "//platforms:linux_x86_64_musl",
)

build_test(
Expand Down
Loading

0 comments on commit ba49599

Please sign in to comment.