-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
mod.rs
1118 lines (998 loc) · 37 KB
/
mod.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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::collections::{HashMap, HashSet};
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io::{self, Write};
use std::path::{self, PathBuf};
use std::sync::Arc;
use same_file::is_same_file;
use serde_json;
use core::{Feature, Package, PackageId, PackageSet, Resolve, Target};
use core::{Profile, Profiles, Workspace};
use core::manifest::Lto;
use core::shell::ColorChoice;
use util::{self, machine_message, ProcessBuilder};
use util::{internal, join_paths, profile, Config};
use util::paths;
use util::errors::{CargoResult, CargoResultExt, Internal};
use util::Freshness;
use self::job::{Job, Work};
use self::job_queue::JobQueue;
use self::output_depinfo::output_depinfo;
pub use self::compilation::Compilation;
pub use self::context::{Context, TargetFileType, Unit};
pub use self::custom_build::{BuildMap, BuildOutput, BuildScripts};
pub use self::layout::is_bad_artifact_name;
mod compilation;
mod context;
mod custom_build;
mod fingerprint;
mod job;
mod job_queue;
mod layout;
mod links;
mod output_depinfo;
/// Whether an object is for the host arch, or the target arch.
///
/// These will be the same unless cross-compiling.
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, PartialOrd, Ord)]
pub enum Kind {
Host,
Target,
}
/// Configuration information for a rustc build.
#[derive(Default, Clone)]
pub struct BuildConfig {
/// The host arch triple
///
/// e.g. x86_64-unknown-linux-gnu, would be
/// - machine: x86_64
/// - hardware-platform: unknown
/// - operating system: linux-gnu
pub host_triple: String,
/// Build information for the host arch
pub host: TargetConfig,
/// The target arch triple, defaults to host arch
pub requested_target: Option<String>,
/// Build information for the target
pub target: TargetConfig,
/// How many rustc jobs to run in parallel
pub jobs: u32,
/// Whether we are building for release
pub release: bool,
/// Whether we are running tests
pub test: bool,
/// Whether we are building documentation
pub doc_all: bool,
/// Whether to print std output in json format (for machine reading)
pub json_messages: bool,
}
/// Information required to build for a target
#[derive(Clone, Default)]
pub struct TargetConfig {
/// The path of archiver (lib builder) for this target.
pub ar: Option<PathBuf>,
/// The path of the linker for this target.
pub linker: Option<PathBuf>,
/// Special build options for any necessary input files (filename -> options)
pub overrides: HashMap<String, BuildOutput>,
}
pub type PackagesToBuild<'a> = [(&'a Package, Vec<(&'a Target, &'a Profile)>)];
/// A glorified callback for executing calls to rustc. Rather than calling rustc
/// directly, we'll use an Executor, giving clients an opportunity to intercept
/// the build calls.
pub trait Executor: Send + Sync + 'static {
/// Called after a rustc process invocation is prepared up-front for a given
/// unit of work (may still be modified for runtime-known dependencies, when
/// the work is actually executed).
fn init(&self, _cx: &Context, _unit: &Unit) {}
/// In case of an `Err`, Cargo will not continue with the build process for
/// this package.
fn exec(&self, cmd: ProcessBuilder, _id: &PackageId, _target: &Target) -> CargoResult<()> {
cmd.exec()?;
Ok(())
}
fn exec_json(
&self,
cmd: ProcessBuilder,
_id: &PackageId,
_target: &Target,
handle_stdout: &mut FnMut(&str) -> CargoResult<()>,
handle_stderr: &mut FnMut(&str) -> CargoResult<()>,
) -> CargoResult<()> {
cmd.exec_with_streaming(handle_stdout, handle_stderr, false)?;
Ok(())
}
/// Queried when queuing each unit of work. If it returns true, then the
/// unit will always be rebuilt, independent of whether it needs to be.
fn force_rebuild(&self, _unit: &Unit) -> bool {
false
}
}
/// A `DefaultExecutor` calls rustc without doing anything else. It is Cargo's
/// default behaviour.
#[derive(Copy, Clone)]
pub struct DefaultExecutor;
impl Executor for DefaultExecutor {}
// Returns a mapping of the root package plus its immediate dependencies to
// where the compiled libraries are all located.
pub fn compile_targets<'a, 'cfg: 'a>(
ws: &Workspace<'cfg>,
pkg_targets: &'a PackagesToBuild<'a>,
packages: &'a PackageSet<'cfg>,
resolve: &'a Resolve,
config: &'cfg Config,
build_config: BuildConfig,
profiles: &'a Profiles,
exec: &Arc<Executor>,
) -> CargoResult<Compilation<'cfg>> {
let units = pkg_targets
.iter()
.flat_map(|&(pkg, ref targets)| {
let default_kind = if build_config.requested_target.is_some() {
Kind::Target
} else {
Kind::Host
};
targets.iter().map(move |&(target, profile)| Unit {
pkg,
target,
profile,
kind: if target.for_host() {
Kind::Host
} else {
default_kind
},
})
})
.collect::<Vec<_>>();
let mut cx = Context::new(ws, resolve, packages, config, build_config, profiles)?;
let mut queue = JobQueue::new(&cx);
cx.prepare()?;
cx.probe_target_info(&units)?;
cx.build_used_in_plugin_map(&units)?;
custom_build::build_map(&mut cx, &units)?;
for unit in units.iter() {
// Build up a list of pending jobs, each of which represent
// compiling a particular package. No actual work is executed as
// part of this, that's all done next as part of the `execute`
// function which will run everything in order with proper
// parallelism.
compile(&mut cx, &mut queue, unit, exec)?;
}
// Now that we've figured out everything that we're going to do, do it!
queue.execute(&mut cx)?;
for unit in units.iter() {
for &(ref dst, ref link_dst, file_type) in cx.target_filenames(unit)?.iter() {
if file_type == TargetFileType::DebugInfo {
continue;
}
let bindst = match *link_dst {
Some(ref link_dst) => link_dst,
None => dst,
};
if unit.profile.test {
cx.compilation.tests.push((
unit.pkg.clone(),
unit.target.kind().clone(),
unit.target.name().to_string(),
dst.clone(),
));
} else if unit.target.is_bin() || unit.target.is_example() {
cx.compilation.binaries.push(bindst.clone());
} else if unit.target.is_lib() {
let pkgid = unit.pkg.package_id().clone();
cx.compilation
.libraries
.entry(pkgid)
.or_insert_with(HashSet::new)
.insert((unit.target.clone(), dst.clone()));
}
}
for dep in cx.dep_targets(unit)?.iter() {
if !unit.target.is_lib() {
continue;
}
if dep.profile.run_custom_build {
let out_dir = cx.build_script_out_dir(dep).display().to_string();
cx.compilation
.extra_env
.entry(dep.pkg.package_id().clone())
.or_insert_with(Vec::new)
.push(("OUT_DIR".to_string(), out_dir));
}
if !dep.target.is_lib() {
continue;
}
if dep.profile.doc {
continue;
}
let v = cx.target_filenames(dep)?;
cx.compilation
.libraries
.entry(unit.pkg.package_id().clone())
.or_insert_with(HashSet::new)
.extend(
v.iter()
.map(|&(ref f, _, _)| (dep.target.clone(), f.clone())),
);
}
let feats = cx.resolve.features(unit.pkg.package_id());
if !feats.is_empty() {
cx.compilation
.cfgs
.entry(unit.pkg.package_id().clone())
.or_insert_with(|| {
feats
.iter()
.map(|feat| format!("feature=\"{}\"", feat))
.collect()
});
}
let rustdocflags = cx.rustdocflags_args(unit)?;
if !rustdocflags.is_empty() {
cx.compilation
.rustdocflags
.entry(unit.pkg.package_id().clone())
.or_insert(rustdocflags);
}
output_depinfo(&mut cx, unit)?;
}
for (&(ref pkg, _), output) in cx.build_state.outputs.lock().unwrap().iter() {
cx.compilation
.cfgs
.entry(pkg.clone())
.or_insert_with(HashSet::new)
.extend(output.cfgs.iter().cloned());
cx.compilation
.extra_env
.entry(pkg.clone())
.or_insert_with(Vec::new)
.extend(output.env.iter().cloned());
for dir in output.library_paths.iter() {
cx.compilation.native_dirs.insert(dir.clone());
}
}
cx.compilation.target = cx.target_triple().to_string();
Ok(cx.compilation)
}
fn compile<'a, 'cfg: 'a>(
cx: &mut Context<'a, 'cfg>,
jobs: &mut JobQueue<'a>,
unit: &Unit<'a>,
exec: &Arc<Executor>,
) -> CargoResult<()> {
if !cx.compiled.insert(*unit) {
return Ok(());
}
// Build up the work to be done to compile this unit, enqueuing it once
// we've got everything constructed.
let p = profile::start(format!("preparing: {}/{}", unit.pkg, unit.target.name()));
fingerprint::prepare_init(cx, unit)?;
cx.links.validate(cx.resolve, unit)?;
let (dirty, fresh, freshness) = if unit.profile.run_custom_build {
custom_build::prepare(cx, unit)?
} else if unit.profile.doc && unit.profile.test {
// we run these targets later, so this is just a noop for now
(Work::noop(), Work::noop(), Freshness::Fresh)
} else {
let (mut freshness, dirty, fresh) = fingerprint::prepare_target(cx, unit)?;
let work = if unit.profile.doc {
rustdoc(cx, unit)?
} else {
rustc(cx, unit, exec)?
};
// Need to link targets on both the dirty and fresh
let dirty = work.then(link_targets(cx, unit, false)?).then(dirty);
let fresh = link_targets(cx, unit, true)?.then(fresh);
if exec.force_rebuild(unit) {
freshness = Freshness::Dirty;
}
(dirty, fresh, freshness)
};
jobs.enqueue(cx, unit, Job::new(dirty, fresh), freshness)?;
drop(p);
// Be sure to compile all dependencies of this target as well.
for unit in cx.dep_targets(unit)?.iter() {
compile(cx, jobs, unit, exec)?;
}
Ok(())
}
fn rustc<'a, 'cfg>(
cx: &mut Context<'a, 'cfg>,
unit: &Unit<'a>,
exec: &Arc<Executor>,
) -> CargoResult<Work> {
let mut rustc = prepare_rustc(cx, &unit.target.rustc_crate_types(), unit)?;
let name = unit.pkg.name().to_string();
// If this is an upstream dep we don't want warnings from, turn off all
// lints.
if !cx.show_warnings(unit.pkg.package_id()) {
rustc.arg("--cap-lints").arg("allow");
// If this is an upstream dep but we *do* want warnings, make sure that they
// don't fail compilation.
} else if !unit.pkg.package_id().source_id().is_path() {
rustc.arg("--cap-lints").arg("warn");
}
let filenames = cx.target_filenames(unit)?;
let root = cx.out_dir(unit);
let kind = unit.kind;
// Prepare the native lib state (extra -L and -l flags)
let build_state = cx.build_state.clone();
let current_id = unit.pkg.package_id().clone();
let build_deps = load_build_deps(cx, unit);
// If we are a binary and the package also contains a library, then we
// don't pass the `-l` flags.
let pass_l_flag = unit.target.is_lib() || !unit.pkg.targets().iter().any(|t| t.is_lib());
let do_rename = unit.target.allows_underscores() && !unit.profile.test;
let real_name = unit.target.name().to_string();
let crate_name = unit.target.crate_name();
// XXX(Rely on target_filenames iterator as source of truth rather than rederiving filestem)
let rustc_dep_info_loc = if do_rename && cx.target_metadata(unit).is_none() {
root.join(&crate_name)
} else {
root.join(&cx.file_stem(unit))
}.with_extension("d");
let dep_info_loc = fingerprint::dep_info_loc(cx, unit);
rustc.args(&cx.rustflags_args(unit)?);
let json_messages = cx.build_config.json_messages;
let package_id = unit.pkg.package_id().clone();
let target = unit.target.clone();
exec.init(cx, unit);
let exec = exec.clone();
let root_output = cx.target_root().to_path_buf();
let pkg_root = unit.pkg.root().to_path_buf();
let cwd = rustc
.get_cwd()
.unwrap_or_else(|| cx.config.cwd())
.to_path_buf();
return Ok(Work::new(move |state| {
// Only at runtime have we discovered what the extra -L and -l
// arguments are for native libraries, so we process those here. We
// also need to be sure to add any -L paths for our plugins to the
// dynamic library load path as a plugin's dynamic library may be
// located somewhere in there.
// Finally, if custom environment variables have been produced by
// previous build scripts, we include them in the rustc invocation.
if let Some(build_deps) = build_deps {
let build_state = build_state.outputs.lock().unwrap();
add_native_deps(
&mut rustc,
&build_state,
&build_deps,
pass_l_flag,
¤t_id,
)?;
add_plugin_deps(&mut rustc, &build_state, &build_deps, &root_output)?;
add_custom_env(&mut rustc, &build_state, ¤t_id, kind)?;
}
for &(ref filename, ref _link_dst, _linkable) in filenames.iter() {
// If there is both an rmeta and rlib, rustc will prefer to use the
// rlib, even if it is older. Therefore, we must delete the rlib to
// force using the new rmeta.
if filename.extension() == Some(OsStr::new("rmeta")) {
let dst = root.join(filename).with_extension("rlib");
if dst.exists() {
paths::remove_file(&dst)?;
}
}
}
state.running(&rustc);
if json_messages {
exec.exec_json(
rustc,
&package_id,
&target,
&mut |line| {
if !line.is_empty() {
Err(internal(&format!(
"compiler stdout is not empty: `{}`",
line
)))
} else {
Ok(())
}
},
&mut |line| {
// stderr from rustc can have a mix of JSON and non-JSON output
if line.starts_with('{') {
// Handle JSON lines
let compiler_message = serde_json::from_str(line).map_err(|_| {
internal(&format!("compiler produced invalid json: `{}`", line))
})?;
machine_message::emit(&machine_message::FromCompiler {
package_id: &package_id,
target: &target,
message: compiler_message,
});
} else {
// Forward non-JSON to stderr
writeln!(io::stderr(), "{}", line)?;
}
Ok(())
},
).chain_err(|| format!("Could not compile `{}`.", name))?;
} else {
exec.exec(rustc, &package_id, &target)
.map_err(Internal::new)
.chain_err(|| format!("Could not compile `{}`.", name))?;
}
if do_rename && real_name != crate_name {
let dst = &filenames[0].0;
let src = dst.with_file_name(
dst.file_name()
.unwrap()
.to_str()
.unwrap()
.replace(&real_name, &crate_name),
);
if src.exists() && src.file_name() != dst.file_name() {
fs::rename(&src, &dst)
.chain_err(|| internal(format!("could not rename crate {:?}", src)))?;
}
}
if rustc_dep_info_loc.exists() {
fingerprint::translate_dep_info(&rustc_dep_info_loc, &dep_info_loc, &pkg_root, &cwd)
.chain_err(|| {
internal(format!(
"could not parse/generate dep info at: {}",
rustc_dep_info_loc.display()
))
})?;
}
Ok(())
}));
// Add all relevant -L and -l flags from dependencies (now calculated and
// present in `state`) to the command provided
fn add_native_deps(
rustc: &mut ProcessBuilder,
build_state: &BuildMap,
build_scripts: &BuildScripts,
pass_l_flag: bool,
current_id: &PackageId,
) -> CargoResult<()> {
for key in build_scripts.to_link.iter() {
let output = build_state.get(key).ok_or_else(|| {
internal(format!(
"couldn't find build state for {}/{:?}",
key.0, key.1
))
})?;
for path in output.library_paths.iter() {
rustc.arg("-L").arg(path);
}
if key.0 == *current_id {
for cfg in &output.cfgs {
rustc.arg("--cfg").arg(cfg);
}
if pass_l_flag {
for name in output.library_links.iter() {
rustc.arg("-l").arg(name);
}
}
}
}
Ok(())
}
// Add all custom environment variables present in `state` (after they've
// been put there by one of the `build_scripts`) to the command provided.
fn add_custom_env(
rustc: &mut ProcessBuilder,
build_state: &BuildMap,
current_id: &PackageId,
kind: Kind,
) -> CargoResult<()> {
let key = (current_id.clone(), kind);
if let Some(output) = build_state.get(&key) {
for &(ref name, ref value) in output.env.iter() {
rustc.env(name, value);
}
}
Ok(())
}
}
/// Link the compiled target (often of form `foo-{metadata_hash}`) to the
/// final target. This must happen during both "Fresh" and "Compile"
fn link_targets<'a, 'cfg>(
cx: &mut Context<'a, 'cfg>,
unit: &Unit<'a>,
fresh: bool,
) -> CargoResult<Work> {
let filenames = cx.target_filenames(unit)?;
let package_id = unit.pkg.package_id().clone();
let target = unit.target.clone();
let profile = unit.profile.clone();
let features = cx.resolve
.features_sorted(&package_id)
.into_iter()
.map(|s| s.to_owned())
.collect();
let json_messages = cx.build_config.json_messages;
Ok(Work::new(move |_| {
// If we're a "root crate", e.g. the target of this compilation, then we
// hard link our outputs out of the `deps` directory into the directory
// above. This means that `cargo build` will produce binaries in
// `target/debug` which one probably expects.
let mut destinations = vec![];
for &(ref src, ref link_dst, _file_type) in filenames.iter() {
// This may have been a `cargo rustc` command which changes the
// output, so the source may not actually exist.
if !src.exists() {
continue;
}
let dst = match link_dst.as_ref() {
Some(dst) => dst,
None => {
destinations.push(src.display().to_string());
continue;
}
};
destinations.push(dst.display().to_string());
debug!("linking {} to {}", src.display(), dst.display());
if is_same_file(src, dst).unwrap_or(false) {
continue;
}
if dst.exists() {
paths::remove_file(&dst)?;
}
let link_result = if src.is_dir() {
#[cfg(unix)]
use std::os::unix::fs::symlink;
#[cfg(target_os = "redox")]
use std::os::redox::fs::symlink;
#[cfg(windows)]
use std::os::windows::fs::symlink_dir as symlink;
let dst_dir = dst.parent().unwrap();
assert!(src.starts_with(dst_dir));
symlink(src.strip_prefix(dst_dir).unwrap(), dst)
} else {
fs::hard_link(src, dst)
};
link_result
.or_else(|err| {
debug!("link failed {}. falling back to fs::copy", err);
fs::copy(src, dst).map(|_| ())
})
.chain_err(|| {
format!(
"failed to link or copy `{}` to `{}`",
src.display(),
dst.display()
)
})?;
}
if json_messages {
machine_message::emit(&machine_message::Artifact {
package_id: &package_id,
target: &target,
profile: &profile,
features,
filenames: destinations,
fresh,
});
}
Ok(())
}))
}
fn load_build_deps(cx: &Context, unit: &Unit) -> Option<Arc<BuildScripts>> {
cx.build_scripts.get(unit).cloned()
}
// For all plugin dependencies, add their -L paths (now calculated and
// present in `state`) to the dynamic library load path for the command to
// execute.
fn add_plugin_deps(
rustc: &mut ProcessBuilder,
build_state: &BuildMap,
build_scripts: &BuildScripts,
root_output: &PathBuf,
) -> CargoResult<()> {
let var = util::dylib_path_envvar();
let search_path = rustc.get_env(var).unwrap_or_default();
let mut search_path = env::split_paths(&search_path).collect::<Vec<_>>();
for id in build_scripts.plugins.iter() {
let key = (id.clone(), Kind::Host);
let output = build_state
.get(&key)
.ok_or_else(|| internal(format!("couldn't find libs for plugin dep {}", id)))?;
search_path.append(&mut filter_dynamic_search_path(
output.library_paths.iter(),
root_output,
));
}
let search_path = join_paths(&search_path, var)?;
rustc.env(var, &search_path);
Ok(())
}
// Determine paths to add to the dynamic search path from -L entries
//
// Strip off prefixes like "native=" or "framework=" and filter out directories
// *not* inside our output directory since they are likely spurious and can cause
// clashes with system shared libraries (issue #3366).
fn filter_dynamic_search_path<'a, I>(paths: I, root_output: &PathBuf) -> Vec<PathBuf>
where
I: Iterator<Item = &'a PathBuf>,
{
let mut search_path = vec![];
for dir in paths {
let dir = match dir.to_str() {
Some(s) => {
let mut parts = s.splitn(2, '=');
match (parts.next(), parts.next()) {
(Some("native"), Some(path))
| (Some("crate"), Some(path))
| (Some("dependency"), Some(path))
| (Some("framework"), Some(path))
| (Some("all"), Some(path)) => path.into(),
_ => dir.clone(),
}
}
None => dir.clone(),
};
if dir.starts_with(&root_output) {
search_path.push(dir);
} else {
debug!(
"Not including path {} in runtime library search path because it is \
outside target root {}",
dir.display(),
root_output.display()
);
}
}
search_path
}
fn prepare_rustc<'a, 'cfg>(
cx: &mut Context<'a, 'cfg>,
crate_types: &[&str],
unit: &Unit<'a>,
) -> CargoResult<ProcessBuilder> {
let mut base = cx.compilation.rustc_process(unit.pkg)?;
base.inherit_jobserver(&cx.jobserver);
build_base_args(cx, &mut base, unit, crate_types)?;
build_deps_args(&mut base, cx, unit)?;
Ok(base)
}
fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult<Work> {
let mut rustdoc = cx.compilation.rustdoc_process(unit.pkg)?;
rustdoc.inherit_jobserver(&cx.jobserver);
rustdoc.arg("--crate-name").arg(&unit.target.crate_name());
add_path_args(cx, unit, &mut rustdoc);
if unit.kind != Kind::Host {
if let Some(target) = cx.requested_target() {
rustdoc.arg("--target").arg(target);
}
}
let doc_dir = cx.out_dir(unit);
// Create the documentation directory ahead of time as rustdoc currently has
// a bug where concurrent invocations will race to create this directory if
// it doesn't already exist.
fs::create_dir_all(&doc_dir)?;
rustdoc.arg("-o").arg(doc_dir);
for feat in cx.resolve.features_sorted(unit.pkg.package_id()) {
rustdoc.arg("--cfg").arg(&format!("feature=\"{}\"", feat));
}
if let Some(ref args) = unit.profile.rustdoc_args {
rustdoc.args(args);
}
build_deps_args(&mut rustdoc, cx, unit)?;
rustdoc.args(&cx.rustdocflags_args(unit)?);
let name = unit.pkg.name().to_string();
let build_state = cx.build_state.clone();
let key = (unit.pkg.package_id().clone(), unit.kind);
Ok(Work::new(move |state| {
if let Some(output) = build_state.outputs.lock().unwrap().get(&key) {
for cfg in output.cfgs.iter() {
rustdoc.arg("--cfg").arg(cfg);
}
for &(ref name, ref value) in output.env.iter() {
rustdoc.env(name, value);
}
}
state.running(&rustdoc);
rustdoc
.exec()
.chain_err(|| format!("Could not document `{}`.", name))?;
Ok(())
}))
}
// The path that we pass to rustc is actually fairly important because it will
// show up in error messages (important for readability), debug information
// (important for caching), etc. As a result we need to be pretty careful how we
// actually invoke rustc.
//
// In general users don't expect `cargo build` to cause rebuilds if you change
// directories. That could be if you just change directories in the project or
// if you literally move the whole project wholesale to a new directory. As a
// result we mostly don't factor in `cwd` to this calculation. Instead we try to
// track the workspace as much as possible and we update the current directory
// of rustc/rustdoc where approrpriate.
//
// The first returned value here is the argument to pass to rustc, and the
// second is the cwd that rustc should operate in.
fn path_args(cx: &Context, unit: &Unit) -> (PathBuf, PathBuf) {
let ws_root = cx.ws.root();
let src = unit.target.src_path();
assert!(src.is_absolute());
match src.strip_prefix(ws_root) {
Ok(path) => (path.to_path_buf(), ws_root.to_path_buf()),
Err(_) => (src.to_path_buf(), unit.pkg.root().to_path_buf()),
}
}
fn add_path_args(cx: &Context, unit: &Unit, cmd: &mut ProcessBuilder) {
let (arg, cwd) = path_args(cx, unit);
cmd.arg(arg);
cmd.cwd(cwd);
}
fn build_base_args<'a, 'cfg>(
cx: &mut Context<'a, 'cfg>,
cmd: &mut ProcessBuilder,
unit: &Unit<'a>,
crate_types: &[&str],
) -> CargoResult<()> {
let Profile {
ref opt_level,
ref lto,
codegen_units,
ref rustc_args,
debuginfo,
debug_assertions,
overflow_checks,
rpath,
test,
doc: _doc,
run_custom_build,
ref panic,
check,
..
} = *unit.profile;
assert!(!run_custom_build);
cmd.arg("--crate-name").arg(&unit.target.crate_name());
add_path_args(cx, unit, cmd);
match cx.config.shell().color_choice() {
ColorChoice::Always => {
cmd.arg("--color").arg("always");
}
ColorChoice::Never => {
cmd.arg("--color").arg("never");
}
ColorChoice::CargoAuto => {}
}
if cx.build_config.json_messages {
cmd.arg("--error-format").arg("json");
}
if !test {
for crate_type in crate_types.iter() {
cmd.arg("--crate-type").arg(crate_type);
}
}
if check {
cmd.arg("--emit=dep-info,metadata");
} else {
cmd.arg("--emit=dep-info,link");
}
let prefer_dynamic = (unit.target.for_host() && !unit.target.is_custom_build())
|| (crate_types.contains(&"dylib") && cx.ws.members().any(|p| p != unit.pkg));
if prefer_dynamic {
cmd.arg("-C").arg("prefer-dynamic");
}
if opt_level != "0" {
cmd.arg("-C").arg(&format!("opt-level={}", opt_level));
}
// If a panic mode was configured *and* we're not ever going to be used in a
// plugin, then we can compile with that panic mode.
//
// If we're used in a plugin then we'll eventually be linked to libsyntax
// most likely which isn't compiled with a custom panic mode, so we'll just
// get an error if we actually compile with that. This fixes `panic=abort`
// crates which have plugin dependencies, but unfortunately means that
// dependencies shared between the main application and plugins must be
// compiled without `panic=abort`. This isn't so bad, though, as the main
// application will still be compiled with `panic=abort`.
if let Some(panic) = panic.as_ref() {
if !cx.used_in_plugin.contains(unit) {
cmd.arg("-C").arg(format!("panic={}", panic));
}
}
let manifest = unit.pkg.manifest();
if manifest.features().is_enabled(Feature::epoch()) {
cmd.arg(format!("-Zepoch={}", manifest.epoch()));
}
// Disable LTO for host builds as prefer_dynamic and it are mutually
// exclusive.
if unit.target.can_lto() && !unit.target.for_host() {
match *lto {
Lto::Bool(false) => {}
Lto::Bool(true) => {
cmd.args(&["-C", "lto"]);
}
Lto::Named(ref s) => {
cmd.arg("-C").arg(format!("lto={}", s));
}
}
}
if let Some(n) = codegen_units {
// There are some restrictions with LTO and codegen-units, so we
// only add codegen units when LTO is not used.
cmd.arg("-C").arg(&format!("codegen-units={}", n));
}
if let Some(debuginfo) = debuginfo {
cmd.arg("-C").arg(format!("debuginfo={}", debuginfo));
}
if let Some(ref args) = *rustc_args {
cmd.args(args);
}
// -C overflow-checks is implied by the setting of -C debug-assertions,
// so we only need to provide -C overflow-checks if it differs from
// the value of -C debug-assertions we would provide.
if opt_level != "0" {
if debug_assertions {
cmd.args(&["-C", "debug-assertions=on"]);
if !overflow_checks {
cmd.args(&["-C", "overflow-checks=off"]);
}
} else if overflow_checks {
cmd.args(&["-C", "overflow-checks=on"]);
}
} else if !debug_assertions {
cmd.args(&["-C", "debug-assertions=off"]);
if overflow_checks {
cmd.args(&["-C", "overflow-checks=on"]);
}
} else if !overflow_checks {
cmd.args(&["-C", "overflow-checks=off"]);
}
if test && unit.target.harness() {
cmd.arg("--test");
} else if test {
cmd.arg("--cfg").arg("test");
}
// We ideally want deterministic invocations of rustc to ensure that
// rustc-caching strategies like sccache are able to cache more, so sort the
// feature list here.
for feat in cx.resolve.features_sorted(unit.pkg.package_id()) {
cmd.arg("--cfg").arg(&format!("feature=\"{}\"", feat));
}
match cx.target_metadata(unit) {
Some(m) => {
cmd.arg("-C").arg(&format!("metadata={}", m));
cmd.arg("-C").arg(&format!("extra-filename=-{}", m));
}
None => {
cmd.arg("-C")
.arg(&format!("metadata={}", cx.target_short_hash(unit)));
}
}
if rpath {
cmd.arg("-C").arg("rpath");
}
cmd.arg("--out-dir").arg(&cx.out_dir(unit));
fn opt(cmd: &mut ProcessBuilder, key: &str, prefix: &str, val: Option<&OsStr>) {
if let Some(val) = val {
let mut joined = OsString::from(prefix);
joined.push(val);
cmd.arg(key).arg(joined);
}
}
if unit.kind == Kind::Target {
opt(
cmd,
"--target",
"",
cx.requested_target().map(|s| s.as_ref()),
);
}
opt(cmd, "-C", "ar=", cx.ar(unit.kind).map(|s| s.as_ref()));
opt(
cmd,
"-C",
"linker=",
cx.linker(unit.kind).map(|s| s.as_ref()),
);
cmd.args(&cx.incremental_args(unit)?);