-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.rs
1015 lines (926 loc) · 32.7 KB
/
main.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 anyhow::{bail, Context};
//use indicatif::{ProgressBar, ProgressStyle};
use serde::Deserialize;
use std::{
collections::BTreeMap,
fs::File,
io::{BufRead, BufReader, Cursor, Write},
process::{Command, Output, Stdio},
str,
time::SystemTime,
};
use structopt::StructOpt;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use vcpkg::{find_vcpkg_root, Config};
// settings for a specific Rust target
#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct Target {
triplet: Option<String>,
// this dependencies key for a specific target overrides the main entry
// so a the target can opt out of installing packages
#[serde(alias = "install")]
dependencies: Option<Vec<String>>,
dev_dependencies: Option<Vec<String>>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
struct Vcpkg {
// vcpkg_root: Option<String>,
#[serde(default = "BTreeMap::new")]
target: BTreeMap<String, Target>,
branch: Option<String>,
rev: Option<String>,
git: Option<String>,
tag: Option<String>,
#[serde(alias = "install")]
dependencies: Option<Vec<String>>,
dev_dependencies: Option<Vec<String>>,
//
build_type: Option<String>,
}
#[derive(Debug, Deserialize)]
struct Metadata {
vcpkg: Vcpkg,
}
#[derive(Debug, PartialEq, StructOpt)]
/// Install vcpkg and build packages
///
/// This program clones vcpkg from the specified source and
/// compiles it. It then builds packages required by crates
/// that are depended on by the top level crate being built.
#[structopt(rename_all = "kebab-case")]
struct Opt {
/// Display more information while building
///
/// This will display the output of git and
/// vcpkg operations.
#[structopt(short, long)]
verbose: bool,
// #[structopt(long)]
// manifest_path: Option<String>,
#[structopt(subcommand)]
sub: Subcommands,
}
#[derive(Debug, PartialEq, StructOpt)]
enum Subcommands {
/// Build packages, checking out the correct version
///
/// This command will clone or update a vcpkg tree to the version specified
/// in Cargo.toml and build the required packages.
Build {
#[structopt(long)]
/// Path to Cargo.toml
manifest_path: Option<String>,
#[structopt(long)]
/// Build for the target triple
target: Option<String>,
},
// `external_subcommand` tells structopt to put
// all the extra arguments into this Vec
// #[structopt(external_subcommand)]
// Cargo(Vec<String>),
}
enum RevSelector {
Rev(String),
Tag(String),
Branch(String),
}
fn main() {
// cargo passes us the "vcpkg" arg when it calls us. Drop it before
// parsing the arg list so it doesn't end up the usage description
let mut args = std::env::args().collect::<Vec<_>>();
if args.len() >= 2 && args[1] == "vcpkg" {
args.remove(1);
}
let args = Opt::from_iter(args);
match args.sub {
Subcommands::Build { .. } => {
build(args).unwrap_or_else(|e| {
eprintln!("cargo-vcpkg: {}", e);
std::process::exit(1);
});
}
}
}
fn build(opt: Opt) -> Result<(), anyhow::Error> {
let start_time = SystemTime::now();
let target_triple = target_triple();
let verbose = opt.verbose;
let mut args = std::env::args().skip_while(|val| !val.starts_with("--manifest-path"));
let mut cmd = cargo_metadata::MetadataCommand::new();
match args.next() {
Some(p) if p == "--manifest-path" => {
cmd.manifest_path(args.next().unwrap());
}
Some(p) => {
cmd.manifest_path(p.trim_start_matches("--manifest-path="));
}
None => {}
}
let metadata = cmd.exec()?;
let (git_url, vcpkg_ports, rev_tag_branch, vcpkg_triplet, root_crate) =
process_metadata(&metadata, &target_triple)?;
// should we modify the existing?
// let mut allow_updates = true;
// find the vcpkg root
let vcpkg_root = find_vcpkg_root(&Config::default()).unwrap_or_else(|_| {
let target_directory = metadata.target_directory.clone();
let mut vcpkg_root = target_directory;
vcpkg_root.push("vcpkg");
vcpkg_root.into()
});
if verbose {
println!("vcpkg root is {}", vcpkg_root.display());
}
// if it does not exist, clone vcpkg from git
let mut vcpkg_root_file = vcpkg_root.clone();
vcpkg_root_file.push(".vcpkg-root");
if !vcpkg_root_file.exists() {
let git_url = git_url.context(format!(
"could not find a vcpkg installation and crate \n\
{} does not specify a git repository to clone from. \n\n\
Add a [package.metadata.vcpkg] section to the root crate's\n\
Cargo.toml, and add a 'git' key and one of the 'branch',\n\
'tag' or 'rev' keys to tell this program where to get\n\
the correct version of vcpkg from. For example:\n\n\
[package.metadata.vcpkg]\n\
git = \"https://github.com/microsoft/vcpkg\"\n\
branch = \"master\" ",
root_crate
))?;
print_tag("Cloning", &git_url);
let mut cmd = Command::new("git");
cmd.arg("clone");
cmd.arg(git_url);
cmd.arg(&vcpkg_root);
let _output = run_command(cmd, verbose).context("failed to run git clone")?;
//eprintln!("git clone done = {:?}", output.status);
} else {
print_tag("Fetching", "vcpkg");
let mut cmd = Command::new("git");
cmd.arg("fetch");
cmd.arg("--verbose");
cmd.arg("--all");
cmd.current_dir(&vcpkg_root);
let output = run_command(cmd, verbose).context("failed to run git fetch")?;
if !output.status.success() {
bail!("fetch failed");
}
}
// create a cargo-vcpkg.toml in the vcpkg tree
let mut cargo_vcpkg_config_file = vcpkg_root.clone();
cargo_vcpkg_config_file.push("downloads");
std::fs::create_dir_all(&cargo_vcpkg_config_file)
.context("could not create downloads directory in vcpkg tree")?;
cargo_vcpkg_config_file.push("cargo-vcpkg.toml");
if !cargo_vcpkg_config_file.exists() {
let mut file =
File::create(cargo_vcpkg_config_file).context("could not create cargo-vcpkg.toml")?;
file.write_all(b"# This file was created automatically by cargo-vcpkg\n")?;
}
// check out the required rev
let rev_tag_branch = rev_tag_branch.unwrap();
let (desc, rev_tag_branch, do_pull) = match rev_tag_branch {
RevSelector::Rev(r) => ("rev", r, false),
RevSelector::Tag(t) => ("tag", t, false), //?
RevSelector::Branch(b) => ("branch", b, true),
};
print_tag("Checkout", &format!("{} {}", desc, rev_tag_branch));
let mut cmd = Command::new("git");
cmd.arg("checkout");
cmd.arg(&rev_tag_branch);
cmd.current_dir(&vcpkg_root);
run_command(cmd, verbose).context("failed to execute process")?;
// if it is a branch, run a git pull to move to the correct commit
if do_pull {
print_tag("Pulling", &format!("{} {}", desc, rev_tag_branch));
let mut cmd = Command::new("git");
cmd.arg("pull");
//cmd.arg(rev_tag_branch);
cmd.current_dir(&vcpkg_root);
run_command(cmd, verbose).context("failed to execute process")?;
}
// try and run 'vcpkg update' and if it fails or gives the version warning, rebuild it
let require_bootstrap = match vcpkg_command(&vcpkg_root, &vcpkg_triplet)
.arg("update")
.output()
{
Ok(output) => {
if verbose {
println!("-- stdout --\n{}", String::from_utf8_lossy(&output.stdout));
println!("-- stderr --\n{}", String::from_utf8_lossy(&output.stderr));
println!("{:?}", output.status);
}
!output.status.success()
|| str::from_utf8(&output.stdout)
.unwrap()
.contains("Warning: Different source is available for vcpkg")
}
Err(_) => true,
};
if require_bootstrap {
run_bootstrap(&vcpkg_root, verbose)?;
}
// TODO: upgrade anything that is installed
print_tag("Installing", &vcpkg_ports.join(" "));
let mut v = vcpkg_command(&vcpkg_root, &vcpkg_triplet);
v.arg("install");
v.arg("--recurse");
v.args(vcpkg_ports.as_slice());
v.stdout(Stdio::piped());
let mut output = v.spawn()?;
let reader = BufReader::new(output.stdout.take().context("could not get stdout")?);
// let style = ProgressStyle::default_bar()
// .progress_chars("=> ")
// .template(" Building [{bar:40.cyan/blue}] {pos:>7}/{len:7} {msg}");
// let bar = ProgressBar::new(10).with_style(style);
for line in reader.lines().flat_map(Result::ok) {
if let Some((pkg, triplet, _num, _tot)) = parse_build_line(&line) {
print_tag("Compiling", &format!("{} (triplet {})", pkg, triplet))
}
if verbose {
println!("{}", line);
}
// bar.set_length(tot);
// bar.set_position(num);
// Building [==============================================> ] 58/59
}
// grab anything that is left
let output = output.wait_with_output()?;
if !output.status.success() {
if !verbose {
println!("-- stdout --\n{}", String::from_utf8_lossy(&output.stdout));
println!("-- stderr --\n{}", String::from_utf8_lossy(&output.stderr));
}
bail!("failed");
}
let duration = SystemTime::now().duration_since(start_time).unwrap();
print_tag("Finished", &format!("in {:0.2}s", duration.as_secs_f32()));
Ok(())
}
fn process_metadata(
metadata: &cargo_metadata::Metadata,
target_triple: &str,
) -> Result<
(
Option<String>,
Vec<String>,
Option<RevSelector>,
Option<String>,
cargo_metadata::PackageId,
),
anyhow::Error,
> {
let dep_graph = metadata.resolve.as_ref().unwrap();
let root_crate = dep_graph
.root
.as_ref()
.context("cannot run on a virtual manifest, this command requires running against an actual package in this workspace.")?;
let mut git_url = None;
let mut vcpkg_ports = Vec::<String>::new();
let mut rev_tag_branch: Option<RevSelector> = None;
let mut vcpkg_triplet = None;
// dbg!(&metadata.workspace_root);
// dbg!(&metadata.workspace_metadata);
for p in &metadata.packages {
// println!("-------------");
// dbg!(&p);
if let Ok(v) = serde_json::from_value::<Metadata>(p.metadata.clone()) {
// dbg!(&v);
let v = v.vcpkg;
let is_root_crate = p.id == *root_crate;
// only use git url and rev from the root crate
if v.git.is_some() && is_root_crate {
git_url = v.git;
// TODO: check the target and use it's package set if required
// TODO: get the correct target
// TODO: make sure to pull if it's a branch
rev_tag_branch = match (&v.branch, &v.tag, &v.rev) {
(Some(b), None, None) => Some(RevSelector::Branch(b.into())),
(None, Some(t), None) => Some(RevSelector::Tag(t.into())),
(None, None, Some(r)) => Some(RevSelector::Rev(r.into())),
_ => {
bail!("must specify one of branch,rev,tag for git source");
}
};
}
// if there is specific configuration for the target and it has
// a dependencies key, use that rather than the general dependencies key
match v.target.get(target_triple) {
Some(target) => {
let deps = target.dependencies.as_ref().or(v.dependencies.as_ref());
if deps.is_some() {
vcpkg_ports.extend_from_slice(deps.unwrap().as_slice());
}
if is_root_crate && target.triplet.is_some() {
vcpkg_triplet = target.triplet.clone();
}
if is_root_crate {
let dev_deps = target
.dev_dependencies
.as_ref()
.or(v.dev_dependencies.as_ref());
if dev_deps.is_some() {
vcpkg_ports.extend_from_slice(dev_deps.unwrap().as_slice());
}
}
}
_ => {
// not found or dependencies is empty
if v.dependencies.is_some() {
vcpkg_ports.extend_from_slice(&v.dependencies.as_ref().unwrap().as_slice());
}
if is_root_crate && v.dev_dependencies.is_some() {
vcpkg_ports
.extend_from_slice(&v.dev_dependencies.as_ref().unwrap().as_slice());
}
}
}
}
}
Ok((
git_url,
vcpkg_ports,
rev_tag_branch,
vcpkg_triplet,
root_crate.clone(),
))
}
fn target_triple() -> String {
let mut args = std::env::args().skip_while(|val| !val.starts_with("--target"));
match args.next() {
Some(p) if p == "--target" => args.next().unwrap(),
Some(p) => p.trim_start_matches("--target=").into(),
// if target is unspecified, default to the architecture that this
// cargo-vcpkg binary was built for
None => std::env!("TARGET").into(),
}
}
fn vcpkg_command(vcpkg_root: &std::path::Path, vcpkg_triplet: &Option<String>) -> Command {
let mut x = vcpkg_root.to_path_buf();
if cfg!(windows) {
x.push("vcpkg.exe");
} else {
x.push("vcpkg")
}
let mut command = Command::new(x);
command.current_dir(&vcpkg_root);
if let Some(triplet) = &vcpkg_triplet {
command.arg("--triplet");
command.arg(triplet);
}
command
}
fn run_command(mut cmd: Command, verbose: bool) -> Result<Output, anyhow::Error> {
if verbose {
cmd.stdout(Stdio::inherit());
cmd.stderr(Stdio::inherit());
}
let output = cmd.output()?;
if !output.status.success() {
if !verbose {
println!("-- stdout --\n{}", String::from_utf8_lossy(&output.stdout));
println!("-- stderr --\n{}", String::from_utf8_lossy(&output.stderr));
}
bail!("failed");
}
Ok(output)
}
fn print_tag(tag: &str, detail: &str) {
let mut stdout = StandardStream::stdout(ColorChoice::Auto);
stdout
.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true))
.unwrap();
print!("{:>12} ", tag);
stdout.reset().unwrap();
println!("{}", detail);
}
fn parse_build_line(line: &str) -> Option<(String, String, u64, u64)> {
let line = Some(line)
.filter(|line| line.starts_with("Starting package "))
.map(|line| line.trim_start_matches("Starting package ").to_string())?;
let progress_and_pkg_trp = line.splitn(2, ':').collect::<Vec<_>>();
if progress_and_pkg_trp.len() != 2 {
return None;
}
let pkg_with_triplet = progress_and_pkg_trp[1].trim();
let (pkg, triplet) = match pkg_with_triplet
.rsplitn(2, ':')
.collect::<Vec<_>>()
.as_slice()
{
[t, p] => (p.to_string(), t.to_string()),
_ => return None,
};
let (cnt, tot) = match &progress_and_pkg_trp[0]
.splitn(2, '/')
.filter_map(|s| s.parse::<u64>().ok())
.collect::<Vec<_>>()
.as_slice()
{
[cnt, tot] => (*cnt, *tot),
_ => (0, 0),
};
Some((pkg, triplet, cnt, tot))
}
fn run_bootstrap(vcpkg_root: &std::path::Path, verbose: bool) -> Result<(), anyhow::Error> {
print_tag("Compiling", "vcpkg");
if cfg!(windows) {
let mut cmd = Command::new("cmd");
cmd.arg("/C");
cmd.arg("bootstrap-vcpkg.bat");
cmd.arg("-disableMetrics");
cmd.current_dir(&vcpkg_root);
run_command(cmd, verbose).context("failed to run vcpkg bootstrap")?;
} else {
// if we are on a mac with clang 11, try it first. Fall back to the
// installation that requires gcc if this build fails
if cfg!(target_os = "macos") {
if let Some(version) = apple_clang_version() {
if version >= 11 {
let mut cmd = Command::new("sh");
cmd.arg("-c");
cmd.arg("./bootstrap-vcpkg.sh -disableMetrics -allowAppleClang");
cmd.current_dir(&vcpkg_root);
if run_command(cmd, verbose).is_ok() {
return Ok(());
}
println!(
"note: building vcpkg with apple clang failed, falling \
back to using another compiler."
);
}
}
}
let mut cmd = Command::new("sh");
cmd.arg("-c");
cmd.arg("./bootstrap-vcpkg.sh -disableMetrics");
cmd.current_dir(&vcpkg_root);
run_command(cmd, verbose).context("failed to run vcpkg bootstrap")?;
};
Ok(())
}
fn apple_clang_version() -> Option<u64> {
let output = Command::new("clang").arg("--version").output().ok()?;
parse_apple_clang_version(&output.stdout)
}
fn parse_apple_clang_version(bytes: &[u8]) -> Option<u64> {
Cursor::new(bytes)
.lines()
.filter_map(Result::ok)
.filter(|line| line.starts_with("Apple clang version "))
.map(|line| line.trim_start_matches("Apple clang version ").to_string())
.next()?
.splitn(2, '.')
.next()
.and_then(|x| x.parse::<u64>().ok())
}
#[cfg(test)]
mod test {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{
env, fs,
path::{Path, PathBuf},
};
// Cadged from https://github.com/rust-lang/cargo/tree/master/crates/cargo-test-support
#[derive(Default)]
pub(super) struct ProjectBuilder {
root: PathBuf,
// files:
}
pub(super) fn project() -> ProjectBuilder {
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
println!("new id {}", id);
let mut root = {
let mut path = env::current_exe().unwrap();
// dbg!(&path);
path.pop(); // chop off exe name
path.pop(); // chop off deps
path.pop(); // chop off 'debug'
// if path.file_name().and_then(|s| s.to_str()) != Some("target") {
// path.pop();
// }
path.push("cv_int");
path.mkdir_p();
path
};
root.push(&format!("test_{}", id));
root.rm_rf();
root.mkdir_p();
ProjectBuilder { root }
}
impl ProjectBuilder {
// pub(super) fn file(&mut self, name: &str, contents: &str) -> &mut Self {
// self
// }
pub(super) fn metadata(
&mut self,
manifest_path: &str,
) -> Result<cargo_metadata::Metadata, anyhow::Error> {
let mut cmd = cargo_metadata::MetadataCommand::new();
let mut path = self.root.clone();
path.push(manifest_path);
cmd.manifest_path(path);
Ok(cmd.exec()?)
}
/// Adds a file to the project.
pub(super) fn file<B: AsRef<Path>>(self, path: B, body: &str) -> Self {
let path = self.root.clone().join(path);
let dirname = path.parent().unwrap();
dirname.mkdir_p();
fs::write(&path, &body)
.unwrap_or_else(|e| panic!("could not create file {}: {}", path.display(), e));
self
}
}
pub trait TestPathExt {
fn rm_rf(&self);
fn mkdir_p(&self);
}
impl TestPathExt for Path {
fn rm_rf(&self) {
if self.exists() {
if let Err(e) = remove_dir_all::remove_dir_all(self) {
panic!("failed to remove {:?}: {:?}", self, e)
}
}
}
fn mkdir_p(&self) {
fs::create_dir_all(self)
.unwrap_or_else(|e| panic!("failed to mkdir_p {}: {}", self.display(), e))
}
}
pub fn basic_manifest(name: &str, version: &str) -> String {
format!(
r#"
[package]
name = "{}"
version = "{}"
authors = []
"#,
name, version
)
}
pub fn extended_manifest(name: &str, version: &str, tail: &str) -> String {
format!("{}\n\n{}", &basic_manifest(name, version), tail)
}
#[test]
fn test_parse_apple_clang_version() {
assert_eq!(
parse_apple_clang_version(b"la la la\nApple clang version 9.0.1"),
Some(9)
);
assert_eq!(
parse_apple_clang_version(b"ho ho ho\nhe he he\nApple clang version 10.0.1"),
Some(10)
);
assert_eq!(
parse_apple_clang_version(b"Apple clang version 11.0.1"),
Some(11)
);
assert_eq!(
parse_apple_clang_version(b"Apple clang version 12.0.1"),
Some(12)
);
assert_eq!(
parse_apple_clang_version(b"Opple clong version 12.0.1"),
None
);
}
#[test]
fn run_on_workspace_fails() {
let metadata = test::project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["top", "dep"]
"#,
)
.file(
"top/Cargo.toml",
&extended_manifest(
"top",
"0.1.0",
r#"
[dependencies]
dep = { path = "../dep" }
[package.manifest.vcpkg]
dependencies = ["z85"]
"#,
),
)
.file("top/src/main.rs", "")
.file(
"dep/Cargo.toml",
&extended_manifest(
"dep",
"0.1.0",
r#"
[lib]
[package.manifest.vcpkg]
"#,
),
)
.file("dep/src/lib.rs", "")
.metadata("Cargo.toml")
.unwrap();
let err = process_metadata(&metadata, "").err().unwrap();
assert!(err.to_string().contains("cannot run on a virtual manifest"));
}
#[test]
fn install_in_root_crate() {
let metadata = test::project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["top", "dep"]
"#,
)
.file(
"top/Cargo.toml",
&extended_manifest(
"top",
"0.1.0",
r#"
[dependencies]
dep = { path = "../dep" }
[package.metadata.vcpkg]
install = ["z85"]
"#,
),
)
.file("top/src/main.rs", "")
.file(
"dep/Cargo.toml",
&extended_manifest(
"dep",
"0.1.0",
r#"
[lib]
[package.metadata.vcpkg]
"#,
),
)
.file("dep/src/lib.rs", "")
.metadata("top/Cargo.toml")
.unwrap();
let (_, vcpkg_ports, _, _, _) = process_metadata(&metadata, "").unwrap();
assert_eq!(vcpkg_ports, vec!["z85"]);
}
#[test]
fn same_dependencies_but_specified_triplet() {
let metadata = test::project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["top", "dep"]
"#,
)
.file(
"top/Cargo.toml",
&extended_manifest(
"top",
"0.1.0",
r#"
[dependencies]
dep = { path = "../dep" }
[package.metadata.vcpkg]
install = ["z85"]
[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md" }
"#,
),
)
.file("top/src/main.rs", "")
.file(
"dep/Cargo.toml",
&extended_manifest(
"dep",
"0.1.0",
r#"
[lib]
[package.metadata.vcpkg]
"#,
),
)
.file("dep/src/lib.rs", "")
.metadata("top/Cargo.toml")
.unwrap();
let (_, vcpkg_ports, _, vcpkg_triplet, _) =
process_metadata(&metadata, "x86_64-pc-windows-msvc").unwrap();
assert_eq!(vcpkg_ports, vec!["z85"]);
assert_eq!(vcpkg_triplet, Some("x64-windows-static-md".to_owned()));
}
#[test]
fn same_dev_dependencies_but_specified_triplet() {
let metadata = test::project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["top", "dep"]
"#,
)
.file(
"top/Cargo.toml",
&extended_manifest(
"top",
"0.1.0",
r#"
[dependencies]
dep = { path = "../dep" }
[package.metadata.vcpkg]
dependencies = ["a"]
dev-dependencies = ["b"]
[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md" }
"#,
),
)
.file("top/src/main.rs", "")
.file(
"dep/Cargo.toml",
&extended_manifest(
"dep",
"0.1.0",
r#"
[lib]
[package.metadata.vcpkg]
"#,
),
)
.file("dep/src/lib.rs", "")
.metadata("top/Cargo.toml")
.unwrap();
let (_, mut vcpkg_ports, _, vcpkg_triplet, _) =
process_metadata(&metadata, "x86_64-pc-windows-msvc").unwrap();
vcpkg_ports.sort();
assert_eq!(vcpkg_ports, vec!["a", "b"]);
assert_eq!(vcpkg_triplet, Some("x64-windows-static-md".to_owned()));
}
#[test]
fn specified_triplet_requires_no_dependencies() {
let metadata = test::project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["top", "dep"]
"#,
)
.file(
"top/Cargo.toml",
&extended_manifest(
"top",
"0.1.0",
r#"
[dependencies]
dep = { path = "../dep" }
[package.metadata.vcpkg]
install = ["z85"]
[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md", dependencies = [] }
"#,
),
)
.file("top/src/main.rs", "")
.file(
"dep/Cargo.toml",
&extended_manifest(
"dep",
"0.1.0",
r#"
[lib]
[package.metadata.vcpkg]
"#,
),
)
.file("dep/src/lib.rs", "")
.metadata("top/Cargo.toml")
.unwrap();
let (_, vcpkg_ports, _, vcpkg_triplet, _) =
process_metadata(&metadata, "x86_64-pc-windows-msvc").unwrap();
assert_eq!(vcpkg_ports, Vec::<String>::new());
assert_eq!(vcpkg_triplet, Some("x64-windows-static-md".to_owned()));
}
#[test]
fn combine_deps_from_all_crates() {
let metadata = test::project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["top", "dep"]
"#,
)
.file(
"top/Cargo.toml",
&extended_manifest(
"top",
"0.1.0",
r#"
[dependencies]
dep = { path = "../dep" }
[package.metadata.vcpkg]
dependencies = ["a"]
dev-dependencies = ["d"]
[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md", dev-dependencies = ["b", "c"] }
"#,
),
)
.file("top/src/main.rs", "")
.file(
"dep/Cargo.toml",
&extended_manifest(
"dep",
"0.1.0",
r#"
[lib]
[package.metadata.vcpkg]
dependencies = ["m"]
dev-dependencies = ["n"]
[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md", dependencies = ["o"], dev-dependencies = ["p"] }
"#,
),
)
.file("dep/src/lib.rs", "")
.metadata("top/Cargo.toml")
.unwrap();
let (_, mut vcpkg_ports, _, vcpkg_triplet, _) =
process_metadata(&metadata, "x86_64-pc-windows-msvc").unwrap();
vcpkg_ports.sort();
assert_eq!(vcpkg_ports, vec!["a", "b", "c", "o"]);
assert_eq!(vcpkg_triplet, Some("x64-windows-static-md".to_owned()));
let (_, mut vcpkg_ports, _, _, _) = process_metadata(&metadata, "").unwrap();
vcpkg_ports.sort();
assert_eq!(vcpkg_ports, vec!["a", "d", "m"]);
}
// This test is currently disabled as it isn't clear what the desired behavior is for when
// specifying vcpkg dependencies at the workspace level. This is in part due to
// cargo-metadata not telling us which is the desired root crate (resolve.root == null) and
// ambiguity in this case as to which git rev to use as this is usually determined by the root
// crate.
#[test]
#[ignore]
fn combine_deps_from_all_crates_and_workspace() {
let metadata = test::project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["top", "dep"]
[workspace.metadata.vcpkg]
dependencies = ["a"]
dev-dependencies = ["d"]
[workspace.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md", dev-dependencies = ["b", "c"] }
"#,
)
.file(
"top/Cargo.toml",
&extended_manifest(
"top",
"0.1.0",
r#"
[dependencies]
dep = { path = "../dep" }
"#,
),
)
.file("top/src/main.rs", "")
.file(
"dep/Cargo.toml",
&extended_manifest(
"dep",
"0.1.0",
r#"
[lib]
[package.metadata.vcpkg]
dependencies = ["m"]
dev-dependencies = ["n"]
[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md", dependencies = ["o"], dev-dependencies = ["p"] }
"#,
),
)