Bump version for release
[quassel.git] / cmake / QuasselMacros.cmake
1 # This file contains various functions and macros useful for building Quassel.
2 #
3 # (C) 2014-2020 by the Quassel Project <devel@quassel-irc.org>
4 #
5 # Redistribution and use is allowed according to the terms of the BSD license.
6 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
7 ###################################################################################################
8
9 include(CMakeParseArguments)
10 include(GenerateExportHeader)
11 include(QuasselCompileFeatures)
12
13 ###################################################################################################
14 # Adds a library target for a Quassel module.
15 #
16 # quassel_add_module(Module [STATIC] [EXPORT])
17 #
18 # The function expects the (CamelCased) module name as a parameter, and derives various
19 # strings from it. For example, quassel_add_module(Client) produces
20 #  - a library target named quassel_client with output name (lib)quassel-client(.so)
21 #  - an alias target named Quassel::Client in global scope
22 #
23 # If the optional argument STATIC is given, or the ENABLE_SHARED option is OFF,
24 # a static library is built; otherwise a shared library is created. For shared
25 # libraries, an install rule is also added.
26 #
27 # To generate an export header for the library, specify EXPORT. The header will be named
28 # ${module}-export.h (where ${module} is the lower-case name of the module).
29 #
30 # The function exports the TARGET variable which can be used in the current scope
31 # for setting source files, properties, link dependencies and so on.
32 # To refer to the target outside of the current scope, e.g. for linking, use
33 # the alias name.
34 #
35 function(quassel_add_module _module)
36     set(options EXPORT STATIC NOINSTALL)
37     set(oneValueArgs )
38     set(multiValueArgs )
39     cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
40
41     # Derive target, alias target, output name from the given module name
42     set(alias "Quassel::${_module}")
43     set(target ${alias})
44     string(TOLOWER ${target} target)
45     string(REPLACE "::" "_" target ${target})
46     string(REPLACE "_" "-" output_name ${target})
47
48     if (ARG_STATIC OR NOT ENABLE_SHARED)
49         set(buildmode STATIC)
50     else()
51         set(buildmode SHARED)
52     endif()
53
54     add_library(${target} ${buildmode} "")
55     add_library(${alias} ALIAS ${target})
56
57     target_link_libraries(${target} PRIVATE Qt5::Core)
58     target_include_directories(${target}
59         PUBLIC  ${CMAKE_CURRENT_SOURCE_DIR}
60         PRIVATE ${CMAKE_CURRENT_BINARY_DIR} # for generated files
61     )
62     target_compile_features(${target} PUBLIC ${QUASSEL_COMPILE_FEATURES})
63
64     set_target_properties(${target} PROPERTIES
65         OUTPUT_NAME ${output_name}
66         VERSION ${QUASSEL_MAJOR}.${QUASSEL_MINOR}.${QUASSEL_PATCH}
67     )
68
69     if (buildmode STREQUAL "SHARED" AND NOT ${ARG_NOINSTALL})
70         install(TARGETS ${target}
71             RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
72             LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
73             ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
74         )
75     endif()
76
77     if (ARG_EXPORT)
78         string(TOLOWER ${_module} lower_module)
79         string(TOUPPER ${_module} upper_module)
80         string(REPLACE "::" "-" header_base ${lower_module})
81         string(REPLACE "::" "_" macro_base ${upper_module})
82         generate_export_header(${target}
83             BASE_NAME ${macro_base}
84             EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/export/${header_base}-export.h
85         )
86         target_include_directories(${target} PUBLIC ${CMAKE_BINARY_DIR}/export)
87     endif()
88
89     # Export the target name for further use
90     set(TARGET ${target} PARENT_SCOPE)
91 endfunction()
92
93 ###################################################################################################
94 # Provides a library that contains data files as a Qt resource (.qrc).
95 #
96 # quassel_add_resource(QrcName
97 #                      [BASEDIR basedir]
98 #                      [PREFIX prefix]
99 #                      PATTERNS pattern1 pattern2...
100 #                      [DEPENDS dep1 dep2...]
101 # )
102 #
103 # The first parameter is the CamelCased name of the resource; the library target will be called
104 # "Quassel::Resource::QrcName". The library provides a Qt resource named "qrcname" (lowercased QrcName)
105 # containing the files matching PATTERNS relative to BASEDIR (by default, the current source dir).
106 # The resource prefix can be set by giving the PREFIX argument.
107 # Additional target dependencies can be specified with DEPENDS.
108 #
109 function(quassel_add_resource _name)
110     set(options )
111     set(oneValueArgs BASEDIR PREFIX)
112     set(multiValueArgs DEPENDS PATTERNS)
113     cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
114
115     if (NOT ARG_BASEDIR)
116         set(ARG_BASEDIR ${CMAKE_CURRENT_SOURCE_DIR})
117     endif()
118     get_filename_component(basedir ${ARG_BASEDIR} REALPATH)
119
120     string(TOLOWER ${_name} lower_name)
121
122     set(qrc_target   quassel-qrc-${lower_name})
123     set(qrc_file     ${lower_name}.qrc)
124     set(qrc_src      qrc_${lower_name}.cpp)
125     set(qrc_filepath ${CMAKE_CURRENT_BINARY_DIR}/${qrc_file})
126     set(qrc_srcpath  ${CMAKE_CURRENT_BINARY_DIR}/${qrc_src})
127
128     # This target will always be built, so the qrc file will always be freshly generated.
129     # That way, changes to the glob result are always taken into account.
130     add_custom_target(${qrc_target} VERBATIM
131         COMMENT "Generating ${qrc_file}"
132         COMMAND ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/cmake/GenerateQrc.cmake "${qrc_filepath}" "${ARG_PREFIX}" "${ARG_PATTERNS}"
133         DEPENDS ${ARG_DEPENDS}
134         BYPRODUCTS ${qrc_filepath}
135         WORKING_DIRECTORY ${basedir}
136     )
137     set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${qrc_filepath})
138
139     # RCC sucks and expects the data files relative to the qrc file, with no way to configure it differently.
140     # Only when reading from stdin ("-") it takes the working directory as a base, so we have to use this if
141     # we want to use generated qrc files (which obviously cannot be placed in the source directory).
142     # Since neither autorcc nor qt5_add_resources() support this, we have to invoke rcc manually :(
143     #
144     # On Windows, input redirection apparently doesn't work, however piping does. Use this for all platforms for
145     # consistency, accommodating for the fact that the 'cat' equivalent on Windows is 'type'.
146     if (WIN32 AND NOT MSYS)
147         set(cat_cmd type)
148     else()
149         set(cat_cmd cat)
150     endif()
151     add_custom_command(VERBATIM
152         COMMENT "Generating ${qrc_src}"
153         COMMAND ${cat_cmd} "$<SHELL_PATH:${qrc_filepath}>"
154                 | "$<SHELL_PATH:$<TARGET_FILE:Qt5::rcc>>" --name "${lower_name}" --output "$<SHELL_PATH:${qrc_srcpath}>" -
155         DEPENDS ${qrc_target}
156         MAIN_DEPENDENCY ${qrc_filepath}
157         OUTPUT ${qrc_srcpath}
158         WORKING_DIRECTORY ${basedir}
159     )
160
161     # Generate library target that can be referenced elsewhere. Force static, because
162     # we can't easily export symbols from the generated sources.
163     quassel_add_module(Resource::${_name} STATIC)
164     target_sources(${TARGET} PRIVATE ${qrc_srcpath})
165     set_target_properties(${TARGET} PROPERTIES AUTOMOC OFF AUTOUIC OFF AUTORCC OFF)
166
167     # Set variable for referencing the target from outside
168     set(RESOURCE_TARGET ${TARGET} PARENT_SCOPE)
169 endfunction()
170
171 ###################################################################################################
172 # Adds a unit test case
173 #
174 # quassel_add_test(TestName
175 #                  [LIBRARIES lib1 lib2...]
176 # )
177 #
178 # The test name is given in CamelCase as first and mandatory parameter. The corresponding source file
179 # is expected the lower-cased test name plus the .cpp extension.
180 # The test case is automatically linked against Qt5::Test, GMock, Quassel::Common and
181 # Quassel::Test::Main, which contains the main function. This main function also instantiates a
182 # QCoreApplication, so the event loop can be used in test cases.
183 #
184 # Additional libraries can be given using the LIBRARIES argument.
185 #
186 # Test cases should include testglobal.h, which transitively includes the GTest/GMock headers and
187 # exports the main function.
188 #
189 # The compiled test case binary is located in the unit/ directory in the build directory.
190 #
191 function(quassel_add_test _target)
192     set(options )
193     set(oneValueArgs )
194     set(multiValueArgs LIBRARIES)
195     cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
196
197     string(TOLOWER ${_target} lower_target)
198     set(srcfile ${lower_target}.cpp)
199
200     list(APPEND ARG_LIBRARIES
201         Qt5::Test
202         Quassel::Common
203         Quassel::Test::Global
204         Quassel::Test::Main
205     )
206
207     if (WIN32)
208         # On Windows, tests need to be built in the same directory that contains the libraries
209         set(output_dir "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
210     else()
211         # On other platforms, separate the test cases out
212         set(output_dir "${CMAKE_BINARY_DIR}/unit")
213     endif()
214
215     add_executable(${_target} ${srcfile})
216     set_target_properties(${_target} PROPERTIES
217         OUTPUT_NAME ${_target}
218         RUNTIME_OUTPUT_DIRECTORY "${output_dir}"
219     )
220     target_link_libraries(${_target} PUBLIC ${ARG_LIBRARIES})
221
222     add_test(
223         NAME ${_target}
224         COMMAND $<TARGET_FILE:${_target}>
225         WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
226     )
227 endfunction()
228
229 ###################################################################################################
230 # target_link_if_exists(Target
231 #                       [PUBLIC dep1 dep2...]
232 #                       [PRIVATE dep3 dep4...]
233 # )
234 #
235 # Convenience function to add dependencies to a target only if they exist. This is useful when
236 # handling targets that are conditionally created, e.g. resource libraries depending on -DEMBED_DATA.
237 #
238 # NOTE: In order to link a given target, it must already have been created, i.e its subdirectory
239 #       must already have been added. This is also true for globally visible ALIAS targets that
240 #       can otherwise be linked to regardless of creation order; "if (TARGET...)" does not
241 #       support handling this case correctly.
242 #
243 function(target_link_if_exists _target)
244     set(options )
245     set(oneValueArgs )
246     set(multiValueArgs PUBLIC PRIVATE)
247     cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
248
249     if (ARG_PUBLIC)
250         foreach(dep ${ARG_PUBLIC})
251             if (TARGET ${dep})
252                 target_link_libraries(${_target} PUBLIC ${dep})
253             endif()
254         endforeach()
255     endif()
256
257     if (ARG_PRIVATE)
258         foreach(dep ${ARG_PRIVATE})
259             if (TARGET ${dep})
260                 target_link_libraries(${_target} PRIVATE ${dep})
261             endif()
262         endforeach()
263     endif()
264 endfunction()
265
266 ###################################################################################################
267 # process_cmake_cxx_flags()
268 #
269 # Append the options declared CMAKE_CXX_FLAGS and CMAKE_CXX_FLAGS_<BUILD_TYPE> to the global
270 # compile options.
271 # Unset the variables afterwards to avoid duplication.
272 #
273 function(process_cmake_cxx_flags)
274     string(TOUPPER ${CMAKE_BUILD_TYPE} upper_build_type)
275     set(cxx_flags "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${upper_build_type}}")
276     separate_arguments(sep_cxx_flags UNIX_COMMAND ${cxx_flags})
277     add_compile_options(${sep_cxx_flags})
278     set(CMAKE_CXX_FLAGS "" PARENT_SCOPE)
279     set(CMAKE_CXX_FLAGS_${upper_build_type} "" PARENT_SCOPE)
280 endfunction()