Giter Club home page Giter Club 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)

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.