1 # This file contains various macros useful for building Quassel.
3 # (C) 2014 by the Quassel Project <devel@quassel-irc.org>
5 # The qt4_use_modules function was taken from CMake's Qt4Macros.cmake:
6 # (C) 2005-2009 Kitware, Inc.
8 # The qt5_use_modules function was taken from Qt 5.10.1 (and modified):
9 # (C) 2005-2011 Kitware, Inc.
11 # Redistribution and use is allowed according to the terms of the BSD license.
12 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
14 ############################
15 # Macros for dealing with Qt
16 ############################
18 # CMake gained this function in 2.8.10. To be able to use older versions, we've copied
19 # this here. If present, the function from CMake will take precedence and our copy will be ignored.
20 function(qt4_use_modules _target _link_type)
21 if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE")
23 set(link_type ${_link_type})
25 set(modules ${_link_type} ${ARGN})
27 foreach(_module ${modules})
28 string(TOUPPER ${_module} _ucmodule)
29 set(_targetPrefix QT_QT${_ucmodule})
30 if (_ucmodule STREQUAL QAXCONTAINER OR _ucmodule STREQUAL QAXSERVER)
31 if (NOT QT_Q${_ucmodule}_FOUND)
32 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
34 set(_targetPrefix QT_Q${_ucmodule})
36 if (NOT QT_QT${_ucmodule}_FOUND)
37 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
39 if ("${_ucmodule}" STREQUAL "MAIN")
40 message(FATAL_ERROR "Can not use \"${_module}\" module with qt4_use_modules.")
43 target_link_libraries(${_target} ${link_type} ${${_targetPrefix}_LIBRARIES})
44 set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${${_targetPrefix}_INCLUDE_DIR} ${QT_HEADERS_DIR} ${QT_MKSPECS_DIR}/default)
45 set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${${_targetPrefix}_COMPILE_DEFINITIONS})
49 # Qt 5.11 removed the qt5_use_modules function, so we need to provide it until we can switch to a modern CMake version.
50 # If present, the Qt-provided version will be used automatically instead.
51 function(qt5_use_modules _target _link_type)
52 if (NOT TARGET ${_target})
53 message(FATAL_ERROR "The first argument to qt5_use_modules must be an existing target.")
55 if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE" )
56 set(_qt5_modules ${ARGN})
57 set(_qt5_link_type ${_link_type})
59 set(_qt5_modules ${_link_type} ${ARGN})
62 if ("${_qt5_modules}" STREQUAL "")
63 message(FATAL_ERROR "qt5_use_modules requires at least one Qt module to use.")
65 foreach(_module ${_qt5_modules})
66 if (NOT Qt5${_module}_FOUND)
67 find_package(Qt5${_module} PATHS "${_Qt5_COMPONENT_PATH}" NO_DEFAULT_PATH)
68 if (NOT Qt5${_module}_FOUND)
69 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
72 target_link_libraries(${_target} ${_qt5_link_type} ${Qt5${_module}_LIBRARIES})
73 set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${Qt5${_module}_INCLUDE_DIRS})
74 set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${Qt5${_module}_COMPILE_DEFINITIONS})
75 if (Qt5_POSITION_INDEPENDENT_CODE
76 AND (CMAKE_VERSION VERSION_LESS 2.8.12
77 AND (NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
78 OR CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)))
79 set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ${Qt5_POSITION_INDEPENDENT_CODE})
84 # Some wrappers for simplifying dual-Qt support
86 function(qt_use_modules)
88 qt5_use_modules(${ARGN})
90 qt4_use_modules(${ARGN})
94 function(qt_wrap_ui _var)
96 qt5_wrap_ui(var ${ARGN})
98 qt4_wrap_ui(var ${ARGN})
100 set(${_var} ${${_var}} ${var} PARENT_SCOPE)
103 function(qt_add_resources _var)
105 qt5_add_resources(var ${ARGN})
107 qt4_add_resources(var ${ARGN})
109 set(${_var} ${${_var}} ${var} PARENT_SCOPE)
112 function(qt_add_dbus_interface _var)
114 qt5_add_dbus_interface(var ${ARGN})
116 qt4_add_dbus_interface(var ${ARGN})
118 set(${_var} ${${_var}} ${var} PARENT_SCOPE)
121 function(qt_add_dbus_adaptor _var)
123 qt5_add_dbus_adaptor(var ${ARGN})
125 qt4_add_dbus_adaptor(var ${ARGN})
127 set(${_var} ${${_var}} ${var} PARENT_SCOPE)
130 ######################################
131 # Macros for dealing with translations
132 ######################################
134 # This generates a .ts from a .po file
135 macro(generate_ts outvar basename)
136 set(input ${basename}.po)
137 set(output ${CMAKE_BINARY_DIR}/po/${basename}.ts)
138 add_custom_command(OUTPUT ${output}
139 COMMAND ${QT_LCONVERT_EXECUTABLE}
143 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/po
144 # This is a workaround to add (duplicate) strings that lconvert missed to the .ts
145 COMMAND ${QT_LUPDATE_EXECUTABLE}
147 ${CMAKE_SOURCE_DIR}/src/
149 DEPENDS ${basename}.po)
150 set(${outvar} ${output})
151 endmacro(generate_ts outvar basename)
153 # This generates a .qm from a .ts file
154 macro(generate_qm outvar basename)
155 set(input ${CMAKE_BINARY_DIR}/po/${basename}.ts)
156 set(output ${CMAKE_BINARY_DIR}/po/${basename}.qm)
157 add_custom_command(OUTPUT ${output}
158 COMMAND ${QT_LRELEASE_EXECUTABLE}
161 DEPENDS ${basename}.ts)
162 set(${outvar} ${output})
163 endmacro(generate_qm outvar basename)