Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Open Source Swift in Linux Mint

Avatar for ron333 ron333
April 13, 2016

Using Open Source Swift in Linux Mint

Avatar for ron333

ron333

April 13, 2016

More Decks by ron333

Other Decks in Programming

Transcript

  1. Using Open Source Swift in Linux Mint Ron Mourant Boston

    Swift Meetup April 13, 2016 https://medium.com/@ronm333 http://opensourceswift.co Twitter: @ronm333 1
  2. Topics ➤ Installation of LinuxMint in a Virtual Machine: A

    Development Environment for Swift ➤ Install Apple’s Linux Swift in LinuxMint ➤ An Introduction to the Swift Package Manager on Linux 2
  3. Installation of LinuxMint in a Virtual Machine:
 A Development Environment

    for Swift Switching between the host (Mac OS X) and client (LinuxMint 17.3) is done by just a mouse click. 
 Copying and Pasting between Mac OS X and LinuxMint is easy.
 Saving and restoring the LinuxMint client is fast. Virtual Box is a pleasure to work with. 3
  4. Install Apple’s Linux Swift in LinuxMint 4 Swift 3.0-dev is

    needed for the Swift Package Manager I downloaded the Ubuntu 14.04 dated April 12, 2016. The folder name is: swift-DEVELOPMENT-SNAPSHOT-2016-04-12-a-ubuntu14.04.tar.gz Code in the blog needs to be updated to reflect the new date.
  5. Install Apple’s Linux Swift in LinuxMint 5 The installation process

    entails: Step 1: Install Dependencies sudo apt-get install clang libicu-dev Step 2: Download Apple’ Swift 3.0-dev for Linux Step 3: Unpack the Tarball Step 4: Put swift in the PATH environment variable and Make the Change Permanent.
  6. Install Apple’s Linux Swift in LinuxMint 6 mint@mint ~ $

    mkdir swiftHello
 mint@mint ~ $ cd swiftHello
 
 mint@mint ~/swiftHello $ cat > helloworld.swift
 print("This is Hello World via Swift") // Press return and enter a control-d
 
 mint@mint ~/swiftHello $ cat helloworld.swift 
 print(“This is Hello World via Swift”) // Press return and enter a control-d
 
 mint@mint ~/swiftHello $ swift helloworld.swift 
 This is Hello World via Swift
 
 mint@mint ~/swiftHello $ swiftc helloworld.swift
 mint@mint ~/swiftHello $ ls -l
 -rwxr-xr-x 1 mint mint 10100 Apr 9 13:57 helloworld
 -rw-r--r-- 1 mint mint 77 Apr 9 13:54 helloworld.swift
 
 mint@mint ~/swiftHello $ ./helloworld
 This is Hello World via Swift
  7. An Introduction to the Swift Package Manager on Linux 7

    The Swift Package Manager is a tool that enables the easy distribution of Swift source code, executables and libraries. It handles dependencies, compiles source files, and links Swift packages. HelloPackage
 |— Package.swift
 |— Sources
 | |— main.swift
 | |— Greeter.swift
 |— .build
 | |—debug
 | |— HelloPackage
  8. An Introduction to the Swift Package Manager on Linux 8

    The swift build command creates an executable named HelloPackage, and places it in the hidden directory .build/debug. mint@mint ~/HelloPackage/Sources $ swift build mint@mint ~/HelloPackage $ .build/debug/HelloPackage Hello World Package from Getting Started
  9. An Introduction to the Swift Package Manager on Linux 9

    Multiple Source Files mint@mint ~/HelloPackage/Sources $ sudo gedit Greeter.swift func sayHello(name: String) { print("Hello, \(name)! This is the World Package from Getting Started.") } mint@mint ~/HelloPackage/Sources $ ls Greeter.swift main.swift Getting Command Line Arguments if Process.arguments.count != 2 { print("Usage: HelloPackage NAME") } else { let name = Process.arguments[1] sayHello(name) }
  10. An Introduction to the Swift Package Manager on Linux 10

    Rebuild the package mint@mint ~/HelloPackage/Sources $ swift build
 Compiling Swift Module 'HelloPackage' (2 sources) Linking ../.build/debug/HelloPackage Run the revised program mint@mint ~/HelloPackage $ .build/debug/HelloPackage Ron Hello, Ron! This is the World Package from Getting Started. https://swift.org/package-manager/
  11. More - Swift Package Library 11 A library is a

    package that builds a module which can be imported
 by other packages. example-package-playingcard ├── Sources │ ├── PlayingCard.swift │ ├── Rank.swift │ └── Suit.swift └── Package.swift Running swift build produces the static library PlayingCard.a example-package-fisheryates-1.0.1 │ ├── Package.swift │ ├── README.md │ └── Sources The fisheryates package implements the shuttle() an shuffleInPlace() methods
  12. More - Swift Package Library 12 The DeckOfPlayingCards package includes

    the previous two packages.
 Its manifest file includes dependencies. import PackageDescription let package = Package( name: "DeckOfPlayingCards", targets: [], dependencies: [ .Package(url: "https://github.com/apple/example-package-fisheryates.git", majorVersion: 1), .Package(url: "https://github.com/apple/example-package-playingcard.git", majorVersion: 1), ] )
  13. More - Swift Package Library 13 Note the static libraries

    below, generated by the swift build command. example-package-deckofplayingcards ├── .build │ └── debug │ ├── DeckOfPlayingCards.a │ ├── DeckOfPlayingCards.o │ ├── DeckOfPlayingCards.swiftdoc │ ├── DeckOfPlayingCards.swiftmodule │ ├── FisherYates.a │ ├── FisherYates.o │ ├── FisherYates.swiftdoc │ ├── FisherYates.swiftmodule │ ├── PlayingCard.a │ ├── PlayingCard.o │ ├── PlayingCard.swiftdoc │ └── PlayingCard.swiftmodule └── Packages └── example-package-fisheryates-1.0.1 │ ├── Package.swift │ ├── README.md │ └── Sources └── example-package-playingcard-1.0.0 ├── Package.swift ├── README.md └── Sources
  14. More - Swift Package Library 14 The Dealer module depends

    on the DeckOfPlayingCards package.
 Here is its manifest file. import PackageDescription let package = Package( name: "Dealer", targets: [], dependencies: [ .Package(url: "https://github.com/apple/example-package-deckofplayingcards.git", majorVersion: 1), ] ) The Sources directory of Dealer package includes main.swift.
 This causes the swift build command to produce the Dealer executable. https://github.com/apple/swift-package-manager