CMake with Qt - Qt Developer Days

43
CMake with Qt Stephen Kelly [email protected]

Transcript of CMake with Qt - Qt Developer Days

Page 1: CMake with Qt - Qt Developer Days

CMake with Qt

Stephen [email protected]

Page 2: CMake with Qt - Qt Developer Days

Qt Buildsystems

Page 3: CMake with Qt - Qt Developer Days

Why CMake?

● Finding dependencies

● Providing shared libraries

● Platform independence● Configure checks

● Code generators

● Extendible

● Scalable

Page 4: CMake with Qt - Qt Developer Days

Related tools

CPack CTest CDash

Page 5: CMake with Qt - Qt Developer Days

Stages of building

Configure Generate moc Compile Link

Page 6: CMake with Qt - Qt Developer Days

Finding Dependencies

Page 7: CMake with Qt - Qt Developer Days

CMake today

find_package(Qt4 REQUIRED QtCore QtGui)

include_directories(${QT_INCLUDES})

add_definitions(${QT_DEFINITIONS})

add_executable(myexe WIN32 main.cpp)

target_link_libraries(myexe

${QT_QTCORE_LIBRARIES}

${QT_QTGUI_LIBRARIES}

)

Page 8: CMake with Qt - Qt Developer Days

CMake today

find_package(Qt5Widgets)

include_directories(${Qt5Widgets_INCLUDE_DIRS})

add_definitions(${Qt5Widgets_DEFINITIONS})

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_executable(myexe WIN32 main.cpp)

target_link_libraries(myexe

${Qt5Widgets_LIBRARIES}

)

Page 9: CMake with Qt - Qt Developer Days

QMake (Qt 5)

TARGET = myexe

SOURCES = main.cpp

QT += widgets

Page 10: CMake with Qt - Qt Developer Days

CMake today

find_package(Qt5Widgets)

include_directories(${Qt5Widgets_INCLUDE_DIRS})

add_definitions(${Qt5Widgets_DEFINITIONS})

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

add_executable(myexe WIN32 main.cpp)

target_link_libraries(myexe

${Qt5Widgets_LIBRARIES}

)

Page 11: CMake with Qt - Qt Developer Days

CMake tomorrow (2013)

find_package(Qt5Widgets)

add_executable(myexe WIN32 main.cpp)

target_link_libraries(myexe

Qt5::Widgets

)

Page 12: CMake with Qt - Qt Developer Days

CMake tomorrow (2013)

find_package(Qt4)

add_executable(myexe WIN32 main.cpp)

target_link_libraries(myexe

Qt4::Gui

)

Page 13: CMake with Qt - Qt Developer Days

Finding dependencies

find_package(Qt5Widgets)

find_package(boost …)

add_executable(dependent …)

target_link_libraries(dependent

Qt5::Widgets

boost::spirit

boost::mpl)

Page 14: CMake with Qt - Qt Developer Days

The QTestLib problem

Page 15: CMake with Qt - Qt Developer Days

Finding dependencies

find_package(LibXml2)● Libraries

● Header files

● Defines

● Version variables

find_package(Squish)● Executables

● Test macros

Page 16: CMake with Qt - Qt Developer Days

Finding dependencies

● Treasure maps● FindLibXml2.cmake

● FindSquish.cmake● FindQt4.cmake● Not part of upstream

● Local Guide● Qt5WidgetsConfig.cmake

● PulseAudioConfig.cmake● Part of upstream package

Page 17: CMake with Qt - Qt Developer Days

Generating Code

Page 18: CMake with Qt - Qt Developer Days

Generated code

CMakeqt4_wrap_cpp(moc_files mywidget.h)

qt4_wrap_ui(ui_files mywidget.ui)

add_library(mylib ${sources}

${moc_files}

${ui_files})

Page 19: CMake with Qt - Qt Developer Days

Generated code

CMakeqt4_wrap_cpp(moc_files mywidget.h)

qt4_wrap_ui(ui_files mywidget.ui)

add_library(mylib ${sources}

${moc_files}

${ui_files})

QMake

SOURCES += mywidget.cpp

HEADERS += mywidget.h mycontrol.h

Page 20: CMake with Qt - Qt Developer Days

Automoc

CMake (2.8.8)

● No need for qt[4|5]_wrap_cpp

● #include “moc_mywidget.cpp”

● Or don't!● Finds Q_OBJECT in .h and .cpp files

Page 21: CMake with Qt - Qt Developer Days

Automoc

CMake (2.8.8)

qt4_wrap_ui(ui_files mywidget.ui)

add_library(mylib ${sources}

${ui_files})

set_target_property(mylib AUTOMOC ON)

Page 22: CMake with Qt - Qt Developer Days

Automoc

CMake (2.8.8)

set(CMAKE_AUTOMOC ON)

qt4_wrap_ui(ui_files mywidget.ui)

add_library(mylib ${sources}

${ui_files})

Page 23: CMake with Qt - Qt Developer Days

Extended code generation

qt4_create_translation(qm_files

${mylib_SRCS} mylib_de.ts

)

Page 24: CMake with Qt - Qt Developer Days

Extended code generation

qt4_generate_dbus_interface(mywidget.h

com.kdab.mywidget.xml

)

qt4_add_dbus_interfaces(dbus_files

com.kdab.mywidget.xml)

Page 25: CMake with Qt - Qt Developer Days

Platform independence

Page 26: CMake with Qt - Qt Developer Days

Platform checks

check_include_file(“limits.h” HAVE_LIMITS)

check_source_compiles(

“#include <qglobal.h>

#ifndef __PIC__

#error \"Compile your code with -fPIC or -fPIE.\"

#endif

HAVE_PIC

)

Page 27: CMake with Qt - Qt Developer Days

Creating Dependencies

Page 28: CMake with Qt - Qt Developer Days

Providing dependencies

install(TARGETS mylib

EXPORT myexports …)

install(TARGETS otherlib

EXPORT myexports …)

install(EXPORT myexport …)

Page 29: CMake with Qt - Qt Developer Days

Providing dependencies

install(EXPORT myexport

FILENAME mytargets.cmake)

# mylibConfig.cmake

include(mytargets.cmake)

mylib_helpful_macro(...)

Page 30: CMake with Qt - Qt Developer Days

Providing dependencies

# No Findmylib.cmake needed!

find_package(mylib)

add_executable(dependent …)

target_link_libraries(dependent

mylib

otherlib)

Page 31: CMake with Qt - Qt Developer Days

Imported targets

# Qt5GuiConfig.cmake

add_library(Qt5::Gui IMPORTED)

set_target_property(Qt5::Gui

LOCATION

“/opt/qt5/lib/Qt5Gui.so”

)

Page 32: CMake with Qt - Qt Developer Days

Imported targets

# Qt5GuiConfig.cmake

add_library(Qt5::Gui IMPORTED)

set_target_property(Qt5::Gui

INTERFACE_INCLUDE_DIRECTORIES

“/opt/qt5/include/Qt5Gui”

“$<TARGET_INCLUDES:Qt5::Core>”

)

Page 33: CMake with Qt - Qt Developer Days

Imported targets

# Qt5GuiConfig.cmake

add_library(Qt5::Gui IMPORTED)

set_target_property(Qt5::Gui

INTERFACE_INCLUDE_DIRECTORIES

“/opt/qt5/include/Qt5Gui”

“$<TARGET_PROPERTY:Qt5::Core,INTERFACE_INCLUDE_DIRECTRIES>”

)

Page 34: CMake with Qt - Qt Developer Days

Imported targets

# Qt5GuiConfig.cmake

add_library(Qt5::Gui IMPORTED)

set_target_property(Qt5::Gui

INTERFACE_INCLUDE_DIRECTORIES

“/opt/qt5/include/Qt5Gui”

“$<TARGET_INCLUDES:Qt5::Core>”

)

Page 35: CMake with Qt - Qt Developer Days

qtmain.lib

QMake

CONFIG -= console

CMake (Qt 4)

add_executable(exec WIN32 …)

target_link_libraries(${QT_QTMAIN_LIBS})

Page 36: CMake with Qt - Qt Developer Days

qtmain.lib

CMake (Qt 5)

add_executable(exec WIN32 …)

target_link_libraries(${QT_QTMAIN_LIBS})

Page 37: CMake with Qt - Qt Developer Days

Imported targets

# Qt5GuiConfig.cmake

add_library(Qt5::Gui IMPORTED)

set_target_property(Qt5::Gui

INTERFACE_LINK_LIBRARIES

“$<TARGET_LIBRARIES:Qt5::Core>”

“$<$<WIN32_EXECUTABLE>:Qt5::WinMain>”

)

Page 38: CMake with Qt - Qt Developer Days

Declarative expressions

add_executable(CoolApp …)

target_link_libraries(CoolApp

Qt5::Widgets

$<$<CONFIG:Debug>:Qt5::Test>

)

Page 39: CMake with Qt - Qt Developer Days

Declarative expressions

add_executable(CoolApp …)

set_target_property(CoolApp

INCLUDE_DIRECTORIES $<$<CONFIG:Debug>:/usr/include/valgrind>

)

Page 40: CMake with Qt - Qt Developer Days

● Object orientated

● Declarative

Page 41: CMake with Qt - Qt Developer Days

Qt Buildsystems

QBS

Page 42: CMake with Qt - Qt Developer Days

Configure Generate moc Compile Link

QBS

Page 43: CMake with Qt - Qt Developer Days

Questions?

Stephen [email protected]