30
Automating Software Build Process using CMake Armin Sobhani [email protected] SHARCNET University of Ontario Institute of Technology (UOIT) September 14, 2016 • Makefiles • Kdevelop files •… • MSVC files • Borland files •… • Makefiles • Xcode files •… CMake Project description (.txt)

Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani [email protected] SHARCNET University of Ontario

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Automating

Software Build

Process using

CMake

Armin Sobhani

[email protected]

SHARCNET

University of Ontario Institute

of Technology (UOIT)

September 14, 2016

• Makefiles

• Kdevelop files

• …

• MSVC files

• Borland files

• …

• Makefiles

• Xcode files

• …

CMakeProject

description

(.txt)

Page 2: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Outline

• CMake Overview

– What is CMake

– Why CMake

– CMake history

– Features

– Concepts

• Using CMake

• Developing with CMake

Page 3: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

What is CMake?

• CMake is the cross-platform, open-source build

system that lets you use the native development tools

• It’s a build system generator

• It takes plain text files as input that describe your

project and produces project files or make files for

use with a wide variety of native development tools.

Automating Software Build Process using CMake 3

Page 4: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Why CMake?

• It’s easy and works well

– Certainly way simpler than Makefiles!

• It’s cross-platform

• It’s popular (scientific and commercial)

• It can build a directory tree outside of the

source tree

Automating Software Build Process using CMake 4

Page 5: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

CMake History

• Built for the Insight Segmentation and

Registration Toolkit (ITK) (http://www.itk.org)

• Funded by National Library of Medicine

(NLM): part of the Visible Human Project

• Release 1.0 branch created in late 2001

Automating Software Build Process using CMake 5

Page 6: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

CMake Features

• Native tools– Unix Makefiles

– Borland Makefiles

– MSYS Makefiles

– MinGW Makefiles

– NMake Makefiles

– Watcom WMake

– Ninja (Google)

– MSBuild

• IDE Support– Code::Blocks

– CodeLite

– Eclipse CDT

– KDevelop

– Visual Studio

– Xcode

– Kate (KDE Text Editor)

– Sublime Text 2

Automating Software Build Process using CMake 6

Page 7: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

CMake Features

OS Support

Linux

OS X

Windows

Solaris / SunOS

HPUX

IRIX

Platform inspections commands can:

• Search for

– Programs

– Libraries and header files

– Packages

• Determine hardware

specifics

– Byte order

– Number of bits

Automating Software Build Process using CMake 7

Page 8: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Using CMake

Automating Software Build Process using CMake 8

Page 9: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Installing CMake

• Windows

– Download: https://cmake.org/download/

• OS X

– Download

– MacPorts: port install cmake

• Linux

– Download

– Use package manager

Automating Software Build Process using CMake 9

Page 10: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

CMake on SHARCNET’s Systems

• https://www.sharcnet.ca/my/software/show/122

• https://www.sharcnet.ca/help/index.php/CMAKE

• Default (CentOS 6.0): 2.8.12.2

• Using modules: 3.3.2, 3.4.3

Automating Software Build Process using CMake 10

Page 11: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Developing Software with CMake

Automating Software Build Process using CMake 11

Page 12: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

CMake Workflow

2

Run cmake/ccmake to

generate or configure

native build system

files

3

Open project files from the

build tree and use the native

build tools

1

Edit files in the source

tree

Automating Software Build Process using CMake 12

Page 13: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Editing CMakeLists.txt

• Editor support and syntax highlighting:

– https://cmake.org/Wiki/CMake_Editors_Support

• Emacs

• vim

• Notepad++

• Sublime Text

• ...

Automating Software Build Process using CMake 13

Page 14: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

CMake Concepts

• Source tree

• Binary tree = Build tree

• Install tree

• Generator

• Cache Entry or Cache Variable

• Target (library, executable, custom)

Automating Software Build Process using CMake 14

Page 15: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Build Configurations

Make file generators

• CMAKE_BUILD_TYPE

• Known values

– Debug

– Release

– MinSizeRel

– RelWithDebInfo

Multi-config generators

• Use multiple build trees

Automating Software Build Process using CMake 16

Page 16: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Our First CMake Project

• Only 2 lines of code!

Automating Software Build Process using CMake 17

Page 17: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

CMake Syntax 101

Automating Software Build Process using CMake 18

Page 18: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Basic CMake Syntax

• CMake language components

– Commands (with arguments)

– Variables

– All values are strings

– Comments (start with ‘#’)

Automating Software Build Process using CMake 19

Page 19: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Basic CMake Syntax – Commands

• Case insensitive

• Arguments are case sensitive and space

separated

• Quoted argument is always one value

• List of valid commands:– https://cmake.org/cmake/help/v3.0/manual/cmake-commands.7.html

– cmake --help-command-list

Automating Software Build Process using CMake 20

Page 20: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Basic CMake Syntax – Commands

• Multiple argument to the SET command are

combined with semicolon:

set(VAR a b c) # VAR=“a;b;c”

set(VAR “a;b;c”) # VAR=“a;b;c”

Automating Software Build Process using CMake 21

Page 21: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Basic CMake Syntax – Commands

• Examples:

set(name myexe)

set(srcs src1.c src2.c s3.c)

set(srcs “src1.c;src2.c;s3.c”)

add_executable(“${name}” ${srcs})

add_executable(“myexe” src1.c src2.c s3.c)

add_executable(“myexe” src1.c;src2.c;s3.c)

Automating Software Build Process using CMake 22

Page 22: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Basic CMake Syntax – Variables

• Variable names are case sensitive:set(VAR value)

set(var value)

• Use alpha-numerics and underscores

• Variables are strings:

set(VAR “a b c”) # VAR holds one thing

set(var a;b;c) # VAR holds three things

set(var a b c) # VAR holds three things

Automating Software Build Process using CMake 23

Page 23: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Basic CMake Syntax – Variables

• Use the list or foreach commands to

access list elements

• Special syntax for setting environment

variables:

set(ENV{ROOT_DIR} “/home/username/root”)

Automating Software Build Process using CMake 24

Page 24: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Basic CMake Syntax – Variables

• Variable (de-) referencing: ${VAR}

set(my_dir “${CMAKE_SOURCE_DIR}/my_dir”)

message(“my_dir=‘${my_dir}’”)

• For environment variables: $ENV{VAR}

set(my_path “$ENV{PATH}”)

message(“my_path=‘${my_path}’”)

Automating Software Build Process using CMake 25

Page 25: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Basic CMake Syntax – Variables

• Escaping – \ is the escape character used in

string literals:

set(VAR “a\\b\\c and \”embedded quotes\””)

message(${VAR}) # “a\b\c and ”embedded quotes””

set(VAR a b c)

message(${VAR}) # “abc”

message(“${VAR}”) # “a;b;c”

set(VAR)

message(“${VAR}”) # “”

message(${VAR}) # error!

Automating Software Build Process using CMake 26

Page 26: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Flow Control (if)

if(VAR)

code

endif(VAR) # arguments to endif must match if

if(NOT VAR)

if(VAR AND VAR2)

if(VAR OR VAR2)

if(VAR MATCHES regular_expression)

if(COMMAND command)

if(EXISTS file)

if(VAR LESS VAR2)

cmake --help-command if for more details

Automating Software Build Process using CMake 29

Page 27: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Flow Control (if) – FALSE Values

• “” (the empty string)

• OFF

• 0 (the number zero)

• NO

• FALSE

• N

• NOTFOUND exactly or ends in “-NOTFOUND”

• IGNORE

Automating Software Build Process using CMake 30

Page 28: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Flow Control (FOREACH, WHILE)

foreach(F a b c)

message(${F})

endforeach(F)

while(VAR)

message(${VAR})

set(VAR FALSE)

endwhile(VAR)

Automating Software Build Process using CMake 31

Page 29: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

Macros and Functions

macro(MYMACRO arg1 arg2)

command1(…)

command2(…)

endmacro(MYMACRO)

# cmake 2.6 and later

# dynamically scoped, any variables set are local to func

function(myfunction arg1 arg2)

command1(…)

command2(…)

endfunction(myfunction)

Automating Software Build Process using CMake 32

Page 30: Automating Software Build Process using Makefiles Kdevelop files · Automating Software Build Process using CMake Armin Sobhani asobhani@sharcnet.ca SHARCNET University of Ontario

CMake Commands

Common commands

cmake_minimum_required(VERSION 2.6)

project(projectname CXX)

set(libname sumdumlib)

add_library(${libname} src1.cpp src2.cpp …)

add_executable(exename src1.cpp src2.cpp …)

target_line_libraries(exename ${libname} …)

add_subdirectory(tests)

Automating Software Build Process using CMake 33