CMake is a cross-platform, open-source meta build system to build, test and package software. It creates Makefile or other build files (KDevelop, Eclipse). The intention is to test it if people like it and if we could replace the current build system with CMake.
Lets start to do a checkout and compile something. Run some commands and then come back and talk about the features.
$ git clone git://gitorious.org/samba-wip/samba-wip.git $ cd samba-wip $ git checkout -b cmake origin/cmake $ cd lib/talloc
CMake is designed to build out of the source. This has several advantages. You don't have to create thousand of entries in .gitignore. You can create a build directory for *every local git branch* you create. CMake can be invoked by differnet executables:
- cmake: the commandline tool
- ccmake: a ncurses based gui
- cmake-gui: a Qt gui.
$ mkdir build $ cd build $ cmake ..
This is a talloc stand alone build. Normally you have to copy libreplace in the talloc dir but there is a check to look for ../replace, if you're in the Samba tree. It does all the required configure checks here to build on Linux as this is just a test.
I think you know what it does. What's nice it shows only what it's building and the warnings and errors. If you want to see how make/cmake invokes the compiler, try the following:
$ make clean $ make VERBOSE=1
To see what targets are availalbe, you can use
$ make help
If you go to a source directory you have a target for each .c file.
$ cd replace $ make help $ cd ..
You can execute every binary in the build directory and it will work. CMake uses rpath to build executables so that test scripts are able to find the right libraries, and things like make test can be run from the build directory without requiring you to install or build everything static. Executables are re-linked at install time to remove the rpath restriction. http://www.cmake.org/Wiki/CMake_RPATH_handling See:
$ ldd talloc_testsuite
Building with RPATH can be disabled with 'cmake -DSKIP_BUILD_RPATH=OFF ..'. You have to set environment variables in this case to get executables working.
Lets look at tevent. I've created a way to let tevent use talloc from the system or compile it in source if you're in the samba tree.
$ cd lib/tevent $ mkdir build $ cd build $ cmake .. $ make
This will build against the talloc system library. CMake uses so called "Find Packages" to find a library. There is no standard way to search for libraries and header files so CMake created a way to to it. You write a module and all you have to do is call 'find_package(Talloc REQUIRED)'. You can take a look at 'cmake/Modules/FindTalloc.cmake' in the tevent source directory if you want. Here is the way to build it in the Samba source tree.
$ cd lib/tevent/build $ rm CMakeCache.txt $ cmake -DWITH_SYSTEM_TALLOC=OFF .. $ make
CMake is pretty simple to learn. The way to tell CMake what to do, is to create a CMakeLists.txt file in each source directory. If you want to get started I suggest to look at http://www.cmake.org/cmake/help/examples.html.
I've tried to document the CMakeLists.txt a bit to make it easier to start. Everything else is documented in 'man cmake'.
If you want to use functions or macros which are not covered by the cmake documentation then you find it at the top of each file. They are in '/usr/share/cmake/Modules' or in cmake/Modules if you look at replace/talloc/tevent here.
If you need help with cmake there are different sources:
- The manpage 'man cmake'
- The Wiki http://www.cmake.org/Wiki/Main_Page
- The #cmake channel at Freenode
- The mailing list which is pretty active
Advantages of CMake
- Out of source build
- Automatic dependency generation (parallel builds)
- Build system in *one* language
- Good documentation (man cmake)
- Works on a lot of different machines and operating systems -> http://www.cdash.org/CDash/index.php?project=CMake
- Complex custom commands: qt moc, yacc, pidl!
- Reruns itself if one of the input files has changed
- Compiler independent