-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfixups.rs
1215 lines (1089 loc) · 47.4 KB
/
fixups.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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
//! Per-package configuration information
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use crate::buck;
use crate::buck::Alias;
use crate::buck::BuckPath;
use crate::buck::BuildscriptGenrule;
use crate::buck::Common;
use crate::buck::Name;
use crate::buck::Rule;
use crate::buck::RuleRef;
use crate::buck::RustBinary;
use crate::buck::StringOrPath;
use crate::buck::Subtarget;
use crate::buck::SubtargetOrPath;
use crate::buck::Visibility;
use crate::buckify::normalize_path;
use crate::buckify::relative_path;
use crate::buckify::short_name_for_git_repo;
use crate::cargo::Manifest;
use crate::cargo::ManifestTarget;
use crate::cargo::NodeDepKind;
use crate::cargo::Source;
use crate::collection::SetOrMap;
use crate::config::Config;
use crate::config::VendorConfig;
use crate::glob::Globs;
use crate::glob::SerializableGlobSet as GlobSet;
use crate::glob::NO_EXCLUDE;
use crate::index::Index;
use crate::index::ResolvedDep;
use crate::platform::platform_names_for_expr;
use crate::platform::PlatformExpr;
use crate::platform::PlatformPredicate;
use crate::Paths;
mod buildscript;
mod config;
use buildscript::BuildscriptFixup;
use buildscript::CxxLibraryFixup;
use buildscript::GenSrcs;
use buildscript::PrebuiltCxxLibraryFixup;
use buildscript::RustcFlags;
use config::CargoEnv;
pub use config::ExportSources;
use config::FixupConfigFile;
/// Fixups for a specific package & target
pub struct Fixups<'meta> {
config: &'meta Config,
third_party_dir: PathBuf,
index: &'meta Index<'meta>,
package: &'meta Manifest,
target: &'meta ManifestTarget,
fixup_dir: PathBuf,
fixup_config: FixupConfigFile,
manifest_dir: &'meta Path,
}
impl<'meta> fmt::Debug for Fixups<'meta> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Fixups")
.field("package", &self.package.to_string())
.field("target", &self.target)
.field("third_party_dir", &self.third_party_dir)
.field("fixup_dir", &self.fixup_dir)
.field("manifest_dir", &self.manifest_dir)
.field("fixup_config", &self.fixup_config)
.finish()
}
}
impl<'meta> Fixups<'meta> {
/// Get any fixups needed for a specific package target
pub fn new(
config: &'meta Config,
paths: &Paths,
index: &'meta Index,
package: &'meta Manifest,
target: &'meta ManifestTarget,
) -> anyhow::Result<Self> {
let fixup_dir = paths.third_party_dir.join("fixups").join(&package.name);
let fixup_path = fixup_dir.join("fixups.toml");
let fixup_config: FixupConfigFile = if let Ok(file) = fs::read_to_string(&fixup_path) {
log::debug!("read fixups from {}", fixup_path.display());
toml::from_str(&file).context(format!("Failed to parse {}", fixup_path.display()))?
} else {
log::debug!("no fixups at {}", fixup_path.display());
let fixup = FixupConfigFile::template(&paths.third_party_dir, target);
if config.fixup_templates && target.kind_custom_build() {
log::debug!(
"Writing template for {} to {}",
package,
relative_path(&paths.third_party_dir, &fixup_path).display()
);
log::debug!(
"fixup template: {:#?}, path {}",
fixup,
fixup_path.display()
);
let file = toml::to_string_pretty(&fixup)?;
fs::create_dir_all(fixup_path.parent().unwrap())?;
fs::write(&fixup_path, file)?;
}
fixup
};
if fixup_config.custom_visibility.is_some() && !index.is_public_package_name(&package.name)
{
return Err(anyhow!(
"only public packages can have a fixup `visibility`."
))
.with_context(|| format!("package {package} is private."));
}
Ok(Fixups {
third_party_dir: paths.third_party_dir.to_path_buf(),
manifest_dir: package.manifest_dir(),
index,
package,
target,
fixup_dir,
fixup_config,
config,
})
}
// Return true if the script applies to this target.
// There's a few cases:
// - if the script doesn't specify a target, then it applies to the main "lib" target of the package
// - otherwise it applies to the matching kind and (optionally) name (all names if not specified)
fn target_match(&self, script: &BuildscriptFixup) -> bool {
let default = self.package.dependency_target();
match (script.targets(), default) {
(Some([]), Some(default)) | (None, Some(default)) => self.target == default,
(Some([]), None) | (None, None) => false,
(Some(tgts), _) => tgts.iter().any(|(kind, name)| {
self.target.kind.contains(kind)
&& name.as_ref().map_or(true, |name| &self.target.name == name)
}),
}
}
fn subtarget_or_path(
&self,
relative_to_manifest_dir: &Path,
) -> anyhow::Result<SubtargetOrPath> {
if matches!(self.config.vendor, VendorConfig::Source(_))
|| matches!(self.package.source, Source::Local)
{
// Path to vendored file looks like "vendor/foo-1.0.0/src/lib.rs"
let manifest_dir = relative_path(&self.third_party_dir, self.manifest_dir);
let path = manifest_dir.join(relative_to_manifest_dir);
Ok(SubtargetOrPath::Path(BuckPath(path)))
} else if let Source::Git { .. } = &self.package.source {
// This is unsupported because git_fetch doesn't support subtargets.
Err(anyhow!(
"git dependencies in non-vendor mode cannot have subtargets"
))
.with_context(|| format!("package {} is a git depedency", self.package))
} else {
// Subtarget inside an http_archive: ":foo-1.0.0.crate[src/lib.rs]"
Ok(SubtargetOrPath::Subtarget(Subtarget {
target: Name(format!(
"{}-{}.crate",
self.package.name, self.package.version
)),
relative: BuckPath(relative_to_manifest_dir.to_owned()),
}))
}
}
pub fn public_visibility(&self) -> Visibility {
match self.fixup_config.custom_visibility.as_deref() {
Some(visibility) => Visibility::Custom(visibility.to_vec()),
None => Visibility::Public,
}
}
pub fn python_ext(&self) -> Option<&str> {
self.fixup_config.python_ext.as_deref()
}
pub fn omit_target(&self) -> bool {
self.fixup_config.omit_targets.contains(&self.target.name)
}
pub fn export_sources(&self) -> Option<&ExportSources> {
self.fixup_config.export_sources.as_ref()
}
pub fn precise_srcs(&self) -> bool {
self.fixup_config
.precise_srcs
.unwrap_or(self.config.precise_srcs)
}
fn buildscript_target(&self) -> Option<&ManifestTarget> {
self.package
.targets
.iter()
.find(|tgt| tgt.kind_custom_build())
}
pub fn buildscript_genrule_name(&self) -> Name {
let mut name = self.buildscript_rule_name().expect("no buildscript");
// Cargo names the build script target after the filename of the build
// script's crate root. In the overwhelmingly common case of a build
// script located in build.rs, this is build-script-build, and we name
// the genrule build-script-run. Some crates such as typenum have a
// build/main.rs such that the build script target ends up being named
// build-script-main, and in this case we use build-script-main-run for
// the genrule.
if name.0.ends_with("-build-script-build") {
name.0.truncate(name.0.len() - 6);
}
name.0.push_str("-run");
name
}
fn buildscript_rule_name(&self) -> Option<Name> {
self.buildscript_target()
.map(|tgt| Name(format!("{}-{}", self.package, tgt.name)))
}
/// Return buildscript-related rules
/// The rules may be platform specific, but they're emitted unconditionally. (The
/// dependencies referencing them are conditional).
pub fn emit_buildscript_rules(
&self,
buildscript: RustBinary,
config: &'meta Config,
) -> anyhow::Result<Vec<Rule>> {
let mut res = Vec::new();
let rel_fixup = relative_path(&self.third_party_dir, &self.fixup_dir);
let buildscript_rule_name = match self.buildscript_rule_name() {
None => {
log::warn!(
"Package {} doesn't have a build script to fix up",
self.package
);
return Ok(res);
}
Some(name) => name,
};
// Generate features extracting them from the buildscript RustBinary. The assumption
// that fixups are already per-platform if necessary so there's no need for platform-specific
// rule attributes.
let mut features = BTreeSet::new();
for (plat, _fixup) in self.fixup_config.configs(&self.package.version) {
match plat {
None => features.extend(
buildscript
.common
.base
.features
.unwrap_ref()
.iter()
.cloned(),
),
Some(expr) => {
let platnames = platform_names_for_expr(self.config, expr)?;
for platname in platnames {
if let Some(platattr) = buildscript.common.platform.get(platname) {
features.extend(platattr.features.unwrap_ref().iter().cloned())
}
}
}
}
}
// Flat list of buildscript fixups.
let fixes = self
.fixup_config
.configs(&self.package.version)
.flat_map(|(_platform, fixup)| fixup.buildscript.iter());
let mut buildscript_run = None;
let default_genrule = || BuildscriptGenrule {
name: self.buildscript_genrule_name(),
buildscript_rule: buildscript_rule_name.clone(),
package_name: self.package.name.clone(),
version: self.package.version.clone(),
features: buck::Selectable::Value(features.clone()),
env: BTreeMap::new(),
};
for fix in fixes {
if !matches!(self.config.vendor, VendorConfig::Source(_)) {
if let Source::Git { repo, .. } = &self.package.source {
// Cxx_library fixups only work if the sources are vendored
// or from an http_archive. They do not work with sources
// from git_fetch, because we do not currently have a way to
// generate subtargets referring to the git repo contents:
// e.g. "briansmith/ring[crypto/crypto.c]"
if let Some(unsupported_fixup_kind) = match fix {
BuildscriptFixup::CxxLibrary(_) => Some("buildscript.cxx_library"),
BuildscriptFixup::PrebuiltCxxLibrary(_) => {
Some("buildscript.prebuilt_cxx_library")
}
_ => None,
} {
bail!(
"{} fixup is not supported in vendor=false mode for crates that come from a git repo: {}",
unsupported_fixup_kind,
repo,
);
}
}
}
match fix {
// Build and run it, and filter the output for --cfg options
// for the main target's rustc command line
BuildscriptFixup::RustcFlags(RustcFlags { env, .. }) => {
// Emit the build script itself
res.push(Rule::BuildscriptBinary(buildscript.clone()));
// Emit rule to get its stdout and filter it into args
let buildscript_run = buildscript_run.get_or_insert_with(default_genrule);
buildscript_run.env.extend(env.clone());
}
// Generated source files - given a list, set up rules to extract them from
// the buildscript.
BuildscriptFixup::GenSrcs(GenSrcs { env, .. }) => {
// Emit the build script itself
res.push(Rule::BuildscriptBinary(buildscript.clone()));
// Emit rules to extract generated sources
let buildscript_run = buildscript_run.get_or_insert_with(default_genrule);
buildscript_run.env.extend(env.clone());
}
// Emit a C++ library build rule (elsewhere - add a dependency to it)
BuildscriptFixup::CxxLibrary(CxxLibraryFixup {
name,
srcs,
headers,
exported_headers,
public,
include_paths,
fixup_include_paths,
exclude,
compiler_flags,
preprocessor_flags,
header_namespace,
deps,
compatible_with,
preferred_linkage,
undefined_symbols,
..
}) => {
let actual = Name(format!(
"{}-{}",
self.index.private_rule_name(self.package),
name,
));
if *public {
let rule = Rule::Alias(Alias {
name: Name(format!(
"{}-{}",
self.index.public_rule_name(self.package),
name,
)),
actual: actual.clone(),
visibility: self.public_visibility(),
});
res.push(rule);
}
let rule = buck::CxxLibrary {
common: Common {
name: actual,
visibility: Visibility::Private,
licenses: Default::default(),
compatible_with: compatible_with
.iter()
.cloned()
.map(RuleRef::new)
.collect(),
},
// Just collect the sources, excluding things in the exclude list
srcs: {
let mut globs = Globs::new(srcs, exclude).context("C++ sources")?;
let srcs = globs
.walk(self.manifest_dir)
.map(|path| self.subtarget_or_path(&path))
.collect::<anyhow::Result<_>>()?;
if self.config.strict_globs {
globs.check_all_globs_used()?;
}
srcs
},
// Collect the nominated headers, plus everything in the fixup include
// path(s).
headers: {
let mut globs = Globs::new(headers, exclude)?;
let mut headers = BTreeSet::new();
for path in globs.walk(self.manifest_dir) {
headers.insert(self.subtarget_or_path(&path)?);
}
let mut globs = Globs::new(["**/*.asm", "**/*.h"], NO_EXCLUDE)?;
for fixup_include_path in fixup_include_paths {
for path in globs.walk(self.fixup_dir.join(fixup_include_path)) {
headers.insert(SubtargetOrPath::Path(BuckPath(
rel_fixup.join(fixup_include_path).join(path),
)));
}
}
headers
},
exported_headers: match exported_headers {
SetOrMap::Set(exported_headers) => {
let mut exported_header_globs =
Globs::new(exported_headers, exclude)
.context("C++ exported headers")?;
let exported_headers = exported_header_globs
.walk(self.manifest_dir)
.map(|path| self.subtarget_or_path(&path))
.collect::<anyhow::Result<_>>()?;
if self.config.strict_globs {
exported_header_globs.check_all_globs_used()?;
}
SetOrMap::Set(exported_headers)
}
SetOrMap::Map(exported_headers) => SetOrMap::Map(
exported_headers
.iter()
.map(|(name, path)| {
Ok((name.clone(), self.subtarget_or_path(Path::new(path))?))
})
.collect::<anyhow::Result<_>>()?,
),
},
include_directories: fixup_include_paths
.iter()
.map(|path| Ok(SubtargetOrPath::Path(BuckPath(rel_fixup.join(path)))))
.chain(
include_paths
.iter()
.map(|path| self.subtarget_or_path(path)),
)
.collect::<anyhow::Result<_>>()?,
compiler_flags: compiler_flags.clone(),
preprocessor_flags: preprocessor_flags.clone(),
header_namespace: header_namespace.clone(),
deps: deps.iter().cloned().map(RuleRef::new).collect(),
preferred_linkage: preferred_linkage.clone(),
undefined_symbols: undefined_symbols.clone(),
};
res.push(Rule::CxxLibrary(rule));
}
// Emit a prebuilt C++ library rule for each static library (elsewhere - add dependencies to them)
BuildscriptFixup::PrebuiltCxxLibrary(PrebuiltCxxLibraryFixup {
name,
static_libs,
public,
compatible_with,
..
}) => {
let mut static_lib_globs =
Globs::new(static_libs, NO_EXCLUDE).context("Static libraries")?;
for static_lib in static_lib_globs.walk(self.manifest_dir) {
let actual = Name(format!(
"{}-{}-{}",
self.index.private_rule_name(self.package),
name,
static_lib.file_name().unwrap().to_string_lossy(),
));
if *public {
let rule = Rule::Alias(Alias {
name: Name(format!(
"{}-{}-{}",
self.index.public_rule_name(self.package),
name,
static_lib.file_name().unwrap().to_string_lossy(),
)),
actual: actual.clone(),
visibility: self.public_visibility(),
});
res.push(rule);
}
let rule = buck::PrebuiltCxxLibrary {
common: Common {
name: actual,
visibility: Visibility::Private,
licenses: Default::default(),
compatible_with: compatible_with
.iter()
.cloned()
.map(RuleRef::new)
.collect(),
},
static_lib: self.subtarget_or_path(&static_lib)?,
};
res.push(Rule::PrebuiltCxxLibrary(rule));
}
if self.config.strict_globs {
static_lib_globs.check_all_globs_used()?;
}
}
// Complain and omit
BuildscriptFixup::Unresolved(msg) => {
let unresolved_package_msg = format!(
"{} has a build script, but I don't know what to do with it: {}",
self.package, msg
);
if config.unresolved_fixup_error {
log::error!("{}", unresolved_package_msg);
return Err(anyhow!(
"Unresolved fix up errors, fix them and rerun buckify."
));
} else {
log::warn!("{}", unresolved_package_msg);
}
}
}
}
if let Some(buildscript_run) = buildscript_run {
res.push(Rule::BuildscriptGenrule(buildscript_run));
}
Ok(res)
}
/// Return the set of features to enable, which is the union of the cargo-resolved ones
/// and additional ones defined in the fixup.
pub fn compute_features(
&self,
) -> anyhow::Result<HashMap<Option<PlatformExpr>, BTreeSet<String>>> {
let mut ret = HashMap::new();
let mut platform_omits = HashMap::new();
for (platform, fixup) in self.fixup_config.configs(&self.package.version) {
// Group by feature which platforms omit it.
for feature in &fixup.omit_features {
platform_omits
.entry(feature.as_str())
.or_insert_with(HashSet::new)
.insert(platform);
}
if !fixup.features.is_empty() {
ret.entry(platform.cloned())
.or_insert_with(BTreeSet::new)
.extend(fixup.features.clone());
}
}
for feature in self.index.resolved_features(self.package) {
let Some(omitted_platforms) = platform_omits.get(feature) else {
// Feature is unconditionally included on all platforms.
ret.entry(None)
.or_insert_with(BTreeSet::new)
.insert(feature.to_owned());
continue;
};
if omitted_platforms.contains(&None) {
// Feature is unconditionally omitted on all platforms.
continue;
}
let mut excludes = vec![];
for platform in omitted_platforms {
if let Some(platform_expr) = platform {
// If a platform filters a feature added by the base,
// we need to filter it from the base and add it to all
// other platforms. Create a predicate that excludes all
// filtered platforms. This will be the "all other
// platforms".
let platform_pred = PlatformPredicate::parse(platform_expr)?;
excludes.push(PlatformPredicate::Not(Box::new(platform_pred)));
}
}
assert!(!excludes.is_empty());
let platform_pred = PlatformPredicate::All(excludes);
let platform_expr: PlatformExpr = format!("cfg({})", platform_pred).into();
ret.entry(Some(platform_expr))
.or_insert_with(BTreeSet::new)
.insert(feature.to_owned());
}
Ok(ret)
}
fn buildscript_rustc_flags(
&self,
) -> Vec<(
Option<PlatformExpr>,
(Vec<String>, BTreeMap<String, Vec<String>>),
)> {
let mut ret = vec![];
if self.buildscript_target().is_none() {
return ret; // no buildscript
}
for (platform, config) in self.fixup_config.configs(&self.package.version) {
let mut flags = vec![];
for buildscript in &config.buildscript {
if !self.target_match(buildscript) {
continue;
}
if let BuildscriptFixup::RustcFlags(_) = buildscript {
flags.push(format!(
"@$(location :{}[rustc_flags])",
self.buildscript_genrule_name()
))
}
}
if !flags.is_empty() {
ret.push((platform.cloned(), (flags, Default::default())));
}
}
ret
}
/// Return extra command-line options, with platform annotation if needed
pub fn compute_cmdline(
&self,
) -> Vec<(
Option<PlatformExpr>,
(Vec<String>, BTreeMap<String, Vec<String>>),
)> {
let mut ret = vec![];
for (platform, config) in self.fixup_config.configs(&self.package.version) {
let mut flags = vec![];
flags.extend(config.rustc_flags.clone());
flags.extend(config.cfgs.iter().map(|cfg| format!("--cfg={}", cfg)));
let flags_select = config.rustc_flags_select.clone();
if !flags.is_empty() || !flags_select.is_empty() {
ret.push((platform.cloned(), (flags, flags_select)));
}
}
ret.extend(self.buildscript_rustc_flags());
ret
}
/// Generate the set of deps for the target. This could just return the unmodified
/// depenedencies, or it could add/remove them. This returns the Buck rule reference
/// and the corresponding package if there is one (so the caller can limit its enumeration
/// to only targets which were actually used).
pub fn compute_deps(
&self,
) -> anyhow::Result<
Vec<(
Option<&'meta Manifest>,
RuleRef,
Option<&'meta str>,
&'meta NodeDepKind,
)>,
> {
let mut ret = vec![];
let mut omits = HashMap::new();
let mut all_omits = HashSet::new();
// Pre-compute the list of all filtered dependencies. If a platform filters a dependency
// added by the base, we need to filter it from the base and add it to all other platforms.
for (platform, fixup) in self.fixup_config.configs(&self.package.version) {
let platform_omits = omits.entry(platform).or_insert_with(HashSet::new);
platform_omits.extend(fixup.omit_deps.iter().map(String::as_str));
all_omits.extend(fixup.omit_deps.iter().map(String::as_str));
}
for ResolvedDep {
package,
platform,
rename,
dep_kind,
} in self
.index
.resolved_deps_for_target(self.package, self.target)
{
log::debug!(
"Resolved deps for {}/{} ({:?}) - {} as {} ({:?})",
self.package,
self.target.name,
self.target.kind(),
package,
rename,
self.target.kind()
);
// Only use the rename if it isn't the same as the target anyway.
let tgtname = package
.dependency_target()
.map(|tgt| tgt.name.replace('-', "_"));
let original_rename = rename;
let rename = match tgtname {
Some(ref tgtname) if tgtname == rename => None,
Some(_) | None => Some(rename),
};
if omits
.get(&None)
.is_some_and(|omits| omits.contains(original_rename))
{
// Dependency is unconditionally omitted on all platforms.
continue;
} else if all_omits.contains(original_rename) {
// If the dependency is for a particular platform and that has it excluded,
// skip it.
if let Some(platform_omits) = omits.get(&platform.as_ref()) {
if platform_omits.contains(original_rename) {
continue;
}
}
// If it's a default dependency, but certain specific platforms filter it,
// produce a new rule that excludes those platforms.
if platform.is_none() {
// Create a new predicate that excludes all filtered platforms.
let mut excludes = vec![];
for (platform_expr, platform_omits) in &omits {
if let Some(platform_expr) = platform_expr {
if platform_omits.contains(original_rename) {
let platform_pred = PlatformPredicate::parse(platform_expr)?;
excludes.push(PlatformPredicate::Not(Box::new(platform_pred)));
}
}
}
let platform_pred = PlatformPredicate::All(excludes);
let platform_expr: PlatformExpr = format!("cfg({})", platform_pred).into();
ret.push((
Some(package),
RuleRef::from(self.index.private_rule_name(package))
.with_platform(Some(&platform_expr)),
rename,
dep_kind,
));
// Since we've already added the platform-excluding rule, skip the generic rule
// adding below.
continue;
}
}
// No filtering involved? Just insert it like normal.
ret.push((
Some(package),
RuleRef::from(self.index.private_rule_name(package))
.with_platform(platform.as_ref()),
rename,
dep_kind,
))
}
for (platform, config) in self.fixup_config.configs(&self.package.version) {
ret.extend(config.extra_deps.iter().map(|dep| {
(
None,
RuleRef::new(dep.to_string()).with_platform(platform),
None,
&NodeDepKind::ORDINARY,
)
}));
for buildscript in &config.buildscript {
if !self.target_match(buildscript) {
continue;
}
if let BuildscriptFixup::CxxLibrary(CxxLibraryFixup {
add_dep: true,
name,
..
}) = buildscript
{
ret.push((
None,
RuleRef::new(format!(
":{}-{}",
self.index.private_rule_name(self.package),
name
))
.with_platform(platform),
None,
&NodeDepKind::ORDINARY,
));
}
if let BuildscriptFixup::PrebuiltCxxLibrary(PrebuiltCxxLibraryFixup {
add_dep: true,
name,
static_libs,
..
}) = buildscript
{
let mut static_lib_globs =
Globs::new(static_libs, NO_EXCLUDE).context("Prebuilt C++ libraries")?;
for static_lib in static_lib_globs.walk(self.manifest_dir) {
ret.push((
None,
RuleRef::new(format!(
":{}-{}-{}",
self.index.private_rule_name(self.package),
name,
static_lib.file_name().unwrap().to_string_lossy(),
))
.with_platform(platform),
None,
&NodeDepKind::ORDINARY,
));
}
}
}
}
Ok(ret)
}
/// Additional environment
pub fn compute_env(
&self,
) -> anyhow::Result<Vec<(Option<PlatformExpr>, BTreeMap<String, StringOrPath>)>> {
let mut ret = vec![];
for (platform, config) in self.fixup_config.configs(&self.package.version) {
let mut map: BTreeMap<String, StringOrPath> = config
.env
.iter()
.map(|(k, v)| (k.clone(), StringOrPath::String(v.clone())))
.collect();
for cargo_env in config.cargo_env.iter() {
let v = match cargo_env {
CargoEnv::CARGO_MANIFEST_DIR => {
if matches!(self.config.vendor, VendorConfig::Source(_))
|| matches!(self.package.source, Source::Local)
{
StringOrPath::Path(BuckPath(relative_path(
&self.third_party_dir,
self.manifest_dir,
)))
} else if let VendorConfig::LocalRegistry = self.config.vendor {
StringOrPath::String(format!(
"{}-{}.crate",
self.package.name, self.package.version,
))
} else if let Source::Git { repo, .. } = &self.package.source {
let short_name = short_name_for_git_repo(repo)?;
StringOrPath::String(short_name.to_owned())
} else {
StringOrPath::String(format!(
"{}-{}.crate",
self.package.name, self.package.version,
))
}
}
CargoEnv::CARGO_PKG_AUTHORS => {
StringOrPath::String(self.package.authors.join(":"))
}
CargoEnv::CARGO_PKG_DESCRIPTION => {
StringOrPath::String(self.package.description.clone().unwrap_or_default())
}
CargoEnv::CARGO_PKG_REPOSITORY => {
StringOrPath::String(self.package.repository.clone().unwrap_or_default())
}
CargoEnv::CARGO_PKG_VERSION => {
StringOrPath::String(self.package.version.to_string())
}
CargoEnv::CARGO_PKG_VERSION_MAJOR => {
StringOrPath::String(self.package.version.major.to_string())
}
CargoEnv::CARGO_PKG_VERSION_MINOR => {
StringOrPath::String(self.package.version.minor.to_string())
}
CargoEnv::CARGO_PKG_VERSION_PATCH => {
StringOrPath::String(self.package.version.patch.to_string())
}
CargoEnv::CARGO_PKG_NAME => StringOrPath::String(self.package.name.clone()),
};
map.insert(cargo_env.to_string(), v);
}
if !map.is_empty() {
ret.push((platform.cloned(), map));
}
}
Ok(ret)
}
/// Given a glob for the srcs, walk the filesystem to get the full set.
/// `srcs` is the normal source glob rooted at the package's manifest dir.
pub fn compute_srcs(
&self,
srcs: Vec<PathBuf>,
) -> anyhow::Result<Vec<(Option<PlatformExpr>, BTreeSet<PathBuf>)>> {
let mut ret: Vec<(Option<PlatformExpr>, BTreeSet<PathBuf>)> = vec![];
// This function is only used in vendoring mode, so it's guaranteed that
// manifest_dir is a subdirectory of third_party_dir.
assert!(
matches!(self.config.vendor, VendorConfig::Source(_))
|| matches!(self.package.source, Source::Local)
);
let manifest_rel = relative_path(&self.third_party_dir, self.manifest_dir);
let srcs_globs: Vec<String> = srcs
.iter()
.map(|src| src.to_string_lossy().into_owned())
.collect();
log::debug!(
"pkg {}, srcs {:?}, manifest_rel {}",
self.package,
srcs_globs,
manifest_rel.display()
);
// Do any platforms have an overlay or platform-specific mapped srcs or
// omitted sources? If so, the srcs are per-platform.
let needs_per_platform_srcs =
self.fixup_config
.configs(&self.package.version)
.any(|(platform, config)| {
platform.is_some()
&& (config.overlay.is_some()
|| !config.extra_mapped_srcs.is_empty()
|| !config.omit_srcs.is_empty())
});
let mut common_files = HashSet::new();
let mut srcs_globs = Globs::new(srcs_globs, NO_EXCLUDE).context("Srcs")?;
for path in srcs_globs.walk(self.manifest_dir) {
common_files.insert(manifest_rel.join(path));
}
if self.config.strict_globs {
// Do not check srcs_globs.check_all_globs_used(). Base sources are
// not required because they are either computed precisely or a
// random guess of globs.
}
if let Some(base) = self.fixup_config.base(&self.package.version) {
common_files.extend(self.compute_extra_srcs(&base.extra_srcs)?);
}
let no_omit_srcs;
let (common_overlay_files, common_omit_srcs) =
match self.fixup_config.base(&self.package.version) {
Some(base) => (
base.overlay_and_mapped_files(&self.fixup_dir)?,
&base.omit_srcs,
),
None => {
no_omit_srcs = GlobSet::default();
(HashSet::default(), &no_omit_srcs)
}
};
if !needs_per_platform_srcs {
let mut set = BTreeSet::new();
for file in &common_files {