User Tools

Site Tools


info:cmake

CMake

See also CMake wiki page on internal company wiki.

I've been trying to start using CMake for some things. It seems like it may be the best thing available, but some things make me cringe. These are just some random observations and links:

Basics

CMake is configured with a "CMakeLists.txt" file in each subdirectory.

Build can (probably should) be done "out-of-source". To start a build, do something like this:

# in top-level project directory
mkdir build
cd build
ccmake .. # use interface to configure
make

To build in-source, skip the "build" directory and run ccmake ..

"ccmake" is a Curses (console) UI for configuring a project. Use "cmake-gui" for a Qt4-based GUI, or "cmake" for non-interactive mode (variables can be defined with the "-D" option).

A common non-interactive build might look like this:

mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make
make install #optional

Potential Gotchas

Important Differences from qmake

Unlike qmake, CMake subdirectories inherit the environment of the parent directories.

Using "glob" for Filenames

Sometimes I use "glob" to get a list of files, rather than listing them each in the CMake file. For example:

file(GLOB SOURCES *.cpp)

If this is done, then CMake needs to be manually rerun when files are added or removed (or renamed). Just running "cmake .." from the build directory will retain all the settings from previous runs.

An alternative (that appears to be commonly used) is to list each file in the CMake file. That way, CMake automatically gets rerun when you run "make". Either way, something manually needs to be done, but this way is more explicit.

Eclipse Plugins

I've had more luck so far with http://cmakeed.sourceforge.net/, but http://www.cmakebuilder.com/ looks interesting.

Coding Style

The KDE CMake Coding Style seems reasonable. In short:

  • Use lowercase commands
  • Use spaces for indentation (2/4/6; be consistent within a file); I like 4 spaces, because that's what Eclipse will insert by default with the CMakeEd plugin.

See also cmake-lint.

Writing Find Modules

There appears to be some conflicting information. The most canonical appears to be the readme mentioned here: [1], Modules/readme.txt on ViewCVS.

It can be useful to look at existing find modules, but they are very inconsistent.

Relevant pages on the CMake wiki:

Issues

  • CMake uses "/usr/bin/c++" as its default C++ compiler. This may cause an issue on machines where "g++" has, for whatever reason, been replaced with a non-OS version (without bothering to replace "/usr/bin/c++").
    • The best solution is not to replace g++. Bad idea. Next to that, either change the compiler in the CMake configuration or replace "/usr/bin/c++" with a copy of "/usr/bin/g++".
  • CMake documentation is okay, but CPack documentation is almost nonexistant. I can't find any place that lists all of the variables it uses; I keep finding things I need in the notes of Mantis bugs or mailing list posts.

CPack Notes

Module help may have stuff not in the online documentation. Examples:

cmake --help-module-list
cmake --help-module CPack
cmake --help-module CPackRPM

Build-Time SVN Revision

It's easy to use the FindSubversion module, but then the revision number doesn't get updated until you re-run CMake. There are a few different ways to get around this.

Examples of creating a header containing the SVN version: http://stackoverflow.com/questions/3780667/use-cmake-to-get-build-time-svn-revision

For setting CPack variables, you can set CPACK_PROJECT_CONFIG_FILE (something like CMakeCPackOptions.cmake or CPackOptions.cmake), which gets run once each build for each generator, but there are some gotchas. In particular, you don't have access to CMAKE_... variables, so you may need to generate the CPack project config file from a ".in" using configure_file. See http://cmake.3232098.n2.nabble.com/Automatically-add-a-revision-number-to-the-CPack-installer-name-td7356239.html.

info/cmake.txt · Last modified: 2014-01-14 17:26 by sam