@@ -491,28 +491,28 @@ func (target *BuildTarget) StartTestSuite() {
491
491
}
492
492
493
493
// AllSourcePaths returns all the source paths for this target
494
- func (target * BuildTarget ) AllSourcePaths (graph * BuildGraph ) []string {
495
- return target .allSourcePaths (graph , BuildInput .Paths )
494
+ func (target * BuildTarget ) AllSourcePaths (graph * BuildGraph , ffDefaultProvide bool ) []string {
495
+ return target .allSourcePaths (graph , BuildInput .Paths , ffDefaultProvide )
496
496
}
497
497
498
498
// AllSourceFullPaths returns all the source paths for this target, with a leading
499
499
// plz-out/gen etc if appropriate.
500
- func (target * BuildTarget ) AllSourceFullPaths (graph * BuildGraph ) []string {
501
- return target .allSourcePaths (graph , BuildInput .FullPaths )
500
+ func (target * BuildTarget ) AllSourceFullPaths (graph * BuildGraph , ffDefaultProvide bool ) []string {
501
+ return target .allSourcePaths (graph , BuildInput .FullPaths , ffDefaultProvide )
502
502
}
503
503
504
504
// AllSourceLocalPaths returns the local part of all the source paths for this target,
505
505
// i.e. without this target's package in it.
506
- func (target * BuildTarget ) AllSourceLocalPaths (graph * BuildGraph ) []string {
507
- return target .allSourcePaths (graph , BuildInput .LocalPaths )
506
+ func (target * BuildTarget ) AllSourceLocalPaths (graph * BuildGraph , ffDefaultProvide bool ) []string {
507
+ return target .allSourcePaths (graph , BuildInput .LocalPaths , ffDefaultProvide )
508
508
}
509
509
510
510
type buildPathsFunc func (BuildInput , * BuildGraph ) []string
511
511
512
- func (target * BuildTarget ) allSourcePaths (graph * BuildGraph , full buildPathsFunc ) []string {
512
+ func (target * BuildTarget ) allSourcePaths (graph * BuildGraph , full buildPathsFunc , ffDefaultProvide bool ) []string {
513
513
ret := make ([]string , 0 , len (target .Sources ))
514
514
for _ , source := range target .AllSources () {
515
- ret = append (ret , target .sourcePaths (graph , source , full )... )
515
+ ret = append (ret , target .sourcePaths (graph , source , full , ffDefaultProvide )... )
516
516
}
517
517
return ret
518
518
}
@@ -533,7 +533,7 @@ func (target *BuildTarget) AllURLs(state *BuildState) []string {
533
533
// TODO(peterebden,tatskaari): Work out if we really want to have this and how the suite of *Dependencies functions
534
534
//
535
535
// below should behave (preferably nicely).
536
- func (target * BuildTarget ) resolveDependencies (graph * BuildGraph , callback func (* BuildTarget ) error ) error {
536
+ func (target * BuildTarget ) resolveDependencies (graph * BuildGraph , callback func (* BuildTarget ) error , ffDefaultProvide bool ) error {
537
537
var g errgroup.Group
538
538
target .mutex .RLock ()
539
539
for i := range target .dependencies {
@@ -542,7 +542,7 @@ func (target *BuildTarget) resolveDependencies(graph *BuildGraph, callback func(
542
542
continue // already done
543
543
}
544
544
g .Go (func () error {
545
- if err := target .resolveOneDependency (graph , dep ); err != nil {
545
+ if err := target .resolveOneDependency (graph , dep , ffDefaultProvide ); err != nil {
546
546
return err
547
547
}
548
548
for _ , d := range dep .deps {
@@ -557,14 +557,15 @@ func (target *BuildTarget) resolveDependencies(graph *BuildGraph, callback func(
557
557
return g .Wait ()
558
558
}
559
559
560
- func (target * BuildTarget ) resolveOneDependency (graph * BuildGraph , dep * depInfo ) error {
560
+ func (target * BuildTarget ) resolveOneDependency (graph * BuildGraph , dep * depInfo , ffDefaultProvide bool ) error {
561
561
t := graph .WaitForTarget (* dep .declared )
562
562
if t == nil {
563
563
return fmt .Errorf ("Couldn't find dependency %s" , dep .declared )
564
564
}
565
565
dep .declared = & t .Label // saves memory by not storing the label twice once resolved
566
566
567
- labels := t .provideFor (target )
567
+ // TODO(jpoole): shouldn't this use the ProvideFor wrapper that handles resolving to self if there's no match?
568
+ labels := t .provideFor (target , ffDefaultProvide )
568
569
569
570
if len (labels ) == 0 {
570
571
target .mutex .Lock ()
@@ -595,7 +596,7 @@ func (target *BuildTarget) resolveOneDependency(graph *BuildGraph, dep *depInfo)
595
596
// MustResolveDependencies is exposed only for testing purposes.
596
597
// TODO(peterebden, tatskaari): See if we can get rid of this.
597
598
func (target * BuildTarget ) ResolveDependencies (graph * BuildGraph ) error {
598
- return target .resolveDependencies (graph , func (* BuildTarget ) error { return nil })
599
+ return target .resolveDependencies (graph , func (* BuildTarget ) error { return nil }, false )
599
600
}
600
601
601
602
// DeclaredDependencies returns all the targets this target declared any kind of dependency on (including sources and tools).
@@ -880,19 +881,19 @@ func (target *BuildTarget) GetRealOutput(output string) string {
880
881
}
881
882
882
883
// SourcePaths returns the source paths for a given set of sources.
883
- func (target * BuildTarget ) SourcePaths (graph * BuildGraph , sources []BuildInput ) []string {
884
+ func (target * BuildTarget ) SourcePaths (graph * BuildGraph , sources []BuildInput , ffDefaultProvide bool ) []string {
884
885
ret := make ([]string , 0 , len (sources ))
885
886
for _ , source := range sources {
886
- ret = append (ret , target .sourcePaths (graph , source , BuildInput .Paths )... )
887
+ ret = append (ret , target .sourcePaths (graph , source , BuildInput .Paths , ffDefaultProvide )... )
887
888
}
888
889
return ret
889
890
}
890
891
891
892
// sourcePaths returns the source paths for a single source.
892
- func (target * BuildTarget ) sourcePaths (graph * BuildGraph , source BuildInput , f buildPathsFunc ) []string {
893
+ func (target * BuildTarget ) sourcePaths (graph * BuildGraph , source BuildInput , f buildPathsFunc , ffDefaultProvide bool ) []string {
893
894
if label , ok := source .nonOutputLabel (); ok {
894
895
ret := []string {}
895
- for _ , providedLabel := range graph .TargetOrDie (label ).ProvideFor (target ) {
896
+ for _ , providedLabel := range graph .TargetOrDie (label ).ProvideFor (target , ffDefaultProvide ) {
896
897
ret = append (ret , f (providedLabel , graph )... )
897
898
}
898
899
return ret
@@ -1195,20 +1196,25 @@ func (target *BuildTarget) AddProvide(language string, label BuildLabel) {
1195
1196
}
1196
1197
1197
1198
// ProvideFor returns the build label that we'd provide for the given target.
1198
- func (target * BuildTarget ) ProvideFor (other * BuildTarget ) []BuildLabel {
1199
- if p := target .provideFor (other ); len (p ) > 0 {
1199
+ func (target * BuildTarget ) ProvideFor (other * BuildTarget , ffDefaultProvide bool ) []BuildLabel {
1200
+ if p := target .provideFor (other , ffDefaultProvide ); len (p ) > 0 {
1200
1201
return p
1201
1202
}
1202
1203
return []BuildLabel {target .Label }
1203
1204
}
1204
1205
1205
1206
// provideFor is like ProvideFor but returns an empty slice if there is a direct dependency.
1206
1207
// It's a small optimisation to save allocating extra slices.
1207
- func (target * BuildTarget ) provideFor (other * BuildTarget ) []BuildLabel {
1208
+ func (target * BuildTarget ) provideFor (other * BuildTarget , ffDefaultProvide bool ) []BuildLabel {
1208
1209
target .mutex .RLock ()
1209
1210
defer target .mutex .RUnlock ()
1211
+ var defaultValue []BuildLabel
1212
+ if def , ok := target .Provides ["default" ]; ok && ffDefaultProvide {
1213
+ defaultValue = []BuildLabel {def }
1214
+ }
1215
+
1210
1216
if target .Provides == nil || len (other .Requires ) == 0 {
1211
- return nil
1217
+ return defaultValue
1212
1218
}
1213
1219
// Never do this if the other target has a data or tool dependency on us.
1214
1220
for _ , data := range other .Data {
@@ -1228,6 +1234,10 @@ func (target *BuildTarget) provideFor(other *BuildTarget) []BuildLabel {
1228
1234
ret = append (ret , label )
1229
1235
}
1230
1236
}
1237
+
1238
+ if len (ret ) == 0 {
1239
+ return defaultValue
1240
+ }
1231
1241
return ret
1232
1242
}
1233
1243
0 commit comments