Skip to content
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

Add a command plugin which validates the cmake based build #256

Merged
merged 1 commit into from
Apr 1, 2025
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
7 changes: 7 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ let package = Package(
verb: "run-xcodebuild",
description: "Run xcodebuild from the currently selected Xcode configured to use the just-built build service"
))
),
.plugin(
name: "cmake-smoke-test",
capability: .command(intent: .custom(
verb: "cmake-smoke-test",
description: "Build Swift Build using CMake for validation purposes"
))
)
],
swiftLanguageModes: [.v5, .v6],
Expand Down
191 changes: 191 additions & 0 deletions Plugins/cmake-smoke-test/cmake-smoke-test.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import PackagePlugin
import Foundation

@main
struct CMakeSmokeTest: CommandPlugin {
func performCommand(context: PluginContext, arguments: [String]) async throws {
var args = ArgumentExtractor(arguments)

let hostOS = try OS.host()

guard let cmakePath = args.extractOption(named: "cmake-path").last else { throw Errors.missingRequiredOption("--cmake-path") }
print("using cmake at \(cmakePath)")
let cmakeURL = URL(filePath: cmakePath)
guard let ninjaPath = args.extractOption(named: "ninja-path").last else { throw Errors.missingRequiredOption("--ninja-path") }
print("using ninja at \(ninjaPath)")
let ninjaURL = URL(filePath: ninjaPath)
guard let sysrootPath = args.extractOption(named: "sysroot-path").last else { throw Errors.missingRequiredOption("--sysroot-path") }
print("using sysroot at \(sysrootPath)")

let moduleCachePath = context.pluginWorkDirectoryURL.appending(component: "module-cache").path()

let swiftBuildURL = context.package.directoryURL
let swiftBuildBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-build")
print("swift-build: \(swiftBuildURL.path())")

let swiftToolsSupportCoreURL = try findSiblingRepository("swift-tools-support-core", swiftBuildURL: swiftBuildURL)
let swiftToolsSupportCoreBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-tools-support-core")

let swiftSystemURL = try findSiblingRepository("swift-system", swiftBuildURL: swiftBuildURL)
let swiftSystemBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-system")

let swiftAsn1URL = try findSiblingRepository("swift-asn1", swiftBuildURL: swiftBuildURL)
let swiftAsn1BuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-asn1")

let swiftCryptoURL = try findSiblingRepository("swift-crypto", swiftBuildURL: swiftBuildURL)
let swiftCryptoBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-crypto")

let llbuildURL = try findSiblingRepository("llbuild", swiftBuildURL: swiftBuildURL)
let llbuildBuildURL = context.pluginWorkDirectoryURL.appending(component: "llbuild")

let swiftArgumentParserURL = try findSiblingRepository("swift-argument-parser", swiftBuildURL: swiftBuildURL)
let swiftArgumentParserBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-argument-parser")

let swiftDriverURL = try findSiblingRepository("swift-driver", swiftBuildURL: swiftBuildURL)
let swiftDriverBuildURL = context.pluginWorkDirectoryURL.appending(component: "swift-driver")

for url in [swiftToolsSupportCoreBuildURL, swiftAsn1BuildURL, swiftCryptoBuildURL, swiftSystemBuildURL, llbuildBuildURL, swiftArgumentParserBuildURL, swiftDriverBuildURL, swiftBuildBuildURL] {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
}

let sharedSwiftFlags = [
"-sdk", sysrootPath,
"-module-cache-path", moduleCachePath
]

let cMakeProjectArgs = [
"-DArgumentParser_DIR=\(swiftArgumentParserBuildURL.appending(components: "cmake", "modules").path())",
"-DLLBuild_DIR=\(llbuildBuildURL.appending(components: "cmake", "modules").path())",
"-DTSC_DIR=\(swiftToolsSupportCoreBuildURL.appending(components: "cmake", "modules").path())",
"-DSwiftASN1_DIR=\(swiftAsn1BuildURL.appending(components: "cmake", "modules").path())",
"-DSwiftCrypto_DIR=\(swiftCryptoBuildURL.appending(components: "cmake", "modules").path())",
"-DSwiftDriver_DIR=\(swiftDriverBuildURL.appending(components: "cmake", "modules").path())",
"-DSwiftSystem_DIR=\(swiftSystemBuildURL.appending(components: "cmake", "modules").path())"
]

let sharedCMakeArgs = [
"-G", "Ninja",
"-DCMAKE_MAKE_PROGRAM=\(ninjaPath)",
"-DCMAKE_BUILD_TYPE:=Debug",
"-DCMAKE_Swift_FLAGS='\(sharedSwiftFlags.joined(separator: " "))'"
] + cMakeProjectArgs

print("Building swift-tools-support-core")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftToolsSupportCoreURL.path()], workingDirectory: swiftToolsSupportCoreBuildURL)
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftToolsSupportCoreBuildURL)
print("Built swift-tools-support-core")

if hostOS != .macOS && hostOS != .windows {
print("Building swift-asn1")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftAsn1URL.path()], workingDirectory: swiftAsn1BuildURL)
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftAsn1BuildURL)
print("Built swift-asn1")

print("Building swift-crypto")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftCryptoURL.path()], workingDirectory: swiftCryptoBuildURL)
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftCryptoBuildURL)
print("Built swift-crypto")
}

if hostOS != .macOS {
print("Building swift-system")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftSystemURL.path()], workingDirectory: swiftSystemBuildURL)
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftSystemBuildURL)
print("Built swift-system")
}

print("Building llbuild")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + ["-DLLBUILD_SUPPORT_BINDINGS:=Swift", llbuildURL.path()], workingDirectory: llbuildBuildURL)
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: llbuildBuildURL)
print("Built llbuild")

print("Building swift-argument-parser")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + ["-DBUILD_TESTING=NO", "-DBUILD_EXAMPLES=NO", swiftArgumentParserURL.path()], workingDirectory: swiftArgumentParserBuildURL)
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftArgumentParserBuildURL)
print("Built swift-argument-parser")

print("Building swift-driver")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftDriverURL.path()], workingDirectory: swiftDriverBuildURL)
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftDriverBuildURL)
print("Built swift-driver")

print("Building swift-build in \(swiftBuildBuildURL)")
try await Process.checkNonZeroExit(url: cmakeURL, arguments: sharedCMakeArgs + [swiftBuildURL.path()], workingDirectory: swiftBuildBuildURL)
try await Process.checkNonZeroExit(url: ninjaURL, arguments: [], workingDirectory: swiftBuildBuildURL)
print("Built swift-build")
}

func findSiblingRepository(_ name: String, swiftBuildURL: URL) throws -> URL {
let url = swiftBuildURL.deletingLastPathComponent().appending(component: name)
print("\(name): \(url.path())")
guard FileManager.default.fileExists(atPath: url.path()) else { throw Errors.missingRepository(url.path()) }
return url
}
}

enum Errors: Error {
case processError(terminationReason: Process.TerminationReason, terminationStatus: Int32)
case missingRequiredOption(String)
case missingRepository(String)
case unimplementedForHostOS
}

enum OS {
case macOS
case linux
case windows

static func host() throws -> Self {
#if os(macOS)
return .macOS
#elseif os(Linux)
return .linux
#elseif os(Windows)
return .windows
#else
throw Errors.unimplementedForHostOS
#endif
}
}

extension Process {
func run() async throws {
try await withCheckedThrowingContinuation { continuation in
terminationHandler = { _ in
continuation.resume()
}

do {
try run()
} catch {
terminationHandler = nil
continuation.resume(throwing: error)
}
}
}

static func checkNonZeroExit(url: URL, arguments: [String], workingDirectory: URL, environment: [String: String]? = nil) async throws {
print("\(url.path()) \(arguments.joined(separator: " "))")
let process = Process()
process.executableURL = url
process.arguments = arguments
process.currentDirectoryURL = workingDirectory
process.environment = environment
try await process.run()
if process.terminationStatus != 0 {
throw Errors.processError(terminationReason: process.terminationReason, terminationStatus: process.terminationStatus)
}
}
}
2 changes: 1 addition & 1 deletion Sources/SWBBuildService/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ target_link_libraries(SWBBuildService PUBLIC
SWBUniversalPlatform
SWBWebAssemblyPlatform
SWBWindowsPlatform
SwiftSystem::SystemPackage)
$<$<NOT:$<PLATFORM_ID:Darwin>>:SwiftSystem::SystemPackage>)
2 changes: 1 addition & 1 deletion Sources/SWBCSupport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ target_include_directories(SWBCSupport PUBLIC
${CMAKE_CURRENT_SOURCE_DIR})
# TODO(compnerd) wire this up with `find_package`
target_link_libraries(SWBCSupport PRIVATE
BlocksRuntime)
$<$<NOT:$<PLATFORM_ID:Darwin>>:BlocksRuntime>)
4 changes: 2 additions & 2 deletions Sources/SWBUtil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ target_link_libraries(SWBUtil PUBLIC
SWBCSupport
SWBLibc
ArgumentParser
$<$<NOT:$<PLATFORM_ID:Windows>>:Crypto::Crypto>
SwiftSystem::SystemPackage)
$<$<AND:$<NOT:$<PLATFORM_ID:Windows>>,$<NOT:$<PLATFORM_ID:Darwin>>>:Crypto::Crypto>
$<$<NOT:$<PLATFORM_ID:Darwin>>:SwiftSystem::SystemPackage>)