CSci 6971: Image Registration Lecture 11b: CMake Tutorial February 20, 2004
description
Transcript of CSci 6971: Image Registration Lecture 11b: CMake Tutorial February 20, 2004
Image Registration Lecture 11b
CSci 6971: CSci 6971: Image RegistrationImage RegistrationLecture 11b: CMake TutorialLecture 11b: CMake Tutorial
February 20, 2004 February 20, 2004
Brad KingBrad King
Kitware, Inc.Kitware, Inc.
Image Registration Lecture 11b
Build-System GeneratorBuild-System Generator
• Provides single-sourcing for build systemsProvides single-sourcing for build systems
• Knowledge of many platforms and toolsKnowledge of many platforms and tools
• Users configure builds through a GUIUsers configure builds through a GUI
CMakeLists.txt CMake Native Build System
Native Build ToolsExecutables
Libraries
Image Registration Lecture 11b
Source and Build TreesSource and Build Trees• The The Source TreeSource Tree contains: contains:
– CMake input files (CMakeLists.txt)CMake input files (CMakeLists.txt)– Program source files (hello.cxx)Program source files (hello.cxx)
• The The Binary TreeBinary Tree (build tree) contains: (build tree) contains:– Native build system files (hello.dsp)Native build system files (hello.dsp)– Program libraries and executables (hello.exe)Program libraries and executables (hello.exe)
• Source and Binary trees may be:Source and Binary trees may be:– In the same directory (In the same directory (in-sourcein-source build) build)– In different directories (In different directories (out-of-sourceout-of-source build) build)
Image Registration Lecture 11b
The CMake CacheThe CMake Cache
• Represents build configurationRepresents build configuration
• Populated by CMake codePopulated by CMake code
• Stored in CMakeCache.txt at top of buildStored in CMakeCache.txt at top of build
• Entries have a type to help the GUIEntries have a type to help the GUI
• Holds global information for CMake codeHolds global information for CMake code
• Updated by CMake configuration phaseUpdated by CMake configuration phase
Image Registration Lecture 11b
Command Line UsageCommand Line Usage
• Can be used from scriptsCan be used from scripts
• Set current-working-directory to binary treeSet current-working-directory to binary tree
• Pass path to source tree as first argumentPass path to source tree as first argument
• Use -G to select build system generatorUse -G to select build system generator
• Use -D to set cache variablesUse -D to set cache variables$ cd Foo-msvc-6
$ cmake ../Foo –G“Visual Studio 6” –DBAR:BOOL=1
Image Registration Lecture 11b
GUI UsageGUI Usage
CMakeSetupCMakeSetup ccmakeccmake
• Edit cache entries to configure the buildEdit cache entries to configure the build• Use configure button after a changeUse configure button after a change• Use OK (generate) button when finishedUse OK (generate) button when finished
Image Registration Lecture 11b
Source Tree StructureSource Tree Structure
• Every directory has a CMakeLists.txt fileEvery directory has a CMakeLists.txt file
• Subdirectories specified by SUBDIRSSubdirectories specified by SUBDIRS
• Directories depend only on parentsDirectories depend only on parents
• A subset of commands are inheritedA subset of commands are inherited
CMakeLists.txtSUBDIRS(Dir1 Dir2)
Dir1/CMakeLists.txt
Dir2/CMakeLists.txt
Image Registration Lecture 11b
Writing CMakeLists.txt FilesWriting CMakeLists.txt Files
• CMake language evolved while in useCMake language evolved while in use• Scripting language with simple syntaxScripting language with simple syntax
– Comments Comments – CommandsCommands– ListsLists– VariablesVariables– Control structuresControl structures
• Processed during CMake configure phaseProcessed during CMake configure phase
# Comment ends at a newline
COMMAND(arg1 arg2 ...)
A;B;C # Semicolon-separated
IF(CONDITION)
${VAR}
Image Registration Lecture 11b
CommandsCommands
• Simple syntax: Simple syntax:
• Each command must start on its own lineEach command must start on its own line
• Variable references are replaced by valuesVariable references are replaced by values
• Lists in unquoted arguments are expandedLists in unquoted arguments are expanded
• Argument meanings defined by commandArgument meanings defined by command
• Both positional and keyword arguments Both positional and keyword arguments usedused
COMMAND(ARG “ARG WITH SPACES”
${A_LIST} “${A_STRING}”)
TARGET_LINK_LIBRARIES(myTarget lib1 lib2)
FIND_LIBRARY(MY_LIB NAMES my1 my2
PATHS /foo /bar)
Image Registration Lecture 11b
VariablesVariables
• Named by C-style identifierNamed by C-style identifier
• Value is always a stringValue is always a string
• No associated typeNo associated type
• Initialized by CMake cache entriesInitialized by CMake cache entries
• Assigned through commands like SETAssigned through commands like SET
• Referenced by ${VAR} (only one level)Referenced by ${VAR} (only one level)SET(A_LIST ${A_LIST} foo)
SET(A_STRING “${A_STRING} bar”)
Image Registration Lecture 11b
Control StructuresControl Structures
• IFIF
• FOREACHFOREACH
• MACROMACRO
IF(CONDITION) MESSAGE(“Yes”)ELSE(CONDITION) MESSAGE(“No”)ENDIF(CONDITION)
FOREACH(c A B C) MESSAGE(“${c}: ${${c}}”)ENDFOREACH(c)
MACRO(MY_MACRO arg1 arg2) SET(${arg1} “${${arg2}}”)ENDMACRO(MY_MACRO)MY_MACRO(A B)
Image Registration Lecture 11b
A Typical ProjectA Typical Project
PROJECT(FOO)
SUBDIRS(Foo Bar Executable)
CMakeLists.txtCMakeLists.txt
ADD_LIBRARY(foo foo1.cxx foo2.cxx)
Foo/CMakeLists.txtFoo/CMakeLists.txt
ADD_EXECUTABLE(zot zot1.cxx zot2.cxx)
TARGET_LINK_LIBRARIES(zot bar)
Executable/CMakeLists.txtExecutable/CMakeLists.txt
ADD_LIBRARY(bar bar1.cxx bar2.cxx)
TARGET_LINK_LIBRARIES(bar foo)
Bar/CMakeLists.txtBar/CMakeLists.txt
Image Registration Lecture 11b
Developer DocumentationDeveloper Documentation• Command-line documentation:Command-line documentation:
– Run “Run “cmake --helpcmake --help” for summary” for summary– Run “Run “cmake --help cmake --help COMMANDCOMMAND” for detailed help ” for detailed help
with a specific listfile commandwith a specific listfile command– Try “Try “cmake --help IFcmake --help IF””
• Online documentation:Online documentation:– http://www.cmake.org/HTML/Documentation.htmlhttp://www.cmake.org/HTML/Documentation.html
• Mastering CMakeMastering CMake– Published by Kitware, Inc.Published by Kitware, Inc.– ISBN 1-930934-09-2ISBN 1-930934-09-2
Image Registration Lecture 11b
Editing CMake CodeEditing CMake Code
• EMACS mode for CMakeEMACS mode for CMake– cmake-mode.elcmake-mode.el located in CMake/Docs directory located in CMake/Docs directory
– Provides highlighting and indentationProvides highlighting and indentation
– Use this code in your .emacs file:Use this code in your .emacs file:
• VIM mode is also availableVIM mode is also available
(setq load-path (cons “/path/to/cmake-mode” load-path))(require 'cmake-mode)(setq auto-mode-alist (append '(("CMakeLists.txt" . cmake-mode) ("\\.cmake$" . cmake-mode)) auto-mode-alist))
Image Registration Lecture 11b
Building ITK & VXL TogetherBuilding ITK & VXL Together
• Build VXL using any configurationBuild VXL using any configuration
• Run CMakeSetup to build ITKRun CMakeSetup to build ITK
• Click “Configure”Click “Configure”
• Turn on “Show Advanced Values”Turn on “Show Advanced Values”
• Set ITK_USE_SYSTEM_VXL to ONSet ITK_USE_SYSTEM_VXL to ON
• Click “Configure”Click “Configure”
• Set VXL_DIR to point at VXL build treeSet VXL_DIR to point at VXL build tree
Image Registration Lecture 11b
Using ITK & VXL TogetherUsing ITK & VXL Together
• Import ITK using code like this:Import ITK using code like this:FIND_PACKAGE(ITK)
IF(ITK_FOUND)
INCLUDE(${ITK_USE_FILE})
IF(NOT ITK_USE_SYSTEM_VXL)
MESSAGE(“Need an ITK with ITK_USE_SYSTEM_VXL ON.”)
ENDIF(NOT ITK_USE_SYSTEM_VXL)
ELSE(ITK_FOUND)
MESSAGE(FATAL_ERROR “Set ITK_DIR”)
ENDIF(ITK_FOUND)
Image Registration Lecture 11b
Using ITK & VXL TogetherUsing ITK & VXL Together
• Create your application like this:Create your application like this:
• C++ code is straightforward:C++ code is straightforward:
ADD_EXECUTABLE(myapp myapp.cxx)
TARGET_LINK_LIBRARIES(myapp ITKIO vil)
#include “itkImage.h”
#include <vil/vil_transform.h>
// ...