Developer(s) | Andy Cedilnik, Bill Hoffman, Brad King, Ken Martin, Alexander Neundorf |
---|---|
Initial release | 2000 |
Stable release | |
Repository | |
Written in | C, C++ [2] |
Operating system | Cross-platform |
Type | Software development tools |
License | BSD-3-Clause |
Website | cmake |
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. [4] It supports directory hierarchies and applications that depend on multiple libraries. It can invoke native build environments such as Make, Qt Creator, Ninja, Android Studio, Apple's Xcode, and Microsoft Visual Studio. [4] It has minimal dependencies, requiring only a C++ compiler on its own build system. [4]
CMake is distributed as free and open-source software under a permissive BSD-3-Clause license. [5]
CMake development began in 1999, in response to the need for a cross-platform build environment for the Insight Segmentation and Registration Toolkit (ITK). [6] [4] The project is funded by the United States National Library of Medicine as part of the Visible Human Project. [4] It was partially inspired by pcmaker
, a predecessor to CMake, which was made by Ken Martin and other developers to support building of the Visualization Toolkit (VTK). pcmaker was a C program that converted Make files into MS Windows' NMake counterparts. [4] 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.
Historically CMake was conceived with the following major features in mind: [4]
Because of these constraints CMake didn't choose to use Tcl (popular at the time) scripting language as its default and instead, developers decided to create a simpler scripting language. [4]
Continued 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.
Developer Brad King has stated that "the 'C' in CMake stands for 'cross-platform'". [11]
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. [4] 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. [4]
CMake keeps track and recompiles all the upstream dependencies of a given sub-module if its sources are changed. [4] [lower-alpha 1]
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. [12]
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.
CMake allows specification of features that the compiler is required to support in order to get the target program or library compiled. [13]
CMake supports an extensive list of compilers, including: Apple Clang, Clang, GNU GCC, MSVC, Oracle Developer Studio, and Intel C++ Compiler. [14]
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. [15] : 132, 142 [16] [17]
Cmake may be run by using a ncurses program like ccmake
that can be used to configure projects via command-line interface.
The build of a program or library with CMake is a two-stage process. [4] First, build files (usually scripts) are created (generated) from configuration files (CMakeLists.txt scripts) written in CMake language. Then the platform's native build tools that can read these build files (native toolchain) are invoked either manually externally or via cmake --build
for actual building of programs (build targets). [12] [18] The generator specified by the user on the commandline determines which build tool chain to use. [4]
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 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 can be 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. [10] [19] The older approach is discouraged now.
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. [20]
Build dependencies may be determined automatically.
It's possible to generate precompiled headers by using CMake since version 3.6. [21]
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. [22]
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 ...)
.
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_FLAGScompiler_option...)
the keyword is COMPILE_FLAGS
. It serves as a delimiter between the list of source files and some other options. [23]
Examples of commands that CMake offers to specify targets and their dependencies and which serve as the starting point of the CMakeLists.txt: [24] [25] [26]
add_executable(...)
— declares an executable binary target with sources (depend on language chosen) to be builtadd_library(...)
— the same but for a librarytarget_link_libraries(...)
— adds dependencies etc.CMake supports extracting values into variables from JSON-data strings (since version 3.19). [27]
The CMake scripting language is implemented by using Yacc and Lex generators. [lower-alpha 2]
The executable programs CMake, CPack, and CTest are written in the C++ programming language. [lower-alpha 2]
Much of CMake's functionality is implemented in modules that are written in the CMake language. [28]
Since release 3.0, CMake's documentation uses reStructuredText markup. HTML pages and man pages are generated by the Sphinx documentation generator.
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): [29] [30]
CPack is a packaging system for software distributions. It is tightly integrated with CMake but can function without it. [31] [32]
It can be used to generate:
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, [33] Biicode, ReactOS, Apache Qpid, the ATLAS experiment, [34] and Second Life. [35]
The following source code files demonstrate how to build a simple hello world program written in C++ by using CMake.
// hello.cpp#include<iostream>intmain(){std::cout<<"Hello, world!"<<std::endl;return0;}
# CMakeLists.txtcmake_minimum_required(VERSION3.5)project(HelloWorldCXX)add_executable(hellohello.cpp)
// hello.cpp#include"hello_heading.h"#include<iostream>intmain(){for(inti=0;i<Times;i++){std::cout<<"Hello, world!"<<std::endl;}return0;}
// hello_heading.h#ifndef HELLO_HEADING_H_#define HELLO_HEADING_H_constintTimes=10;#endif
# CMakeLists.txtcmake_minimum_required(VERSION3.5)project(HelloWorldCXX)include_directories(${PROJECT_SOURCE_DIR})add_executable(hellohello.cpp)
Shell commands to run CMake on a Linux system (to be entered in the directory that contains the two files above):
cmake-Bbuild.# Configure the build directory. cmake--buildbuild# Build the program in the build directory. ./build/hello# Run the program (outputs "Hello, world!")
A package manager or package-management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer in a consistent manner.
GNU Autoconf is a tool for producing configure scripts for building, installing, and packaging software on computer systems where a Bourne shell is available.
The GNU Autotools, also known as the GNU Build System, is a suite of programming tools designed to assist in making source code packages portable to many Unix-like systems.
In computer programming, GNU Libtool is a software development tool, part of the GNU build system, consisting of a shell script created to address the software portability problem when compiling shared libraries from source code. It hides the differences between computing platforms for the commands which compile shared libraries. It provides a command-line interface that is identical across platforms and it executes the platform's native commands.
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.
In software development, GNU Automake is a programming tool to automate parts of the compilation process. It eases usual compilation problems. For example, it points to needed dependencies.
Portage is a package management system originally created for and used by Gentoo Linux and also by ChromeOS, Calculate, Sabayon, and Funtoo Linux among others. Portage is based on the concept of ports collections. Gentoo is sometimes referred to as a meta-distribution due to the extreme flexibility of Portage, which makes it operating-system-independent. The Gentoo/Alt project was concerned with using Portage to manage other operating systems, such as BSDs, macOS and Solaris. The most notable of these implementations is the Gentoo/FreeBSD project.
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.
SCons is a computer software build tool that automatically analyzes source code file 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 analogous to the traditional GNU build system based on the make utility and the autoconf tools.
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.
Waf is a build automation tool designed to assist in the automatic compilation and installation of computer software. It is written in Python and maintained by Thomas Nagy.
A configure script is an executable script designed to aid in developing a program to be run on a wide number of different computers. It matches the libraries on the user's computer, with those required by the program before compiling it from its source code.
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++.
sbt is an open-source build tool created explicitly for Scala and Java 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.
cdist is a free software configuration management tool for Unix-like systems. It manages nodes over SSH using the Bourne Shell, and does not require any additional software to be installed on target nodes.
Mingw-w64 is a free and open-source suite of developments tools that generate Portable Executable (PE) binaries for Microsoft Windows. It was forked in 2005–2010 from MinGW.
Meson is a software tool for automating the building (compiling) of software. Meson adopts a convention over configuration approach, promoting standard development tools and practice instead of providing unlimited flexibility to the user. Meson is free and open-source software written in Python, under the Apache License 2.0.