Git Product home page Git Product logo

swift-cmake-examples's Introduction

CMake Examples for Swift

This repository contains example projects demonstrating how to setup a Swift project with CMake. Make sure to use the CMake Ninja generator and Ninja build tool. These hopefully cover a wide variety of use cases. Patches to add examples of missing use cases are welcome.

Projects

  • Hello Minimal
    • Simple project with a pure Swift library and executable
  • Build Dependencies
    • Simple project with a library that depends on an external package which is built as part of the build
  • Hello World
    • Multi-library project with C and Swift code

Why Use This

CMake is a meta-build system that allows you to generate the build rules using different build tools. It also makes it possible to setup the build in a way which supports cross-compiling for various targets (including Linux, Windows, and android).

How to build with CMake

If swiftc is not in your path, you will need to add -DCMAKE_Swift_COMPILER= with the path to swiftc.

Linux or macOS
cmake -B build -D CMAKE_BUILD_TYPE=RelWithDebInfo -D BUILD_TESTING=YES -G Ninja -S .
ninja -C build
ninja -C build test
Windows

NOTE: we must build with the Release configuration on Windows as the Swift runtime in debug configuration is not distributed with the standard toolchain. MSVCRT cannot be used in different configurations in the same process, and will result in runtime failures.

set SWIFTFLAGS=-sdk %SDKROOT%
cmake -B build -D CMAKE_BUILD_TYPE=Release -D CMAKE_Swift_FLAGS=%SWIFTFLAGS% -D BUILD_TESTING=YES -G Ninja -S .
ninja -C build
ninja -C build test

This invocation builds the project in release mode with debug information. This enables optimized builds with debug information (or release only). Additionally, the standard CMake option BUILD_TESTING is used to enable tests.

What is supported

These project build on Linux, macOS, and Windows!

  • CMAKE_BUILD_TYPE

    • Debug (no optimizations, debug info)
    • Release (all optimizations, no debug info)
    • RelWithDebInfo (all optimizations, debug info)
    • MinSizeRel (optimized for size)
  • MSVC_RUNTIME_LIBRARY (Windows Only)

    • MultiThreadedDebugDLL (MDd)
    • MultiThreadedDLL (MD)

swift-cmake-examples's People

Contributors

adkaster avatar compnerd avatar masterswift avatar mikecodesdotnet avatar toulouse 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

swift-cmake-examples's Issues

Cmake and Ninja Error on Windows

HELLOMINIMAL project

cmake -G Ninja -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=YES

-- The Swift compiler identification is Apple 5.3.2
-- Check for working Swift compiler: C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe
-- Check for working Swift compiler: C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.19/Modules/CMakeTestSwiftCompiler.cmake:41 (message):
The Swift compiler

"C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe"

is not able to compile a simple test program.

It fails with the following output:

Change Dir: F:/DEV/swift-cmake-examples-master/HelloMinimal/build/CMakeFiles/CMakeTmp

Run Build Command(s):C:/PROGRA~1/CMake/bin/ninja.exe cmTC_3377c && [1/1] Linking Swift executable cmTC_3377c.exe
FAILED: cmTC_3377c.exe CMakeFiles/cmTC_3377c.dir/main.swift.obj cmTC_3377c.swiftmodule
cmd.exe /C "cd . && C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe -output-file-map CMakeFiles\cmTC_3377c.dir\\output-file-map.json -incremental -j 8 -emit-executable -o cmTC_3377c.exe -emit-dependencies    main.swift    && cd ."
clang: error: no such file or directory: 'C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\lib\swift\windows\x86_64\swiftrt.obj'
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:3 (project)

-- Configuring incomplete, errors occurred!
See also "F:/DEV/swift-cmake-examples-master/HelloMinimal/build/CMakeFiles/CMakeOutput.log".
See also "F:/DEV/swift-cmake-examples-master/HelloMinimal/build/CMakeFiles/CMakeError.log".

CMakeOutput.log
The system is: Windows - 10.0.18363 - AMD64
Running the Swift compiler: "C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe" -version
compnerd.org Swift version 5.3.2 (swift-5.3.2-RELEASE)
Target: x86_64-unknown-windows-msvc

Running the Swift compiler: "C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe" -version
compnerd.org Swift version 5.3.2 (swift-5.3.2-RELEASE)
Target: x86_64-unknown-windows-msvc

Problems while using on macOS

Hi!

I'm trying to build the example on macOS 10.14 with xCode 10.2, and it seems broken for me.
First of all, cmake does not seems to support the version in the way it written:

Devs-Mac-mini:swift-cmake-demo developer$ pwd
/Users/developer/Documents/swift-cmake-demo
Devs-Mac-mini:swift-cmake-demo developer$ cmake .
CMake Error at CMakeLists.txt:2 (cmake_minimum_required):
CMake 3.15.20190814 or higher is required. You are running version 3.15.5

When I changed the required version to 3.15.2 (which is not totally correct, I know), it allows me to generate the project.

The project SwiftDemo.xcodeproj which is generated I could open, but it could not be built.

It throws Swift compile error "No such module HelloWorldCore in TestHelloWorldCore.swift:3", same but in HelloWorld.swift:2, and 'Expression is not allowed at the top level in HelloWorld.swift' on line 23 (main())

I noticed that Swift language version in Xcode project differs from the one, that stated in cmakelists: it is 4 vs 4.2, but switching this version does not resolves the problem.

I'm not a proficient Swift developer, so I hope somebody could help me to run this demo.

Build failure: unknown argument: '-static'

I've attempted to build this example using the instructions on the readme, and when I try to invoke ninja from the build directory, I get this error:

[1/4] Linking Swift static library Source/HelloWorldCore/libHelloWorldCore.a
FAILED: Source/HelloWorldCore/libHelloWorldCore.a Source/HelloWorldCore/CMakeFiles/HelloWorldCore.dir/_HelloWorldCore.swift.o Source/HelloWorldCore/CMakeFiles/HelloWorldCore.dir/HelloWorldCore.swift.o swift/HelloWorldCore.swiftmodule 
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -output-file-map Source/HelloWorldCore/CMakeFiles/HelloWorldCore.dir/output-file-map.json -incremental -num-threads 8 -emit-library -static -o Source/HelloWorldCore/libHelloWorldCore.a -module-name HelloWorldCore -module-link-name HelloWorldCore -emit-module -emit-module-path swift/HelloWorldCore.swiftmodule -emit-dependencies  -enable-testing  ../Source/HelloWorldCore/_HelloWorldCore.swift ../Source/HelloWorldCore/HelloWorldCore.swift    && :
<unknown>:0: error: unknown argument: '-static'
[2/4] Building C object Source/CCore/CMakeFiles/CCore.dir/CCoreVersion.c.o
ninja: build stopped: subcommand failed.

My environment is:

macOS 10.14.5

swiftc --version:
Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
Target: x86_64-apple-darwin18.6.0

cmake version 3.15.20190718-gf0461

ninja --version 1.9.0

Failed to compile HelloWorld

Hey there @compnerd , I am trying to compile, the Hello World app on windows and I am running into errors. Attached below are screenshots of my HelloMinimal running, but HelloWorld failing to compile.
Capture
Capture2

Failing to build on Windows

I followed the getting started directions for windows under docs, using the provided 5.2.1 binaries (I ran all of the available exe’s and msi's). When I go to compile the HelloWorld-CMake program from the getting started, I get a no such module CCore error:

C:\Users\Alex\Downloads>cmake --build /BinaryCache/HelloWorld
[1/3] Linking Swift shared library bin\HelloWorldCore.dll
FAILED: bin/HelloWorldCore.dll Source/HelloWorldCore/CMakeFiles/HelloWorldCore.dir/_HelloWorldCore.swift.obj Source/HelloWorldCore/CMakeFiles/HelloWorldCore.dir/HelloWorldCore.swift.obj swift/HelloWorldCore.swiftmodule lib/HelloWorldCore.lib
cmd.exe /C "cd . && C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe -output-file-map Source\HelloWorldCore\CMakeFiles\HelloWorldCore.dir\Release\output-file-map.json -incremental -j 12 -emit-library -o bin\HelloWorldCore.dll -module-name HelloWorldCore -module-link-name HelloWorldCore -emit-module -emit-module-path swift\HelloWorldCore.swiftmodule -emit-dependencies -DHelloWorldCore_EXPORTS -sdk C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk -I C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift -L C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows -O -libc MD -enable-testing -I \SourceCache\swift-build-examples\HelloWorld-CMake\Source\CCore \SourceCache\swift-build-examples\HelloWorld-CMake\Source\HelloWorldCore\_HelloWorldCore.swift \SourceCache\swift-build-examples\HelloWorld-CMake\Source\HelloWorldCore\HelloWorldCore.swift    -Xlinker -implib:lib\HelloWorldCore.lib -L \BinaryCache\HelloWorld\lib lib\CCore.lib && cd ."
\SourceCache\swift-build-examples\HelloWorld-CMake\Source\HelloWorldCore\HelloWorldCore.swift:3:8: error: no such module 'CCore'
import CCore
       ^
\SourceCache\swift-build-examples\HelloWorld-CMake\Source\HelloWorldCore\HelloWorldCore.swift:3:8: error: no such module 'CCore'
import CCore
       ^
ninja: build stopped: subcommand failed.
 
C:\Users\Alex\Downloads>

Output of the CMAKE configure:

C:\Users\Alex\Downloads>"%ProgramFiles%/CMake/bin/cmake.exe" -B /BinaryCache/HelloWorld -D BUILD_SHARED_LIBS=YES -D CMAKE_BUILD_TYPE=Release -D CMAKE_Swift_FLAGS="%SWIFTFLAGS%" -G Ninja -S /SourceCache/swift-build-examples/HelloWorld-CMake
-- The C compiler identification is MSVC 19.26.28806.0
^C
C:\Users\Alex\Downloads>"%ProgramFiles%/CMake/bin/cmake.exe" -B /BinaryCache/HelloWorld -D BUILD_SHARED_LIBS=YES -D CMAKE_BUILD_TYPE=Release -D CMAKE_Swift_FLAGS="%SWIFTFLAGS%" -G Ninja -S /SourceCache/swift-build-examples/HelloWorld-CMake
-- The C compiler identification is MSVC 19.26.28806.0
-- The Swift compiler identification is Apple 5.3
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working Swift compiler: C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe
-- Check for working Swift compiler: C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe - works
-- Configuring done
-- Generating done
-- Build files have been written to: /BinaryCache/HelloWorld

Support for C code?

Pretty straightforward question, is linking swift code to C libraries supported?

`aux_source_directory ` can't find *.swift sources

It's not work:

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} _sources)

a workaround:

file(GLOB _sources ${CMAKE_CURRENT_SOURCE_DIR}/*.swift)

or using macro:

macro(aux_swift_source_directory _dir _variable)
  file(GLOB ${_variable} ${_dir}/*.swift)
endmacro()

aux_swift_source_directory(${CMAKE_CURRENT_SOURCE_DIR} _sources)

Build HelloWorld and HelloMini failed

On windows, these two projects builed failed using the commands in README.

set SWIFTFLAGS=-sdk %SDKROOT%
cmake -B build -D CMAKE_BUILD_TYPE=Release -D BUILD_TESTING=YES -G Ninja -S .
ninja -C build
ninja -C build test

following is the fail message:

It fails with the following output:

    Change Dir: C:/Users/Administrator/Desktop/swift-cmake-examples/HelloWorld/build/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/PROGRA~1/CMake/bin/ninja.exe cmTC_8c91d && [1/1] Linking Swift executable cmTC_8c91d.exe
    FAILED: cmTC_8c91d.exe CMakeFiles/cmTC_8c91d.dir/main.swift.obj cmTC_8c91d.swiftmodule       
    cmd.exe /C "cd . && C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe -output-file-map CMakeFiles\cmTC_8c91d.dir\\output-file-map.json -incremental -j 8 -emit-executable -o cmTC_8c91d.exe -emit-dependencies    main.swift    && cd ."
    clang: error: no such file or directory: 'C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\lib\swift\windows\x86_64\swiftrt.obj'
    <unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.

Then i found method in https://github.com/compnerd/swift-build/blob/master/docs/Windows.md,

SET INSTALLATION_DIR=C:
SET SDK=%INSTALLATION_DIR%\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk
SET OS=windows
cmake -G Ninja -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=YES -D CMAKE_Swift_FLAGS="-sdk %SDK% -I %SDK%/usr/lib/swift -L %SDK%/usr/lib/swift/%OS%"
cd build
ninja
ninja test

use this method HelloWorld build success : Hello World!, but HelloMini still failed. and the fail message is:

FAILED: HiKit.lib CMakeFiles/HiKit.dir/hikit.swift.obj HiKit.swiftmodule 
cmd.exe /C "cd . && C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe -output-file-map CMakeFiles\HiKit.dir\RelWithDebInfo\output-file-map.json -incremental -j 8 -emit-library -static -o HiKit.lib -module-name HiKit -module-link-name HiKit -emit-module -emit-module-path HiKit.swiftmodule -emit-dependencies  -sdk C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk -I C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift -L C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk/usr/lib/swift/windows -O -g  ..\hikit.swift    && cd ."
<unknown>:0: error: unable to execute command: <unknown>
ninja: build stopped: subcommand failed.

CMAKE_Swift_COMPILER

ran cmake . on the folder

CMake Error: CMAKE_Swift_COMPILER not set, after EnableLanguage

What is this suppose to be set to?

Support for use with XCode?

Hi Saleem, maybe not the most relevant place to post but better then filing a CMake issue: have you successfully gotten CMake swift support working so that a Swift library can be compiled with CMake and imported into XCode? Presumably the problem is twofold, both getting CMake to properly compile for the architecture and getting XCode to import the library. Regarding the first, I'm aware of several ios CMake toolchains with guides for C but not swift, hopefully differences would be minor or just work out of the box. I'm unsure how one would go about the latter. No need to implement an example here, I'm undertaking this myself and may submit a PR with example if I get this working, just thought I'd see if you had any resources/tips to point me in the right direction.

Test fails

I did the following

cd ~/git/swift-cmake-demo/
rm -rf build
mkdir build
cd build
cmake -S ../ -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=YES
ninja
ninja test

and I got

dyld: Library not loaded: Source/HelloWorldCore/libHelloWorldCore.dylib
  Referenced from: /Users/wander/git/swift-cmake-demo/build/Tests/TestHelloWorldCore/TestHelloWorldCore
  Reason: image not found
Abort trap: 6

By the way, I can run build/Source/HelloWorld/HelloWorld without problems.

Build failure using Ninja generator

With Xcode 10 and 10.2, this demo or any one line swift code will fail to build. Xcode generator works.

The error message:

ninja: Entering directory `out/'
[1/1] Linking Swift executable Test0
FAILED: Test0 CMakeFiles/Test0.dir/src/main.swift.o Test0.swiftmodule
: && /usr/bin/swiftc -output-file-map CMakeFiles/Test0.dir/output-file-map.json -incremental -emit-executable -o Test0 -emit-module -emit-module-path Test0.swiftmodule -emit-dependencies ../src/main.swift && :
:0: error: unable to load output file map 'CMakeFiles/Test0.dir/output-file-map.json': No such file or directory
ninja: build stopped: subcommand failed.

CCore is useless

  • No explanation for the export macro
  • CCoreVersion isn't used anywhere
  • No header files
  • Doesn't show how C and Swift integrates

CMake problem when building HelloWorld project

Hi, I have been exploring working with swift with libraries on windows, and have been trying to get the HelloWorld sample to build. I have installed a fresh new toolchain from the latest snapshots potion of swift.org downloads, (the march 2nd one). I have followed the steps to copy everything, and am running all commands from an elevated x64 Native Tools command prompt for vs 2019. When I try to run the cmake commands, this is what I get:

C:\Users\Sheen\Swift\swift-cmake-examples\HelloWorld>cmake -B build -D CMAKE_BUILD_TYPE=Release -D BUILD_TESTING=YES -G Ninja -S .
-- The Swift compiler identification is Apple 5.4
-- Check for working Swift compiler: C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe
-- Check for working Swift compiler: C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe -- broken
CMake Error at C:/Applications/cmake/share/cmake-3.16/Modules/CMakeTestSwiftCompiler.cmake:41 (message):
  The Swift compiler

    "C:/Library/Developer/Toolchains/unknown-Asserts-development.xctoolchain/usr/bin/swiftc.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/Sheen/Swift/swift-cmake-examples/HelloWorld/build/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/APPLIC~1/VISUAL~1/Common7/IDE/COMMON~1/MICROS~1/CMake/Ninja/ninja.exe cmTC_37353 && [1/1] Linking Swift executable cmTC_37353.exe
    FAILED: cmTC_37353.exe CMakeFiles/cmTC_37353.dir/main.swift.obj cmTC_37353.swiftmodule
    cmd.exe /C "cd . && C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swiftc.exe -output-file-map CMakeFiles\cmTC_37353.dir\output-file-map.json -incremental -j 12 -emit-executable -o cmTC_37353.exe -emit-module -emit-module-path cmTC_37353.swiftmodule -emit-dependencies    main.swift  -Xlinker -implib:cmTC_37353.lib   && cd ."
    clang: error: no such file or directory: 'C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\lib\swift\windows\x86_64\swiftrt.obj'
    <unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:4 (project)


-- Configuring incomplete, errors occurred!
See also "C:/Users/Sheen/Swift/swift-cmake-examples/HelloWorld/build/CMakeFiles/CMakeOutput.log".
See also "C:/Users/Sheen/Swift/swift-cmake-examples/HelloWorld/build/CMakeFiles/CMakeError.log".

I also made sure that I have all the required components, as well as all the optional components, even git, which was already installed (but I don't think that is the problem).
I went searching for the swiftrt.obj file it references, and there is no windows folder at C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\lib\swift
I deleted everything inside of the C:\Library folder before installing a newer version of the toolchain.
I have CMake version 3.16.2 as found by cmake --version
For clang --version:

compnerd.org clang version 12.0.0
Target: x86_64-unknown-windows-msvc
Thread model: posix
InstalledDir: C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin

I have tried in both an elevated and non-elevated x64 Native Tools Command Prompt for VS 2019
I am using the latest commit (as of march 6th, 10PM EST) of this repo.

Any suggestions?

Thank you,
Sheen

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.