Simplify handling of (dual-)Qt in the build system
[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 # Redistribution and use is allowed according to the terms of the BSD license.
9 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
10
11 ############################
12 # Macros for dealing with Qt
13 ############################
14
15 # CMake gained this function in 2.8.10. To be able to use older versions, we've copied
16 # this here. If present, the function from CMake will take precedence and our copy will be ignored.
17 function(quassel_qt4_use_modules _target _link_type)
18     message("SPUT calling")
19     if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE")
20         set(modules ${ARGN})
21         set(link_type ${_link_type})
22     else()
23         set(modules ${_link_type} ${ARGN})
24     endif()
25     foreach(_module ${modules})
26         string(TOUPPER ${_module} _ucmodule)
27         set(_targetPrefix QT_QT${_ucmodule})
28         if (_ucmodule STREQUAL QAXCONTAINER OR _ucmodule STREQUAL QAXSERVER)
29             if (NOT QT_Q${_ucmodule}_FOUND)
30                 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
31             endif()
32             set(_targetPrefix QT_Q${_ucmodule})
33         else()
34             if (NOT QT_QT${_ucmodule}_FOUND)
35                 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
36             endif()
37             if ("${_ucmodule}" STREQUAL "MAIN")
38                 message(FATAL_ERROR "Can not use \"${_module}\" module with qt4_use_modules.")
39             endif()
40         endif()
41         target_link_libraries(${_target} ${link_type} ${${_targetPrefix}_LIBRARIES})
42         set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${${_targetPrefix}_INCLUDE_DIR} ${QT_HEADERS_DIR} ${QT_MKSPECS_DIR}/default)
43         set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${${_targetPrefix}_COMPILE_DEFINITIONS})
44     endforeach()
45 endfunction()
46
47 # Some wrappers for simplifying dual-Qt support
48
49 function(qt_use_modules)
50     if (WITH_QT5)
51         qt5_use_modules(${ARGN})
52     else()
53         qt4_use_modules(${ARGN})
54     endif()
55 endfunction()
56
57 function(qt_wrap_ui _var)
58     if (WITH_QT5)
59         qt5_wrap_ui(var ${ARGN})
60     else()
61         qt4_wrap_ui(var ${ARGN})
62     endif()
63     set(${_var} ${${_var}} ${var} PARENT_SCOPE)
64 endfunction()
65
66 function(qt_add_resources _var)
67     if (WITH_QT5)
68         qt5_add_resources(var ${ARGN})
69     else()
70         qt4_add_resources(var ${ARGN})
71     endif()
72     set(${_var} ${${_var}} ${var} PARENT_SCOPE)
73 endfunction()
74
75 function(qt_add_dbus_interface _var)
76     if (WITH_QT5)
77         qt5_add_dbus_interface(var ${ARGN})
78     else()
79         qt4_add_dbus_interface(var ${ARGN})
80     endif()
81     set(${_var} ${${_var}} ${var} PARENT_SCOPE)
82 endfunction()
83
84 function(qt_add_dbus_adaptor _var)
85     if (WITH_QT5)
86         qt5_add_dbus_adaptor(var ${ARGN})
87     else()
88         qt4_add_dbus_adaptor(var ${ARGN})
89     endif()
90     set(${_var} ${${_var}} ${var} PARENT_SCOPE)
91 endfunction()
92
93 ######################################
94 # Macros for dealing with translations
95 ######################################
96
97 # This generates a .ts from a .po file
98 macro(generate_ts outvar basename)
99   set(input ${basename}.po)
100   set(output ${CMAKE_BINARY_DIR}/po/${basename}.ts)
101   add_custom_command(OUTPUT ${output}
102           COMMAND ${QT_LCONVERT_EXECUTABLE}
103           ARGS -i ${input}
104                -of ts
105                -o ${output}
106           WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/po
107 # This is a workaround to add (duplicate) strings that lconvert missed to the .ts
108           COMMAND ${QT_LUPDATE_EXECUTABLE}
109           ARGS -silent
110                ${CMAKE_SOURCE_DIR}/src/
111                -ts ${output}
112           DEPENDS ${basename}.po)
113   set(${outvar} ${output})
114 endmacro(generate_ts outvar basename)
115
116 # This generates a .qm from a .ts file
117 macro(generate_qm outvar basename)
118   set(input ${CMAKE_BINARY_DIR}/po/${basename}.ts)
119   set(output ${CMAKE_BINARY_DIR}/po/${basename}.qm)
120   add_custom_command(OUTPUT ${output}
121           COMMAND ${QT_LRELEASE_EXECUTABLE}
122           ARGS -silent
123                ${input}
124           DEPENDS ${basename}.ts)
125   set(${outvar} ${output})
126 endmacro(generate_qm outvar basename)