beng

Untitled

Sep 26th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. % cat greetings.swift
  2. public func sayHello(name: String) {
  3. print("Hello, " + name)
  4. }
  5.  
  6. % cat input.swift
  7. import Foundation
  8.  
  9. public func getInput() -> String {
  10. let stdin = NSFileHandle.fileHandleWithStandardInput()
  11. let data = stdin.availableData
  12. return NSString(data: data, encoding: NSUTF8StringEncoding) as! String
  13. }
  14.  
  15. % cat main.swift
  16. // This file must be named main.swift; the compiler won't allow top-level statements
  17. // otherwise
  18. import greetings
  19.  
  20. print("What is your name?")
  21. let name = getInput()
  22. sayHello(name)
  23.  
  24. # compiles greetings.swift and input.swift into a single "library" greetings.o
  25. % swift -frontend -c -module-name greetings -sdk `xcrun --sdk macosx --show-sdk-path` -target x86_64-apple-macosx10.11 -g -enable-testing -emit-dependencies -emit-module -emit-module-path greetings.swiftmodule -enable-objc-interop -emit-objc-header -emit-objc-header-path greetings-Swift.h -c greetings.swift input.swift
  26.  
  27. # compiles main.swift into main.o
  28. % swift -frontend -c -I . -c -primary-file main.swift -sdk `xcrun --sdk macosx --show-sdk-path` -target x86_64-apple-macosx10.11 -g -enable-testing -emit-dependencies -enable-objc-interop main.swift
  29.  
  30. # links the final binary, automatically pulling in swift library dependencies
  31. # note: the automatic swift library dependency system does NOT work if
  32. # greetings.o is in a standard libgreetings.a AR archive and pulled in with -lgreetings
  33. # (and it appears there are circular dependencies in these libraries so linking
  34. # them manually is not fun)
  35. % clang -isysroot `xcrun --sdk macosx --show-sdk-path` -mmacosx-version-min=10.11 -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift_static/macosx -Xlinker -force_load_swift_libs -lswiftRuntime -lc++ -framework Foundation -Xlinker -add_ast_path -Xlinker greetings.swiftmodule -o hello main.o greetings.o
  36.  
  37. % ./hello
  38. What is your name?
  39. Ben
  40. Hello, Ben
  41.  
  42. % otool -L ./hello
  43. ./hello:
  44. /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
  45. /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1252.0.0)
  46. /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1225.1.1)
  47. /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1253.0.0)
  48. /System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 600.0.0)
  49. /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
Advertisement
Add Comment
Please, Sign In to add comment