CMake Useful Variables

From KitwarePublic
Revision as of 17:49, 1 March 2006 by Alex (talk | contribs)
Jump to navigationJump to search

CMake uses and defines several variables, which can be used in CMakeLists.txt files.

Locations

CMAKE_BINARY_DIR
if you are building in-source, this is the same as CMAKE_SOURCE_DIR, otherwise this is the top level directory of your build tree
CMAKE_CURRENT_BINARY_DIR
if you are building in-source, this is the same as CMAKE_CURRENT_SOURCE_DIR, otherwise this is the directory where the compiled or generated files from the current CMakeLists.txt will go to
CMAKE_SOURCE_DIR
this is the directory, from which cmake was started, i.e. the top level source directory
CMAKE_CURRENT_SOURCE_DIR
this is the directory where the currently processed CMakeLists.txt is located in
PROJECT_BINARY_DIR
contains the full path to the top level directory of your build tree
PROJECT_SOURCE_DIR
contains the full path to the root of your project source directory, i.e. to the nearest directory where CMakeLists.txt contains the PROJECT() command
EXECUTABLE_OUTPUT_PATH
set this variable to specify a common place where CMake should put all executable files (instead of CMAKE_CURRENT_BINARY_DIR)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
LIBRARY_OUTPUT_PATH
set this variable to specify a common place where CMake should put all libraries (instead of CMAKE_CURRENT_BINARY_DIR)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
CMAKE_MODULE_PATH
tell CMake to search first in directories listed in CMAKE_MODULE_PATH when you use FIND_PACKAGE() or INCLUDE()
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/MyCMakeScripts)

FIND_PACKAGE(HelloWorld)

CMAKE_COMMAND
this is the complete path of the cmake which runs currently (e.g. /usr/local/bin/cmake)
CMAKE_ROOT
this is the CMake installation directory
CMAKE_CURRENT_LIST_FILE
this is the filename including the complete path of the file where this variable is used.
CMAKE_CURRENT_LIST_LINE
this is linenumber where the variable is used.

Environment Variables

$ENV{name}
returns the content of the environment variable with the given name (e.g. $ENV{PROGRAMFILES})
$ENV{name}
returns the content of the environment variable with the given name (e.g. $ENV{PROGRAMFILES})

System Information

CMAKE_SYSTEM
the complete system name, e.g. "Linux-2.4.22", "FreeBSD-5.4-RELEASE" or "Windows 5.1"
CMAKE_SYSTEM_NAME
the short system name, e.g. "Linux", "FreeBSD" or "Windows"
CMAKE_SYSTEM_VERSION
only the version part of CMAKE_SYSTEM
UNIX
is TRUE on all UNIX-like OS's, including Apple OS X and CygWin
WIN32
is TRUE on Windows, including CygWin
APPLE
is TRUE on Apple OS X
MINGW
is TRUE when using the MinGW compiler in Windows
CYGWIN
is TRUE on Windows when using the CygWin version of cmake
BORLAND
is TRUE on Windows when using a Borland compiler

Various Options

CMAKE_SKIP_RULE_DEPENDENCY
set this to true if you don't want to rebuild the object files if the rules have changed, but not the actual source files or headers (e.g. if you changed the some compiler switches)
CMAKE_SKIP_INSTALL_ALL_DEPENDENCY
since CMake 2.1 the install rule depends on all, i.e. everything will be built before installing. If you don't like this, set this one to true.
CMAKE_SKIP_RPATH
If set, runtime paths are not added when using shared libraries. Default it is set to OFF.
CMAKE_VERBOSE_MAKEFILE
set this to true if you are using makefiles and want to see the full compile and link commands instead of only the shortened ones
CMAKE_SUPPRESS_REGENERATION
this will cause CMake to not put in the rules that re-run CMake. This might be useful if you want to use the generated build files on another machine.

Compilers and Tools

A simple way to get switches to the compiler is to use ADD_DEFINITIONS(). But there are also two variables exactly for this purpose:

CMAKE_C_FLAGS
the compiler flags for compiling C sources
CMAKE_CXX_FLAGS
the compiler flags for compiling C++ sources
CMAKE_BUILD_TYPE
Choose the type of build. CMake has default flags for these:
  • None (CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used)
  • Debug (CMAKE_C_FLAGS_DEBUG or CMAKE_CXX_FLAGS_DEBUG)
  • Release (CMAKE_C_FLAGS_RELEASE or CMAKE_CXX_FLAGS_RELEASE)
  • RelWithDebInfo (CMAKE_C_FLAGS_RELWITHDEBINFO or CMAKE_CXX_FLAGS_RELWITHDEBINFO
  • MinSizeRel (CMAKE_C_FLAGS_MINSIZEREL or CMAKE_CXX_FLAGS_MINSIZEREL)

Example: SET(CMAKE_BUILD_TYPE Debug)

You can create your own build type like this:
SET(CMAKE_BUILD_TYPE distribution)
SET(CMAKE_CXX_FLAGS_DISTRIBUTION "-O3")
SET(CMAKE_C_FLAGS_DISTRIBUTION "-O3")

BUILD_SHARED_LIBS
if this is set to ON, then all libraries are built as shared libraries by default.
SET(BUILD_SHARED_LIBS ON)

The following variables are detected during the cmake run and set accordingly (e.g. to gcc). If you want to force the use of other tools, you can set these variables manually to the desired tools. E.g. if you want to use the gcc cross compiling toolchain for arm processors, you could do the following: SET(CMAKE_C_COMPILER arm-elf-gcc)

CMAKE_C_COMPILER
the compiler used for C files
CMAKE_CXX_COMPILER
the compiler used for C++ files
CMAKE_COMPILER_IS_GNUCC
if the compiler is a variant of gcc, this should be set to 1
CMAKE_COMPILER_IS_GNUCXX
if the compiler is a variant of g++, this should be set to 1
CMAKE_AR, CMAKE_RANLIB
the tools for creating libraries

Build rules

Build rules are defined in CMakeCInformation.cmake and CMakeCXXInformation.cmake.

Rules for C++ sources:

  • CMAKE_CXX_CREATE_SHARED_LIBRARY
  • CMAKE_CXX_CREATE_SHARED_MODULE
  • CMAKE_CXX_CREATE_STATIC_LIBRARY
  • CMAKE_CXX_COMPILE_OBJECT
  • CMAKE_CXX_LINK_EXECUTABLE

and the equivalents for C sources:

  • CMAKE_C_CREATE_SHARED_LIBRARY
  • CMAKE_C_CREATE_SHARED_MODULE
  • CMAKE_C_CREATE_STATIC_LIBRARY
  • CMAKE_C_COMPILE_OBJECT
  • CMAKE_C_LINK_EXECUTABLE

You can override the variables manually, e.g. replacing some flags in the linker command, but you can't change the value of the variables in sharp braces. Usually you don't have to change these rules, only in rare cases. You should only do this if you know what you are doing and there is no other way.



CMake: [Welcome | Site Map]