Jump to content

CMake: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
ce; minor fixes/; reorder a bit; name subsection
Adoption: Android NDK
Line 114: Line 114:
== Adoption ==
== Adoption ==
CMake has been very widely adopted among commercial, open source, and academic software projects. A few notable users include
CMake has been very widely adopted among commercial, open source, and academic software projects. A few notable users include
[[Netflix]], [[Inria]], [[MySQL]], [[Boost (C++ libraries)]], [[KeePassXC]], [[KDE]]/[[KDE Plasma 5]], [[KiCad|KiCAD]], [[FreeCAD]], [[WebKit|Webkit]], [[Blender (software)|Blender]],<ref>{{Cite web|title=Building Blender - Blender Developer Wiki|url=https://wiki.blender.org/wiki/Building_Blender|access-date=2021-07-01|website=wiki.blender.org}}</ref> Biicode, [[ReactOS]], Apache QPid, the [[ATLAS experiment]],<ref>{{Cite web|year=2017|title=Large Scale Software Building with CMake in ATLAS|url=https://cds.cern.ch/record/2243765/files/ATL-SOFT-PROC-2017-033.pdf|url-status=live|publisher=CERN
[[Android NDK]], [[Netflix]], [[Inria]], [[MySQL]], [[Boost (C++ libraries)]], [[KeePassXC]], [[KDE]], [[KiCad|KiCAD]], [[FreeCAD]], [[WebKit|Webkit]], [[Blender (software)|Blender]],<ref>{{Cite web|title=Building Blender - Blender Developer Wiki|url=https://wiki.blender.org/wiki/Building_Blender|access-date=2021-07-01|website=wiki.blender.org}}</ref> Biicode, [[ReactOS]], Apache QPid, the [[ATLAS experiment]],<ref>{{Cite web|year=2017|title=Large Scale Software Building with CMake in ATLAS|url=https://cds.cern.ch/record/2243765/files/ATL-SOFT-PROC-2017-033.pdf|url-status=live|publisher=CERN
|first1=J|last1=Elmsheuser
|first1=J|last1=Elmsheuser
|first2=A|last2=Krasznahorkay
|first2=A|last2=Krasznahorkay

Revision as of 13:33, 1 December 2022

CMake
Developer(s)Andy Cedilnik, Bill Hoffman, Brad King, Ken Martin, Alexander Neundorf
Initial release2000; 24 years ago (2000)
Stable release
3.31.0[1] Edit this on Wikidata / 7 November 2024
Repository
Written inC, C++[2]
Operating systemCross-platform
TypeSoftware development tools
LicenseBSD-3-Clause
Websitecmake.org Edit this on Wikidata

In software development, CMake is cross-platform free and open-source software for build automation, testing, packaging and installation of software by using a compiler-independent method.[3] CMake is not a build system itself; it generates another system's build files. 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 a permissive BSD-3-Clause license.[4]

History

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

A key feature is the ability to place compiler outputs (such as object files) outside of the source tree. This enables multiple builds from the same source tree and cross-compilation. Placing the compiler outputs outside of the source tree keeps the tree separate from the build files, ensuring that removing a build directory will not remove the source files. However, the users aren't protected in any way from removing the original source code folder.

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

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

Compilers

CMake supports an extensive list of compilers, including: Apple Clang, Clang, GNU GCC, MSVC, Oracle Developer Studio, and Intel C++ Compiler.[12]

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 CMakeList.txt 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.[13]: 132, 142 [14][15]

Build process

The build of a program or library with CMake is a two-stage process. First, standard build files (usually scripts) 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][16] The generated by CMake scripts are further used to produce actual binary files or else.

Generators

The build files are configured depending on the used generator (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 cmake's flag) into a folder outside of the source's one (out of source build), e.g., build/.

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

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

Types of build targets

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) linking instead. This enables flexibility in configuration of various optimizations.[18]

Build dependencies may be determined automatically.

Precompiled headers

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

Language

CMakeLists.txt

CMake has a relatively simple interpreted, imperative scripting language. It supports variables, string manipulation methods, arrays, function/macro declarations, and module inclusion (importing). CMake Language commands (or directives) are read by cmake from a file named CMakeLists.txt. This file specifies the source files and build parameters, which CMake will place in the project's build specification (such as a Makefile). Additionally, .cmake-suffixed files can contain scripts used by CMake.[20]

To generate a project's build files, one invokes cmake in terminal and specifies the directory that contains CMakeLists.txt. This file contains one or more commands in the form of COMMAND(argument ...).

Command syntax

The arguments of the commands are whitespace-separated and can include keywords to separate groups of arguments. Commands can take keywords. For instance, in the command SET_SOURCE_FILE_PROPERTIES(source_file ... COMPILE_FLAGS compiler_option ...) the keyword is COMPILE_FLAGS. It serves as a delimiter between the list of source files and some other options.[21]

Examples of commands that CMake offers to specify targets and their dependencies and which serve as the starting point of the CMakeLists.txt:[22][23][24]

  • add_executable(...)— declares an executable binary target with sources (depend on language chosen) to be built
  • add_library(...) — the same but for a library
  • target_link_libraries(...) — adds dependencies etc.

JSON strings

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

Internals

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

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

Modules and tools

CMake ships with numerous .cmake modules and tools. These facilitate work such as finding dependencies (both built-in and external, e.g. FindXYZ modules), testing the toolchain environment and executables, packaging releases (CPack module and cpack command), and managing dependencies on external projects (ExternalProject module):[27][28]

  • ctest — is used for target testing commands specified by CMakeLists.txt
  • ccmake and cmake-gui — tweaks and updates configuration variables intended for the native build system
  • cpack — helps to package software

CPack

CPack is a packaging system for software distributions. It is tightly integrated with CMake but can function without it. [29][30]

It can be used to generate:

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,[31] Biicode, ReactOS, Apache QPid, the ATLAS experiment,[32] and Second Life.[33]

Examples

Hello world

The following source code files demonstrate how to build a simple hello world program written in C++ by using CMake.

// hello.cpp
#include <iostream>

int main()
{
    std::cout << "Hello, world!\n";
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(HelloWorld CXX)
add_executable(hello hello.cpp)

Shell commands to run CMake on a Linux system (to be entered in the directory that contains the two files above):

cmake -B build .          # Configure the build directory.
cmake --build build       # Build the program in the build directory.
./build/hello             # Run the program (outputs "Hello, world!")


See also

References

  1. ^ "CMake 3.31.0 available for download". 7 November 2024. Retrieved 14 November 2024.
  2. ^ "The CMake Open Source Project on OpenHub". OpenHub. Retrieved 9 April 2016.
  3. ^ "CMake".
  4. ^ "Licenses · master · CMake / CMake". GitLab. Retrieved 13 November 2020.
  5. ^ "FLOSS Weekly 111: CMake". podcast. TWiT Network. Retrieved 27 February 2011.
  6. ^ "The CABLE". Archived from the original on 19 June 2013. Retrieved 10 November 2010.
  7. ^ Maynard, Robert (10 June 2014). "[CMake] [ANNOUNCE] CMake 3.0.0 Released".
  8. ^ "Effective Modern CMake". Gist.
  9. ^ a b Binna, Manuel (22 July 2018). "Effective Modern CMake".
  10. ^ a b Neundorf, Alexander (21 June 2006). "Why the KDE project switched to CMake—and how". LWN.net.
  11. ^ "CMake compiler feature detect". www.scivision.dev. 15 November 2020. Retrieved 22 January 2022.
  12. ^ "Supported Compilers". CMake Documentation. Retrieved 22 January 2022.
  13. ^ 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.
  14. ^ "cmake-packages(7) — CMake 3.24.1 Documentation". cmake.org. Retrieved 11 September 2022.
  15. ^ "Exposing a module's configuration (advanced) — CMake build procedure 1.0 documentation". docs.salome-platform.org. Retrieved 11 September 2022.
  16. ^ "cmake-toolchains(7) — CMake 3.19.0-rc2 Documentation". cmake.org. Retrieved 29 October 2020.
  17. ^ Pfeifer, Daniel (19 May 2017). Effective Cmake (PDF). CPPNow.
  18. ^ "cmake-buildsystem(7) — CMake 3.19.0-rc3 Documentation". cmake.org. Retrieved 14 November 2020.
  19. ^ "target_precompile_headers — CMake 3.21.20210925-gb818e3c Documentation". cmake.org. Retrieved 25 September 2021.
  20. ^ "cmake-language(7) — CMake 3.19.0-rc2 Documentation". cmake.org. Retrieved 29 October 2020.
  21. ^ Cedilnik, Andrej (30 October 2003). "Cross-Platform Software Development Using CMake Software". Linux Journal. Retrieved 29 January 2021.
  22. ^ "add_executable — CMake 3.19.0-rc1 Documentation". cmake.org. Retrieved 25 October 2020.
  23. ^ "add_library — CMake 3.19.0-rc1 Documentation". cmake.org. Retrieved 25 October 2020.
  24. ^ "target_link_directories — CMake 3.20.2 Documentation". cmake.org. Retrieved 10 May 2021.
  25. ^ "CMake 3.19 Release Notes — CMake 3.19.7 Documentation". cmake.org. Retrieved 15 March 2021.
  26. ^ "cmake-language(7) — CMake 3.19.0-rc1 Documentation". cmake.org. Retrieved 25 October 2020.
  27. ^ "cmake-modules(7) — CMake 3.14.7 Documentation". cmake.org. Retrieved 24 October 2020.
  28. ^ "ExternalProject — CMake 3.14.7 Documentation". cmake.org. Retrieved 24 October 2020.
  29. ^ "Packaging With CPack". CMake Community Wiki.
  30. ^ cpack(1) – Linux General Commands Manual.
  31. ^ "Building Blender - Blender Developer Wiki". wiki.blender.org. Retrieved 1 July 2021.
  32. ^ 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.
  33. ^ "CMake Success". cmake.org. Kitware. 2011. Retrieved 12 March 2022.