Skip to content

Commit

Permalink
Adapted CMakeLists.txt to support Qt6
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed May 17, 2024
1 parent 9067764 commit f0ba957
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 113 deletions.
64 changes: 0 additions & 64 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ endif()
set(eCAL_install_lib_dir ${CMAKE_INSTALL_LIBDIR})
set(eCAL_install_bin_dir ${CMAKE_INSTALL_BINDIR})

if (WIN32)
if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
set(PLATFORM_NAME "x64")
else()
set(PLATFORM_NAME "win32")
endif()
set(VS_PLATFORM "v140_${PLATFORM_NAME}")
endif()

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake_utils/mon_functions.cmake)

Expand All @@ -76,67 +68,11 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake_utils/mon_functions.cmake)
# --------------------------------------------------------
include(CMakeDependentOption)

# --------------------------------------------------------
# detect qt library
# --------------------------------------------------------
if(WIN32)
if(NOT DEFINED ENV{QT5_ROOT_DIRECTORY})
# If the QT5_ROOT_DIRECTORY Variable is not set, there is still a chance that
# the user set the CMAKE_PREFIX_PATH himself. Thus we try to find
# Qt5 Core just to see if that works. If we can find Qt, we assume
# that the user knows what he is doing.
find_package(Qt5 COMPONENTS Core)
if (NOT Qt5_FOUND)
message(FATAL_ERROR "QT5_ROOT_DIRECTORY is not defined. Please create an \
environment variable QT5_ROOT_DIRECTORY pointing to your QT5 installation \
(the directory that contains the msvc2015 or msvc2015_64 \
directory)")
endif()
else()
if(NOT CMAKE_CL_64)
# Add 32bit Qt5
list(APPEND CMAKE_PREFIX_PATH $ENV{QT5_ROOT_DIRECTORY}/msvc2015/)
message(STATUS "QT 32 Bit")
message(STATUS "CMAKE_PREFIX_PATH appended: $ENV{QT5_ROOT_DIRECTORY}/msvc2015/")
else ()
# Add 64bit Qt5
list(APPEND CMAKE_PREFIX_PATH $ENV{QT5_ROOT_DIRECTORY}/msvc2015_64/)
message(STATUS "QT 64 Bit")
message(STATUS "CMAKE_PREFIX_PATH appended: $ENV{QT5_ROOT_DIRECTORY}/msvc2015_64/")
endif()
endif()
endif(WIN32)

if(WIN32)
include(${CMAKE_CURRENT_LIST_DIR}/cmake_utils/qt/qt_windeployqt.cmake)
endif()


if(POLICY CMP0071)
cmake_policy(SET CMP0071 NEW)
endif()

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC OFF) # Reason for being turned off: AutoUIC will prevent VS from detecting changes in .ui files
set(CMAKE_AUTORCC OFF) # Reason for being turned off: AutoRCC will create an entirely new project in VS which clutters the solution appearance. Additionally, we cannot assign a source group to the generated .cpp files which will clutter the project.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(eCAL COMPONENTS core pb mon_plugin_lib REQUIRED)
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
find_package(Protobuf REQUIRED)

create_targets_protobuf()
set(THREADS_PREFER_PTHREAD_FLAG ON)

if(UNIX)
message(STATUS "GCC detected - Adding flags")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_definitions(-fno-pie -fno-PIE -fPIC)
if(NOT CMAKE_VERSION VERSION_LESS 3.10 AND NOT OpenGL_GL_PREFERENCE)
set(OpenGL_GL_PREFERENCE GLVND)
endif()
endif()


add_subdirectory(ecal_camera_snd)
add_subdirectory(camera_receiver_mon_plugin)
34 changes: 26 additions & 8 deletions camera_receiver_mon_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@
#
# ========================= LICENSE =================================

cmake_minimum_required(VERSION 3.14)

# Allow the install command to use generator expressions
if(POLICY CMP0087)
cmake_policy(SET CMP0087 NEW)
endif()

project(mon_plugin_camera_receiver VERSION 0.1.0)

find_package(Qt5 COMPONENTS
Core
Widgets
REQUIRED)
# Legacy Qt5 (pre 5.15) support as suggested by teh Qt Documentation:
# https://doc.qt.io/qt-6/cmake-qt5-and-qt6-compatibility.html#supporting-older-qt-5-versions
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Widgets)

find_package(eCAL REQUIRED)
find_package(Protobuf REQUIRED)
Expand All @@ -45,7 +52,12 @@ set(${PROJECT_NAME}_ui
src/plugin_widget.ui
)

qt5_wrap_ui(autogen_ui ${${PROJECT_NAME}_ui})
# compile qt resource files and ui files
if (${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_wrap_ui(autogen_ui ${${PROJECT_NAME}_ui})
else()
qt5_wrap_ui(autogen_ui ${${PROJECT_NAME}_ui})
endif()

add_mon_plugin(${PROJECT_NAME}
${${PROJECT_NAME}_src} ${${PROJECT_NAME}_header} ${${PROJECT_NAME}_ui} ${autogen_ui}
Expand All @@ -57,9 +69,15 @@ set(protobuf_files
)
PROTOBUF_TARGET_CPP(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/proto_messages/ ${protobuf_files})


create_targets_protobuf()
target_link_libraries(${PROJECT_NAME} Qt5::Widgets eCAL::core protobuf::libprotobuf eCAL::mon_plugin_lib)

target_link_libraries(${PROJECT_NAME}
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Widgets
eCAL::core
protobuf::libprotobuf
eCAL::mon_plugin_lib
)

if(MSVC)
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "/wd4127 /wd4714")
Expand All @@ -71,7 +89,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE $<TARGET_PROPERTY:eCAL::core,
install_mon_plugin(${PROJECT_NAME})

if (TARGET mon)
add_dependencies(mon ${PROJECT_NAME})
add_dependencies(mon ${PROJECT_NAME})
endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY CMAKE_BINARY_DIR/lib)
Expand Down
114 changes: 73 additions & 41 deletions ecal_camera_snd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@
#
# ========================= LICENSE =================================

cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.14)

# Allow the install command to use generator expressions
if(POLICY CMP0087)
cmake_policy(SET CMP0087 NEW)
endif()

set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)

project(ecal_camera_snd)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
# Legacy Qt5 (pre 5.15) support as suggested by teh Qt Documentation:
# https://doc.qt.io/qt-6/cmake-qt5-and-qt6-compatibility.html#supporting-older-qt-5-versions
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Widgets MultimediaWidgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Widgets MultimediaWidgets)

find_package(eCAL REQUIRED)
find_package(Protobuf REQUIRED)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt5MultimediaWidgets REQUIRED)

set(CMAKE_AUTOMOC ON)

set(source_files
main.cpp
Expand All @@ -49,45 +56,70 @@ PROTOBUF_TARGET_CPP(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/proto_messages/ ${protob
target_link_libraries(${PROJECT_NAME}
eCAL::core
protobuf::libprotobuf
Qt5::Widgets
Qt5::MultimediaWidgets
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::MultimediaWidgets
Qt${QT_VERSION_MAJOR}::Multimedia
)

if(WIN32)
# Deploy Qt DLLs in the binary folder. This is necessary for starting the application from whithin the IDE without having to copy QtCore.dll, QtWidgets.dll etc. by hand each time
qt_add_windeployqt_postbuild(--no-system-d3d-compiler --libdir ${CMAKE_SOURCE_DIR}/../_deploy/bin --plugindir ${CMAKE_SOURCE_DIR}/../_deploy/bin --no-compiler-runtime --no-opengl-sw --pdb "$<TARGET_FILE:${PROJECT_NAME}>")
endif()
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)

install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin COMPONENT app
)
if ((WIN32 OR APPLE) AND (${QT_VERSION_MAJOR} GREATER_EQUAL 6))

# Generate a script that will deploy all necessary Qt DLLs to the binary folder
# https://doc.qt.io/qt-6/qt-deploy-runtime-dependencies.html
# Available for Qt 6.3 and up (=> Not for Qt5!)
# Executing it requires CMake 3.14 and up, due to policy https://cmake.org/cmake/help/latest/policy/CMP0087.html
qt_generate_deploy_app_script(
TARGET ${PROJECT_NAME}
OUTPUT_SCRIPT qt_deploy_script
NO_COMPILER_RUNTIME
NO_UNSUPPORTED_PLATFORM_ERROR
)

# Add a postbuild script that will also execute the created script via cmake -P
# This is necessary to make the application startable / debuggable from the build directory.
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -DQT_DEPLOY_PREFIX=$<TARGET_FILE_DIR:${PROJECT_NAME}> -DQT_DEPLOY_BIN_DIR=. -P ${qt_deploy_script}
)

# Use the script for deploying the qt dlls in the install dir
install(SCRIPT ${qt_deploy_script})

if(WIN32)
install(CODE
"
set(_file ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Release/ecal_play_gui.exe)
execute_process(
COMMAND \"${CMAKE_COMMAND}\" -E
env PATH=\"${_qt_bin_dir}\" \"${WINDEPLOYQT_EXECUTABLE}\"
--dry-run
--no-compiler-runtime
--no-angle
--no-opengl-sw
--list mapping
\${_file}
OUTPUT_VARIABLE _output
OUTPUT_STRIP_TRAILING_WHITESPACE
)
separate_arguments(_files WINDOWS_COMMAND \${_output})
while(_files)
list(GET _files 0 _src)
list(GET _files 1 _dest)
execute_process(
elseif(WIN32)

# For Qt5 we use our legacy script.
# Deploy Qt DLLs in the binary folder. This is necessary for starting the application from whithin the IDE without having to copy QtCore.dll, QtWidgets.dll etc. by hand each time
qt_add_windeployqt_postbuild(--no-system-d3d-compiler --no-compiler-runtime --no-opengl-sw --pdb "$<TARGET_FILE:${PROJECT_NAME}>")

get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")
install(CODE
"
set(_file ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Release/ecal_mon_gui.exe)
execute_process(
COMMAND \"${CMAKE_COMMAND}\" -E
copy \${_src} \"\${CMAKE_INSTALL_PREFIX}/bin/\${_dest}\"
env PATH=\"${_qt_bin_dir}\" \"${WINDEPLOYQT_EXECUTABLE}\"
--dry-run
--no-compiler-runtime
--no-angle
--no-opengl-sw
--list mapping
\${_file}
OUTPUT_VARIABLE _output
OUTPUT_STRIP_TRAILING_WHITESPACE
)
list(REMOVE_AT _files 0 1)
endwhile()
"
separate_arguments(_files WINDOWS_COMMAND \${_output})
while(_files)
list(GET _files 0 _src)
list(GET _files 1 _dest)
execute_process(
COMMAND \"${CMAKE_COMMAND}\" -E
copy \${_src} \"\${CMAKE_INSTALL_PREFIX}/bin/\${_dest}\"
)
list(REMOVE_AT _files 0 1)
endwhile()
"
)
endif()
endif()

0 comments on commit f0ba957

Please sign in to comment.