modern cmake with qt and boost · 2017-06-01 · stephen kelly c++/qt user since 2006 kde...

50
Modern CMake with Qt and Boost Stephen Kelly [email protected] KDAB

Upload: others

Post on 29-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

Modern CMake with Qt and Boost

Stephen [email protected]

KDAB

Page 2: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

Stephen Kelly

● C++/Qt user since 2006

● KDE contributor since 2007

● KDAB Engineer since 2009

● Qt contributor since 2009

● CMake contributor since 2011

● Boost contributor since 2013

● Living in Berlin

Page 3: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor
Page 4: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

● Modelview maintainer

● Metatype system

● CMake files maintainer

● Consulting/training

● KDE libraries

KDAB, Qt & KDE

Page 5: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

● Top contributor to CMake 2.8.11, 2.8.12

● Transformed how CMake works

● Designed for Qt, KDE and Boost

CMake

Page 6: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

stephen@hal:~/dev/src/boost-trunk{mystuff}

$ git diff –shortstat ea38de1f3b8b..HEAD

2029 files changed, 2482 insertions(+), 51267 deletions(-)

Boost

Page 7: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

Why?

Page 8: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

● Dependencies

● Versioning

● Distributable

Why?

Page 9: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

● Dependencies

● Versioning

● Distributable

● Portability

● Compiler-specific flags

● Dependency-specific flags

Why?

Page 10: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

Why CMake?

Page 11: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

● Find dependencies

● Cross-platform

● Multiple generators

Why CMake?

Page 12: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

● Find dependencies

● Cross-platform

● Multiple generators

● Buildsystem abstractions

● Dependency abstractions

● Compiler feature abstractions

Why CMake?

Page 13: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

find_package(Qt5Widgets 5.2 REQUIRED)

add_executable(myapp main.cpp)

target_link_libraries(myapp

Qt5::Widgets

)

Dependencies

Page 14: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

# find_package(Qt5Widgets 5.2 REQUIRED)

add_executable(myapp main.cpp)

target_link_libraries(myapp

Qt5::Widgets # Error diagnostic

)

Dependencies

Page 15: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

add_library(Qt5::Widgets

SHARED IMPORTED)

set_property(TARGET Qt5::Widgets

LOCATION

"${somewhere}/lib/libQt5Widgets.so"

)

Qt5WidgetsConfig

Page 16: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

...

set_property(TARGET Qt5::Widgets

INTERFACE_INCLUDE_DIRECTORIES

"${somewhere}/include/QtWidgets"

)

Qt5WidgetsConfig

Page 17: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

...

set_property(TARGET Qt5::Widgets

INTERFACE_COMPILE_DEFINITIONS

"QT_WIDGETS_LIB"

)

Qt5WidgetsConfig

Page 18: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

...

set_property(TARGET Qt5::Core

INTERFACE_COMPILE_DEFINITIONS

"$<$<CONFIG:Debug>:QT_DEBUG>"

)

Qt5WidgetsConfig

Page 19: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

#if !defined(__PIC__)

# error "Compile your code "

"with -fPIC or -fPIE."

#endif

Qt5WidgetsConfig

Page 20: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

set_property(TARGET Qt5::Core

INTERFACE_COMPILE_OPTIONS

"$<AND:\

$<STREQUAL:\

$<TARGET_PROPERTY:TYPE>,\

EXECUTABLE\

>,$<OR:\

$<COMPILER_ID:GNU>,\

$<COMPILER_ID:Clang>\

>:-fPIE>"

)

Qt5WidgetsConfig

Page 21: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

...

set_property(TARGET Qt5::Core

INTERFACE_POSITION_INDEPENDENT_CODE

"ON"

)

Qt5WidgetsConfig

Page 22: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor
Page 23: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

● Automatic Qt code generation features

● Automatic moc invocation● Automatic uic invocation● Automatic rcc invocation

Qt specific features

Page 24: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

set(CMAKE_AUTOMOC ON)

class MyClass : public QObject

{

Q_OBJECT

};

AUTOMOC

Page 25: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

set(CMAKE_AUTORCC ON)

find_package(Qt5Widgets REQUIRED)

add_executable(myapp

main.cpp

myresources.qrc

)

AUTORCC

Page 26: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

set(CMAKE_AUTOUIC ON)

#include "ui_mywidget.h"

MyWidget::MyWidget(QWidget *parent)

: QWidget(parent),

ui(new Ui::MyWidget)

{

}

AUTOUIC

Page 27: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor
Page 28: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

add_library(boost::any

IMPORTED INTERFACE)

# No LOCATION!

set_property(TARGET boost::any

INTERFACE_INCLUDE_DIRECTORIES

"${someprefix}/include"

)

Header-only libraries

Page 29: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

Boost dependency graph

Transitive usage requirements

Page 30: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

add_library(kdab::sqlate

IMPORTED INTERFACE)

set_property(TARGET kdab::sqlate

INTERFACE_COMPILE_DEFINITIONS

"BOOST_MPL_LIMIT_VECTOR_SIZE=50"

)

Transitive usage requirements

Page 31: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

add_library(other::lib

IMPORTED INTERFACE)

set_property(TARGET other::lib

INTERFACE_COMPILE_DEFINITIONS

"BOOST_MPL_LIMIT_VECTOR_SIZE=30"

)

Transitive usage requirements

Page 32: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

add_executable(user main.cpp)

target_link_libraries(user

kdab::sqlate

other::lib

)

-DBOOST_MPL_LIMIT_VECTOR_SIZE=50

Transitive usage requirements

Page 33: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

set_property(TARGET kdab::sqlate

INTERFACE_COMPILE_DEFINITIONS

"QT_DISABLE_DEPRECATED_BEFORE=0x050100"

)

Transitive usage requirements

Page 34: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

set_property(TARGET other::lib

INTERFACE_COMPILE_DEFINITIONS

"QT_DISABLE_DEPRECATED_BEFORE=0x050200"

)

Transitive usage requirements

Page 35: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

add_executable(user main.cpp)

target_link_libraries(user

kdab::sqlate

other::lib

)

-DQT_DISABLE_DEPRECATED_BEFORE=0x050100

Transitive usage requirements

Page 36: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

add_executable(user main.cpp)

target_link_libraries(user

Qt4::QtCore

Qt5::Core # Diagnostic

)

Transitive usage requirements

Page 37: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

set_property(TARGET Qt5::Core

INTERFACE_QT_MAJOR_VERSION

"5"

)

set_property(TARGET Qt4::QtCore

INTERFACE_QT_MAJOR_VERSION

"4"

)

Transitive usage requirements

Page 38: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor
Page 39: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

C++11

Page 40: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

C++14

Page 41: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

C++98

Page 42: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

C89 C11 C99 C1Y

Page 43: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

add_executable(new-app main.cpp)

target_compile_features(new-app

cxx_member_templates

cxx_constexpr

cxx_generic_lambda

c_restrict

gnuxx_typeof

)

Transitive compiler features

Page 44: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

write_compiler_feature_header(

FILE "mycompiler_detection.h"

PREFIX MyPrefix

FEATURES

cxx_static_assert

cxx_final

cxx_variadic_templates

)

Optional compiler features

Page 45: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

#if defined(__clang__) \

&& __has_feature(cxx_static_assert)

#define MyPrefix_STATIC_ASSERT(a) \

static_assert(a, #a)

#else

...

#endif

Optional compiler features

Page 46: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

#if defined(__clang__) \

&& __has_feature(cxx_static_assert)

#define MyPrefix_STATIC_ASSERT(a) \

static_assert(a, #a)

#else

template<bool test>

struct MyPrefixStaticAssert;

template<>

struct MyPrefixStaticAssert<true> {};

#define MyPrefix_STATIC_ASSERT(a) \

MyPrefixStaticAssert<a>;

#endif

Optional compiler features

Page 47: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

#if defined(__clang__) \

&& __has_feature(cxx_variadic_templates)

|| defined(__GNUC__) && (VER >= 4.5)

#define MyPrefix_VARIADIC_TEMPLATES 1

#else

#define MyPrefix_VARIADIC_TEMPLATES 0

#endif

Optional compiler features

Page 48: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

#if ...

#define MyPrefix_FINAL final

#elif defined(_MSC_VER) && ...

#define MyPrefix_FINAL sealed

#else

#define MyPrefix_FINAL

#endif

Optional compiler features

Page 49: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor

● CMake designed for Qt and Boost● Diagnostics● Built-in code-generator support● Usage requirements

● Transitive● Conditionals

● Header-only library abstraction● Compiler feature support

Summary

Page 50: Modern CMake with Qt and Boost · 2017-06-01 · Stephen Kelly C++/Qt user since 2006 KDE contributor since 2007 KDAB Engineer since 2009 Qt contributor since 2009 CMake contributor