Wednesday, January 6, 2016

What you need for Swift on Linux

What you need

(1) ubuntu-14.04.3-desktop-amd64.iso
http://releases.ubuntu.com/14.04/

(2) Virtual Box 5.0 from https://www.virtualbox.org/wiki/Downloads
Virtual Harddisk of 20G or above is recommended.

(3) Install clang in ubuntu 14
sudo apt-get update
sudo apt-get --assume-yes install clang-3.4 libicu-dev git vim

(4) download swift in ubuntu 14
curl -O https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-31-a/swift-2.2-SNAPSHOT-2015-12-31-a-ubuntu14.04.tar.gz
tar xzvf swift-2.2-SNAPSHOT-2015-12-31-a-ubuntu14.04.tar.gz
export PATH=$HOME/swift-2.2-SNAPSHOT-2015-12-31-a-ubuntu14.04/usr/bin:$PATH

(5) test compile
swift --version
echo 'print("Hello World")' > helloworld.swift
swiftc helloworld.swift
./helloworld

(6) remember to Insert Guest additions CD-image and reboot the virtual host.

(7) Test build
cd ${HOME}
git clone https://github.com/apple/example-package-dealer.git
cd example-package-dealer
swift build
.build/debug/Dealer

(8) Test build by adding swift package
shell script    Select all
cd ${HOME} mkdir -p teststring cd teststring cat > Package.swift <<EOF import PackageDescription let package = Package( name: "teststring", dependencies: [ .Package(url: "https://github.com/erica/SwiftString.git", majorVersion: 1), ] ) EOF cat > main.swift <<EOF import SwiftString let line = "apple,peach,kiwi" let parts = line.componentsSeparatedByString(",") print(parts) EOF swift build (cd Packages/SwiftString-1.0.5; git pull) swift build .build/debug/teststring


(9) Test build by adding C module
sudo apt-get install -y libcurl4-gnutls-dev libjson-c-dev
cd ${HOME}
git clone https://github.com/iachievedit/moreswift
cd moreswift/translator_swiftpm
swift build
.build/debug/translator "Hello" from "en" to "zh-cn"


(10) Test build by adding and modifying swift package
shell script    Select all
cd ${HOME} mkdir -p testdatestring/Sources/Test1 cd testdatestring cat > Package.swift <<EOF import PackageDescription let package = Package( name: "testdatestring", dependencies: [ .Package(url: "https://github.com/erica/SwiftString.git", majorVersion: 1), .Package(url: "https://github.com/haginile/SwiftDate.git", majorVersion: 1), ] ) EOF cat > Sources/Test1/main.swift <<EOF #if os(Linux) import Glibc import Foundation #endif import SwiftString import SwiftDate let start_date = Date(string: "2000-01-15") var end_date = Date(string: "2000-05-31") var actual_365 = Actual365Fixed() print(actual_365.dayCountFraction(start_date, date2: end_date)) EOF swift build (cd Packages/SwiftDate; ln -s SwiftDate Sources; echo "import PackageDescription;let package = Package(name: \"SwiftDate\")" > Package.swift) (cd Packages; mv SwiftDate SwiftDate-1.0.0) swift build (cd Packages/SwiftString-1.0.5; git pull) swift build


(10.1) Fix some errors in DateHelpers.swift
vim Packages/SwiftDate-1.0.0/SwiftDate/DateHelpers.swift    Select all
import Foundation import SwiftString ... extension String { //subscript (i: Int) -> String { // return String(Array(self.characters)[i]) //} subscript (r: Range) -> String { get { let subStart = self.startIndex.advancedBy(r.startIndex, limit: self.endIndex) let subEnd = subStart.advancedBy(r.endIndex - r.startIndex, limit: self.endIndex) return self[Range(start: startIndex, end: endIndex)] //return self.substringWithRange(Range(start: subStart, end: subEnd)) } }


(10.2) Fix error in Term.swift at line 195
vim +195 Packages/SwiftDate-1.0.0/SwiftDate/Term.swift    Select all
class func parseOneTerm(string : String) -> Term { let str = string.uppercaseString let len = str.characters.count let length = Int(str[0..<(len - 1)]) let timeUnit = str[len-1] switch timeUnit! { // unwrapped optional case "D": return Term(length: length!, timeUnit: TimeUnit.Day) case "W": return Term(length: length!, timeUnit: TimeUnit.Week) case "M": return Term(length: length!, timeUnit: TimeUnit.Month) case "Y": return Term(length: length!, timeUnit: TimeUnit.Year) default: return Term() } }


(10.3) Build target Test1
shell script    Select all
swift build .build/debug/Test1


(10.4) Add XCTest for target Test2
shell script    Select all
cd ${HOME}/testdatestring mkdir -p Sources/Test2 cat > Sources/Test2/main.swift <<EOF #if os(Linux) import Glibc import Foundation #endif import SwiftString import SwiftDate import XCTest class SwiftDateTesting: XCTestCase { var allTests : [(String, () -> Void)] { return [ ("testCalendar", testCalendar), ("testDayCounter", testDayCounter), ] } func testCalendar() { var cal = USSettlementCalendar() XCTAssert(cal.isBizDay(Date(string: "2014-05-15")) == true, "Pass") XCTAssert(cal.isBizDay(Date(string: "2014-05-17")) == false, "Pass") XCTAssertEqual(cal.nextBizDay(Date(string: "2014-05-16")).serialNumber, Date(string: "2014-05-19").serialNumber, "Pass") XCTAssertEqual(cal.bizDaysBetween(Date(string: "2014-05-15"), toDate: Date(string : "2014-05-20")), 3, "Pass") } func testDayCounter() { var start_date = Date(string: "2000-01-15") var end_date = Date(string: "2000-05-31") var actual_365 = Actual365Fixed() XCTAssertEqual(actual_365.dayCountFraction(start_date, date2: end_date), 137.0 / 365, "Pass") var nl_365 = NL365() XCTAssertEqual(nl_365.dayCountFraction(start_date, date2: end_date), 136.0 / 365, "Pass") var act_360 = Actual360() XCTAssertEqual(act_360.dayCountFraction(start_date, date2: end_date), 137.0 / 360.0, "Pass") var thirty_360 = Thirty360() XCTAssertEqual(thirty_360.dayCountFraction(start_date, date2: end_date), 136.0 / 360.0, "Pass") var thirty_360e = Thirty360(convention: Thirty360.Convention.EurobondBasis) } } XCTMain([SwiftDateTesting()]) EOF swift build .build/debug/Test2