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