Code Monkey home page Code Monkey logo

swift-cmake-examples's Introduction

Swift CMake Examples

This repository contains examples for using CMake to build Swift with different project layouts, including using CMake to build pure-Swift projects, and projects that use Swift-C++ interoperability.

Requirements

The projects in this repository have been tested with the 5.7.3 Linux container, as well as with the 5.7 compiler toolchain on macOS. For some of the examples with more advanced usage, you may need a newer CMake than what is shipped by the packages in your Linux distro.

  • CMake 3.22+*
  • Ninja 1.5+
  • Swift 5.7+*

* Unless noted otherwise.

These projects build with the Ninja generator (e.g cmake -G 'Ninja' ...) and likely do not work with the other generators.

Single Executable

Directory: 1_single_executable

This project demonstrates creating a simple executable written in Swift.

Executable with Library

Directory: 2_executable_library

This project demonstrates creating a single executable that links against a Swift library.

Bi-directional Swift/C++ Interop

Directory: 3_bidirectional_cxx_interop

This project demonstrates mixing Swift and C++ in a library using the Swift/C++ interoperability in Swift 5.9. Requires CMake 3.26, Swift 5.9, and Clang 11 or newer, or Apple Clang shipped in Xcode 12 or newer.

Requires:

  • CMake 3.26
  • Swift 5.9
  • Clang 11 or Apple Clang in Xcode 12 or newer

Swift Macros

Directory: 4_swift_macros

This project demonstrates how to build a custom macro in a CMake-based project using the Swift macro support introduced in Swift 5.9.

Requires:

  • Swift 5.9 (macOS: Swift 5.9.0, Windows and Linux: Swift 5.9.1)

swift-cmake-examples's People

Contributors

etcwilde avatar torarnv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swift-cmake-examples's Issues

Example 3 broken in Xcode 15

I used cmake version 3.26.4 to generate the Xcode PingPong project. Xcode 15.0 reports build errors:

  1. in the fibonacci_swift example:
swift-cmake-examples/3_bidirectional_cxx_interop/src/fibonacci.swift:14:1: error: expressions are not allowed at the top level
print(fibonacci_cpp(5))
^
  1. in the fibonacci_cpp example:
ld: Undefined symbols:
  SwiftFibonacci.fibonacciSwift(Swift.Int32) -> Swift.Int32, referenced from:
      SwiftFibonacci::fibonacciSwift(int) in fibonacci.o

More than 1 swift file for the bidirectional scenario does not build

When adding a swift file solve.swift here in the CmakeLists.txt of the library, the generation of the bridge code fails.

FAILED: lib/fibonacci/include/fibonacci/fibonacci-swift.h /Users/geometryfactory/afabri/swift-cmake-examples/3_bidirectional_cxx_interop/build/lib/fibonacci/include/fibonacci/fibonacci-swift.h 
cd /Users/geometryfactory/afabri/swift-cmake-examples/3_bidirectional_cxx_interop/lib/fibonacci && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -frontend -typecheck -I/Users/geometryfactory/afabri/swift-cmake-examples/3_bidirectional_cxx_interop/include fibonacci.swift;solve.swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.4.sdk -module-name SwiftFibonacci -cxx-interoperability-mode=default -emit-clang-header-path /Users/geometryfactory/afabri/swift-cmake-examples/3_bidirectional_cxx_interop/build/lib/fibonacci/include/fibonacci/fibonacci-swift.h
<unknown>:0: error: unable to load standard library for target 'x86_64-apple-macosx14.0'

This also happens with the code of PR #9

Ideally I want the bridge code generator only getting applied on one of the swift files, and the other swift files are the backend code doing the real work. I think this might make a nice scenario for your repository.

Build under Windows

Hi, I wanted to build the example projects under Windows 11, Swift 5.10, installation of the toolchain via WinGet following instructions on swift.org.

Swift version 5.10 (swift-5.10-RELEASE)
Target: x86_64-unknown-windows-msvc

When I want to build 3_bidirectional_cxx_interop using

cmake -S . -B build -GNinja
cmake --build build

I get:

-- The CXX compiler identification is Clang 16.0.0 with GNU-like command-line
-- The Swift compiler identification is Apple 5.10
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Users/fpultar/AppData/Local/Programs/Swift/Toolchains/5.10.0+Asserts/usr/bin/clang++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working Swift compiler: C:/Users/fpultar/AppData/Local/Programs/Swift/Toolchains/5.10.0+Asserts/usr/bin/swiftc.exe
-- Check for working Swift compiler: C:/Users/fpultar/AppData/Local/Programs/Swift/Toolchains/5.10.0+Asserts/usr/bin/swiftc.exe - works
-- Configuring done (4.6s)
-- Generating done (0.0s)
-- Build files have been written to: C:/Users/fpultar/source/repos/test-fxp/swift-cmake-examples/3_bidirectional_cxx_interop/build

and

[4/6] Linking Swift static library lib\fibonacci\fibonacci.lib
FAILED: lib/fibonacci/fibonacci.lib lib/fibonacci/CMakeFiles/fibonacci.dir/fibonacci.swift.obj lib/fibonacci/SwiftFibonacci.swiftmodule
C:\Windows\system32\cmd.exe /C "cd . && C:\Users\fpultar\AppData\Local\Programs\Swift\Toolchains\5.10.0+Asserts\usr\bin\swiftc.exe -j 32 -num-threads 32 -emit-library -static -o lib\fibonacci\fibonacci.lib -module-name SwiftFibonacci -module-link-name fibonacci -emit-module -emit-module-path lib\fibonacci\SwiftFibonacci.swiftmodule -emit-dependencies  -Onone -g -incremental -libc MDd -cxx-interoperability-mode=default -output-file-map lib\fibonacci\CMakeFiles\fibonacci.dir\Debug\output-file-map.json -I C:/Users/fpultar/source/repos/test-fxp/swift-cmake-examples/3_bidirectional_cxx_interop/include -I C:/Users/fpultar/source/repos/test-fxp/swift-cmake-examples/3_bidirectional_cxx_interop/build/lib/fibonacci/include C:\Users\fpultar\source\repos\test-fxp\swift-cmake-examples\3_bidirectional_cxx_interop\lib\fibonacci\fibonacci.swift lib\fibonacci\CMakeFiles\fibonacci.dir\fibonacci.cpp.obj    && cd ."
error: unableToFind(tool: "link")
ninja: build stopped: subcommand failed.

I get similar errors for 2_executable_library.

Forcing building shared libraries (cmake -S . -B build -GNinja -DBUILD_SHARED_LIBS=TRUE) resolves the problem for 2_executable_library but 3_bidirectional_cxx_interop fails with:

[4/6] Linking Swift shared library lib\fibonacci\fibonacci.dll
FAILED: lib/fibonacci/fibonacci.dll lib/fibonacci/CMakeFiles/fibonacci.dir/fibonacci.swift.obj lib/fibonacci/SwiftFibonacci.swiftmodule lib/fibonacci/fibonacci.lib
C:\Windows\system32\cmd.exe /C "cd . && C:\Users\fpultar\AppData\Local\Programs\Swift\Toolchains\5.10.0+Asserts\usr\bin\swiftc.exe -j 32 -num-threads 32 -emit-library -o lib\fibonacci\fibonacci.dll -module-name SwiftFibonacci -module-link-name fibonacci -emit-module -emit-module-path lib\fibonacci\SwiftFibonacci.swiftmodule -emit-dependencies -Dfibonacci_EXPORTS -Onone -g -incremental -libc MDd -cxx-interoperability-mode=default -output-file-map lib\fibonacci\CMakeFiles\fibonacci.dir\Debug\output-file-map.json -I C:/Users/fpultar/source/repos/test-fxp/swift-cmake-examples/3_bidirectional_cxx_interop/include -I C:/Users/fpultar/source/repos/test-fxp/swift-cmake-examples/3_bidirectional_cxx_interop/build/lib/fibonacci/include C:\Users\fpultar\source\repos\test-fxp\swift-cmake-examples\3_bidirectional_cxx_interop\lib\fibonacci\fibonacci.swift lib\fibonacci\CMakeFiles\fibonacci.dir\fibonacci.cpp.obj C:\Users\fpultar\AppData\Local\Programs\Swift\Platforms\5.10.0\Windows.platform\Developer\SDKs\Windows.sdk\/usr/lib/swift/windows/x86_64/swiftrt.obj   -Xlinker -implib:lib\fibonacci\fibonacci.lib -L C:/Users/fpultar/AppData/Local/Programs/Swift/Toolchains/5.10.0+Asserts/usr/lib/swift/windows   -L C:/Users/fpultar/AppData/Local/Programs/Swift/Toolchains/5.10.0+Asserts/usr/lib/swift/windows/x86_64  && cd ."
error: link command failed with exit code 1319 (use -v to see invocation)
fibonacci.cpp.obj : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in swiftrt.obj
fibonacci.cpp.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MD_DynamicRelease' in swiftrt.obj
C:\Users\fpultar\AppData\Local\Programs\Swift\Platforms\5.10.0\Windows.platform\Developer\SDKs\Windows.sdk\\usr\lib\swift\windows\x86_64\swiftrt.obj : warning LNK4042: object specified more than once; extras ignored
   Creating library lib\fibonacci\fibonacci.lib and object lib\fibonacci\fibonacci.exp
LINK : warning LNK4098: defaultlib 'msvcrtd.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
lib\fibonacci\fibonacci.dll : fatal error LNK1319: 2 mismatches detected
clang: error: linker command failed with exit code 1319 (use -v to see invocation)
error: fatalError
ninja: build stopped: subcommand failed.

Do you have any suggestions what could be the issue? I understand that these examples should all work under Windows so I am assuming something might be off with my dev setup.

Best,
Felix

How to consume Swift modules when Cxx interop is enabled

Hey there ๐Ÿ‘‹๐Ÿผ

First, thanks for this repository, he helped me learn a lot about Swift and CMake and Swift/Cxx interop.

I was experimenting with Swift/Cxx interoperability, especially with consuming a Swift class from C++.

It worked nicely until I needed to import a Swift module from one file to another.

Here is my project structure:

โ”œโ”€โ”€ CMakeLists.txt
โ”œโ”€โ”€ app
โ”‚ย ย  โ”œโ”€โ”€ App.cc  
โ”‚ย ย  โ”œโ”€โ”€ App.swift                 // HERE, I NEED to import module from window.swift
โ”‚ย ย  โ”œโ”€โ”€ CMakeLists.txt
โ”‚ย ย  โ””โ”€โ”€ include
โ”‚ย ย      โ”œโ”€โ”€ application.h
โ”‚ย ย      โ””โ”€โ”€ module.modulemap
โ”œโ”€โ”€ main.cc
โ””โ”€โ”€ window
    โ”œโ”€โ”€ CMakeLists.txt
    โ”œโ”€โ”€ include
    โ”‚ย ย  โ”œโ”€โ”€ module.modulemap
    โ”‚ย ย  โ””โ”€โ”€ window.h
    โ”œโ”€โ”€ window.cc
    โ””โ”€โ”€ window.swift
import Ui

But it does not work as expected:

[4/8] Generating '/Users/tonygo/oss/native-research/build/app/include/app-swift.h'
FAILED: app/include/app-swift.h /Users/tonygo/oss/native-research/build/app/include/app-swift.h
cd /Users/tonygo/oss/native-research/build/app && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -frontend -typecheck -I/Users/tonygo/oss/native-research/app/include /Users/tonygo/oss/native-research/app/App.swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -module-name Native -cxx-interoperability-mode=default -emit-clang-header-path /Users/tonygo/oss/native-research/build/app/include/app-swift.h
/Users/tonygo/oss/native-research/app/App.swift:3:8: error: no such module 'Ui'
import Ui
       ^

But when I looked at the build output, I saw Ui module files generated:

โ”œโ”€โ”€ build
โ”‚ย ย  โ””โ”€โ”€ window
โ”‚ย ย      โ”œโ”€โ”€ CMakeFiles
โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ window.dir
โ”‚ย ย      โ”‚ย ย      โ”œโ”€โ”€ output-file-map.json
โ”‚ย ย      โ”‚ย ย      โ”œโ”€โ”€ window.cc.o
โ”‚ย ย      โ”‚ย ย      โ”œโ”€โ”€ window.swift.o
โ”‚ย ย      โ”‚ย ย      โ”œโ”€โ”€ window.swift.o.d
โ”‚ย ย      โ”‚ย ย      โ”œโ”€โ”€ window.swift.swiftdoc
โ”‚ย ย      โ”‚ย ย      โ”œโ”€โ”€ window.swift.swiftmodule
โ”‚ย ย      โ”‚ย ย      โ”œโ”€โ”€ window.swift.swiftsourceinfo
โ”‚ย ย      โ”‚ย ย      โ””โ”€โ”€ window.swiftdeps
โ”‚ย ย      โ”œโ”€โ”€ Ui.abi.json
โ”‚ย ย      โ”œโ”€โ”€ Ui.swiftdoc
โ”‚ย ย      โ”œโ”€โ”€ Ui.swiftmodule
โ”‚ย ย      โ”œโ”€โ”€ Ui.swiftsourceinfo
โ”‚ย ย      โ”œโ”€โ”€ cmake_install.cmake
โ”‚ย ย      โ”œโ”€โ”€ include
โ”‚ย ย      โ”‚ย ย  โ””โ”€โ”€ window-swift.h
โ”‚ย ย      โ””โ”€โ”€ libwindow.a

I also tried with:

import window

Like it is done here: https://github.com/apple/swift-cmake-examples/tree/main/2_executable_library

I checked that app is linked to window target.

I wonder if it is not just a CMake knowledge that I miss or something else. I run out of ideas ๐Ÿ˜….

Accessing a swift struct from C++ in ping pong

I added a struct in the swift file but on the C++ side I get the error message "no type named ....." . I use the latest version of cmake. It would be great if you provided an example for that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.