@@ -637,20 +637,6 @@ void process(NSString *path, void(^dylibVisitor)(NSString *),
637
637
}
638
638
639
639
640
- @implementation NSString (sst)
641
- -(NSString *)sst_stringByAppendingPathComponents : (NSArray *)components
642
- {
643
- NSString *result = self;
644
- @autoreleasepool {
645
- for (NSString *component in components) {
646
- result = [result stringByAppendingPathComponent: component];
647
- }
648
- [result retain ];
649
- }
650
- return [result autorelease ];
651
- }
652
- @end
653
-
654
640
@implementation NSTask (sst)
655
641
-(NSString *)sst_command {
656
642
NSMutableString *command = [self .launchPath mutableCopy ];
@@ -803,22 +789,22 @@ void copyAndStripBitcode(NSString *src, NSString *dst)
803
789
}
804
790
805
791
806
- void copyLibraries (NSString *src_dir, NSString * dst_dir,
792
+ void copyLibraries (NSString *dst_dir,
807
793
NSMutableDictionary *libs, bool stripBitcode)
808
794
{
809
795
NSFileManager *fm = NSFileManager .defaultManager ;
810
796
811
797
[fm createDirectoryAtPath: dst_dir withIntermediateDirectories: YES
812
798
attributes: nil error: nil ];
813
799
814
- for (NSString *lib in libs) @autoreleasepool {
815
- NSString *src = [src_dir stringByAppendingPathComponent: lib];
816
- NSString *dst = [ dst_dir stringByAppendingPathComponent: lib ];
817
-
800
+ for (NSString *src in libs) @autoreleasepool {
801
+ NSString *dst = [
802
+ dst_dir stringByAppendingPathComponent: src.lastPathComponent ];
803
+
818
804
// Compare UUIDs of src and dst and don't copy if they're the same.
819
805
// Do not use mod times for this task: the dst copy gets code-signed
820
806
// and bitcode-stripped so it can look newer than it really is.
821
- NSSet *srcUUIDs = libs[lib ];
807
+ NSSet *srcUUIDs = libs[src ];
822
808
NSMutableSet *dstUUIDs = [NSMutableSet set ];
823
809
process (dst, nil , ^(NSUUID *uuid) {
824
810
[dstUUIDs addObject: uuid];
@@ -831,15 +817,14 @@ void copyLibraries(NSString *src_dir, NSString *dst_dir,
831
817
832
818
if ([srcUUIDs isEqualToSet: dstUUIDs]) {
833
819
log_v (" %s is up to date at %s" ,
834
- lib .fileSystemRepresentation , dst.fileSystemRepresentation );
820
+ src .fileSystemRepresentation , dst.fileSystemRepresentation );
835
821
continue ;
836
822
}
837
823
838
824
// Perform the copy.
839
-
840
- log_v (" Copying %s from %s to %s" ,
841
- lib.fileSystemRepresentation ,
842
- src_dir.fileSystemRepresentation ,
825
+
826
+ log_v (" Copying %s to %s" ,
827
+ src.fileSystemRepresentation ,
843
828
dst_dir.fileSystemRepresentation );
844
829
845
830
[fm removeItemAtPath: dst error: nil ]; // fixme report this err?
@@ -899,7 +884,7 @@ int main(int argc, const char *argv[])
899
884
// Copy source.
900
885
// --source-libraries
901
886
// or /path/to/swift-stdlib-tool/../../lib/swift/<--platform>
902
- NSString *src_dir = nil ;
887
+ NSMutableArray < NSString *> *src_dirs = [ NSMutableArray array ] ;
903
888
904
889
// Copy destinations, signed and unsigned.
905
890
// --destination and --unsigned-destination
@@ -939,7 +924,7 @@ int main(int argc, const char *argv[])
939
924
[embedDirs addObject: [NSString stringWithUTF8String: argv[++i]]];
940
925
}
941
926
if (0 == strcmp (argv[i], " --source-libraries" )) {
942
- src_dir = [NSString stringWithUTF8String: argv[++i]];
927
+ [src_dirs addObject: [NSString stringWithUTF8String: argv[++i] ]];
943
928
}
944
929
if (0 == strcmp (argv[i], " --platform" )) {
945
930
platform = [NSString stringWithUTF8String: argv[++i]];
@@ -976,22 +961,36 @@ int main(int argc, const char *argv[])
976
961
}
977
962
978
963
// Fix up src_dir and platform values.
979
- if (!src_dir && !platform) {
980
- // Neither src_dir nor platform is set. Die.
964
+ if (![src_dirs count ] && !platform) {
965
+ // Neither src_dirs nor platform is set. Die.
981
966
fail_usage (" At least one of --source-libraries and --platform "
982
967
" must be set." );
983
968
}
984
- else if (!src_dir) {
985
- // platform is set but src_dir is not.
986
- // Use platform to set src_dir relative to us.
987
- src_dir = [[[self_executable stringByDeletingLastPathComponent ]
988
- stringByDeletingLastPathComponent ]
989
- sst_stringByAppendingPathComponents:
990
- @[ @" lib" , @" swift-5.0" , platform ]];
969
+ else if (![src_dirs count ]) {
970
+ // platform is set but src_dirs is not.
971
+ // Use platform to set src_dirs relative to us.
972
+ NSString *root_path = [[self_executable stringByDeletingLastPathComponent ]
973
+ stringByDeletingLastPathComponent ];
974
+ NSURL *root_url = [[NSURL fileURLWithPath: root_path isDirectory: YES ]
975
+ URLByAppendingPathComponent: @" lib" ];
976
+ NSArray <NSURL *> *URLs = [
977
+ fm contentsOfDirectoryAtURL: root_url
978
+ includingPropertiesForKeys: @[NSURLNameKey , NSURLIsDirectoryKey ]
979
+ options: NSDirectoryEnumerationSkipsHiddenFiles error: nil ];
980
+
981
+ for (NSURL *URL in URLs) {
982
+ if ([URL.lastPathComponent hasPrefix: @" swift-" ]) {
983
+ [src_dirs addObject: [[URL URLByAppendingPathComponent: platform] path ]];
984
+ }
985
+ }
986
+
987
+ if (![src_dirs count ]) {
988
+ fail (" Couldn't discover Swift library directories in: %s" , root_path.fileSystemRepresentation );
989
+ }
991
990
} else if (!platform) {
992
- // src_dir is set but platform is not.
993
- // Pick platform from src_dir's name.
994
- platform = src_dir .lastPathComponent ;
991
+ // src_dirs is set but platform is not.
992
+ // Pick platform from any src_dirs' name.
993
+ platform = src_dirs[ 0 ] .lastPathComponent ;
995
994
}
996
995
997
996
// Add the platform to unsigned_dst_dir if it is not already present.
@@ -1037,10 +1036,13 @@ int main(int argc, const char *argv[])
1037
1036
process (path,
1038
1037
^(NSString *linkedLib) {
1039
1038
@autoreleasepool {
1040
- NSString *linkedSrc =
1041
- [src_dir stringByAppendingPathComponent: linkedLib];
1042
- if ([fm fileExistsAtPath: linkedSrc]) {
1043
- swiftLibs[linkedLib] = [NSMutableSet set ];
1039
+ for (NSString *src_dir in src_dirs) {
1040
+ NSString *linkedSrc =
1041
+ [src_dir stringByAppendingPathComponent: linkedLib];
1042
+ if ([fm fileExistsAtPath: linkedSrc]) {
1043
+ swiftLibs[linkedSrc] = [NSMutableSet set ];
1044
+ break ;
1045
+ }
1044
1046
}
1045
1047
}
1046
1048
},
@@ -1051,24 +1053,26 @@ int main(int argc, const char *argv[])
1051
1053
// Also collect the Swift libraries' UUIDs.
1052
1054
NSMutableArray *worklist = [swiftLibs.allKeys mutableCopy ];
1053
1055
while (worklist.count ) @autoreleasepool {
1054
- NSString *lib = [worklist lastObject ];
1056
+ NSString *path = [worklist lastObject ];
1055
1057
[worklist removeLastObject ];
1056
- NSString *path = [src_dir stringByAppendingPathComponent: lib];
1057
1058
process (path,
1058
1059
^(NSString *linkedLib) {
1059
1060
@autoreleasepool {
1060
- NSString *linkedSrc =
1061
- [src_dir stringByAppendingPathComponent: linkedLib];
1062
- if (!swiftLibs[linkedLib] &&
1063
- [fm fileExistsAtPath: linkedSrc])
1064
- {
1065
- swiftLibs[linkedLib] = [NSMutableSet set ];
1066
- [worklist addObject: linkedLib];
1061
+ for (NSString *src_dir in src_dirs) {
1062
+ NSString *linkedSrc =
1063
+ [src_dir stringByAppendingPathComponent: linkedLib];
1064
+ if (!swiftLibs[linkedSrc] &&
1065
+ [fm fileExistsAtPath: linkedSrc])
1066
+ {
1067
+ swiftLibs[linkedSrc] = [NSMutableSet set ];
1068
+ [worklist addObject: linkedSrc];
1069
+ break ;
1070
+ }
1067
1071
}
1068
1072
}
1069
1073
},
1070
1074
^(NSUUID *uuid) {
1071
- NSMutableSet *uuids = swiftLibs[lib ];
1075
+ NSMutableSet *uuids = swiftLibs[path ];
1072
1076
[uuids addObject: uuid];
1073
1077
});
1074
1078
}
@@ -1078,31 +1082,36 @@ int main(int argc, const char *argv[])
1078
1082
// with --resource-library.
1079
1083
NSMutableDictionary *swiftLibsForResources = [NSMutableDictionary new ];
1080
1084
for (NSString *lib in resourceLibraries) @autoreleasepool {
1081
- NSString *libSrc = [src_dir stringByAppendingPathComponent: lib];
1082
- if ([fm fileExistsAtPath: libSrc]) {
1083
- swiftLibsForResources[lib] = [NSMutableSet set ];
1085
+ for (NSString *src_dir in src_dirs) {
1086
+ NSString *libSrc = [src_dir stringByAppendingPathComponent: lib];
1087
+ if ([fm fileExistsAtPath: libSrc]) {
1088
+ swiftLibsForResources[libSrc] = [NSMutableSet set ];
1089
+ break ;
1090
+ }
1084
1091
}
1085
1092
}
1086
1093
1087
1094
// Collect dependencies of --resource-library libs.
1088
1095
worklist = [swiftLibsForResources.allKeys mutableCopy ];
1089
1096
while (worklist.count ) @autoreleasepool {
1090
- NSString *lib = [worklist lastObject ];
1097
+ NSString *path = [worklist lastObject ];
1091
1098
[worklist removeLastObject ];
1092
- NSString *path = [src_dir stringByAppendingPathComponent: lib];
1093
1099
process (path,
1094
1100
^(NSString *linkedLib) {
1095
- NSString *linkedSrc =
1096
- [src_dir stringByAppendingPathComponent: linkedLib];
1097
- if (!swiftLibsForResources[linkedLib] &&
1098
- [fm fileExistsAtPath: linkedSrc])
1099
- {
1100
- swiftLibsForResources[linkedLib] = [NSMutableSet set ];
1101
- [worklist addObject: linkedLib];
1101
+ for (NSString *src_dir in src_dirs) {
1102
+ NSString *linkedSrc =
1103
+ [src_dir stringByAppendingPathComponent: linkedLib];
1104
+ if (!swiftLibsForResources[linkedSrc] &&
1105
+ [fm fileExistsAtPath: linkedSrc])
1106
+ {
1107
+ swiftLibsForResources[linkedSrc] = [NSMutableSet set ];
1108
+ [worklist addObject: linkedSrc];
1109
+ break ;
1110
+ }
1102
1111
}
1103
1112
},
1104
1113
^(NSUUID *uuid) {
1105
- NSMutableSet *uuids = swiftLibsForResources[lib ];
1114
+ NSMutableSet *uuids = swiftLibsForResources[path ];
1106
1115
[uuids addObject: uuid];
1107
1116
});
1108
1117
}
@@ -1111,26 +1120,25 @@ int main(int argc, const char *argv[])
1111
1120
// Print the Swift libraries (full path to toolchain's copy)
1112
1121
if (print) {
1113
1122
for (NSString *lib in swiftLibs) {
1114
- printf (" %s\n " , [[src_dir stringByAppendingPathComponent: lib]
1115
- fileSystemRepresentation ]);
1123
+ printf (" %s\n " , lib.fileSystemRepresentation );
1116
1124
}
1117
1125
}
1118
1126
1119
1127
// Copy the Swift libraries to $build_dir/$frameworks
1120
1128
// and $build_dir/$unsigned_frameworks
1121
1129
if (copy) {
1122
- copyLibraries (src_dir, dst_dir, swiftLibs, stripBitcode);
1130
+ copyLibraries (dst_dir, swiftLibs, stripBitcode);
1123
1131
if (unsigned_dst_dir) {
1124
1132
// Never strip bitcode from the unsigned libraries.
1125
1133
// Their existing signatures must be preserved.
1126
- copyLibraries (src_dir, unsigned_dst_dir, swiftLibs, false );
1134
+ copyLibraries (unsigned_dst_dir, swiftLibs, false );
1127
1135
}
1128
1136
1129
1137
if (resource_dst_dir) {
1130
1138
// Never strip bitcode from resources libraries, for
1131
1139
// the same reason as the libraries copied to
1132
1140
// unsigned_dst_dir.
1133
- copyLibraries (src_dir, resource_dst_dir, swiftLibsForResources, false );
1141
+ copyLibraries (resource_dst_dir, swiftLibsForResources, false );
1134
1142
}
1135
1143
}
1136
1144
0 commit comments