Skip to content

Commit 2f2fc23

Browse files
authored
Merge pull request #39996 from keith/ks/swift-stdlib-tool-add-support-for-back-deployed-concurrency
[swift-stdlib-tool] Add support for back deployed concurrency
2 parents dd984b1 + 615b1bc commit 2f2fc23

File tree

1 file changed

+79
-71
lines changed

1 file changed

+79
-71
lines changed

tools/swift-stdlib-tool/swift-stdlib-tool.mm

Lines changed: 79 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -637,20 +637,6 @@ void process(NSString *path, void(^dylibVisitor)(NSString *),
637637
}
638638

639639

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-
654640
@implementation NSTask (sst)
655641
-(NSString *)sst_command {
656642
NSMutableString *command = [self.launchPath mutableCopy];
@@ -803,22 +789,22 @@ void copyAndStripBitcode(NSString *src, NSString *dst)
803789
}
804790

805791

806-
void copyLibraries(NSString *src_dir, NSString *dst_dir,
792+
void copyLibraries(NSString *dst_dir,
807793
NSMutableDictionary *libs, bool stripBitcode)
808794
{
809795
NSFileManager *fm = NSFileManager.defaultManager;
810796

811797
[fm createDirectoryAtPath: dst_dir withIntermediateDirectories:YES
812798
attributes:nil error:nil];
813799

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+
818804
// Compare UUIDs of src and dst and don't copy if they're the same.
819805
// Do not use mod times for this task: the dst copy gets code-signed
820806
// and bitcode-stripped so it can look newer than it really is.
821-
NSSet *srcUUIDs = libs[lib];
807+
NSSet *srcUUIDs = libs[src];
822808
NSMutableSet *dstUUIDs = [NSMutableSet set];
823809
process(dst, nil, ^(NSUUID *uuid) {
824810
[dstUUIDs addObject:uuid];
@@ -831,15 +817,14 @@ void copyLibraries(NSString *src_dir, NSString *dst_dir,
831817

832818
if ([srcUUIDs isEqualToSet:dstUUIDs]) {
833819
log_v("%s is up to date at %s",
834-
lib.fileSystemRepresentation, dst.fileSystemRepresentation);
820+
src.fileSystemRepresentation, dst.fileSystemRepresentation);
835821
continue;
836822
}
837823

838824
// 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,
843828
dst_dir.fileSystemRepresentation);
844829

845830
[fm removeItemAtPath:dst error:nil]; // fixme report this err?
@@ -899,7 +884,7 @@ int main(int argc, const char *argv[])
899884
// Copy source.
900885
// --source-libraries
901886
// or /path/to/swift-stdlib-tool/../../lib/swift/<--platform>
902-
NSString *src_dir = nil;
887+
NSMutableArray<NSString *> *src_dirs = [NSMutableArray array];
903888

904889
// Copy destinations, signed and unsigned.
905890
// --destination and --unsigned-destination
@@ -939,7 +924,7 @@ int main(int argc, const char *argv[])
939924
[embedDirs addObject:[NSString stringWithUTF8String:argv[++i]]];
940925
}
941926
if (0 == strcmp(argv[i], "--source-libraries")) {
942-
src_dir = [NSString stringWithUTF8String:argv[++i]];
927+
[src_dirs addObject:[NSString stringWithUTF8String:argv[++i]]];
943928
}
944929
if (0 == strcmp(argv[i], "--platform")) {
945930
platform = [NSString stringWithUTF8String:argv[++i]];
@@ -976,22 +961,36 @@ int main(int argc, const char *argv[])
976961
}
977962

978963
// 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.
981966
fail_usage("At least one of --source-libraries and --platform "
982967
"must be set.");
983968
}
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+
}
991990
} 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;
995994
}
996995

997996
// Add the platform to unsigned_dst_dir if it is not already present.
@@ -1037,10 +1036,13 @@ int main(int argc, const char *argv[])
10371036
process(path,
10381037
^(NSString *linkedLib) {
10391038
@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+
}
10441046
}
10451047
}
10461048
},
@@ -1051,24 +1053,26 @@ int main(int argc, const char *argv[])
10511053
// Also collect the Swift libraries' UUIDs.
10521054
NSMutableArray *worklist = [swiftLibs.allKeys mutableCopy];
10531055
while (worklist.count) @autoreleasepool {
1054-
NSString *lib = [worklist lastObject];
1056+
NSString *path = [worklist lastObject];
10551057
[worklist removeLastObject];
1056-
NSString *path = [src_dir stringByAppendingPathComponent:lib];
10571058
process(path,
10581059
^(NSString *linkedLib) {
10591060
@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+
}
10671071
}
10681072
}
10691073
},
10701074
^(NSUUID *uuid) {
1071-
NSMutableSet *uuids = swiftLibs[lib];
1075+
NSMutableSet *uuids = swiftLibs[path];
10721076
[uuids addObject:uuid];
10731077
});
10741078
}
@@ -1078,31 +1082,36 @@ int main(int argc, const char *argv[])
10781082
// with --resource-library.
10791083
NSMutableDictionary *swiftLibsForResources = [NSMutableDictionary new];
10801084
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+
}
10841091
}
10851092
}
10861093

10871094
// Collect dependencies of --resource-library libs.
10881095
worklist = [swiftLibsForResources.allKeys mutableCopy];
10891096
while (worklist.count) @autoreleasepool {
1090-
NSString *lib = [worklist lastObject];
1097+
NSString *path = [worklist lastObject];
10911098
[worklist removeLastObject];
1092-
NSString *path = [src_dir stringByAppendingPathComponent:lib];
10931099
process(path,
10941100
^(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+
}
11021111
}
11031112
},
11041113
^(NSUUID *uuid) {
1105-
NSMutableSet *uuids = swiftLibsForResources[lib];
1114+
NSMutableSet *uuids = swiftLibsForResources[path];
11061115
[uuids addObject:uuid];
11071116
});
11081117
}
@@ -1111,26 +1120,25 @@ int main(int argc, const char *argv[])
11111120
// Print the Swift libraries (full path to toolchain's copy)
11121121
if (print) {
11131122
for (NSString *lib in swiftLibs) {
1114-
printf("%s\n", [[src_dir stringByAppendingPathComponent:lib]
1115-
fileSystemRepresentation]);
1123+
printf("%s\n", lib.fileSystemRepresentation);
11161124
}
11171125
}
11181126

11191127
// Copy the Swift libraries to $build_dir/$frameworks
11201128
// and $build_dir/$unsigned_frameworks
11211129
if (copy) {
1122-
copyLibraries(src_dir, dst_dir, swiftLibs, stripBitcode);
1130+
copyLibraries(dst_dir, swiftLibs, stripBitcode);
11231131
if (unsigned_dst_dir) {
11241132
// Never strip bitcode from the unsigned libraries.
11251133
// Their existing signatures must be preserved.
1126-
copyLibraries(src_dir, unsigned_dst_dir, swiftLibs, false);
1134+
copyLibraries(unsigned_dst_dir, swiftLibs, false);
11271135
}
11281136

11291137
if (resource_dst_dir) {
11301138
// Never strip bitcode from resources libraries, for
11311139
// the same reason as the libraries copied to
11321140
// unsigned_dst_dir.
1133-
copyLibraries(src_dir, resource_dst_dir, swiftLibsForResources, false);
1141+
copyLibraries(resource_dst_dir, swiftLibsForResources, false);
11341142
}
11351143
}
11361144

0 commit comments

Comments
 (0)