1 use std
::collections
::{BTreeMap, HashMap}
;
3 use std
::hash
::{Hash, Hasher}
;
4 use std
::path
::{Path, PathBuf}
;
12 use core
::interning
::InternedString
;
13 use core
::profiles
::Profiles
;
14 use core
::{Dependency, PackageId, PackageIdSpec, SourceId, Summary}
;
15 use core
::{Edition, Feature, Features, WorkspaceConfig}
;
17 use util
::toml
::TomlManifest
;
18 use 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
,
71 /// General metadata about a package which is just blindly uploaded to the
74 /// Note that many of these fields can contain invalid values such as the
75 /// homepage, repository, documentation, or license. These fields are not
76 /// validated by cargo itself, but rather it is up to the registry when uploaded
77 /// to validate these fields. Cargo will itself accept any valid TOML
78 /// specification for these values.
79 #[derive(PartialEq, Clone, Debug)]
80 pub struct ManifestMetadata
{
81 pub authors
: Vec
<String
>,
82 pub keywords
: Vec
<String
>,
83 pub categories
: Vec
<String
>,
84 pub license
: Option
<String
>,
85 pub license_file
: Option
<String
>,
86 pub description
: Option
<String
>, // not markdown
87 pub readme
: Option
<String
>, // file, not contents
88 pub homepage
: Option
<String
>, // url
89 pub repository
: Option
<String
>, // url
90 pub documentation
: Option
<String
>, // url
91 pub badges
: BTreeMap
<String
, BTreeMap
<String
, String
>>,
92 pub links
: Option
<String
>,
95 #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
105 /// Returns the argument suitable for `--crate-type` to pass to rustc.
106 pub fn crate_type(&self) -> &str {
108 LibKind
::Lib
=> "lib",
109 LibKind
::Rlib
=> "rlib",
110 LibKind
::Dylib
=> "dylib",
111 LibKind
::ProcMacro
=> "proc-macro",
112 LibKind
::Other(ref s
) => s
,
116 pub fn linkable(&self) -> bool
{
118 LibKind
::Lib
| LibKind
::Rlib
| LibKind
::Dylib
| LibKind
::ProcMacro
=> true,
119 LibKind
::Other(..) => false,
124 impl fmt
::Debug
for LibKind
{
125 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
126 self.crate_type().fmt(f
)
130 impl<'a
> From
<&'a String
> for LibKind
{
131 fn from(string
: &'a String
) -> Self {
132 match string
.as_ref() {
133 "lib" => LibKind
::Lib
,
134 "rlib" => LibKind
::Rlib
,
135 "dylib" => LibKind
::Dylib
,
136 "proc-macro" => LibKind
::ProcMacro
,
137 s
=> LibKind
::Other(s
.to_string()),
142 #[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
143 pub enum TargetKind
{
148 ExampleLib(Vec
<LibKind
>),
153 impl ser
::Serialize
for TargetKind
{
154 fn serialize
<S
>(&self, s
: S
) -> Result
<S
::Ok
, S
::Error
>
158 use self::TargetKind
::*;
160 Lib(ref kinds
) => s
.collect_seq(kinds
.iter().map(LibKind
::crate_type
)),
161 Bin
=> ["bin"].serialize(s
),
162 ExampleBin
| ExampleLib(_
) => ["example"].serialize(s
),
163 Test
=> ["test"].serialize(s
),
164 CustomBuild
=> ["custom-build"].serialize(s
),
165 Bench
=> ["bench"].serialize(s
),
170 impl fmt
::Debug
for TargetKind
{
171 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
172 use self::TargetKind
::*;
174 Lib(ref kinds
) => kinds
.fmt(f
),
176 ExampleBin
| ExampleLib(_
) => "example".fmt(f
),
177 Test
=> "test".fmt(f
),
178 CustomBuild
=> "custom-build".fmt(f
),
179 Bench
=> "bench".fmt(f
),
185 pub fn description(&self) -> &'
static str {
187 TargetKind
::Lib(..) => "lib",
188 TargetKind
::Bin
=> "bin",
189 TargetKind
::Test
=> "integration-test",
190 TargetKind
::ExampleBin
| TargetKind
::ExampleLib(..) => "example",
191 TargetKind
::Bench
=> "bench",
192 TargetKind
::CustomBuild
=> "build-script",
197 /// Information about a binary, a library, an example, etc. that is part of the
199 #[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
203 // Note that the `src_path` here is excluded from the `Hash` implementation
204 // as it's absolute currently and is otherwise a little too brittle for
205 // causing rebuilds. Instead the hash for the path that we send to the
206 // compiler is handled elsewhere.
207 src_path
: TargetSourcePath
,
208 required_features
: Option
<Vec
<String
>>,
213 harness
: bool
, // whether to use the test harness (--test)
218 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
219 pub enum TargetSourcePath
{
224 impl TargetSourcePath
{
225 pub fn path(&self) -> &Path
{
227 TargetSourcePath
::Path(path
) => path
.as_ref(),
228 TargetSourcePath
::Metabuild
=> panic
!("metabuild not expected"),
232 pub fn is_path(&self) -> bool
{
234 TargetSourcePath
::Path(_
) => true,
240 impl Hash
for TargetSourcePath
{
241 fn hash
<H
: Hasher
>(&self, _
: &mut H
) {
246 impl fmt
::Debug
for TargetSourcePath
{
247 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
249 TargetSourcePath
::Path(path
) => path
.fmt(f
),
250 TargetSourcePath
::Metabuild
=> "metabuild".fmt(f
),
255 impl From
<PathBuf
> for TargetSourcePath
{
256 fn from(path
: PathBuf
) -> Self {
257 assert
!(path
.is_absolute(), "`{}` is not absolute", path
.display());
258 TargetSourcePath
::Path(path
)
263 struct SerializedTarget
<'a
> {
264 /// Is this a `--bin bin`, `--lib`, `--example ex`?
265 /// Serialized as a list of strings for historical reasons.
266 kind
: &'a TargetKind
,
267 /// Corresponds to `--crate-type` compiler attribute.
268 /// See https://doc.rust-lang.org/reference/linkage.html
269 crate_types
: Vec
<&'a
str>,
271 src_path
: &'a PathBuf
,
273 #[serde(rename = "required-features", skip_serializing_if = "Option::is_none")]
274 required_features
: Option
<Vec
<&'a
str>>,
277 impl ser
::Serialize
for Target
{
278 fn serialize
<S
: ser
::Serializer
>(&self, s
: S
) -> Result
<S
::Ok
, S
::Error
> {
281 crate_types
: self.rustc_crate_types(),
283 src_path
: &self.src_path
.path().to_path_buf(),
284 edition
: &self.edition
.to_string(),
285 required_features
: self
288 .map(|rf
| rf
.iter().map(|s
| &**s
).collect()),
295 impl fmt
::Debug
for Target
{
296 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
297 let (default, default_name
) = {
299 TargetKind
::Lib(kinds
) => {
304 self.src_path().path().to_path_buf(),
307 format
!("lib_target({:?}, {:?}, {:?}, {:?})",
308 self.name
, kinds
, self.src_path
, self.edition
),
311 TargetKind
::CustomBuild
=> {
312 match self.src_path
{
313 TargetSourcePath
::Path(ref path
) => {
315 Target
::custom_build_target(
320 format
!("custom_build_target({:?}, {:?}, {:?})",
321 self.name
, path
, self.edition
),
324 TargetSourcePath
::Metabuild
=> {
326 Target
::metabuild_target(&self.name
),
327 format
!("metabuild_target({:?})", self.name
),
333 Target
::new(self.src_path
.clone(), self.edition
),
334 format
!("with_path({:?}, {:?})", self.src_path
, self.edition
),
358 targets
: Vec
<Target
>,
359 exclude
: Vec
<String
>,
360 include
: Vec
<String
>,
361 links
: Option
<String
>,
362 metadata
: ManifestMetadata
,
363 custom_metadata
: Option
<toml
::Value
>,
365 publish
: Option
<Vec
<String
>>,
366 publish_lockfile
: bool
,
367 replace
: Vec
<(PackageIdSpec
, Dependency
)>,
368 patch
: HashMap
<Url
, Vec
<Dependency
>>,
369 workspace
: WorkspaceConfig
,
372 im_a_teapot
: Option
<bool
>,
373 default_run
: Option
<String
>,
374 original
: Rc
<TomlManifest
>,
375 metabuild
: Option
<Vec
<String
>>,
380 warnings
: Warnings
::new(),
401 pub fn dependencies(&self) -> &[Dependency
] {
402 self.summary
.dependencies()
404 pub fn exclude(&self) -> &[String
] {
407 pub fn include(&self) -> &[String
] {
410 pub fn metadata(&self) -> &ManifestMetadata
{
413 pub fn name(&self) -> InternedString
{
414 self.package_id().name()
416 pub fn package_id(&self) -> &PackageId
{
417 self.summary
.package_id()
419 pub fn summary(&self) -> &Summary
{
422 pub fn targets(&self) -> &[Target
] {
425 pub fn version(&self) -> &Version
{
426 self.package_id().version()
428 pub fn warnings_mut(&mut self) -> &mut Warnings
{
431 pub fn warnings(&self) -> &Warnings
{
434 pub fn profiles(&self) -> &Profiles
{
437 pub fn publish(&self) -> &Option
<Vec
<String
>> {
440 pub fn publish_lockfile(&self) -> bool
{
441 self.publish_lockfile
443 pub fn replace(&self) -> &[(PackageIdSpec
, Dependency
)] {
446 pub fn original(&self) -> &TomlManifest
{
449 pub fn patch(&self) -> &HashMap
<Url
, Vec
<Dependency
>> {
452 pub fn links(&self) -> Option
<&str> {
453 self.links
.as_ref().map(|s
| &s
[..])
456 pub fn workspace_config(&self) -> &WorkspaceConfig
{
460 pub fn features(&self) -> &Features
{
464 pub fn set_summary(&mut self, summary
: Summary
) {
465 self.summary
= summary
;
468 pub fn map_source(self, to_replace
: SourceId
, replace_with
: SourceId
) -> Manifest
{
470 summary
: self.summary
.map_source(to_replace
, replace_with
),
475 pub fn feature_gate(&self) -> CargoResult
<()> {
476 if self.im_a_teapot
.is_some() {
478 .require(Feature
::test_dummy_unstable())
481 "the `im-a-teapot` manifest key is unstable and may \
482 not work properly in England"
487 if self.default_run
.is_some() {
489 .require(Feature
::default_run())
490 .chain_err(|| format_err
!("the `default-run` manifest key is unstable"))?
;
496 // Just a helper function to test out `-Z` flags on Cargo
497 pub fn print_teapot(&self, config
: &Config
) {
498 if let Some(teapot
) = self.im_a_teapot
{
499 if config
.cli_unstable().print_im_a_teapot
{
500 println
!("im-a-teapot = {}", teapot
);
505 pub fn edition(&self) -> Edition
{
509 pub fn custom_metadata(&self) -> Option
<&toml
::Value
> {
510 self.custom_metadata
.as_ref()
513 pub fn default_run(&self) -> Option
<&str> {
514 self.default_run
.as_ref().map(|s
| &s
[..])
517 pub fn metabuild(&self) -> Option
<&Vec
<String
>> {
518 self.metabuild
.as_ref()
521 pub fn metabuild_path(&self, target_dir
: Filesystem
) -> PathBuf
{
522 let hash
= short_hash(self.package_id());
524 .into_path_unlocked()
526 .join(format
!("metabuild-{}-{}.rs", self.name(), hash
))
530 impl VirtualManifest
{
532 replace
: Vec
<(PackageIdSpec
, Dependency
)>,
533 patch
: HashMap
<Url
, Vec
<Dependency
>>,
534 workspace
: WorkspaceConfig
,
536 ) -> VirtualManifest
{
542 warnings
: Warnings
::new(),
546 pub fn replace(&self) -> &[(PackageIdSpec
, Dependency
)] {
550 pub fn patch(&self) -> &HashMap
<Url
, Vec
<Dependency
>> {
554 pub fn workspace_config(&self) -> &WorkspaceConfig
{
558 pub fn profiles(&self) -> &Profiles
{
562 pub fn warnings_mut(&mut self) -> &mut Warnings
{
566 pub fn warnings(&self) -> &Warnings
{
572 fn new(src_path
: TargetSourcePath
, edition
: Edition
) -> Target
{
574 kind
: TargetKind
::Bin
,
577 required_features
: None
,
588 fn with_path(src_path
: PathBuf
, edition
: Edition
) -> Target
{
589 Target
::new(TargetSourcePath
::from(src_path
), edition
)
594 crate_targets
: Vec
<LibKind
>,
599 kind
: TargetKind
::Lib(crate_targets
),
600 name
: name
.to_string(),
603 ..Target
::with_path(src_path
, edition
)
610 required_features
: Option
<Vec
<String
>>,
614 kind
: TargetKind
::Bin
,
615 name
: name
.to_string(),
618 ..Target
::with_path(src_path
, edition
)
622 /// Builds a `Target` corresponding to the `build = "build.rs"` entry.
623 pub fn custom_build_target(name
: &str, src_path
: PathBuf
, edition
: Edition
) -> Target
{
625 kind
: TargetKind
::CustomBuild
,
626 name
: name
.to_string(),
630 ..Target
::with_path(src_path
, edition
)
634 pub fn metabuild_target(name
: &str) -> Target
{
636 kind
: TargetKind
::CustomBuild
,
637 name
: name
.to_string(),
641 ..Target
::new(TargetSourcePath
::Metabuild
, Edition
::Edition2015
)
645 pub fn example_target(
647 crate_targets
: Vec
<LibKind
>,
649 required_features
: Option
<Vec
<String
>>,
652 let kind
= if crate_targets
.is_empty()
655 .all(|t
| *t
== LibKind
::Other("bin".into()))
657 TargetKind
::ExampleBin
659 TargetKind
::ExampleLib(crate_targets
)
664 name
: name
.to_string(),
668 ..Target
::with_path(src_path
, edition
)
675 required_features
: Option
<Vec
<String
>>,
679 kind
: TargetKind
::Test
,
680 name
: name
.to_string(),
683 ..Target
::with_path(src_path
, edition
)
690 required_features
: Option
<Vec
<String
>>,
694 kind
: TargetKind
::Bench
,
695 name
: name
.to_string(),
698 ..Target
::with_path(src_path
, edition
)
702 pub fn name(&self) -> &str {
705 pub fn crate_name(&self) -> String
{
706 self.name
.replace("-", "_")
708 pub fn src_path(&self) -> &TargetSourcePath
{
711 pub fn set_src_path(&mut self, src_path
: TargetSourcePath
) {
712 self.src_path
= src_path
;
714 pub fn required_features(&self) -> Option
<&Vec
<String
>> {
715 self.required_features
.as_ref()
717 pub fn kind(&self) -> &TargetKind
{
720 pub fn tested(&self) -> bool
{
723 pub fn harness(&self) -> bool
{
726 pub fn documented(&self) -> bool
{
729 pub fn for_host(&self) -> bool
{
732 pub fn edition(&self) -> Edition
{
735 pub fn benched(&self) -> bool
{
738 pub fn doctested(&self) -> bool
{
742 pub fn doctestable(&self) -> bool
{
744 TargetKind
::Lib(ref kinds
) => kinds
746 .any(|k
| *k
== LibKind
::Rlib
|| *k
== LibKind
::Lib
|| *k
== LibKind
::ProcMacro
),
751 pub fn allows_underscores(&self) -> bool
{
752 self.is_bin() || self.is_example() || self.is_custom_build()
755 pub fn is_lib(&self) -> bool
{
757 TargetKind
::Lib(_
) => true,
762 pub fn is_dylib(&self) -> bool
{
764 TargetKind
::Lib(ref libs
) => libs
.iter().any(|l
| *l
== LibKind
::Dylib
),
769 pub fn is_cdylib(&self) -> bool
{
770 let libs
= match self.kind
{
771 TargetKind
::Lib(ref libs
) => libs
,
774 libs
.iter().any(|l
| match *l
{
775 LibKind
::Other(ref s
) => s
== "cdylib",
780 pub fn linkable(&self) -> bool
{
782 TargetKind
::Lib(ref kinds
) => kinds
.iter().any(|k
| k
.linkable()),
787 pub fn is_bin(&self) -> bool
{
788 self.kind
== TargetKind
::Bin
791 pub fn is_example(&self) -> bool
{
793 TargetKind
::ExampleBin
| TargetKind
::ExampleLib(..) => true,
798 pub fn is_bin_example(&self) -> bool
{
799 // Needed for --all-examples in contexts where only runnable examples make sense
801 TargetKind
::ExampleBin
=> true,
806 pub fn is_test(&self) -> bool
{
807 self.kind
== TargetKind
::Test
809 pub fn is_bench(&self) -> bool
{
810 self.kind
== TargetKind
::Bench
812 pub fn is_custom_build(&self) -> bool
{
813 self.kind
== TargetKind
::CustomBuild
816 /// Returns the arguments suitable for `--crate-type` to pass to rustc.
817 pub fn rustc_crate_types(&self) -> Vec
<&str> {
819 TargetKind
::Lib(ref kinds
) | TargetKind
::ExampleLib(ref kinds
) => {
820 kinds
.iter().map(LibKind
::crate_type
).collect()
822 TargetKind
::CustomBuild
825 | TargetKind
::ExampleBin
826 | TargetKind
::Bin
=> vec
!["bin"],
830 pub fn can_lto(&self) -> bool
{
832 TargetKind
::Lib(ref v
) => {
833 !v
.contains(&LibKind
::Rlib
)
834 && !v
.contains(&LibKind
::Dylib
)
835 && !v
.contains(&LibKind
::Lib
)
841 pub fn set_tested(&mut self, tested
: bool
) -> &mut Target
{
842 self.tested
= tested
;
845 pub fn set_benched(&mut self, benched
: bool
) -> &mut Target
{
846 self.benched
= benched
;
849 pub fn set_doctest(&mut self, doctest
: bool
) -> &mut Target
{
850 self.doctest
= doctest
;
853 pub fn set_for_host(&mut self, for_host
: bool
) -> &mut Target
{
854 self.for_host
= for_host
;
857 pub fn set_edition(&mut self, edition
: Edition
) -> &mut Target
{
858 self.edition
= edition
;
861 pub fn set_harness(&mut self, harness
: bool
) -> &mut Target
{
862 self.harness
= harness
;
865 pub fn set_doc(&mut self, doc
: bool
) -> &mut Target
{
871 impl fmt
::Display
for Target
{
872 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
874 TargetKind
::Lib(..) => write
!(f
, "Target(lib)"),
875 TargetKind
::Bin
=> write
!(f
, "Target(bin: {})", self.name
),
876 TargetKind
::Test
=> write
!(f
, "Target(test: {})", self.name
),
877 TargetKind
::Bench
=> write
!(f
, "Target(bench: {})", self.name
),
878 TargetKind
::ExampleBin
| TargetKind
::ExampleLib(..) => {
879 write
!(f
, "Target(example: {})", self.name
)
881 TargetKind
::CustomBuild
=> write
!(f
, "Target(script)"),
887 fn new() -> Warnings
{
891 pub fn add_warning(&mut self, s
: String
) {
892 self.0.push(DelayedWarning
{
898 pub fn add_critical_warning(&mut self, s
: String
) {
899 self.0.push(DelayedWarning
{
905 pub fn warnings(&self) -> &[DelayedWarning
] {