Skip to content

Commit

Permalink
Add Objective-C Support to ClangModules
Browse files Browse the repository at this point in the history
  • Loading branch information
aciidgh committed May 20, 2016
1 parent 26e7437 commit b1f455b
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Fixtures/ClangModules/Objc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
7 changes: 7 additions & 0 deletions Fixtures/ClangModules/Objc/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import PackageDescription

let package = Package(
name: "Objc",
targets: [Target(name: "objc-exec", dependencies:["objc"]),
Target(name: "swift-exec", dependencies:["objc"])]
)
8 changes: 8 additions & 0 deletions Fixtures/ClangModules/Objc/Sources/objc-exec/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <Foundation/Foundation.h>
#import "objc.h"

int main() {
int result = [[[Foo alloc] init] foo] + one();
printf("Hello from Objc %d", result);
return 0;
}
8 changes: 8 additions & 0 deletions Fixtures/ClangModules/Objc/Sources/objc/include/objc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <Foundation/Foundation.h>
#include "sea.h"

@interface Foo: NSObject

- (int)foo;

@end
1 change: 1 addition & 0 deletions Fixtures/ClangModules/Objc/Sources/objc/include/sea.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int one();
9 changes: 9 additions & 0 deletions Fixtures/ClangModules/Objc/Sources/objc/objc.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import "include/objc.h"

@implementation Foo

- (int)foo {
return 3 + one();
}

@end
4 changes: 4 additions & 0 deletions Fixtures/ClangModules/Objc/Sources/objc/sea.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "include/sea.h"
int one() {
return 1;
}
3 changes: 3 additions & 0 deletions Fixtures/ClangModules/Objc/Sources/swift-exec/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import objc

print("\(Foo().foo() + one())")
12 changes: 6 additions & 6 deletions Sources/PackageModel/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public enum ModuleType {
public protocol ModuleTypeProtocol {
var sources: Sources { get }
var type: ModuleType { get }
var mainFile: String { get }
var mainFiles: Set<String> { get }
}

extension ModuleTypeProtocol {
public var type: ModuleType {
let isLibrary = !sources.relativePaths.contains { path in
path.basename.lowercased() == mainFile
mainFiles.contains(path.basename.lowercased())
}
return isLibrary ? .Library : .Executable
}
Expand Down Expand Up @@ -86,8 +86,8 @@ public class SwiftModule: Module {
}

extension SwiftModule: ModuleTypeProtocol {
public var mainFile: String {
return "main.swift"
public var mainFiles: Set<String> {
return ["main.swift"]
}
}

Expand Down Expand Up @@ -125,8 +125,8 @@ extension ClangModule: XcodeModuleProtocol {
}

extension ClangModule: ModuleTypeProtocol {
public var mainFile: String {
return "main.c"
public var mainFiles: Set<String> {
return ["main.c", "main.m"]
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageModel/Sources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct Sources {

static public var validSwiftExtensions = Set<String>(["swift"])

static public var validCExtensions = Set<String>(["c"])
static public var validCExtensions = Set<String>(["c", "m"])

static public var validExtensions = { validSwiftExtensions.union(validCExtensions) }()
}
23 changes: 23 additions & 0 deletions Tests/Functional/ClangModuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ class ClangModulesTestCase: XCTestCase {
XCTAssertFileExists(prefix, ".build", "debug", "libCDynamicLookup.so")
}
}

#if os(OSX)
func testObjectiveCModule() {
fixture(name: "ClangModules/Objc") { prefix in
XCTAssertBuilds(prefix)

// Check if objc module is present.
XCTAssertFileExists(prefix, ".build", "debug", "libobjc.so")

// Test objc executable (depends on objc module).
let objcExec = ".build/debug/objc_exec"
XCTAssertFileExists(prefix, objcExec)
var output = try popen([Path.join(prefix, objcExec)])
XCTAssertEqual(output, "Hello from Objc 5")

// Test swift executable (depends on objc module).
let swiftExec = ".build/debug/swift-exec"
XCTAssertFileExists(prefix, swiftExec)
output = try popen([Path.join(prefix, swiftExec)])
XCTAssertEqual(output, "5\n")
}
}
#endif
}


Expand Down

0 comments on commit b1f455b

Please sign in to comment.