82
82
import com .google .common .hash .Hashing ;
83
83
import java .nio .charset .StandardCharsets ;
84
84
import java .nio .file .Path ;
85
- import java .nio .file .Paths ;
86
85
import java .util .Collection ;
87
86
import java .util .List ;
88
87
import java .util .Map ;
@@ -112,7 +111,7 @@ public static RustCompileRule requireBuild(
112
111
CrateType crateType ,
113
112
Optional <String > edition ,
114
113
LinkableDepType depType ,
115
- ImmutableSortedMap <SourcePath , String > mappedSources ,
114
+ ImmutableSortedMap <SourcePath , Optional < String > > mappedSources ,
116
115
String rootModule ,
117
116
boolean forceRlib ,
118
117
boolean preferStatic ,
@@ -246,7 +245,7 @@ private static RustCompileRule createBuild(
246
245
Optional <String > edition ,
247
246
LinkableDepType depType ,
248
247
boolean rpath ,
249
- ImmutableSortedMap <SourcePath , String > mappedSources ,
248
+ ImmutableSortedMap <SourcePath , Optional < String > > mappedSources ,
250
249
String rootModule ,
251
250
boolean forceRlib ,
252
251
boolean preferStatic ,
@@ -642,7 +641,7 @@ public static BinaryWrapperRule createBinaryBuildRule(
642
641
643
642
CxxPlatform cxxPlatform = rustPlatform .getCxxPlatform ();
644
643
645
- Pair <String , ImmutableSortedMap <SourcePath , String >> rootModuleAndSources =
644
+ Pair <String , ImmutableSortedMap <SourcePath , Optional < String > >> rootModuleAndSources =
646
645
getRootModuleAndSources (
647
646
projectFilesystem ,
648
647
buildTarget ,
@@ -830,15 +829,16 @@ public static Optional<String> getCrateRoot(
830
829
SourcePathResolverAdapter resolver ,
831
830
String crate ,
832
831
ImmutableSet <String > defaults ,
833
- Stream <String > sources ) {
832
+ Stream <Path > sources ) {
834
833
String crateName = String .format ("%s.rs" , crate );
835
834
ImmutableList <String > res =
836
835
sources
837
836
.filter (
838
837
src -> {
839
- String name = Paths . get ( src ) .getFileName ().toString ();
838
+ String name = src .getFileName ().toString ();
840
839
return defaults .contains (name ) || name .equals (crateName );
841
840
})
841
+ .map (src -> src .toString ())
842
842
.collect (ImmutableList .toImmutableList ());
843
843
844
844
if (res .size () == 1 ) {
@@ -854,7 +854,7 @@ public static Optional<String> getCrateRoot(
854
854
* module is what's passed to rustc as the input source file, and may not exist until the
855
855
* symlinking phase happens.
856
856
*/
857
- static Pair <String , ImmutableSortedMap <SourcePath , String >> getRootModuleAndSources (
857
+ static Pair <String , ImmutableSortedMap <SourcePath , Optional < String > >> getRootModuleAndSources (
858
858
ProjectFilesystem projectFilesystem ,
859
859
BuildTarget target ,
860
860
ActionGraphBuilder graphBuilder ,
@@ -865,64 +865,55 @@ static Pair<String, ImmutableSortedMap<SourcePath, String>> getRootModuleAndSour
865
865
ImmutableSortedSet <SourcePath > srcs ,
866
866
ImmutableSortedMap <SourcePath , String > mappedSrcs ) {
867
867
868
- ImmutableSortedMap .Builder <SourcePath , String > fixedBuilder = ImmutableSortedMap .naturalOrder ();
869
-
870
- Path buildTargetPath =
871
- target .getCellRelativeBasePath ().getPath ().toPath (projectFilesystem .getFileSystem ());
872
-
873
- SourcePathResolverAdapter resolver = graphBuilder .getSourcePathResolver ();
868
+ ImmutableSortedMap .Builder <SourcePath , Optional <String >> fixedBuilder =
869
+ ImmutableSortedMap .naturalOrder ();
874
870
875
871
srcs .stream ()
876
872
.map (
877
873
src ->
878
874
CxxGenruleDescription .fixupSourcePath (graphBuilder , cxxPlatform .getFlavor (), src ))
879
- .forEach (
880
- src -> {
881
- SourcePath srcPath =
882
- CxxGenruleDescription .fixupSourcePath (graphBuilder , cxxPlatform .getFlavor (), src );
883
- Path candidatePath = resolver .getCellUnsafeRelPath (src ).getPath ();
884
-
885
- // These are files with no user-defined virtual path, they are either
886
- // generated (ie with a genrule) or references files in the source
887
- // tree
888
-
889
- // If the path references files in the source tree
890
- if (candidatePath .startsWith (buildTargetPath )) {
891
- // We relativize them to reduce path amplification - mostly for
892
- // windows's sake
893
- fixedBuilder .put (srcPath , buildTargetPath .relativize (candidatePath ).toString ());
894
- } else {
895
-
896
- // These are generated files, since these are generated paths with hashes, they are
897
- // rewritable.
898
- fixedBuilder .put (
899
- srcPath , resolver .getAbsolutePath (srcPath ).getPath ().getFileName ().toString ());
900
- }
901
- });
875
+ .forEach (src -> fixedBuilder .put (src , Optional .empty ()));
902
876
mappedSrcs
903
877
.entrySet ()
904
878
.forEach (
905
- ent -> {
906
- SourcePath sourcePath =
907
- CxxGenruleDescription .fixupSourcePath (
908
- graphBuilder , cxxPlatform .getFlavor (), ent .getKey ());
909
- Path virtualPath = Paths .get (ent .getValue ()).normalize ();
910
- // These are rs files with user-defined virtual path, usually these are
911
- // the from mapped_srcs
912
- // Ban remappings that contain paths that go outside the base path
913
- if (virtualPath .startsWith (".." )) {
914
- throw new HumanReadableException (
915
- "Mapped sources must be forward paths ie. no ../ " );
916
- }
917
- fixedBuilder .put (sourcePath , virtualPath .toString ());
918
- });
919
-
920
- ImmutableSortedMap <SourcePath , String > fixed = fixedBuilder .build ();
879
+ ent ->
880
+ fixedBuilder .put (
881
+ CxxGenruleDescription .fixupSourcePath (
882
+ graphBuilder , cxxPlatform .getFlavor (), ent .getKey ()),
883
+ Optional .of (ent .getValue ())));
884
+
885
+ ImmutableSortedMap <SourcePath , Optional <String >> fixed = fixedBuilder .build ();
886
+
887
+ SourcePathResolverAdapter resolver = graphBuilder .getSourcePathResolver ();
888
+ Stream <Path > filenames =
889
+ Stream .concat (
890
+ srcs .stream ()
891
+ .map (
892
+ src ->
893
+ CxxGenruleDescription .fixupSourcePath (
894
+ graphBuilder , cxxPlatform .getFlavor (), src ))
895
+ .map (sp -> resolver .getCellUnsafeRelPath (sp ).getPath ()),
896
+ mappedSrcs .values ().stream ()
897
+ .map (
898
+ path ->
899
+ target
900
+ .getCellRelativeBasePath ()
901
+ .getPath ()
902
+ .toPath (projectFilesystem .getFileSystem ())
903
+ .resolve (path )));
921
904
922
905
Optional <String > rootModule =
923
906
crateRoot
907
+ .map (
908
+ name ->
909
+ target
910
+ .getCellRelativeBasePath ()
911
+ .getPath ()
912
+ .toPath (projectFilesystem .getFileSystem ())
913
+ .resolve (name )
914
+ .toString ())
924
915
.map (Optional ::of )
925
- .orElseGet (() -> getCrateRoot (resolver , crate , defaultRoots , fixed . values (). stream () ));
916
+ .orElseGet (() -> getCrateRoot (resolver , crate , defaultRoots , filenames ));
926
917
927
918
return new Pair <>(
928
919
rootModule .orElseThrow (
0 commit comments