Slide 1

Slide 1 text

Swift Package Manager NSBarcelona, April 2016 Boris Bügling - @NeoNacho

Slide 2

Slide 2 text

CocoaPods

Slide 3

Slide 3 text

Contentful

Slide 4

Slide 4 text

Agenda • What is swiftpm? • Making our own package • How does it work? • Comparison with related tools

Slide 5

Slide 5 text

What is swiftpm?

Slide 6

Slide 6 text

Development Snapshot March 24, 2016 $ swift --version Apple Swift version 3.0-dev (LLVM b010debd0e, Clang 3e4d01d89b, Swift 7182c58cb2)

Slide 7

Slide 7 text

import PackageDescription let package = Package( name: "Hello", dependencies: [ .Package(url: "ssh://[email protected]/Greeter.git", versions: Version(1,0,0)..

Slide 8

Slide 8 text

$ swift build Compiling Swift Module 'Clock' (2 sources) Linking Library: .build/debug/Clock.a

Slide 9

Slide 9 text

What does it do? • Compiles and links Swift packages • Resolves, fetches and builds their dependencies • Runs tests

Slide 10

Slide 10 text

Current state • Currently builds dynamic/static libraries or binaries • Supported platforms are OS X and Ubuntu Linux • Only builds Swift code, no C/C++/Objective-C/... (but there's an accepted proposal SE-0038)

Slide 11

Slide 11 text

swift build OVERVIEW: Build sources into binary products USAGE: swift build [options] MODES: --configuration Build with configuration (debug|release) [-c] --clean[=] Delete artefacts (build|dist) [-k] --init Creates a new Swift package (executable|library) --fetch Fetch package dependencies --generate-xcodeproj [] Generates an Xcode project for this package [-X] OPTIONS: --chdir Change working directory before any other operation [-C] -v[v] Increase verbosity of informational output -Xcc Pass flag through to all C compiler instantiations -Xlinker Pass flag through to all linker instantiations -Xswiftc Pass flag through to all Swift compiler instantiations

Slide 12

Slide 12 text

swift test OVERVIEW: Build and run tests USAGE: swift test [options] OPTIONS: TestModule.TestCase Run a test case subclass TestModule.TestCase/test1 Run a specific test method

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Making our own package

Slide 15

Slide 15 text

! A small library for parsing and writing ISO8601 date strings. https://github.com/neonichu/Clock/tree/swift-3.0

Slide 16

Slide 16 text

Sources └── Clock ├── ISO8601Parser.swift └── ISO8601Writer.swift 1 directory, 2 files

Slide 17

Slide 17 text

$ touch Package.swift $ swift build

Slide 18

Slide 18 text

Tests?

Slide 19

Slide 19 text

Tests ├── Clock │ ├── Localtime.swift │ ├── String.swift │ ├── TMStruct.swift │ └── UTC.swift └── LinuxMain.swift 1 directory, 5 files

Slide 20

Slide 20 text

XCTest import XCTest @testable import Clock class DateToStringConversionTests: XCTestCase { func testConvertTMStructToAnISO8601GMTString() { let actual = tm_struct(year: 1971, month: 2, day: 3, hour: 9, minute: 16, second: 6) XCTAssertEqual(actual.toISO8601GMTString(), "1971-02-03T09:16:06Z") } }

Slide 21

Slide 21 text

Linux #if os(Linux) extension DateToStringConversionTests { static var allTests : [(String, DateToStringConversionTests -> () throws -> Void)] { return [ ("testConvertTMStructToAnISO8601GMTString", testConvertTMStructToAnISO8601GMTString) ] } } #endif

Slide 22

Slide 22 text

LinuxMain.swift import XCTest @testable import ClockTestSuite XCTMain([ testCase(TMStructTests.allTests), testCase(DateToStringConversionTests.allTests), ])

Slide 23

Slide 23 text

Side-note about Linux

Slide 24

Slide 24 text

Foundation is incomplete and sometimes different from OS X: #if os(Linux) let index = p.startIndex.distanceTo(p.startIndex.successor()) path = NSString(string: p).substringFromIndex(index) #else path = p.substringFromIndex(p.startIndex.successor()) #endif

Slide 25

Slide 25 text

Some things in the standard library might not be available: #if _runtime(_ObjC) // Excluded due to use of dynamic casting and Builtin.autorelease, neither // of which correctly work without the ObjC Runtime right now. // See rdar://problem/18801510 [...] public func getVaList(args: [CVarArgType]) -> CVaListPointer {

Slide 26

Slide 26 text

OS X libc and Glibc can differ: let flags = GLOB_TILDE | GLOB_BRACE | GLOB_MARK if system_glob(cPattern, flags, nil, >) == 0 { #if os(Linux) let matchc = gt.gl_pathc #else let matchc = gt.gl_matchc #endif

Slide 27

Slide 27 text

And other random fun: ./.build/debug/spectre-build /usr/bin/ld: .build/debug/Clock.a(ISO8601Parser.swift.o): undefined reference to symbol '_swift_FORCE_LOAD_$_swiftGlibc' /home/travis/.swiftenv/versions/swift-2.2-SNAPSHOT-2015-12-22-a/ usr/lib/swift/linux/libswiftGlibc.so: error adding symbols: DSO missing from command line clang: error: linker command failed with exit code 1 (use -v to see invocation)

Slide 28

Slide 28 text

Running the tests $ swift test Test Suite 'All tests' started at 2016-04-06 17:06:00.900 Test Suite 'Package.xctest' started at 2016-04-06 17:06:00.901 Test Suite 'DateToStringConversionTests' started at 2016-04-06 17:06:00.901 Test Case '-[ClockTestSuite.DateToStringConversionTests testCanConvertNSDateToAnISO8601GMString]' started. [...] Test Suite 'All tests' passed at 2016-04-06 17:06:00.911. Executed 12 tests, with 0 failures (0 unexpected) in 0.008 (0.011) seconds

Slide 29

Slide 29 text

Swift versions $ cat .swift-version swift-2.2-SNAPSHOT-2015-12-22-a • Is read by either chswift or swiftenv

Slide 30

Slide 30 text

Travis CI os: - linux - osx language: generic sudo: required dist: trusty osx_image: xcode7.2 install: - curl -sL https://gist.github.com/kylef/ 5c0475ff02b7c7671d2a/raw/ 621ef9b29bbb852fdfd2e10ed147b321d792c1e4/swiftenv-install.sh | bash script: - . ~/.swiftenv/init

Slide 31

Slide 31 text

How does it work?

Slide 32

Slide 32 text

Modules inside SwiftPackageManager Target( /** “Swifty” POSIX functions from libc */ name: "POSIX", dependencies: ["libc"]), Target( /** Abstractions for common operations */ name: "Utility", dependencies: ["POSIX"]), Target( /** Base types for the package-engine */ name: "PackageType", dependencies: ["PackageDescription", "Utility"]), Target( name: "ManifestParser", dependencies: ["PackageDescription", "PackageType"]), Target( /** Turns Packages into Modules & Products */ name: "Transmute", dependencies: ["PackageDescription", "PackageType"]), Target( /** Fetches Packages and their dependencies */ name: "Get", dependencies: ["PackageDescription", "PackageType"]),

Slide 33

Slide 33 text

Modules inside SwiftPackageManager Target( /** Builds Modules and Products */ name: "Build", dependencies: ["PackageType"]), Target( /** Common components of both executables */ name: "Multitool", dependencies: ["PackageType"]), Target( /** Generates Xcode projects */ name: "Xcodeproj", dependencies: ["PackageType"]), Target( /** The main executable provided by SwiftPM */ name: "swift-build", dependencies: ["ManifestParser", "Get", "Transmute", "Build", "Multitool", "Xcodeproj"]), Target( /** Runs package tests */ name: "swift-test", dependencies: ["Multitool"]),

Slide 34

Slide 34 text

Dependencies • Can be local or remote Git repositories • Need to be tagged • Will be fetched to ./Packages/MyPackage-0.0.1

Slide 35

Slide 35 text

Git tagging • Package.swift only supports tagged dependencies • Don't forget to push your tags to GitHub

Slide 36

Slide 36 text

Rough build process • PackageDescription generates TOML • Transmute parses the TOML and can generate YAML • Dependencies are fetched by Get • YAML is used as input to llbuild • Build calls out to llbuild

Slide 37

Slide 37 text

llbuild

Slide 38

Slide 38 text

$ cat .build/debug.yaml client: name: swift-build tools: {} targets: test: [, ] default: []

Slide 39

Slide 39 text

commands: .../.build/debug/Clock.build: tool: mkdir outputs: [.../.build/debug/Clock.build] : tool: swift-compiler executable: .../bin/swiftc module-name: Clock module-output-path: .../.build/debug/Clock.swiftmodule inputs: [] outputs: [, .../.build/debug/Clock.build/ISO8601Parser.swift.o, .../.build/debug/Clock.build/ISO8601Writer.swift.o] import-paths: .../.build/debug temps-path: .../.build/debug/Clock.build objects: [.../.build/debug/Clock.build/ISO8601Parser.swift.o, .../.build/debug/Clock.build/ISO8601Writer.swift.o] other-args: ["-j8", "-Onone", "-g", "-D", SWIFT_PACKAGE, "-enable-testing", "-F", ".../Developer/Library/Frameworks", "-target", "x86_64-apple-macosx10.10", "-sdk", ".../Developer/SDKs/MacOSX10.11.sdk"] sources: [.../Sources/Clock/ISO8601Parser.swift, .../Sources/Clock/ISO8601Writer.swift] is-library: true

Slide 40

Slide 40 text

.../.build/debug/ClockTestSuite.build: tool: mkdir outputs: [.../.build/debug/ClockTestSuite.build] : tool: swift-compiler executable: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-03-24-a.xctoolchain/usr/bin/swiftc module-name: ClockTestSuite module-output-path: .../.build/debug/ClockTestSuite.swiftmodule inputs: [] outputs: [, .../.build/debug/ClockTestSuite.build/Localtime.swift.o, .../.build/debug/ClockTestSuite.build/String.swift.o, .../.build/debug/ClockTestSuite.build/TMStruct.swift.o, .../.build/debug/ClockTestSuite.build/UTC.swift.o] import-paths: .../.build/debug temps-path: .../.build/debug/ClockTestSuite.build objects: [.../.build/debug/ClockTestSuite.build/Localtime.swift.o, .../.build/debug/ClockTestSuite.build/String.swift.o, .../.build/debug/ClockTestSuite.build/TMStruct.swift.o, .../.build/debug/ClockTestSuite.build/UTC.swift.o] other-args: ["-j8", "-Onone", "-g", "-D", SWIFT_PACKAGE, "-enable-testing", "-F", ".../Developer/Library/Frameworks", "-target", "x86_64-apple-macosx10.10", "-sdk", ".../Developer/SDKs/MacOSX10.11.sdk"] sources: [.../Tests/Clock/Localtime.swift, .../Tests/Clock/String.swift, .../Tests/Clock/TMStruct.swift, .../Tests/Clock/UTC.swift] is-library: true

Slide 41

Slide 41 text

: tool: shell description: Linking .build/debug/Package.xctest/Contents/MacOS/Package inputs: [, , .../.build/debug/ClockTestSuite.build/Localtime.swift.o, .../.build/debug/ClockTestSuite.build/String.swift.o, .../.build/debug/ClockTestSuite.build/TMStruct.swift.o, .../.build/debug/ClockTestSuite.build/UTC.swift.o] outputs: [, .../.build/debug/Package.xctest/Contents/MacOS/Package] args: [".../bin/swiftc", "-target", "x86_64-apple-macosx10.10", "-sdk", ".../Developer/SDKs/MacOSX10.11.sdk", "-g", "-L.../.build/debug", "-o", .../.build/debug/Package.xctest/Contents/MacOS/Package, "-Xlinker", "-bundle", "-F", ".../Developer/Library/Frameworks", .../.build/debug/ClockTestSuite.build/Localtime.swift.o, .../.build/debug/ClockTestSuite.build/String.swift.o, .../.build/debug/ClockTestSuite.build/TMStruct.swift.o, .../.build/debug/ClockTestSuite.build/UTC.swift.o, .../.build/debug/Clock.build/ISO8601Parser.swift.o, .../.build/debug/Clock.build/ISO8601Writer.swift.o]

Slide 42

Slide 42 text

Additional Package.swift syntax

Slide 43

Slide 43 text

Targets import PackageDescription let package = Package( name: "Example", targets: [ Target( name: "top", dependencies: [.Target(name: "bottom")]), Target( name: "bottom") ] )

Slide 44

Slide 44 text

Exclusion let package = Package( name: "Example", exclude: ["tools", "docs", "Sources/libA/images"] )

Slide 45

Slide 45 text

Test dependencies import PackageDescription let package = Package( name: "Hello", testDependencies: [ .Package(url: "ssh://[email protected]/Tester.git", versions: Version(1,0,0)..

Slide 46

Slide 46 text

Customizing builds import PackageDescription var package = Package() #if os(Linux) let target = Target(name: "LinuxSources/foo") package.targets.append(target) #endif

Slide 47

Slide 47 text

Integrate system libraries • Empty Package.swift • module.modulemap: module curl [system] { header "/usr/include/curl/curl.h" link "curl" export * }

Slide 48

Slide 48 text

let package = Package( name: "example", dependencies: [ .Package(url: "https://github.com/neonichu/curl", majorVersion: 1) ] )

Slide 49

Slide 49 text

Comparison with related tools

Slide 50

Slide 50 text

CocoaPods • Centralized discovery • Xcode integration • Support for other languages • Additional metadata in the Manifest • Does not cover the build process

Slide 51

Slide 51 text

Carthage • No manifest for packages • Xcode integration • Support for other languages

Slide 52

Slide 52 text

Support all three • Package.swift • .podspec • .xcodeproj / .xcworkspace

Slide 53

Slide 53 text

!

Slide 54

Slide 54 text

CocoaPods • chocolat-cli converts Package.swift to a JSON Podspec

Slide 55

Slide 55 text

public func parse_package(packagePath: String) throws -> PackageDescription.Package { // FIXME: We depend on `chswift` installation and use here let toolchainPath = PathKit.Path(POSIX.getenv("CHSWIFT_TOOLCHAIN") ?? "") libc.setenv("SPM_INSTALL_PATH", toolchainPath.parent().description, 1) print_if("Using libPath \(Resources.runtimeLibPath)", false) let package = (try Manifest(path: packagePath)).package print_if("Converting package \(package.name) at \(packagePath)", false) return package }

Slide 56

Slide 56 text

You should think of it as an alpha code base that hasn't had a release yet. Yes, it is useful for doing some things [...] — Daniel Dunbar

Slide 57

Slide 57 text

References • https://swift.org • https://github.com/apple/swift-package-manager • https://github.com/apple/llbuild • https://github.com/neonichu/chocolat • https://github.com/neonichu/chswift • https://github.com/neonichu/freedom • https://github.com/kylef/swiftenv

Slide 58

Slide 58 text

Thank you!

Slide 59

Slide 59 text

@NeoNacho [email protected] http://buegling.com/talks