CMake

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
CMake
Cmake.svg
Developer(s)Andy Cedilnik, Bill Hoffman, Brad King, Ken Martin, Alexander Neundorf
Initial release2000; 21 years ago (2000)
Stable release
3.19.3[1] / January 13, 2021; 2 days ago (2021-01-13)
Repository Edit this at Wikidata
Written inC, C++[2]
Operating systemCross-platform
TypeSoftware development tools
LicenseNew BSD
Websitecmake.org Edit this on Wikidata

In software development, CMake is cross-platform free and open-source software for build automation, testing and packaging using a compiler-independent method.[3] CMake is not a build system but rather it's a build-system generator. It supports directory hierarchies and applications that depend on multiple libraries. It is used in conjunction with native build environments such as Make, Qt Creator, Ninja, Android Studio, Apple's Xcode, and Microsoft Visual Studio. It has minimal dependencies, requiring only a C++ compiler on its own build system.

CMake is distributed as open-source software under permissive New BSD license.[4]

History[edit]

CMake development began in 1999 in response to the need for a cross-platform build environment for the Insight Segmentation and Registration Toolkit.[5] The project is funded by the United States National Library of Medicine as part of the Visible Human Project. It was partially inspired by pcmaker, which was made by Ken Martin and other developers to support the Visualization Toolkit (VTK). At Kitware, Bill Hoffman blended components of pcmaker with his own ideas, striving to mimic the functionality of Unix configure scripts. CMake was first implemented in 2000 and further developed in 2001.

Continued development and improvements were fueled by the incorporation of CMake into developers’ own systems, including the VXL Project,[clarification needed] the CABLE[6] 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.[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 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.

Features[edit]

CMake can handle in-source and out-of-source builds, enabling several builds from the same source tree, and cross-compilation. The ability to build a directory tree outside the source tree is a key feature, ensuring that if a build directory is removed, the original source files remain unaffected.

Flexible project structure[edit]

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 the 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.[10]

IDEs configuration support[edit]

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.

Build process[edit]

The build process of a program or library with CMake takes place in two stages. First, standard build files are created (generated) from configuration files (CMakeLists.txt) which are written in CMake language. Then the platform's native build tools (native toolchain) are used for actual building of programs.[10][11]

The build files are configured depending on used generator (e.g. Unix Makefiles for make). 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 cmake flag) into a different from sources folder e.g. build/.

Each build project in turn contains a CMakeCache.txt file and CMakeFiles directory in every (sub-)directory of the projects (happened to be included by add_subdirectory(...) command earlier) helping to avoid or speed up regeneration stage once it's run over again.

Types of build targets[edit]

Depending on CMakeLists.txt configuration the build files may be either executables, libraries (e.g. libxyz, xyz.dll etc), object file libraries or 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) one instead. This enables flexibility in configuration of various optimizations.[12]

Language[edit]

CMake language is a relatively simple interpreted, functional scripting language which supports basic features such as variables, strings manipulation, arrays, function/macro declarations, and module inclusion (import) common to many other languages. CMake Language commands are read by cmake from CMakeLists.txt and .cmake-suffixed textual files (the latter are treated as scripts, not toolchain's files generator).[13]

At the stage of build files generation (to be consumed by the native toolchain) cmake takes as input path to directory which contains CMakeLists.txt whose commands in turn drive CMake's behavior and output. It has one or more commands in the form COMMAND(args...), with COMMAND taking arguments declared by a set of variables or strings each separated by whitespace or (optionally) a keyword specific to that command. While there are many built-in rules (commands) for compiling the software libraries (static and dynamic) and executables, there are also means to make custom ones like functions or macros. Some build dependencies can be determined automatically.

Users basically have to specify within scripts what they intend to build by using basic add_executable(...) and add_library(...) commands.[14][15] From there on they may either tweak compiler's behavior relative to the target they are building via target properties (as of v3.1 version) or set up configuration globally via CMAKE_... prefixed variables. The latter is discouraged though for target-only configs as such variables are also used to config CMake itself or set up globally initial values.[9]

Internals[edit]

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

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

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

Modules[edit]

CMake is shipped with numerous .cmake modules which among things help users to do routine work ranging from finding dependencies via FindXYZ modules to testing both binaries and environment (dependencies and toolchain) and packaging releases by CPack module and cpack executable.[17] CMake also provide importable module to manage external projects called ExternalProject.[18]

CPack[edit]

CPack is a packaging system for software distributions which is tightly integrated with CMake, but can function without it.[19][20]

It can be used to generate:

Examples[edit]

Hello World[edit]

The following source code files (assumed to be put into src/ folder) demonstrate compilation of basic hello world program called hello written in C++.

// src/Hello_world.cc
#include <iostream>

int main()
{
    std::cout << "Hello, world!\n";
}
# src/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)

# set the project name
project("Hello World")

# executable to compile
add_executable(hello "Hello_world.cc")

In order for CMake to work you have to run the following bash script (placed next to the src/ folder assuming that you are working under Linux-based OS and have all necessary dependencies installed):

#!/usr/bin/env bash
                            # Place this file next to src/ folder
cmake -S src/ -B src.build/ # Generates building files into src.build/ folder
cmake --build src.build/    # Actually builds executable
src.build/hello             # => Hello, world! - output from compiled program

See also[edit]

References[edit]

  1. ^ "CMake Release Tags on GitLab". Retrieved 13 January 2021.
  2. ^ "The CMake Open Source Project on OpenHub". OpenHub. Retrieved 2016-04-09.
  3. ^ "CMake".
  4. ^ "Licenses · master · CMake / CMake". GitLab. Retrieved 2020-11-13.
  5. ^ "FLOSS Weekly 111: CMake". podcast. TWiT Network. Retrieved 27 February 2011.
  6. ^ "The CABLE". Archived from the original on 2013-06-19. Retrieved 2010-11-10.
  7. ^ Maynard, Robert (June 10, 2014). "[CMake] [ANNOUNCE] CMake 3.0.0 Released".
  8. ^ "Effective Modern CMake". Gist.
  9. ^ a b https://github.com/boostcon/cppnow_presentations_2017/blob/master/05-19-2017_friday/effective_cmake__daniel_pfeifer__cppnow_05-19-2017.pdf, https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1
  10. ^ a b Neundorf, Alexander (2006-06-21). "Why the KDE project switched to CMake—and how". LWN.net.
  11. ^ "cmake-toolchains(7) — CMake 3.19.0-rc2 Documentation". cmake.org. Retrieved 2020-10-29.
  12. ^ "cmake-buildsystem(7) — CMake 3.19.0-rc3 Documentation". cmake.org. Retrieved 2020-11-14.
  13. ^ "cmake-language(7) — CMake 3.19.0-rc2 Documentation". cmake.org. Retrieved 2020-10-29.
  14. ^ "add_executable — CMake 3.19.0-rc1 Documentation". cmake.org. Retrieved 2020-10-25.
  15. ^ "add_library — CMake 3.19.0-rc1 Documentation". cmake.org. Retrieved 2020-10-25.
  16. ^ "cmake-language(7) — CMake 3.19.0-rc1 Documentation". cmake.org. Retrieved 2020-10-25.
  17. ^ "cmake-modules(7) — CMake 3.14.7 Documentation". cmake.org. Retrieved 2020-10-24.
  18. ^ "ExternalProject — CMake 3.14.7 Documentation". cmake.org. Retrieved 2020-10-24.
  19. ^ "Packaging With CPack". CMake Community Wiki.
  20. ^ cpack(1) – Linux General Commands Manual

External links[edit]