Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to CMake, v. 2

Similar presentations


Presentation on theme: "Introduction to CMake, v. 2"— Presentation transcript:

1 Introduction to CMake, v. 2
Content: Basic CMake usage Ready to use samples Advanced CMake usage Document history: 2009 Constantine Shulyupin, 2007 Jan Engels, DESY,

2 Purposes of a build system
Efficiency: Save time on recompilations Flexibility: Configure compilation parameters (optimization, logging, debug and release builds) Scalability: Manage subproject and libraries Portability: Compile for different platforms Introduction to CMake, v. 2

3 Introduction to CMake, v. 2
Major CMake features Generates native build environments UNIX/Linux -> Makefiles Windows -> VS Projects/Workspaces Apple -> Xcode Eclipse projects Big library of helper commands (Finders) Cross-Platform (easy to port) Introduction to CMake, v. 2

4 Introduction to CMake, v. 2
Build process Target projects: CMake Source file: Makefile CMakeLists.txt CMakeLists.txt CMakeLists.txt CMake Eclipse Generated internal files: KDevelop CMakeCache.txt MS Visual Studio CMakeFiles/ And other Introduction to CMake, v. 2

5 Introduction to CMake, v. 2
CMake source file CMakeLists.txt Input text files that contain the project parameters and describe the flow control of the build process in simple CMake language. Introduction to CMake syntax: # This is a comment command(argument1 argument2 ...argumentN) COMMAND(argument1 argument2 ...argumentN) Introduction to CMake, v. 2

6 Introduction to CMake, v. 2
“Hello world!” File hello.c : int main() { printf("Hello, world!\n"); } File CMakeLists.txt : add_executable(hello hello.c) Shell: $ cmake . $ make $ ./hello Hello, world! Introduction to CMake, v. 2

7 Introduction to CMake, v. 2
Using Libraries For C source code: ... pthread_create(&pt, NULL, routine, NULL); Add to CMakeLists.txt : target_link_libraries(pthread) Introduction to CMake, v. 2

8 Introduction to CMake, v. 2
Creating Libraries Initial source file: common.h common.c Target library file: libcommon.so CMakeLists.txt : add_library (common SHARED common.c) Introduction to CMake, v. 2

9 Woking with subdirectories
Defining subproject: add_subdirectory(sub) CMake adds subdirectory to dependency. Child inherits from parent Search include files in subdirectories: include_directories(sub) Search libraries in subdirectories link_directories(sub) Introduction to CMake, v. 2

10 Additional useful commands
project (projectname [CXX] [C] [Java]) This creates the variables projectname_BINARY_DIR and projectname_SOURCE_DIR. cmake_minimum_required (VERSION 2.6) Set the minimum required version of cmake for a project. add_definitions (-g) Arguments are passed to compiler (like CFLAGS) message (text) Prints text Introduction to CMake, v. 2

11 Introduction to CMake, v. 2
Most useful commands project(name C CPP JAVA) cmake_minimum_required(VERSION 2.6) add_definitions(-g) # added to CFLAGS add_library(common SHARED common.c) add_subdirectory(sub) include_directories(sub) link_directories(sub) add_executable(app app.c) target_link_libraries(app common pthread) message(text) - 10 most useful functions for 90% of projects Introduction to CMake, v. 2

12 Introduction to CMake, v. 2
Advanced facilities Variables and lists Variables cache Out-of-source build Finding libraries Regular expressions Flow control Reusable code: includes and macros Installers Cross-compilation Introduction to CMake, v. 2

13 Introduction to CMake, v. 2
Variables and Lists Variables: set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") message(CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}) cmake -D <var>:<type>=<value> # define variable and store in cache Lists set(L 1;2;3) # semi-colon is list separator list(APPEND L a b c ) message("\${L}=" ${L}) message("\"\${L}\"=" "${L}") Output: ${L}=123abc "${L}"=1;2;3;a;b;c Introduction to CMake, v. 2

14 Introduction to CMake, v. 2
CMake Cache Created in the build tree ( CMakeCache.txt ) Contains Entries VAR:TYPE=VALUE Populated/Updated during configuration phase Speeds up build process Can be initialized with cmake -C <file> GUI can be used to change values Introduction to CMake, v. 2

15 Introduction to CMake, v. 2
Out-of-source build Out-of-source build used to separate build files from sources. Very useful for multi- platform development. Change to a build directory and give path to the sources as argument: mkdir -p ../build; cd ../build rm -f ../hello/CMakeCache.txt # cleanup after in-source build cmake ../hello make Introduction to CMake, v. 2

16 Introduction to CMake, v. 2
Finding libraries Special cmake file written for the purpose of finding a certain piece of software and to set its libraries, include files and definitions into appropriate variables so that they can be used in the build process of another project. (e.g. FindJava.cmake, FindZLIB.cmake, FindQt4.cmake) More than 300 modules located in /usr/share/cmake-2.6/Modules/ E.g. some lines from /usr/share/cmake-2.6/Modules/Platform/ Linux.cmake : SET(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-shared") SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "-rdynamic") SET(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,") Introduction to CMake, v. 2

17 Introduction to CMake, v. 2
Regular expressions Warning: non Perl compatible set (S "abc") string (REGEX REPLACE "b.*" "BC" S ${S}) set (VER 1.2) string (REGEX MATCHALL "([0-9]+)" NUMS "${VER}") # NUMS is list Introduction to CMake, v. 2

18 Introduction to CMake, v. 2
Flow control Most useful: if (exp) elseif (exp2) else ([exp]) endif ([exp]) And other: foreach while Introduction to CMake, v. 2

19 Introduction to CMake, v. 2
Reusable code include (<file|module>) macro (<name> [arg1 [arg2 [arg3 …]]]) command1 (ARGS ...) command2 (ARGS …) endmacro (<name>) function (<name> [arg1 [arg2 [arg3 …]]]) command1 (ARGS …) command2 (ARGS ...) endfunction (<name>) Introduction to CMake, v. 2

20 Introduction to CMake, v. 2
Installers Could be very simple install (TARGETS hello DESTINATION /tmp) Or more complex: set(CMAKE_INSTALL_PREFIX ....) install (TARGETS targets... [EXPORT <export- name>] [[ARCHIVE|LIBRARY|RUNTIME|FRAMEW ORK|BUNDLE| PRIVATE_HEADER|PUBLIC_HEADER|RES OURCE] [DESTINATION <dir>] [PERMISSIONS permissions...] [CONFIGURATIONS [Debug|Release|...]] [COMPONENT <component>] [OPTIONAL] [NAMELINK_ONLY|NAMELINK_SKIP] ] [...]) Introduction to CMake, v. 2

21 Introduction to CMake, v. 2
Cross-compilation arm.cmake file: set (CMAKE_SYSTEM_NAME Linux) set ( CMAKE_C_COMPILER /opt/arm- 2009q1/bin/arm-none-linux-gnueabi-gcc) set ( CMAKE_CXX_COMPILER /opt/arm- 2009q1/bin/arm-none-linux-gnueabi-gcc) set ( CMAKE_FIND_ROOT_PATH /opt/arm- 2009q1/arm-none-linux-gnueabi/) # adjust the default behaviour of the FIND_XXX() commands: set (CMAKE_FIND_ROOT_PATH_MODE_PROGR AM NEVER) set (CMAKE_FIND_ROOT_PATH_MODE_LIBRAR Y ONLY) set (CMAKE_FIND_ROOT_PATH_MODE_INCLUD E ONLY) $ cmake - DCMAKE_TOOLCHAIN_FILE=arm.cmake Introduction to CMake, v. 2

22 Well known CMake usages
KDE MySQL (on Windows only) Introduction to CMake, v. 2

23 Introduction to CMake, v. 2
Samples Sample sources for the presentation: KDE: Introduction to CMake, v. 2

24 Introduction to CMake, v. 2
Further readings Introduction to CMake, v. 2


Download ppt "Introduction to CMake, v. 2"

Similar presentations


Ads by Google