Skip to content

Handle swift-sdk search paths #6608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ public final class ClangTargetBuildDescription {
args += ["-flto=thin"]
}

// Pass default include paths from the toolchain.
for includeSearchPath in self.buildParameters.toolchain.includeSearchPaths {
args += ["-I", includeSearchPath.pathString]
}

return args
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
// User arguments (from -Xlinker) should follow generated arguments to allow user overrides
args += self.buildParameters.flags.linkerFlags.asSwiftcLinkerFlags()

// Pass default library paths from the toolchain.
for librarySearchPath in self.buildParameters.toolchain.librarySearchPaths {
args += ["-L", librarySearchPath.pathString]
}

// Add toolchain's libdir at the very end (even after the user -Xlinker arguments).
//
// This will allow linking to libraries shipped in the toolchain.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ public final class SwiftTargetBuildDescription {
args += ["-lto=llvm-thin"]
}

// Pass default include paths from the toolchain.
for includeSearchPath in self.buildParameters.toolchain.includeSearchPaths {
args += ["-I", includeSearchPath.pathString]
}

// suppress warnings if the package is remote
if self.package.isRemote {
args += ["-suppress-warnings"]
Expand Down Expand Up @@ -627,6 +632,11 @@ public final class SwiftTargetBuildDescription {
result += try self.buildSettingsFlags()
result += try self.macroArguments()

// Pass default include paths from the toolchain.
for includeSearchPath in self.buildParameters.toolchain.includeSearchPaths {
result += ["-I", includeSearchPath.pathString]
}

return result
}

Expand Down Expand Up @@ -706,6 +716,11 @@ public final class SwiftTargetBuildDescription {
result += ["-lto=llvm-thin"]
}

// Pass default include paths from the toolchain.
for includeSearchPath in self.buildParameters.toolchain.includeSearchPaths {
result += ["-I", includeSearchPath.pathString]
}

result += try self.macroArguments()
return result
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/PackageModel/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public protocol Toolchain {
/// Path containing the macOS Swift stdlib.
var macosSwiftStdlib: AbsolutePath { get throws }

/// An array of paths to search for headers and modules at compile time.
var includeSearchPaths: [AbsolutePath] { get }

/// An array of paths to search for libraries at link time.
var librarySearchPaths: [AbsolutePath] { get }

/// Path of the `clang` compiler.
func getClangCompiler() throws -> AbsolutePath

Expand Down
10 changes: 10 additions & 0 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public final class UserToolchain: Toolchain {
/// Path of the `swiftc` compiler.
public let swiftCompilerPath: AbsolutePath

/// An array of paths to search for headers and modules at compile time.
public let includeSearchPaths: [AbsolutePath]

/// An array of paths to search for libraries at link time.
public let librarySearchPaths: [AbsolutePath]

/// Additional flags to be passed to the build tools.
public var extraFlags: BuildFlags

Expand Down Expand Up @@ -482,6 +488,7 @@ public final class UserToolchain: Toolchain {
}

self.triple = triple

self.extraFlags = BuildFlags(
cCompilerFlags: destination.toolset.knownTools[.cCompiler]?.extraCLIOptions ?? [],
cxxCompilerFlags: destination.toolset.knownTools[.cxxCompiler]?.extraCLIOptions ?? [],
Expand All @@ -492,6 +499,9 @@ public final class UserToolchain: Toolchain {
linkerFlags: destination.toolset.knownTools[.linker]?.extraCLIOptions ?? [],
xcbuildFlags: destination.toolset.knownTools[.xcbuild]?.extraCLIOptions ?? [])

self.includeSearchPaths = destination.pathsConfiguration.includeSearchPaths ?? []
self.librarySearchPaths = destination.pathsConfiguration.includeSearchPaths ?? []

self.librarianPath = try UserToolchain.determineLibrarian(
triple: triple,
binDirectories: destination.toolset.rootPaths,
Expand Down
63 changes: 59 additions & 4 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3554,10 +3554,7 @@ final class BuildPlanTests: XCTestCase {
runTimeTriple: runtimeTriple,
properties: .init(
sdkRootPath: "/fake/sdk",
swiftStaticResourcesPath: "/usr/lib/swift_static/none",
includeSearchPaths: ["/usr/lib/swift_static/none"],
librarySearchPaths: ["/usr/lib/swift_static/none"],
toolsetPaths: []),
swiftStaticResourcesPath: "/usr/lib/swift_static/none"),
toolset: toolSet,
destinationDirectory: nil)
let toolchain = try UserToolchain(destination: destination)
Expand Down Expand Up @@ -3669,6 +3666,64 @@ final class BuildPlanTests: XCTestCase {
XCTAssertCount(1, exeLinkArguments, cliFlag(tool: .linker))
}

func testUserToolchainWithSDKSearchPaths() throws {
let fileSystem = InMemoryFileSystem(emptyFiles:
"/Pkg/Sources/exe/main.swift",
"/Pkg/Sources/cLib/cLib.c",
"/Pkg/Sources/cLib/include/cLib.h"
)

let observability = ObservabilitySystem.makeForTesting()
let graph = try loadPackageGraph(
fileSystem: fileSystem,
manifests: [
Manifest.createRootManifest(
displayName: "Pkg",
path: "/Pkg",
targets: [
TargetDescription(name: "exe", dependencies: ["cLib"]),
TargetDescription(name: "cLib", dependencies: []),
]),
],
observabilityScope: observability.topScope
)
XCTAssertNoDiagnostics(observability.diagnostics)

let runtimeTriple = try UserToolchain.default.triple
let sdkIncludeSearchPath = "/usr/lib/swift_static/none/include"
let sdkLibrarySearchPath = "/usr/lib/swift_static/none/lib"
let destination = try Destination(
runTimeTriple: runtimeTriple,
properties: .init(
sdkRootPath: "/fake/sdk",
includeSearchPaths: [sdkIncludeSearchPath],
librarySearchPaths: [sdkLibrarySearchPath]))
let toolchain = try UserToolchain(destination: destination)
let buildParameters = mockBuildParameters(toolchain: toolchain)
let result = try BuildPlanResult(plan: BuildPlan(
buildParameters: buildParameters,
graph: graph,
fileSystem: fileSystem,
observabilityScope: observability.topScope))
result.checkProductsCount(1)
result.checkTargetsCount(2)

// Compile C Target
let cLibCompileArguments = try result.target(for: "cLib").clangTarget().basicArguments(isCXX: false)
let cLibCompileArgumentsPattern: [StringPattern] = ["-I", "\(sdkIncludeSearchPath)"]
XCTAssertMatch(cLibCompileArguments, cLibCompileArgumentsPattern)

// Compile Swift Target
let exeCompileArguments = try result.target(for: "exe").swiftTarget().compileArguments()
let exeCompileArgumentsPattern: [StringPattern] = ["-I", "\(sdkIncludeSearchPath)"]
XCTAssertMatch(exeCompileArguments, exeCompileArgumentsPattern)

// Link Product
let exeLinkArguments = try result.buildProduct(for: "exe").linkArguments()
let exeLinkArgumentsPattern: [StringPattern] = ["-L", "\(sdkIncludeSearchPath)"]
XCTAssertMatch(exeLinkArguments, exeLinkArgumentsPattern)
}

func testExecBuildTimeDependency() throws {
let PkgA = AbsolutePath("/PkgA")

Expand Down
2 changes: 2 additions & 0 deletions Tests/BuildTests/MockBuildTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct MockToolchain: PackageModel.Toolchain {
let librarianPath = AbsolutePath("/fake/path/to/ar")
#endif
let swiftCompilerPath = AbsolutePath("/fake/path/to/swiftc")
let includeSearchPaths = [AbsolutePath]()
let librarySearchPaths = [AbsolutePath]()
let isSwiftDevelopmentToolchain = false
let swiftPluginServerPath: AbsolutePath? = nil
let extraFlags = PackageModel.BuildFlags()
Expand Down