I hava a Swift Package with an executable target, the project structure looks like this:
MySwiftPackage
├── Package.swift
├── Sources
│ └── MySwiftPackage
│ ├── SwiftBridgeCore.swift
│ ├── main.swift
│ └── my_rust_lib.swift
├── bridging-header.h
├── file.json
├── generated
│ ├── SwiftBridgeCore.h
│ └── my_rust_lib
│ └── my_rust_lib.h
└── lib_mac
└── libmy_rust_lib.a
This package needs to link to the static library lib_mac/libmy_rust_lib.a
and links with the bridging-header.h
.
The bridging-header.h
just has some #include
to the headers in generated
.
To build this project, I run
swiftc -L lib_mac -lmy_rust_lib \
-import-objc-header bridging-header.h \
Sources/LinuxSwiftPackage/*.swift
This will build an executable I can run. I now want to accomplish the same this using swift build
, so that I can add dependencies.
I have tried the following:
1. In my Package.swift
, I added the following:
let package = Package(
name: "MySwiftPackage",
dependencies: [],
targets: [
.executableTarget(
name: "MySwiftPackage",
dependencies: [],
cSettings: [
.headerSearchPath(".")
],
linkerSettings: [
.linkedLibrary(":lib_mac/libmy_rust_lib.a")
]
)
]
)
This did not link the library however, as I got a lot of cannot find type <SomeType defined in the header> in scope
2. I have added the following to Package.swift
:
let package = Package(
name: "MySwiftPackage",
dependencies: [],
targets: [
.executableTarget(
name: "MySwiftPackage",
dependencies: [],
linkerSettings: [
.unsafeFlags(["-L lib_mac", "-lmy_rust_lib", "-import-objc-header bridging-header.h"])
]
)
]
)
This would yield the same errors.
I also tried swiftSettings
instead of linkerSettings
, this would give me the following error: ld: symbol(s) not found for architecture x86_64
3. I have tried the following:
file.json
{
"version": 1,
"sdk": "/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift",
"toolchain-bin-dir": "/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin",
"target": "x86_64-apple-macosx",
"dynamic-library-extension": "lib",
"extra-cc-flags": [
],
"extra-swiftc-flags": [
"-L",
"lib_mac",
"-lmy_rust_lib",
"-import-objc-header",
"bridging-header.h"
],
"extra-cpp-flags": []
}
with a Package.swift
that looks like this:
let package = Package(
name: "MySwiftPackage",
dependencies: [],
targets: [
.executableTarget(
name: "MySwiftPackage",
dependencies: []
)
]
)
I then ran swift build --destination file.json
, with would give me the following error:
error: no such module 'Foundation'
import Foundation
Will any of these approaches work with some changes? Or is there another approach that should work?
Thanks in advance,
Jonas
P.S. I'm using macOS (12)