Swift and FreeBSD

The package exists on freebsd, yay! I’ve even compiled some simple programs!

I can’t, however, figure out how to use 3rd party packages. I tried looking for some examples using Linux, but almost everything seemed to be about creating packages. I’m used to Xcode on macOS, obviously, so manually dealing with SwiftPM hasn’t been something I’ve done before.

Anyone have any pointers, suggestions, or willingness to just chat about some of this? (Again, I’m assuming it’s pretty similar to Linux, but I’m struggling on that front.)

1 Like

Hi, There are a few ways you can interact with SwiftPM to add new packages as a dependency.

Note:
To setup your project, if you haven’t already done so, follow these steps:

  • Open your Terminal
  • Make sure that your current Terminal is in the folder you want to work in
    • run the cd command passing in the location of your workspace
  • run swift package init

Once your project is ready you can write your code within the provided .swift file in a directory within the Sources directory.
The Package.swift file will be automatically generated and populated for you.

The way that most people do it is directly from Package.swift which you can find about here SwiftPM | Getting Started | Importing Dependencies. If you would like to learn more you can go throu.

There is also a CLI you can learn with swift package.

Further Reading:
For more information about how to interact with Packages you can read the documentation for PackageDescription

Continue reading SwiftPM | Getting Started to go through an intro to SwiftPM

Yes, I did all that. I tried using ArgumentParser as a package, but when I added dependencies: [“ArgumentParser”] to the target call, it said it couldn’t find it. It was downloading from github (I had to cheat and look at the URL in an Xcode project – I assume there’s a site with a list of packages, but I couldn’t find it?)

If you need to use ArgumentParser you can use the following command when building your project to make a CLI tool:

swift package init -type=tool

Also if you want a website to search on, you can use swiftpackageindex.com.
For example you can find ArgumentParser at swiftpackageindex.com/apple/swift-argument-parser.

Just click on “Use this Package” and it will bring up snippets for you to paste into your Package.swift

1 Like

Thank you! That tells me that what I was getting wrong was the argument; I need to use .product(name: "ArgumentParser", package: "swift-argument-parser") (and, presumably, similar for any other package).

Happy to help!

1 Like

A related question is how do I use C/C++ code from swift here? I couldn’t see how to use a bridging header. (Simple case for now: I just added foo.c to the Package.swift file, and want to call foo() from my Swift code.)

ETA: Oh, it doesn’t seem supported (yet?): error: 'swifttest': target at '/tmp/SwiftTest/Sources' contains mixed language source files; feature not supported :slight_smile:

You can follow SwiftPM | Creating C Language Targets.

You can simply create a directory called include. You can then either place Foo/Foo.h or Foo.h for your use.

1 Like

Yep pretty much you can’t mix .c and .swift files in a target with SwiftPM. If you need C or C++ code, you must have a separate target that contains that code. Here’s a resource for C++ if that’s what you need to do:

Mixing Swift and C++ | Swift.org

1 Like

Time to learn module_map I guess. :grinning_face_with_smiling_eyes:

FYI:
C and C++ are both considered C targets by SwiftPM and interact with Swift with similar effects between each other. You should still learn the particular differences between C and C++ interop.

So this seems to mean that if I wanted to have a project that used C and Swift, I would need to create different targets, and use a module map for the C code. That’s assuming straight-forward swift-calls-c.

Is that about right?

1 Like

FYI the Swift 5.10 package on FreeBSD's ports tree is not an official release and is not supported by the Swift team.

1 Like

That’s ok, I am assuming it is very much beta, since Swift 6 was the promised version for FreeBSD.

It's not even beta, it's a third-party upload. So caveat downloader.

2 Likes

Which doesn’t change the issues I’m having, since they don’t seem to be platform specific. Unlike the ones I’ve run into where some packages won’t build. I haven’t mentioned those before. :slight_smile:

Fair enough. If you're looking to add a package dependency to your package (so that code from the other package compiles alongside yours and can be imported into your Swift files), see here. I hope that's helpful!

As I said, I haven’t had to deal with SwiftPM before, since I use Xcode and it hides it all from me (and I haven’t made a package yet!), so this is all useful!

I am very much looking forward to the official freebsd release. It’s very exciting to have a good OO, type- and memory-safe language!