Skip to content

Commit

Permalink
Auto merge of #12201 - hi-rustin:rustin-patch-build-script, r=epage
Browse files Browse the repository at this point in the history
Extend the build directive syntax with `cargo::`
  • Loading branch information
bors committed Dec 24, 2023
2 parents 74ef21b + dec9e8c commit d45969a
Show file tree
Hide file tree
Showing 20 changed files with 577 additions and 291 deletions.
215 changes: 167 additions & 48 deletions src/cargo/core/compiler/custom_build.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/cargo/core/compiler/fingerprint/dirty_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl DirtyReason {
DirtyReason::LocalLengthsChanged => {
s.dirty_because(unit, "the local lengths changed")?;
s.note(
"This could happen because of added/removed `cargo:rerun-if` instructions in the build script",
"This could happen because of added/removed `cargo::rerun-if` instructions in the build script",
)?;

Ok(())
Expand Down
44 changes: 22 additions & 22 deletions src/doc/src/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Cargo has no way right now of after-the-fact debugging "why was that rebuilt?"

Some issues we've seen historically which can cause crates to get rebuilt are:

* A build script prints `cargo:rerun-if-changed=foo` where `foo` is a file that
* A build script prints `cargo::rerun-if-changed=foo` where `foo` is a file that
doesn't exist and nothing generates it. In this case Cargo will keep running
the build script thinking it will generate the file but nothing ever does. The
fix is to avoid printing `rerun-if-changed` in this scenario.
Expand Down Expand Up @@ -280,38 +280,38 @@ issue](https://github.com/rust-lang/cargo/issues/new)!
Have you seen the error message above?

This is one of the most annoying error message for Cargo users. There are several
situations may lead us to a version conflict. Below we'll walk through possible
This is one of the most annoying error message for Cargo users. There are several
situations may lead us to a version conflict. Below we'll walk through possible
causes and provide diagnostic techniques to help you out there:

- The project and its dependencies use [links] to repeatedly link the local
library. Cargo forbids linking two packages with the same native library, so
even with multiple layers of dependencies it is not allowed. In this case, the
error message will prompt: `Only one package in the dependency graph may specify
the same links value`, you may need to manually check and delete duplicate link
- The project and its dependencies use [links] to repeatedly link the local
library. Cargo forbids linking two packages with the same native library, so
even with multiple layers of dependencies it is not allowed. In this case, the
error message will prompt: `Only one package in the dependency graph may specify
the same links value`, you may need to manually check and delete duplicate link
values. The community also have [conventions in place] to alleviate this.

- When depending on different crates in the project, if these crates use the same
dependent library, but the version used is restricted, making it impossible to
determine the correct version, it will also cause conflicts. The error message
will prompt: `all possible versions conflict with previously selected packages`.
- When depending on different crates in the project, if these crates use the same
dependent library, but the version used is restricted, making it impossible to
determine the correct version, it will also cause conflicts. The error message
will prompt: `all possible versions conflict with previously selected packages`.
You may need to modify the version requirements to make them consistent.

- If there are multiple versions of dependencies in the project, when using
[`direct-minimal-versions`], the minimum version requirements cannot be met,
- If there are multiple versions of dependencies in the project, when using
[`direct-minimal-versions`], the minimum version requirements cannot be met,
which will cause conflicts. You may need to modify version requirements of your
direct dependencies to meet the minimum SemVer version accordingly.

- If the dependent crate does not have the features you choose, it will also
cause conflicts. At this time, you need to check the dependent version and its
- If the dependent crate does not have the features you choose, it will also
cause conflicts. At this time, you need to check the dependent version and its
features.

- Conflicts may occur when merging branches or PRs, if there are non-trivial
conflicts, you can reset all "yours" changes, fix all other conflicts in the
branch, and then run some cargo command (like `cargo tree` or `cargo check`),
which should re-update the lockfile with your own local changes. If you previously
ran some `cargo update` commands in your branch, you can re-run them that this
time. The community has been looking to resolve merge conflicts with `Cargo.lock`
- Conflicts may occur when merging branches or PRs, if there are non-trivial
conflicts, you can reset all "yours" changes, fix all other conflicts in the
branch, and then run some cargo command (like `cargo tree` or `cargo check`),
which should re-update the lockfile with your own local changes. If you previously
ran some `cargo update` commands in your branch, you can re-run them that this
time. The community has been looking to resolve merge conflicts with `Cargo.lock`
and `Cargo.toml` using a [custom merge tool].


Expand Down
32 changes: 16 additions & 16 deletions src/doc/src/reference/build-script-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn main() {
}
"
).unwrap();
println!("cargo:rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=build.rs");
}
```

Expand Down Expand Up @@ -173,9 +173,9 @@ fn main() {
.current_dir(&Path::new(&out_dir))
.status().unwrap();
println!("cargo:rustc-link-search=native={}", out_dir);
println!("cargo:rustc-link-lib=static=hello");
println!("cargo:rerun-if-changed=src/hello.c");
println!("cargo::rustc-link-search=native={}", out_dir);
println!("cargo::rustc-link-lib=static=hello");
println!("cargo::rerun-if-changed=src/hello.c");
}
```

Expand Down Expand Up @@ -214,7 +214,7 @@ fn main() {
cc::Build::new()
.file("src/hello.c")
.compile("hello");
println!("cargo:rerun-if-changed=src/hello.c");
println!("cargo::rerun-if-changed=src/hello.c");
}
```

Expand Down Expand Up @@ -316,7 +316,7 @@ The build script is fairly simple:
fn main() {
pkg_config::Config::new().probe("zlib").unwrap();
println!("cargo:rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=build.rs");
}
```

Expand Down Expand Up @@ -344,9 +344,9 @@ Run `cargo build -vv` to see the output from the build script. On a system
with `libz` already installed, it may look something like this:

```text
[libz-sys 0.1.0] cargo:rustc-link-search=native=/usr/lib
[libz-sys 0.1.0] cargo:rustc-link-lib=z
[libz-sys 0.1.0] cargo:rerun-if-changed=build.rs
[libz-sys 0.1.0] cargo::rustc-link-search=native=/usr/lib
[libz-sys 0.1.0] cargo::rustc-link-lib=z
[libz-sys 0.1.0] cargo::rerun-if-changed=build.rs
```

Nice! `pkg-config` did all the work of finding the library and telling Cargo
Expand Down Expand Up @@ -408,7 +408,7 @@ fn main() {
cfg.include(include);
}
cfg.compile("zuser");
println!("cargo:rerun-if-changed=src/zuser.c");
println!("cargo::rerun-if-changed=src/zuser.c");
}
```

Expand Down Expand Up @@ -440,7 +440,7 @@ script looks something [like
this](https://github.com/sfackler/rust-openssl/blob/dc72a8e2c429e46c275e528b61a733a66e7877fc/openssl-sys/build/main.rs#L216):

```rust,ignore
println!("cargo:version_number={:x}", openssl_version);
println!("cargo::version_number={:x}", openssl_version);
```

This instruction causes the `DEP_OPENSSL_VERSION_NUMBER` environment variable
Expand All @@ -460,19 +460,19 @@ if let Ok(version) = env::var("DEP_OPENSSL_VERSION_NUMBER") {
let version = u64::from_str_radix(&version, 16).unwrap();
if version >= 0x1_00_01_00_0 {
println!("cargo:rustc-cfg=ossl101");
println!("cargo::rustc-cfg=ossl101");
}
if version >= 0x1_00_02_00_0 {
println!("cargo:rustc-cfg=ossl102");
println!("cargo::rustc-cfg=ossl102");
}
if version >= 0x1_01_00_00_0 {
println!("cargo:rustc-cfg=ossl110");
println!("cargo::rustc-cfg=ossl110");
}
if version >= 0x1_01_00_07_0 {
println!("cargo:rustc-cfg=ossl110g");
println!("cargo::rustc-cfg=ossl110g");
}
if version >= 0x1_01_01_00_0 {
println!("cargo:rustc-cfg=ossl111");
println!("cargo::rustc-cfg=ossl111");
}
}
```
Expand Down
Loading

0 comments on commit d45969a

Please sign in to comment.