Skip to content

Add basic support for building C via swiftpm #183

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 16 commits into from
Mar 12, 2016
Merged

Conversation

aciidgh
Copy link
Contributor

@aciidgh aciidgh commented Mar 9, 2016

Basic implementation of proposal https://github.com/apple/swift-evolution/blob/master/proposals/0038-swiftpm-c-language-targets.md

  • Swiftpm can be used to create and build C libraries.
  • Same rules apply for layouts except that include/module.modulemap should be present for each C lang module
  • If a dir contains .swift and .c, it will error out.
  • Right now only creates shared libraries and not executable.
  • Assumes module.modulemap will be available inside a include directory, doesn't generates it automatically.
  • Adds Test cases for possible layouts and mixing of swift and c code.

Remaining features will be added incrementally.

@@ -0,0 +1,6 @@
int foo() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Clang' is spelled with a lowercase 'l'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I wanted to name it CModules but CModules refers to system libraries having a modulemaps as a wrapper. so I changed it to C Lang which spelled CLang lol, but anyway Clang would make perfect sense here too. Changing.

@aciidgh aciidgh force-pushed the c_support branch 3 times, most recently from 2e79aec to a65e1b1 Compare March 9, 2016 17:35
@aciidgh
Copy link
Contributor Author

aciidgh commented Mar 10, 2016

@mxcl @ddunbar would love inputs on this

@mxcl
Copy link
Contributor

mxcl commented Mar 10, 2016

Looks like a great start and really: great job getting this going.

I'll review it more thoroughly in the morning.

@ddunbar
Copy link
Contributor

ddunbar commented Mar 10, 2016

OMG awesome! Will review ASAP

@ddunbar
Copy link
Contributor

ddunbar commented Mar 10, 2016

Overall seems like a great start to me. There are a couple things I'd like to see fixed before merge (mostly having to do with erroring out on unsupported things, or not erroring out on things which do work but aren't yet complete (like not require the module.modulemap to be written, even though we don't synthesize it yet)), but once @mxcl is happy with it I'd be glad to see it go in and then build in the other things like module map synthesis or incremental build support.

@aciidgh
Copy link
Contributor Author

aciidgh commented Mar 10, 2016

@ddunbar I think I was able to resolve almost all of the things pointed by you. You can re-review the changes here: https://github.com/apple/swift-package-manager/pull/183/files


var args: [String] = []
#if os(Linux)
args += ["-fPIC"]
Copy link
Contributor Author

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

Copy link
Contributor

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.

On Mar 10, 2016, at 11:04 AM, Ankit Agarwal [email protected] wrote:

In Sources/Build/describe().swift #183 (comment):

  •    //if it not present
    
  •    if !module.moduleMapPath.isFile {
    
  •        try mkdir(module.moduleMapPath.parentDirectory)
    
  •        try fopen(module.moduleMapPath, mode: .Write) { fp in
    
  •            try fputs("\n", fp)
    
  •        }
    
  •    }
    
  •    let inputs = module.dependencies.map{ $0.targetName } + module.sources.paths
    
  •    let productPath = Path.join(prefix, "lib(module.c99name).so")
    
  •    let wd = Path.join(prefix, "(module.c99name).build")
    
  •    mkdirs.insert(wd)
    
  •    var args: [String] = []
    
  • #if os(Linux)
  •     args += ["-fPIC"]
    
    Tests were failing on linux and clang suggested to recompile using this. Not sure if this is correct? @mxcl https://github.com/mxcl @ddunbar https://github.com/ddunbar

    Reply to this email directly or view it on GitHub https://github.com/apple/swift-package-manager/pull/183/files#r55731599.

Copy link
Contributor Author

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.

@aciidgh
Copy link
Contributor Author

aciidgh commented Mar 10, 2016

Also I am waiting for a new toolchain to arrive before I resolve conflicts. Its just one small method rename I think.

@aciidgh
Copy link
Contributor Author

aciidgh commented Mar 11, 2016

I succeeded in building toolchain from latest masters and then use that for developing this. Now rebased correctly. 😬

@aciidgh
Copy link
Contributor Author

aciidgh commented Mar 11, 2016

@mxcl CI on this? though probably review first.

@ddunbar
Copy link
Contributor

ddunbar commented Mar 11, 2016

Let's try it.

@swift-ci please test

mxcl added a commit that referenced this pull request Mar 12, 2016
Add basic support for building C via swiftpm
@mxcl mxcl merged commit 57c869a into swiftlang:master Mar 12, 2016
@mxcl
Copy link
Contributor

mxcl commented Mar 12, 2016

A key point in our evolution, many thanks everyone 👊🏻

@ddunbar
Copy link
Contributor

ddunbar commented Mar 12, 2016

Woot!

@aciidgh
Copy link
Contributor Author

aciidgh commented Mar 12, 2016

😱😱 didn't expect a merge so soon. Thanks! It was fun doing this. Will try adding other remaining things from proposal😀

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) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check the basename.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raised #199 for this

@aciidgh aciidgh deleted the c_support branch April 1, 2016 09:11
aciidgh pushed a commit to aciidgh/swift-package-manager that referenced this pull request Jan 11, 2019
Fix Windows build with posix specific stat APIs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants