20
20
import com .google .common .collect .ImmutableList ;
21
21
import com .google .common .collect .ImmutableMap ;
22
22
import com .google .common .collect .Interner ;
23
+ import com .google .common .collect .Maps ;
23
24
import com .google .common .collect .Sets ;
24
25
import com .google .devtools .build .lib .actions .AbstractCommandLine ;
25
26
import com .google .devtools .build .lib .actions .ActionKeyContext ;
36
37
import com .google .devtools .build .lib .actions .PathMapper ;
37
38
import com .google .devtools .build .lib .actions .SingleStringArgFormatter ;
38
39
import com .google .devtools .build .lib .cmdline .Label ;
40
+ import com .google .devtools .build .lib .cmdline .RepositoryMapping ;
41
+ import com .google .devtools .build .lib .cmdline .RepositoryName ;
39
42
import com .google .devtools .build .lib .collect .nestedset .NestedSet ;
40
43
import com .google .devtools .build .lib .concurrent .BlazeInterners ;
41
44
import com .google .devtools .build .lib .skyframe .serialization .VisibleForSerialization ;
@@ -182,7 +185,8 @@ private int eval(
182
185
int argi ,
183
186
List <String > builder ,
184
187
@ Nullable ArtifactExpander artifactExpander ,
185
- PathMapper pathMapper )
188
+ PathMapper pathMapper ,
189
+ @ Nullable RepositoryMapping mainRepoMapping )
186
190
throws CommandLineExpansionException , InterruptedException {
187
191
StarlarkCallable mapEach = null ;
188
192
StarlarkSemantics starlarkSemantics = null ;
@@ -219,7 +223,7 @@ private int eval(
219
223
int count = expandedValues .size ();
220
224
stringValues = new ArrayList <>(expandedValues .size ());
221
225
for (int i = 0 ; i < count ; ++i ) {
222
- stringValues .add (expandToCommandLine (expandedValues .get (i ), pathMapper ));
226
+ stringValues .add (expandToCommandLine (expandedValues .get (i ), pathMapper , mainRepoMapping ));
223
227
}
224
228
}
225
229
// It's safe to uniquify at this stage, any transformations after this
@@ -607,9 +611,15 @@ static void push(List<Object> arguments, Object object, String format) {
607
611
arguments .add (format );
608
612
}
609
613
610
- static int eval (List <Object > arguments , int argi , List <String > builder , PathMapper pathMapper ) {
614
+ static int eval (
615
+ List <Object > arguments ,
616
+ int argi ,
617
+ List <String > builder ,
618
+ PathMapper pathMapper ,
619
+ @ Nullable RepositoryMapping mainRepoMapping ) {
611
620
Object object = arguments .get (argi ++);
612
- String stringValue = StarlarkCustomCommandLine .expandToCommandLine (object , pathMapper );
621
+ String stringValue =
622
+ StarlarkCustomCommandLine .expandToCommandLine (object , pathMapper , mainRepoMapping );
613
623
String formatStr = (String ) arguments .get (argi ++);
614
624
builder .add (SingleStringArgFormatter .format (formatStr , stringValue ));
615
625
return argi ;
@@ -662,10 +672,13 @@ Builder addFormatted(Object object, String format) {
662
672
return this ;
663
673
}
664
674
665
- CommandLine build (boolean flagPerLine ) {
675
+ CommandLine build (boolean flagPerLine , @ Nullable RepositoryMapping mainRepoMapping ) {
666
676
if (arguments .isEmpty ()) {
667
677
return CommandLine .empty ();
668
678
}
679
+ if (mainRepoMapping != null ) {
680
+ arguments .add (mainRepoMapping );
681
+ }
669
682
Object [] args = arguments .toArray ();
670
683
return flagPerLine
671
684
? new StarlarkCustomCommandLineWithIndexes (args , argStartIndexes .build ())
@@ -700,20 +713,37 @@ public Iterable<String> arguments(
700
713
List <String > result = new ArrayList <>();
701
714
List <Object > arguments = rawArgsAsList ();
702
715
703
- for (int argi = 0 ; argi < arguments .size (); ) {
716
+ RepositoryMapping mainRepoMapping ;
717
+ int size ;
718
+ if (arguments .getLast () instanceof RepositoryMapping ) {
719
+ mainRepoMapping = (RepositoryMapping ) arguments .getLast ();
720
+ size = arguments .size () - 1 ;
721
+ } else {
722
+ mainRepoMapping = null ;
723
+ size = arguments .size ();
724
+ }
725
+
726
+ for (int argi = 0 ; argi < size ; ) {
704
727
Object arg = arguments .get (argi ++);
705
728
if (arg instanceof VectorArg ) {
706
- argi = ((VectorArg ) arg ).eval (arguments , argi , result , artifactExpander , pathMapper );
729
+ argi =
730
+ ((VectorArg ) arg )
731
+ .eval (arguments , argi , result , artifactExpander , pathMapper , mainRepoMapping );
707
732
} else if (arg == SingleFormattedArg .MARKER ) {
708
- argi = SingleFormattedArg .eval (arguments , argi , result , pathMapper );
733
+ argi = SingleFormattedArg .eval (arguments , argi , result , pathMapper , mainRepoMapping );
709
734
} else {
710
- result .add (expandToCommandLine (arg , pathMapper ));
735
+ result .add (expandToCommandLine (arg , pathMapper , mainRepoMapping ));
711
736
}
712
737
}
713
738
return ImmutableList .copyOf (pathMapper .mapCustomStarlarkArgs (result ));
714
739
}
715
740
716
- private static String expandToCommandLine (Object object , PathMapper pathMapper ) {
741
+ private static String expandToCommandLine (
742
+ Object object , PathMapper pathMapper , @ Nullable RepositoryMapping mainRepoMapping ) {
743
+ if (mainRepoMapping != null && object instanceof Label label ) {
744
+ return label .getDisplayForm (mainRepoMapping );
745
+ }
746
+
717
747
// It'd be nice to build this into DerivedArtifact's CommandLine interface so we don't have
718
748
// to explicitly check if an object is a DerivedArtifact. Unfortunately that would require
719
749
// a lot more dependencies on the Java library DerivedArtifact is built into.
@@ -751,7 +781,17 @@ public Iterable<String> arguments(
751
781
Iterator <Integer > startIndexIterator = argStartIndexes .iterator ();
752
782
int nextStartIndex = startIndexIterator .hasNext () ? startIndexIterator .next () : -1 ;
753
783
754
- for (int argi = 0 ; argi < arguments .size (); ) {
784
+ RepositoryMapping mainRepoMapping ;
785
+ int size ;
786
+ if (arguments .getLast () instanceof RepositoryMapping ) {
787
+ mainRepoMapping = (RepositoryMapping ) arguments .getLast ();
788
+ size = arguments .size () - 1 ;
789
+ } else {
790
+ mainRepoMapping = null ;
791
+ size = arguments .size ();
792
+ }
793
+
794
+ for (int argi = 0 ; argi < size ; ) {
755
795
756
796
// Record the actual beginning of each group.
757
797
if (argi == nextStartIndex ) {
@@ -761,11 +801,14 @@ public Iterable<String> arguments(
761
801
762
802
Object arg = arguments .get (argi ++);
763
803
if (arg instanceof VectorArg ) {
764
- argi = ((VectorArg ) arg ).eval (arguments , argi , result , artifactExpander , pathMapper );
804
+ argi =
805
+ ((VectorArg ) arg )
806
+ .eval (arguments , argi , result , artifactExpander , pathMapper , mainRepoMapping );
765
807
} else if (arg == SingleFormattedArg .MARKER ) {
766
- argi = SingleFormattedArg .eval (arguments , argi , result , pathMapper );
808
+ argi = SingleFormattedArg .eval (arguments , argi , result , pathMapper , mainRepoMapping );
767
809
} else {
768
- result .add (StarlarkCustomCommandLine .expandToCommandLine (arg , pathMapper ));
810
+ result .add (
811
+ StarlarkCustomCommandLine .expandToCommandLine (arg , pathMapper , mainRepoMapping ));
769
812
}
770
813
}
771
814
@@ -796,7 +839,15 @@ public void addToFingerprint(
796
839
Fingerprint fingerprint )
797
840
throws CommandLineExpansionException , InterruptedException {
798
841
List <Object > arguments = rawArgsAsList ();
799
- for (int argi = 0 ; argi < arguments .size (); ) {
842
+ int size ;
843
+ if (arguments .getLast () instanceof RepositoryMapping mainRepoMapping ) {
844
+ fingerprint .addStringMap (
845
+ Maps .transformValues (mainRepoMapping .entries (), RepositoryName ::getName ));
846
+ size = arguments .size () - 1 ;
847
+ } else {
848
+ size = arguments .size ();
849
+ }
850
+ for (int argi = 0 ; argi < size ; ) {
800
851
Object arg = arguments .get (argi ++);
801
852
if (arg instanceof VectorArg ) {
802
853
argi =
0 commit comments