qtui: Fix icons in the AboutDlg
[quassel.git] / cmake / QuasselMacros.cmake
1 # This file contains various macros useful for building Quassel.
2 #
3 # (C) 2014 by the Quassel Project <devel@quassel-irc.org>
4 #
5 # The qt4_use_modules function was taken from CMake's Qt4Macros.cmake:
6 # (C) 2005-2009 Kitware, Inc.
7 #
8 # The qt5_use_modules function was taken from Qt 5.10.1 (and modified):
9 # (C) 2005-2011 Kitware, Inc.
10 #
11 # Redistribution and use is allowed according to the terms of the BSD license.
12 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
13
14 ############################
15 # Macros for dealing with Qt
16 ############################
17
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")
22         set(modules ${ARGN})
23         set(link_type ${_link_type})
24     else()
25         set(modules ${_link_type} ${ARGN})
26     endif()
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.")
33             endif()
34             set(_targetPrefix QT_Q${_ucmodule})
35         else()
36             if (NOT QT_QT${_ucmodule}_FOUND)
37                 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
38             endif()
39             if ("${_ucmodule}" STREQUAL "MAIN")
40                 message(FATAL_ERROR "Can not use \"${_module}\" module with qt4_use_modules.")
41             endif()
42         endif()
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})
46     endforeach()
47 endfunction()
48
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.")
54     endif()
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})
58     else()
59         set(_qt5_modules ${_link_type} ${ARGN})
60     endif()
61
62     if ("${_qt5_modules}" STREQUAL "")
63         message(FATAL_ERROR "qt5_use_modules requires at least one Qt module to use.")
64     endif()
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.")
70             endif()
71         endif()
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})
80         endif()
81     endforeach()
82 endfunction()
83
84 # Some wrappers for simplifying dual-Qt support
85
86 function(qt_use_modules)
87     if (USE_QT5)
88         qt5_use_modules(${ARGN})
89     else()
90         qt4_use_modules(${ARGN})
91     endif()
92 endfunction()
93
94 function(qt_wrap_ui _var)
95     if (USE_QT5)
96         qt5_wrap_ui(var ${ARGN})
97     else()
98         qt4_wrap_ui(var ${ARGN})
99     endif()
100     set(${_var} ${${_var}} ${var} PARENT_SCOPE)
101 endfunction()
102
103 function(qt_add_resources _var)
104     if (USE_QT5)
105         qt5_add_resources(var ${ARGN})
106     else()
107         qt4_add_resources(var ${ARGN})
108     endif()
109     set(${_var} ${${_var}} ${var} PARENT_SCOPE)
110 endfunction()
111
112 function(qt_add_dbus_interface _var)
113     if (USE_QT5)
114         qt5_add_dbus_interface(var ${ARGN})
115     else()
116         qt4_add_dbus_interface(var ${ARGN})
117     endif()
118     set(${_var} ${${_var}} ${var} PARENT_SCOPE)
119 endfunction()
120
121 function(qt_add_dbus_adaptor _var)
122     if (USE_QT5)
123         qt5_add_dbus_adaptor(var ${ARGN})
124     else()
125         qt4_add_dbus_adaptor(var ${ARGN})
126     endif()
127     set(${_var} ${${_var}} ${var} PARENT_SCOPE)
128 endfunction()
129
130 ######################################
131 # Macros for dealing with translations
132 ######################################
133
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}
140           ARGS -i ${input}
141                -of ts
142                -o ${output}
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}
146           ARGS -silent
147                ${CMAKE_SOURCE_DIR}/src/
148                -ts ${output}
149           DEPENDS ${basename}.po)
150   set(${outvar} ${output})
151 endmacro(generate_ts outvar basename)
152
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}
159           ARGS -silent
160                ${input}
161           DEPENDS ${basename}.ts)
162   set(${outvar} ${output})
163 endmacro(generate_qm outvar basename)