Subscribed unsubscribe Subscribe Subscribe

JPMartha's Pancake

This blog is the way to brush up my poor English.

#swiftlang try! Swift Build ๐Ÿ’ฅ

Updated: 2016-03-27

Latest Development Snapshots had updated, and I tried using the swift build command. ๐Ÿ’ช

ใฟใชใ•ใพใ„ใ‹ใŒใŠ้Žใ”ใ—ใงใ—ใ‚‡ใ†ใ‹ใ€‚Latest Development Snapshots ใŒๆ›ดๆ–ฐใ•ใ‚ŒใŸใฎใง swift build ใ‚ณใƒžใƒณใƒ‰ใง้Šใ‚“ใงใฟใพใ—ใŸใ€‚๐Ÿ’ƒ

Summary

  • Created files and directories by running the swift build command:
    • executable (Pokemon)
      • .gitignore
      • Sources/
      • Sources/main.swift
      • Tests/
    • library (Bakemon)
      • Package.swift
      • .gitignore
      • Sources/
      • Sources/Bakemon.swift
      • Tests/
      • Tests/LinuxMain.swift
      • Tests/Bakemon/
      • Tests/Bakemon/Bakemon.swift
  • To generate an Xcode project (Getemon), locate the following:
    • Package.swift
    • Some sources

try! Swift Build

Getting Started

First, download and install the following:

If an error is occurred, see the following:

To use from the terminal with the following command:

$ export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:"${PATH}"

Verifying the version of Swift

$ swift --version

Apple Swift version 3.0-dev (LLVM 699a786c15, Clang 77080f2c03, Swift d22638766e)
Target: x86_64-apple-macosx10.9

Creating a package

Create an empty manifest using:

$ touch Package.swift

What is the touch?

$ touch --help

touch: illegal option -- -
usage:
touch [-A [-][[hh]mm]SS] [-acfhm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...

swift build usage

$ swift build --help

OVERVIEW: Build sources into binary products

USAGE: swift build [options]

MODES:
  --configuration <value>        Build with configuration (debug|release) [-c]
  --clean[=<mode>]               Delete artefacts (build|dist) [-k]
  --init <mode>                  Creates a new Swift package (executable|library)
  --fetch                        Fetch package dependencies
  --generate-xcodeproj [<path>]  Generates an Xcode project for this package [-X]

OPTIONS:
  --chdir <value>    Change working directory before any other operation [-C]
  -v[v]              Increase verbosity of informational output
  -Xcc <flag>        Pass flag through to all C compiler instantiations
  -Xlinker <flag>    Pass flag through to all linker instantiations
  -Xswiftc <flag>    Pass flag through to all Swift compiler instantiations

Try creating a new Swift package with Package.swift

๐Ÿšจ Package.swift is not necessary. ๐Ÿ˜ข

$ swift build --init library

Creating Library package: MyPackage
error: a manifest file already exists in this directory

I removed the Package.swift. ๐Ÿ‘‹

$ rm Package.swift

Creating a new Swift package (executable)

$ mkdir ./Pokemon
$ cd ./Pokemon
$ swift build --init executable

Creating Executable package: Pokemon
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
Creating Tests/

The result with -v is the same.

Package.swift

import PackageDescription

let package = Package(
    name: "Pokemon"
)

.gitignore

.DS_Store
/.build
/Packages
/*.xcodeproj

Sources/main.swift

print("Hello, world!")

Building

$ swift build -v

/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/bin/swiftc --driver-mode=swift -I /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/lib/swift/pm -L /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/lib/swift/pm -lPackageDescription -target x86_64-apple-macosx10.10 /.../Pokemon/Package.swift -fileno 3
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/bin/swift-build-tool -f /.../Pokemon/.build/debug.yaml default -v
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/bin/swiftc -module-name Pokemon -incremental -emit-dependencies -emit-module -emit-module-path /.../Pokemon/.build/debug/Pokemon.swiftmodule -output-file-map /.../Pokemon/.build/debug/Pokemon.build/output-file-map.json -c /.../Pokemon/Sources/main.swift -I /.../Pokemon/.build/debug -j8 -Onone -g -D SWIFT_PACKAGE -enable-testing -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/bin/swiftc -emit-executable -g -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -L/.../Pokemon/.build/debug -o /.../Pokemon/.build/debug/Pokemon /.../Pokemon/.build/debug/Pokemon.build/main.swift.o

Creating a new Swift package (library)

$ mkdir ./Bakemon
$ cd ./Bakemon
$ swift build --init library

Creating Library package: Bakemon
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/Bakemon.swift
Creating Tests/
Creating Tests/LinuxMain.swift
Creating Tests/Bakemon/
Creating Tests/Bakemon/Bakemon.swift

The result with -v is the same.

Package.swift

import PackageDescription

let package = Package(
    name: "Bakemon"
)

.gitignore

.DS_Store
/.build
/Packages
/*.xcodeproj

Sources/Bakemon.swift

struct Bakemon {

}

Tests/LinuxMain.swift

import XCTest
@testable import Bakemontest

XCTMain([
    Bakemon(),
])

Tests/Bakemon/Bakemon.swift

import XCTest
@testable import Bakemon

class Bakemon: XCTestCase {

    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    }

}

#if os(Linux)
extension Bakemon: XCTestCaseProvider {
    var allTests : [(String, () throws -> Void)] {
        return [
            ("testExample", testExample),
        ]
    }
}
#endif

Building

$ swift build -v

/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/bin/swiftc --driver-mode=swift -I /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/lib/swift/pm -L /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/lib/swift/pm -lPackageDescription -target x86_64-apple-macosx10.10 /.../Bakemon/Package.swift -fileno 3
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/bin/swift-build-tool -f /.../Bakemon/.build/debug.yaml default -v
/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/bin/swiftc -module-name Bakemon -incremental -emit-dependencies -emit-module -emit-module-path /.../Bakemon/.build/debug/Bakemon.swiftmodule -output-file-map /.../Bakemon/.build/debug/Bakemon.build/output-file-map.json -parse-as-library -c /.../Bakemon/Sources/Bakemon.swift -I /.../Bakemon/.build/debug -j8 -Onone -g -D SWIFT_PACKAGE -enable-testing -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk

Generating an Xcode project

๐Ÿšจ Package.swift is necessary. ๐Ÿ˜ญ

$ mkdir ./Getemon
$ cd ./Getemon
$ swift build -X

error: no Package.swift file found

The result with -v is the same.

Retrying

โš ๏ธ Some sources are necessary. ๐Ÿ˜‚

$ touch Package.swift
$ swift build -X -v

/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/bin/swiftc --driver-mode=swift -I /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/lib/swift/pm -L /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/lib/swift/pm -lPackageDescription -target x86_64-apple-macosx10.10 /.../Getemon/Package.swift -fileno 3
warning: root package 'Getemon' does not contain any sources

Retrying

$ touch Getemon.swift
$ swift build -X -v

/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/bin/swiftc --driver-mode=swift -I /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/lib/swift/pm -L /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-16-a.xctoolchain/usr/lib/swift/pm -lPackageDescription -target x86_64-apple-macosx10.10 /.../Getemon/Package.swift -fileno 3
generated: ./Getemon.xcodeproj

Running swift build --clean

build (default)

$ swift build --clean build

The .build directory is removed.

dist

$ swift build --clean dist

The Packages directory and .build directory are removed.

The Packages directory is removed.



Thank you for reading it to the end. ๐Ÿ˜‡