You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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"}
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.
0 commit comments