Last active
December 16, 2020 16:50
-
-
Save bhamiltoncx/3d846862eb42eede17fa to your computer and use it in GitHub Desktop.
Compiling Swift and Objective-C from the Command-Line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% ls -l | |
total 40 | |
-rw-r--r--@ 1 bhamiltoncx staff 111 Sep 26 14:42 ObjcGreeter.h | |
-rw-r--r--@ 1 bhamiltoncx staff 155 Sep 26 14:50 ObjcGreeter.m | |
-rw-r--r-- 1 bhamiltoncx staff 300 Sep 26 15:06 greetings.swift | |
-rw-r--r-- 1 bhamiltoncx staff 304 Sep 26 15:06 input.swift | |
-rw-r--r-- 1 bhamiltoncx staff 165 Sep 26 15:06 main.swift | |
% cat greetings.swift | |
import Foundation | |
public func sayHello(name: String) { | |
print("Hello " + name) | |
} | |
@objc public class Greeter : NSObject { | |
public class func sayHello(name: String) { | |
// To access the global function, you have to prefix it with the module name | |
greetings.sayHello(name) | |
} | |
} | |
% cat input.swift | |
import Foundation | |
public func getInput() -> String? { | |
let stdin = NSFileHandle.fileHandleWithStandardInput() | |
let data = stdin.availableData | |
let str = NSString(data: data, encoding: NSUTF8StringEncoding) | |
let trimmedStr = str?.stringByTrimmingCharactersInSet( | |
NSCharacterSet.whitespaceAndNewlineCharacterSet()) | |
return trimmedStr as String? | |
} | |
% cat ObjcGreeter.h | |
#import <Foundation/Foundation.h> | |
@interface ObjcGreeter : NSObject | |
+ (void)sayHello:(NSString *)name; | |
@end | |
% cat ObjcGreeter.m | |
#import "ObjcGreeter.h" | |
// Generated by the Swift compiler | |
#import "greetings-Swift.h" | |
@implementation ObjcGreeter | |
+ (void)sayHello:(NSString *)name { | |
[Greeter sayHello:name]; | |
} | |
@end | |
% cat main.swift | |
import greetings | |
print("What is your name?") | |
let name = getInput() ?? "(unable to decode input)" | |
sayHello(name) | |
ObjcGreeter.sayHello("to Swift from Objective-C") | |
% swift -frontend -c -module-name greetings -sdk `xcrun --sdk macosx --show-sdk-path` -target x86_64-apple-macosx10.11 -emit-module -emit-module-path greetings.swiftmodule -enable-objc-interop -emit-objc-header -emit-objc-header-path greetings-Swift.h -c -o libgreetings.o greetings.swift input.swift | |
% clang -isysroot `xcrun --sdk macosx --show-sdk-path` -mmacosx-version-min=10.11 -c -I . ObjcGreeter.m | |
% swift -frontend -c -I . -c -primary-file main.swift -sdk `xcrun --sdk macosx --show-sdk-path` -target x86_64-apple-macosx10.11 -enable-objc-interop -import-objc-header ObjcGreeter.h main.swift | |
% 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 libgreetings.o ObjcGreeter.o | |
% ./hello | |
What is your name? | |
Ben | |
Hello Ben | |
Hello to Swift from Objective-C | |
% printf "\xAB\xCD\xEF" | ./hello | |
What is your name? | |
Hello (unable to decode input) | |
Hello to Swift from Objective-C | |
% echo 💩 | ./hello | |
What is your name? | |
Hello 💩 | |
Hello to Swift from Objective-C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment