Skip to content

Commit 540ddcc

Browse files
committed
Handle swift-sdk search paths
- Fixes a bug where includeSearchPaths and librarySearchPaths defined in a swift-sdk.json file were not properly respected. Updates UserToolchain to support these by converting them to extra flags. Ideally these flags would be handled with discrete properties instead of being bundled into extra flags.
1 parent 706d1f1 commit 540ddcc

File tree

2 files changed

+100
-10
lines changed

2 files changed

+100
-10
lines changed

Sources/PackageModel/UserToolchain.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,8 @@ public final class UserToolchain: Toolchain {
482482
}
483483

484484
self.triple = triple
485+
486+
// Extract extra flags starting with flags from the destination's toolset
485487
self.extraFlags = BuildFlags(
486488
cCompilerFlags: destination.toolset.knownTools[.cCompiler]?.extraCLIOptions ?? [],
487489
cxxCompilerFlags: destination.toolset.knownTools[.cxxCompiler]?.extraCLIOptions ?? [],
@@ -492,6 +494,19 @@ public final class UserToolchain: Toolchain {
492494
linkerFlags: destination.toolset.knownTools[.linker]?.extraCLIOptions ?? [],
493495
xcbuildFlags: destination.toolset.knownTools[.xcbuild]?.extraCLIOptions ?? [])
494496

497+
// Extract the destination's include search paths into c, cxx, and swift
498+
// compiler flags.
499+
for path in destination.pathsConfiguration.includeSearchPaths ?? [] {
500+
self.extraFlags.cCompilerFlags.append("-I\(path)")
501+
self.extraFlags.cxxCompilerFlags.append("-I\(path)")
502+
self.extraFlags.swiftCompilerFlags.append("-I\(path)")
503+
}
504+
505+
// Extract the destination's library search paths into linker flags.
506+
for path in destination.pathsConfiguration.librarySearchPaths ?? [] {
507+
self.extraFlags.linkerFlags.append("-L\(path)")
508+
}
509+
495510
self.librarianPath = try UserToolchain.determineLibrarian(
496511
triple: triple,
497512
binDirectories: destination.toolset.rootPaths,

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,13 +3547,15 @@ final class BuildPlanTests: XCTestCase {
35473547
],
35483548
rootPaths: try UserToolchain.default.destination.toolset.rootPaths)
35493549
let runtimeTriple = try Triple("armv7em-unknown-none-macho")
3550+
let sdkIncludeSearchPath = "/usr/lib/swift_static/none/include"
3551+
let sdkLibrarySearchPath = "/usr/lib/swift_static/none/lib"
35503552
let destination = try Destination(
35513553
runTimeTriple: runtimeTriple,
35523554
properties: .init(
35533555
sdkRootPath: "/fake/sdk",
35543556
swiftStaticResourcesPath: "/usr/lib/swift_static/none",
3555-
includeSearchPaths: ["/usr/lib/swift_static/none"],
3556-
librarySearchPaths: ["/usr/lib/swift_static/none"],
3557+
includeSearchPaths: [sdkIncludeSearchPath],
3558+
librarySearchPaths: [sdkLibrarySearchPath],
35573559
toolsetPaths: []),
35583560
toolset: toolSet,
35593561
destinationDirectory: nil)
@@ -3596,7 +3598,9 @@ final class BuildPlanTests: XCTestCase {
35963598
// Compile C Target
35973599
let cLibCompileArguments = try result.target(for: "cLib").clangTarget().basicArguments(isCXX: false)
35983600
let cLibCompileArgumentsPattern: [StringPattern] = [
3599-
jsonFlag(tool: .cCompiler), cliFlag(tool: .cCompiler),
3601+
jsonFlag(tool: .cCompiler),
3602+
.equal("-I\(sdkIncludeSearchPath)"),
3603+
cliFlag(tool: .cCompiler),
36003604
]
36013605
XCTAssertMatch(cLibCompileArguments, cLibCompileArgumentsPattern)
36023606
XCTAssertCount(0, cLibCompileArguments, jsonFlag(tool: .swiftCompiler))
@@ -3611,9 +3615,13 @@ final class BuildPlanTests: XCTestCase {
36113615
// Compile Cxx Target
36123616
let cxxLibCompileArguments = try result.target(for: "cxxLib").clangTarget().basicArguments(isCXX: true)
36133617
let cxxLibCompileArgumentsPattern: [StringPattern] = [
3614-
jsonFlag(tool: .cCompiler), cliFlag(tool: .cCompiler),
3618+
jsonFlag(tool: .cCompiler),
3619+
.equal("-I\(sdkIncludeSearchPath)"),
3620+
cliFlag(tool: .cCompiler),
36153621
.anySequence,
3616-
jsonFlag(tool: .cxxCompiler), cliFlag(tool: .cxxCompiler)
3622+
jsonFlag(tool: .cxxCompiler),
3623+
.equal("-I\(sdkIncludeSearchPath)"),
3624+
cliFlag(tool: .cxxCompiler)
36173625
]
36183626
XCTAssertMatch(cxxLibCompileArguments, cxxLibCompileArgumentsPattern)
36193627
XCTAssertCount(0, cxxLibCompileArguments, jsonFlag(tool: .swiftCompiler))
@@ -3628,13 +3636,19 @@ final class BuildPlanTests: XCTestCase {
36283636
// Compile Swift Target
36293637
let exeCompileArguments = try result.target(for: "exe").swiftTarget().compileArguments()
36303638
let exeCompileArgumentsPattern: [StringPattern] = [
3631-
jsonFlag(tool: .swiftCompiler), cliFlag(tool: .swiftCompiler),
3639+
jsonFlag(tool: .swiftCompiler),
3640+
.equal("-I\(sdkIncludeSearchPath)"),
3641+
cliFlag(tool: .swiftCompiler),
36323642
.anySequence,
3633-
"-Xcc", jsonFlag(tool: .cCompiler), "-Xcc", cliFlag(tool: .cCompiler),
3643+
"-Xcc", jsonFlag(tool: .cCompiler),
3644+
"-Xcc", .equal("-I\(sdkIncludeSearchPath)"),
3645+
"-Xcc", cliFlag(tool: .cCompiler),
36343646
// TODO: Pass -Xcxx flags to swiftc (#6491)
36353647
// Uncomment when downstream support arrives.
36363648
// .anySequence,
3637-
// "-Xcxx", jsonFlag(tool: .cxxCompiler), "-Xcxx", cliFlag(tool: .cxxCompiler),
3649+
// "-Xcxx", jsonFlag(tool: .cxxCompiler),
3650+
// "-Xcc", .equal("-I\(sdkIncludeSearchPath)"),
3651+
// "-Xcxx", cliFlag(tool: .cxxCompiler),
36383652
]
36393653
XCTAssertMatch(exeCompileArguments, exeCompileArgumentsPattern)
36403654
XCTAssertCount(1, exeCompileArguments, jsonFlag(tool: .swiftCompiler))
@@ -3651,9 +3665,13 @@ final class BuildPlanTests: XCTestCase {
36513665
// Link Product
36523666
let exeLinkArguments = try result.buildProduct(for: "exe").linkArguments()
36533667
let exeLinkArgumentsPattern: [StringPattern] = [
3654-
jsonFlag(tool: .swiftCompiler), cliFlag(tool: .swiftCompiler),
3668+
jsonFlag(tool: .swiftCompiler),
3669+
.equal("-I\(sdkIncludeSearchPath)"),
3670+
cliFlag(tool: .swiftCompiler),
36553671
.anySequence,
3656-
"-Xlinker", jsonFlag(tool: .linker), "-Xlinker", cliFlag(tool: .linker),
3672+
"-Xlinker", jsonFlag(tool: .linker),
3673+
.equal("-L\(sdkLibrarySearchPath)"),
3674+
"-Xlinker", cliFlag(tool: .linker),
36573675
]
36583676
XCTAssertMatch(exeLinkArguments, exeLinkArgumentsPattern)
36593677
XCTAssertCount(1, exeLinkArguments, jsonFlag(tool: .swiftCompiler))
@@ -4691,3 +4709,60 @@ final class BuildPlanTests: XCTestCase {
46914709
XCTAssertMatch(try result.buildProduct(for: "exe").linkArguments(), ["-sanitize=\(expectedName)"])
46924710
}
46934711
}
4712+
4713+
extension TSCTestSupport.StringPattern: CustomStringConvertible {
4714+
public var description: String {
4715+
switch self {
4716+
case .start:
4717+
return "^"
4718+
case .end:
4719+
return "$"
4720+
case .anySequence:
4721+
return "(.*)*"
4722+
case .any:
4723+
return ".*"
4724+
case .contains(let literal):
4725+
return ".*\"\(literal)\".*"
4726+
case .equal(let literal):
4727+
return "\"\(literal)\""
4728+
case .regex(let literal):
4729+
return "\"\(literal)\""
4730+
case .prefix(let literal):
4731+
return "\"\(literal)\".*"
4732+
case .suffix(let literal):
4733+
return ".*\"\(literal)\""
4734+
case .and(let rhs, let lhs):
4735+
return "\(rhs)&\(lhs)"
4736+
case .or(let rhs, let lhs):
4737+
return "\(rhs)|\(lhs)"
4738+
}
4739+
}
4740+
}
4741+
4742+
struct UIServiceClient {
4743+
func connect() async throws { }
4744+
func disconnect() async throws { }
4745+
}
4746+
func foo() {
4747+
var _client: UIServiceClient?
4748+
func withUIServiceClient<T>(body: (UIServiceClient) async throws -> T) async throws -> T {
4749+
let client: UIServiceClient
4750+
if let _client = _client {
4751+
client = _client
4752+
} else {
4753+
do {
4754+
client = UIServiceClient()
4755+
try await client.connect()
4756+
} catch {
4757+
throw error
4758+
}
4759+
}
4760+
do {
4761+
return try await body(client)
4762+
} catch {
4763+
try? await client.disconnect()
4764+
_client = nil
4765+
throw error
4766+
}
4767+
}
4768+
}

0 commit comments

Comments
 (0)