MarkSchofield / WindowsToolchain

A repository containing a CMake toolchain for using MSVC
MIT License
79 stars 17 forks source link

CMake Toolchain files for Windows

A CMake toolchain file describes the set of tools and utilities for compiling code in CMake. This repo provides toolchains that describes how to compile using MSVC and Clang in CMake, with the goal of making Windows CMake builds more canonical to reduce the 'barrier-to-entry' to build code for Windows.

This repo contains three toolchains:

  1. Windows.MSVC.toolchain.cmake - Uses the MSVC compiler from an installed Visual Studio instance.

  2. Windows.Clang.toolchain.cmake - Uses Clang from either an LLVM installation or from a Visual Studio installation.

  3. Windows.EWDK.toolchain.cmake - Uses the MSVC compiler from an Enterprise Windows Driver Kit. This toolchain requires that CMake be invoked from an EWDK build prompt.

Each toolchain will find a compiler, runtime and a Windows SDK.

build status

The CMake scripts for Windows-specific build tasks that used to be in this repository can now be found in MarkSchofield/WindowsCMake.

But I can build with MSVC in CMake already...?

Yes, but you're probably either:

  1. using a "Visual Studio Generator". In which case, CMake will emit - and then build - a Visual Studio Solution file that knows how to find the build tools and utilities.

  2. running from a Visual Studio "Command Prompt". In which case, the command prompt is hard-coded to use a set of tools and utilities, which means that you always need to initialize your command prompt before getting started.

By using a Toolchain file to describe the tools and utilities to build in CMake-terms, you can use other generators (e.g. Ninja) and don't have to initialize an environment to run a build.

So this is a replacement for Visual Studio!?

No, no. This uses the tools and utilities from installed Visual Studio but from a CMake build.

So this solves all my build problems!?

No. This is an early attempt at making Visual Studio tooling easier to use from CMake. There's definitely gaps - there's no support for "Windows Universal" builds, for example. But it gets enough of a stake-in-the-ground to start a conversation.

How do I use this?

Simply specify one of the toolchain files to your CMake builds. See the documentation for CMake toolchains for more details on Toolchain files and how to consume them.

Each toolchain file ('Windows.MSVC.toolchain.cmake', 'Windows.Clang.toolchain.cmake', or Windows.EWDK.toolchain.cmake) has details on the various CMake properties that can be used to configure the build. And the example folder provides a CMake project that builds a variety of Windows projects, using each of the toolchain files.

WindowsToolchain and VCPkg

VCPkg - https://github.com/microsoft/vcpkg - is a Package Manager for C++. When using WindowsToolchain with VCPkg:

  1. The vcpkg.cmake script must be specified as the toolchain for CMake builds - as per the VCPkg documentation.

  2. The path to the WindowsToolchain file - Windows.MSVC.toolchain.cmake - should be specified as the VCPKG_CHAINLOAD_TOOLCHAIN_FILE. The VCPkg toolchain will load the specified VCPKG_CHAINLOAD_TOOLCHAIN_FILE.

Note:

Linting

WindowsToolchain uses cmakelang for linting the CMake files in the codebase. The .cmake-format.yaml file describes the formatting style for the codebase. To run the linting tools:

  1. Install cmakelang following the installation instuctions. Note: Since WindowsToolchain uses a .yaml file for configuration, make sure to install the cmakelang[YAML] package.

  2. Run ./analyze.ps1

The Toolchain CI GitHub Workflow enforces the linting rules during PR and CI.