CMake explained

CMake
Logo Size:96px
Developer:Andy Cedilnik, Bill Hoffman, Brad King, Ken Martin, Alexander Neundorf
Programming Language:C, C++[1]
Operating System:Cross-platform
Genre:Software development tools
License:BSD-3-Clause

CMake is a free, cross-platform, software development tool for building applications via compiler-independent instructions. It also can automate testing, packaging and installation. It runs on a variety of platforms and supports many programming languages.[2]

As a meta-build tool, CMake configures native build tools which in turn build the codebase. CMake generates configuration files for other build tools based on CMake-specific configuration files. The other tools are responsible for more directly building; using the generated files. A single set of CMake-specific configuration files can be used to build a codebase using the native build tools of multiple platforms.[3]

Notable native build tools supported by CMake include: Make, Qt Creator, Ninja, Android Studio, Xcode, and Visual Studio.

CMake is distributed as free and open-source software under a permissive BSD-3-Clause license.[4]

History

Initial development began in 1999 at Kitware with funding from the United States National Library of Medicine as part of the Visible Human Project. CMake was first released in 2000.

CMake was developed to support building the Insight Segmentation and Registration Toolkit (ITK) for multiple platforms. Stated goals included addressing weaknesses while maintaining strengths of contemporary tools such as autoconf and libtool, and to align with state of the art build technology of the time: configure scripts and Make files for Unix platforms, and Visual Studio project files for Windows.[5]

CMake was inspired by multiple contemporary tools. pcmaker developed by Ken Martin and others to support building the Visualization Toolkit (VTK) converted Unix Make files into NMake files for building on Windows. gmake supported Unix and Windows compilers, but was its design lead to hard to resolve environment issues. Both tools were working examples of a build tool that supported both Unix and Windows, but they suffered from a serious flaw: they required Windows developers to use the command line even though many prefer to use an integrated development environment (IDE) such as Visual Studio.

CMake was to provide similar cross-platform support but to better satisfy the preferences of the developers on each platform.

The design goals of the first version included:

For various reasons, CMake developers chose to develop a scripting language for CMake instead of using Tcl a popular language for building at the time. Use of Tcl would have then added a dependency to the host machine which is counter to the goal of no dependencies other than a compiler. Also, Tcl was not well supported on Windows and some Unix systems at the time of initial development.

Subsequent development and improvements were fueled by the incorporation of CMake into developers’ own systems, including the VXL Project, the CABLE[6] features added by Brad King, and GE Corporate R&D for support of DART. Additional features were created when VTK transitioned to CMake for its build environment and for supporting ParaView.

Version 3.0 was released in June 2014.[7] It has been described as the beginning of "Modern CMake".[8] Experts now advise to avoid variables in favor of targets and properties.[9] The commands,,, that were at the core of CMake 2 should now be replaced by target-specific commands.

Name

CMake developer Brad King stated that "the 'C' in CMake stands for 'cross-platform.[10]

Features

Generators

CMake generates configuration files for a particular native build tool via one of its generators. A user can select a generator via the CMake command line.

The build files are configured depending on the generator used (e.g. Unix Makefiles for make) and associated toolchain files. Advanced users can also create and incorporate additional makefile generators to support their specific compiler and OS needs. Generated files are typically placed (by using 's flag) into a folder outside of the source's one (out of source build), e.g., .

Each build project in turn contains its own file and directory in every project (sub-)directory of included by the command, helping to avoid or speed up regeneration when it is run repeatedly.

The generation process and the output can be fine-tuned via target properties. Previously it was done via -prefixed global variables that are also used to configure CMake itself and to set up initial defaults.[11] The older approach is discouraged now.

Build targets

CMake supports building executables, libraries (e.g., etc.), object file libraries and pseudo-targets (including aliases). CMake can produce object files that can be linked against by executable binaries/libraries, avoiding dynamic (run-time) linking and using static (compile-time) linking instead. This enables flexibility in configuration of various optimizations.[12]

Separate build tree

One of its major features is the ability to place compiler outputs (such as object files) into a build tree which is located outside of the source tree. This enables multiple builds from the same source tree and cross-compilation. Separate source and build files ensure that removing a build directory will not affect source files and prevents clutter which might confuse version control systems.

Dependency management

CMake tracks changes and recompiles upstream dependencies of a given sub-module if its sources are changed.

Flexible project structure

CMake can locate system-wide and user-specified executables, files, and libraries. These locations are stored in a cache, which can then be tailored before generating the target build files. The cache can be edited with a graphical editor, which is shipped with CMake.

Complicated directory hierarchies and applications that rely on several libraries are well supported by CMake. For instance, CMake is able to accommodate a project that has multiple toolkits, or libraries that each have multiple directories. In addition, CMake can work with projects that require executables to be created before generating code to be compiled for the final application. Its open-source, extensible design allows CMake to be adapted as necessary for specific projects.[13]

IDE configuration support

CMake can generate project files for several popular IDEs, such as Microsoft Visual Studio, Xcode, and Eclipse CDT. It can also produce build scripts for MSBuild or NMake on Windows; Unix Make on Unix-like platforms such as Linux, macOS, and Cygwin; and Ninja on both Windows and Unix-like platforms.

Compiler feature detection

CMake allows specification of features that the compiler is required to support in order to get the target program or library compiled.[14]

Compiler support

CMake supports many compilers, including: Apple Clang, Clang, GNU GCC, MSVC, Oracle Developer Studio, and Intel C++ Compiler.[15]

Packaging system

Even though CMake is not a package manager, it provides basic modules (see CPack) functions for installing binaries and package information declared by the script to be used by consumer CMake projects. The package may also be packed into an archive file for package manager or installer supported by a target platform. Third-party packages may also be imported via configured CMake files which are either provided by the same third-party or created manually.[16] [17] [18]

GUI

Cmake may be run by using a ncurses program like that can be used to configure projects via command-line interface.

Precompiled headers

It's possible to generate precompiled headers via CMake since version 3.6.[19]

JSON strings

CMake supports extracting values into variables from JSON-data strings (since version 3.19).[20]

Build process

Building via CMake has a two major stages. First, native build tool configuration files are generated from CMake configuration files written in the CMake scripting language. The command line syntax is where

is a directory that contains a file. Then, the native build tools are invoked either via CMake or directly via the native tools interface. The native build tools use the generated files.[21]

Language

CMake includes an interpreter for a relatively simple, custom, imperative scripting language that supports variables, string manipulation, arrays, function and macro declaration, and module inclusion (importing).

The interpreter reads CMake language commands from files named which specify source files and build preferences. CMake uses this information to generate native tool configuration files. Additionally, files with suffix can be used for storing additional script.[22]

Command syntax

CMake language commands are formatted as:

name(argument ...)

Arguments are whitespace-separated and can include keywords to separate groups of arguments. For instance, in the following command, the keyword delimits a list of source files from compiler flags.[23]

set_source_file_properties(filename ... COMPILE_FLAGS compile_flag ...)

Some commonly used commands include:[24] [25] [26]

Implementation

The CMake scripting language is implemented via Yacc and Lex generators.

The executable programs CMake, CPack, and CTest are written in C++.

Much of CMake's functionality is implemented in modules written in the CMake language.[27]

Since release 3.0, CMake's documentation uses reStructuredText markup. HTML pages and man pages are generated by the Sphinx documentation generator.

Additional tools

CMake ships with numerous script files and development tools that facilitate tasks such as finding dependencies (both built-in and external, e.g. modules), testing the toolchain environment and executables, packaging releases (CPack), and managing dependencies on external projects (module). Additional development tools include:[28] [29]

Adoption

CMake has been very widely adopted among commercial, open source, and academic software projects. A few notable users include Android NDK, Netflix, Inria, MySQL, Boost (C++ libraries), KeePassXC, KDE, KiCAD, FreeCAD, Webkit, Blender,[32] Biicode, ReactOS, Apache Qpid, the ATLAS experiment,[33] and Second Life.[34]

Examples

Hello world

The following demonstrates how to configure CMake to build a hello world program written in C++.

hello.cpp:

  1. include

int main

CMakeLists.txt:cmake_minimum_required(VERSION 3.5)project(HelloWorld CXX)add_executable(hello hello.cpp)

To build via CMake, first cd to the directory containing the two files above. Then, generate the native build config files via the cross-platform CMake command:cmake -B build .Then, build the program via the native build tool as supported thru CMake:cmake --build build

The program is then available for running. Via Bash, the command is like . On Windows, the output file ends with .

Include

This example demonstrates configuring the preprocessor include path.

hello.cpp:

  1. include "hello.h"
  2. include

int main

hello.h:const int Times = 10;

CMakeLists.txt:cmake_minimum_required(VERSION 3.5)project(HelloWorld CXX)include_directories($)add_executable(hello hello.cpp)

Notes and References

  1. Web site: The CMake Open Source Project on OpenHub . OpenHub . 2016-04-09.
  2. Web site: CMake.
  3. Web site: The Architecture of Open Source Applications (Volume 1)CMake . 2023-06-11 . aosabook.org.
  4. Web site: Licenses · master · CMake / CMake. 2020-11-13. GitLab. en.
  5. Web site: FLOSS Weekly 111: CMake . podcast . TWiT Network . 27 February 2011.
  6. Web site: The CABLE . 2010-11-10 . https://web.archive.org/web/20130619224333/http://public.kitware.com/Cable/HTML/Index.html . 2013-06-19 . dead .
  7. Web site: [CMake] [ANNOUNCE] CMake 3.0.0 Released.]. Robert. Maynard. June 10, 2014.
  8. Web site: Effective Modern CMake. Gist.
  9. Web site: Binna . Manuel . Jul 22, 2018 . Effective Modern CMake .
  10. https://public.kitware.com/Bug/view.php?id=14012#c32631
  11. Pfeifer . Daniel . May 19, 2017 . Effective Cmake . CPPNow.
  12. Web site: cmake-buildsystem(7) — CMake 3.19.0-rc3 Documentation. 2020-11-14. cmake.org.
  13. Web site: Why the KDE project switched to CMake—and how . . 2006-06-21 . Neundorf . Alexander.
  14. Web site: 2020-11-15. CMake compiler feature detect. 2022-01-22. www.scivision.dev. en-us.
  15. Web site: Supported Compilers. CMake Documentation. January 22, 2022.
  16. Book: Berner, Dominik . CMake Best Practices : Discover Proven Techniques for Creating and Maintaining Programming Projects with CMake . 2022 . Packt Publishing, Limited . Mustafa Kemal Gilor . 978-1-80324-424-2 . Birmingham . 1319218167.
  17. Web site: cmake-packages(7) — CMake 3.24.1 Documentation . 2022-09-11 . cmake.org.
  18. Web site: Exposing a module's configuration (advanced) — CMake build procedure 1.0 documentation . 2022-09-11 . docs.salome-platform.org.
  19. Web site: target_precompile_headers — CMake 3.21.20210925-gb818e3c Documentation. 2021-09-25. cmake.org.
  20. Web site: CMake 3.19 Release Notes — CMake 3.19.7 Documentation. 2021-03-15. cmake.org.
  21. Web site: cmake-toolchains(7) — CMake 3.19.0-rc2 Documentation. 2020-10-29. cmake.org.
  22. Web site: cmake-language(7) — CMake 3.19.0-rc2 Documentation. 2020-10-29. cmake.org.
  23. Web site: 2021-01-29. Cross-Platform Software Development Using CMake Software. Andrej. Cedilnik. Linux Journal. 2003-10-30.
  24. Web site: add_executable — CMake 3.19.0-rc1 Documentation. 2020-10-25. cmake.org.
  25. Web site: add_library — CMake 3.19.0-rc1 Documentation. 2020-10-25. cmake.org.
  26. Web site: target_link_directories — CMake 3.20.2 Documentation. 2021-05-10. cmake.org.
  27. Web site: cmake-language(7) — CMake 3.19.0-rc1 Documentation. 2020-10-25. cmake.org.
  28. Web site: cmake-modules(7) — CMake 3.14.7 Documentation . 2020-10-24 . cmake.org.
  29. Web site: ExternalProject — CMake 3.14.7 Documentation . 2020-10-24 . cmake.org.
  30. Web site: Packaging With CPack . CMake Community Wiki . en.
  31. .
  32. Web site: Building Blender - Blender Developer Wiki. 2021-07-01. wiki.blender.org.
  33. Web site: 2017. Large Scale Software Building with CMake in ATLAS. live. CERN. J. Elmsheuser. A. Krasznahorkay. E. Obreshkov. A. Undrus. https://web.archive.org/web/20180728080455/http://cds.cern.ch/record/2243765/files/ATL-SOFT-PROC-2017-033.pdf . 28 July 2018 .
  34. Web site: CMake Success . 2011 . cmake.org . Kitware . March 12, 2022.