-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
cargo_compile.rs
933 lines (879 loc) · 30.5 KB
/
cargo_compile.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
//!
//! Cargo compile currently does the following steps:
//!
//! All configurations are already injected as environment variables via the
//! main cargo command
//!
//! 1. Read the manifest
//! 2. Shell out to `cargo-resolve` with a list of dependencies and sources as
//! stdin
//!
//! a. Shell out to `--do update` and `--do list` for each source
//! b. Resolve dependencies and return a list of name/version/source
//!
//! 3. Shell out to `--do download` for each source
//! 4. Shell out to `--do get` for each source, and build up the list of paths
//! to pass to rustc -L
//! 5. Call `cargo-rustc` with the results of the resolver zipped together with
//! the results of the `get`
//!
//! a. Topologically sort the dependencies
//! b. Compile each dependency in order, passing in the -L's pointing at each
//! previously compiled dependency
//!
use std::collections::{HashMap, HashSet};
use std::default::Default;
use std::path::PathBuf;
use std::sync::Arc;
use core::{Package, Source, Target};
use core::{PackageId, PackageIdSpec, Profile, Profiles, TargetKind, Workspace};
use core::resolver::{Method, Resolve};
use ops::{self, BuildOutput, DefaultExecutor, Executor};
use util::config::Config;
use util::{profile, CargoResult};
/// Contains information about how a package should be compiled.
#[derive(Debug)]
pub struct CompileOptions<'a> {
pub config: &'a Config,
/// Number of concurrent jobs to use.
pub jobs: Option<u32>,
/// The target platform to compile for (example: `i686-unknown-linux-gnu`).
pub target: Option<String>,
/// Extra features to build for the root package
pub features: Vec<String>,
/// Flag whether all available features should be built for the root package
pub all_features: bool,
/// Flag if the default feature should be built for the root package
pub no_default_features: bool,
/// A set of packages to build.
pub spec: Packages,
/// Filter to apply to the root package to select which targets will be
/// built.
pub filter: CompileFilter,
/// Whether this is a release build or not
pub release: bool,
/// Mode for this compile.
pub mode: CompileMode,
/// `--error_format` flag for the compiler.
pub message_format: MessageFormat,
/// Extra arguments to be passed to rustdoc (for main crate and dependencies)
pub target_rustdoc_args: Option<Vec<String>>,
/// The specified target will be compiled with all the available arguments,
/// note that this only accounts for the *final* invocation of rustc
pub target_rustc_args: Option<Vec<String>>,
}
impl<'a> CompileOptions<'a> {
pub fn default(config: &'a Config, mode: CompileMode) -> CompileOptions<'a> {
CompileOptions {
config,
jobs: None,
target: None,
features: Vec::new(),
all_features: false,
no_default_features: false,
spec: ops::Packages::Packages(Vec::new()),
mode,
release: false,
filter: CompileFilter::Default {
required_features_filterable: false,
},
message_format: MessageFormat::Human,
target_rustdoc_args: None,
target_rustc_args: None,
}
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum CompileMode {
Test,
Build,
Check { test: bool },
Bench,
Doc { deps: bool },
Doctest,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MessageFormat {
Human,
Json,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Packages {
Default,
All,
OptOut(Vec<String>),
Packages(Vec<String>),
}
impl Packages {
pub fn from_flags(all: bool, exclude: Vec<String>, package: Vec<String>) -> CargoResult<Self> {
Ok(match (all, exclude.len(), package.len()) {
(false, 0, 0) => Packages::Default,
(false, 0, _) => Packages::Packages(package),
(false, _, _) => bail!("--exclude can only be used together with --all"),
(true, 0, _) => Packages::All,
(true, _, _) => Packages::OptOut(exclude),
})
}
pub fn into_package_id_specs(&self, ws: &Workspace) -> CargoResult<Vec<PackageIdSpec>> {
let specs = match *self {
Packages::All => ws.members()
.map(Package::package_id)
.map(PackageIdSpec::from_package_id)
.collect(),
Packages::OptOut(ref opt_out) => ws.members()
.map(Package::package_id)
.map(PackageIdSpec::from_package_id)
.filter(|p| opt_out.iter().position(|x| *x == p.name()).is_none())
.collect(),
Packages::Packages(ref packages) if packages.is_empty() => ws.current_opt()
.map(Package::package_id)
.map(PackageIdSpec::from_package_id)
.into_iter()
.collect(),
Packages::Packages(ref packages) => packages
.iter()
.map(|p| PackageIdSpec::parse(p))
.collect::<CargoResult<Vec<_>>>()?,
Packages::Default => ws.default_members()
.map(Package::package_id)
.map(PackageIdSpec::from_package_id)
.collect(),
};
if specs.is_empty() {
if ws.is_virtual() {
bail!(
"manifest path `{}` contains no package: The manifest is virtual, \
and the workspace has no members.",
ws.root().display()
)
}
bail!("no packages to compile")
}
Ok(specs)
}
}
#[derive(Debug)]
pub enum FilterRule {
All,
Just(Vec<String>),
}
#[derive(Debug)]
pub enum CompileFilter {
Default {
/// Flag whether targets can be safely skipped when required-features are not satisfied.
required_features_filterable: bool,
},
Only {
all_targets: bool,
lib: bool,
bins: FilterRule,
examples: FilterRule,
tests: FilterRule,
benches: FilterRule,
},
}
pub fn compile<'a>(
ws: &Workspace<'a>,
options: &CompileOptions<'a>,
) -> CargoResult<ops::Compilation<'a>> {
compile_with_exec(ws, options, Arc::new(DefaultExecutor))
}
pub fn compile_with_exec<'a>(
ws: &Workspace<'a>,
options: &CompileOptions<'a>,
exec: Arc<Executor>,
) -> CargoResult<ops::Compilation<'a>> {
for member in ws.members() {
for warning in member.manifest().warnings().iter() {
if warning.is_critical {
let err = format_err!("{}", warning.message);
let cx = format_err!(
"failed to parse manifest at `{}`",
member.manifest_path().display()
);
return Err(err.context(cx).into());
} else {
options.config.shell().warn(&warning.message)?
}
}
}
compile_ws(ws, None, options, exec)
}
pub fn compile_ws<'a>(
ws: &Workspace<'a>,
source: Option<Box<Source + 'a>>,
options: &CompileOptions<'a>,
exec: Arc<Executor>,
) -> CargoResult<ops::Compilation<'a>> {
let CompileOptions {
config,
jobs,
ref target,
ref spec,
ref features,
all_features,
no_default_features,
release,
mode,
message_format,
ref filter,
ref target_rustdoc_args,
ref target_rustc_args,
} = *options;
let target = target.clone();
if jobs == Some(0) {
bail!("jobs must be at least 1")
}
let profiles = ws.profiles();
let specs = spec.into_package_id_specs(ws)?;
let features = Method::split_features(features);
let method = Method::Required {
dev_deps: ws.require_optional_deps() || filter.need_dev_deps(mode),
features: &features,
all_features,
uses_default_features: !no_default_features,
};
let resolve = ops::resolve_ws_with_method(ws, source, method, &specs)?;
let (packages, resolve_with_overrides) = resolve;
let to_builds = specs
.iter()
.map(|p| {
let pkgid = p.query(resolve_with_overrides.iter())?;
let p = packages.get(pkgid)?;
p.manifest().print_teapot(ws.config());
Ok(p)
})
.collect::<CargoResult<Vec<_>>>()?;
let mut general_targets = Vec::new();
let mut package_targets = Vec::new();
match (target_rustc_args, target_rustdoc_args) {
(&Some(..), _) | (_, &Some(..)) if to_builds.len() != 1 => {
panic!("`rustc` and `rustdoc` should not accept multiple `-p` flags")
}
(&Some(ref args), _) => {
let all_features =
resolve_all_features(&resolve_with_overrides, to_builds[0].package_id());
let targets =
generate_targets(to_builds[0], profiles, mode, filter, &all_features, release)?;
if targets.len() == 1 {
let (target, profile) = targets[0];
let mut profile = profile.clone();
profile.rustc_args = Some(args.to_vec());
general_targets.push((target, profile));
} else {
bail!(
"extra arguments to `rustc` can only be passed to one \
target, consider filtering\nthe package by passing \
e.g. `--lib` or `--bin NAME` to specify a single target"
)
}
}
(&None, &Some(ref args)) => {
let all_features =
resolve_all_features(&resolve_with_overrides, to_builds[0].package_id());
let targets =
generate_targets(to_builds[0], profiles, mode, filter, &all_features, release)?;
if targets.len() == 1 {
let (target, profile) = targets[0];
let mut profile = profile.clone();
profile.rustdoc_args = Some(args.to_vec());
general_targets.push((target, profile));
} else {
bail!(
"extra arguments to `rustdoc` can only be passed to one \
target, consider filtering\nthe package by passing e.g. \
`--lib` or `--bin NAME` to specify a single target"
)
}
}
(&None, &None) => for &to_build in to_builds.iter() {
let all_features = resolve_all_features(&resolve_with_overrides, to_build.package_id());
let targets =
generate_targets(to_build, profiles, mode, filter, &all_features, release)?;
package_targets.push((to_build, targets));
},
};
for &(target, ref profile) in &general_targets {
for &to_build in to_builds.iter() {
package_targets.push((to_build, vec![(target, profile)]));
}
}
let mut ret = {
let _p = profile::start("compiling");
let mut build_config = scrape_build_config(config, jobs, target)?;
build_config.release = release;
build_config.test = mode == CompileMode::Test || mode == CompileMode::Bench;
build_config.json_messages = message_format == MessageFormat::Json;
if let CompileMode::Doc { deps } = mode {
build_config.doc_all = deps;
}
ops::compile_targets(
ws,
&package_targets,
&packages,
&resolve_with_overrides,
config,
build_config,
profiles,
&exec,
)?
};
ret.to_doc_test = to_builds.into_iter().cloned().collect();
return Ok(ret);
fn resolve_all_features(
resolve_with_overrides: &Resolve,
package_id: &PackageId,
) -> HashSet<String> {
let mut features = resolve_with_overrides.features(package_id).clone();
// Include features enabled for use by dependencies so targets can also use them with the
// required-features field when deciding whether to be built or skipped.
let deps = resolve_with_overrides.deps(package_id);
for dep in deps {
for feature in resolve_with_overrides.features(dep) {
features.insert(dep.name().to_string() + "/" + feature);
}
}
features
}
}
impl FilterRule {
pub fn new(targets: Vec<String>, all: bool) -> FilterRule {
if all {
FilterRule::All
} else {
FilterRule::Just(targets)
}
}
fn matches(&self, target: &Target) -> bool {
match *self {
FilterRule::All => true,
FilterRule::Just(ref targets) => targets.iter().any(|x| *x == target.name()),
}
}
fn is_specific(&self) -> bool {
match *self {
FilterRule::All => true,
FilterRule::Just(ref targets) => !targets.is_empty(),
}
}
pub fn try_collect(&self) -> Option<Vec<String>> {
match *self {
FilterRule::All => None,
FilterRule::Just(ref targets) => Some(targets.clone()),
}
}
}
impl CompileFilter {
pub fn new(
lib_only: bool,
bins: Vec<String>,
all_bins: bool,
tsts: Vec<String>,
all_tsts: bool,
exms: Vec<String>,
all_exms: bool,
bens: Vec<String>,
all_bens: bool,
all_targets: bool,
) -> CompileFilter {
let rule_bins = FilterRule::new(bins, all_bins);
let rule_tsts = FilterRule::new(tsts, all_tsts);
let rule_exms = FilterRule::new(exms, all_exms);
let rule_bens = FilterRule::new(bens, all_bens);
if all_targets {
CompileFilter::Only {
all_targets: true,
lib: true,
bins: FilterRule::All,
examples: FilterRule::All,
benches: FilterRule::All,
tests: FilterRule::All,
}
} else if lib_only || rule_bins.is_specific() || rule_tsts.is_specific()
|| rule_exms.is_specific() || rule_bens.is_specific()
{
CompileFilter::Only {
all_targets: false,
lib: lib_only,
bins: rule_bins,
examples: rule_exms,
benches: rule_bens,
tests: rule_tsts,
}
} else {
CompileFilter::Default {
required_features_filterable: true,
}
}
}
pub fn need_dev_deps(&self, mode: CompileMode) -> bool {
match mode {
CompileMode::Test | CompileMode::Doctest | CompileMode::Bench => true,
CompileMode::Build | CompileMode::Doc { .. } | CompileMode::Check { .. } => match *self
{
CompileFilter::Default { .. } => false,
CompileFilter::Only {
ref examples,
ref tests,
ref benches,
..
} => examples.is_specific() || tests.is_specific() || benches.is_specific(),
},
}
}
// this selects targets for "cargo run". for logic to select targets for
// other subcommands, see generate_targets and generate_default_targets
pub fn target_run(&self, target: &Target) -> bool {
match *self {
CompileFilter::Default { .. } => true,
CompileFilter::Only {
lib,
ref bins,
ref examples,
ref tests,
ref benches,
..
} => {
let rule = match *target.kind() {
TargetKind::Bin => bins,
TargetKind::Test => tests,
TargetKind::Bench => benches,
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => examples,
TargetKind::Lib(..) => return lib,
TargetKind::CustomBuild => return false,
};
rule.matches(target)
}
}
}
pub fn is_specific(&self) -> bool {
match *self {
CompileFilter::Default { .. } => false,
CompileFilter::Only { .. } => true,
}
}
}
#[derive(Clone, Copy, Debug)]
struct BuildProposal<'a> {
target: &'a Target,
profile: &'a Profile,
required: bool,
}
fn generate_default_targets<'a>(
mode: CompileMode,
targets: &'a [Target],
profile: &'a Profile,
dep: &'a Profile,
required_features_filterable: bool,
) -> Vec<BuildProposal<'a>> {
match mode {
CompileMode::Bench => targets
.iter()
.filter(|t| t.benched())
.map(|t| BuildProposal {
target: t,
profile,
required: !required_features_filterable,
})
.collect::<Vec<_>>(),
CompileMode::Test => {
let mut base = targets
.iter()
.filter(|t| t.tested())
.map(|t| BuildProposal {
target: t,
profile: if t.is_example() { dep } else { profile },
required: !required_features_filterable,
})
.collect::<Vec<_>>();
// Always compile the library if we're testing everything as
// it'll be needed for doctests
if let Some(t) = targets.iter().find(|t| t.is_lib()) {
if t.doctested() {
base.push(BuildProposal {
target: t,
profile: dep,
required: !required_features_filterable,
});
}
}
base
}
CompileMode::Build | CompileMode::Check { .. } => targets
.iter()
.filter(|t| t.is_bin() || t.is_lib())
.map(|t| BuildProposal {
target: t,
profile,
required: !required_features_filterable,
})
.collect(),
CompileMode::Doc { .. } => targets
.iter()
.filter(|t| {
t.documented()
&& (!t.is_bin() || !targets.iter().any(|l| l.is_lib() && l.name() == t.name()))
})
.map(|t| BuildProposal {
target: t,
profile,
required: !required_features_filterable,
})
.collect(),
CompileMode::Doctest => {
if let Some(t) = targets.iter().find(|t| t.is_lib()) {
if t.doctested() {
return vec![
BuildProposal {
target: t,
profile,
required: !required_features_filterable,
},
];
}
}
Vec::new()
}
}
}
/// Given a filter rule and some context, propose a list of targets
fn propose_indicated_targets<'a>(
pkg: &'a Package,
rule: &FilterRule,
desc: &'static str,
is_expected_kind: fn(&Target) -> bool,
profile: &'a Profile,
) -> CargoResult<Vec<BuildProposal<'a>>> {
match *rule {
FilterRule::All => {
let result = pkg.targets()
.iter()
.filter(|t| is_expected_kind(t))
.map(|t| BuildProposal {
target: t,
profile,
required: false,
});
Ok(result.collect())
}
FilterRule::Just(ref names) => {
let mut targets = Vec::new();
for name in names {
let target = pkg.targets()
.iter()
.find(|t| t.name() == *name && is_expected_kind(t));
let t = match target {
Some(t) => t,
None => {
let suggestion = pkg.find_closest_target(name, is_expected_kind);
match suggestion {
Some(s) => {
let suggested_name = s.name();
bail!(
"no {} target named `{}`\n\nDid you mean `{}`?",
desc,
name,
suggested_name
)
}
None => bail!("no {} target named `{}`", desc, name),
}
}
};
debug!("found {} `{}`", desc, name);
targets.push(BuildProposal {
target: t,
profile,
required: true,
});
}
Ok(targets)
}
}
}
/// Collect the targets that are libraries or have all required features available.
fn filter_compatible_targets<'a>(
mut proposals: Vec<BuildProposal<'a>>,
features: &HashSet<String>,
) -> CargoResult<Vec<(&'a Target, &'a Profile)>> {
let mut compatible = Vec::with_capacity(proposals.len());
for proposal in proposals.drain(..) {
let unavailable_features = match proposal.target.required_features() {
Some(rf) => rf.iter().filter(|f| !features.contains(*f)).collect(),
None => Vec::new(),
};
if proposal.target.is_lib() || unavailable_features.is_empty() {
compatible.push((proposal.target, proposal.profile));
} else if proposal.required {
let required_features = proposal.target.required_features().unwrap();
let quoted_required_features: Vec<String> = required_features
.iter()
.map(|s| format!("`{}`", s))
.collect();
bail!(
"target `{}` requires the features: {}\n\
Consider enabling them by passing e.g. `--features=\"{}\"`",
proposal.target.name(),
quoted_required_features.join(", "),
required_features.join(" ")
);
}
}
Ok(compatible)
}
/// Given the configuration for a build, this function will generate all
/// target/profile combinations needed to be built.
fn generate_targets<'a>(
pkg: &'a Package,
profiles: &'a Profiles,
mode: CompileMode,
filter: &CompileFilter,
features: &HashSet<String>,
release: bool,
) -> CargoResult<Vec<(&'a Target, &'a Profile)>> {
let build = if release {
&profiles.release
} else {
&profiles.dev
};
let test = if release {
&profiles.bench
} else {
&profiles.test
};
let profile = match mode {
CompileMode::Test => test,
CompileMode::Bench => &profiles.bench,
CompileMode::Build => build,
CompileMode::Check { test: false } => &profiles.check,
CompileMode::Check { test: true } => &profiles.check_test,
CompileMode::Doc { .. } => &profiles.doc,
CompileMode::Doctest => &profiles.doctest,
};
let test_profile = if profile.check {
&profiles.check_test
} else if mode == CompileMode::Build {
test
} else {
profile
};
let bench_profile = if profile.check {
&profiles.check_test
} else if mode == CompileMode::Build {
&profiles.bench
} else {
profile
};
let targets = match *filter {
CompileFilter::Default {
required_features_filterable,
} => {
let deps = if release {
&profiles.bench_deps
} else {
&profiles.test_deps
};
generate_default_targets(
mode,
pkg.targets(),
profile,
deps,
required_features_filterable,
)
}
CompileFilter::Only {
all_targets,
lib,
ref bins,
ref examples,
ref tests,
ref benches,
} => {
let mut targets = Vec::new();
if lib {
if let Some(t) = pkg.targets().iter().find(|t| t.is_lib()) {
targets.push(BuildProposal {
target: t,
profile,
required: true,
});
} else if !all_targets {
bail!("no library targets found")
}
}
targets.append(&mut propose_indicated_targets(
pkg,
bins,
"bin",
Target::is_bin,
profile,
)?);
targets.append(&mut propose_indicated_targets(
pkg,
examples,
"example",
Target::is_example,
profile,
)?);
// If --tests was specified, add all targets that would be
// generated by `cargo test`.
let test_filter = match *tests {
FilterRule::All => Target::tested,
FilterRule::Just(_) => Target::is_test,
};
targets.append(&mut propose_indicated_targets(
pkg,
tests,
"test",
test_filter,
test_profile,
)?);
// If --benches was specified, add all targets that would be
// generated by `cargo bench`.
let bench_filter = match *benches {
FilterRule::All => Target::benched,
FilterRule::Just(_) => Target::is_bench,
};
targets.append(&mut propose_indicated_targets(
pkg,
benches,
"bench",
bench_filter,
bench_profile,
)?);
targets
}
};
filter_compatible_targets(targets, features)
}
/// Parse all config files to learn about build configuration. Currently
/// configured options are:
///
/// * build.jobs
/// * build.target
/// * target.$target.ar
/// * target.$target.linker
/// * target.$target.libfoo.metadata
fn scrape_build_config(
config: &Config,
jobs: Option<u32>,
target: Option<String>,
) -> CargoResult<ops::BuildConfig> {
if jobs.is_some() && config.jobserver_from_env().is_some() {
config.shell().warn(
"a `-j` argument was passed to Cargo but Cargo is \
also configured with an external jobserver in \
its environment, ignoring the `-j` parameter",
)?;
}
let cfg_jobs = match config.get_i64("build.jobs")? {
Some(v) => {
if v.val <= 0 {
bail!(
"build.jobs must be positive, but found {} in {}",
v.val,
v.definition
)
} else if v.val >= i64::from(u32::max_value()) {
bail!(
"build.jobs is too large: found {} in {}",
v.val,
v.definition
)
} else {
Some(v.val as u32)
}
}
None => None,
};
let jobs = jobs.or(cfg_jobs).unwrap_or(::num_cpus::get() as u32);
let cfg_target = config.get_string("build.target")?.map(|s| s.val);
let target = target.or(cfg_target);
let mut base = ops::BuildConfig {
host_triple: config.rustc()?.host.clone(),
requested_target: target.clone(),
jobs,
..Default::default()
};
base.host = scrape_target_config(config, &base.host_triple)?;
base.target = match target.as_ref() {
Some(triple) => scrape_target_config(config, triple)?,
None => base.host.clone(),
};
Ok(base)
}
fn scrape_target_config(config: &Config, triple: &str) -> CargoResult<ops::TargetConfig> {
let key = format!("target.{}", triple);
let mut ret = ops::TargetConfig {
ar: config.get_path(&format!("{}.ar", key))?.map(|v| v.val),
linker: config.get_path(&format!("{}.linker", key))?.map(|v| v.val),
overrides: HashMap::new(),
};
let table = match config.get_table(&key)? {
Some(table) => table.val,
None => return Ok(ret),
};
for (lib_name, value) in table {
match lib_name.as_str() {
"ar" | "linker" | "runner" | "rustflags" => continue,
_ => {}
}
let mut output = BuildOutput {
library_paths: Vec::new(),
library_links: Vec::new(),
cfgs: Vec::new(),
env: Vec::new(),
metadata: Vec::new(),
rerun_if_changed: Vec::new(),
rerun_if_env_changed: Vec::new(),
warnings: Vec::new(),
};
// We require deterministic order of evaluation, so we must sort the pairs by key first.
let mut pairs = Vec::new();
for (k, value) in value.table(&lib_name)?.0 {
pairs.push((k, value));
}
pairs.sort_by_key(|p| p.0);
for (k, value) in pairs {
let key = format!("{}.{}", key, k);
match &k[..] {
"rustc-flags" => {
let (flags, definition) = value.string(k)?;
let whence = format!("in `{}` (in {})", key, definition.display());
let (paths, links) = BuildOutput::parse_rustc_flags(flags, &whence)?;
output.library_paths.extend(paths);
output.library_links.extend(links);
}
"rustc-link-lib" => {
let list = value.list(k)?;
output
.library_links
.extend(list.iter().map(|v| v.0.clone()));
}
"rustc-link-search" => {
let list = value.list(k)?;
output
.library_paths
.extend(list.iter().map(|v| PathBuf::from(&v.0)));
}
"rustc-cfg" => {
let list = value.list(k)?;
output.cfgs.extend(list.iter().map(|v| v.0.clone()));
}
"rustc-env" => for (name, val) in value.table(k)?.0 {
let val = val.string(name)?.0;
output.env.push((name.clone(), val.to_string()));
},
"warning" | "rerun-if-changed" | "rerun-if-env-changed" => {
bail!("`{}` is not supported in build script overrides", k);
}
_ => {
let val = value.string(k)?.0;
output.metadata.push((k.clone(), val.to_string()));
}
}
}
ret.overrides.insert(lib_name, output);
}
Ok(ret)
}