user6451264
user6451264

Reputation:

How to specify the name of the output executable?

By default the SPM builds the executable target with the same name (uppercase) as the module folder containing its main.swift. How do I get it to build the binary with a different file name? I cannot find any instructions on the SPM manual.

Upvotes: 4

Views: 2628

Answers (3)

kevinzhow
kevinzhow

Reputation: 94

Change executable name

In SPM 5.4, You can set executable name in products.

.executable(name: "ExecutableName", targets: ["ExecutableTargetName"])

Build the executable product directly

and if you want to build the executable directly, try command swift build --product ExecutableName

also you can get where the build result located with --show-bin-path parameter

for example swift build --product ExecutableName --show-bin-path

Demo Package.swift

// swift-tools-version:5.4

import PackageDescription

let package = Package(
  name: "PackageName",
  platforms: [
    .iOS(.v11),
    .macOS(.v10_15),
  ],
  products: [
    .library(name: "LibraryName", targets: ["LibraryTarget"]),
    .executable(name: "ExecutableName", targets: ["ExecutableTargetName"])
  ],
  dependencies: [...],
  targets: [
    .target(
      name: "LibraryTarget",
      dependencies: [...],
      path: "Sources/..."
    ),
    .executableTarget(
      name: "ExecutableTargetName",
      dependencies: [...],
      path: "Sources/..."
    ),
  ]
)

Upvotes: 2

Daniel Thompson
Daniel Thompson

Reputation: 603

With Swift 4, given DemoProject created with swift package init --type executable, make the changes to Package.swift (adding the products section).

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    name: "DemoProject",
    products: [
        .executable(name: "demo", targets: ["DemoProject"]),
    ],
    dependencies: [],
    targets: [
        .target(
            name: "DemoProject",
            dependencies: []),
    ]
)

This will create an executable called demo.

Upvotes: 6

Vadim Eisenberg
Vadim Eisenberg

Reputation: 3427

I am in favor of using another build system, for example make, on top of swift build. Currently, swift build does not allow defining custom "hooks" to implement various build automation tasks - generating code, copying resources, deploying the executable, etc. Once you use make you can define whichever tasks you desire, in particular, renaming the binary.

Upvotes: -2

Related Questions