SlideShare una empresa de Scribd logo
1 de 58
Descargar para leer sin conexión
cmake
Introduction and best practices
Daniel Pfeifer
May 21, 2015
daniel@pfeifer-mail.de
interest over time
2005 2007 2009 2011 2013 2015
autoconf
automake
bjam
cmake
scons
0https://www.google.com/trends/explore#q=autoconf,automake,bjam,cmake,scons
1
what is cmake?
according to its website
Welcome to CMake, the cross-platform, open-source build system.
CMake is a family of tools designed to build, test and package
software. CMake is used to control the software compilation process
using simple platform and compiler independent configuration files.
CMake generates native makefiles and workspaces that can be used
in the compiler environment of your choice.1
1http://www.cmake.org/
3
cmake’s position among build systems
standalone Make, NMake, SCons, Jom, BJam, Ninja
integrated Visual Studio, Xcode, Eclipse
generators Autotools, CMake, GYP
4
cmake is simple
> ls zlib-1.2.8
amiga/ as400/ contrib/ doc/
examples/ msdos/ nintendods/ old/
qnx/ test/ watcom/ win32/
adler32.c ChangeLog CMakeLists.txt compress.c
configure crc32.c crc32.h deflate.c
deflate.h FAQ gzclose.c gzguts.h
gzlib.c gzread.c gzwrite.c INDEX
infback.c inffast.c inffast.h inffixed.h
inflate.c inflate.h inftrees.c inftrees.h
make_vms.com Makefile Makefile.in README
treebuild.xml trees.c trees.h uncompr.c
zconf.h zconf.h.in zlib.3 zlib.3.pdf
zlib.h zlib.map zlib.pc.in zlib2ansi
zutil.c zutil.h
5
reduce your maintenance ...
You write a single configuration that CMake understands.
CMake takes care that it works on all compilers and platforms.
6
... unless you break it!
Don’t make any assumption about the platform or compiler!
7
cmake is fast!
I was quite surprised with the speed of building Quantum GIS
codebase in comparison to Autotools.2
Task CMake Autotools
Configure 0:08 0:61
Make 12:15 21:16
Install 0:20 0:36
Total 12:43 22:43
2http://blog.qgis.org/node/16.html
8
the cmake cache
∙ Compiler and platform test results are cached in
CMakeCache.txt.
∙ You can modify this file and re-run CMake.
∙ CMake also provides graphical cache editors.
9
cmake’s graphical cache editor
10
cmake’s position on the time axis
past CMake generates build systems.
today CMake is integrated into IDEs.
future IDEs run CMake as a background process.3
3http://public.kitware.com/pipermail/cmake-developers/2015-April/025012.html
11
modes of operation
cmake as a build system generator
1 > cmake [<options>] <path>
∙ If the specified path contains a CMakeCache.txt, it is treated as a
build directory where the build system is reconfigured and
regenerated.
∙ Otherwise the specified path is treated as the source directory
and the current working directory is treated as build directory.
1 > mkdir build
2 > cd build
3 > time cmake ..
4 > time cmake .
5 > time make
13
cmake as a build system generator
Issues:
∙ The commands mkdir and time are not portable.
∙ With cmake . you may accidentally perform an in-source
configure.
∙ Who said that make is the right build command?
1 > mkdir build
2 > cd build
3 > time cmake ..
4 > time cmake .
5 > time make mylibrary
14
cmake’s command-line tool mode
1 > cmake -E <command> [<options>...]
Run cmake -E help for a summary of commands.
1 > cmake -E make_directory build
2 > cmake -E chdir cmake -E time cmake ..
3 > cmake -E time cmake build
15
use cmake to run the build system
1 > cmake --build build 
2 --target mylibrary 
3 --config Release 
4 --clean-first
Just don’t:
1 > ‘grep ’CMAKE_MAKE_PROGRAM:’ CMakeCache.txt 
2 | awk -F= ’{print $2}’‘ mylibrary
16
cmake as a scripting language
1 > cat hello_world.cmake
2 foreach(greet Hello Goodbye)
3 message(”${greet}, World!”)
4 endforeach()
5
6 > cmake -P hello_world.cmake
7 Hello, World!
8 Goodbye, World!
No configure or generate step is performed and the cache is not
modified.
17
Resist the bash/perl/python temptation!
18
commands for projects
cmake_minimum_required()
Sets the mimimum required version of CMake.
1 cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
∙ Prefer the latest version of CMake.
∙ Please don’t set 2.8 as the minimum.
∙ If you use 2.6 or 2.4, God kills a kitten.
20
project()
Set the name and version; enable languages.
1 project(<name> VERSION <version> LANGUAGES CXX)
∙ CMake sets several variables based on project().
∙ Call to project() must be direct, not through a
function/macro/include.
∙ CMake will add a call to project() if not found on the top level.
21
add_subdirectory()
Embed projects as sub-projects.
1 add_subdirectory(<sourcedir> [<binarydir>])
∙ CMake creates a Makefile/Solution for each subproject.
∙ The subproject does not have to reside in a subfolder.
22
hint!
Make sure that all your projects can be built both standalone and as
a subproject of another project:
∙ Don’t assume that your project root is the build root.
∙ Don’t modify global compile/link flags.
∙ Don’t make any global changes!
23
find_package()
Finds preinstalled dependencies
1 find_package(Qt5 REQUIRED COMPONENTS Widgets)
∙ Can set some variables and define imported targets.
∙ Supports components.
24
add_executable()
Add an executable target.
1 add_executable(tool
2 main.cpp
3 another_file.cpp
4 )
5 add_executable(my::tool ALIAS tool)
25
add_library()
Add a library target.
1 add_library(foo STATIC
2 foo1.cpp
3 foo2.cpp
4 )
5 add_library(my::foo ALIAS foo)
∙ Libraries can be STATIC, SHARED, MODULE, or INTERFACE.
∙ Default can be controlled with BUILD_SHARED_LIBS.
26
things to remember
∙ Always add namespaced aliases for libraries.
∙ Dont’t make libraries STATIC/SHARED unless they cannot be built
otherwise.
∙ Leave the control of BUILD_SHARED_LIBS to your clients!
27
usage requirements
usage requirements signature
1 target_<usage requirement>(<target>
2 <PRIVATE|PUBLIC|INTERFACE> <lib> ...
3 [<PRIVATE|PUBLIC|INTERFACE> <lib> ... ] ...]
4 )
PRIVATE Only used for this target.
PUBLIC Used for this target and all targets that link against it.
INTERFACE Only used for targets that link against this library.
29
target_link_libraries()
Set libraries as dependency.
1 target_link_libraries(foobar
2 PUBLIC my::foo
3 PRIVATE my::bar
4 )
30
things to remember
∙ Prefer to link against namespaced targets.
∙ Specify the dependencies are private or public.
∙ Avoid the link_libraries() command.
∙ Avoid the link_directories() command.
∙ No need to call add_dependencies().
31
target_include_directories()
Set include directories.
1 target_include_directories(foo
2 PUBLIC include
3 PRIVATE src
4 )
32
things to remember
∙ Avoid the include_directories() command.
33
target_compile_definitions()
Set compile definitions (preprocessor constants).
1 target_compile_definitions(foo
2 PRIVATE SRC_DIR=${Foo_SOURCE_DIR}
3 )
34
things to remember
∙ Avoid the add_definitions() command.
∙ Avoid adding definitions to CMAKE_<LANG>_FLAGS.
35
target_compile_options()
Set compile options/flags.
1 if(CMAKE_COMPILER_IS_GNUCXX)
2 target_compile_options(foo
3 PUBLIC -fno-elide-constructors
4 )
5 endif()
36
things to remember
∙ Wrap compiler specific options in an appropriate condition.
∙ Avoid the add_compile_options() command.
∙ Avoid adding options to CMAKE_<LANG>_FLAGS.
37
target_compile_features()
Set required compiler features.
1 target_compile_features(foo
2 PUBLIC
3 cxx_auto_type
4 cxx_range_for
5 PRIVATE
6 cxx_variadic_templates
7 )
∙ CMake will add required compile flags.
∙ Errors if the compiler is not supported.
38
things to remember
∙ Don’t add -std=c++11 to CMAKE_<LANG>_FLAGS.
∙ Don’t pass -std=c++11 to target_compile_options().
39
best practices
goal: no custom variables
variables are fragile
1 set(PROJECT foobar)
2 set(LIBRARIES foo)
3
4 if(WIN32)
5 list(APPEND LIBRARIES bar)
6 endif()
7
8 target_link_libraries(${Project}
9 ${LIBRARIES}
10 )
42
variables are fragile
Hard to diagnose:
bar links against foo! But only on Windows!
1 set(PROJECT foobar)
2 set(LIBRARIES foo)
3
4 if(WIN32)
5 list(APPEND LIBRARIES bar)
6 endif()
7
8 target_link_libraries(${Project}
9 ${LIBRARIES}
10 )
42
variables are fragile
This is better:
Without variables the code is more robust and easier to read.
1 target_link_libraries(foobar PRIVATE my::foo)
2
3 if(WIN32)
4 target_link_libraries(foobar PRIVATE my::bar)
5 endif()
43
don’t use file(glob)!
Not recommended:
1 file(GLOB sources ”*.cpp”)
2 add_library(mylib ${sources})
∙ file(GLOB) is useful in scripting mode.
∙ Don’t use it in configure mode!
44
list all sources explicitly
1 add_library(mylib
2 main.cpp
3 file1.cpp
4 file2.cpp
5 )
45
Explicit is better than implicit
46
goal: no custom functions
picking on myself
∙ I wrote many custom functions.
∙ Experience tought me: Most of the time it is a bad idea.
48
just don’t
1 magic_project(<name> ...)
49
why no custom functions?
∙ Remember what I said about the project() command?
∙ The signature of project() was extended recently.
50
just don’t
1 magic_add_library(<name>
2 SOURCES
3 <list of sources>
4 LIBRARIES
5 <list of libraries>
6 )
51
why no custom functions?
∙ How to add platform specific libraries?
∙ Harder to get started for contributors.
∙ Maintenance!
52
so you wrote a custom function ...
∙ Contribute it to CMake!
∙ If it is accepted, it is no longer a custom function.
∙ Otherwise, the reason for rejection should give you a hint.
∙ The maintainers are very helpful and experienced.
53
summary
summary
∙ CMake is widely used, fast, cross-platform.
∙ Build system generator plus platform abstraction.
∙ Manage build targets with usage requirements.
∙ Goal: No custom variables.
∙ Goal: No custom functions.
55
Thank you!
56

Más contenido relacionado

La actualidad más candente

Ninja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for BeginnersNinja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for BeginnersChang W. Doh
 
Yocto Project introduction
Yocto Project introductionYocto Project introduction
Yocto Project introductionYi-Hsiu Hsu
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Adrian Huang
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageShih-Hsiang Lin
 
Qt5 (minimal) on beaglebone, with Yocto
Qt5 (minimal) on beaglebone, with YoctoQt5 (minimal) on beaglebone, with Yocto
Qt5 (minimal) on beaglebone, with YoctoPrabindh Sundareson
 
ACPI Debugging from Linux Kernel
ACPI Debugging from Linux KernelACPI Debugging from Linux Kernel
ACPI Debugging from Linux KernelSUSE Labs Taipei
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)shimosawa
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingTushar B Kute
 
Conan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for DevelopersConan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for DevelopersUilian Ries
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking ExplainedThomas Graf
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicJoseph Lu
 
Introduction To Makefile
Introduction To MakefileIntroduction To Makefile
Introduction To MakefileWaqqas Jabbar
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in LinuxAdrian Huang
 
BPF / XDP 8월 세미나 KossLab
BPF / XDP 8월 세미나 KossLabBPF / XDP 8월 세미나 KossLab
BPF / XDP 8월 세미나 KossLabTaeung Song
 
Performance Analysis Tools for Linux Kernel
Performance Analysis Tools for Linux KernelPerformance Analysis Tools for Linux Kernel
Performance Analysis Tools for Linux Kernellcplcp1
 

La actualidad más candente (20)

Ninja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for BeginnersNinja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for Beginners
 
Yocto Project introduction
Yocto Project introductionYocto Project introduction
Yocto Project introduction
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
 
cmake.pdf
cmake.pdfcmake.pdf
cmake.pdf
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming Language
 
Qt5 (minimal) on beaglebone, with Yocto
Qt5 (minimal) on beaglebone, with YoctoQt5 (minimal) on beaglebone, with Yocto
Qt5 (minimal) on beaglebone, with Yocto
 
ACPI Debugging from Linux Kernel
ACPI Debugging from Linux KernelACPI Debugging from Linux Kernel
ACPI Debugging from Linux Kernel
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Conan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for DevelopersConan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for Developers
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking Explained
 
Toolchain
ToolchainToolchain
Toolchain
 
Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panic
 
Introduction To Makefile
Introduction To MakefileIntroduction To Makefile
Introduction To Makefile
 
BusyBox for Embedded Linux
BusyBox for Embedded LinuxBusyBox for Embedded Linux
BusyBox for Embedded Linux
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in Linux
 
Spi drivers
Spi driversSpi drivers
Spi drivers
 
Introduction to Modern U-Boot
Introduction to Modern U-BootIntroduction to Modern U-Boot
Introduction to Modern U-Boot
 
BPF / XDP 8월 세미나 KossLab
BPF / XDP 8월 세미나 KossLabBPF / XDP 8월 세미나 KossLab
BPF / XDP 8월 세미나 KossLab
 
Performance Analysis Tools for Linux Kernel
Performance Analysis Tools for Linux KernelPerformance Analysis Tools for Linux Kernel
Performance Analysis Tools for Linux Kernel
 

Similar a CMake - Introduction and best practices

Red Teaming macOS Environments with Hermes the Swift Messenger
Red Teaming macOS Environments with Hermes the Swift MessengerRed Teaming macOS Environments with Hermes the Swift Messenger
Red Teaming macOS Environments with Hermes the Swift MessengerJustin Bui
 
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfThninh2
 
Makefile
MakefileMakefile
MakefileIonela
 
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...Eric Smalling
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014biicode
 
Yocto: Training in English
Yocto: Training in EnglishYocto: Training in English
Yocto: Training in EnglishOtavio Salvador
 
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)Alexandre Gouaillard
 
Makefile for python projects
Makefile for python projectsMakefile for python projects
Makefile for python projectsMpho Mphego
 
Training: Day Two - Eclipse, Git, Maven
Training: Day Two - Eclipse, Git, MavenTraining: Day Two - Eclipse, Git, Maven
Training: Day Two - Eclipse, Git, MavenArtur Ventura
 
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily JobLuca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Joblinuxlab_conf
 
3.1.d manual bash script guide lsstv 2.0r11
3.1.d manual bash script guide lsstv 2.0r113.1.d manual bash script guide lsstv 2.0r11
3.1.d manual bash script guide lsstv 2.0r11Acácio Oliveira
 
C make tutorial
C make tutorialC make tutorial
C make tutorialzeng724
 
Server(less) Swift at SwiftCloudWorkshop 3
Server(less) Swift at SwiftCloudWorkshop 3Server(less) Swift at SwiftCloudWorkshop 3
Server(less) Swift at SwiftCloudWorkshop 3kognate
 
Composer JSON kills make files
Composer JSON kills make filesComposer JSON kills make files
Composer JSON kills make filesropsu
 
[Codelab 2017] Docker 기초 및 활용 방안
[Codelab 2017] Docker 기초 및 활용 방안[Codelab 2017] Docker 기초 및 활용 방안
[Codelab 2017] Docker 기초 및 활용 방안양재동 코드랩
 

Similar a CMake - Introduction and best practices (20)

Red Teaming macOS Environments with Hermes the Swift Messenger
Red Teaming macOS Environments with Hermes the Swift MessengerRed Teaming macOS Environments with Hermes the Swift Messenger
Red Teaming macOS Environments with Hermes the Swift Messenger
 
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdf
 
Makefile
MakefileMakefile
Makefile
 
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
 
Install guide
Install guideInstall guide
Install guide
 
Install guide
Install guideInstall guide
Install guide
 
Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014Dependencies Managers in C/C++. Using stdcpp 2014
Dependencies Managers in C/C++. Using stdcpp 2014
 
Yocto: Training in English
Yocto: Training in EnglishYocto: Training in English
Yocto: Training in English
 
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
 
Makefile for python projects
Makefile for python projectsMakefile for python projects
Makefile for python projects
 
Training: Day Two - Eclipse, Git, Maven
Training: Day Two - Eclipse, Git, MavenTraining: Day Two - Eclipse, Git, Maven
Training: Day Two - Eclipse, Git, Maven
 
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily JobLuca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
Luca Ceresoli - Buildroot vs Yocto: Differences for Your Daily Job
 
HPC_MPI_CICD.pptx
HPC_MPI_CICD.pptxHPC_MPI_CICD.pptx
HPC_MPI_CICD.pptx
 
3.1.d manual bash script guide lsstv 2.0r11
3.1.d manual bash script guide lsstv 2.0r113.1.d manual bash script guide lsstv 2.0r11
3.1.d manual bash script guide lsstv 2.0r11
 
CMake_Tutorial.pdf
CMake_Tutorial.pdfCMake_Tutorial.pdf
CMake_Tutorial.pdf
 
C make tutorial
C make tutorialC make tutorial
C make tutorial
 
C make tutorial
C make tutorialC make tutorial
C make tutorial
 
Server(less) Swift at SwiftCloudWorkshop 3
Server(less) Swift at SwiftCloudWorkshop 3Server(less) Swift at SwiftCloudWorkshop 3
Server(less) Swift at SwiftCloudWorkshop 3
 
Composer JSON kills make files
Composer JSON kills make filesComposer JSON kills make files
Composer JSON kills make files
 
[Codelab 2017] Docker 기초 및 활용 방안
[Codelab 2017] Docker 기초 및 활용 방안[Codelab 2017] Docker 기초 및 활용 방안
[Codelab 2017] Docker 기초 및 활용 방안
 

Último

Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 

Último (20)

Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 

CMake - Introduction and best practices

  • 1. cmake Introduction and best practices Daniel Pfeifer May 21, 2015 daniel@pfeifer-mail.de
  • 2. interest over time 2005 2007 2009 2011 2013 2015 autoconf automake bjam cmake scons 0https://www.google.com/trends/explore#q=autoconf,automake,bjam,cmake,scons 1
  • 4. according to its website Welcome to CMake, the cross-platform, open-source build system. CMake is a family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files. CMake generates native makefiles and workspaces that can be used in the compiler environment of your choice.1 1http://www.cmake.org/ 3
  • 5. cmake’s position among build systems standalone Make, NMake, SCons, Jom, BJam, Ninja integrated Visual Studio, Xcode, Eclipse generators Autotools, CMake, GYP 4
  • 6. cmake is simple > ls zlib-1.2.8 amiga/ as400/ contrib/ doc/ examples/ msdos/ nintendods/ old/ qnx/ test/ watcom/ win32/ adler32.c ChangeLog CMakeLists.txt compress.c configure crc32.c crc32.h deflate.c deflate.h FAQ gzclose.c gzguts.h gzlib.c gzread.c gzwrite.c INDEX infback.c inffast.c inffast.h inffixed.h inflate.c inflate.h inftrees.c inftrees.h make_vms.com Makefile Makefile.in README treebuild.xml trees.c trees.h uncompr.c zconf.h zconf.h.in zlib.3 zlib.3.pdf zlib.h zlib.map zlib.pc.in zlib2ansi zutil.c zutil.h 5
  • 7. reduce your maintenance ... You write a single configuration that CMake understands. CMake takes care that it works on all compilers and platforms. 6
  • 8. ... unless you break it! Don’t make any assumption about the platform or compiler! 7
  • 9. cmake is fast! I was quite surprised with the speed of building Quantum GIS codebase in comparison to Autotools.2 Task CMake Autotools Configure 0:08 0:61 Make 12:15 21:16 Install 0:20 0:36 Total 12:43 22:43 2http://blog.qgis.org/node/16.html 8
  • 10. the cmake cache ∙ Compiler and platform test results are cached in CMakeCache.txt. ∙ You can modify this file and re-run CMake. ∙ CMake also provides graphical cache editors. 9
  • 12. cmake’s position on the time axis past CMake generates build systems. today CMake is integrated into IDEs. future IDEs run CMake as a background process.3 3http://public.kitware.com/pipermail/cmake-developers/2015-April/025012.html 11
  • 14. cmake as a build system generator 1 > cmake [<options>] <path> ∙ If the specified path contains a CMakeCache.txt, it is treated as a build directory where the build system is reconfigured and regenerated. ∙ Otherwise the specified path is treated as the source directory and the current working directory is treated as build directory. 1 > mkdir build 2 > cd build 3 > time cmake .. 4 > time cmake . 5 > time make 13
  • 15. cmake as a build system generator Issues: ∙ The commands mkdir and time are not portable. ∙ With cmake . you may accidentally perform an in-source configure. ∙ Who said that make is the right build command? 1 > mkdir build 2 > cd build 3 > time cmake .. 4 > time cmake . 5 > time make mylibrary 14
  • 16. cmake’s command-line tool mode 1 > cmake -E <command> [<options>...] Run cmake -E help for a summary of commands. 1 > cmake -E make_directory build 2 > cmake -E chdir cmake -E time cmake .. 3 > cmake -E time cmake build 15
  • 17. use cmake to run the build system 1 > cmake --build build 2 --target mylibrary 3 --config Release 4 --clean-first Just don’t: 1 > ‘grep ’CMAKE_MAKE_PROGRAM:’ CMakeCache.txt 2 | awk -F= ’{print $2}’‘ mylibrary 16
  • 18. cmake as a scripting language 1 > cat hello_world.cmake 2 foreach(greet Hello Goodbye) 3 message(”${greet}, World!”) 4 endforeach() 5 6 > cmake -P hello_world.cmake 7 Hello, World! 8 Goodbye, World! No configure or generate step is performed and the cache is not modified. 17
  • 19. Resist the bash/perl/python temptation! 18
  • 21. cmake_minimum_required() Sets the mimimum required version of CMake. 1 cmake_minimum_required(VERSION 3.2 FATAL_ERROR) ∙ Prefer the latest version of CMake. ∙ Please don’t set 2.8 as the minimum. ∙ If you use 2.6 or 2.4, God kills a kitten. 20
  • 22. project() Set the name and version; enable languages. 1 project(<name> VERSION <version> LANGUAGES CXX) ∙ CMake sets several variables based on project(). ∙ Call to project() must be direct, not through a function/macro/include. ∙ CMake will add a call to project() if not found on the top level. 21
  • 23. add_subdirectory() Embed projects as sub-projects. 1 add_subdirectory(<sourcedir> [<binarydir>]) ∙ CMake creates a Makefile/Solution for each subproject. ∙ The subproject does not have to reside in a subfolder. 22
  • 24. hint! Make sure that all your projects can be built both standalone and as a subproject of another project: ∙ Don’t assume that your project root is the build root. ∙ Don’t modify global compile/link flags. ∙ Don’t make any global changes! 23
  • 25. find_package() Finds preinstalled dependencies 1 find_package(Qt5 REQUIRED COMPONENTS Widgets) ∙ Can set some variables and define imported targets. ∙ Supports components. 24
  • 26. add_executable() Add an executable target. 1 add_executable(tool 2 main.cpp 3 another_file.cpp 4 ) 5 add_executable(my::tool ALIAS tool) 25
  • 27. add_library() Add a library target. 1 add_library(foo STATIC 2 foo1.cpp 3 foo2.cpp 4 ) 5 add_library(my::foo ALIAS foo) ∙ Libraries can be STATIC, SHARED, MODULE, or INTERFACE. ∙ Default can be controlled with BUILD_SHARED_LIBS. 26
  • 28. things to remember ∙ Always add namespaced aliases for libraries. ∙ Dont’t make libraries STATIC/SHARED unless they cannot be built otherwise. ∙ Leave the control of BUILD_SHARED_LIBS to your clients! 27
  • 30. usage requirements signature 1 target_<usage requirement>(<target> 2 <PRIVATE|PUBLIC|INTERFACE> <lib> ... 3 [<PRIVATE|PUBLIC|INTERFACE> <lib> ... ] ...] 4 ) PRIVATE Only used for this target. PUBLIC Used for this target and all targets that link against it. INTERFACE Only used for targets that link against this library. 29
  • 31. target_link_libraries() Set libraries as dependency. 1 target_link_libraries(foobar 2 PUBLIC my::foo 3 PRIVATE my::bar 4 ) 30
  • 32. things to remember ∙ Prefer to link against namespaced targets. ∙ Specify the dependencies are private or public. ∙ Avoid the link_libraries() command. ∙ Avoid the link_directories() command. ∙ No need to call add_dependencies(). 31
  • 33. target_include_directories() Set include directories. 1 target_include_directories(foo 2 PUBLIC include 3 PRIVATE src 4 ) 32
  • 34. things to remember ∙ Avoid the include_directories() command. 33
  • 35. target_compile_definitions() Set compile definitions (preprocessor constants). 1 target_compile_definitions(foo 2 PRIVATE SRC_DIR=${Foo_SOURCE_DIR} 3 ) 34
  • 36. things to remember ∙ Avoid the add_definitions() command. ∙ Avoid adding definitions to CMAKE_<LANG>_FLAGS. 35
  • 37. target_compile_options() Set compile options/flags. 1 if(CMAKE_COMPILER_IS_GNUCXX) 2 target_compile_options(foo 3 PUBLIC -fno-elide-constructors 4 ) 5 endif() 36
  • 38. things to remember ∙ Wrap compiler specific options in an appropriate condition. ∙ Avoid the add_compile_options() command. ∙ Avoid adding options to CMAKE_<LANG>_FLAGS. 37
  • 39. target_compile_features() Set required compiler features. 1 target_compile_features(foo 2 PUBLIC 3 cxx_auto_type 4 cxx_range_for 5 PRIVATE 6 cxx_variadic_templates 7 ) ∙ CMake will add required compile flags. ∙ Errors if the compiler is not supported. 38
  • 40. things to remember ∙ Don’t add -std=c++11 to CMAKE_<LANG>_FLAGS. ∙ Don’t pass -std=c++11 to target_compile_options(). 39
  • 42. goal: no custom variables
  • 43. variables are fragile 1 set(PROJECT foobar) 2 set(LIBRARIES foo) 3 4 if(WIN32) 5 list(APPEND LIBRARIES bar) 6 endif() 7 8 target_link_libraries(${Project} 9 ${LIBRARIES} 10 ) 42
  • 44. variables are fragile Hard to diagnose: bar links against foo! But only on Windows! 1 set(PROJECT foobar) 2 set(LIBRARIES foo) 3 4 if(WIN32) 5 list(APPEND LIBRARIES bar) 6 endif() 7 8 target_link_libraries(${Project} 9 ${LIBRARIES} 10 ) 42
  • 45. variables are fragile This is better: Without variables the code is more robust and easier to read. 1 target_link_libraries(foobar PRIVATE my::foo) 2 3 if(WIN32) 4 target_link_libraries(foobar PRIVATE my::bar) 5 endif() 43
  • 46. don’t use file(glob)! Not recommended: 1 file(GLOB sources ”*.cpp”) 2 add_library(mylib ${sources}) ∙ file(GLOB) is useful in scripting mode. ∙ Don’t use it in configure mode! 44
  • 47. list all sources explicitly 1 add_library(mylib 2 main.cpp 3 file1.cpp 4 file2.cpp 5 ) 45
  • 48. Explicit is better than implicit 46
  • 49. goal: no custom functions
  • 50. picking on myself ∙ I wrote many custom functions. ∙ Experience tought me: Most of the time it is a bad idea. 48
  • 52. why no custom functions? ∙ Remember what I said about the project() command? ∙ The signature of project() was extended recently. 50
  • 53. just don’t 1 magic_add_library(<name> 2 SOURCES 3 <list of sources> 4 LIBRARIES 5 <list of libraries> 6 ) 51
  • 54. why no custom functions? ∙ How to add platform specific libraries? ∙ Harder to get started for contributors. ∙ Maintenance! 52
  • 55. so you wrote a custom function ... ∙ Contribute it to CMake! ∙ If it is accepted, it is no longer a custom function. ∙ Otherwise, the reason for rejection should give you a hint. ∙ The maintainers are very helpful and experienced. 53
  • 57. summary ∙ CMake is widely used, fast, cross-platform. ∙ Build system generator plus platform abstraction. ∙ Manage build targets with usage requirements. ∙ Goal: No custom variables. ∙ Goal: No custom functions. 55