Skip to content

build C executables #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Fixtures/ValidLayouts/SingleModule/CExecutable/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import PackageDescription

let package = Package(
name: "CExecutable"
)
6 changes: 6 additions & 0 deletions Fixtures/ValidLayouts/SingleModule/CExecutable/Sources/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main() {
int a = 5;
printf("hello %d", a);
}
9 changes: 7 additions & 2 deletions Sources/Build/Command.compile(ClangModule).swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension Command {
let mkdir = Command.createDirectory(wd)

let inputs = module.dependencies.map{ $0.targetName } + module.sources.paths + [mkdir.node]
let productPath = Path.join(prefix, "lib\(module.c99name).so")
let productPath = Path.join(prefix, module.type == .Library ? "lib\(module.c99name).so" : module.c99name)

var args: [String] = []
#if os(Linux)
Expand Down Expand Up @@ -56,7 +56,12 @@ extension Command {
}

args += module.sources.paths
args += ["-shared", "-o", productPath]

if module.type == .Library {
args += ["-shared"]
}

args += ["-o", productPath]

let clang = ShellTool(
description: "Compiling \(module.name)",
Expand Down
2 changes: 1 addition & 1 deletion Sources/Build/describe().swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public func describe(prefix: String, _ conf: Configuration, _ modules: [Module],
//FIXME: Generate modulemaps if possible
//Since we're not generating modulemaps currently we'll just emit empty module map file
//if it not present
if !module.moduleMapPath.isFile {
if module.type == .Library && !module.moduleMapPath.isFile {
try POSIX.mkdir(module.moduleMapPath.parentDirectory)
try fopen(module.moduleMapPath, mode: .Write) { fp in
try fputs("\n", fp)
Expand Down
38 changes: 29 additions & 9 deletions Sources/PackageType/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ public class Module {
}
}

public enum ModuleType {
case Library, Executable
}

public protocol ModuleTypeProtocol {
var sources: Sources { get }
var type: ModuleType { get }
var mainFile: String { get }
}

extension ModuleTypeProtocol {
public var type: ModuleType {
let isLibrary = !sources.relativePaths.contains { path in
path.basename.lowercased() == mainFile
}
return isLibrary ? .Library : .Executable
}
}

extension Module: Hashable, Equatable {
public var hashValue: Int { return c99name.hashValue }
}
Expand All @@ -52,16 +71,11 @@ public class SwiftModule: Module {
self.sources = sources
super.init(name: name)
}
}

public enum ModuleType {
case Library, Executable
}

public var type: ModuleType {
let isLibrary = !sources.relativePaths.contains { path in
path.basename.lowercased() == "main.swift"
}
return isLibrary ? .Library : .Executable
extension SwiftModule: ModuleTypeProtocol {
public var mainFile: String {
return "main.swift"
}
}

Expand All @@ -84,6 +98,12 @@ public class ClangModule: CModule {
}
}

extension ClangModule: ModuleTypeProtocol {
public var mainFile: String {
return "main.c"
}
}

public class TestModule: SwiftModule {

public init(basename: String, sources: Sources) {
Expand Down
1 change: 0 additions & 1 deletion Sources/Transmute/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ extension Module {
public enum Error: ErrorProtocol {
case NoSources(String)
case MixedSources(String)
case CExecutableNotSupportedYet(String) //TODO: Remove this when add Support for C Exectuable
}
}
2 changes: 0 additions & 2 deletions Sources/Transmute/Package+modules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ extension Package {

if !cSources.isEmpty {
guard swiftSources.isEmpty else { throw Module.Error.MixedSources(path) }
//FIXME: Support executables for C languages
guard !cSources.contains({ $0.basename == "main.c" }) else { throw Module.Error.CExecutableNotSupportedYet(path) }
return ClangModule(name: name, sources: Sources(paths: cSources, root: path))
}

Expand Down
11 changes: 11 additions & 0 deletions Tests/Functional/TestClangModules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ class TestClangModulesTestCase: XCTestCase {
XCTAssertDirectoryExists(prefix, "Bar/Packages/Foo-1.2.3")
}
}

func testCExecutable() {
fixture(name: "ValidLayouts/SingleModule/CExecutable") { prefix in
XCTAssertBuilds(prefix)
let exec = ".build/debug/CExecutable"
XCTAssertFileExists(prefix, exec)
let output = try popen([Path.join(prefix, exec)])
XCTAssertEqual(output, "hello 5")
}
}
}


Expand All @@ -75,6 +85,7 @@ extension TestClangModulesTestCase {
("testExternalSimpleCDep", testExternalSimpleCDep),
("testiquoteDep", testiquoteDep),
("testCUsingCDep", testCUsingCDep),
("testCExecutable", testCExecutable),
]
}
}