Search code examples
swiftxcodeswift-package-manager

How to add executable in Xcode that can be run by the Swift Package Manager?


I used SPM to initialize the package and then generated xcodeproj for editing in Xcode. I added an executable in Xcode but SPM isn't picking it up when building. If I run generate-xcodeproj again using SPM, the executable product is gone. I looked into the .build folder and the executable isn't there. Right now this is my Package.swift:

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "Swerver",
    products: [
        .library(
            name: "Swerver",
            targets: ["Swerver"]),
    ],
    dependencies: [
    .package(url: "https://github.com/IBM-Swift/BlueSocket.git", from: "1.0.8"),
    ],
    targets: [
        .target(
            name: "Swerver",
            dependencies: [
                "Socket"
            ],
            path: "Sources/SwerverCore"),
        .testTarget(
            name: "SwerverTests",
            dependencies: ["Swerver"]),
    ]
)

Solution

  • You can't add an executable in Xcode and have it picked up by the Swift Package Manager. You need to add it in the Package.swift file and regenerate the Xcode project.

    Alternatively, add the executable to a new project that references the generated project.