CMake

Last updated

CMake
Developer(s) Andy Cedilnik, Bill Hoffman, Brad King, Ken Martin, Alexander Neundorf
Initial release2000;25 years ago (2000)
Stable release
3.31.4 [1]   OOjs UI icon edit-ltr-progressive.svg / 10 January 2025
Repository
Written in C, C++ [2]
Operating system Cross-platform
Type Software development tools
License BSD-3-Clause
Website cmake.org   OOjs UI icon edit-ltr-progressive.svg

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. [3]

Contents

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. [4]

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

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

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. [4] 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. [6] [4]

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. [4] gmake supported Unix and Windows compilers, but its design led to issues that were hard to resolve. 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: [4]

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. [4]

Subsequent development and improvements were fueled by the incorporation of CMake into developers’ own systems, including the VXL Project,[ clarification needed ] the CABLE [7] features added by Brad King,[ clarification needed ] and GE Corporate R&D for support of DART.[ clarification needed ] 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. [8] It has been described as the beginning of "Modern CMake". [9] Experts now advise to avoid variables in favor of targets and properties. [10] The commands add_compile_options, include_directories, link_directories, link_libraries 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'". [11]

Features

Generators

Support for each native build tool is provided as a separate generator. CMake uses a particular generator by default for the host environment. Alternatively, a generator can be selected via the command line option -G. For example, generator Unix Makefiles creates files for make. [4]

CMake does not support custom generators without modifying the CMake implementation. None-the-less, the CMake source code could be modified to include a custom generator.

Build targets

CMake supports building executables, libraries (e.g. libxyz, xyz.dll 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]

Target generation can be configured via target properties. With older versions, this was done via CMAKE_-prefixed global variables, but this approach is deprecated. [10] [13]

Hierarchical configuration

CMake configuration files can be structured according the hierarchical structure of the source code; the source tree. A CMakeLists.txt in a root source directory serves as the root of the configuration. It may include sub-directories which each contain a CMakeLists.txt. Repeating this, results in a hierarchical structure of configuration that follows the structure of the source code. [10] [13]

Separate build tree

CMake can locate generated files (both by CMake and the native build tools) in a directory tree that is separate from the source tree. [4]

This enables multiple builds from the same source tree since each has non-overlapping file system space. This may be leveraged to build different or even incompatible configurations such as for different platforms.

This also simplifies file management by allowing removing generated files by deleting a single directory tree instead of removing multiple files and directories throughout the source tree. This tends to prevent accidentally deleting source files or accidentally adding generated files to source control.

Dependency management

CMake ensures that downstream components are re-built when its sources are changed or built. [4]

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. [14]

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. [15]

Compiler support

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

Packaging

CMake can both consume and produce packages. CMake provides functions for pulling packages from a remote server that can be used as part of the build process. Via CPack, files may be packed into an archive file for package manager or installer supported by a target platform. [17] :132,142 [18] [19]

GUI

Cmake may be run by using a ncurses program like ccmake 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. [20]

JSON strings

CMake supports extracting values into variables from JSON-data strings (since version 3.19). [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 CMakeLists.txt which specify source files and build preferences. CMake uses this information to generate native tool configuration files. Additionally, files with suffix .cmake 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 COMPILE_FLAGS delimits a list of source files from compiler flags. [23]

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

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. [24]

CMake documentation (since release 3.0) uses reStructuredText markup. HTML pages and man pages are generated by the Sphinx documentation generator.

Additional tools

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

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, [29] Biicode, ReactOS, Apache Qpid, the ATLAS experiment, [30] and Second Life. [31]

Build process

Building via CMake has a two major stages. [4] First, native build tool configuration files are generated from CMake configuration files written in the CMake scripting language. The command line syntax is cmake <dir> where <dir> is a directory that contains a CMakeLists.txt file. Then, the native build tools are invoked either via CMake (cmake --build <dir>) or directly via the native tool's interface. The native build tools use the generated files. [14] [32]

Examples

Hello world

The following demonstrates configuring CMake to build a hello world program written in C++, and using CMake to build the program.

hello.cpp:

#include<iostream>intmain(){std::cout<<"Hello, world!"<<std::endl;return0;}

CMakeLists.txt:

cmake_minimum_required(VERSION3.5)project(HelloWorldCXX)add_executable(hellohello.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-Bout. 

All generated files will be under the directory out as specified via -B out.

Then, build via the native build tool as supported thru CMake:

cmake--buildout 

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

Include

This example demonstrates configuring the preprocessor include path.

hello.cpp:

#include"hello.h"#include<iostream>intmain(){for(inti=0;i<Times;i++){std::cout<<"Hello, world!"<<std::endl;}return0;}

hello.h:

constintTimes=10;

CMakeLists.txt:

cmake_minimum_required(VERSION3.5)project(HelloWorldCXX)include_directories(${PROJECT_SOURCE_DIR})add_executable(hellohello.cpp)

See also

Related Research Articles

GNU Autoconf is a software development tool for generating a configure script that in turn generates files for building a codebase and for packaging or installing the resulting files. Autoconf is part of the GNU Build System – along with Automake, Libtool, Autoheader and other tools.

<span class="mw-page-title-main">GNU Autotools</span> Software build toolset from GNU

The GNU Autotools, also known as the GNU Build System, is a suite of build automation tools designed to support building source code and packaging the resulting binaries. It supports building a codebase for multiple target systems without customizing or modifying the code. It is available on many Linux distributions and Unix-like environments.

In software development, Make is a command-line interface software tool that performs actions ordered by configured dependencies as defined in a configuration file called a makefile. It is commonly used for build automation to build executable code from source code. But, not limited to building, Make can perform any operation available via the operating system shell.

<span class="mw-page-title-main">Xcode</span> IDE including tools for developing software for Apple platforms

Xcode is Apple's integrated development environment (IDE) for macOS, used to develop software for macOS, iOS, iPadOS, watchOS, tvOS, and visionOS. It was initially released in late 2003; the latest stable release is version 16, released on September 16, 2024, and is available free of charge via the Mac App Store and the Apple Developer website. Registered developers can also download preview releases and prior versions of the suite through the Apple Developer website. Xcode includes command-line tools that enable UNIX-style development via the Terminal app in macOS. They can also be downloaded and installed without the GUI.

<span class="mw-page-title-main">MinGW</span> Free and open-source software for developing applications in Microsoft Windows

MinGW, formerly mingw32, is a free and open source software development environment to create Microsoft Windows applications.

<span class="mw-page-title-main">Apache Ant</span> Java build tool

Apache Ant is a software tool for automating software build processes for Java applications which originated from the Apache Tomcat project in early 2000 as a replacement for the Make build tool of Unix. It is similar to Make, but is implemented using the Java language and requires the Java platform. Unlike Make, which uses the Makefile format, Ant uses XML to describe the code build process and its dependencies.

<span class="mw-page-title-main">SCons</span>

SCons is a software development tool that analyzes source code dependencies and operating system adaptation requirements from a software project description and generates final binary executables for installation on the target operating system platform. Its function is similar to the more popular GNU build system.

Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. The Maven project is hosted by The Apache Software Foundation, where it was formerly part of the Jakarta Project.

ITK is a cross-platform, open-source application development framework widely used for the development of image segmentation and image registration programs. Segmentation is the process of identifying and classifying data found in a digitally sampled representation. Typically the sampled representation is an image acquired from such medical instrumentation as CT or MRI scanners. Registration is the task of aligning or developing correspondences between data. For example, in the medical environment, a CT scan may be aligned with an MRI scan in order to combine the information contained in both.

When installing a package on a Unix or Unix-like environment, a configure script is a shell script that generates build configuration files for a codebase to facilitate cross-platform support. It generates files tailoring for the host system – the environment on which the codebase is built and run.

<span class="mw-page-title-main">Vala (programming language)</span> Programming language

Vala is an object-oriented programming language with a self-hosting compiler that generates C code and uses the GObject system.

Qbs is a cross-platform free and open-source software for managing the build process of software. It was designed to support large, complex projects, written in any number of programming languages, primarily C/C++.

The Java Development Kit (JDK) is a distribution of Java technology by Oracle Corporation. It implements the Java Language Specification (JLS) and the Java Virtual Machine Specification (JVMS) and provides the Standard Edition (SE) of the Java Application Programming Interface (API). It is derivative of the community driven OpenJDK which Oracle stewards. It provides software for working with Java applications. Examples of included software are the Java virtual machine, a compiler, performance monitoring tools, a debugger, and other utilities that Oracle considers useful for Java programmers.

sbt (software) Open-source build tool for Scala and Java projects

sbt is an open-source build tool which can build Java, Scala, and Kotlin projects. It aims to streamline the procedure of constructing, compiling, testing, and packaging applications, libraries, and frameworks. sbt is highly adaptable, permitting developers to customize the build process according to their project's specific needs.

Dart is a programming language designed by Lars Bak and Kasper Lund and developed by Google. It can be used to develop web and mobile apps as well as server and desktop applications.

Mingw-w64 is a free and open-source suite of development tools that generate Portable Executable (PE) binaries for Microsoft Windows. It was forked in 2005–2010 from MinGW.

<span class="mw-page-title-main">Meson (software)</span> Build automation tool

Meson is a software build automation tool for building a codebase. Meson adopts a convention over configuration approach to minimize the data required to configure the most common operations. Meson is free and open-source software under the Apache License 2.0.

<span class="mw-page-title-main">Ninja (build system)</span> Free build automation software

Ninja is a build system developed by Evan Martin, a Google employee. Ninja has a focus on speed and it differs from other build systems in two major respects: it is designed to have its input files generated by a higher-level build system, and it is designed to run builds as fast as possible.

References

  1. "CMake 3.31.3 available for download". 10 January 2025. Retrieved 10 January 2025.
  2. "The CMake Open Source Project on OpenHub". OpenHub. Retrieved 9 April 2016.
  3. "CMake".
  4. 1 2 3 4 5 6 7 8 9 10 11 "The Architecture of Open Source Applications (Volume 1)CMake". aosabook.org. Retrieved 11 June 2023.
  5. "Licenses · master · CMake / CMake". GitLab. Retrieved 13 November 2020.
  6. "FLOSS Weekly 111: CMake". podcast. TWiT Network. Retrieved 27 February 2011.
  7. "The CABLE". Archived from the original on 19 June 2013. Retrieved 10 November 2010.
  8. Maynard, Robert (10 June 2014). "[CMake][ANNOUNCE] CMake 3.0.0 Released".
  9. "Effective Modern CMake". Gist.
  10. 1 2 3 Binna, Manuel (22 July 2018). "Effective Modern CMake".
  11. https://public.kitware.com/Bug/view.php?id=14012#c32631 [ bare URL ]
  12. "cmake-buildsystem(7) — CMake 3.19.0-rc3 Documentation". cmake.org. Retrieved 14 November 2020.
  13. 1 2 Pfeifer, Daniel (19 May 2017). Effective Cmake (PDF). CPPNow.
  14. 1 2 Neundorf, Alexander (21 June 2006). "Why the KDE project switched to CMake—and how". LWN.net.
  15. "CMake compiler feature detect". www.scivision.dev. 15 November 2020. Retrieved 22 January 2022.
  16. "Supported Compilers". CMake Documentation. Retrieved 22 January 2022.
  17. Berner, Dominik (2022). CMake Best Practices : Discover Proven Techniques for Creating and Maintaining Programming Projects with CMake. Mustafa Kemal Gilor. Birmingham: Packt Publishing, Limited. ISBN   978-1-80324-424-2. OCLC   1319218167.
  18. "cmake-packages(7) — CMake 3.24.1 Documentation". cmake.org. Retrieved 11 September 2022.
  19. "Exposing a module's configuration (advanced) — CMake build procedure 1.0 documentation". docs.salome-platform.org. Retrieved 11 September 2022.
  20. "target_precompile_headers — CMake 3.21.20210925-gb818e3c Documentation". cmake.org. Retrieved 25 September 2021.
  21. "CMake 3.19 Release Notes — CMake 3.19.7 Documentation". cmake.org. Retrieved 15 March 2021.
  22. "cmake-language(7) — CMake 3.19.0-rc2 Documentation". cmake.org. Retrieved 29 October 2020.
  23. Cedilnik, Andrej (30 October 2003). "Cross-Platform Software Development Using CMake Software". Linux Journal. Retrieved 29 January 2021.
  24. "cmake-language(7) — CMake 3.19.0-rc1 Documentation". cmake.org. Retrieved 25 October 2020.
  25. "cmake-modules(7) — CMake 3.14.7 Documentation". cmake.org. Retrieved 24 October 2020.
  26. "ExternalProject — CMake 3.14.7 Documentation". cmake.org. Retrieved 24 October 2020.
  27. "Packaging With CPack". CMake Community Wiki.
  28. cpack(1)    Linux General Commands Manual.
  29. "Building Blender - Blender Developer Wiki". wiki.blender.org. Retrieved 1 July 2021.
  30. Elmsheuser, J; Krasznahorkay, A; Obreshkov, E; Undrus, A (2017). "Large Scale Software Building with CMake in ATLAS" (PDF). CERN. Archived (PDF) from the original on 28 July 2018.
  31. "CMake Success". cmake.org. Kitware. 2011. Retrieved 12 March 2022.
  32. "cmake-toolchains(7) — CMake 3.19.0-rc2 Documentation". cmake.org. Retrieved 29 October 2020.