Skip to content

Commit

Permalink
Merge branch 'main' into process-wrapper-rust
Browse files Browse the repository at this point in the history
  • Loading branch information
hlopko committed Mar 9, 2022
2 parents 8136fca + e5fefdc commit 6f39bc7
Show file tree
Hide file tree
Showing 673 changed files with 119,764 additions and 7,695 deletions.
36 changes: 16 additions & 20 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ default_windows_targets: &default_windows_targets
- "-//test/proto/..."
- "-//tools/rust_analyzer/..."
- "-//test/rustfmt/..."
crate_universe_vendor_example_targets: &crate_universe_vendor_example_targets
- "//vendor_local_manifests:crates_vendor"
- "//vendor_local_pkgs:crates_vendor"
- "//vendor_remote_manifests:crates_vendor_manifests"
- "//vendor_remote_pkgs:crates_vendor_pkgs"
tasks:
ubuntu2004:
build_targets: *default_linux_targets
Expand Down Expand Up @@ -249,9 +254,8 @@ tasks:
crate_universe_examples_ubuntu2004:
name: Crate Universe Examples
platform: ubuntu2004
environment:
RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP: true
working_directory: examples/crate_universe
run_targets: *crate_universe_vendor_example_targets
build_targets:
- "//..."
test_targets:
Expand All @@ -260,12 +264,11 @@ tasks:
crate_universe_rbe_ubuntu1604:
name: Crate Universe Examples
platform: rbe_ubuntu1604
environment:
RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP: true
working_directory: examples/crate_universe
shell_commands:
- sed -i 's/^# load("@bazelci_rules/load("@bazelci_rules/' WORKSPACE.bazel
- sed -i 's/^# rbe_preconfig/rbe_preconfig/' WORKSPACE.bazel
run_targets: *crate_universe_vendor_example_targets
build_targets:
- "//..."
test_targets:
Expand All @@ -274,9 +277,8 @@ tasks:
crate_universe_examples_macos:
name: Crate Universe Examples
platform: macos
environment:
RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP: true
working_directory: examples/crate_universe
run_targets: *crate_universe_vendor_example_targets
build_targets:
- "//..."
test_targets:
Expand All @@ -285,22 +287,16 @@ tasks:
crate_universe_examples_windows:
name: Crate Universe Examples
platform: windows
environment:
RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP: true
working_directory: examples/crate_universe
build_flags:
- "--enable_runfiles" # this is not enabled by default on windows and is necessary for the cargo build scripts
- "--config=rustfmt"
- "--config=clippy"
crate_universe_windows_targets: &crate_universe_windows_targets
batch_commands:
- echo startup --windows_enable_symlinks >> user.bazelrc
- echo build --enable_runfiles >> user.bazelrc # this is not enabled by default on windows and is necessary for the cargo build scripts
run_targets: *crate_universe_vendor_example_targets
build_flags: *aspects_flags
build_targets:
- "//..."
test_targets:
- "//..."
# TODO: There are windows specific build issues in the generated
# dependencies. This should be fixed and all testing enabled.
- "-//uses_proc_macro/..."
- "-//uses_sys_crate/..."
build_targets: *crate_universe_windows_targets
# TODO: fix test targets in `crate_universe_windows_targets`
test_targets: *crate_universe_windows_targets
buildifier:
version: latest
warnings: "all"
10 changes: 7 additions & 3 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# `.bazelrc` is a Bazel configuration file.
# https://docs.bazel.build/versions/master/best-practices.html#using-the-bazelrc-file
# https://bazel.build/docs/best-practices#bazelrc-file

# Enable rustfmt for all targets in the workspace
build:rustfmt --aspects=//rust:defs.bzl%rustfmt_aspect
Expand All @@ -9,5 +9,9 @@ build:rustfmt --output_groups=+rustfmt_checks
build:clippy --aspects=//rust:defs.bzl%rust_clippy_aspect
build:clippy --output_groups=+clippy_checks

# https://bazel.googlesource.com/bazel/+/master/site/docs/windows.md#enable-symlink-support
startup --windows_enable_symlinks
# https://bazel.build/docs/windows#symlink
startup --windows_enable_symlinks

# This import should always be last to allow users to override
# settings for local development.
try-import %workspace%/user.bazelrc
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/examples/bazel-*
/examples/crate_universe/*/bazel-*
/docs/bazel-*
user.bazelrc

# rustfmt
*.rs.bk
Expand Down
4 changes: 4 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ rules_rust_dependencies()

rust_register_toolchains(include_rustc_srcs = True)

load("@rules_rust//crate_universe:crates.bzl", "crate_deps_repository")

crate_deps_repository(bootstrap = True)

load("@rules_rust//proto:repositories.bzl", "rust_proto_repositories")

rust_proto_repositories()
Expand Down
5 changes: 5 additions & 0 deletions cargo/cargo_bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ exports_files([
"{binary}"
])
alias(
name = "binary",
actual = "{binary}",
)
rust_binary(
name = "install",
rustc_env = {{
Expand Down
30 changes: 23 additions & 7 deletions cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load("//rust:defs.bzl", "rust_binary", "rust_common")
load("//rust/private:rustc.bzl", "BuildInfo", "get_compilation_mode_opts", "get_linker_and_args")

# buildifier: disable=bzl-visibility
load("//rust/private:utils.bzl", "expand_dict_value_locations", "find_cc_toolchain", "find_toolchain", "name_to_crate_name")
load("//rust/private:utils.bzl", "dedent", "expand_dict_value_locations", "find_cc_toolchain", "find_toolchain", "name_to_crate_name")

def get_cc_compile_env(cc_toolchain, feature_configuration):
"""Gather cc environment variables from the given `cc_toolchain`
Expand Down Expand Up @@ -56,11 +56,7 @@ def _build_script_impl(ctx):

pkg_name = _name_to_pkg_name(ctx.label.name)

toolchain_tools = [
# Needed for rustc to function.
toolchain.rustc_lib,
toolchain.rust_std,
]
toolchain_tools = [toolchain.all_files]

cc_toolchain = find_cpp_toolchain(ctx)

Expand Down Expand Up @@ -121,6 +117,13 @@ def _build_script_impl(ctx):
if cc_toolchain.sysroot:
env["SYSROOT"] = cc_toolchain.sysroot

# Inform build scripts of rustc flags
# https://github.com/rust-lang/cargo/issues/9600
env["CARGO_ENCODED_RUSTFLAGS"] = "\\x1f".join([
# Allow build scripts to locate the generated sysroot
"--sysroot=${{pwd}}/{}".format(toolchain.sysroot),
] + ctx.attr.rustc_flags)

for f in ctx.attr.crate_features:
env["CARGO_FEATURE_" + f.upper().replace("-", "_")] = "1"

Expand All @@ -136,7 +139,6 @@ def _build_script_impl(ctx):
direct = [
script,
ctx.executable._cargo_build_script_runner,
toolchain.rustc,
] + ctx.files.data + ctx.files.tools + ([toolchain.target_json] if toolchain.target_json else []),
transitive = toolchain_tools,
)
Expand Down Expand Up @@ -217,6 +219,16 @@ _build_script_run = rule(
"links": attr.string(
doc = "The name of the native library this crate links against.",
),
"rustc_flags": attr.string_list(
doc = dedent("""\
List of compiler flags passed to `rustc`.
These strings are subject to Make variable expansion for predefined
source/output path variables like `$location`, `$execpath`, and
`$rootpath`. This expansion is useful if you wish to pass a generated
file of arguments to rustc: `@$(location //package:target)`.
"""),
),
# The source of truth will be the `cargo_build_script` macro until stardoc
# implements documentation inheritence. See https://github.com/bazelbuild/stardoc/issues/27
"script": attr.label(
Expand Down Expand Up @@ -262,6 +274,7 @@ def cargo_build_script(
tools = [],
links = None,
rustc_env = {},
rustc_flags = [],
visibility = None,
tags = None,
**kwargs):
Expand Down Expand Up @@ -333,6 +346,7 @@ def cargo_build_script(
tools (list, optional): Tools (executables) needed by the build script.
links (str, optional): Name of the native library this crate links against.
rustc_env (dict, optional): Environment variables to set in rustc when compiling the build script.
rustc_flags (list, optional): List of compiler flags passed to `rustc`.
visibility (list of label, optional): Visibility to apply to the generated build script output.
tags: (list of str, optional): Tags to apply to the generated build script output.
**kwargs: Forwards to the underlying `rust_binary` rule.
Expand All @@ -357,6 +371,7 @@ def cargo_build_script(
deps = deps,
data = data,
rustc_env = rustc_env,
rustc_flags = rustc_flags,
tags = binary_tags,
**kwargs
)
Expand All @@ -370,6 +385,7 @@ def cargo_build_script(
deps = deps,
data = data,
tools = tools,
rustc_flags = rustc_flags,
visibility = visibility,
tags = tags,
)
Expand Down
9 changes: 9 additions & 0 deletions cargo/cargo_build_script_runner/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ fn run_buildrs() -> Result<(), String> {
}
}

// Bazel does not support byte strings so in order to correctly represent `CARGO_ENCODED_RUSTFLAGS`
// the escaped `\x1f` sequences need to be unescaped
if let Ok(encoded_rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
command.env(
"CARGO_ENCODED_RUSTFLAGS",
encoded_rustflags.replace("\\x1f", "\x1f"),
);
}

let (buildrs_outputs, process_output) = BuildScriptOutput::outputs_from_command(&mut command)
.map_err(|process_output| {
format!(
Expand Down
20 changes: 20 additions & 0 deletions crate_universe/3rdparty/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("//crate_universe:crates.bzl", "crate_deps_target")

package(default_visibility = ["//visibility:public"])

crate_deps_target()

filegroup(
name = "distro",
srcs = glob([
"*.bzl",
"*.bazel",
]) + [
"//crate_universe/3rdparty/crates:srcs",
],
)

filegroup(
name = "bzl_srcs",
srcs = glob(["*.bzl"]),
)
Loading

0 comments on commit 6f39bc7

Please sign in to comment.