-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
manifest.rs
639 lines (577 loc) · 17.9 KB
/
manifest.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
use std::collections::HashMap;
use std::fmt;
use std::path::{PathBuf, Path};
use semver::Version;
use rustc_serialize::{Encoder, Encodable};
use core::{Dependency, PackageId, Summary, SourceId, PackageIdSpec};
use core::WorkspaceConfig;
pub enum EitherManifest {
Real(Manifest),
Virtual(VirtualManifest),
}
/// Contains all the information about a package, as loaded from a Cargo.toml.
#[derive(Clone, Debug)]
pub struct Manifest {
summary: Summary,
targets: Vec<Target>,
links: Option<String>,
warnings: Vec<String>,
exclude: Vec<String>,
include: Vec<String>,
metadata: ManifestMetadata,
profiles: Profiles,
publish: bool,
replace: Vec<(PackageIdSpec, Dependency)>,
workspace: WorkspaceConfig,
}
#[derive(Clone, Debug)]
pub struct VirtualManifest {
replace: Vec<(PackageIdSpec, Dependency)>,
workspace: WorkspaceConfig,
profiles: Profiles,
}
/// General metadata about a package which is just blindly uploaded to the
/// registry.
///
/// Note that many of these fields can contain invalid values such as the
/// homepage, repository, documentation, or license. These fields are not
/// validated by cargo itself, but rather it is up to the registry when uploaded
/// to validate these fields. Cargo will itself accept any valid TOML
/// specification for these values.
#[derive(PartialEq, Clone, Debug)]
pub struct ManifestMetadata {
pub authors: Vec<String>,
pub keywords: Vec<String>,
pub categories: Vec<String>,
pub license: Option<String>,
pub license_file: Option<String>,
pub description: Option<String>, // not markdown
pub readme: Option<String>, // file, not contents
pub homepage: Option<String>, // url
pub repository: Option<String>, // url
pub documentation: Option<String>, // url
pub badges: HashMap<String, HashMap<String, String>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum LibKind {
Lib,
Rlib,
Dylib,
ProcMacro,
Other(String),
}
impl LibKind {
pub fn from_str(string: &str) -> LibKind {
match string {
"lib" => LibKind::Lib,
"rlib" => LibKind::Rlib,
"dylib" => LibKind::Dylib,
"proc-macro" => LibKind::ProcMacro,
s => LibKind::Other(s.to_string()),
}
}
/// Returns the argument suitable for `--crate-type` to pass to rustc.
pub fn crate_type(&self) -> &str {
match *self {
LibKind::Lib => "lib",
LibKind::Rlib => "rlib",
LibKind::Dylib => "dylib",
LibKind::ProcMacro => "proc-macro",
LibKind::Other(ref s) => s,
}
}
pub fn linkable(&self) -> bool {
match *self {
LibKind::Lib |
LibKind::Rlib |
LibKind::Dylib |
LibKind::ProcMacro => true,
LibKind::Other(..) => false,
}
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum TargetKind {
Lib(Vec<LibKind>),
Bin,
Test,
Bench,
ExampleLib(Vec<LibKind>),
ExampleBin,
CustomBuild,
}
impl TargetKind {
/// Returns a vector of crate types as specified in a manifest with one difference.
/// For ExampleLib it returns "example" instead of crate types
pub fn kinds(&self) -> Vec<&str> {
use self::TargetKind::*;
match *self {
Lib(ref kinds) => kinds.iter().map(LibKind::crate_type).collect(),
Bin => vec!["bin"],
ExampleBin | ExampleLib(_) => vec!["example"],
Test => vec!["test"],
CustomBuild => vec!["custom-build"],
Bench => vec!["bench"]
}
}
/// Returns a vector of crate types as specified in a manifest
pub fn crate_types(&self) -> Vec<&str> {
use self::TargetKind::*;
match *self {
Lib(ref kinds) | ExampleLib(ref kinds) => {
kinds.iter().map(LibKind::crate_type).collect()
}
Bin => vec!["bin"],
ExampleBin => vec!["example"],
Test => vec!["test"],
CustomBuild => vec!["custom-build"],
Bench => vec!["bench"]
}
}
}
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct Profile {
pub opt_level: String,
pub lto: bool,
pub codegen_units: Option<u32>, // None = use rustc default
pub rustc_args: Option<Vec<String>>,
pub rustdoc_args: Option<Vec<String>>,
pub debuginfo: Option<u32>,
pub debug_assertions: bool,
pub rpath: bool,
pub test: bool,
pub doc: bool,
pub run_custom_build: bool,
pub check: bool,
pub panic: Option<String>,
}
#[derive(RustcEncodable)]
struct SerializedProfile<'a> {
opt_level: &'a str,
debuginfo: Option<u32>,
debug_assertions: bool,
test: bool,
}
impl Encodable for Profile {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
SerializedProfile {
opt_level: &self.opt_level,
debuginfo: self.debuginfo,
debug_assertions: self.debug_assertions,
test: self.test,
}.encode(s)
}
}
#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct Profiles {
pub release: Profile,
pub dev: Profile,
pub test: Profile,
pub test_deps: Profile,
pub bench: Profile,
pub bench_deps: Profile,
pub doc: Profile,
pub custom_build: Profile,
pub check: Profile,
pub doctest: Profile,
}
/// Information about a binary, a library, an example, etc. that is part of the
/// package.
#[derive(Clone, Hash, PartialEq, Eq, Debug)]
pub struct Target {
kind: TargetKind,
name: String,
src_path: PathBuf,
required_features: Option<Vec<String>>,
tested: bool,
benched: bool,
doc: bool,
doctest: bool,
harness: bool, // whether to use the test harness (--test)
for_host: bool,
}
#[derive(RustcEncodable)]
struct SerializedTarget<'a> {
kind: Vec<&'a str>,
crate_types: Vec<&'a str>,
name: &'a str,
src_path: &'a str,
}
impl Encodable for Target {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
SerializedTarget {
kind: self.kind.kinds(),
crate_types: self.kind.crate_types(),
name: &self.name,
src_path: &self.src_path.display().to_string(),
}.encode(s)
}
}
impl Manifest {
pub fn new(summary: Summary,
targets: Vec<Target>,
exclude: Vec<String>,
include: Vec<String>,
links: Option<String>,
metadata: ManifestMetadata,
profiles: Profiles,
publish: bool,
replace: Vec<(PackageIdSpec, Dependency)>,
workspace: WorkspaceConfig) -> Manifest {
Manifest {
summary: summary,
targets: targets,
warnings: Vec::new(),
exclude: exclude,
include: include,
links: links,
metadata: metadata,
profiles: profiles,
publish: publish,
replace: replace,
workspace: workspace,
}
}
pub fn dependencies(&self) -> &[Dependency] { self.summary.dependencies() }
pub fn exclude(&self) -> &[String] { &self.exclude }
pub fn include(&self) -> &[String] { &self.include }
pub fn metadata(&self) -> &ManifestMetadata { &self.metadata }
pub fn name(&self) -> &str { self.package_id().name() }
pub fn package_id(&self) -> &PackageId { self.summary.package_id() }
pub fn summary(&self) -> &Summary { &self.summary }
pub fn targets(&self) -> &[Target] { &self.targets }
pub fn version(&self) -> &Version { self.package_id().version() }
pub fn warnings(&self) -> &[String] { &self.warnings }
pub fn profiles(&self) -> &Profiles { &self.profiles }
pub fn publish(&self) -> bool { self.publish }
pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] { &self.replace }
pub fn links(&self) -> Option<&str> {
self.links.as_ref().map(|s| &s[..])
}
pub fn workspace_config(&self) -> &WorkspaceConfig {
&self.workspace
}
pub fn add_warning(&mut self, s: String) {
self.warnings.push(s)
}
pub fn set_summary(&mut self, summary: Summary) {
self.summary = summary;
}
pub fn map_source(self, to_replace: &SourceId, replace_with: &SourceId)
-> Manifest {
Manifest {
summary: self.summary.map_source(to_replace, replace_with),
..self
}
}
}
impl VirtualManifest {
pub fn new(replace: Vec<(PackageIdSpec, Dependency)>,
workspace: WorkspaceConfig,
profiles: Profiles) -> VirtualManifest {
VirtualManifest {
replace: replace,
workspace: workspace,
profiles: profiles,
}
}
pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] {
&self.replace
}
pub fn workspace_config(&self) -> &WorkspaceConfig {
&self.workspace
}
pub fn profiles(&self) -> &Profiles {
&self.profiles
}
}
impl Target {
fn with_path(src_path: PathBuf) -> Target {
assert!(src_path.is_absolute());
Target {
kind: TargetKind::Bin,
name: String::new(),
src_path: src_path,
required_features: None,
doc: false,
doctest: false,
harness: true,
for_host: false,
tested: true,
benched: true,
}
}
pub fn lib_target(name: &str,
crate_targets: Vec<LibKind>,
src_path: PathBuf) -> Target {
Target {
kind: TargetKind::Lib(crate_targets),
name: name.to_string(),
doctest: true,
doc: true,
..Target::with_path(src_path)
}
}
pub fn bin_target(name: &str, src_path: PathBuf,
required_features: Option<Vec<String>>) -> Target {
Target {
kind: TargetKind::Bin,
name: name.to_string(),
required_features: required_features,
doc: true,
..Target::with_path(src_path)
}
}
/// Builds a `Target` corresponding to the `build = "build.rs"` entry.
pub fn custom_build_target(name: &str, src_path: PathBuf) -> Target {
Target {
kind: TargetKind::CustomBuild,
name: name.to_string(),
for_host: true,
benched: false,
tested: false,
..Target::with_path(src_path)
}
}
pub fn example_target(name: &str,
crate_targets: Vec<LibKind>,
src_path: PathBuf,
required_features: Option<Vec<String>>) -> Target {
let kind = if crate_targets.is_empty() {
TargetKind::ExampleBin
} else {
TargetKind::ExampleLib(crate_targets)
};
Target {
kind: kind,
name: name.to_string(),
required_features: required_features,
benched: false,
..Target::with_path(src_path)
}
}
pub fn test_target(name: &str, src_path: PathBuf,
required_features: Option<Vec<String>>) -> Target {
Target {
kind: TargetKind::Test,
name: name.to_string(),
required_features: required_features,
benched: false,
..Target::with_path(src_path)
}
}
pub fn bench_target(name: &str, src_path: PathBuf,
required_features: Option<Vec<String>>) -> Target {
Target {
kind: TargetKind::Bench,
name: name.to_string(),
required_features: required_features,
tested: false,
..Target::with_path(src_path)
}
}
pub fn name(&self) -> &str { &self.name }
pub fn crate_name(&self) -> String { self.name.replace("-", "_") }
pub fn src_path(&self) -> &Path { &self.src_path }
pub fn required_features(&self) -> Option<&Vec<String>> { self.required_features.as_ref() }
pub fn kind(&self) -> &TargetKind { &self.kind }
pub fn tested(&self) -> bool { self.tested }
pub fn harness(&self) -> bool { self.harness }
pub fn documented(&self) -> bool { self.doc }
pub fn for_host(&self) -> bool { self.for_host }
pub fn benched(&self) -> bool { self.benched }
pub fn doctested(&self) -> bool {
self.doctest && match self.kind {
TargetKind::Lib(ref kinds) => {
kinds.iter().find(|k| {
*k == &LibKind::Rlib ||
*k == &LibKind::Lib ||
*k == &LibKind::ProcMacro
}).is_some()
}
_ => false,
}
}
pub fn allows_underscores(&self) -> bool {
self.is_bin() || self.is_example() || self.is_custom_build()
}
pub fn is_lib(&self) -> bool {
match self.kind {
TargetKind::Lib(_) => true,
_ => false
}
}
pub fn is_dylib(&self) -> bool {
match self.kind {
TargetKind::Lib(ref libs) => libs.iter().any(|l| *l == LibKind::Dylib),
_ => false
}
}
pub fn linkable(&self) -> bool {
match self.kind {
TargetKind::Lib(ref kinds) => {
kinds.iter().any(|k| k.linkable())
}
_ => false
}
}
pub fn is_bin(&self) -> bool { self.kind == TargetKind::Bin }
pub fn is_example(&self) -> bool {
match self.kind {
TargetKind::ExampleBin |
TargetKind::ExampleLib(..) => true,
_ => false
}
}
pub fn is_test(&self) -> bool { self.kind == TargetKind::Test }
pub fn is_bench(&self) -> bool { self.kind == TargetKind::Bench }
pub fn is_custom_build(&self) -> bool { self.kind == TargetKind::CustomBuild }
/// Returns the arguments suitable for `--crate-type` to pass to rustc.
pub fn rustc_crate_types(&self) -> Vec<&str> {
match self.kind {
TargetKind::Lib(ref kinds) |
TargetKind::ExampleLib(ref kinds) => {
kinds.iter().map(LibKind::crate_type).collect()
}
TargetKind::CustomBuild |
TargetKind::Bench |
TargetKind::Test |
TargetKind::ExampleBin |
TargetKind::Bin => vec!["bin"],
}
}
pub fn can_lto(&self) -> bool {
match self.kind {
TargetKind::Lib(ref v) => {
!v.contains(&LibKind::Rlib) &&
!v.contains(&LibKind::Dylib) &&
!v.contains(&LibKind::Lib)
}
_ => true,
}
}
pub fn set_tested(&mut self, tested: bool) -> &mut Target {
self.tested = tested;
self
}
pub fn set_benched(&mut self, benched: bool) -> &mut Target {
self.benched = benched;
self
}
pub fn set_doctest(&mut self, doctest: bool) -> &mut Target {
self.doctest = doctest;
self
}
pub fn set_for_host(&mut self, for_host: bool) -> &mut Target {
self.for_host = for_host;
self
}
pub fn set_harness(&mut self, harness: bool) -> &mut Target {
self.harness = harness;
self
}
pub fn set_doc(&mut self, doc: bool) -> &mut Target {
self.doc = doc;
self
}
}
impl fmt::Display for Target {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.kind {
TargetKind::Lib(..) => write!(f, "Target(lib)"),
TargetKind::Bin => write!(f, "Target(bin: {})", self.name),
TargetKind::Test => write!(f, "Target(test: {})", self.name),
TargetKind::Bench => write!(f, "Target(bench: {})", self.name),
TargetKind::ExampleBin |
TargetKind::ExampleLib(..) => write!(f, "Target(example: {})", self.name),
TargetKind::CustomBuild => write!(f, "Target(script)"),
}
}
}
impl Profile {
pub fn default_dev() -> Profile {
Profile {
debuginfo: Some(2),
debug_assertions: true,
..Profile::default()
}
}
pub fn default_release() -> Profile {
Profile {
opt_level: "3".to_string(),
debuginfo: None,
..Profile::default()
}
}
pub fn default_test() -> Profile {
Profile {
test: true,
..Profile::default_dev()
}
}
pub fn default_bench() -> Profile {
Profile {
test: true,
..Profile::default_release()
}
}
pub fn default_doc() -> Profile {
Profile {
doc: true,
..Profile::default_dev()
}
}
pub fn default_custom_build() -> Profile {
Profile {
run_custom_build: true,
..Profile::default_dev()
}
}
pub fn default_check() -> Profile {
Profile {
check: true,
..Profile::default_dev()
}
}
pub fn default_doctest() -> Profile {
Profile {
doc: true,
test: true,
..Profile::default_dev()
}
}
}
impl Default for Profile {
fn default() -> Profile {
Profile {
opt_level: "0".to_string(),
lto: false,
codegen_units: None,
rustc_args: None,
rustdoc_args: None,
debuginfo: None,
debug_assertions: false,
rpath: false,
test: false,
doc: false,
run_custom_build: false,
check: false,
panic: None,
}
}
}
impl fmt::Display for Profile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.test {
write!(f, "Profile(test)")
} else if self.doc {
write!(f, "Profile(doc)")
} else if self.run_custom_build {
write!(f, "Profile(run)")
} else if self.check {
write!(f, "Profile(check)")
} else {
write!(f, "Profile(build)")
}
}
}