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
Add basic support for building C via swiftpm #183
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3b87ea0
Add basic support for building C via swiftpm. As of now assumes `modu…
ankitspd 4c9389d
Add tests for C libraries, Rename CLang to Clang
ankitspd 492a081
Refactor isValidSource
ankitspd e5914ad
refactor module generation
ankitspd 1b5cd20
Rename CLANG_EXEC to CC
ankitspd 15e1298
change base class for ClangModule
ankitspd 60b7a5c
Throw error if sources contain main.c
ankitspd 0d7d866
Generate empty module map if needed
ankitspd 5b1e92c
Fix product path for cmodules, remove -working-dir flag
ankitspd 50b7bb6
Add iquote support
ankitspd d2eb438
Add iquote to have dependencies between c modules
ankitspd cc54c3b
Use -I for other packages
ankitspd ca48afb
Simplify module logic
ankitspd 32ed31c
Add comment explaining -iquote vs -I and discovery of external deps
ankitspd 5f426f7
Update split method to swift-3-api-guidelines
ankitspd 114cf3e
rename test file
ankitspd File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
int foo() { | ||
int a = 5; | ||
int b = a; | ||
a = b; | ||
return a; | ||
} |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
int foo(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module CLibraryFlat { | ||
header "abc.h" | ||
link "CLibraryFlat" | ||
export * | ||
} |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
int foo() { | ||
int a = 5; | ||
int b = a; | ||
a = b; | ||
return a; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
int foo(); |
5 changes: 5 additions & 0 deletions
5
Fixtures/ClangModules/CLibrarySources/Sources/include/module.modulemap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module CLibrarySources { | ||
header "abc.h" | ||
link "CLibrarySources" | ||
export * | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "CLibraryiquote", | ||
targets: [ | ||
Target(name: "Bar", dependencies: ["Foo"]), | ||
Target(name: "Baz", dependencies: ["Foo", "Bar"])] | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "include/Bar/Bar.h" | ||
#include "Foo/Foo.h" | ||
|
||
int bar() { | ||
int a = foo(); | ||
int b = a; | ||
a = b; | ||
return a; | ||
} |
1 change: 1 addition & 0 deletions
1
Fixtures/ClangModules/CLibraryiquote/Sources/Bar/include/Bar/Bar.h
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
int bar(); |
5 changes: 5 additions & 0 deletions
5
Fixtures/ClangModules/CLibraryiquote/Sources/Bar/include/module.modulemap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Bar { | ||
header "Bar/Bar.h" | ||
link "Bar" | ||
export * | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foo | ||
import Bar | ||
|
||
let _ = foo() | ||
let _ = bar() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "include/Foo/Foo.h" | ||
|
||
int foo() { | ||
int a = 5; | ||
int b = a; | ||
a = b; | ||
return a; | ||
} |
1 change: 1 addition & 0 deletions
1
Fixtures/ClangModules/CLibraryiquote/Sources/Foo/include/Foo/Foo.h
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
int foo(); |
5 changes: 5 additions & 0 deletions
5
Fixtures/ClangModules/CLibraryiquote/Sources/Foo/include/module.modulemap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Foo { | ||
header "Foo/Foo.h" | ||
link "Foo" | ||
export * | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "SwiftCMixed", | ||
targets: [Target(name: "SeaExec", dependencies: ["SeaLib"])] | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import SeaLib | ||
|
||
let a = foo(5) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
int foo(int a) { | ||
return a; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
int foo(int a); |
5 changes: 5 additions & 0 deletions
5
Fixtures/ClangModules/SwiftCMixed/Sources/SeaLib/include/module.modulemap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module SeaLib { | ||
header "Foo.h" | ||
link "SeaLib" | ||
export * | ||
} |
8 changes: 8 additions & 0 deletions
8
Fixtures/DependencyResolution/External/CUsingCDep/Bar/Package.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "Bar", | ||
dependencies: [ | ||
.Package(url: "../Foo", majorVersion: 1) | ||
] | ||
) |
5 changes: 5 additions & 0 deletions
5
Fixtures/DependencyResolution/External/CUsingCDep/Bar/Sources/SeaLover/Sea.c
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include <Foo/Foo.h> | ||
|
||
void cool() { | ||
foo(); | ||
} |
Empty file.
5 changes: 5 additions & 0 deletions
5
...es/DependencyResolution/External/CUsingCDep/Bar/Sources/SeaLover/include/module.modulemap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module SeaLover { | ||
header "Sea/Sea.h" | ||
link "SeaLover" | ||
export * | ||
} |
3 changes: 3 additions & 0 deletions
3
Fixtures/DependencyResolution/External/CUsingCDep/Bar/Sources/SwiftExec/main.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Foo | ||
|
||
foo() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
void foo() { | ||
|
||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
Fixtures/DependencyResolution/External/CUsingCDep/Foo/include/Foo/Foo.h
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void foo(); |
5 changes: 5 additions & 0 deletions
5
Fixtures/DependencyResolution/External/CUsingCDep/Foo/include/module.modulemap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Foo { | ||
header "Foo/Foo.h" | ||
link "Foo" | ||
export * | ||
} |
6 changes: 6 additions & 0 deletions
6
Fixtures/DependencyResolution/External/SimpleCDep/Bar/Package.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "Bar", | ||
dependencies: [ | ||
.Package(url: "../Foo", majorVersion: 1)]) |
3 changes: 3 additions & 0 deletions
3
Fixtures/DependencyResolution/External/SimpleCDep/Bar/main.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Foo | ||
|
||
foo() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
void foo() { | ||
|
||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
Fixtures/DependencyResolution/External/SimpleCDep/Foo/include/Foo.h
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void foo(); |
5 changes: 5 additions & 0 deletions
5
Fixtures/DependencyResolution/External/SimpleCDep/Foo/include/module.modulemap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Foo { | ||
header "Foo.h" | ||
link "Foo" | ||
export * | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,19 +38,19 @@ extension Package { | |
let modules: [Module] | ||
if maybeModules.isEmpty { | ||
do { | ||
modules = [SwiftModule(name: self.name, sources: try sourcify(srcroot))] | ||
modules = [try modulify(srcroot, name: self.name)] | ||
} catch Module.Error.NoSources { | ||
throw ModuleError.NoModules(self) | ||
} | ||
} else { | ||
modules = try maybeModules.map(sourcify).map { sources in | ||
modules = try maybeModules.map { path in | ||
let name: String | ||
if sources.root == srcroot { | ||
if path == srcroot { | ||
name = self.name | ||
} else { | ||
name = sources.root.basename | ||
name = path.basename | ||
} | ||
return SwiftModule(name: name, sources: sources) | ||
return try modulify(path, name: name) | ||
} | ||
} | ||
|
||
|
@@ -74,19 +74,36 @@ extension Package { | |
|
||
return modules | ||
} | ||
|
||
func sourcify(path: String) throws -> Sources { | ||
let sources = walk(path, recursing: shouldConsiderDirectory).filter(isValidSource) | ||
guard sources.count > 0 else { throw Module.Error.NoSources(path) } | ||
return Sources(paths: sources, root: path) | ||
|
||
func modulify(path: String, name: String) throws -> Module { | ||
let walked = walk(path, recursing: shouldConsiderDirectory).map{ $0 } | ||
|
||
let cSources = walked.filter{ isValidSource($0, validExtensions: Sources.validCExtensions) } | ||
let swiftSources = walked.filter{ isValidSource($0, validExtensions: Sources.validSwiftExtensions) } | ||
|
||
if !cSources.isEmpty { | ||
guard swiftSources.isEmpty else { throw Module.Error.MixedSources(path) } | ||
//FIXME: Support executables for C languages | ||
guard !cSources.contains({ $0.hasSuffix("main.c") }) else { throw Module.Error.CExecutableNotSupportedYet(path) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should check the basename. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. raised #199 for this |
||
return ClangModule(name: name, sources: Sources(paths: cSources, root: path)) | ||
} | ||
|
||
guard !swiftSources.isEmpty else { throw Module.Error.NoSources(path) } | ||
return SwiftModule(name: name, sources: Sources(paths: swiftSources, root: path)) | ||
} | ||
|
||
func isValidSource(path: String) -> Bool { | ||
return isValidSource(path, validExtensions: Sources.validExtensions) | ||
} | ||
|
||
func isValidSource(path: String, validExtensions: Set<String>) -> Bool { | ||
if path.basename.hasPrefix(".") { return false } | ||
let path = path.normpath | ||
if path == manifest.path.normpath { return false } | ||
if excludes.contains(path) { return false } | ||
return path.lowercased().hasSuffix(".swift") && path.isFile | ||
if !path.isFile { return false } | ||
guard let ext = path.fileExt else { return false } | ||
return validExtensions.contains(ext) | ||
} | ||
|
||
private func targetForName(name: String) -> Target? { | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests were failing on linux and clang suggested to recompile using this. Not sure if this is correct? @mxcl @ddunbar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is probably correct behavior, I would want to test it with Swift interop to know for sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tests in this PR calling c methods from swift.