CMake tutorial

and its friends CPack, CTest and CDash

Eric NOULARD - eric.noulard@gmail.com

http://www.cmake.org February, 8th 2012

This presentation is licensed http://creativecommons.org/licenses/by-sa/3.0/us/

CMake tutorial

1 / 118

Thanks to. . .
Kitware for making a really nice set of tools and making them open-source the CMake mailing list for its friendliness and its more than valuable source of information CMake developers for their tolerance when I break the dashboard or mess-up with the git workflow, CPack users for their patience when things don’t work as they shouldexpect Alan, Alex, Bill, Brad, Clint, David, Eike, Julien, Mathieu, Michael & Michael, and many more. . . My son Louis for the nice CPack 3D logo done with Blender. and...Toulibre for hosting this presention in Toulouse, France.
CMake tutorial 2 / 118

Outlines

CMake tool sets
CMake CMake is a cross-platform build systems generator which makes it easier to build software in a unified manner on a broad set of platforms: , Windows, MacOS, AIX, IRIX, , iOS · · ·

CMake has friends softwares that may be used on their own or together:
CMake: build system generator CPack: package generator CTest: systematic test driver CDash: a dashboard collector
CMake tutorial 3 / 118

Outlines Outline of Part I: CMake 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 4 / 118 .

Outlines Outline of Part II: CPack 5 CPack: Packaging made easy 6 CPack with CMake 7 Various package generators CMake tutorial 5 / 118 .

Outlines Outline of Part III: CTest and CDash 8 Systematic Testing 9 CTest submission to CDash 10 References CMake tutorial 6 / 118 .

Why do we need that? .Outlines Build what? Software build system A software build system is the usage of a [set of] tool[s] for building software applications.

.Outlines Build what? Software build system A software build system is the usage of a [set of] tool[s] for building software applications. Why do we need that? because most softwares consist in several parts that need some building to put them together.

Why do we need that? because most softwares consist in several parts that need some building to put them together.Outlines Build what? Software build system A software build system is the usage of a [set of] tool[s] for building software applications. . because softwares are written in various languages that may share the same building process.

because we want to build the same software for various computers (PC. embbeded computers) and systems (Windows. . *BSD. Workstation. . because softwares are written in various languages that may share the same building process. Linux. mobile phones and other PDA. Android. Macintosh.Outlines Build what? Software build system A software build system is the usage of a [set of] tool[s] for building software applications. etc. other Unices (many). Why do we need that? because most softwares consist in several parts that need some building to put them together. ) CMake tutorial 7 / 118 .

C. C++. .. Python Programming languages Perl OCaml executable C++ object code Fortran C compiles links executes ?byte-compile? interpreter interprets Running program CMake tutorial 8 / 118 . OCaml.Outlines Programming languages Compiled vs interpreted or what? Building an application requires the use of some programming language: Python. Tcl/Tk. Ruby. Java. . Fortran. Go. Perl.

Ruby. Java. Perl. . Fortran. C++. .. C. OCaml.Outlines Programming languages Compiled vs interpreted or what? Building an application requires the use of some programming language: Python. Python Programming languages Perl OCaml executable C++ object code Fortran C compiles links executes ?byte-compile? interpreter interprets Running program CMake tutorial 8 / 118 . Tcl/Tk. Go.

http://www. dedicated to Java (almost).apache. .Outlines Build systems: several choices Alternatives CMake is not the only build system [generator]: (portable) hand-written Makefiles. Produce makefiles. Portable IDE: Eclipse. Needs bourne shell (and M4 macro processor). Automake. Code::Blocks. depends on make tool.scons.gnu. . CMake tutorial 9 / 118 .org/software/autoconf/ SCons: http://www. ... Libtool. NetBeans. Geany. Apache ant http://ant.org/. GNU Autotools: Autoconf. . see e.org only depends on python.g. Extensible with python.

I am a big CMake fan. thus I’m not impartial at all.org/wiki/List_of_build_ automation_software A comparison: http://www. But I will be ready to discuss why CMake is the greatest build system out there :-) Go and forge your own opinion: Bare list: http://en.scons. I’m contributing to CMake. I mean totally.org/wiki/SconsVsOtherBuildTools KDE success story (2006): “Why the KDE project switched to CMake – and how” http://lwn.wikipedia.net/Articles/188693/ CMake tutorial 10 / 118 .Outlines Comparisons and [success] stories Disclaimer This presentation is biased.

net/languages/compare Language comparison of CMake to automake and autoconf showing the percentage of developers commits that modify a source file of the respective language.ohloh. CMake tutorial 11 / 118 .Outlines CMake/Auto[conf|make] on Ohloh https://www.

google.com/trends Scale is based on the average worldwide traffic of cmake in all years. CMake tutorial 12 / 118 .Outlines CMake/Auto[conf|make] on Google Trend http://www.

Basic CMake usage Outline 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 13 / 118 .

CMake may be (automatically) re-invoked by the build system. IDE project files. ).Basic CMake usage A build system generator CMake is a generator: it generates native build systems files (Makefile. invoke CMake but should never edit the generated files. CMake tutorial 14 / 118 . CMake scripting language (declarative) is used to describe the build. . The developer edit CMakeLists. .txt. .

e. XCode. . ) does.Basic CMake usage The CMake workflow When do things take place? CMake is a generator so it does not compile (i. Code::Blocks. the underlying build tool (make. . build) the sources. CMake tutorial 15 / 118 .

txt When do things take place? CMake is a generator so it does not compile (i.Basic CMake usage The CMake workflow 1 CMake time: CMake is running & processing CMakeLists. ) does. Code::Blocks.e. the underlying build tool (make. build) the sources. XCode. CMake tutorial 15 / 118 . . .

. the underlying build tool (make.e. ) does. Code::Blocks.txt Build time: the build tool runs and invokes (at least) the compiler When do things take place? CMake is a generator so it does not compile (i. XCode. build) the sources. . CMake tutorial 15 / 118 .Basic CMake usage The CMake workflow 1 2 CMake time: CMake is running & processing CMakeLists.

e. ) does. . Code::Blocks. the underlying build tool (make. build) the sources. When do things take place? CMake is a generator so it does not compile (i.e. CMake tutorial 15 / 118 . from build area to an install location.Basic CMake usage The CMake workflow 1 2 3 CMake time: CMake is running & processing CMakeLists. XCode.txt Build time: the build tool runs and invokes (at least) the compiler Install time: the compiled binaries are installed i. .

Basic CMake usage

The CMake workflow
1 2 3

CMake time: CMake is running & processing CMakeLists.txt Build time: the build tool runs and invokes (at least) the compiler Install time: the compiled binaries are installed i.e. from build area to an install location. CPack time: CPack is running for building package

4

When do things take place? CMake is a generator so it does not compile (i.e. build) the sources, the underlying build tool (make, XCode, Code::Blocks. . . ) does.
CMake tutorial

15 / 118

Basic CMake usage

The CMake workflow
1 2 3

CMake time: CMake is running & processing CMakeLists.txt Build time: the build tool runs and invokes (at least) the compiler Install time: the compiled binaries are installed i.e. from build area to an install location. CPack time: CPack is running for building package Package Install time: the package (from previous step) is installed

4 5

When do things take place? CMake is a generator so it does not compile (i.e. build) the sources, the underlying build tool (make, XCode, Code::Blocks. . . ) does.
CMake tutorial

15 / 118

Basic CMake usage

The CMake workflow (pictured)
CMakeLists.txt

Source files

CMake tutorial

16 / 118

. Source files Generated Sources files CMake time CMake tutorial 16 / 118 .txt Project file(s). . Makefiles. .Basic CMake usage The CMake workflow (pictured) CMakeLists.

Object files Source files Generated Sources files CMake time Build time CMake tutorial 16 / 118 .txt Project file(s). . . Makefiles. .Basic CMake usage The CMake workflow (pictured) CMakeLists.

Object files Source files Generated Sources files CMake time Build time Install time Installed files CMake tutorial 16 / 118 . Makefiles. .txt Project file(s). .Basic CMake usage The CMake workflow (pictured) CMakeLists. .

.Basic CMake usage The CMake workflow (pictured) CMakeLists. . . Makefiles.txt Project file(s). Object files Source files Generated Sources files CMake time Installed files Binary package Source package Build time Install time CPack time CMake tutorial 16 / 118 .

txt Project file(s). . . Makefiles.Basic CMake usage The CMake workflow (pictured) CMakeLists. Object files Source files Generated Sources files CMake time Installed files Binary package Source package Build time Install time CPack time Package Install time Installed package CMake tutorial 16 / 118 . .

. c ) CMake scripting language is [mostly] declarative.Basic CMake usage Building an executable Listing 1: Building a simple program cmake minimum required (VERSION 2 . 8 ) # T h i s p r o j e c t use C s o u r c e code project ( TotallyFree C) # build executable using s p e c i f i e d # l i s t of source f i l e s add executable ( A c r o l i b r e a c r o l i b r e .. CMake tutorial 17 / 118 1 2 3 4 5 6 . add_executable Add an executable to the project using the specified source files. It has commands which are documented from within CMake: $ cmake --help-command-list | wc -l 96 $ cmake --help-command add_executable .

Additionally this sets the variables <projectName>_BINARY_DIR and <projectName>_SOURCE_DIR to the respective values.e. CMake tutorial 18 / 118 . ] ) Sets the name of the project. Optionally you can specify which languages your project supports. E. By default C and CXX are enabled. C.20120121-g751713-dirty project Set a name for the entire project. C. you can disable the check for it by explicitly listing the languages you want to support.g. Fortran..7.g. e. Example languages are CXX (i.Basic CMake usage Builtin documentation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 CMake builtin doc for ’project’ command $ cmake --help-command project cmake version 2. if you do not have a C++ compiler. etc. project(<projectname> [languageName1 languageName2 .8.. By using the special language "NONE" all checks for any language can be disabled. C++).

Check for working C compiler: /usr/bin/gcc -./Acrolibre toulibre Source tree vs Build tree Even the most simple project should never mix-up sources with generated files.2 -.Basic CMake usage Generating & building Building with CMake is easy: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 CMake + Unix Makefile $ ls totally-free acrolibre...Check for working C compiler: /usr/bin/gcc -./totally-free -. CMake supports out-of-source build.6.c CMakeLists.txt $ mkdir build $ cd build $ cmake .c. CMake tutorial 19 / 118 .. $ make Scanning dependencies of target Acrolibre [100%] Building C object CMakeFiles/Acrolibre.dir/acrolibre.o Linking C executable Acrolibre [100%] Built target Acrolibre $ .works .The C compiler identification is GNU 4.

In-source build is a BAD choice.Basic CMake usage Always build out-of-source Out-of-source is better People are lazy (me too) and they think that because building in source is possible and authorizes less typing they can get away with it. Out-of-source build is always better because: CMake tutorial 20 / 118 .

Out-of-source build is always better because: 1 Generated files are separate from manually edited ones (thus you don’t have to clutter you favorite VCS ignore files). CMake tutorial 20 / 118 .Basic CMake usage Always build out-of-source Out-of-source is better People are lazy (me too) and they think that because building in source is possible and authorizes less typing they can get away with it. In-source build is a BAD choice.

Out-of-source build is always better because: 1 Generated files are separate from manually edited ones (thus you don’t have to clutter you favorite VCS ignore files).Basic CMake usage Always build out-of-source Out-of-source is better People are lazy (me too) and they think that because building in source is possible and authorizes less typing they can get away with it. In-source build is a BAD choice. You can have several build trees for the same source tree 2 CMake tutorial 20 / 118 .

In-source build is a BAD choice.Basic CMake usage Always build out-of-source Out-of-source is better People are lazy (me too) and they think that because building in source is possible and authorizes less typing they can get away with it. Out-of-source build is always better because: 1 Generated files are separate from manually edited ones (thus you don’t have to clutter you favorite VCS ignore files). You can have several build trees for the same source tree This way it’s always safe to completely delete the build tree in order to do a clean build CMake tutorial 20 / 118 2 3 .

the main C program acrodict. the Acrodict library header acrodict.txt.h. the Acrodict library source CMakeLists. the soon to be updated CMake entry file CMake tutorial 21 / 118 .c.c.Basic CMake usage Building program + autonomous library We now have the following set of files in our source tree: acrolibre.

Basic CMake usage Building program + autonomous library Conditional build We want to keep a version of our program that can be compiled and run without the new Acrodict library and the new version which uses the library.c.c.txt. We now have the following set of files in our source tree: acrolibre. the soon to be updated CMake entry file CMake tutorial 21 / 118 . the Acrodict library source CMakeLists. the main C program acrodict.h. the Acrodict library header acrodict.

item− > description ) . #i f d e f USE ACRODICT c o n s t a c r o I t e m t ∗ item .h" #e n d i f i n t main ( i n t argc . } #e n d i f else { p r i n t f ( "Sorry . r e t u r n EXIT FAILURE . e x i t ( EXIT FAILURE ) . a r g v [ 0 ] ) . } 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 CMake tutorial 22 / 118 . item− > description ) . #i f n d e f USE ACRODICT i f ( s t r c a s e c m p ( name . i f (NULL!= item ) { p r i n t f ( "%s: %s\n" . c h a r ∗ a r g v [ ] ) { c o n s t c h a r ∗ name . name ) . h> #i n c l u d e <s t d i o . item− >name . #e n d i f i f ( argc < 2) { f p r i n t f ( s t d e r r . a r g v [0]) . } e l s e i f ( item=a c r o d i c t g e t a p p r o x ( name ) ) { p r i n t f ( " <%s> is unknown may be you mean :\n" . } r e t u r n EXIT SUCCESS . p r i n t f ( "%s: %s\n" . } #e l s e item = a c r o d i c t g e t ( name ) . f p r i n t f ( s t d e r r . "%s <name >\n" . } name = a r g v [ 1 ] . "%s: you need one argument\n" .The main program source 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #i n c l u d e < s t d l i b . I don’t know: <%s>\n " . name ) . h> #i f d e f USE ACRODICT #i n c l u d e "acrodict. h> #i n c l u d e < s t r i n g s .\n" ) . "toulibre" ) ==0) { p r i n t f ( "Toulibre is a french organization promoting FLOSS . item− >name .

a c r o d i c t [ c u r r e n t ] . } acroItem t . w h i l e ( ( s t r l e n ( a c r o d i c t [ c u r r e n t ] . } } 1 2 3 4 5 6 7 8 9 #i n c l u d e < s t d l i b . {"RMLL" . "Club des Utilisateurs de Logiciels libres et de gnu/ linux de Toulouse et des environs" } . "Lea -Linux: Linux entre ami(e )s" } . "Berkeley Software Distribution" } . name ) >0) && ! found ) { i f ( s t r c a s e c m p ( name . {"GNU" . const acroItem t ∗ a c r o d i c t g e t ( c o n s t c h a r ∗ name ) { i n t c u r r e n t =0. i n t found =0. "GNU general Public License" }. "GNU is Not Unix" } . #e n d i f 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 {"Lea" . } else { r e t u r n NULL . char ∗ d e s c r i p t i o n .The library source 1 2 3 4 5 6 7 8 9 10 #i f n d e f ACRODICT H #d e f i n e ACRODICT H typedef s t r u c t acroItem { c h a r ∗ name .h" s t a t i c const acroItem t acrodict [ ] = { {"Toulibre" . "Free Libre Open Source Software" } . h> #i n c l u d e < s t r i n g . CMake tutorial 23 / 118 . const acroItem t ∗ a c r o d i c t g e t ( c o n s t c h a r ∗ name ) . {"CULTe" . "" } } . "Rencontres Mondiales du Logiciel Libre" } . "Toulibre is a french organization promoting FLOSS" } . name ) ==0) { found =1. {"" . } else { c u r r e n t ++. h> #i n c l u d e "acrodict. {"BSD" . } } i f ( found ) { r e t u r n &( a c r o d i c t [ c u r r e n t ] ) . {"FLOSS" . {"GPL" .

Basic CMake usage Building a library I Listing 2: Building a simple program + shared library 1 2 3 4 5 6 7 8 9 cmake minimum required (VERSION 2 . c ) s e t ( LIBSRC a c r o d i c t . 8 ) p r o j e c t ( TotallyFree C) add executable ( Acrolibre a c r o l i b r e . h ) a d d l i b r a r y ( a c r o d i c t ${LIBSRC } ) add executable ( A c r o d i c t l i b r e a c r o l i b r e . c a c r o d i c t . c ) t a r g e t l i n k l i b r a r i e s ( Acrodictlibre acrodict ) set target properties ( Acrodictlibre PROPERTIES COMPILE FLAGS "-DUSE_ACRODICT" ) .

c.cmake Makefile Acrolibre* CMakeFiles/ libacrodict.o Linking C shared library libacrodict.dir/acrolibre.c.Basic CMake usage Building a library II And it builds.dir/acrolibre.so [ 33%] Built target acrodict [ 66%] Building C object CMakeFiles/Acrodictlibre..dir/acrodict.o Linking C executable Acrolibre [100%] Built target Acrolibre $ ls -F Acrodictlibre* CMakeCache. 1 2 3 4 5 6 7 8 9 10 11 12 13 CMake + Unix Makefile $ make [ 33%] Building C object CMakeFiles/acrodict. All in all CMake generates appropriate Unix makefiles which build all this smoothly..so* CMake tutorial 25 / 118 .o Linking C executable Acrodictlibre [ 66%] Built target Acrodictlibre [100%] Building C object CMakeFiles/Acrolibre.c.txt cmake_install.

.. Acrodictlibre . . $ . depend . acrodict ...Basic CMake usage Building a library III And it works...../Acrodictlibre FLOSS FLOSS: Free Libre Open Source Software $ make help The following are some of the valid targets for this Makefile: .. clean . .. Generated Makefiles has several builtin targets besides the expected ones: one per target (library or executable) clean../Acrolibre FLOSS Sorry. all more to come . We get the two different variants of our program. with varying capabilities. 1 2 3 4 5 6 $ .. all (the default if no target is provided) .. ... I don’t know: <FLOSS> $ . Acrolibre ../Acrolibre toulibre Toulibre is a french organization promoting FLOSS.

Listing 3: User controlled build option 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 cmake minimum required (VERSION 2 . c ) t a r g e t l i n k l i b r a r i e s ( Acrodictlibre acrodict ) s e t t a r g e t p r o p e r t i e s ( A c r o d i c t l i b r e PROPERTIES COMPILE FLAGS "-DUSE_ACRODICT" ) e n d i f (WITH ACRODICT) CMake tutorial 27 / 118 . We can use CMake OPTION command. h a c r o d i c t . c ) i f (WITH ACRODICT) s e t ( LIBSRC a c r o d i c t .Basic CMake usage User controlled build option User controlled option May be our users don’t want the acronym dictionnary support. c ) a d d l i b r a r y ( a c r o d i c t ${LIBSRC } ) add executable ( A c r o d i c t l i b r e a c r o l i b r e . 8 ) # T h i s p r o j e c t use C s o u r c e code p r o j e c t ( TotallyFree C) # B u i l d o p t i o n w i t h d e f a u l t v a l u e t o ON o p t i o n (WITH ACRODICT "Include acronym dictionary support" ON) s e t ( BUILD SHARED LIBS t r u e ) # build executable using s p e c i f i e d l i s t of source f i l e s add executable ( Acrolibre a c r o l i b r e .

Basic CMake usage Too much keyboard. e. path to the source tree.g. e.: cmake /path/to/source path to an existing build tree. time to click? I CMake comes with severals tools A matter of choice / taste: a command line: cmake a curse-based TUI: ccmake a QT-based GUI: cmake-gui Call convention All tools expect to be called with a single argument which may be interpreted in 2 different ways.g.: cmake-gui . CMake tutorial 28 / 118 .

CMake tutorial 29 / 118 . time to click? II ccmake : the curse-based TUI (demo) Here we can choose to toggle the WITH ACRONYM OPTION.Basic CMake usage Too much keyboard.

time to click? III cmake-gui : the QT-based GUI (demo) Again. we can choose to toggle the WITH ACRONYM OPTION. CMake tutorial 30 / 118 .Basic CMake usage Too much keyboard.

com/ninja/ CMake tutorial 31 / 118 . Apple.NET 2003 Visual Studio 8 2005 Visual Studio 8 2005 Win64 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Visual Studio 9 2008 Visual Studio 9 2008 IA64 Visual Studio 9 2008 Win64 Watcom WMake CodeBlocks .MinGW Makefiles Eclipse CDT4 .MinGW Makefiles CodeBlocks . Windows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Borland Makefiles MSYS Makefiles MinGW Makefiles NMake Makefiles NMake Makefiles JOM Unix Makefiles Visual Studio 10 Visual Studio 10 IA64 Visual Studio 10 Win64 Visual Studio 11 Visual Studio 11 Win64 Visual Studio 6 Visual Studio 7 Visual Studio 7 .Basic CMake usage Remember CMake is a build generator? The number of active generators depends on the platform we are running on Unix.Unix Makefiles Eclipse CDT4 .Unix Makefiles XCode Ninja (in development) http://martine.Unix Makefiles KDevelop3 KDevelop3 .NMake Makefiles Eclipse CDT4 .NMake Makefiles CodeBlocks .github.

dir/acrolibre.exe [100%] Built target Acrolibre CMake tutorial 32 / 118 .txt $ mkdir build-win32 $ cd build-win32 .dir/acrolibre. $ make Scanning dependencies of target acrodict [ 33%] Building C object CMakeFiles/acrodict.h acrodict.exe [ 66%] Built target Acrodictlibre Scanning dependencies of target Acrolibre [100%] Building C object CMakeFiles/Acrolibre.c acrolibre.dir/acrodict.obj Linking C executable Acrolibre. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 CMake + MinGW Makefile $ ls totally-free acrodict.c.c.c.obj Linking C shared library libacrodict.obj Linking C executable Acrodictlibre.c CMakeLists.Basic CMake usage Equally simple on other platforms It is as easy for a windows build.a [ 33%] Built target acrodict Scanning dependencies of target Acrodictlibre [ 66%] Building C object CMakeFiles/Acrodictlibre.dll Creating library file: libacrodict.dll. however names for executables and libraries are computed in a platform specific way...

this is controlled by the CMake install command.. h DESTINATION i n c l u d e ) e n d i f (WITH ACRODICT) CMake tutorial 33 / 118 ...Basic CMake usage Installing things Install Several parts or the software may need to be installed. i n s t a l l (TARGETS A c r o d i c t l i b r e a c r o d i c t RUNTIME DESTINATION b i n LIBRARY DESTINATION l i b ARCHIVE DESTINATION l i b / s t a t i c ) i n s t a l l ( FILES a c r o d i c t . Remember cmake --help-command install!! Listing 4: install command examples 1 2 3 4 5 6 7 8 9 10 11 .. c ) i n s t a l l (TARGETS A c r o l i b r e DESTINATION b i n ) i f (WITH ACRODICT) . add executable ( Acrolibre a c r o l i b r e .

Basic CMake usage Controlling installation destination Use relative DESTINATION One should always use relative installation DESTINATION unless you really want to use absolute path like /etc. Then depending when you install: CMake tutorial 34 / 118 .

Then depending when you install: At CMake-time set CMAKE INSTALL PREFIX value $ cmake --help-variable CMAKE_INSTALL_PREFIX CMake tutorial 34 / 118 .Basic CMake usage Controlling installation destination Use relative DESTINATION One should always use relative installation DESTINATION unless you really want to use absolute path like /etc.

Then depending when you install: At CMake-time set CMAKE INSTALL PREFIX value $ cmake --help-variable CMAKE_INSTALL_PREFIX At Install-time use DESTDIR mechanism (Unix Makefiles) $ make DESTDIR=/tmp/testinstall install CMake tutorial 34 / 118 .Basic CMake usage Controlling installation destination Use relative DESTINATION One should always use relative installation DESTINATION unless you really want to use absolute path like /etc.

we will see that later CMake tutorial 34 / 118 . At Package-install-time. . . be patient.Basic CMake usage Controlling installation destination Use relative DESTINATION One should always use relative installation DESTINATION unless you really want to use absolute path like /etc. CPack what? . Then depending when you install: At CMake-time set CMAKE INSTALL PREFIX value $ cmake --help-variable CMAKE_INSTALL_PREFIX At Install-time use DESTDIR mechanism (Unix Makefiles) $ make DESTDIR=/tmp/testinstall install At CPack-time.

txt Project file(s). . Makefiles. . . Object files Source files Generated Sources files CMake time Installed files Binary package Source package Build time Install time CPack time Package Install time Installed package CMake tutorial 35 / 118 .Basic CMake usage The CMake workflow (pictured) CMakeLists.

but CMake uses many (˜170+) of them to control/change its [default] behavior. Inside a CMake script set (CMAKE INSTALL PREFIX /home/eric/testinstall) $ cmake --help-command set On the command line/TUI/GUI Remember that (beside options) each CMake tool takes a single argument (source tree or existing build tree) $ cmake -DCMAKE_INSTALL_PREFIX=/home/eric/testinstall .Basic CMake usage Using CMake variables CMake variables They are used by the user to simplify its CMakeLists.txt. Try: cmake --help-variables-list. CMake tutorial 36 / 118 .

Install configuration: "" -..Installing: /tmp/testinstall/bin/Acrodictlibre -. 1 2 3 4 5 6 7 8 9 10 11 12 $ make DESTDIR=/tmp/testinstall install [ 33%] Built target acrodict [ 66%] Built target Acrodictlibre [100%] Built target Acrolibre Install the project..txt.Removed runtime path from "/tmp/testinstall/bin/Acrodictlibre" -.Installing: /tmp/testinstall/include/acrodict. -.Installing: /tmp/testinstall/bin/Acrolibre -.so -.Installing: /tmp/testinstall/lib/libacrodict.Basic CMake usage The install target Install target The install target of the underlying build tool (in our case make) appears in the generated build system as soon as some install command are used in the CMakeLists.h $ CMake tutorial 37 / 118 .

1..0-Linux. e n d i f (WITH ACRODICT) ..0-Linux/bin/Acrodictlibre .1. # Near t h e end o f t h e CMakeLists .Basic CMake usage Package the whole thing CPack CPack is a CMake friend application (detailed later) which may be used to easily package your software.gz generated.tar. CPack: Create package using TGZ CPack: Install projects CPack: ..1.1.Install project: TotallyFree CPack: Create package CPack: .0-Linux/lib/libacrodict.. TotallyFree-0..gz . TotallyFree-0...0-Linux/include/acrodict. Listing 5: add CPack support 1 2 3 4 5 6 7 8 9 10 11 12 .. TotallyFree-0.0-Linux/bin/Acrolibre . $ tar ztvf TotallyFree-0.0-Linux.... TotallyFree-0..Run preinstall target for: TotallyFree CPack: .package: <build-tree>/. t x t # Chose y o u r CPack g e n e r a t o r s e t (CPACK GENERATOR "TGZ" ) # Setup package v e r s i o n s e t (CPACK PACKAGE VERSION MAJOR 0 ) s e t ( CPACK PACKAGE VERSION MINOR 1 ) s e t ( CPACK PACKAGE VERSION PATCH 0 ) # ’ c a l l ’ CPack i n c l u d e ( CPack ) $ make package [ 33%] Built target acrodict [ 66%] Built target Acrodictlibre [100%] Built target Acrolibre Run CPack packaging tool..1.tar...h . TotallyFree-0..1.so CMake tutorial 38 / 118 .

1.1.zip Archive: TotallyFree-0.so OK No errors detected in compressed data of TotallyFree-0.1./include/acrodict.Basic CMake usage CPack the packaging friend CPack is a standalone generator As we will see later on..Install project: TotallyFree CPack: Create package CPack: .Install project: TotallyFree CPack: Create package CPackRPM: Will use GENERATED spec file: <build-tree>/..0-Linux. _CPack_Packages/Linux/RPM/SPECS/totallyfree..package: <build-tree>/...zip testing: To.1.package: <build-tree>/..zip./bin/Acrolibre OK testing: To..1. $ unzip -t TotallyFree-0.0-Linux.Run preinstall target for: TotallyFree CPack: .0-Linux.Run preinstall target for: TotallyFree CPack: ..0-Linux. CPack is standalone application.h /usr/lib /usr/lib/libacrodict. TotallyFree-0.rpm /usr /usr/bin /usr/bin/Acrodictlibre /usr/bin/Acrolibre /usr/include /usr/include/acrodict. $ cpack -G RPM CPack: Create package using RPM CPack: Install projects CPack: . $ cpack -G ZIP CPack: Create package using ZIP CPack: Install projects CPack: .0-Linux...spec CPack: . which as CMake is a generator.zip generated. $ rpm -qpl TotallyFree-0.so CMake tutorial 39 / 118 .rpm generated.h OK testing: To.0-Linux.1.... TotallyFree-0../bin/Acrodictlibre OK testing: To./lib/libacrodict.

.......... 1 tests failed out of 4 Total Test time (real) = 0. Test project <buildtree-prefix>/build Start 1: toulibre-builtin 1/4 Test #1: toulibre-builtin ...... Passed Start 3: FLOSS-dict 3/4 Test #3: FLOSS-dict .00 sec 0. enable testing ( ) a d d t e s t ( t o u l i b r e −b u i l t i n A c r o l i b r e "toulibre" ) a d d t e s t ( t o u l i b r e −d i c t A c r o d i c t l i b r e "toulibre" ) a d d t e s t ( FLOSS i c t −d A c r o d i c t l i b r e "FLOSS" ) a d d t e s t ( FLOSS a i l −f A c r o l i b r e "FLOSS" ) $ make test Running tests...00 sec The following tests FAILED: 4 .FLOSS-fail (Failed) CMake tutorial 40 / 118 .***Failed 75% tests passed.00 sec 0...00 sec 0.. Passed Start 4: FLOSS-fail 4/4 Test #4: FLOSS-fail . Listing 6: add CTest support 1 2 3 4 5 6 7 8 9 10 11 12 .... e n d i f (WITH ACRODICT) . Passed Start 2: toulibre-dict 2/4 Test #2: toulibre-dict.01 sec 0..Basic CMake usage Didn’t you mentioned testing? I CTest CTest is a CMake friend application (detailed later) which may be used to easily test your software...........

0 tests failed out of 4 Total Test time (real) = 0. enable testing ( ) a d d t e s t ( t o u l i b r e −b u i l t i n A c r o l i b r e "toulibre" ) a d d t e s t ( t o u l i b r e −d i c t A c r o d i c t l i b r e "toulibre" ) a d d t e s t ( FLOSS i c t −d A c r o d i c t l i b r e "FLOSS" ) a d d t e s t ( FLOSS a i l −f A c r o l i b r e "FLOSS" ) s e t t e s t s p r o p e r t i e s ( FLOSS a i l −f PROPERTIES PASS REGULAR EXPRESSION "Sorry .. Passed Start 3: FLOSS-dict 3/4 Test #3: FLOSS-dict ......01 sec 0.... Passed Start 4: FLOSS-fail 4/4 Test #4: FLOSS-fail .. Passed 100% tests passed...... Listing 7: add CTest support 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 .Basic CMake usage Didn’t you mentioned testing? II Tailor success rule CTest uses the return code in order to get success/failure status. I don ’t know :...... e n d i f (WITH ACRODICT) .. but one can tailor the success/fail rule.* FLOSS" ) $ make test Running tests.... Test project <buildtree-prefix>/build Start 1: toulibre-builtin 1/4 Test #1: toulibre-builtin .00 sec 0.00 sec 0. Passed Start 2: toulibre-dict 2/4 Test #2: toulibre-dict.00 sec CMake tutorial 41 / 118 .00 sec 0...........

00 sec 0% tests passed. 1 tests failed out of 1 Total Test time (real) = 0.99988e+06 4: Sorry.. Passed 0...FLOSS-fail (Failed) Errors while running CTest CMake tutorial 42 / 118 .. 0 tests failed out of 2 Start 4: FLOSS-fail 4: Test command: <build-tree>/Acrolibre "FLOSS" Total Test time (real) = 0.00 sec Checking test dependency graph end test 4 100% tests passed.01 sec 4: Test timeout computed to be: 9.***Failed 0. $ ctest -R toulibre$ ctest -R FLOSS-fail -V Test project <build-tree>/build Test project <build-tree> Start 1: toulibre-builtin Constructing a list of tests 1/2 Test #1: toulibre-builtin . I don’t know: <FLOSS> 1/1 Test #4: FLOSS-fail . which can run a set of test programs.. 2/2 Test #2: toulibre-dict .. Passed 0..00 sec The following tests FAILED: 4 ..00 sec Done constructing a list of tests Start 2: toulibre-dict Checking test dependency graph.... CTest is standalone application....Basic CMake usage CTest the testing friend CTest is a standalone generic test driver As we will see later on....

org/–http://dash. More on this later.org/) for easing collective regression testing.Basic CMake usage CDash the test results publishing Dashboard CTest may help publishing the results of the tests on a CDash dashboard (http://www. . .orfeo-toolbox. http://www.orfeo-toolbox.cdash.org/ CMake tutorial 43 / 118 .

txt Discover compilers (C.Basic CMake usage Summary CMake basics Using CMake basics we can already do a lot a things with minimal writing. Write simple build specification file: CMakeLists. C++. Fortran) Build executable and library (shared or static) in a cross-platform manner Package the resulting binaries with CPack Runs systematic test with CTest and publish them with CDash CMake tutorial 44 / 118 .

cmake.html Browse the built-in help: cmake --help-xxxxx 2 3 4 CMake tutorial 45 / 118 .org/cmake/help/mailing.Basic CMake usage Seeking more information or help There are several places you can go by yourself: 1 Read the FAQ: http://www.org/Wiki/CMake Ask on the Mailing List: http://www.cmake.org/Wiki/CMake_FAQ Read the Wiki: http://www.cmake.

Discovering environment specificities Outline 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 46 / 118 .

Discovering environment specificities Outline 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 47 / 118 .

CYGWIN True for cygwin. MINGW true if the compiler is MinGW.Discovering environment specificities How to discover system System/compiler specific variables Right after the project command CMake has set up a bunch of variables which can be used to tailor the build in a platform specific way. including win64. APPLE True if running on Mac OSX. system specific WIN32 True on windows systems. CMake tutorial 48 / 118 . UNIX True for UNIX and UNIX like operating systems. compiler specific MSVC True when using Microsoft Visual C CMAKE COMPILER IS GNU<LANG> True if the <LANG> compiler is GNU.

name ) ! = 0 ) ) { found =1. #i f d e f GUESS NAME w h i l e ( ( s t r l e n ( a c r o d i c t [ c u r r e n t ] . name .Discovering environment specificities Handle system specific code Some functions like strcasestr (lines 6 and 7) may not be available on all platforms. Listing 8: excerpt from acrodict. } else #e n d i f { r e t u r n NULL . i n t found =0. name ) ! = 0 ) | | ( s t r c a s e s t r ( a c r o d i c t [ c u r r e n t ] . } else { c u r r e n t ++. } } CMake tutorial 49 / 118 . a c r o d i c t [ c u r r e n t ] . name) >0) && ! found ) { i f ( ( s t r c a s e s t r ( name .c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 c o n s t a c r o I t e m t ∗ a c r o d i c t g e t a p p r o x ( c o n s t c h a r ∗ name ) { i n t c u r r e n t =0. } } i f ( found ) { r e t u r n &( a c r o d i c t [ c u r r e n t ] ) .

but not on WIN32 system. if the option is set then we pass a source specific compile flags.. Then on line 11.. c ) i f (WITH GUESS NAME) s e t s o u r c e f i l e s p r o p e r t i e s ( a c r o d i c t . i f (WITH ACRODICT) # l i s t o f sources in our l i b r a r y s e t ( LIBSRC a c r o d i c t . h a c r o d i c t .Discovering environment specificities Use system specific 1 2 3 4 5 6 7 8 9 10 11 12 13 14 option # B u i l d o p t i o n w i t h d e f a u l t v a l u e t o ON o p t i o n (WITH ACRODICT "Include acronym dictionary support" ON) i f (NOT WIN32 ) o p t i o n (WITH GUESS NAME "Guess acronym name" ON) e n d i f (NOT WIN32 ) . cmake --help-command set source files properties CMake tutorial 50 / 118 . c PROPERTIES COMPILE FLAGS "-DGUESS_NAME" ) e n d i f (WITH GUESS NAME) a d d l i b r a r y ( a c r o d i c t ${LIBSRC } ) . Line 4 defines a CMake option...

MuseScore : http://musescore.nongnu.svn. .org/projects/certi/ http://cvs.org LLVM: http://llvm.org/docs/CMake.Discovering environment specificities System specific in real life Real [numeric] life project Real projects (i.sourceforge. .cmake.org/viewvc/certi/?root=certi CMake (of course): http://www. CMake tutorial 51 / 118 . not the toy of this tutorial) have many parts of their CMakeLists.txt from MuseScore CERTI : https://savannah.org http://mscore.html a lot more .net/viewvc/mscore/trunk/mscore/mscore/ Display CMakeLists.txt which deals with system/compiler specific option/feature.gnu.e.savannah.

use the defined variable in order to write a template configuration header file then use configure file in order to produce the actual config file from the template. project config.Discovering environment specificities What about projectConfig. functions. 2 3 CMake tutorial 52 / 118 . ). macros (built-in or imported) then set various variables.h.h file? I Project config files Sometimes it’s easier to test for feature and then write a configuration file (config. The CMake way to do that is to: 1 lookup system informations using CMake variable. . . .h.

. h ) CMake tutorial 53 / 118 . h .txt 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # Load C h ec ke r macros INCLUDE ( C h e c k F u n c t i o n E x i s t s ) FIND FILE ( HAVE STDINT H NAMES s t d i n t ..h file? II Listing 9: Excerpt from CERTI project main CMakeLists. h PATH SUFFIXES s y s ) INCLUDE ( C h e c k I n c l u d e F i l e ) CHECK INCLUDE FILE ( time . cmake ${CMAKE CURRENT BINARY DIR} / c o n f i g . h ) FIND FILE ( HAVE SYS SELECT H NAMES s e l e c t . h HAVE TIME H ) FIND LIBRARY ( RT LIBRARY r t ) i f ( RT LIBRARY ) SET( CMAKE REQUIRED LIBRARIES ${CMAKE REQUIRED LIBRARIES} ${RT LIBRARY } ) e n d i f ( RT LIBRARY ) CHECK FUNCTION EXISTS ( c l o c k g e t t i m e HAVE CLOCK GETTIME) CHECK FUNCTION EXISTS ( c l o c k s e t t i m e HAVE CLOCK SETTIME ) CHECK FUNCTION EXISTS ( c l o c k g e t r e s HAVE CLOCK GETRES ) CHECK FUNCTION EXISTS ( c l o c k n a n o s l e e p HAVE CLOCK NANOSLEEP) IF (HAVE CLOCK GETTIME AND HAVE CLOCK SETTIME AND HAVE CLOCK GETRES ) SET( HAVE POSIX CLOCK 1 ) ENDIF (HAVE CLOCK GETTIME AND HAVE CLOCK SETTIME AND HAVE CLOCK GETRES ) . CONFIGURE FILE ( ${CMAKE CURRENT SOURCE DIR} / c o n f i g .Discovering environment specificities What about projectConfig.

h> header file. */ #cmakedefine PACKAGE_BUGREPORT "@PACKAGE_BUGREPORT@" /* Define to the full name of this package. /* Name of package */ #cmakedefine PACKAGE "@PACKAGE_NAME@" /* Define to the address where bug reports for this package should be sent.. */ #cmakedefine HAVE_STDLIB_H 1 /* Define to 1 if you have the <strings.cmake /* define if the compiler has numeric_limits<T> */ #cmakedefine HAVE_NUMERIC_LIMITS /* Define to 1 if you have the <stdint. */ #cmakedefine PACKAGE_NAME "@PACKAGE_NAME@" /* Define to the full name and version of this package.h file? III 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Excerpt from CERTI config.Discovering environment specificities What about projectConfig. */ #cmakedefine PACKAGE_STRING "@PACKAGE_NAME@-@PACKAGE_VERSION@" CMake tutorial 54 / 118 ..h> header file. */ #cmakedefine HAVE_STRINGS_H 1 .h> header file. */ #cmakedefine HAVE_STDINT_H 1 /* Define to 1 if you have the <stdlib.h.

h file? IV And you get something like: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Excerpt from generated CERTI config.h> header file. */ #define PACKAGE_BUGREPORT "certi-devel@nongnu. */ #define HAVE_STDLIB_H 1 /* Define to 1 if you have the <strings.. /* Name of package */ /* #undef PACKAGE */ /* Define to the address where bug reports for this package should be sent.h /* define if the compiler has numeric_limits<T> */ #define HAVE_NUMERIC_LIMITS /* Define to 1 if you have the <stdint.. */ #define PACKAGE_NAME "CERTI" /* Define to the full name and version of this package.h> header file.h> header file. */ #define HAVE_STRINGS_H 1 .Discovering environment specificities What about projectConfig. */ #define HAVE_STDINT_H 1 /* Define to 1 if you have the <stdlib.org" /* Define to the full name of this package. */ /* #undef PACKAGE_STRING */ CMake tutorial 55 / 118 .

Discovering environment specificities Outline 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 56 / 118 .

.. files etc.Discovering environment specificities The find package command I Finding external package Project may be using external libraries. . Those can be found using the find package command.. program. Listing 10: using libxml2 1 2 3 4 5 6 7 8 9 f i n d p a c k a g e ( LibXml2 ) i f ( LIBXML2 FOUND ) a d d d e f i n i t i o n s (−DHAVE XML ${LIBXML2 DEFINITIONS } ) i n c l u d e d i r e c t o r i e s ( ${LIBXML2 INCLUDE DIR } ) e l s e ( LIBXML2 FOUND ) s e t ( LIBXML2 LIBRARIES "" ) e n d i f ( LIBXML2 FOUND ) . t a r g e t l i n k l i b r a r i e s ( MyTarget ${LIBXML2 LIBRARIES } ) CMake tutorial 57 / 118 .

the final set of library directories listed in one variable for use by client code. XXX LIBRARIES: The libraries to link against to use XXX. 2 3 4 5 6 See doc cmake --help-module FindLibXml2 Many modules are provided by CMake (130 as of CMake 2. XXX EXECUTABLE: Where to find the XXX tool. XXX INCLUDE DIRS: The final set of include directories listed in one variable for use by client code. XXX DEFINITIONS: Definitions to use when compiling code that uses XXX. or undefined.7) CMake tutorial 58 / 118 . or don’t want to use XXX. if we haven’t found. These should include full paths.Discovering environment specificities The 1 find package command II Find modules usually defines standard variables (for module XXX) XXX FOUND: Set to false.8. XXX LIBRARY DIRS: Optionally.

Discovering environment specificities The find package command III You may write your own: http://www. . A module may provide not only CMake variables but new CMake macros (we will see that later with the MACRO.kde.cmake. .org/trunk/KDE/kdelibs/cmake/modules/ PlPlot: http://plplot.sourceforge.com/svn/trunk/Modules/ probably many more. FUNCTION CMake language commands) CMake tutorial 59 / 118 .org/Wiki/CMake:Module_Maintainers You may find/borrow modules from other projects which use CMake KDE4: http://websvn.svn.net/viewvc/plplot/ trunk/cmake/modules/ http://cmake-modules.googlecode.

Discovering environment specificities The other find xxxx commands I The find xxx command family find package is a high level module finding mechanism but there are lower-level CMake commands which may be used to write find modules or anything else inside CMakeLists.txt to find an executable program: find program to find a library: find library to find any kind of file: find file to find a path where a file reside: find path CMake tutorial 60 / 118 .

f r / ˜ f o r g e t / p r e l u d e . t h e v e r s i o n o f t h e p r e l u d e c o m p i l e r # PRELUDE INCLUDE DIR .. l i f l . .. # PRELUDE FOUND. where t o f i n d dword . P r e l u d e was n o t f o u n d . I f f a l s e . # On can s e t PRELUDE PATH HINT b e f o r e u s i n g f i n d p a c k a g e ( P r e l u d e ) and t h e # module w i t h use t h e PATH as a h i n t t o f i n d p r e l u d e c . t h e p r e l u d e c o m p i l e r # PRELUDE COMPILER VERSION .Discovering environment specificities The other 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 find xxxx commands II # − Find P r e l u d e c o m p i l e r # Find t h e P r e l u d e s y n c h r o n o u s language c o m p i l e r w i t h a s s o c i a t e d i n c l u d e s p a t h . # See h t t p : / / www . h . h t m l # T h i s module d e f i n e s # PRELUDE COMPILER . e t c . i f ( PRELUDE PATH HINT ) message (STATUS "FindPrelude: using PATH HINT: ${PRELUDE_PATH_HINT}" ) else ( ) s e t ( PRELUDE PATH HINT ) endif ( ) # FIND PROGRAM t w i c e u s i n g NO DEFAULT PATH on f i r s t s h o t f i n d p r o g r a m ( PRELUDE COMPILER NAMES p r e l u d e c PATHS ${PRELUDE PATH HINT} PATH SUFFIXES b i n NO DEFAULT PATH DOC "Path to the Prelude compiler command ’preludec ’" ) f i n d p r o g r a m ( PRELUDE COMPILER NAMES p r e l u d e c PATHS ${PRELUDE PATH HINT} PATH SUFFIXES b i n DOC "Path to the Prelude compiler command ’preludec ’" ) CMake tutorial 61 / 118 .

. h PATHS ${PRELUDE PATH HINT} PATH SUFFIXES l i b / p r e l u d e DOC "The Prelude include headers" ) . # h a n d l e t h e QUIETLY and REQUIRED a r g u m e n t s and s e t PRELUDE FOUND t o TRUE i f # a l l l i s t e d v a r i a b l e s a r e TRUE i n c l u d e ( FindPackageHandleStandardArgs ) FIND PACKAGE HANDLE STANDARD ARGS(PRELUDE REQUIRED VARS PRELUDE COMPILER PRELUDE INCLUDE DIR ) CMake tutorial 62 / 118 ..Discovering environment specificities The other 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 find xxxx commands III i f ( PRELUDE COMPILER ) # g e t t h e p a t h where t h e p r e l u d e c o m p i l e r was f o u n d g e t f i l e n a m e c o m p o n e n t (PRELUDE PATH ${PRELUDE COMPILER} PATH) # remove b i n g e t f i l e n a m e c o m p o n e n t (PRELUDE PATH ${PRELUDE PATH} PATH) # add p a t h t o PRELUDE PATH HINT l i s t (APPEND PRELUDE PATH HINT ${PRELUDE PATH} ) e x e c u t e p r o c e s s (COMMAND ${PRELUDE COMPILER} −v e r s i o n OUTPUT VARIABLE PRELUDE COMPILER VERSION OUTPUT STRIP TRAILING WHITESPACE ) e n d i f ( PRELUDE COMPILER ) f i n d p a t h ( PRELUDE INCLUDE DIR NAMES dword .

you did install the runtime libraries you did install eventual developer libraries. . ) use a build system (autotools or CMake or . VCS repositories. . . .Discovering environment specificities Advanced use of external package I Installed External package The previous examples suppose that you have the package you are looking for on your host. . ) CMake tutorial 63 / 118 . headers and tools What if the external packages: are only available as source (tarball.

an external project $ cmake --help-module ExternalProject CMake tutorial 64 / 118 . .Discovering environment specificities Advanced use of external package II ExternalProject Add The ExternalProject. .cmake CMake module defines a highlevel macro which does just that: 1 2 3 4 5 download/checkout source update/patch configure build install (and test) .

More CMake scripting Outline 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 65 / 118 .

More CMake scripting

The different CMake “modes”
Normal mode: the mode used when processing CMakeLists.txt Command mode: cmake -E <command>, command line mode which offers basic command in a portable way:

Process scripting mode: cmake -P <script>, used to execute a CMake script which is not a CMakeLists.txt. Wizard mode: cmake -i, interactive equivalent of the Normal mode.

CMake tutorial

66 / 118

More CMake scripting

The different CMake “modes”
Normal mode: the mode used when processing CMakeLists.txt Command mode: cmake -E <command>, command line mode which offers basic command in a portable way: works on all supported CMake platforms. I.e. you don’t want to rely on shell or native command interpreter capabilities. Process scripting mode: cmake -P <script>, used to execute a CMake script which is not a CMakeLists.txt. Wizard mode: cmake -i, interactive equivalent of the Normal mode.

CMake tutorial

66 / 118

More CMake scripting

The different CMake “modes”
Normal mode: the mode used when processing CMakeLists.txt Command mode: cmake -E <command>, command line mode which offers basic command in a portable way: works on all supported CMake platforms. I.e. you don’t want to rely on shell or native command interpreter capabilities. Process scripting mode: cmake -P <script>, used to execute a CMake script which is not a CMakeLists.txt. Not all CMake commands are scriptable!! Wizard mode: cmake -i, interactive equivalent of the Normal mode.

CMake tutorial

66 / 118

create a tar archive time command [args] ..check if file1 is same as file2 copy file destination . .displays arguments as text echo_append [string].More CMake scripting Command mode Just try: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 list of command mode commands $ cmake -E CMake Error: cmake version 2.touch a file..copy file if input has changed echo [string].run command and return elapsed time touch file .copy directory ’source’ content to directory ’destination’ copy_if_different in-file out-file .create a directory md5sum file1 [.. use -f to force it remove_directory dir .remove a directory and its contents rename oldname newname .remove the file(s).] Available commands: chdir dir cmd [args]..run command in a given directory compare_files file1 file2 .. Available on UNIX only: create_symlink old new ..compute md5sum of files remove [-f] file1 file2 . ..create a symbolic link new -> old CMake tutorial 67 / 118 .touch a file but do not create it..7 Usage: cmake -E [command] [arguments . touch_nocreate file . .] .tar file/dir1 file/dir2 .displays arguments as text but no new line environment .copy file to destination (either file or directory) copy_directory source destination ....8...display the current environment make_directory dir .. . . .rename a file or directory (on one volume) tar [cxt][vfz][cvfj] file...

endif. if . foreach. elseif . continue but skip generation FATAL_ERROR = CMake Error. unset. continue processing SEND_ERROR = CMake Error. message([STATUS|WARNING|AUTHOR_WARNING|FATAL_ERROR|SEND_ERROR] "message to display" . else . break Remember: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $ cmake --help-command-list $ cmake --help-command <command-name> $ cmake --help-command message cmake version 2.8.7 message Display a message to the user. continue processing AUTHOR_WARNING = CMake Warning (dev).. It contains general purpose constructs: set .) The optional keyword determines the type of message: (none) = Important information STATUS = Incidental information WARNING = CMake Warning.More CMake scripting CMake scripting Overview of CMake language CMake is a declarative language which contains 90+ commands. while.. stop all processing CMake tutorial 68 / 118 .

upper/lower case conversion. FIND. GET. APPEND...). DOWNLOAD. comparison. INSERT. MAKE DIRECTORY advanced files operations: GLOB. REMOVE AT. . REMOVE DUPLICATES REVERSE. WRITE.More CMake scripting Higher level commands as well file manipulation with file : READ. SORT string manipulation: string . REMOVE ITEM. CMake tutorial 69 / 118 . RENAME. APPEND. UPLOAD working with path: file (TO CMAKE PATH /TO NATIVE PATH . APPEND. . regular expression match. REMOVE. stderr and return code retrieval): execute process builtin list manipulation command: list with sub-commands LENGTH. get filename component execute an external process (with stdout. length. substring. . GLOB RECURSE file name in a path.

org/files/v2. z s o u r c e t r e e tmp / cmake−x . y . cmake you s h o u l d end up w i t h a tmp / cmake−x . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 # # # # # # # # # Simple cmake s c r i p t which may be used t o b u i l d cmake f r o m a u t o m a t i c a l l y downloaded s o u r c e cd tmp / cmake −P CMake−a u t o b u i l d−v2 .tar. cmake minimum required (VERSION 2 .7" ) s e t ( CMAKE FILE PREFIX "cmake -${CMAKE_VERSION}" ) s e t (CMAKE REMOTE PREFIX "http :// www.cmake.8/" ) s e t ( CMAKE FILE SUFFIX ".gz" ) s e t ( CMAKE BUILD TYPE "Debug" ) s e t (CPACK GEN "TGZ" ) CMake tutorial 70 / 118 .8. z−b u i l d b u i l d t r e e c o n f i g u r e and c o m p i l e d t r e e . 8 ) s e t (CMAKE VERSION "2.More CMake scripting Portable script for building CMake I As an example of what can be done with pure CMake script (script mode) here is a script for building CMake package using a previously installed CMake. u s i n g t h e t a r b a l l f o u n d on K i t w a r e . y .

./${CMAKE_FILE_PREFIX}${CMAKE_FILE_SUFFIX}" ) s e t ( REMOTE FILE "${CMAKE_REMOTE_PREFIX}${CMAKE_FILE_PREFIX}${CMAKE_FILE_SUFFIX}" ) message (STATUS "Trying to autoinstall CMake version ${CMAKE_VERSION} using ${ REMOTE_FILE} file ...." ) f i l e (DOWNLOAD ${REMOTE FILE} ${LOCAL FILE} TIMEOUT 600 STATUS DL STATUS LOG DL LOG SHOW PROGRESS) l i s t (GET DL STATUS 0 DL NOK) i f ( "${DL_LOG}" MATCHES "404 Not Found" ) s e t (DL NOK 1 ) e n d i f ( "${DL_LOG}" MATCHES "404 Not Found" ) i f (DL NOK) # we s h a l l remove t h e f i l e because i t i s c r e a t e d # w i t h an i n a p p r o p r i a t e c o n t e n t f i l e (REMOVE ${LOCAL FILE } ) message (SEND ERROR "Download failed: ${DL_LOG}" ) CMake tutorial 71 / 118 ." ) i f ( EXISTS ${LOCAL FILE } ) message (STATUS "Already there: nothing to do" ) e l s e ( EXISTS ${LOCAL FILE } ) message (STATUS "Not there .. trying to download .More CMake scripting Portable script for building CMake II 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 s e t ( LOCAL FILE "." ) message (STATUS "Downloading ..

" ) CMake tutorial 72 / 118 ." ) message (STATUS "Configuring with CMake (build type=${CMAKE_BUILD_TYPE }).More CMake scripting Portable script for building CMake III 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 e l s e (DL NOK) message (STATUS "Download successful.." ) f i l e (MAKE DIRECTORY ${CMAKE FILE PREFIX}−b u i l d ) e x e c u t e p r o c e s s (COMMAND ${CMAKECOMMAND} −DCMAKE BUILD TYPE=${CMAKE BUILD TYPE} − DBUILD QtDialog :BOOL= ON . / ${CMAKE FILE PREFIX} WORKING DIRECTORY ${CMAKE FILE PREFIX}−b u i l d RESULT VARIABLE CONFIG RES OUTPUT VARIABLE CONFIG OUT ERROR VARIABLE CONFIG ERR TIMEOUT 2 0 0 ) message (STATUS "Building with cmake --build .. .." ) e n d i f (DL NOK) e n d i f ( EXISTS ${LOCAL FILE } ) message (STATUS "Unarchiving the file" ) e x e c u t e p r o c e s s (COMMAND ${CMAKECOMMAND} −E t a r z x v f ${LOCAL FILE} RESULT VARIABLE UNTAR RES OUTPUT VARIABLE UNTAR OUT ERROR VARIABLE UNTAR ERR ) message (STATUS "CMake version ${CMAKE_VERSION} has been unarchived in ${ CMAKE_CURRENT_SOURCE_DIR }/${CMAKE_FILE_PREFIX }..

*) generated" PACKAGES "${CONFIG_OUT}" ) message (STATUS "CMake package(s) are: ${CMAKE_MATCH_1}" ) CMake tutorial 73 / 118 .More CMake scripting Portable script for building CMake IV 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 e x e c u t e p r o c e s s (COMMAND ${CMAKECOMMAND} − u i l d ..." ) s t r i n g (REGEX MATCH "CPack: ." ) e x e c u t e p r o c e s s (COMMAND ${CMAKE CPACK COMMAND} −G ${CPACK GEN} WORKING DIRECTORY ${CMAKE FILE PREFIX}−b u i l d RESULT VARIABLE CONFIG RES OUTPUT VARIABLE CONFIG OUT ERROR VARIABLE CONFIG ERR ) message (STATUS "CMake version ${CMAKE_VERSION} has been built in ${ CMAKE_CURRENT_SOURCE_DIR }/${CMAKE_FILE_PREFIX }. −b WORKING DIRECTORY ${CMAKE FILE PREFIX}−b u i l d RESULT VARIABLE CONFIG RES OUTPUT VARIABLE CONFIG OUT ERROR VARIABLE CONFIG ERR ) message (STATUS "Create package ${CPACK_GEN} with CPack .package :(.

$ cmake --help-property-list $ cmake --help-property COMPILE_FLAGS CMake tutorial 74 / 118 . set tests properties : 190+ different properties may be used.More CMake scripting Build specific commands create executable or library: add executable. set source files properties . include directories . add library add compiler/linker definitions/options: add definition . try run fine control of various properties: set target properties . target link libraries powerful installation specification: install probing command: try compile . set directory properties .

More CMake scripting Outline 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 75 / 118 .

1 2 3 4 Executable are targets: add executable Libraries are targets: add library There exist some builtin targets: install. package. A target is something that CMake should build (i. .e. .More CMake scripting What are CMake targets? CMake target Many times in the documentation you may read about CMake target. clean. A CMake target has dependencies and properties. . You may create custom targets: add custom target CMake tutorial 76 / 118 . generate something enabling the building of the target).

More CMake scripting Target dependencies and properties I A CMake target has dependencies and properties. Dependencies Most of the time. If this is not enough then one can use add dependencies. source dependencies are computed from target specifications using CMake builtin dependency scanner (C. C++. CMake tutorial 77 / 118 . Fortran) whereas library dependencies are inferred via target link libraries specification. or some properties.

link flags. linker language.More CMake scripting Target dependencies and properties II Properties Properties may be attached to either target or source file (or even test). see : set target properties or set source files properties Sources vs Targets Properties set to a target like COMPILE FLAGS are used for all sources of the concerned target. Properties set to a source are used for the source file itself (which may be involved in several targets). shared libraries version. They may be used to tailor prefix or suffix to be used for libraries. . CMake tutorial 78 / 118 . . compile flags. .

Bison) or other files derived from source like embedded documentation (Doxygen)..More CMake scripting Custom targets and commands Custom Custom targets and custom commands are a way to create target which may be used to execute arbitrary command at Build-time. in order to add some custom build step to another (existing) target. CMake tutorial 79 / 118 .. for target : add custom target for command : add custom command. . This is usually for: generating source files (Flex.

More CMake scripting Outline 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 80 / 118 .

. ).. Moreover you usually already need to do that when using a VCS (cvs. git. . However some files may be generated during the build (using add custom xxx) in this case you must tell CMake that they are GENERATED files using: 1 2 s e t s o u r c e f i l e s p r o p e r t i e s ( ${SOME GENERATED FILES} PROPERTIES GENERATED TRUE) CMake tutorial 81 / 118 . hg.. subversion. .More CMake scripting Generated files List all the sources CMake advocates to specify all the source files explicitly (this do not use file (GLOB .)) This is the only way to keep robust dependencies.

Makefiles.txt Project file(s). . . Object files Source files Generated Sources files CMake time Installed files Binary package Source package Build time Install time CPack time Package Install time Installed package CMake tutorial 82 / 118 . .More CMake scripting The CMake workflow (pictured) CMakeLists.

l l ) s e t ( LEX OUT PREFIX ${CMAKE CURRENT BINARY DIR} / l s m c t a s k f i l e t o k e n s y y ) s e t ( LEX WANTED OUT PREFIX ${CMAKE CURRENT BINARY DIR} / l s m c t a s k f i l e t o k e n s ) #Exec Lex add custom command ( OUTPUT ${LEX WANTED OUT PREFIX } . c ${YACC WANTED OUT PREFIX } . c COMMAND ${LEX PROGRAM} ARGS −l −o${LEX WANTED OUT PREFIX } .More CMake scripting Example I 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 # ## Handle Source g e n e r a t i o n f o r t a s k f i l e p a r s e r i n c l u d e d i r e c t o r i e s ( ${CMAKE CURRENT SOURCE DIR} ) f i n d p a c k a g e ( LexYacc ) s e t ( YACC SRC ${CMAKE CURRENT SOURCE DIR} / l s m c t a s k f i l e s y n t a x . c DEPENDS ${YACC SRC} CMake tutorial 83 / 118 . tab ) s e t (YACC WANTED OUT PREFIX ${CMAKE CURRENT BINARY DIR} / l s m c t a s k f i l e s y n t a x ) s e t ( LEX SRC ${CMAKE CURRENT SOURCE DIR} / l s m c t a s k f i l e t o k e n s . c ) #Exec Yacc add custom command ( OUTPUT ${YACC WANTED OUT PREFIX } . yy ) s e t ( YACC OUT PREFIX ${CMAKE CURRENT BINARY DIR} / y . h COMMAND ${CMAKECOMMAND} −E copy ${YACC OUT PREFIX } . c ${YACC WANTED OUT PREFIX } . h COMMAND ${YACC PROGRAM} ARGS ${YACC COMPAT ARG} −d ${YACC SRC} COMMAND ${CMAKECOMMAND} −E copy ${YACC OUT PREFIX } . h ${YACC WANTED OUT PREFIX } . c ${LEX SRC} DEPENDS ${LEX SRC} ) s e t (GENERATED SRCS ${GENERATED SRCS} ${LEX WANTED OUT PREFIX } .

c ${GENERATED SRCS} ) a d d l i b r a r y ( lsmc ${LSCHED SRC} ) CMake tutorial 84 / 118 . c l s m c t a s k f i l e p a r s e r .. h ) # T e l l CMake t h a t some f i l e a r e g e n e r a t e d s e t s o u r c e f i l e s p r o p e r t i e s ( ${GENERATED SRCS} PROPERTIES GENERATED TRUE) # I n h i b i t c o m p i l e r w a r n i n g f o r LEX / YACC g e n e r a t e d f i l e s # Note t h a t t h e i n h i b i t i o n i s COMPILER d ep e n de n t . c ${YACC WANTED OUT PREFIX } . c lsmc time . c l s m c c o r e . . c PROPERTIES COMPILE FLAGS "-w" ) e n d i f ( CMAKE COMPILER IS GNUCC ) . h PROPERTIES COMPILE FLAGS "-w" ) SET SOURCE FILES PROPERTIES ( ${LEX WANTED OUT PREFIX } . # GNU CC s p e c i f i c w a r n i n g s t o p i f ( CMAKE COMPILER IS GNUCC ) message (STATUS "INHIBIT Compiler warning for LEX/YACC generated files" ) SET SOURCE FILES PROPERTIES ( ${YACC WANTED OUT PREFIX } . s e t ( LSCHED SRC lsmc dependency .More CMake scripting Example II 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 ) s e t (GENERATED SRCS ${GENERATED SRCS} ${YACC WANTED OUT PREFIX } .. c l s m c u t i l s . . c ${YACC WANTED OUT PREFIX } .

Advanced CMake usage Outline 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 85 / 118 .

Advanced CMake usage Outline 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 86 / 118 .

./totally-free/ Demo CMake tutorial 87 / 118 .cmake .Advanced CMake usage Cross-compiling Definition: Cross-compiling Cross-compiling is when the host system. the one the compiled program will be running on.. 1 2 3 mkdir build-win32 cd build-win32 cmake -DCMAKE_TOOLCHAIN_FILE=.cmake. the one the compiler is running on.org/Wiki/CMake_Cross_Compiling. is not the same as the target system./totally-free/Toolchain-cross-mingw32-linux. CMake can handle cross-compiling using Toolchain see http://www.

n e t / s e t ( COMPILER PREFIX "i686 -w64 -mingw32" ) #s e t ( COMPILER PREFIX ” x 8 6 6 4−w64−mingw32 ” # which c o m p i l e r s t o use f o r C and C++ f i n d p r o g r a m (CMAKE RC COMPILER NAMES ${COMPILER PREFIX}−w i n d r e s ) #SET ( CMAKE RC COMPILER ${COMPILER PREFIX}−w i n d r e s ) f i n d p r o g r a m (CMAKE C COMPILER NAMES ${COMPILER PREFIX}−gcc ) #SET ( CMAKE C COMPILER ${COMPILER PREFIX}−g c c ) f i n d p r o g r a m (CMAKE CXX COMPILER NAMES ${COMPILER PREFIX}−g++) #SET ( CMAKE CXX COMPILER ${COMPILER PREFIX}−g++) # here i s the t a r g e t environment located SET(USER ROOT PATH / home / e r k / erk−win32−dev ) SET(CMAKE FIND ROOT PATH / u s r / ${COMPILER PREFIX} ${USER ROOT PATH} ) # a d j u s t t h e d e f a u l t b e h a v i o u r o f t h e FIND XXX ( ) commands : # s e a r c h h e a d e r s and l i b r a r i e s i n t h e t a r g e t e n v i r o n m e n t . s e a r c h # programs i n the host environment s e t (CMAKE FIND ROOT PATH MODE PROGRAM NEVER) s e t ( CMAKE FIND ROOT PATH MODE LIBRARY ONLY) s e t (CMAKE FIND ROOT PATH MODE INCLUDE ONLY) CMake tutorial 88 / 118 . s o u r c e f o r g e . mingw .Advanced CMake usage Linux to Win32 Toolchain example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 # t h e name o f t h e t a r g e t o p e r a t i n g s y s t e m SET(CMAKE SYSTEM NAME Windows ) # Choose an a p p r o p r i a t e c o m p i l e r p r e f i x # f o r c l a s s i c a l mingw32 see h t t p : / / www . o r g / #s e t ( COMPILER PREFIX ” i 5 8 6−mingw32msvc ” ) # f o r 32 o r 64 b i t s mingw−w64 see h t t p : / / mingw−w64 .

Advanced CMake usage Outline 1 2 Basic CMake usage Discovering environment specificities Handling platform specificities Working with external packages More CMake scripting Custom commands Generated files Advanced CMake usage Cross-compiling with CMake Export your project 3 4 CMake tutorial 89 / 118 .

cmake.org/Wiki/CMake/Tutorials/Exporting_ and_Importing_Targets CMake tutorial 90 / 118 . see: http://www.Advanced CMake usage Exporting/Import your project Export/Import to/from others CMake can help project using CMake as a build system to export/import targets to/from other project using CMake as a build system. No more time for that today sorry.

CPack: Packaging made easy Outline 5 CPack: Packaging made easy 6 CPack with CMake 7 Various package generators CMake tutorial 91 / 118 .

Bundle.TGZ. RPM (Linux) Cygwin Source or Binary (Windows/Cygwin) NSIS (Windows.CPack: Packaging made easy Introduction A Package generator In the same way as CMake generates build files. . CPack generates packager files. ] (All platforms) DEB. Linux) DragNDrop.. OSXX11 (MacOS) CMake tutorial 92 / 118 . Archive generators [ZIP. .

CPack with CMake Outline 5 CPack: Packaging made easy 6 CPack with CMake 7 Various package generators CMake tutorial 93 / 118 .

Makefiles. . .CPack with CMake The CMake workflow (pictured) CMakeLists. .txt Project file(s). Object files Source files Generated Sources files CMake time Installed files Binary package Source package Build time Install time CPack time Package Install time Installed package CMake tutorial 94 / 118 .

g. CPack with CMake When CPack is used to package a project built with CPack then the CPack configuration is usually generated by CMake by including CPack.cmake. This file is a CMake language script which defines CPACK XXXX variables: the config parameters of the CPack run.txt: include(CPack) CMake tutorial 95 / 118 . CPackConfig.CPack with CMake The CPack application CPack standalone CPack is a standalone application which behavior is driven by a configuration file e.cmake in the main CMakeLists.

CPack with CMake

CPack variables in CMakeLists.txt
When used with CMake, one writes something like this in CMakeLists.txt:
1 2 3 4 5 6 7 8 9 10 11

s e t (CPACK GENERATOR "TGZ" ) i f ( WIN32 ) l i s t (APPEND CPACK GENERATOR "NSIS" ) e l s e i f (APPLE) l i s t (APPEND CPACK GENERATOR "Bundle" ) e n d i f ( WIN32 ) s e t (CPACK SOURCE GENERATOR "ZIP;TGZ" ) s e t (CPACK PACKAGE VERSION MAJOR 0 ) s e t ( CPACK PACKAGE VERSION MINOR 1 ) s e t ( CPACK PACKAGE VERSION PATCH 0 ) i n c l u d e ( CPack )

This will create CPackSourceConfig.cmake and CPackConfig.cmake in the build tree and will bring you the package and package source built-in targets.
CMake tutorial 96 / 118

CPack with CMake

A CPack config file I
A CPack config file looks like this one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

# T h i s f i l e w i l l be c o n f i g u r e d t o c o n t a i n v a r i a b l e s f o r CPack . # These v a r i a b l e s s h o u l d be s e t i n t h e CMake l i s t f i l e o f t h e # p r o j e c t b e f o r e CPack module i s i n c l u d e d . ... SET( CPACK BINARY BUNDLE "" ) SET( CPACK BINARY CYGWIN "" ) SET( CPACK BINARY DEB "" ) ... SET( CPACK BINARY ZIP "" ) SET(CPACK CMAKE GENERATOR "Unix Makefiles" ) SET(CPACK GENERATOR "TGZ" ) SET( CPACK INSTALL CMAKE PROJECTS "/home/erk/erkit/CMakeTutorial/ examples/build;TotallyFree;ALL;/" ) SET( CPACK INSTALL PREFIX "/usr/local" ) SET(CPACK MODULE PATH "" ) SET( CPACK NSIS DISPLAY NAME "TotallyFree 0.1.0" )

CMake tutorial

97 / 118

CPack with CMake

A CPack config file II
16 17 18 19 20 21

22 23 24 25 26 27 28 29

SET( CPACK NSIS INSTALLER ICON CODE "" ) SET( CPACK NSIS INSTALL ROOT "$PROGRAMFILES" ) SET( CPACK NSIS PACKAGE NAME "TotallyFree 0.1.0" ) SET( CPACK OUTPUT CONFIG FILE "/home/erk/erkit/CMakeTutorial/ examples/build/CPackConfig.cmake" ) SET(CPACK PACKAGE DEFAULT LOCATION "/" ) SET( CPACK PACKAGE DESCRIPTION FILE "/home/erk/CMake/cmake -Verk HEAD/share/cmake -2.8/ Templates/CPack.GenericDescription.txt ") SET(CPACK PACKAGE DESCRIPTION SUMMARY "TotallyFree built using CMake" ) SET( CPACK PACKAGE FILE NAME "TotallyFree -0.1.0 - Linux" ) SET( CPACK PACKAGE INSTALL DIRECTORY "TotallyFree 0.1.0" ) SET( CPACK PACKAGE INSTALL REGISTRY KEY "TotallyFree 0.1.0" ) SET(CPACK PACKAGE NAME "TotallyFree" ) SET(CPACK PACKAGE RELOCATABLE "true" ) SET(CPACK PACKAGE VENDOR "Humanity" ) SET( CPACK PACKAGE VERSION "0.1.0" )

CMake tutorial

98 / 118

txt" ) SET( CPACK RESOURCE FILE WELCOME "/home/erk/CMake/cmake -Verk -HEAD /share/cmake -2.GenericDescription.8/ Templates/CPack.txt" ) SET( CPACK SET DESTDIR "OFF" ) SET(CPACK SOURCE CYGWIN "" ) SET(CPACK SOURCE GENERATOR "TGZ.GenericWelcome.TBZ2.CPack with CMake A CPack config file III 30 31 32 33 34 35 36 37 38 39 40 41 42 SET( CPACK RESOURCE FILE LICENSE "/home/erk/CMake/cmake -Verk -HEAD /share/cmake -2.txt" ) SET( CPACK RESOURCE FILE README "/home/erk/CMake/cmake -Verk -HEAD/ share/cmake -2.cmake" ) SET( CPACK SOURCE TBZ2 "ON" ) SET(CPACK SOURCE TGZ "ON" ) SET( CPACK SOURCE TZ "ON" ) SET( CPACK SOURCE ZIP "OFF" ) SET(CPACK SYSTEM NAME "Linux" ) SET(CPACK TOPLEVEL TAG "Linux" ) CMake tutorial 99 / 118 .8/ Templates/CPack.GenericLicense.8/ Templates/CPack.TZ" ) SET( CPACK SOURCE OUTPUT CONFIG FILE "/home/erk/erkit/ CMakeTutorial/examples/build/ CPackSourceConfig.

g.org/Wiki/CMake:CPackConfiguration http://www.cmake.cmake. thus the best CPack documentation is currently found on the Wiki: http://www.org/Wiki/CMake:CPackPackageGenerators http://www.cmake.: $ cpack -G RPM Currently cpack has [almost] no builtin documentation support besides cpack --help (work is underway though).org/Wiki/CMake: Component_Install_With_CPack CMake tutorial 100 / 118 .CPack with CMake CPack running steps I For a CMake enabled project one can run CPack in two ways: 1 2 use the build tool to run targets: package or package source invoke CPack manually from within the build tree e.

For each generator: 3 3 3 3 3 (re)sets CPACK GENERATOR to the one currently being iterated over includes the CPACK PROJECT CONFIG FILE installs the project into CPack private location (using DESTDIR) calls the generator and produces the package(s) for that generator CMake tutorial 101 / 118 . . it iterates over the generators list found in CPACK GENERATOR (or from -G command line option). the CPack steps are: 1 2 cpack command starts and parses arguments etc.cmake (usually found in the build tree) or the file given as an argument to --config command line option. it reads CPackConfig.CPack with CMake CPack running steps II Whichever way you call it. .

...package: <.package: <.CPack with CMake CPack running steps III 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 cpack command line example $ cpack -G "TGZ.gz generated.0-Linux.Run preinstall target for: TotallyFree CPack: .rpm generated.Install project: TotallyFree CPack: Create package CPack: .>/build/TotallyFree-0. CPack: Create package using RPM CPack: Install projects CPack: . $ CMake tutorial 102 / 118 .0-Linux.>/build/TotallyFree-0..spec CPack: .tar..>/build/_CPack_Packages/Linux/RPM/SPECS/totallyfree.RPM" CPack: Create package using TGZ CPack: Install projects CPack: .1..1.Run preinstall target for: TotallyFree CPack: .Install project: TotallyFree CPack: Create package CPackRPM: Will use GENERATED spec file: <.

gz generated.Install project: TotallyFree CPack: Create package CPack: . CPack: Create package using TGZ CPack: Install projects CPack: . CMake tutorial 103 / 118 ..1.package: <. Rebuild project In the make package case CMake is checking that the project does not need a rebuild..tar..Run preinstall target for: TotallyFree CPack: .CPack with CMake CPack running steps IV 1 2 3 4 5 6 7 8 9 10 11 make package example $ make package [ 33%] Built target acrodict [ 66%] Built target Acrodictlibre [100%] Built target Acrolibre Run CPack packaging tool..>/build/TotallyFree-0.0-Linux.

>/build/TotallyFree-0..bz2 generated. CPack: Create package using TGZ CPack: Install projects CPack: .0-Source.tar.>/build/TotallyFree-0.package: <.0-Source...Install directory: <... CPack: Create package using TBZ2 CPack: Install projects CPack: ..1.CPack with CMake CPack running steps V 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 make package source example $ make package_source make package_source Run CPack packaging tool for source.1.gz generated.package: <..>/build/TotallyFree-0.package: <.0-Source...tar.tar.1.>/totally-free CPack: Create package CPack: .Install directory: <.Z generated.>/totally-free CPack: Create package CPack: . CMake tutorial 104 / 118 .. CPack: Create package using TZ CPack: Install projects CPack: .Install directory: <.....>/totally-free CPack: Create package CPack: .

CPack with CMake The CPack workflow (pictured) CMakeLists.txt Source files CMake tutorial 105 / 118 .

CPack with CMake The CPack workflow (pictured) CMakeLists.txt Sou Tree rce Source files CMake tutorial 105 / 118 .

CPack with CMake The CPack workflow (pictured) CMakeLists.txt Sou Tree rce ild Bu ee Tr Source files CMake time Build time Install time (from CPack) CPack time Package Install time CMake tutorial 105 / 118 .

cmake ee Tr Sou Tree rce ild Bu Source files CMake time Build time Install time (from CPack) CPack time Package Install time CPackSourceConfig.cmake CMake tutorial 105 / 118 .CPack with CMake The CPack workflow (pictured) CMakeLists.txt CPackConfig.

CPack with CMake The CPack workflow (pictured) CMakeLists.cmake Source package CMake tutorial 105 / 118 .cmake ee Tr Sou Tree rce ild Bu Source files CMake time Build time Install time (from CPack) CPack time Package Install time CPackSourceConfig.txt CPackConfig.

txt CPackConfig.CPack with CMake The CPack workflow (pictured) CMakeLists.cmake ee Tr CPack Installed files Sou Tree rce ild Bu Source files CMake time Build time Install time (from CPack) CPack time Package Install time CPackSourceConfig.cmake Source package CMake tutorial 105 / 118 .

cmake Source package CMake tutorial 105 / 118 .txt CPackConfig.cmake ee Tr CPack Installed files Sou Tree rce ild Bu Source files Binary package CMake time Build time Install time (from CPack) CPack time Package Install time CPackSourceConfig.CPack with CMake The CPack workflow (pictured) CMakeLists.

cmake Source package CMake tutorial 105 / 118 .CPack with CMake The CPack workflow (pictured) CMakeLists.cmake ee Tr CPack Installed files Sou Tree rce ild Bu Source files Binary package Installed package CMake time Build time Install time (from CPack) CPack time Package Install time CPackSourceConfig.txt CPackConfig.

Moreover CPack knows that a project is built with CMake and inherits many properties from the install rules found in the project. exclude and include rules. CMake tutorial 106 / 118 .cmake. CPack binary package The CPack configuration file is: CPackConfig.CPack with CMake Source vs Binary Generators CPack does not really distinguish “source” from “binaries”!! CPack source package The CPack configuration file is: CPackSourceConfig. The CPack source generator is essentially packaging directories with install.cmake.

Various package generators Outline 5 CPack: Packaging made easy 6 CPack with CMake 7 Various package generators CMake tutorial 107 / 118 .

google.Various package generators Archive Generators A family of generators The archive generators is a family of generators which is supported on all CMake supported platforms through libarchive: http://code. STGZ Self extracting Tar GZip compression TBZ2 Tar BZip2 compression TGZ Tar GZip compression TZ Tar Compress compression ZIP Zip archive CMake tutorial 108 / 118 .com/p/libarchive/.

Those people can get a clean package without too much effort and get better package than a bare TAR archive. CMake tutorial 109 / 118 .rpm”. No official packaging replacement Those generators are no replacement for official packaging tools.deb or . CPack vs native tools One could argue “why using CPack for building .Various package generators Linux-friendly generators Tar-kind archive generators Binary RPM: only needs rpmbuild to work. The primary target of CPack RPM and DEB generators are people who are NOT professional packager. Binary DEB: works on any Linux distros.

Various package generators Windows-friendly generators Zip archive generator NullSoft System Installer generator (http://nsis. Cygwin: Binary and Source generators.com/Bug/view. produce nice GUI installer.php?id=11575.sourceforge.kitware.net/ Support component installation. CMake tutorial 110 / 118 . MSI installer requested: http://public.

org/Wiki/CMake: CPackPackageGenerators http://www.Various package generators MacOS-friendly generators Tar-kind archive generators DragNDrop PackageMaker OSXX11 Bundle Don’t ask me I’m not a MacOS user and I don’t know them.org/cmake/help/mailing. Go and read the Wiki or ask on the ML. http://www.html CMake tutorial 111 / 118 .cmake.cmake.

Add some more [CPack] information about how to group components. . CMake tutorial 2 3 4 5 6 112 / 118 . 1 Use COMPONENT argument in your install rules (in the CMakeLists.txt). ) Possibly specify generator specific behavior in CPACK PROJECT CONFIG FILE Run CPack. 1 package file per group. etc. . Choose a component-aware CPack generator Choose the behavior (1 package file per component.Various package generators Packaging Components I CMake+CPack installation components? Sometimes you want to split the installer into components.

(e. (e. ArchiveGenerator. RPM.cmake.g.Various package generators Packaging Components II demo with ComponentExample More detailed documentation here: http://www.org/Wiki/CMake:Component_Install_With_CPack Component aware generator Not all generators do support component (i. NSIS) Others produce several package files containing one or several components. DEB) CMake tutorial 113 / 118 .e. they are MONOLITHIC) Some produce a single package file containing all components.g.

Systematic Testing Outline 8 Systematic Testing 9 CTest submission to CDash 10 References CMake tutorial 114 / 118 .

CTest submission to CDash Outline 8 Systematic Testing 9 CTest submission to CDash 10 References CMake tutorial 115 / 118 .

In the meantime: Go there: http://www.CTest submission to CDash More to come on CTest/CDash Sorry.cdash.org Open your own (free) Dashboard: http://my.org/ CDash 2.0 should be released in the next few weeks (mid-february) A course on CMake/CTest/CDash in Lyon on April. stay tuned for next time. ..cdash..out of time!! CMake and its friends are so much fun and powerful that I ran out of time to reach a detailed presentation of CTest/CDash.kitware. .fr) CMake tutorial 116 / 118 . 2 2012 (http://formations.

References Outline 8 Systematic Testing 9 CTest submission to CDash 10 References CMake tutorial 117 / 118 .

5th edition edition. Feb. Development/CMake on KDE TechBase.org. Feb. Kitware. http://www. 2011. 2012..org/Development/CMake.org/Wiki/CMake.References References I CDash home page. Feb. ISBN-13 978-1930934221.cmake. Feb. http://www. CMake Wiki. 2012.kde. CMake home page. CMake tutorial 118 / 118 . Inc.cdash. http://www. Mastering CMake: A Cross-Platform Build System. Ken Martin and Bill Hoffman.cmake. 2010. 2012. http://techbase.org.

Sign up to vote on this title
UsefulNot useful