Skip to content

Commit 3c8883f

Browse files
committed
[Build] Pass -application-extension for non-executable/non-system library targets
<rdar://problem/52282996> (cherry picked from commit ee9d4e6)
1 parent d2114f2 commit 3c8883f

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,14 @@ public class BuildPlan {
10871087
buildProduct.additionalFlags += pkgConfig(for: target).libs
10881088
}
10891089

1090+
// Mark dylibs safe for application extension if they don't depend on any system module.
1091+
if buildParameters.triple.isDarwin(), buildProduct.product.type == .library(.dynamic), dependencies.systemModules.isEmpty {
1092+
// We also ensure the user isn't trying to do anything custom using the -Xlinker style args.
1093+
if buildParameters.linkerFlags.isEmpty {
1094+
buildProduct.additionalFlags += ["-application-extension"]
1095+
}
1096+
}
1097+
10901098
// Link C++ if needed.
10911099
// Note: This will come from build settings in future.
10921100
for target in dependencies.staticTargets {
@@ -1178,23 +1186,30 @@ public class BuildPlan {
11781186

11791187
/// Plan a Clang target.
11801188
private func plan(clangTarget: ClangTargetBuildDescription) {
1189+
var dependsOnAnySystemModules = false
11811190
for dependency in clangTarget.target.recursiveDependencies() {
11821191
switch dependency.underlyingTarget {
11831192
case let target as ClangTarget where target.type == .library:
11841193
// Setup search paths for C dependencies:
11851194
clangTarget.additionalFlags += ["-I", target.includeDir.pathString]
11861195
case let target as SystemLibraryTarget:
1196+
dependsOnAnySystemModules = true
11871197
clangTarget.additionalFlags += ["-fmodule-map-file=\(target.moduleMapPath.pathString)"]
11881198
clangTarget.additionalFlags += pkgConfig(for: target).cFlags
11891199
default: continue
11901200
}
11911201
}
1202+
1203+
if buildParameters.triple.isDarwin() && clangTarget.target.type == .library && !dependsOnAnySystemModules {
1204+
clangTarget.additionalFlags += ["-fapplication-extension"]
1205+
}
11921206
}
11931207

11941208
/// Plan a Swift target.
11951209
private func plan(swiftTarget: SwiftTargetBuildDescription) throws {
11961210
// We need to iterate recursive dependencies because Swift compiler needs to see all the targets a target
11971211
// depends on.
1212+
var dependsOnAnySystemModules = false
11981213
for dependency in swiftTarget.target.recursiveDependencies() {
11991214
switch dependency.underlyingTarget {
12001215
case let underlyingTarget as ClangTarget where underlyingTarget.type == .library:
@@ -1211,11 +1226,16 @@ public class BuildPlan {
12111226
"-I", target.clangTarget.includeDir.pathString,
12121227
]
12131228
case let target as SystemLibraryTarget:
1229+
dependsOnAnySystemModules = true
12141230
swiftTarget.additionalFlags += ["-Xcc", "-fmodule-map-file=\(target.moduleMapPath.pathString)"]
12151231
swiftTarget.additionalFlags += pkgConfig(for: target).cFlags
12161232
default: break
12171233
}
12181234
}
1235+
1236+
if buildParameters.triple.isDarwin() && swiftTarget.target.type == .library && !dependsOnAnySystemModules {
1237+
swiftTarget.additionalFlags += ["-application-extension"]
1238+
}
12191239
}
12201240

12211241
/// Creates arguments required to launch the Swift REPL that will allow

Sources/TestSupport/AssertMatch.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public indirect enum StringPattern {
2121

2222
/// Matches any sequence of zero or more strings, when matched a list of inputs.
2323
case anySequence
24+
case optional(String)
2425

2526
case any
2627
case contains(String)
@@ -49,8 +50,8 @@ extension StringPattern: ExpressibleByStringLiteral {
4950

5051
public func ~=(pattern: StringPattern, value: String) -> Bool {
5152
switch pattern {
52-
// These cases never matches individual items, they are just used for matching string lists.
53-
case .start, .end, .anySequence:
53+
// These cases never matches individual items, they are just used for matching string lists.
54+
case .start, .end, .anySequence, .optional:
5455
return false
5556

5657
case .any:
@@ -95,6 +96,10 @@ public func ~=(patterns: [StringPattern], input: [String]) -> Bool {
9596
case .anySequence:
9697
return matchAny(patterns, input: input)
9798

99+
case .optional(let needle):
100+
let newInput = input.first == needle ? input.dropFirst() : input
101+
return match(patterns, onlyAt: newInput)
102+
98103
default:
99104
if input.isEmpty || !(item ~= input.first!) { return false }
100105
return match(patterns, onlyAt: input.dropFirst())

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ final class BuildPlanTests: XCTestCase {
105105
XCTAssertMatch(exe, ["-swift-version", "4", "-enable-batch-mode", "-Onone", "-g", "-enable-testing", .equal(j), "-DSWIFT_PACKAGE", "-DDEBUG", "-module-cache-path", "/path/to/build/debug/ModuleCache", .anySequence])
106106

107107
let lib = try result.target(for: "lib").swiftTarget().compileArguments()
108-
XCTAssertMatch(lib, ["-swift-version", "4", "-enable-batch-mode", "-Onone", "-g", "-enable-testing", .equal(j), "-DSWIFT_PACKAGE", "-DDEBUG", "-module-cache-path", "/path/to/build/debug/ModuleCache", .anySequence])
108+
XCTAssertMatch(lib, ["-swift-version", "4", "-enable-batch-mode", "-Onone", "-g", "-enable-testing", .equal(j), "-DSWIFT_PACKAGE", "-DDEBUG", .optional("-application-extension"), "-module-cache-path", "/path/to/build/debug/ModuleCache", .anySequence])
109109

110110
#if os(macOS)
111111
let linkArguments = [
@@ -285,35 +285,36 @@ final class BuildPlanTests: XCTestCase {
285285
result.checkTargetsCount(3)
286286

287287
let ext = try result.target(for: "extlib").clangTarget()
288-
var args: [String] = []
288+
var args: [StringPattern] = []
289289

290290
#if os(macOS)
291-
args += ["-fobjc-arc", "-target", defaultTargetTriple]
291+
args += ["-fobjc-arc", "-target", .equal(defaultTargetTriple)]
292292
#else
293-
args += ["-target", defaultTargetTriple]
293+
args += ["-target", .equal(defaultTargetTriple)]
294294
#endif
295295

296296
args += ["-g", "-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1"]
297297
args += ["-fblocks", "-fmodules", "-fmodule-name=extlib",
298-
"-I", "/ExtPkg/Sources/extlib/include", "-fmodules-cache-path=/path/to/build/debug/ModuleCache"]
299-
XCTAssertEqual(ext.basicArguments(), args)
298+
"-I", "/ExtPkg/Sources/extlib/include", .optional("-fapplication-extension"),
299+
"-fmodules-cache-path=/path/to/build/debug/ModuleCache"]
300+
XCTAssertMatch(ext.basicArguments(), args)
300301
XCTAssertEqual(ext.objects, [AbsolutePath("/path/to/build/debug/extlib.build/extlib.c.o")])
301302
XCTAssertEqual(ext.moduleMap, AbsolutePath("/path/to/build/debug/extlib.build/module.modulemap"))
302303

303304
let exe = try result.target(for: "exe").clangTarget()
304305
args = []
305306

306307
#if os(macOS)
307-
args += ["-fobjc-arc", "-target", defaultTargetTriple]
308+
args += ["-fobjc-arc", "-target", .equal(defaultTargetTriple)]
308309
#else
309-
args += ["-target", defaultTargetTriple]
310+
args += ["-target", .equal(defaultTargetTriple)]
310311
#endif
311312

312313
args += ["-g", "-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1"]
313314
args += ["-fblocks", "-fmodules", "-fmodule-name=exe",
314315
"-I", "/Pkg/Sources/exe/include", "-I", "/Pkg/Sources/lib/include", "-I", "/ExtPkg/Sources/extlib/include",
315316
"-fmodules-cache-path=/path/to/build/debug/ModuleCache"]
316-
XCTAssertEqual(exe.basicArguments(), args)
317+
XCTAssertMatch(exe.basicArguments(), args)
317318
XCTAssertEqual(exe.objects, [AbsolutePath("/path/to/build/debug/exe.build/main.c.o")])
318319
XCTAssertEqual(exe.moduleMap, nil)
319320

@@ -432,18 +433,19 @@ final class BuildPlanTests: XCTestCase {
432433
result.checkTargetsCount(2)
433434

434435
let lib = try result.target(for: "lib").clangTarget()
435-
var args: [String] = []
436+
var args: [StringPattern] = []
436437

437438
#if os(macOS)
438-
args += ["-fobjc-arc", "-target", defaultTargetTriple]
439+
args += ["-fobjc-arc", "-target", .equal(defaultTargetTriple)]
439440
#else
440-
args += ["-target", defaultTargetTriple]
441+
args += ["-target", .equal(defaultTargetTriple)]
441442
#endif
442443

443444
args += ["-g", "-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1"]
444445
args += ["-fblocks", "-fmodules", "-fmodule-name=lib", "-I", "/Pkg/Sources/lib/include",
446+
.optional("-fapplication-extension"),
445447
"-fmodules-cache-path=/path/to/build/debug/ModuleCache"]
446-
XCTAssertEqual(lib.basicArguments(), args)
448+
XCTAssertMatch(lib.basicArguments(), args)
447449
XCTAssertEqual(lib.objects, [AbsolutePath("/path/to/build/debug/lib.build/lib.c.o")])
448450
XCTAssertEqual(lib.moduleMap, AbsolutePath("/path/to/build/debug/lib.build/module.modulemap"))
449451

@@ -587,7 +589,7 @@ final class BuildPlanTests: XCTestCase {
587589
#endif
588590

589591
let foo = try result.target(for: "Foo").swiftTarget().compileArguments()
590-
XCTAssertMatch(foo, ["-swift-version", "4", "-enable-batch-mode", "-Onone", "-g", "-enable-testing", .equal(j), "-DSWIFT_PACKAGE", "-DDEBUG", "-module-cache-path", "/path/to/build/debug/ModuleCache", .anySequence])
592+
XCTAssertMatch(foo, ["-swift-version", "4", "-enable-batch-mode", "-Onone", "-g", "-enable-testing", .equal(j), "-DSWIFT_PACKAGE", "-DDEBUG", .optional("-application-extension"), "-module-cache-path", "/path/to/build/debug/ModuleCache", .anySequence])
591593

592594
let fooTests = try result.target(for: "FooTests").swiftTarget().compileArguments()
593595
XCTAssertMatch(fooTests, ["-swift-version", "4", "-enable-batch-mode", "-Onone", "-g", "-enable-testing", .equal(j), "-DSWIFT_PACKAGE", "-DDEBUG", "-module-cache-path", "/path/to/build/debug/ModuleCache", .anySequence])
@@ -748,7 +750,7 @@ final class BuildPlanTests: XCTestCase {
748750
])
749751

750752
XCTAssertEqual(barLinkArgs, [
751-
"/fake/path/to/swiftc", "-g", "-L", "/path/to/build/debug", "-o",
753+
"/fake/path/to/swiftc", "-application-extension", "-g", "-L", "/path/to/build/debug", "-o",
752754
"/path/to/build/debug/libBar-Baz.dylib",
753755
"-module-name", "Bar_Baz", "-emit-library",
754756
"@/path/to/build/debug/Bar-Baz.product/Objects.LinkFileList",
@@ -811,11 +813,11 @@ final class BuildPlanTests: XCTestCase {
811813
XCTAssertMatch(exe, ["-swift-version", "4", "-enable-batch-mode", "-Onone", "-g", "-enable-testing", .equal(j), "-DSWIFT_PACKAGE", "-DDEBUG", "-module-cache-path", "/path/to/build/debug/ModuleCache", .anySequence])
812814

813815
let lib = try result.target(for: "lib").swiftTarget().compileArguments()
814-
XCTAssertMatch(lib, ["-swift-version", "4", "-enable-batch-mode", "-Onone", "-g", "-enable-testing", .equal(j), "-DSWIFT_PACKAGE", "-DDEBUG", "-module-cache-path", "/path/to/build/debug/ModuleCache", .anySequence])
816+
XCTAssertMatch(lib, ["-swift-version", "4", "-enable-batch-mode", "-Onone", "-g", "-enable-testing", .equal(j), "-DSWIFT_PACKAGE", "-DDEBUG", .optional("-application-extension"), "-module-cache-path", "/path/to/build/debug/ModuleCache", .anySequence])
815817

816818
#if os(macOS)
817819
let linkArguments = [
818-
"/fake/path/to/swiftc", "-g", "-L", "/path/to/build/debug",
820+
"/fake/path/to/swiftc", "-application-extension", "-g", "-L", "/path/to/build/debug",
819821
"-o", "/path/to/build/debug/liblib.dylib", "-module-name", "lib",
820822
"-emit-library",
821823
"@/path/to/build/debug/lib.product/Objects.LinkFileList",
@@ -879,15 +881,15 @@ final class BuildPlanTests: XCTestCase {
879881

880882
let lib = try result.target(for: "lib").clangTarget()
881883
#if os(macOS)
882-
XCTAssertEqual(lib.basicArguments(), ["-fobjc-arc", "-target", defaultTargetTriple, "-g", "-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1", "-fblocks", "-fmodules", "-fmodule-name=lib", "-I", "/Pkg/Sources/lib/include", "-fmodules-cache-path=/path/to/build/debug/ModuleCache"])
884+
XCTAssertEqual(lib.basicArguments(), ["-fobjc-arc", "-target", defaultTargetTriple, "-g", "-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1", "-fblocks", "-fmodules", "-fmodule-name=lib", "-I", "/Pkg/Sources/lib/include", "-fapplication-extension", "-fmodules-cache-path=/path/to/build/debug/ModuleCache"])
883885
#else
884886
XCTAssertEqual(lib.basicArguments(), ["-target", defaultTargetTriple, "-g", "-O0", "-DSWIFT_PACKAGE=1", "-DDEBUG=1", "-fblocks", "-fmodules", "-fmodule-name=lib", "-I", "/Pkg/Sources/lib/include", "-fmodules-cache-path=/path/to/build/debug/ModuleCache"])
885887
#endif
886888
XCTAssertEqual(lib.objects, [AbsolutePath("/path/to/build/debug/lib.build/lib.cpp.o")])
887889
XCTAssertEqual(lib.moduleMap, AbsolutePath("/path/to/build/debug/lib.build/module.modulemap"))
888890

889891
#if os(macOS)
890-
XCTAssertEqual(try result.buildProduct(for: "lib").linkArguments(), ["/fake/path/to/swiftc", "-lc++", "-g", "-L", "/path/to/build/debug", "-o", "/path/to/build/debug/liblib.dylib", "-module-name", "lib", "-emit-library", "@/path/to/build/debug/lib.product/Objects.LinkFileList", "-runtime-compatibility-version", "none", "-target", "x86_64-apple-macosx10.10"])
892+
XCTAssertEqual(try result.buildProduct(for: "lib").linkArguments(), ["/fake/path/to/swiftc", "-application-extension", "-lc++", "-g", "-L", "/path/to/build/debug", "-o", "/path/to/build/debug/liblib.dylib", "-module-name", "lib", "-emit-library", "@/path/to/build/debug/lib.product/Objects.LinkFileList", "-runtime-compatibility-version", "none", "-target", "x86_64-apple-macosx10.10"])
891893

892894
XCTAssertEqual(try result.buildProduct(for: "exe").linkArguments(), ["/fake/path/to/swiftc", "-g", "-L", "/path/to/build/debug", "-o", "/path/to/build/debug/exe", "-module-name", "exe", "-emit-executable", "@/path/to/build/debug/exe.product/Objects.LinkFileList", "-runtime-compatibility-version", "none", "-target", "x86_64-apple-macosx10.10"])
893895
#else

0 commit comments

Comments
 (0)