Skip to content

Commit 2c029c1

Browse files
authored
Merge branch 'main' into zbarsky/bazel9
2 parents a1a9447 + af24d27 commit 2c029c1

File tree

55 files changed

+2343
-102
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2343
-102
lines changed

.bazelversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.4.2

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
This repository provides rules for building [Rust](https://www.rust-lang.org/) projects with [Bazel](https://bazel.build/).
88

9+
### Starter repo
10+
11+
The fastest way to try this in an empty project is to click the green "Use this template" button on https://github.com/bazel-starters/rust.
12+
913
## Community
1014

1115
General discussions and announcements take place in the [GitHub Discussions](https://github.com/bazelbuild/rules_rust/discussions), but there are

cargo/private/cargo_build_script.bzl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,22 @@ def _cargo_build_script_impl(ctx):
420420
env["LDFLAGS"] = " ".join(_pwd_flags(link_args))
421421

422422
# Defaults for cxx flags.
423-
env["CC"] = "${{pwd}}/{}".format(ctx.executable._fallback_cc.path)
424-
env["CXX"] = "${{pwd}}/{}".format(ctx.executable._fallback_cxx.path)
425-
env["AR"] = "${{pwd}}/{}".format(ctx.executable._fallback_ar.path)
426423
env["ARFLAGS"] = ""
427424
env["CFLAGS"] = ""
428425
env["CXXFLAGS"] = ""
426+
fallback_tools = []
427+
if not cc_toolchain:
428+
fallbacks = {
429+
"AR": "_fallback_ar",
430+
"CC": "_fallback_cc",
431+
"CXX": "_fallback_cxx",
432+
}
433+
for key, attr in fallbacks.items():
434+
tool = getattr(ctx.executable, attr)
435+
if not tool:
436+
fail("cargo_build_script without a cc toolchain requires %s" % attr)
437+
fallback_tools.append(tool)
438+
env[key] = "${{pwd}}/{}".format(tool.path)
429439

430440
if cc_toolchain:
431441
# MSVC requires INCLUDE to be set
@@ -516,10 +526,7 @@ def _cargo_build_script_impl(ctx):
516526
direct = [
517527
script,
518528
ctx.executable._cargo_build_script_runner,
519-
ctx.executable._fallback_cc,
520-
ctx.executable._fallback_cxx,
521-
ctx.executable._fallback_ar,
522-
] + ([toolchain.target_json] if toolchain.target_json else []),
529+
] + fallback_tools + ([toolchain.target_json] if toolchain.target_json else []),
523530
transitive = script_data + script_tools + toolchain_tools,
524531
)
525532

@@ -744,16 +751,19 @@ cargo_build_script = rule(
744751
"_fallback_ar": attr.label(
745752
cfg = "exec",
746753
executable = True,
754+
allow_files = True,
747755
default = Label("//cargo/private:no_ar"),
748756
),
749757
"_fallback_cc": attr.label(
750758
cfg = "exec",
751759
executable = True,
760+
allow_files = True,
752761
default = Label("//cargo/private:no_cc"),
753762
),
754763
"_fallback_cxx": attr.label(
755764
cfg = "exec",
756765
executable = True,
766+
allow_files = True,
757767
default = Label("//cargo/private:no_cxx"),
758768
),
759769
"_incompatible_runfiles_cargo_manifest_dir": attr.label(

crate_universe/extensions.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bazel_dep(name = "rules_rust", version = "0.68.1")
3232
You find the latest version on the [release page](https://github.com/bazelbuild/rules_rust/releases).
3333
3434
35-
After adding `rules_rust` in your `MODULE.bazel, set the following to begin using `crate_universe`:
35+
After adding `rules_rust` in your `MODULE.bazel`, set the following to begin using `crate_universe`:
3636
3737
```python
3838
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")

crate_universe/src/config.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,13 +706,34 @@ pub(crate) struct Config {
706706
pub(crate) rendering: RenderConfig,
707707

708708
/// The contents of a Cargo configuration file
709+
#[serde(default, deserialize_with = "deserialize_cargo_config")]
709710
pub(crate) cargo_config: Option<toml::Value>,
710711

711712
/// A set of platform triples to use in generated select statements
712713
#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
713714
pub(crate) supported_platform_triples: BTreeSet<TargetTriple>,
714715
}
715716

717+
// rules_rust/crate_universe/private/generate_utils.bzl:generate_config
718+
// injects the cargo_config as a literal JSON string, this causes line ending
719+
// instability in Unix vs Windows. Parsing it here fixes any cross platform differences.
720+
fn deserialize_cargo_config<'de, D>(deserializer: D) -> Result<Option<toml::Value>, D::Error>
721+
where
722+
D: serde::Deserializer<'de>,
723+
{
724+
let value: Option<serde_json::Value> = Option::deserialize(deserializer)?;
725+
match value {
726+
None | Some(serde_json::Value::Null) => Ok(None),
727+
Some(serde_json::Value::String(s)) => s
728+
.parse::<toml::Value>()
729+
.map(Some)
730+
.map_err(serde::de::Error::custom),
731+
Some(other) => serde_json::from_value(other)
732+
.map(Some)
733+
.map_err(serde::de::Error::custom),
734+
}
735+
}
736+
716737
impl Config {
717738
pub(crate) fn try_from_path<T: AsRef<Path>>(path: T) -> Result<Self> {
718739
let data = fs::read_to_string(path)?;

crate_universe/src/lockfile.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,57 @@ mod test {
376376
digest,
377377
);
378378
}
379+
380+
#[test]
381+
fn digest_stable_with_crlf_cargo_config() {
382+
let context = Context::default();
383+
let splicing_metadata = SplicingMetadata::default();
384+
385+
let json_config = |cargo_config: &str| {
386+
serde_json::to_string(&serde_json::json!({
387+
"generate_binaries": false,
388+
"generate_build_scripts": false,
389+
"cargo_config": cargo_config,
390+
"rendering": {
391+
"repository_name": "test",
392+
"regen_command": "//test",
393+
"generate_cargo_toml_env_vars": true
394+
}
395+
}))
396+
.unwrap()
397+
};
398+
399+
let config_crlf: Config = serde_json::from_str(&json_config(
400+
"[registries.my-registry]\r\nindex = \"sparse+https://example.com/\"",
401+
))
402+
.unwrap();
403+
404+
let config_lf: Config = serde_json::from_str(&json_config(
405+
"[registries.my-registry]\nindex = \"sparse+https://example.com/\"",
406+
))
407+
.unwrap();
408+
409+
let digest_crlf = Digest::compute(
410+
&context,
411+
&config_crlf,
412+
&splicing_metadata,
413+
"0.1.0",
414+
"cargo 1.57.0 (b2e52d7ca 2021-10-21)",
415+
"rustc 1.57.0 (f1edd0429 2021-11-29)",
416+
);
417+
418+
let digest_lf = Digest::compute(
419+
&context,
420+
&config_lf,
421+
&splicing_metadata,
422+
"0.1.0",
423+
"cargo 1.57.0 (b2e52d7ca 2021-10-21)",
424+
"rustc 1.57.0 (f1edd0429 2021-11-29)",
425+
);
426+
427+
assert_eq!(
428+
digest_crlf, digest_lf,
429+
"Digests should be identical regardless of CRLF vs LF line endings in cargo_config"
430+
);
431+
}
379432
}

crate_universe/src/rendering/templates/module_bzl.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def all_crate_deps(
155155
normal (bool, optional): If True, normal dependencies are included in the
156156
output list.
157157
normal_dev (bool, optional): If True, normal dev dependencies will be
158-
included in the output list..
158+
included in the output list.
159159
proc_macro (bool, optional): If True, proc_macro dependencies are included
160160
in the output list.
161161
proc_macro_dev (bool, optional): If True, dev proc_macro dependencies are
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.4.2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.4.2

examples/android/.bazelversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.4.2

0 commit comments

Comments
 (0)