Use a SPM Package in a project compiled with cmake

Hi there!

I am working on a mixed-language C++/Swift Project and I want to use a package (Vapor) from SPM. Since this is a mixed-language project, I am currently using cmake for the build process. Thus my question: How can I include Vapor inside the cmake project so that it is a dependeny for the swift target there?

Thanks in advance

PS: I tried switching just using a Package.swift to build my project with SPM, but that does not really work for the C++ part. Would love it, though.

Package.swift with local binary target for the c++ part, cmake builds the binary then calls swift build.

Or if separate people will be working on the c++ and swift parts then separate repos and then the swift part can just use spm and download the prebuilt binary.

If your project is similar to the one I worked on, as in 50MB lib and 1.5GB dsym per platform for the c++ part, you might want an option to only download the lib for development. (Assuming multiple releases of the C++ lib per week)

Thanks for the answer.

I have now configured cmake to launch swift build in a custom_target. How do I then point the swift build to the libraries built by cmake?

I have tried:

.binaryTarget(
            name: "<name>",
            path: "out/build/debug-x86-64-unknown-linux-gnu/<name>/"),

results in: unsupported extension for binary target '<name>'; valid extensions are: 'zip', 'xcframework', 'artifactbundle'

And:
add_custom_target(SwiftPackage

find_program(SWIFT NAMES "swift")
find_program(CLANG NAMES "clang") # I am using GCC, which does not work for some swift packages
find_program(CLANGXX NAMES "clang++")

  COMMAND
    "CC=${CLANG}" "CXX=${CLANGXX}"
    ${SWIFT} "build"
    --package-path ${CMAKE_CURRENT_SOURCE_DIR}
    -I${CMAKE_CURRENT_BINARY_DIR}/<name>
    -L${CMAKE_CURRENT_BINARY_DIR}/<name>
    --configuration debug
)
add_dependencies(SwiftPackage ${TARGETS})

which results in an error saying that cmake does not find config?
error: Missing value for '-c <configuration>'

(This seems strange, since I've added the configuration flag alright)

FYI: I am on Linux

Found a solution:
add "-Xswiftc" in front oft the imports. Why a simple "-I" does not suffice, however, is still a mystery to me.

-Xswiftc -I${CMAKE_CURRENT_BINARY_DIR}/Sources/<name>
-Xswiftc -I${CMAKE_CURRENT_BINARY_DIR}/Sources/<name>

So that the it is clear that which option is for which compiler/linker.

-Xcc Pass flag through to all C compiler invocations
-Xswiftc Pass flag through to all Swift compiler invocations
-Xlinker Pass flag through to all linker invocations
-Xcxx Pass flag through to all C++ compiler invocations

1 Like