Skip to content

Commit f890e75

Browse files
committed
[swift-stdlib-tool] Add support for back deployed concurrency
This tool was mostly already compatible with copying `libswift_Concurrency.dylib` based on whether or not the executables it was scanning linked it. The only thing missing was that it can now end up copying from different source directories, the older `swift-5.0` directory, and the new `swift-5.5` directory. This change allows users to pass `--source-libraries` multiple times (or picks a default with both) in order to have it scan both directories for any libraries the app links. The biggest part of this change is instead of storing just the library name throughout this logic we now store the actual discovered path, so that we don't have to loop through all potential source directories each time we need to find the library, only the first time.
1 parent 3290833 commit f890e75

File tree

1 file changed

+67
-57
lines changed

1 file changed

+67
-57
lines changed

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

+67-57
Original file line numberDiff line numberDiff line change
@@ -803,22 +803,22 @@ void copyAndStripBitcode(NSString *src, NSString *dst)
803803
}
804804

805805

806-
void copyLibraries(NSString *src_dir, NSString *dst_dir,
806+
void copyLibraries(NSString *dst_dir,
807807
NSMutableDictionary *libs, bool stripBitcode)
808808
{
809809
NSFileManager *fm = NSFileManager.defaultManager;
810810

811811
[fm createDirectoryAtPath: dst_dir withIntermediateDirectories:YES
812812
attributes:nil error:nil];
813813

814-
for (NSString *lib in libs) @autoreleasepool {
815-
NSString *src = [src_dir stringByAppendingPathComponent:lib];
816-
NSString *dst = [dst_dir stringByAppendingPathComponent:lib];
817-
814+
for (NSString *src in libs) @autoreleasepool {
815+
NSString *dst = [
816+
dst_dir stringByAppendingPathComponent:src.lastPathComponent];
817+
818818
// Compare UUIDs of src and dst and don't copy if they're the same.
819819
// Do not use mod times for this task: the dst copy gets code-signed
820820
// and bitcode-stripped so it can look newer than it really is.
821-
NSSet *srcUUIDs = libs[lib];
821+
NSSet *srcUUIDs = libs[src];
822822
NSMutableSet *dstUUIDs = [NSMutableSet set];
823823
process(dst, nil, ^(NSUUID *uuid) {
824824
[dstUUIDs addObject:uuid];
@@ -831,15 +831,14 @@ void copyLibraries(NSString *src_dir, NSString *dst_dir,
831831

832832
if ([srcUUIDs isEqualToSet:dstUUIDs]) {
833833
log_v("%s is up to date at %s",
834-
lib.fileSystemRepresentation, dst.fileSystemRepresentation);
834+
src.fileSystemRepresentation, dst.fileSystemRepresentation);
835835
continue;
836836
}
837837

838838
// Perform the copy.
839-
840-
log_v("Copying %s from %s to %s",
841-
lib.fileSystemRepresentation,
842-
src_dir.fileSystemRepresentation,
839+
840+
log_v("Copying %s to %s",
841+
src.fileSystemRepresentation,
843842
dst_dir.fileSystemRepresentation);
844843

845844
[fm removeItemAtPath:dst error:nil]; // fixme report this err?
@@ -899,7 +898,7 @@ int main(int argc, const char *argv[])
899898
// Copy source.
900899
// --source-libraries
901900
// or /path/to/swift-stdlib-tool/../../lib/swift/<--platform>
902-
NSString *src_dir = nil;
901+
NSMutableArray<NSString *> *src_dirs = [NSMutableArray array];
903902

904903
// Copy destinations, signed and unsigned.
905904
// --destination and --unsigned-destination
@@ -939,7 +938,7 @@ int main(int argc, const char *argv[])
939938
[embedDirs addObject:[NSString stringWithUTF8String:argv[++i]]];
940939
}
941940
if (0 == strcmp(argv[i], "--source-libraries")) {
942-
src_dir = [NSString stringWithUTF8String:argv[++i]];
941+
[src_dirs addObject:[NSString stringWithUTF8String:argv[++i]]];
943942
}
944943
if (0 == strcmp(argv[i], "--platform")) {
945944
platform = [NSString stringWithUTF8String:argv[++i]];
@@ -976,22 +975,24 @@ int main(int argc, const char *argv[])
976975
}
977976

978977
// Fix up src_dir and platform values.
979-
if (!src_dir && !platform) {
980-
// Neither src_dir nor platform is set. Die.
978+
if (![src_dirs count] && !platform) {
979+
// Neither src_dirs nor platform is set. Die.
981980
fail_usage("At least one of --source-libraries and --platform "
982981
"must be set.");
983982
}
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 ]];
983+
else if (![src_dirs count]) {
984+
// platform is set but src_dirs is not.
985+
// Use platform to set src_dirs relative to us.
986+
NSString *root_path = [[self_executable stringByDeletingLastPathComponent]
987+
stringByDeletingLastPathComponent];
988+
src_dirs = [@[
989+
[root_path sst_stringByAppendingPathComponents: @[ @"lib", @"swift-5.0", platform ]],
990+
[root_path sst_stringByAppendingPathComponents: @[ @"lib", @"swift-5.5", platform ]],
991+
] mutableCopy];
991992
} 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;
993+
// src_dirs is set but platform is not.
994+
// Pick platform from any src_dirs' name.
995+
platform = src_dirs[0].lastPathComponent;
995996
}
996997

997998
// Add the platform to unsigned_dst_dir if it is not already present.
@@ -1037,10 +1038,13 @@ int main(int argc, const char *argv[])
10371038
process(path,
10381039
^(NSString *linkedLib) {
10391040
@autoreleasepool {
1040-
NSString *linkedSrc =
1041-
[src_dir stringByAppendingPathComponent:linkedLib];
1042-
if ([fm fileExistsAtPath:linkedSrc]) {
1043-
swiftLibs[linkedLib] = [NSMutableSet set];
1041+
for (NSString *src_dir in src_dirs) {
1042+
NSString *linkedSrc =
1043+
[src_dir stringByAppendingPathComponent:linkedLib];
1044+
if ([fm fileExistsAtPath:linkedSrc]) {
1045+
swiftLibs[linkedSrc] = [NSMutableSet set];
1046+
break;
1047+
}
10441048
}
10451049
}
10461050
},
@@ -1051,24 +1055,26 @@ int main(int argc, const char *argv[])
10511055
// Also collect the Swift libraries' UUIDs.
10521056
NSMutableArray *worklist = [swiftLibs.allKeys mutableCopy];
10531057
while (worklist.count) @autoreleasepool {
1054-
NSString *lib = [worklist lastObject];
1058+
NSString *path = [worklist lastObject];
10551059
[worklist removeLastObject];
1056-
NSString *path = [src_dir stringByAppendingPathComponent:lib];
10571060
process(path,
10581061
^(NSString *linkedLib) {
10591062
@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];
1063+
for (NSString *src_dir in src_dirs) {
1064+
NSString *linkedSrc =
1065+
[src_dir stringByAppendingPathComponent:linkedLib];
1066+
if (!swiftLibs[linkedSrc] &&
1067+
[fm fileExistsAtPath:linkedSrc])
1068+
{
1069+
swiftLibs[linkedSrc] = [NSMutableSet set];
1070+
[worklist addObject:linkedSrc];
1071+
break;
1072+
}
10671073
}
10681074
}
10691075
},
10701076
^(NSUUID *uuid) {
1071-
NSMutableSet *uuids = swiftLibs[lib];
1077+
NSMutableSet *uuids = swiftLibs[path];
10721078
[uuids addObject:uuid];
10731079
});
10741080
}
@@ -1078,31 +1084,36 @@ int main(int argc, const char *argv[])
10781084
// with --resource-library.
10791085
NSMutableDictionary *swiftLibsForResources = [NSMutableDictionary new];
10801086
for (NSString *lib in resourceLibraries) @autoreleasepool {
1081-
NSString *libSrc = [src_dir stringByAppendingPathComponent:lib];
1082-
if ([fm fileExistsAtPath:libSrc]) {
1083-
swiftLibsForResources[lib] = [NSMutableSet set];
1087+
for (NSString *src_dir in src_dirs) {
1088+
NSString *libSrc = [src_dir stringByAppendingPathComponent:lib];
1089+
if ([fm fileExistsAtPath:libSrc]) {
1090+
swiftLibsForResources[libSrc] = [NSMutableSet set];
1091+
break;
1092+
}
10841093
}
10851094
}
10861095

10871096
// Collect dependencies of --resource-library libs.
10881097
worklist = [swiftLibsForResources.allKeys mutableCopy];
10891098
while (worklist.count) @autoreleasepool {
1090-
NSString *lib = [worklist lastObject];
1099+
NSString *path = [worklist lastObject];
10911100
[worklist removeLastObject];
1092-
NSString *path = [src_dir stringByAppendingPathComponent:lib];
10931101
process(path,
10941102
^(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];
1103+
for (NSString *src_dir in src_dirs) {
1104+
NSString *linkedSrc =
1105+
[src_dir stringByAppendingPathComponent:linkedLib];
1106+
if (!swiftLibsForResources[linkedSrc] &&
1107+
[fm fileExistsAtPath:linkedSrc])
1108+
{
1109+
swiftLibsForResources[linkedSrc] = [NSMutableSet set];
1110+
[worklist addObject:linkedSrc];
1111+
break;
1112+
}
11021113
}
11031114
},
11041115
^(NSUUID *uuid) {
1105-
NSMutableSet *uuids = swiftLibsForResources[lib];
1116+
NSMutableSet *uuids = swiftLibsForResources[path];
11061117
[uuids addObject:uuid];
11071118
});
11081119
}
@@ -1111,26 +1122,25 @@ int main(int argc, const char *argv[])
11111122
// Print the Swift libraries (full path to toolchain's copy)
11121123
if (print) {
11131124
for (NSString *lib in swiftLibs) {
1114-
printf("%s\n", [[src_dir stringByAppendingPathComponent:lib]
1115-
fileSystemRepresentation]);
1125+
printf("%s\n", lib.fileSystemRepresentation);
11161126
}
11171127
}
11181128

11191129
// Copy the Swift libraries to $build_dir/$frameworks
11201130
// and $build_dir/$unsigned_frameworks
11211131
if (copy) {
1122-
copyLibraries(src_dir, dst_dir, swiftLibs, stripBitcode);
1132+
copyLibraries(dst_dir, swiftLibs, stripBitcode);
11231133
if (unsigned_dst_dir) {
11241134
// Never strip bitcode from the unsigned libraries.
11251135
// Their existing signatures must be preserved.
1126-
copyLibraries(src_dir, unsigned_dst_dir, swiftLibs, false);
1136+
copyLibraries(unsigned_dst_dir, swiftLibs, false);
11271137
}
11281138

11291139
if (resource_dst_dir) {
11301140
// Never strip bitcode from resources libraries, for
11311141
// the same reason as the libraries copied to
11321142
// unsigned_dst_dir.
1133-
copyLibraries(src_dir, resource_dst_dir, swiftLibsForResources, false);
1143+
copyLibraries(resource_dst_dir, swiftLibsForResources, false);
11341144
}
11351145
}
11361146

0 commit comments

Comments
 (0)