1 use std
::collections
::{BTreeMap, HashMap}
;
3 use std
::hash
::{Hash, Hasher}
;
4 use std
::path
::{Path, PathBuf}
;
12 use crate::core
::interning
::InternedString
;
13 use crate::core
::profiles
::Profiles
;
14 use crate::core
::{Dependency, PackageId, PackageIdSpec, SourceId, Summary}
;
15 use crate::core
::{Edition, Feature, Features, WorkspaceConfig}
;
16 use crate::util
::errors
::*;
17 use crate::util
::toml
::TomlManifest
;
18 use crate::util
::{short_hash, Config, Filesystem}
;
20 pub enum EitherManifest
{
22 Virtual(VirtualManifest
),
25 /// Contains all the information about a package, as loaded from a `Cargo.toml`.
26 #[derive(Clone, Debug)]
30 links
: Option
<String
>,
34 metadata
: ManifestMetadata
,
35 custom_metadata
: Option
<toml
::Value
>,
37 publish
: Option
<Vec
<String
>>,
38 publish_lockfile
: bool
,
39 replace
: Vec
<(PackageIdSpec
, Dependency
)>,
40 patch
: HashMap
<Url
, Vec
<Dependency
>>,
41 workspace
: WorkspaceConfig
,
42 original
: Rc
<TomlManifest
>,
45 im_a_teapot
: Option
<bool
>,
46 default_run
: Option
<String
>,
47 metabuild
: Option
<Vec
<String
>>,
50 /// When parsing `Cargo.toml`, some warnings should silenced
51 /// if the manifest comes from a dependency. `ManifestWarning`
52 /// allows this delayed emission of warnings.
53 #[derive(Clone, Debug)]
54 pub struct DelayedWarning
{
56 pub is_critical
: bool
,
59 #[derive(Clone, Debug)]
60 pub struct Warnings(Vec
<DelayedWarning
>);
62 #[derive(Clone, Debug)]
63 pub struct VirtualManifest
{
64 replace
: Vec
<(PackageIdSpec
, Dependency
)>,
65 patch
: HashMap
<Url
, Vec
<Dependency
>>,
66 workspace
: WorkspaceConfig
,
72 /// General metadata about a package which is just blindly uploaded to the
75 /// Note that many of these fields can contain invalid values such as the
76 /// homepage, repository, documentation, or license. These fields are not
77 /// validated by cargo itself, but rather it is up to the registry when uploaded
78 /// to validate these fields. Cargo will itself accept any valid TOML
79 /// specification for these values.
80 #[derive(PartialEq, Clone, Debug)]
81 pub struct ManifestMetadata
{
82 pub authors
: Vec
<String
>,
83 pub keywords
: Vec
<String
>,
84 pub categories
: Vec
<String
>,
85 pub license
: Option
<String
>,
86 pub license_file
: Option
<String
>,
87 pub description
: Option
<String
>, // Not in Markdown
88 pub readme
: Option
<String
>, // File, not contents
89 pub homepage
: Option
<String
>, // URL
90 pub repository
: Option
<String
>, // URL
91 pub documentation
: Option
<String
>, // URL
92 pub badges
: BTreeMap
<String
, BTreeMap
<String
, String
>>,
93 pub links
: Option
<String
>,
96 #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
106 /// Returns the argument suitable for `--crate-type` to pass to rustc.
107 pub fn crate_type(&self) -> &str {
109 LibKind
::Lib
=> "lib",
110 LibKind
::Rlib
=> "rlib",
111 LibKind
::Dylib
=> "dylib",
112 LibKind
::ProcMacro
=> "proc-macro",
113 LibKind
::Other(ref s
) => s
,
117 pub fn linkable(&self) -> bool
{
119 LibKind
::Lib
| LibKind
::Rlib
| LibKind
::Dylib
| LibKind
::ProcMacro
=> true,
120 LibKind
::Other(..) => false,
124 pub fn requires_upstream_objects(&self) -> bool
{
126 // "lib" == "rlib" and is a compilation that doesn't actually
127 // require upstream object files to exist, only upstream metadata
128 // files. As a result, it doesn't require upstream artifacts
129 LibKind
::Lib
| LibKind
::Rlib
=> false,
131 // Everything else, however, is some form of "linkable output" or
132 // something that requires upstream object files.
138 impl fmt
::Debug
for LibKind
{
139 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
140 self.crate_type().fmt(f
)
144 impl<'a
> From
<&'a String
> for LibKind
{
145 fn from(string
: &'a String
) -> Self {
146 match string
.as_ref() {
147 "lib" => LibKind
::Lib
,
148 "rlib" => LibKind
::Rlib
,
149 "dylib" => LibKind
::Dylib
,
150 "proc-macro" => LibKind
::ProcMacro
,
151 s
=> LibKind
::Other(s
.to_string()),
156 #[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
157 pub enum TargetKind
{
162 ExampleLib(Vec
<LibKind
>),
167 impl ser
::Serialize
for TargetKind
{
168 fn serialize
<S
>(&self, s
: S
) -> Result
<S
::Ok
, S
::Error
>
172 use self::TargetKind
::*;
174 Lib(ref kinds
) => s
.collect_seq(kinds
.iter().map(LibKind
::crate_type
)),
175 Bin
=> ["bin"].serialize(s
),
176 ExampleBin
| ExampleLib(_
) => ["example"].serialize(s
),
177 Test
=> ["test"].serialize(s
),
178 CustomBuild
=> ["custom-build"].serialize(s
),
179 Bench
=> ["bench"].serialize(s
),
184 impl fmt
::Debug
for TargetKind
{
185 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
186 use self::TargetKind
::*;
188 Lib(ref kinds
) => kinds
.fmt(f
),
190 ExampleBin
| ExampleLib(_
) => "example".fmt(f
),
191 Test
=> "test".fmt(f
),
192 CustomBuild
=> "custom-build".fmt(f
),
193 Bench
=> "bench".fmt(f
),
199 pub fn description(&self) -> &'
static str {
201 TargetKind
::Lib(..) => "lib",
202 TargetKind
::Bin
=> "bin",
203 TargetKind
::Test
=> "integration-test",
204 TargetKind
::ExampleBin
| TargetKind
::ExampleLib(..) => "example",
205 TargetKind
::Bench
=> "bench",
206 TargetKind
::CustomBuild
=> "build-script",
210 /// Returns whether production of this artifact requires the object files
211 /// from dependencies to be available.
213 /// This only returns `false` when all we're producing is an rlib, otherwise
214 /// it will return `true`.
215 pub fn requires_upstream_objects(&self) -> bool
{
217 TargetKind
::Lib(kinds
) | TargetKind
::ExampleLib(kinds
) => {
218 kinds
.iter().any(|k
| k
.requires_upstream_objects())
225 /// Information about a binary, a library, an example, etc. that is part of the
227 #[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
231 // Note that the `src_path` here is excluded from the `Hash` implementation
232 // as it's absolute currently and is otherwise a little too brittle for
233 // causing rebuilds. Instead the hash for the path that we send to the
234 // compiler is handled elsewhere.
235 src_path
: TargetSourcePath
,
236 required_features
: Option
<Vec
<String
>>,
241 harness
: bool
, // whether to use the test harness (--test)
247 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
248 pub enum TargetSourcePath
{
253 impl TargetSourcePath
{
254 pub fn path(&self) -> Option
<&Path
> {
256 TargetSourcePath
::Path(path
) => Some(path
.as_ref()),
257 TargetSourcePath
::Metabuild
=> None
,
261 pub fn is_path(&self) -> bool
{
263 TargetSourcePath
::Path(_
) => true,
269 impl Hash
for TargetSourcePath
{
270 fn hash
<H
: Hasher
>(&self, _
: &mut H
) {
275 impl fmt
::Debug
for TargetSourcePath
{
276 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
278 TargetSourcePath
::Path(path
) => path
.fmt(f
),
279 TargetSourcePath
::Metabuild
=> "metabuild".fmt(f
),
284 impl From
<PathBuf
> for TargetSourcePath
{
285 fn from(path
: PathBuf
) -> Self {
286 assert
!(path
.is_absolute(), "`{}` is not absolute", path
.display());
287 TargetSourcePath
::Path(path
)
292 struct SerializedTarget
<'a
> {
293 /// Is this a `--bin bin`, `--lib`, `--example ex`?
294 /// Serialized as a list of strings for historical reasons.
295 kind
: &'a TargetKind
,
296 /// Corresponds to `--crate-type` compiler attribute.
297 /// See https://doc.rust-lang.org/reference/linkage.html
298 crate_types
: Vec
<&'a
str>,
300 src_path
: Option
<&'a PathBuf
>,
302 #[serde(rename = "required-features", skip_serializing_if = "Option::is_none")]
303 required_features
: Option
<Vec
<&'a
str>>,
307 impl ser
::Serialize
for Target
{
308 fn serialize
<S
: ser
::Serializer
>(&self, s
: S
) -> Result
<S
::Ok
, S
::Error
> {
309 let src_path
= match &self.src_path
{
310 TargetSourcePath
::Path(p
) => Some(p
),
311 // Unfortunately getting the correct path would require access to
312 // target_dir, which is not available here.
313 TargetSourcePath
::Metabuild
=> None
,
317 crate_types
: self.rustc_crate_types(),
320 edition
: &self.edition
.to_string(),
321 required_features
: self
324 .map(|rf
| rf
.iter().map(|s
| &**s
).collect()),
325 doctest
: self.doctest
&& self.doctestable(),
332 impl fmt
::Debug
for Target
{
333 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
334 let (default, default_name
) = {
336 TargetKind
::Lib(kinds
) => {
341 self.src_path().path().unwrap().to_path_buf(),
344 format
!("lib_target({:?}, {:?}, {:?}, {:?})",
345 self.name
, kinds
, self.src_path
, self.edition
),
348 TargetKind
::CustomBuild
=> {
349 match self.src_path
{
350 TargetSourcePath
::Path(ref path
) => {
352 Target
::custom_build_target(
357 format
!("custom_build_target({:?}, {:?}, {:?})",
358 self.name
, path
, self.edition
),
361 TargetSourcePath
::Metabuild
=> {
363 Target
::metabuild_target(&self.name
),
364 format
!("metabuild_target({:?})", self.name
),
370 Target
::new(self.src_path
.clone(), self.edition
),
371 format
!("with_path({:?}, {:?})", self.src_path
, self.edition
),
396 targets
: Vec
<Target
>,
397 exclude
: Vec
<String
>,
398 include
: Vec
<String
>,
399 links
: Option
<String
>,
400 metadata
: ManifestMetadata
,
401 custom_metadata
: Option
<toml
::Value
>,
403 publish
: Option
<Vec
<String
>>,
404 publish_lockfile
: bool
,
405 replace
: Vec
<(PackageIdSpec
, Dependency
)>,
406 patch
: HashMap
<Url
, Vec
<Dependency
>>,
407 workspace
: WorkspaceConfig
,
410 im_a_teapot
: Option
<bool
>,
411 default_run
: Option
<String
>,
412 original
: Rc
<TomlManifest
>,
413 metabuild
: Option
<Vec
<String
>>,
418 warnings
: Warnings
::new(),
439 pub fn dependencies(&self) -> &[Dependency
] {
440 self.summary
.dependencies()
442 pub fn exclude(&self) -> &[String
] {
445 pub fn include(&self) -> &[String
] {
448 pub fn metadata(&self) -> &ManifestMetadata
{
451 pub fn name(&self) -> InternedString
{
452 self.package_id().name()
454 pub fn package_id(&self) -> PackageId
{
455 self.summary
.package_id()
457 pub fn summary(&self) -> &Summary
{
460 pub fn summary_mut(&mut self) -> &mut Summary
{
463 pub fn targets(&self) -> &[Target
] {
466 pub fn version(&self) -> &Version
{
467 self.package_id().version()
469 pub fn warnings_mut(&mut self) -> &mut Warnings
{
472 pub fn warnings(&self) -> &Warnings
{
475 pub fn profiles(&self) -> &Profiles
{
478 pub fn publish(&self) -> &Option
<Vec
<String
>> {
481 pub fn replace(&self) -> &[(PackageIdSpec
, Dependency
)] {
484 pub fn original(&self) -> &TomlManifest
{
487 pub fn patch(&self) -> &HashMap
<Url
, Vec
<Dependency
>> {
490 pub fn links(&self) -> Option
<&str> {
491 self.links
.as_ref().map(|s
| &s
[..])
494 pub fn workspace_config(&self) -> &WorkspaceConfig
{
498 pub fn features(&self) -> &Features
{
502 pub fn map_source(self, to_replace
: SourceId
, replace_with
: SourceId
) -> Manifest
{
504 summary
: self.summary
.map_source(to_replace
, replace_with
),
509 pub fn feature_gate(&self) -> CargoResult
<()> {
510 if self.im_a_teapot
.is_some() {
512 .require(Feature
::test_dummy_unstable())
514 failure
::format_err
!(
515 "the `im-a-teapot` manifest key is unstable and may \
516 not work properly in England"
524 // Just a helper function to test out `-Z` flags on Cargo
525 pub fn print_teapot(&self, config
: &Config
) {
526 if let Some(teapot
) = self.im_a_teapot
{
527 if config
.cli_unstable().print_im_a_teapot
{
528 println
!("im-a-teapot = {}", teapot
);
533 pub fn edition(&self) -> Edition
{
537 pub fn custom_metadata(&self) -> Option
<&toml
::Value
> {
538 self.custom_metadata
.as_ref()
541 pub fn default_run(&self) -> Option
<&str> {
542 self.default_run
.as_ref().map(|s
| &s
[..])
545 pub fn metabuild(&self) -> Option
<&Vec
<String
>> {
546 self.metabuild
.as_ref()
549 pub fn metabuild_path(&self, target_dir
: Filesystem
) -> PathBuf
{
550 let hash
= short_hash(&self.package_id());
552 .into_path_unlocked()
554 .join(format
!("metabuild-{}-{}.rs", self.name(), hash
))
558 impl VirtualManifest
{
560 replace
: Vec
<(PackageIdSpec
, Dependency
)>,
561 patch
: HashMap
<Url
, Vec
<Dependency
>>,
562 workspace
: WorkspaceConfig
,
565 ) -> VirtualManifest
{
571 warnings
: Warnings
::new(),
576 pub fn replace(&self) -> &[(PackageIdSpec
, Dependency
)] {
580 pub fn patch(&self) -> &HashMap
<Url
, Vec
<Dependency
>> {
584 pub fn workspace_config(&self) -> &WorkspaceConfig
{
588 pub fn profiles(&self) -> &Profiles
{
592 pub fn warnings_mut(&mut self) -> &mut Warnings
{
596 pub fn warnings(&self) -> &Warnings
{
600 pub fn features(&self) -> &Features
{
606 fn new(src_path
: TargetSourcePath
, edition
: Edition
) -> Target
{
608 kind
: TargetKind
::Bin
,
611 required_features
: None
,
623 fn with_path(src_path
: PathBuf
, edition
: Edition
) -> Target
{
624 Target
::new(TargetSourcePath
::from(src_path
), edition
)
629 crate_targets
: Vec
<LibKind
>,
634 kind
: TargetKind
::Lib(crate_targets
),
635 name
: name
.to_string(),
638 ..Target
::with_path(src_path
, edition
)
645 required_features
: Option
<Vec
<String
>>,
649 kind
: TargetKind
::Bin
,
650 name
: name
.to_string(),
653 ..Target
::with_path(src_path
, edition
)
657 /// Builds a `Target` corresponding to the `build = "build.rs"` entry.
658 pub fn custom_build_target(name
: &str, src_path
: PathBuf
, edition
: Edition
) -> Target
{
660 kind
: TargetKind
::CustomBuild
,
661 name
: name
.to_string(),
665 ..Target
::with_path(src_path
, edition
)
669 pub fn metabuild_target(name
: &str) -> Target
{
671 kind
: TargetKind
::CustomBuild
,
672 name
: name
.to_string(),
676 ..Target
::new(TargetSourcePath
::Metabuild
, Edition
::Edition2018
)
680 pub fn example_target(
682 crate_targets
: Vec
<LibKind
>,
684 required_features
: Option
<Vec
<String
>>,
687 let kind
= if crate_targets
.is_empty()
690 .all(|t
| *t
== LibKind
::Other("bin".into()))
692 TargetKind
::ExampleBin
694 TargetKind
::ExampleLib(crate_targets
)
699 name
: name
.to_string(),
703 ..Target
::with_path(src_path
, edition
)
710 required_features
: Option
<Vec
<String
>>,
714 kind
: TargetKind
::Test
,
715 name
: name
.to_string(),
718 ..Target
::with_path(src_path
, edition
)
725 required_features
: Option
<Vec
<String
>>,
729 kind
: TargetKind
::Bench
,
730 name
: name
.to_string(),
733 ..Target
::with_path(src_path
, edition
)
737 pub fn name(&self) -> &str {
740 pub fn crate_name(&self) -> String
{
741 self.name
.replace("-", "_")
743 pub fn src_path(&self) -> &TargetSourcePath
{
746 pub fn set_src_path(&mut self, src_path
: TargetSourcePath
) {
747 self.src_path
= src_path
;
749 pub fn required_features(&self) -> Option
<&Vec
<String
>> {
750 self.required_features
.as_ref()
752 pub fn kind(&self) -> &TargetKind
{
755 pub fn tested(&self) -> bool
{
758 pub fn harness(&self) -> bool
{
761 pub fn documented(&self) -> bool
{
764 pub fn for_host(&self) -> bool
{
767 pub fn proc_macro(&self) -> bool
{
770 pub fn edition(&self) -> Edition
{
773 pub fn benched(&self) -> bool
{
776 pub fn doctested(&self) -> bool
{
780 pub fn doctestable(&self) -> bool
{
782 TargetKind
::Lib(ref kinds
) => kinds
784 .any(|k
| *k
== LibKind
::Rlib
|| *k
== LibKind
::Lib
|| *k
== LibKind
::ProcMacro
),
789 pub fn allows_underscores(&self) -> bool
{
790 self.is_bin() || self.is_example() || self.is_custom_build()
793 pub fn is_lib(&self) -> bool
{
795 TargetKind
::Lib(_
) => true,
800 pub fn is_dylib(&self) -> bool
{
802 TargetKind
::Lib(ref libs
) => libs
.iter().any(|l
| *l
== LibKind
::Dylib
),
807 pub fn is_cdylib(&self) -> bool
{
808 let libs
= match self.kind
{
809 TargetKind
::Lib(ref libs
) => libs
,
812 libs
.iter().any(|l
| match *l
{
813 LibKind
::Other(ref s
) => s
== "cdylib",
818 /// Returns whether this target produces an artifact which can be linked
819 /// into a Rust crate.
821 /// This only returns true for certain kinds of libraries.
822 pub fn linkable(&self) -> bool
{
824 TargetKind
::Lib(ref kinds
) => kinds
.iter().any(|k
| k
.linkable()),
829 pub fn is_bin(&self) -> bool
{
830 self.kind
== TargetKind
::Bin
833 pub fn is_example(&self) -> bool
{
835 TargetKind
::ExampleBin
| TargetKind
::ExampleLib(..) => true,
840 /// Returns `true` if it is a binary or executable example.
841 /// NOTE: Tests are `false`!
842 pub fn is_executable(&self) -> bool
{
843 self.is_bin() || self.is_exe_example()
846 /// Returns `true` if it is an executable example.
847 pub fn is_exe_example(&self) -> bool
{
848 // Needed for --all-examples in contexts where only runnable examples make sense
850 TargetKind
::ExampleBin
=> true,
855 pub fn is_test(&self) -> bool
{
856 self.kind
== TargetKind
::Test
858 pub fn is_bench(&self) -> bool
{
859 self.kind
== TargetKind
::Bench
861 pub fn is_custom_build(&self) -> bool
{
862 self.kind
== TargetKind
::CustomBuild
865 /// Returns the arguments suitable for `--crate-type` to pass to rustc.
866 pub fn rustc_crate_types(&self) -> Vec
<&str> {
868 TargetKind
::Lib(ref kinds
) | TargetKind
::ExampleLib(ref kinds
) => {
869 kinds
.iter().map(LibKind
::crate_type
).collect()
871 TargetKind
::CustomBuild
874 | TargetKind
::ExampleBin
875 | TargetKind
::Bin
=> vec
!["bin"],
879 pub fn can_lto(&self) -> bool
{
881 TargetKind
::Lib(ref v
) => {
882 !v
.contains(&LibKind
::Rlib
)
883 && !v
.contains(&LibKind
::Dylib
)
884 && !v
.contains(&LibKind
::Lib
)
890 pub fn set_tested(&mut self, tested
: bool
) -> &mut Target
{
891 self.tested
= tested
;
894 pub fn set_benched(&mut self, benched
: bool
) -> &mut Target
{
895 self.benched
= benched
;
898 pub fn set_doctest(&mut self, doctest
: bool
) -> &mut Target
{
899 self.doctest
= doctest
;
902 pub fn set_for_host(&mut self, for_host
: bool
) -> &mut Target
{
903 self.for_host
= for_host
;
906 pub fn set_proc_macro(&mut self, proc_macro
: bool
) -> &mut Target
{
907 self.proc_macro
= proc_macro
;
910 pub fn set_edition(&mut self, edition
: Edition
) -> &mut Target
{
911 self.edition
= edition
;
914 pub fn set_harness(&mut self, harness
: bool
) -> &mut Target
{
915 self.harness
= harness
;
918 pub fn set_doc(&mut self, doc
: bool
) -> &mut Target
{
924 impl fmt
::Display
for Target
{
925 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
927 TargetKind
::Lib(..) => write
!(f
, "Target(lib)"),
928 TargetKind
::Bin
=> write
!(f
, "Target(bin: {})", self.name
),
929 TargetKind
::Test
=> write
!(f
, "Target(test: {})", self.name
),
930 TargetKind
::Bench
=> write
!(f
, "Target(bench: {})", self.name
),
931 TargetKind
::ExampleBin
| TargetKind
::ExampleLib(..) => {
932 write
!(f
, "Target(example: {})", self.name
)
934 TargetKind
::CustomBuild
=> write
!(f
, "Target(script)"),
940 fn new() -> Warnings
{
944 pub fn add_warning(&mut self, s
: String
) {
945 self.0.push(DelayedWarning
{
951 pub fn add_critical_warning(&mut self, s
: String
) {
952 self.0.push(DelayedWarning
{
958 pub fn warnings(&self) -> &[DelayedWarning
] {