stephen kelly c++ user group, berlin july 2014 · the what, why and how of modern cmake stephen...

65
The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Upload: others

Post on 07-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

The What, Why and How of Modern CMake

Stephen Kelly

C++ User Group, Berlin

July 2014

Page 2: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Who and When

● Using CMake since KDE 4.0 (December 2007)

Page 3: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Who and When

● Using CMake since KDE 4.0 (December 2007)● Contributor since CMake 2.8.7 (Nov 2011)

Page 4: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Who and When

● Using CMake since KDE 4.0 (December 2007)● Contributor since CMake 2.8.7 (Nov 2011)● Feature complete in CMake 3.0 (June 2014)

Page 5: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Who and When

● Using CMake since KDE 4.0 (December 2007)● Contributor since CMake 2.8.7 (Nov 2011)● (Almost) Feature complete in CMake 3.0 (June 2014)

Page 6: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 7: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 8: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 9: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 10: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 11: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 12: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 13: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 14: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 15: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 16: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 17: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 18: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

include_directories("/opt/foo/bar") compile_definitions(-DUSING_BAR=1)

Page 19: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Buildsystem Developers

Page 20: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

Page 21: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

● Describes a useful model of buildsystem● Has design/direction ● Straightforward, clean syntax

Page 22: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

● Describes a useful model of buildsystem● Has design/direction ● Straightforward, clean syntax● High usability, maximum speed● High level of abstraction● Declarative

Page 23: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

add_library(foo foo.cpp)

add_executable(myexe main.cpp)target_link_libraries(myexe foo)

Page 24: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

add_library(foo foo.cpp)target_compile_definitions(foo PRIVATE -DBUILDING_FOO=1)target_include_directories(foo PRIVATE "${srcDir}/bar")

Page 25: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

add_library(foo foo.cpp)target_compile_definitions(foo INTERFACE -DUSING_FOO=1)target_include_directories(foo INTERFACE "/opt/foo/bar")

Page 26: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

add_library(foo foo.cpp)target_compile_definitions(foo INTERFACE -DUSING_FOO=1)target_include_directories(foo INTERFACE "/opt/foo/bar")

add_executable(myexe main.cpp)target_link_libraries(myexe foo)

Page 27: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

Describe target properties

Page 28: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

Describe target dependencies

Page 29: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

Transitive usage requirements

Page 30: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

Exporting for downstream use

Page 31: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

install(EXPORT fooTargets NAMESPACE Bar:: DESTINATION lib/cmake)

Page 32: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

install(EXPORT fooTargets NAMESPACE Bar:: DESTINATION lib/cmake)

# Generates:

add_library(Bar::foo IMPORTED)set_target_properties(Bar::foo INTERFACE_COMPILE_DEFINITIONS USING_FOO=1)

Page 33: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

find_package(Foo REQUIRED)

add_executable(myexe main.cpp)target_link_libraries(myexe Bar::foo)

Page 34: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

What is Modern CMake?

Upstream provides 'CMake config packages'

No more FindFoo.cmake!

Page 35: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014
Page 36: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Why is this better?

# Directory-scoped. Bad!include_directories(${Foo_INCLUDES})

add_executable(myexe1 main1.cpp)target_link_libraries(myexe1 foo)

# Uses the includes too!add_executable(myexe2 main2.cpp)

Page 37: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

How?

Less code!

target_link_libraries()

expresses target dependencies, consumes requirements

Page 38: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

How?

target_include_directories() target_compile_definitions() target_compile_options() target_compile_features() target_sources() target_link_options() (pending) target_archive_options() (pending)

express usage requirements

Page 39: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

How?

target_compile_definitions(tgt PRIVATE I_USE_THIS )

target_compile_definitions(tgt INTERFACE YOU_USE_THIS )

target_compile_definitions(tgt PUBLIC WE_BOTH_USE_THIS )

Page 40: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Compiler requirements

C++11

Page 41: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Compiler requirements

C++11

C++14

Page 42: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Compiler requirements

C++11

C++14

C++98

Page 43: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Compiler requirements

C++11?

C++14?

C++98?

Page 44: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Compiler requirements

add_library(foo SHARED foo.cpp)target_compile_features(foo INTERFACE cxx_variadic_templates)

...

add_executable(myexe main.cpp)target_link_libraries(myexe Bar::foo)

Page 45: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Compiler requirements

add_library(foo SHARED foo.cpp)target_compile_features(foo INTERFACE cxx_variadic_templates cxx_decltype_auto)...

add_executable(myexe main.cpp)target_link_libraries(myexe Bar::foo)

Page 46: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Compiler requirements

add_library(foo SHARED foo.cpp)target_compile_features(foo INTERFACE cxx_template_template_parameters)

...

add_executable(myexe main.cpp)target_link_libraries(myexe Bar::foo)

Page 47: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Compiler requirements

add_library(foo SHARED foo.c)target_compile_features(foo INTERFACE c_restrict c_static_assert)

...

add_executable(myexe main.c)target_link_libraries(myexe Bar::foo)

Page 48: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

The QTestLib problem

add_executable(guitest guitest.cpp)target_link_libraries(guitest Qt5::Test Qt5::Gui)

add_executable(coretest coretest.cpp)target_link_libraries(coretest Qt5::Test Qt5::Core)

Page 49: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

The QTestLib problem

#if defined(QT_CORE_LIB) QCoreApplication#elif defined(QT_GUI_LIB) QGuiApplication#elif defined(QT_WIDGET_LIB) QApplication#endif

Page 50: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Typo diagnostics

find_package(Qt5Widgets REQUIRED)

add_executable(typotest typotest.cpp)target_link_libraries(typotest Qt5::Wydjits)

Page 51: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Typo diagnostics

# find_package(Qt5Widgets REQUIRED)

add_executable(typotest typotest.cpp)target_link_libraries(typotest Qt5::Widgets)

Page 52: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Declarative

add_library(foo foo.cpp)target_include_directories(foo INTERFACE $<BUILD_INTERFACE:${srcDir}/bar> $<INSTALL_INTERFACE:${installDir}/bar>)

Page 53: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Declarative

add_library(foo foo.cpp)target_compile_definitions(foo INTERFACE $<$<CONFIG:Debug>:DEBUG_MODE>)

Page 54: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Interface compatibility

add_library(foo foo.cpp)set_target_property(foo INTERFACE_CUSTOM_STRING MULTI_THREADED)

add_library(bar bar.cpp)set_target_property(bar INTERFACE_CUSTOM_STRING NOT_MULTI_THREADED)

Page 55: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Interface-only libraries

add_library(boost::range INTERFACE IMPORTED)

Page 56: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Interface-only libraries

add_library(boost::range INTERFACE IMPORTED)set_target_properties(boost::range INTERFACE_INCLUDE_DIRECTORIES "/opt/boost_range/include")

Page 57: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Interface-only libraries

add_executable(myexe main.cpp)target_link_libraries(myexe boost::range boost::any boost::units)

Page 58: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Header-library challenges

● Find headers● Use include directories● Use correct macros (BOOST_ALL_NO_LIB etc)

Page 59: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Header-library challenges

● Find headers● Use include directories● Use correct macros (BOOST_ALL_NO_LIB etc)● Use correct compile flags (eg, non-broken preprocessor with Clang on Windows)● Compatibility between components● Diagnostics

Page 60: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Interface sources

add_library(lib_with_global_obj lib.cpp)target_sources(lib_with_global_obj INTERFACE symbol_user.cpp)

Page 61: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Interface sources

set(isExe "$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>")add_library(lib_with_global_obj lib.cpp)target_sources(lib_with_global_obj INTERFACE "$<${isExe}:symbol_user.cpp>")

Page 62: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Interface sources

set(isExe "$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>")add_library(lib_with_global_obj lib.cpp)target_sources(lib_with_global_obj INTERFACE "$<${isExe}:symbol_user.cpp>")

add_library(intermediate other.cpp)target_link_libraries(intermediate PUBLIC lib_with_global_obj)

Page 63: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Interface sources

set(isExe "$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>")add_library(lib_with_global_obj lib.cpp)target_sources(lib_with_global_obj INTERFACE "$<${isExe}:symbol_user.cpp>")

add_library(intermediate other.cpp)target_link_libraries(intermediate PUBLIC lib_with_global_obj)

...

add_executable(myexe intermediate)

Page 64: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Batteries included

Page 65: Stephen Kelly C++ User Group, Berlin July 2014 · The What, Why and How of Modern CMake Stephen Kelly C++ User Group, Berlin July 2014

Questions

And Answers...

steveire.wordpress.comhttp://www.cmake.org/cmake/help/v3.0/