Skip to content

Commit 0e93732

Browse files
committed
Auto merge of #12110 - ehuss:bindep-docs, r=weihanglo
Add more documentation for artifact-dependencies. This adds some more detailed documentation for artifact-dependencies. Whenever I need to work with it, I look at this section and essentially not find any of the information that I am looking for. This fills it out with some of the information that I think the documentation will eventually need.
2 parents fa3fa85 + 0350e79 commit 0e93732

File tree

1 file changed

+114
-6
lines changed

1 file changed

+114
-6
lines changed

src/doc/src/reference/unstable.md

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,120 @@ the command line) target.
781781
* Tracking Issue: [#9096](https://github.com/rust-lang/cargo/pull/9096)
782782
* Original Pull Request: [#9992](https://github.com/rust-lang/cargo/pull/9992)
783783

784-
Allow Cargo packages to depend on `bin`, `cdylib`, and `staticlib` crates,
784+
Artifact dependencies allow Cargo packages to depend on `bin`, `cdylib`, and `staticlib` crates,
785785
and use the artifacts built by those crates at compile time.
786786

787787
Run `cargo` with `-Z bindeps` to enable this functionality.
788788

789-
**Example:** use _cdylib_ artifact in build script
789+
#### artifact-dependencies: Dependency declarations
790+
791+
Artifact-dependencies adds the following keys to a dependency declaration in `Cargo.toml`:
792+
793+
- `artifact` --- This specifies the [Cargo Target](cargo-targets.md) to build.
794+
Normally without this field, Cargo will only build the `[lib]` target from a dependency.
795+
This field allows specifying which target will be built, and made available as a binary at build time:
796+
797+
* `"bin"` --- Compiled executable binaries, corresponding to all of the `[[bin]]` sections in the dependency's manifest.
798+
* `"bin:<bin-name>"` --- Compiled executable binary, corresponding to a specific binary target specified by the given `<bin-name>`.
799+
* `"cdylib"` --- A C-compatible dynamic library, corresponding to a `[lib]` section with `crate-type = ["cdylib"]` in the dependency's manifest.
800+
* `"staticlib"` --- A C-compatible static library, corresponding to a `[lib]` section with `crate-type = ["staticlib"]` in the dependency's manifest.
801+
802+
The `artifact` value can be a string, or it can be an array of strings to specify multiple targets.
803+
804+
Example:
805+
806+
```toml
807+
[dependencies]
808+
bar = { version = "1.0", artifact = "staticlib" }
809+
zoo = { version = "1.0", artifact = ["bin:cat", "bin:dog"]}
810+
```
811+
812+
- `lib` --- This is a Boolean value which indicates whether or not to also build the dependency's library as a normal Rust `lib` dependency.
813+
This field can only be specified when `artifact` is specified.
814+
815+
The default for this field is `false` when `artifact` is specified.
816+
If this is set to `true`, then the dependency's `[lib]` target will also be built for the platform target the declaring package is being built for.
817+
This allows the package to use the dependency from Rust code like a normal dependency in addition to an artifact dependency.
818+
819+
Example:
820+
821+
```toml
822+
[dependencies]
823+
bar = { version = "1.0", artifact = "bin", lib = true }
824+
```
825+
826+
- `target` --- The platform target to build the dependency for.
827+
This field can only be specified when `artifact` is specified.
828+
829+
The default if this is not specified depends on the dependency kind.
830+
For build dependencies, it will be built for the host target.
831+
For all other dependencies, it will be built for the same targets the declaring package is built for.
832+
833+
For a build dependency, this can also take the special value of `"target"` which means to build the dependency for the same targets that the package is being built for.
834+
835+
```toml
836+
[build-dependencies]
837+
bar = { version = "1.0", artifact = "cdylib", target = "wasm32-unknown-unknown"}
838+
same-target = { version = "1.0", artifact = "bin", target = "target" }
839+
```
840+
841+
#### artifact-dependencies: Environment variables
842+
843+
After building an artifact dependency, Cargo provides the following environment variables that you can use to access the artifact:
844+
845+
- `CARGO_<ARTIFACT-TYPE>_DIR_<DEP>` --- This is the directory containing all the artifacts from the dependency.
846+
847+
`<ARTIFACT-TYPE>` is the `artifact` specified for the dependency (uppercased as in `CDYLIB`, `STATICLIB`, or `BIN`) and `<DEP>` is the name of the dependency.
848+
As with other Cargo environment variables, dependency names are converted to uppercase, with dashes replaced by underscores.
849+
850+
If your manifest renames the dependency, `<DEP>` corresponds to the name you specify, not the original package name.
851+
852+
- `CARGO_<ARTIFACT-TYPE>_FILE_<DEP>_<NAME>` --- This is the full path to the artifact.
853+
854+
`<ARTIFACT-TYPE>` is the `artifact` specified for the dependency (uppercased as above), `<DEP>` is the name of the dependency (transformed as above), and `<NAME>` is the name of the artifact from the dependency.
855+
856+
Note that `<NAME>` is not modified in any way from the `name` specified in the crate supplying the artifact, or the crate name if not specified; for instance, it may be in lowercase, or contain dashes.
857+
858+
For convenience, if the artifact name matches the original package name, cargo additionally supplies a copy of this variable with the `_<NAME>` suffix omitted.
859+
For instance, if the `cmake` crate supplies a binary named `cmake`, Cargo supplies both `CARGO_BIN_FILE_CMAKE` and `CARGO_BIN_FILE_CMAKE_cmake`.
860+
861+
For each kind of dependency, these variables are supplied to the same part of the build process that has access to that kind of dependency:
862+
863+
- For build-dependencies, these variables are supplied to the `build.rs` script, and can be accessed using [`std::env::var_os`](https://doc.rust-lang.org/std/env/fn.var_os.html).
864+
(As with any OS file path, these may or may not be valid UTF-8.)
865+
- For normal dependencies, these variables are supplied during the compilation of the crate, and can be accessed using the [`env!`] macro.
866+
- For dev-dependencies, these variables are supplied during the compilation of examples, tests, and benchmarks, and can be accessed using the [`env!`] macro.
867+
868+
[`env!`]: https://doc.rust-lang.org/std/macro.env.html
869+
870+
#### artifact-dependencies: Examples
871+
872+
##### Example: use a binary executable from a build script
873+
874+
In the `Cargo.toml` file, you can specify a dependency on a binary to make available for a build script:
875+
876+
```toml
877+
[build-dependencies]
878+
some-build-tool = { version = "1.0", artifact = "bin" }
879+
```
880+
881+
Then inside the build script, the binary can be executed at build time:
882+
883+
```rust
884+
fn main() {
885+
let build_tool = std::env::var_os("CARGO_BIN_FILE_SOME_BUILD_TOOL").unwrap();
886+
let status = std::process::Command::new(build_tool)
887+
.arg("do-stuff")
888+
.status()
889+
.unwrap();
890+
if !status.success() {
891+
eprintln!("failed!");
892+
std::process::exit(1);
893+
}
894+
}
895+
```
896+
897+
##### Example: use _cdylib_ artifact in build script
790898

791899
The `Cargo.toml` in the consuming package, building the `bar` library as `cdylib`
792900
for a specific build target…
@@ -800,11 +908,11 @@ bar = { artifact = "cdylib", version = "1.0", target = "wasm32-unknown-unknown"
800908

801909
```rust
802910
fn main() {
803-
wasm::run_file(std::env::var("CARGO_CDYLIB_FILE_BAR").unwrap());
911+
wasm::run_file(std::env::var("CARGO_CDYLIB_FILE_BAR").unwrap());
804912
}
805913
```
806914

807-
**Example:** use _binary_ artifact and its library in a binary
915+
##### Example: use _binary_ artifact and its library in a binary
808916

809917
The `Cargo.toml` in the consuming package, building the `bar` binary for inclusion
810918
as artifact while making it available as library as well…
@@ -818,8 +926,8 @@ bar = { artifact = "bin", version = "1.0", lib = true }
818926

819927
```rust
820928
fn main() {
821-
bar::init();
822-
command::run(env!("CARGO_BIN_FILE_BAR"));
929+
bar::init();
930+
command::run(env!("CARGO_BIN_FILE_BAR"));
823931
}
824932
```
825933

0 commit comments

Comments
 (0)