Skip to content

Commit

Permalink
Remove unnecessary external dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Dec 12, 2024
1 parent 9cdb86c commit 3175132
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 54 deletions.
13 changes: 0 additions & 13 deletions examples/crate_universe/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -629,19 +629,6 @@ 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(
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.file.ssl
crypto = ctx.file.crypto

inputs = depset([ssl, crypto])

ctx.actions.run(
executable = ctx.executable._maker,
oututs = [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_single_file = True,
mandatory = True,
),
"out": attr.string(
doc = "The name of the output directory.",
mandatory = True,
),
"ssl": attr.label(
doc = "The `ssl`/`libssl` library.",
allow_single_file = True,
mandatory = True,
),
"_maker": attr.label(
cfg = "exec",
executable = True,
default = Label("//crate_universe/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::{env, fs};
use std::path::PathBuf;

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,6 +1,6 @@
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(":musl_utils.bzl", "platform_transition_binary")

rust_binary(
name = "hello",
Expand All @@ -11,7 +11,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 @@ -27,7 +27,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 @@ -50,7 +50,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
13 changes: 0 additions & 13 deletions examples/musl_cross_compiling/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,6 @@ rust_register_toolchains(

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

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()

http_archive(
name = "musl_toolchains",
sha256 = "1e6cf99f35277dbb9c3b341a9986d0f33cf70e0cc76a58f062d2d9b7ab56eeeb",
Expand Down
50 changes: 50 additions & 0 deletions examples/musl_cross_compiling/musl_utils.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Utility rules"""

def _transition_platform_impl(_, attr):
return {"//command_line_option:platforms": str(attr.platform)}

_transition_platform = transition(
implementation = _transition_platform_impl,
inputs = [],
outputs = ["//command_line_option:platforms"],
)

def _platform_transition_binary_impl(ctx):
default_info = ctx.attr.binary[DefaultInfo]
executable = ctx.executable.binary

output = ctx.actions.declare_file("{}.{}".format(ctx.label.name, executable.extension).rstrip("."))
ctx.actions.symlink(
output = output,
target_file = executable,
is_executable = True,
)
files = depset(direct = [executable], transitive = [default_info.files])
runfiles = ctx.runfiles([output, executable]).merge(default_info.default_runfiles)

return [DefaultInfo(
files = files,
runfiles = runfiles,
executable = output,
)]

platform_transition_binary = rule(
doc = "Transitions a target to the provided platform.",
implementation = _platform_transition_binary_impl,
attrs = {
"binary": attr.label(
doc = "The target to transition",
allow_single_file = True,
cfg = _transition_platform,
executable = True,
),
"platform": attr.label(
doc = "The platform to transition to.",
mandatory = True,
),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
executable = True,
)
4 changes: 2 additions & 2 deletions examples/zig_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_filegroup")
load("@crate_index//:defs.bzl", "aliases", "all_crate_deps")
load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_binary")
load(":zig_utils.bzl", "platform_transition_filegroup")

rust_binary(
name = "uses_ring",
Expand Down Expand Up @@ -33,5 +33,5 @@ platform(
platform_transition_filegroup(
name = "uses_ring_arm",
srcs = [":uses_ring"],
target_platform = "aarch64_linux",
platform = "aarch64_linux",
)
11 changes: 0 additions & 11 deletions examples/zig_cross_compiling/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,3 @@ crates_repository(
load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()

http_archive(
name = "aspect_bazel_lib",
sha256 = "3534a27621725fbbf1d3e53daa0c1dda055a2732d9031b8c579f917d7347b6c4",
strip_prefix = "bazel-lib-1.16.1",
url = "https://github.com/aspect-build/bazel-lib/archive/refs/tags/v1.16.1.tar.gz",
)

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

aspect_bazel_lib_dependencies()
38 changes: 38 additions & 0 deletions examples/zig_cross_compiling/zig_utils.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Utility rules"""

def _transition_platform_impl(_, attr):
return {"//command_line_option:platforms": str(attr.platform)}

_transition_platform = transition(
implementation = _transition_platform_impl,
inputs = [],
outputs = ["//command_line_option:platforms"],
)

def _platform_transition_filegroup_impl(ctx):
files = [src[DefaultInfo].files for src in ctx.attr.srcs]
runfiles = ctx.runfiles().merge_all([src[DefaultInfo].default_runfiles for src in ctx.attr.srcs])

return [DefaultInfo(
files = files,
runfiles = runfiles,
)]

platform_transition_filegroup = rule(
doc = "Transitions a target to the provided platform.",
implementation = _platform_transition_filegroup_impl,
attrs = {
"platform": attr.label(
doc = "The platform to transition to.",
mandatory = True,
),
"srcs": attr.label_list(
doc = "The targets to transition",
allow_files = True,
cfg = _transition_platform,
),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
)

0 comments on commit 3175132

Please sign in to comment.