CMake Tutorial 8feb2012
CMake Tutorial 8feb2012
CMake Tutorial 8feb2012
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 workow, CPack users for their patience when things dont 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 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
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
4 / 118
Outlines
CMake tutorial
5 / 118
Outlines
Systematic Testing
10
References
CMake tutorial
6 / 118
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?
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,
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, because softwares are written in various languages that may share the same building process,
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, 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, Macintosh, Workstation, mobile phones and other PDA,
embbeded computers) and systems (Windows, Linux, *BSD, other Unices (many), Android, etc. . . )
CMake tutorial
7 / 118
Outlines
Programming languages
Compiled vs interpreted or what? Building an application requires the use of some programming language: Python, Java, C++, Fortran, C, Go, Tcl/Tk, Ruby, Perl, OCaml,. . .
Python Programming languages Perl OCaml executable C++ object code Fortran C
compiles links executes ?byte-compile?
interpreter
interprets
Running program
CMake tutorial
8 / 118
Outlines
Programming languages
Compiled vs interpreted or what? Building an application requires the use of some programming language: Python, Java, C++, Fortran, C, Go, Tcl/Tk, Ruby, Perl, OCaml,. . .
Python Programming languages Perl OCaml executable C++ object code Fortran C
compiles links executes ?byte-compile?
interpreter
interprets
Running program
CMake tutorial
8 / 118
Outlines
Outlines
Outlines
CMake/Auto[conf|make] on Ohloh
https://www.ohloh.net/languages/compare Language comparison of CMake to automake and autoconf showing the percentage of developers commits that modify a source le of the respective language.
CMake tutorial 11 / 118
Outlines
http://www.google.com/trends Scale is based on the average worldwide trac of cmake in all years.
CMake tutorial 12 / 118
Outline
1 2
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
13 / 118
CMake tutorial
14 / 118
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
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
CMake time: CMake is running & processing CMakeLists.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.e. build) the sources, the underlying build tool (make, XCode, Code::Blocks. . . ) does.
CMake tutorial
15 / 118
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.
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
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
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
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
Source les
CMake tutorial
16 / 118
CMake tutorial
16 / 118
CMake tutorial
16 / 118
Installed les
CMake tutorial
16 / 118
Installed les
Binary package
Source package
CMake tutorial
16 / 118
Installed les
Binary package
Source package
Installed package
CMake tutorial
16 / 118
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 . c ) CMake scripting language is [mostly] declarative. It has commands which are documented from within CMake:
$ cmake --help-command-list | wc -l 96 $ cmake --help-command add_executable ... add_executable Add an executable to the project using the specified source files.
CMake tutorial 17 / 118
1 2 3 4 5 6
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.8.7.20120121-g751713-dirty project Set a name for the entire project. project(<projectname> [languageName1 languageName2 ... ] ) Sets the name of the project. Additionally this sets the variables <projectName>_BINARY_DIR and <projectName>_SOURCE_DIR to the respective values. Optionally you can specify which languages your project supports. Example languages are CXX (i.e. C++), C, Fortran, etc. By default C and CXX are enabled. E.g. if you do not have a C++ compiler, you can disable the check for it by explicitly listing the languages you want to support, e.g. C. By using the special language "NONE" all checks for any language can be disabled.
CMake tutorial
18 / 118
Source tree vs Build tree Even the most simple project should never mix-up sources with generated les. CMake supports out-of-source build.
CMake tutorial 19 / 118
CMake tutorial
20 / 118
Generated les are separate from manually edited ones (thus you dont have to clutter you favorite VCS ignore les).
CMake tutorial
20 / 118
Generated les are separate from manually edited ones (thus you dont have to clutter you favorite VCS ignore les). You can have several build trees for the same source tree
CMake tutorial
20 / 118
Generated les are separate from manually edited ones (thus you dont have to clutter you favorite VCS ignore les). You can have several build trees for the same source tree This way its always safe to completely delete the build tree in order to do a clean build
CMake tutorial 20 / 118
2 3
CMake tutorial
21 / 118
CMake tutorial
21 / 118
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
CMake tutorial
22 / 118
1 2 3 4 5 6 7 8 9
#i n c l u d e < s t d l i b . h> #i n c l u d e < s t r i n g . h> #i n c l u d e "acrodict.h" s t a t i c const acroItem t acrodict [ ] = { {"Toulibre" , "Toulibre is a french organization promoting FLOSS" } , {"GNU" , "GNU is Not Unix" } , {"GPL" , "GNU general Public License" }, {"BSD" , "Berkeley Software Distribution" } , {"CULTe" , "Club des Utilisateurs de Logiciels libres et de gnu/ linux de Toulouse et des environs" } ,
CMake tutorial
23 / 118
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 . 8 ) p r o j e c t ( TotallyFree C) add executable ( Acrolibre a c r o l i b r e . c ) s e t ( LIBSRC a c r o d i c t . c a c r o d i c t . 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 ) 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" )
Building a library II
And it builds... All in all CMake generates appropriate Unix makeles which build all this smoothly.
1 2 3 4 5 6 7 8 9 10 11 12 13 CMake + Unix Makefile $ make [ 33%] Building C object CMakeFiles/acrodict.dir/acrodict.c.o Linking C shared library libacrodict.so [ 33%] Built target acrodict [ 66%] Building C object CMakeFiles/Acrodictlibre.dir/acrolibre.c.o Linking C executable Acrodictlibre [ 66%] Built target Acrodictlibre [100%] Building C object CMakeFiles/Acrolibre.dir/acrolibre.c.o Linking C executable Acrolibre [100%] Built target Acrolibre $ ls -F Acrodictlibre* CMakeCache.txt cmake_install.cmake Makefile Acrolibre* CMakeFiles/ libacrodict.so*
CMake tutorial
25 / 118
Generated Makefiles has several builtin targets besides the expected ones:
one per target (library or executable) clean, all more to come . . .
Call convention All tools expect to be called with a single argument which may be interpreted in 2 dierent ways.
path to the source tree, e.g.: cmake /path/to/source path to an existing build tree, e.g.: cmake-gui .
CMake tutorial
28 / 118
CMake tutorial
29 / 118
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 .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 CodeBlocks - NMake Makefiles CodeBlocks - Unix Makefiles Eclipse CDT4 - MinGW Makefiles Eclipse CDT4 - NMake Makefiles Eclipse CDT4 - Unix Makefiles KDevelop3 KDevelop3 - Unix Makefiles XCode Ninja (in development) http://martine.github.com/ninja/
CMake tutorial 31 / 118
CMake tutorial
32 / 118
Installing things
Install Several parts or the software may need to be installed, this is controlled by the CMake install command. Remember cmake --help-command install!! Listing 4: install command examples
1 2 3 4 5 6 7 8 9 10 11 ... add executable ( Acrolibre a c r o l i b r e . 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) ... 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 . h DESTINATION i n c l u d e ) e n d i f (WITH ACRODICT)
CMake tutorial
33 / 118
CMake tutorial
34 / 118
CMake tutorial
34 / 118
CMake tutorial
34 / 118
Installed les
Binary package
Source package
Installed package
CMake tutorial
35 / 118
$ make DESTDIR=/tmp/testinstall install [ 33%] Built target acrodict [ 66%] Built target Acrodictlibre [100%] Built target Acrolibre Install the project... -- Install configuration: "" -- Installing: /tmp/testinstall/bin/Acrolibre -- Installing: /tmp/testinstall/bin/Acrodictlibre -- Removed runtime path from "/tmp/testinstall/bin/Acrodictlibre" -- Installing: /tmp/testinstall/lib/libacrodict.so -- Installing: /tmp/testinstall/include/acrodict.h $
CMake tutorial 37 / 118
CMake tutorial
38 / 118
$ cpack -G RPM CPack: Create package using RPM CPack: Install projects CPack: - Run preinstall target for: TotallyFree CPack: - Install project: TotallyFree CPack: Create package CPackRPM: Will use GENERATED spec file: <build-tree>/... _CPack_Packages/Linux/RPM/SPECS/totallyfree.spec CPack: - package: <build-tree>/... TotallyFree-0.1.0-Linux.rpm generated. $ rpm -qpl TotallyFree-0.1.0-Linux.rpm /usr /usr/bin /usr/bin/Acrodictlibre /usr/bin/Acrolibre /usr/include /usr/include/acrodict.h /usr/lib /usr/lib/libacrodict.so CMake tutorial 39 / 118
CMake tutorial
41 / 118
CMake tutorial
43 / 118
Summary
CMake basics Using CMake basics we can already do a lot a things with minimal writing.
Write simple build specication le: CMakeLists.txt Discover compilers (C, 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
Read the FAQ: http://www.cmake.org/Wiki/CMake_FAQ Read the Wiki: http://www.cmake.org/Wiki/CMake Ask on the Mailing List: http://www.cmake.org/cmake/help/mailing.html Browse the built-in help: cmake --help-xxxxx
CMake tutorial
45 / 118
Outline
1 2
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
46 / 118
Outline
1 2
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
47 / 118
CMake tutorial
48 / 118
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 ) ... 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 . 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 . 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 denes a CMake option, but not on WIN32 system. Then on line 11, if the option is set then we pass a source specic compile ags. cmake --help-command set source files properties
CMake tutorial
50 / 118
lookup system informations using CMake variable, functions, macros (built-in or imported) then set various variables, use the dened variable in order to write a template conguration header le then use congure le in order to produce the actual cong le from the template.
CMake tutorial
52 / 118
CMake tutorial
53 / 118
CMake tutorial
54 / 118
CMake tutorial
55 / 118
Outline
1 2
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
56 / 118
The
nd package
command I
Finding external package Project may be using external libraries, program, les etc. . . Those can be found using the nd package command.
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
1
nd package
command II
5 6
See doc cmake --help-module FindLibXml2 Many modules are provided by CMake (130 as of CMake 2.8.7)
CMake tutorial
58 / 118
The
nd package
command III
You may write your own: http://www.cmake.org/Wiki/CMake:Module_Maintainers You may nd/borrow modules from other projects which use CMake
KDE4: http://websvn.kde.org/trunk/KDE/kdelibs/cmake/modules/ PlPlot: http://plplot.svn.sourceforge.net/viewvc/plplot/ trunk/cmake/modules/ http://cmake-modules.googlecode.com/svn/trunk/Modules/ probably many more. . .
A module may provide not only CMake variables but new CMake macros (we will see that later with the MACRO, FUNCTION CMake language commands)
CMake tutorial
59 / 118
The other
nd xxxx
commands I
The nd xxx command family nd package is a high level module nding mechanism but there are lower-level CMake commands which may be used to write nd modules or anything else inside CMakeLists.txt
to nd an executable program: nd program to nd a library: nd library to nd any kind of le: nd le to nd a path where a le reside: nd path
CMake tutorial
60 / 118
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
nd 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 . l i f l . f r / f o r g e t / p r e l u d e . h t m l # T h i s module d e f i n e s # PRELUDE COMPILER , t h e p r e l u d e c o m p i l e r # PRELUDE COMPILER VERSION , 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 , where t o f i n d dword . h , e t c . # PRELUDE FOUND, I f f a l s e , P r e l u d e was n o t f o u n d . # 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 . ... 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
The other
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
nd 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 . 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
CMake tutorial
63 / 118
CMake tutorial
64 / 118
Outline
1 2
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
65 / 118
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
CMake tutorial
66 / 118
CMake tutorial
66 / 118
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.8.7 Usage: cmake -E [command] [arguments ...] Available commands: chdir dir cmd [args]... - run command in a given directory compare_files file1 file2 - check if file1 is same as file2 copy file destination - copy file to destination (either file or directory) copy_directory source destination - copy directory source content to directory destination copy_if_different in-file out-file - copy file if input has changed echo [string]... - displays arguments as text echo_append [string]... - displays arguments as text but no new line environment - display the current environment make_directory dir - create a directory md5sum file1 [...] - compute md5sum of files remove [-f] file1 file2 ... - remove the file(s), use -f to force it remove_directory dir - remove a directory and its contents rename oldname newname - rename a file or directory (on one volume) tar [cxt][vfz][cvfj] file.tar file/dir1 file/dir2 ... - create a tar archive time command [args] ... - run command and return elapsed time touch file - touch a file. touch_nocreate file - touch a file but do not create it. Available on UNIX only: create_symlink old new - create a symbolic link new -> old
CMake tutorial
67 / 118
CMake scripting
Overview of CMake language CMake is a declarative language which contains 90+ commands. It contains general purpose constructs: set , unset, if , elseif , else , endif, foreach, while, 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. message([STATUS|WARNING|AUTHOR_WARNING|FATAL_ERROR|SEND_ERROR] "message to display" ...) The optional keyword determines the type of message: (none) = Important information STATUS = Incidental information WARNING = CMake Warning, continue processing AUTHOR_WARNING = CMake Warning (dev), continue processing SEND_ERROR = CMake Error, continue but skip generation FATAL_ERROR = CMake Error, stop all processing CMake tutorial 68 / 118
advanced les operations: GLOB, GLOB RECURSE le name in a path, DOWNLOAD, UPLOAD working with path: le (TO CMAKE PATH /TO NATIVE PATH ...),
string manipulation: string , upper/lower case conversion, length, comparison, substring, regular expression match, . . .
CMake tutorial 69 / 118
cmake minimum required (VERSION 2 . 8 ) s e t (CMAKE VERSION "2.8.7" ) s e t ( CMAKE FILE PREFIX "cmake -${CMAKE_VERSION}" ) s e t (CMAKE REMOTE PREFIX "http :// www.cmake.org/files/v2.8/" ) s e t ( CMAKE FILE SUFFIX ".tar.gz" ) s e t ( CMAKE BUILD TYPE "Debug" ) s e t (CPACK GEN "TGZ" )
CMake tutorial
70 / 118
CMake tutorial
71 / 118
CMake tutorial
72 / 118
CMake tutorial
73 / 118
CMake tutorial
74 / 118
Outline
1 2
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
75 / 118
1 2 3 4
Executable are targets: add executable Libraries are targets: add library There exist some builtin targets: install, clean, package, . . . You may create custom targets: add custom target
CMake tutorial 76 / 118
CMake tutorial
77 / 118
78 / 118
This is usually for: generating source les (Flex, Bison) or other les derived from source like embedded documentation (Doxygen), ...
CMake tutorial
79 / 118
Outline
1 2
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
80 / 118
Generated les
List all the sources CMake advocates to specify all the source les explicitly (this do not use le (GLOB ...)) This is the only way to keep robust dependencies. Moreover you usually already need to do that when using a VCS (cvs, subversion, git, hg,. . . ). However some les may be generated during the build (using add custom xxx) in this case you must tell CMake that they are GENERATED les using:
1 2
CMake tutorial
81 / 118
Installed les
Binary package
Source package
Installed package
CMake tutorial
82 / 118
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 . yy ) s e t ( YACC OUT PREFIX ${CMAKE CURRENT BINARY DIR} / y . 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 . 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 COMMAND ${LEX PROGRAM} ARGS l o${LEX WANTED OUT PREFIX } . c ${LEX SRC} DEPENDS ${LEX SRC} ) s e t (GENERATED SRCS ${GENERATED SRCS} ${LEX WANTED OUT PREFIX } . c ) #Exec Yacc add custom command ( OUTPUT ${YACC WANTED 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 } . h COMMAND ${CMAKECOMMAND} E copy ${YACC OUT PREFIX } . c ${YACC WANTED OUT PREFIX } . c DEPENDS ${YACC SRC}
CMake tutorial
83 / 118
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 ${YACC WANTED OUT PREFIX } . 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 . . . # 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 } . c ${YACC WANTED OUT PREFIX } . h PROPERTIES COMPILE FLAGS "-w" ) SET SOURCE FILES PROPERTIES ( ${LEX WANTED OUT PREFIX } . c PROPERTIES COMPILE FLAGS "-w" ) e n d i f ( CMAKE COMPILER IS GNUCC ) ... s e t ( LSCHED SRC lsmc dependency . c l s m c c o r e . c l s m c u t i l s . c lsmc time . c l s m c t a s k f i l e p a r s e r . c ${GENERATED SRCS} ) a d d l i b r a r y ( lsmc ${LSCHED SRC} )
CMake tutorial
84 / 118
Outline
1 2
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
85 / 118
Outline
1 2
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
86 / 118
Cross-compiling
Denition: Cross-compiling Cross-compiling is when the host system, the one the compiler is running on, is not the same as the target system, the one the compiled program will be running on. CMake can handle cross-compiling using Toolchain see http://www.cmake.org/Wiki/CMake_Cross_Compiling.
1 2 3 mkdir build-win32 cd build-win32 cmake -DCMAKE_TOOLCHAIN_FILE=../totally-free/Toolchain-cross-mingw32-linux.cmake ../totally-free/
Demo
CMake tutorial 87 / 118
Outline
1 2
Basic CMake usage Discovering environment specicities Handling platform specicities Working with external packages More CMake scripting Custom commands Generated les Advanced CMake usage Cross-compiling with CMake Export your project
CMake tutorial
89 / 118
CMake tutorial
90 / 118
Outline
CMake tutorial
91 / 118
Introduction
A Package generator In the same way as CMake generates build les, CPack generates packager les.
Archive generators [ZIP,TGZ,. . . ] (All platforms) DEB, RPM (Linux) Cygwin Source or Binary (Windows/Cygwin) NSIS (Windows, Linux) DragNDrop, Bundle, OSXX11 (MacOS)
CMake tutorial 92 / 118
Outline
CMake tutorial
93 / 118
Installed les
Binary package
Source package
Installed package
CMake tutorial
94 / 118
95 / 118
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
A CPack cong le I
A CPack cong le 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
A CPack cong le 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
SET( CPACK RESOURCE FILE LICENSE "/home/erk/CMake/cmake -Verk -HEAD /share/cmake -2.8/ Templates/CPack.GenericLicense.txt" ) SET( CPACK RESOURCE FILE README "/home/erk/CMake/cmake -Verk -HEAD/ share/cmake -2.8/ Templates/CPack.GenericDescription.txt" ) SET( CPACK RESOURCE FILE WELCOME "/home/erk/CMake/cmake -Verk -HEAD /share/cmake -2.8/ Templates/CPack.GenericWelcome.txt" ) SET( CPACK SET DESTDIR "OFF" ) SET(CPACK SOURCE CYGWIN "" ) SET(CPACK SOURCE GENERATOR "TGZ;TBZ2;TZ" ) SET( CPACK SOURCE OUTPUT CONFIG FILE "/home/erk/erkit/ CMakeTutorial/examples/build/ CPackSourceConfig.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
use the build tool to run targets: package or package source invoke CPack manually from within the build tree e.g.: $ cpack -G RPM
Currently cpack has [almost] no builtin documentation support besides cpack --help (work is underway though), thus the best CPack documentation is currently found on the Wiki:
http://www.cmake.org/Wiki/CMake:CPackConfiguration http://www.cmake.org/Wiki/CMake:CPackPackageGenerators http://www.cmake.org/Wiki/CMake: Component_Install_With_CPack
CMake tutorial
100 / 118
cpack command starts and parses arguments etc. . . it reads CPackConfig.cmake (usually found in the build tree) or the le given as an argument to --config command line option. it iterates over the generators list found in CPACK GENERATOR (or from -G command line option). For each generator:
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
cpack command line example $ cpack -G "TGZ;RPM" CPack: Create package using TGZ CPack: Install projects CPack: - Run preinstall target for: TotallyFree CPack: - Install project: TotallyFree CPack: Create package CPack: - package: <...>/build/TotallyFree-0.1.0-Linux.tar.gz generated. CPack: Create package using RPM CPack: Install projects CPack: - Run preinstall target for: TotallyFree CPack: - Install project: TotallyFree CPack: Create package
CPackRPM: Will use GENERATED spec file: <...>/build/_CPack_Packages/Linux/RPM/SPECS/totallyfree.spec
CMake tutorial
102 / 118
make package example $ make package [ 33%] Built target acrodict [ 66%] Built target Acrodictlibre [100%] Built target Acrolibre Run CPack packaging tool... CPack: Create package using TGZ CPack: Install projects CPack: - Run preinstall target for: TotallyFree CPack: - Install project: TotallyFree CPack: Create package CPack: - package: <...>/build/TotallyFree-0.1.0-Linux.tar.gz generated.
Rebuild project In the make package case CMake is checking that the project does not need a rebuild.
CMake tutorial
103 / 118
make package source example $ make package_source make package_source Run CPack packaging tool for source... CPack: Create package using TGZ CPack: Install projects CPack: - Install directory: <...>/totally-free CPack: Create package CPack: - package: <...>/build/TotallyFree-0.1.0-Source.tar.gz generated. CPack: Create package using TBZ2 CPack: Install projects CPack: - Install directory: <...>/totally-free CPack: Create package CPack: - package: <...>/build/TotallyFree-0.1.0-Source.tar.bz2 generated. CPack: Create package using TZ CPack: Install projects CPack: - Install directory: <...>/totally-free CPack: Create package CPack: - package: <...>/build/TotallyFree-0.1.0-Source.tar.Z generated.
CMake tutorial
104 / 118
Source les
CMake tutorial
105 / 118
Sou
Tree rce
Source les
CMake tutorial
105 / 118
Sou
Tree rce
ild Bu
ee Tr
Source les
CMake time Build time Install time (from CPack) CPack time Package Install time
CMake tutorial
105 / 118
Sou
Tree rce
ild Bu
Source les
CMake time Build time Install time (from CPack) CPack time Package Install time
CPackSourceCong.cmake
CMake tutorial
105 / 118
Sou
Tree rce
ild Bu
Source les
CMake time Build time Install time (from CPack) CPack time Package Install time
CPackSourceCong.cmake
Source package
CMake tutorial
105 / 118
Sou
Tree rce
ild Bu
Source les
CMake time Build time Install time (from CPack) CPack time Package Install time
CPackSourceCong.cmake
Source package
CMake tutorial
105 / 118
Sou
Tree rce
ild Bu
Source les
Binary package
CMake time Build time Install time (from CPack) CPack time Package Install time
CPackSourceCong.cmake
Source package
CMake tutorial
105 / 118
Sou
Tree rce
ild Bu
Source les
Binary package
Installed package
CMake time Build time Install time (from CPack) CPack time Package Install time
CPackSourceCong.cmake
Source package
CMake tutorial
105 / 118
106 / 118
Outline
CMake tutorial
107 / 118
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.google.com/p/libarchive/.
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
Linux-friendly generators
Tar-kind archive generators Binary RPM: only needs rpmbuild to work. Binary DEB: works on any Linux distros.
CPack vs native tools One could argue why using CPack for building .deb or .rpm. The primary target of CPack RPM and DEB generators are people who are NOT professional packager. Those people can get a clean package without too much eort and get better package than a bare TAR archive. No ocial packaging replacement Those generators are no replacement for ocial packaging tools.
CMake tutorial
109 / 118
Windows-friendly generators
Zip archive generator NullSoft System Installer generator (http://nsis.sourceforge.net/ Support component installation, produce nice GUI installer. MSI installer requested: http://public.kitware.com/Bug/view.php?id=11575. Cygwin: Binary and Source generators.
CMake tutorial
110 / 118
MacOS-friendly generators
Tar-kind archive generators DragNDrop PackageMaker OSXX11 Bundle
Dont ask me Im not a MacOS user and I dont know them. Go and read the Wiki or ask on the ML. http://www.cmake.org/Wiki/CMake: CPackPackageGenerators http://www.cmake.org/cmake/help/mailing.html
CMake tutorial
111 / 118
Packaging Components I
CMake+CPack installation components? Sometimes you want to split the installer into components.
1
Use COMPONENT argument in your install rules (in the CMakeLists.txt), Add some more [CPack] information about how to group components, Choose a component-aware CPack generator Choose the behavior (1 package le per component, 1 package le per group, etc. . . ) Possibly specify generator specic behavior in CPACK PROJECT CONFIG FILE Run CPack.
CMake tutorial
3 4
112 / 118
Packaging Components II
demo with ComponentExample More detailed documentation here:
http://www.cmake.org/Wiki/CMake:Component_Install_With_CPack
113 / 118
Systematic Testing
Outline
Systematic Testing
10
References
CMake tutorial
114 / 118
Outline
Systematic Testing
10
References
CMake tutorial
115 / 118
116 / 118
References
Outline
Systematic Testing
10
References
CMake tutorial
117 / 118
References
References I
CDash home page, Feb. 2011.
http://www.cdash.org.
CMake tutorial
118 / 118