Importing C system libraries


(Kelvin Ma) #1

Idk if this has been asked before, but is there a way to import C libraries
into a Swift project without creating a local git repo? Preferably
something similar to C where you can just `#include` headers and then
specify the link flags (in Package.swift?)

It’s getting very cumbersome to make a bunch of empty git repos just to use
libglfw or libcairo.


(Michael Ilseman) #2

Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

···

On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org> wrote:

Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)

It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


#3

Is it just me, or is Swift moving to much in a command line direction since the open sourcing? I feel being left behind as an Xcode user...

Jan

···

On 27 Mar 2017, at 22:59, Michael Ilseman via swift-users <swift-users@swift.org> wrote:

Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org> wrote:

Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)

It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Kelvin Ma) #4

Can you give an example?

···

On Mon, Mar 27, 2017 at 3:59 PM, Michael Ilseman <milseman@apple.com> wrote:

Sure. At a low level, you can create a module.map file and use -L/-l flags
in your invocation of Swift. If you want to do so at a higher level, then
perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

> On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users < > swift-users@swift.org> wrote:
>
> Idk if this has been asked before, but is there a way to import C
libraries into a Swift project without creating a local git repo?
Preferably something similar to C where you can just `#include` headers and
then specify the link flags (in Package.swift?)
>
> It’s getting very cumbersome to make a bunch of empty git repos just to
use libglfw or libcairo.
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users


(Michael Ilseman) #5

I don’t know what you’re referring to, but my solution also works for pre-open-source versions of Swift.

···

On Mar 27, 2017, at 2:10 PM, Jan Neumüller <nasan@slayers.de> wrote:

Is it just me, or is Swift moving to much in a command line direction since the open sourcing? I feel being left behind as an Xcode user...

Jan

On 27 Mar 2017, at 22:59, Michael Ilseman via swift-users <swift-users@swift.org> wrote:

Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org> wrote:

Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)

It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Josh Parmenter) #6

Is it just me, or is Swift moving to much in a command line direction since the open sourcing? I feel being left behind as an Xcode user...

Jan

you can specify flags in Xcode - Xcode basically just wraps around command line tools anyways for the most part (when it comes to compiling)
Best,
Josh

···

On Mar 27, 2017, at 2:10 PM, Jan Neumüller via swift-users <swift-users@swift.org> wrote:

On 27 Mar 2017, at 22:59, Michael Ilseman via swift-users <swift-users@swift.org> wrote:

Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org> wrote:

Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)

It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Josh Parmenter) #7

Is it just me, or is Swift moving to much in a command line direction since the open sourcing? I feel being left behind as an Xcode user...

Jan

you can specify flags in Xcode - Xcode basically just wraps around command line tools anyways for the most part (when it comes to compiling)
Best,
Josh

···

On Mar 27, 2017, at 2:10 PM, Jan Neumüller via swift-users <swift-users@swift.org> wrote:

On 27 Mar 2017, at 22:59, Michael Ilseman via swift-users <swift-users@swift.org> wrote:

Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org> wrote:

Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)

It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Kelvin Ma) #8

CLI’s are nothing to be afraid of :slight_smile: tbh I find XCode much more difficult
to work with than files and command line invocations.

···

On Mon, Mar 27, 2017 at 4:16 PM, Michael Ilseman <milseman@apple.com> wrote:

I don’t know what you’re referring to, but my solution also works for
pre-open-source versions of Swift.

> On Mar 27, 2017, at 2:10 PM, Jan Neumüller <nasan@slayers.de> wrote:
>
> Is it just me, or is Swift moving to much in a command line direction
since the open sourcing? I feel being left behind as an Xcode user...
>
> Jan
>
>> On 27 Mar 2017, at 22:59, Michael Ilseman via swift-users < > swift-users@swift.org> wrote:
>>
>> Sure. At a low level, you can create a module.map file and use -L/-l
flags in your invocation of Swift. If you want to do so at a higher level,
then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.
>>
>>
>>> On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users < > swift-users@swift.org> wrote:
>>>
>>> Idk if this has been asked before, but is there a way to import C
libraries into a Swift project without creating a local git repo?
Preferably something similar to C where you can just `#include` headers and
then specify the link flags (in Package.swift?)
>>>
>>> It’s getting very cumbersome to make a bunch of empty git repos just
to use libglfw or libcairo.
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users@swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>> _______________________________________________
>> swift-users mailing list
>> swift-users@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
>


(Michael Ilseman) #9

Sure! In this example, I have built libgit2. I have a directory called Git, and inside that I have the following module map:

module Git [system] {
       header "<my path>/libgit2/include/git2.h"
       export *
}

When I run, I use:

swift -I <path-to-“Git”-directory> -L <path-to-built-libgit2> -lgit2 foo.swift

inside foo.swift I can:

import Git
// … use libGit2

Read more about how to write a more appropriate module.map file for your purposes at https://clang.llvm.org/docs/Modules.html. For example, you might be able to define link flags inside the module.map, use umbrella directories, submodules, etc.

···

On Mar 28, 2017, at 6:27 AM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

Can you give an example?

On Mon, Mar 27, 2017 at 3:59 PM, Michael Ilseman <milseman@apple.com <mailto:milseman@apple.com>> wrote:
Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

> On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
>
> Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)
>
> It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org <mailto:swift-users@swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users


(Rien) #10

I feel your pain :wink:

Just embrace the dark side, it takes a little time to get used to, but chances are you won’t regret it.

Btw: I still do my development in Xcode, its just that using the SPM (Swift Package Manager) and git from the command line gives a whole extra dimension to my productivity.

I use a large monitor and have two terminal windows open at all times on the left side, and Xcode open on the right. Two monitors would be even better (I think).
Xcode is really amazing: when I regenerate the project in a terminal, there is no need to close and reopen xcode. Xcode will collapse the navigator, but otherwise it just refreshes with the new content. Its quite neat to work this way. The only two drawbacks that I have detected so far is that I need to “clean” more in Xcode, and that the old project settings are overwritten, thus if you do a lot of tweaking of the build settings this might not work out all that well.

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl

···

On 27 Mar 2017, at 23:10, Jan Neumüller via swift-users <swift-users@swift.org> wrote:

Is it just me, or is Swift moving to much in a command line direction since the open sourcing? I feel being left behind as an Xcode user...

Jan

On 27 Mar 2017, at 22:59, Michael Ilseman via swift-users <swift-users@swift.org> wrote:

Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org> wrote:

Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)

It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Joseph Heck) #11

Given the effort behind wrapping all of the functionality in swift package manager into an API (libPackageManager) so that in the future it can be used by IDE style tools, I don't think you need to be seriously concerned. It's convenient for the swift package folks, especially while supporting Linux (which doesn't have the Xcode IDE) to utilize the CLI, and it embeds nicely into Xcode build scripts as an interim step (and there's a long history of that with clang, llbuild, etc).

I wouldn't call libPackageManager a stable API (and I suspect the SwiftPM team wouldn't either), but it's the right structure to support future interactions and IDE support - Xcode or other projects that want to leverage it.

- joe

···

On Mar 27, 2017, at 2:10 PM, Jan Neumüller via swift-build-dev <swift-build-dev@swift.org> wrote:

Is it just me, or is Swift moving to much in a command line direction since the open sourcing? I feel being left behind as an Xcode user...

Jan

On 27 Mar 2017, at 22:59, Michael Ilseman via swift-users <swift-users@swift.org> wrote:

Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org> wrote:

Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)

It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-build-dev mailing list
swift-build-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-build-dev


(David Sweeris) #12

I feel your pain :wink:

Just embrace the dark side, it takes a little time to get used to, but chances are you won’t regret it.

Btw: I still do my development in Xcode, its just that using the SPM (Swift Package Manager) and git from the command line gives a whole extra dimension to my productivity.

I use a large monitor and have two terminal windows open at all times on the left side, and Xcode open on the right. Two monitors would be even better (I think).

Since you mentioned it… :smiley:

Using two monitors is a whole new world, and I cannot recommend it highly enough. Three is an improvement over two, but not by nearly the same margin as two is to one. Four is, well, I suppose better than leaving a monitor laying around and not doing anything… I've tried stretching my Xcode window really tall, but the bezels are too disruptive for me. I’ve heard more than one person claim that 1x 21:9 monitor is just as good as 2x 16:9/16:10 monitors (maybe better, because the two “screens” don’t have to be the same size anymore), but I haven’t had a chance to try one out yet.

Xcode is really amazing: when I regenerate the project in a terminal, there is no need to close and reopen xcode. Xcode will collapse the navigator, but otherwise it just refreshes with the new content. Its quite neat to work this way. The only two drawbacks that I have detected so far is that I need to “clean” more in Xcode, and that the old project settings are overwritten, thus if you do a lot of tweaking of the build settings this might not work out all that well.

Yeah, if Xcode were open-source, I’d be working on SwiftPM integration. Hmm… Maybe SwiftPM can detect if a .xcodeproj file already exists and copy any custom settings from there before writing the new one? That might not be too hard, depending on how complicated those files are.

- Dave Sweeris

···

On Mar 28, 2017, at 1:58 AM, Rien via swift-build-dev <swift-build-dev@swift.org> wrote:


(Kelvin Ma) #13

How do I compile a project with many modules? My tree looks like this:

···

On Tue, Mar 28, 2017 at 12:47 PM, Michael Ilseman <milseman@apple.com> wrote:

Sure! In this example, I have built libgit2. I have a directory called
Git, and inside that I have the following module map:

module Git [system] {
       header "<my path>/libgit2/include/git2.h"
       export *
}

When I run, I use:

swift -I <path-to-“Git”-directory> -L <path-to-built-libgit2> -lgit2
foo.swift

inside foo.swift I can:

import Git
// … use libGit2

Read more about how to write a more appropriate module.map file for your
purposes at https://clang.llvm.org/docs/Modules.html. For example, you
might be able to define link flags inside the module.map, use umbrella
directories, submodules, etc.

On Mar 28, 2017, at 6:27 AM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

Can you give an example?

On Mon, Mar 27, 2017 at 3:59 PM, Michael Ilseman <milseman@apple.com> > wrote:

Sure. At a low level, you can create a module.map file and use -L/-l
flags in your invocation of Swift. If you want to do so at a higher level,
then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

> On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users < >> swift-users@swift.org> wrote:
>
> Idk if this has been asked before, but is there a way to import C
libraries into a Swift project without creating a local git repo?
Preferably something similar to C where you can just `#include` headers and
then specify the link flags (in Package.swift?)
>
> It’s getting very cumbersome to make a bunch of empty git repos just to
use libglfw or libcairo.
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users


(Michael Ilseman) #14

This is into uncharted territory for me, but it seems you’re building with SwiftPM. You’ll probably want to configure extra compiler flags if that’s possible. You could also bite the bullet and build your C libraries with SwiftPM as well. Hopefully someone on swift-build-dev can help you out.

CC-ing Ankit

···

On Mar 28, 2017, at 5:09 PM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

How do I compile a project with many modules? My tree looks like this:

<Selection_001.png>

On Tue, Mar 28, 2017 at 12:47 PM, Michael Ilseman <milseman@apple.com <mailto:milseman@apple.com>> wrote:
Sure! In this example, I have built libgit2. I have a directory called Git, and inside that I have the following module map:

module Git [system] {
       header "<my path>/libgit2/include/git2.h"
       export *
}

When I run, I use:

swift -I <path-to-“Git”-directory> -L <path-to-built-libgit2> -lgit2 foo.swift

inside foo.swift I can:

import Git
// … use libGit2

Read more about how to write a more appropriate module.map file for your purposes at https://clang.llvm.org/docs/Modules.html. For example, you might be able to define link flags inside the module.map, use umbrella directories, submodules, etc.

On Mar 28, 2017, at 6:27 AM, Kelvin Ma <kelvinsthirteen@gmail.com <mailto:kelvinsthirteen@gmail.com>> wrote:

Can you give an example?

On Mon, Mar 27, 2017 at 3:59 PM, Michael Ilseman <milseman@apple.com <mailto:milseman@apple.com>> wrote:
Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

> On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
>
> Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)
>
> It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org <mailto:swift-users@swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users


(Kelvin Ma) #15

I can only fit one monitor in my dorm and it’s honestly tragic…

···

On Tue, Mar 28, 2017 at 2:58 PM, David Sweeris <davesweeris@mac.com> wrote:

On Mar 28, 2017, at 1:58 AM, Rien via swift-build-dev < > swift-build-dev@swift.org> wrote:

I feel your pain :wink:

Just embrace the dark side, it takes a little time to get used to, but
chances are you won’t regret it.

Btw: I still do my development in Xcode, its just that using the SPM
(Swift Package Manager) and git from the command line gives a whole extra
dimension to my productivity.

I use a large monitor and have two terminal windows open at all times on
the left side, and Xcode open on the right. Two monitors would be even
better (I think).

Since you mentioned it… :smiley:

Using two monitors is a whole new world, and I cannot recommend it highly
enough. Three is an improvement over two, but not by nearly the same margin
as two is to one. Four is, well, I suppose better than leaving a monitor
laying around and not doing anything… I've tried stretching my Xcode window
really tall, but the bezels are too disruptive for me. I’ve heard more than
one person claim that 1x 21:9 monitor is just as good as 2x 16:9/16:10
monitors (maybe better, because the two “screens” don’t have to be the same
size anymore), but I haven’t had a chance to try one out yet.

Xcode is really amazing: when I regenerate the project in a terminal,
there is no need to close and reopen xcode. Xcode will collapse the
navigator, but otherwise it just refreshes with the new content. Its quite
neat to work this way. The only two drawbacks that I have detected so far
is that I need to “clean” more in Xcode, and that the old project settings
are overwritten, thus if you do a lot of tweaking of the build settings
this might not work out all that well.

Yeah, if Xcode were open-source, I’d be working on SwiftPM integration.
Hmm… Maybe SwiftPM can detect if a .xcodeproj file already exists and copy
any custom settings from there before writing the new one? That might not
be too hard, depending on how complicated those files are.

- Dave Sweeris


(Kelvin Ma) #16

see idk how to include the libraries and still make them Importable. And dumping the library sources into the project dir would kind of hamper source distribution…

···

On Mar 28, 2017, at 7:36 PM, Michael Ilseman <milseman@apple.com> wrote:

This is into uncharted territory for me, but it seems you’re building with SwiftPM. You’ll probably want to configure extra compiler flags if that’s possible. You could also bite the bullet and build your C libraries with SwiftPM as well. Hopefully someone on swift-build-dev can help you out.

CC-ing Ankit

On Mar 28, 2017, at 5:09 PM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

How do I compile a project with many modules? My tree looks like this:

<Selection_001.png>

On Tue, Mar 28, 2017 at 12:47 PM, Michael Ilseman <milseman@apple.com> wrote:
Sure! In this example, I have built libgit2. I have a directory called Git, and inside that I have the following module map:

module Git [system] {
       header "<my path>/libgit2/include/git2.h"
       export *
}

When I run, I use:

swift -I <path-to-“Git”-directory> -L <path-to-built-libgit2> -lgit2 foo.swift

inside foo.swift I can:

import Git
// … use libGit2

Read more about how to write a more appropriate module.map file for your purposes at https://clang.llvm.org/docs/Modules.html. For example, you might be able to define link flags inside the module.map, use umbrella directories, submodules, etc.

On Mar 28, 2017, at 6:27 AM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

Can you give an example?

On Mon, Mar 27, 2017 at 3:59 PM, Michael Ilseman <milseman@apple.com> wrote:
Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

> On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org> wrote:
>
> Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)
>
> It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users


(Kelvin Ma) #17

After a lot of wasted effort I’ve found that for some reason only the SPM
is capable of linking Swift code with Linux’s system libraries which are
all compiled with gcc, which apparently doesn’t get along too well with
clang.

All I want is to be able to import a system library without making an empty
git repository…

What I want to do:

main.swift:
    import <zlib.h>

What I have to do instead:

    ~$ cd ../CZlib
    ~$ git add -A
    ~$ git commit -m "who cares"
    ~$ git tag 1.0.0
    ~$ cd ../My_actual_project
    ~$ rm .build -r
    ~$ swift build
    ~$ cd ../CZlib
    ~$ git tag 1.0.0 --force
    ~$ cd ../My_actual_project
    ~$ rm .build -r
    ~$ swift build

···

On Tue, Mar 28, 2017 at 7:36 PM, Michael Ilseman <milseman@apple.com> wrote:

This is into uncharted territory for me, but it seems you’re building with
SwiftPM. You’ll probably want to configure extra compiler flags if that’s
possible. You could also bite the bullet and build your C libraries with
SwiftPM as well. Hopefully someone on swift-build-dev can help you out.

CC-ing Ankit

On Mar 28, 2017, at 5:09 PM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

How do I compile a project with many modules? My tree looks like this:

<Selection_001.png>

On Tue, Mar 28, 2017 at 12:47 PM, Michael Ilseman <milseman@apple.com> > wrote:

Sure! In this example, I have built libgit2. I have a directory called
Git, and inside that I have the following module map:

module Git [system] {
       header "<my path>/libgit2/include/git2.h"
       export *
}

When I run, I use:

swift -I <path-to-“Git”-directory> -L <path-to-built-libgit2> -lgit2
foo.swift

inside foo.swift I can:

import Git
// … use libGit2

Read more about how to write a more appropriate module.map file for your
purposes at https://clang.llvm.org/docs/Modules.html. For example, you
might be able to define link flags inside the module.map, use umbrella
directories, submodules, etc.

On Mar 28, 2017, at 6:27 AM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

Can you give an example?

On Mon, Mar 27, 2017 at 3:59 PM, Michael Ilseman <milseman@apple.com> >> wrote:

Sure. At a low level, you can create a module.map file and use -L/-l
flags in your invocation of Swift. If you want to do so at a higher level,
then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

> On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users < >>> swift-users@swift.org> wrote:
>
> Idk if this has been asked before, but is there a way to import C
libraries into a Swift project without creating a local git repo?
Preferably something similar to C where you can just `#include` headers and
then specify the link flags (in Package.swift?)
>
> It’s getting very cumbersome to make a bunch of empty git repos just
to use libglfw or libcairo.
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users


(Ankit Aggarwal) #18

Hi,

Apologies for not replying to this earlier.

You can have multiple targets in a single package. Each target can either
be Swift or C-family. The type of target is determined by the sources
contained in it (*.c/*.cpp etc means C target, *.swift means Swift target).
So if you want to create multiple C targets, this layout should work:

Package.swift
Sources/
    Bitmap
    Cubify
    Cairo/anchor.c <---- This is just an empty file to tell SwiftPM that
this is a C target.
    Cairo/include/Cairo.h
    Cairo/include/module.modulemap
    GLFW/anchor.c
    GLFW/include/GLFW.h
    GLFW/include/module.modulemap

The modulemap is automatically generated, if not provided. This is a
package which contains two targets (one C and one Swift):
https://github.com/jpsim/Yams

If you need to pass a bunch of compiler flags, you can use SwiftPM's
pkgConfig feature but that will require you to have a separate repository
for Cario and GLFW. You can experiment without creating tags using the edit
feature
<https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#editable-packages>
.

PS: You can join SwiftPM slack channel for quicker turn around time:
https://lists.swift.org/pipermail/swift-build-dev/Week-of-Mon-20160530/000497.html

Thanks,
Ankit

···

On Wed, Mar 29, 2017 at 6:06 AM, Michael Ilseman via swift-build-dev < swift-build-dev@swift.org> wrote:

This is into uncharted territory for me, but it seems you’re building with
SwiftPM. You’ll probably want to configure extra compiler flags if that’s
possible. You could also bite the bullet and build your C libraries with
SwiftPM as well. Hopefully someone on swift-build-dev can help you out.

CC-ing Ankit

On Mar 28, 2017, at 5:09 PM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

How do I compile a project with many modules? My tree looks like this:

<Selection_001.png>

On Tue, Mar 28, 2017 at 12:47 PM, Michael Ilseman <milseman@apple.com> > wrote:

Sure! In this example, I have built libgit2. I have a directory called
Git, and inside that I have the following module map:

module Git [system] {
       header "<my path>/libgit2/include/git2.h"
       export *
}

When I run, I use:

swift -I <path-to-“Git”-directory> -L <path-to-built-libgit2> -lgit2
foo.swift

inside foo.swift I can:

import Git
// … use libGit2

Read more about how to write a more appropriate module.map file for your
purposes at https://clang.llvm.org/docs/Modules.html. For example, you
might be able to define link flags inside the module.map, use umbrella
directories, submodules, etc.

On Mar 28, 2017, at 6:27 AM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

Can you give an example?

On Mon, Mar 27, 2017 at 3:59 PM, Michael Ilseman <milseman@apple.com> >> wrote:

Sure. At a low level, you can create a module.map file and use -L/-l
flags in your invocation of Swift. If you want to do so at a higher level,
then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

> On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users < >>> swift-users@swift.org> wrote:
>
> Idk if this has been asked before, but is there a way to import C
libraries into a Swift project without creating a local git repo?
Preferably something similar to C where you can just `#include` headers and
then specify the link flags (in Package.swift?)
>
> It’s getting very cumbersome to make a bunch of empty git repos just
to use libglfw or libcairo.
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-build-dev mailing list
swift-build-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-build-dev


(Kelvin Ma) #19

I will try this, but why are the header files inside the Sources directory?
System headers should live in /usr/include…

···

On Tue, Mar 28, 2017 at 11:48 PM, Ankit Aggarwal <ankit_aggarwal@apple.com> wrote:

Hi,

Apologies for not replying to this earlier.

You can have multiple targets in a single package. Each target can either
be Swift or C-family. The type of target is determined by the sources
contained in it (*.c/*.cpp etc means C target, *.swift means Swift target).
So if you want to create multiple C targets, this layout should work:

Package.swift
Sources/
    Bitmap
    Cubify
    Cairo/anchor.c <---- This is just an empty file to tell SwiftPM that
this is a C target.
    Cairo/include/Cairo.h
    Cairo/include/module.modulemap
    GLFW/anchor.c
    GLFW/include/GLFW.h
    GLFW/include/module.modulemap

The modulemap is automatically generated, if not provided. This is a
package which contains two targets (one C and one Swift):
https://github.com/jpsim/Yams

If you need to pass a bunch of compiler flags, you can use SwiftPM's
pkgConfig feature but that will require you to have a separate repository
for Cario and GLFW. You can experiment without creating tags using the edit
feature
<https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#editable-packages>
.

PS: You can join SwiftPM slack channel for quicker turn around time:
https://lists.swift.org/pipermail/swift-build-dev/
Week-of-Mon-20160530/000497.html

Thanks,
Ankit

On Wed, Mar 29, 2017 at 6:06 AM, Michael Ilseman via swift-build-dev < > swift-build-dev@swift.org> wrote:

This is into uncharted territory for me, but it seems you’re building
with SwiftPM. You’ll probably want to configure extra compiler flags if
that’s possible. You could also bite the bullet and build your C libraries
with SwiftPM as well. Hopefully someone on swift-build-dev can help you out.

CC-ing Ankit

On Mar 28, 2017, at 5:09 PM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

How do I compile a project with many modules? My tree looks like this:

<Selection_001.png>

On Tue, Mar 28, 2017 at 12:47 PM, Michael Ilseman <milseman@apple.com> >> wrote:

Sure! In this example, I have built libgit2. I have a directory called
Git, and inside that I have the following module map:

module Git [system] {
       header "<my path>/libgit2/include/git2.h"
       export *
}

When I run, I use:

swift -I <path-to-“Git”-directory> -L <path-to-built-libgit2> -lgit2
foo.swift

inside foo.swift I can:

import Git
// … use libGit2

Read more about how to write a more appropriate module.map file for your
purposes at https://clang.llvm.org/docs/Modules.html. For example, you
might be able to define link flags inside the module.map, use umbrella
directories, submodules, etc.

On Mar 28, 2017, at 6:27 AM, Kelvin Ma <kelvinsthirteen@gmail.com> >>> wrote:

Can you give an example?

On Mon, Mar 27, 2017 at 3:59 PM, Michael Ilseman <milseman@apple.com> >>> wrote:

Sure. At a low level, you can create a module.map file and use -L/-l
flags in your invocation of Swift. If you want to do so at a higher level,
then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

> On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users < >>>> swift-users@swift.org> wrote:
>
> Idk if this has been asked before, but is there a way to import C
libraries into a Swift project without creating a local git repo?
Preferably something similar to C where you can just `#include` headers and
then specify the link flags (in Package.swift?)
>
> It’s getting very cumbersome to make a bunch of empty git repos just
to use libglfw or libcairo.
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-build-dev mailing list
swift-build-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-build-dev


(Ankit Aggarwal) #20

In this case, these are just umbrella headers. If your modulemap contains absolute path to the header, then you don't need the header files, but SwiftPM will probably warn about this. Note that this is a "hack" to have system packages inside a single repository. The correct way is to have system package as a separate published package which you only need to do once.

···

On 29-Mar-2017, at 10:26 AM, Kelvin Ma <kelvinsthirteen@gmail.com> wrote:

I will try this, but why are the header files inside the Sources directory? System headers should live in /usr/include…

On Tue, Mar 28, 2017 at 11:48 PM, Ankit Aggarwal <ankit_aggarwal@apple.com <mailto:ankit_aggarwal@apple.com>> wrote:
Hi,

Apologies for not replying to this earlier.

You can have multiple targets in a single package. Each target can either be Swift or C-family. The type of target is determined by the sources contained in it (*.c/*.cpp etc means C target, *.swift means Swift target). So if you want to create multiple C targets, this layout should work:

Package.swift
Sources/
    Bitmap
    Cubify
    Cairo/anchor.c <---- This is just an empty file to tell SwiftPM that this is a C target.
    Cairo/include/Cairo.h
    Cairo/include/module.modulemap
    GLFW/anchor.c
    GLFW/include/GLFW.h
    GLFW/include/module.modulemap

The modulemap is automatically generated, if not provided. This is a package which contains two targets (one C and one Swift): https://github.com/jpsim/Yams

If you need to pass a bunch of compiler flags, you can use SwiftPM's pkgConfig feature but that will require you to have a separate repository for Cario and GLFW. You can experiment without creating tags using the edit feature <https://github.com/apple/swift-package-manager/blob/master/Documentation/Usage.md#editable-packages>.

PS: You can join SwiftPM slack channel for quicker turn around time: https://lists.swift.org/pipermail/swift-build-dev/Week-of-Mon-20160530/000497.html

Thanks,
Ankit

On Wed, Mar 29, 2017 at 6:06 AM, Michael Ilseman via swift-build-dev <swift-build-dev@swift.org <mailto:swift-build-dev@swift.org>> wrote:
This is into uncharted territory for me, but it seems you’re building with SwiftPM. You’ll probably want to configure extra compiler flags if that’s possible. You could also bite the bullet and build your C libraries with SwiftPM as well. Hopefully someone on swift-build-dev can help you out.

CC-ing Ankit

On Mar 28, 2017, at 5:09 PM, Kelvin Ma <kelvinsthirteen@gmail.com <mailto:kelvinsthirteen@gmail.com>> wrote:

How do I compile a project with many modules? My tree looks like this:

<Selection_001.png>

On Tue, Mar 28, 2017 at 12:47 PM, Michael Ilseman <milseman@apple.com <mailto:milseman@apple.com>> wrote:
Sure! In this example, I have built libgit2. I have a directory called Git, and inside that I have the following module map:

module Git [system] {
       header "<my path>/libgit2/include/git2.h"
       export *
}

When I run, I use:

swift -I <path-to-“Git”-directory> -L <path-to-built-libgit2> -lgit2 foo.swift

inside foo.swift I can:

import Git
// … use libGit2

Read more about how to write a more appropriate module.map file for your purposes at https://clang.llvm.org/docs/Modules.html. For example, you might be able to define link flags inside the module.map, use umbrella directories, submodules, etc.

On Mar 28, 2017, at 6:27 AM, Kelvin Ma <kelvinsthirteen@gmail.com <mailto:kelvinsthirteen@gmail.com>> wrote:

Can you give an example?

On Mon, Mar 27, 2017 at 3:59 PM, Michael Ilseman <milseman@apple.com <mailto:milseman@apple.com>> wrote:
Sure. At a low level, you can create a module.map file and use -L/-l flags in your invocation of Swift. If you want to do so at a higher level, then perhaps SwiftPM can. CCing swift-build-dev for the SwiftPM part.

> On Mar 26, 2017, at 3:20 PM, Kelvin Ma via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
>
> Idk if this has been asked before, but is there a way to import C libraries into a Swift project without creating a local git repo? Preferably something similar to C where you can just `#include` headers and then specify the link flags (in Package.swift?)
>
> It’s getting very cumbersome to make a bunch of empty git repos just to use libglfw or libcairo.
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org <mailto:swift-users@swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-build-dev mailing list
swift-build-dev@swift.org <mailto:swift-build-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-build-dev