Skip to content

Commit

Permalink
Merge branch 'main' into revert
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre authored Dec 18, 2024
2 parents fe2f534 + 0e2fbbf commit 6475fa9
Show file tree
Hide file tree
Showing 76 changed files with 2,504 additions and 2,830 deletions.
8 changes: 2 additions & 6 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -539,17 +539,13 @@ tasks:
platform: ubuntu2004
working_directory: examples/crate_universe_local_path
run_targets:
- "//:vendor_lazy_static_out_of_tree"
test_targets:
- "//..."
- "//:vendor_edit_test_out_of_tree"
crate_universe_local_path_in_tree:
name: Crate Universe Local Path In Tree
platform: ubuntu2004
working_directory: examples/crate_universe_local_path
run_targets:
- "//:vendor_lazy_static_in_tree"
test_targets:
- "//..."
- "//:vendor_edit_test_in_tree"
# See https://github.com/bazelbuild/rules_rust/issues/2186 about re-enabling these.
# crate_universe_examples_windows:
# name: Crate Universe Examples
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module(
name = "rules_rust",
version = "0.55.6",
version = "0.56.0",
)

###############################################################################
Expand Down
10 changes: 7 additions & 3 deletions cargo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

package(default_visibility = ["//visibility:public"])
exports_files(["defs.bzl"])

bzl_library(
name = "bzl_lib",
srcs = glob(["**/*.bzl"]),
deps = ["//cargo/private:bzl_lib"],
srcs = glob(["*.bzl"]),
visibility = ["//visibility:public"],
deps = [
"//cargo/private:bzl_lib",
"//cargo/settings:bzl_lib",
],
)
5 changes: 4 additions & 1 deletion cargo/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Common definitions for the `@rules_rust//cargo` package"""
"""# Cargo
Common definitions for the `@rules_rust//cargo` package
"""

load(
"//cargo/private:cargo_bootstrap.bzl",
Expand Down
3 changes: 0 additions & 3 deletions cargo/private/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,6 @@ cargo_build_script = rule(
"_cargo_manifest_dir_filename_suffixes_to_retain": attr.label(
default = Label("//cargo/settings:cargo_manifest_dir_filename_suffixes_to_retain"),
),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_debug_std_streams_output_group": attr.label(
default = Label("//cargo/settings:debug_std_streams_output_group"),
),
Expand Down
59 changes: 24 additions & 35 deletions cargo/settings/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_list_flag")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(
":settings.bzl",
"cargo_manifest_dir_filename_suffixes_to_retain",
"debug_std_streams_output_group",
"experimental_symlink_execroot",
"incompatible_runfiles_cargo_manifest_dir",
"use_default_shell_env",
)

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

# A flag for which causes `cargo_build_script` to symlink the execroot of the action to
# the `CARGO_MANIFEST_DIR` where the scripts are run.
bool_flag(
name = "experimental_symlink_execroot",
build_setting_default = False,
)
exports_files(["settings.bzl"])

# A flag which causes `cargo_build_script` to write an explicit `CARGO_MANFIEST_DIR`
# directory from an action instead of using runfiles directories which cannot be
# passed to downstream actions.
# https://github.com/bazelbuild/bazel/issues/15486
bool_flag(
name = "incompatible_runfiles_cargo_manifest_dir",
build_setting_default = True,
)

# A flag which determines what files are retained in `CARGO_MANIFEST_DIR` directories
# that are created in `CargoBuildScriptRun` actions.
string_list_flag(
name = "cargo_manifest_dir_filename_suffixes_to_retain",
build_setting_default = [
".lib",
".so",
bzl_library(
name = "bzl_lib",
srcs = glob(["*.bzl"]),
visibility = ["//cargo:__pkg__"],
deps = [
"@bazel_skylib//rules:common_settings",
],
)

# A flag which adds a `streams` output group to `cargo_build_script` targets that contain
# the raw `stderr` and `stdout` streams from the build script.
bool_flag(
name = "debug_std_streams_output_group",
build_setting_default = False,
)
cargo_manifest_dir_filename_suffixes_to_retain()

# A flag which controls the global default of `ctx.actions.run.use_default_shell_env`
# for `cargo_build_script` targets.
bool_flag(
name = "use_default_shell_env",
build_setting_default = True,
)
debug_std_streams_output_group()

experimental_symlink_execroot()

incompatible_runfiles_cargo_manifest_dir()

use_default_shell_env()
56 changes: 56 additions & 0 deletions cargo/settings/settings.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""# Cargo settings
Definitions for all `@rules_rust//cargo` settings
"""

load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_list_flag")

def experimental_symlink_execroot():
"""A flag for which causes `cargo_build_script` to symlink the execroot of the action to \
the `CARGO_MANIFEST_DIR` where the scripts are run.
"""
bool_flag(
name = "experimental_symlink_execroot",
build_setting_default = False,
)

def incompatible_runfiles_cargo_manifest_dir():
"""A flag which causes `cargo_build_script` to write an explicit `CARGO_MANFIEST_DIR` \
directory from an action instead of using runfiles directories which cannot be \
passed to downstream actions.
https://github.com/bazelbuild/bazel/issues/15486
"""
bool_flag(
name = "incompatible_runfiles_cargo_manifest_dir",
build_setting_default = True,
)

def cargo_manifest_dir_filename_suffixes_to_retain():
"""A flag which determines what files are retained in `CARGO_MANIFEST_DIR` directories \
that are created in `CargoBuildScriptRun` actions.
"""
string_list_flag(
name = "cargo_manifest_dir_filename_suffixes_to_retain",
build_setting_default = [
".lib",
".so",
],
)

def debug_std_streams_output_group():
"""A flag which adds a `streams` output group to `cargo_build_script` targets that contain \
the raw `stderr` and `stdout` streams from the build script.
"""
bool_flag(
name = "debug_std_streams_output_group",
build_setting_default = False,
)

def use_default_shell_env():
"""A flag which controls the global default of `ctx.actions.run.use_default_shell_env` for `cargo_build_script` targets.
"""
bool_flag(
name = "use_default_shell_env",
build_setting_default = True,
)
11 changes: 9 additions & 2 deletions crate_universe/src/cli/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
// Write the outputs to disk
write_outputs(normalized_outputs, opt.dry_run)?;

let splicing_manifest = SplicingManifest::try_from_path(&opt.splicing_manifest)?;

write_paths_to_track(
&opt.paths_to_track,
&opt.warnings_output_path,
splicing_manifest.manifests.keys().cloned(),
context
.crates
.values()
Expand Down Expand Up @@ -164,9 +167,12 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
&opt.nonhermetic_root_bazel_workspace_dir,
)?;

let splicing_manifest = SplicingManifest::try_from_path(&opt.splicing_manifest)?;

write_paths_to_track(
&opt.paths_to_track,
&opt.warnings_output_path,
splicing_manifest.manifests.keys().cloned(),
annotations.lockfile.crates.values(),
cargo_lockfile.patch.unused.iter(),
)?;
Expand All @@ -189,8 +195,6 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {

// Ensure Bazel lockfiles are written to disk so future generations can be short-circuited.
if let Some(lockfile) = opt.lockfile {
let splicing_manifest = SplicingManifest::try_from_path(&opt.splicing_manifest)?;

let lock_content =
lock_context(context, &config, &splicing_manifest, &cargo_bin, rustc_bin)?;

Expand Down Expand Up @@ -220,10 +224,12 @@ fn update_cargo_lockfile(path: &Path, cargo_lockfile: Lockfile) -> Result<()> {
fn write_paths_to_track<
'a,
SourceAnnotations: Iterator<Item = &'a SourceAnnotation>,
Paths: Iterator<Item = Utf8PathBuf>,
UnusedPatches: Iterator<Item = &'a cargo_lock::Dependency>,
>(
output_file: &Path,
warnings_output_path: &Path,
manifests: Paths,
source_annotations: SourceAnnotations,
unused_patches: UnusedPatches,
) -> Result<()> {
Expand All @@ -235,6 +241,7 @@ fn write_paths_to_track<
None
}
})
.chain(manifests)
.collect();
std::fs::write(
output_file,
Expand Down
1 change: 1 addition & 0 deletions crate_universe/src/metadata/cargo_tree_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl TreeResolver {
// host triple instead of the host triple detected by rustc.
.env("RUSTC_WRAPPER", rustc_wrapper)
.env("HOST_TRIPLE", host_triple)
.env("CARGO_CACHE_RUSTC_INFO", "0")
.current_dir(manifest_path.parent().expect("All manifests should have a valid parent."))
.arg("tree")
.arg("--manifest-path")
Expand Down
22 changes: 15 additions & 7 deletions crate_universe/tests/cargo_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,24 @@ fn host_specific_build_deps() {
}

let r = runfiles::Runfiles::create().unwrap();

let src_cargo_toml = runfiles::rlocation!(
r,
"rules_rust/crate_universe/test_data/metadata/host_specific_build_deps/Cargo.toml"
)
.unwrap();

// Put Cargo.toml into writable directory structure and create target/ directory to verify that
// cargo does not incorrectly cache rustc info in target/.rustc_info.json file.
let scratch = tempfile::tempdir().unwrap();
let cargo_toml = scratch.path().join("Cargo.toml");
fs::copy(src_cargo_toml, &cargo_toml).unwrap();
fs::create_dir(scratch.path().join("target")).unwrap();

let metadata = run(
"host_specific_build_deps",
HashMap::from([(
runfiles::rlocation!(
r,
"rules_rust/crate_universe/test_data/metadata/host_specific_build_deps/Cargo.toml"
)
.unwrap()
.to_string_lossy()
.to_string(),
cargo_toml.to_string_lossy().to_string(),
"//:test_input".to_string(),
)]),
"rules_rust/crate_universe/test_data/metadata/host_specific_build_deps/Cargo.lock",
Expand Down
Loading

0 comments on commit 6475fa9

Please sign in to comment.