cmake: Modernize compile settings; require C++14
[quassel.git] / cmake / QuasselMacros.cmake
1 # This file contains various functions and macros useful for building Quassel.
2 #
3 # (C) 2014-2018 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(QuasselCompileFeatures)
11
12 ###################################################################################################
13 # Adds a library target for a Quassel module.
14 #
15 # It expects the (CamelCased) module name as a parameter, and derives various
16 # strings from it. For example, quassel_add_module(Client) produces
17 #  - a library target named quassel_client with output name (lib)quassel-client(.so)
18 #  - an alias target named Quassel::Client in global scope
19 #
20 # The function exports the TARGET variable which can be used in the current scope
21 # for setting source files, properties, link dependencies and so on.
22 # To refer to the target outside of the current scope, e.g. for linking, use
23 # the alias name.
24 #
25 function(quassel_add_module _module)
26     # Derive target, alias target, output name from the given module name
27     set(alias "Quassel::${_module}")
28     set(target ${alias})
29     string(TOLOWER ${target} target)
30     string(REPLACE "::" "_" target ${target})
31     string(REPLACE "_" "-" output_name ${target})
32
33     add_library(${target} STATIC "")
34     add_library(${alias} ALIAS ${target})
35
36     set_target_properties(${target} PROPERTIES
37         OUTPUT_NAME ${output_name}
38     )
39
40     target_include_directories(${target}
41         PUBLIC  ${CMAKE_CURRENT_SOURCE_DIR}
42         PRIVATE ${CMAKE_CURRENT_BINARY_DIR} # for generated files
43     )
44
45     target_compile_features(${target} PUBLIC ${QUASSEL_COMPILE_FEATURES})
46
47     # Export the target name for further use
48     set(TARGET ${target} PARENT_SCOPE)
49 endfunction()
50
51 ###################################################################################################
52 # Provides a library that contains data files as a Qt resource (.qrc).
53 #
54 # quassel_add_resource(QrcName
55 #                      [BASEDIR basedir]
56 #                      [PREFIX prefix]
57 #                      PATTERNS pattern1 pattern2...
58 #                      [DEPENDS dep1 dep2...]
59 # )
60 #
61 # The first parameter is the CamelCased name of the resource; the library target will be called
62 # "Quassel::Resource::QrcName". The library provides a Qt resource named "qrcname" (lowercased QrcName)
63 # containing the files matching PATTERNS relative to BASEDIR (by default, the current source dir).
64 # The resource prefix can be set by giving the PREFIX argument.
65 # Additional target dependencies can be specified with DEPENDS.
66 #
67 function(quassel_add_resource _name)
68     set(options )
69     set(oneValueArgs BASEDIR PREFIX)
70     set(multiValueArgs DEPENDS PATTERNS)
71     cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
72
73     if (NOT ARG_BASEDIR)
74         set(ARG_BASEDIR ${CMAKE_CURRENT_SOURCE_DIR})
75     endif()
76     get_filename_component(basedir ${ARG_BASEDIR} REALPATH)
77
78     string(TOLOWER ${_name} lower_name)
79
80     set(qrc_target   quassel-qrc-${lower_name})
81     set(qrc_file     ${lower_name}.qrc)
82     set(qrc_src      qrc_${lower_name}.cpp)
83     set(qrc_filepath ${CMAKE_CURRENT_BINARY_DIR}/${qrc_file})
84     set(qrc_srcpath  ${CMAKE_CURRENT_BINARY_DIR}/${qrc_src})
85
86     # This target will always be built, so the qrc file will always be freshly generated.
87     # That way, changes to the glob result are always taken into account.
88     add_custom_target(${qrc_target} VERBATIM
89         COMMENT "Generating ${qrc_file}"
90         COMMAND ${CMAKE_COMMAND} -P ${CMAKE_SOURCE_DIR}/cmake/GenerateQrc.cmake "${qrc_filepath}" "${ARG_PREFIX}" "${ARG_PATTERNS}"
91         DEPENDS ${ARG_DEPENDS}
92         BYPRODUCTS ${qrc_filepath}
93         WORKING_DIRECTORY ${basedir}
94     )
95     set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${qrc_filepath})
96
97     # RCC sucks and expects the data files relative to the qrc file, with no way to configure it differently.
98     # Only when reading from stdin ("-") it takes the working directory as a base, so we have to use this if
99     # we want to use generated qrc files (which obviously cannot be placed in the source directory).
100     # Since neither autorcc nor qt5_add_resources() support this, we have to invoke rcc manually :(
101     #
102     # On Windows, input redirection apparently doesn't work, however piping does. Use this for all platforms for
103     # consistency, accommodating for the fact that the 'cat' equivalent on Windows is 'type'.
104     if (WIN32)
105         set(cat_cmd type)
106     else()
107         set(cat_cmd cat)
108     endif()
109     add_custom_command(VERBATIM
110         COMMENT "Generating ${qrc_src}"
111         COMMAND ${cat_cmd} "$<SHELL_PATH:${qrc_filepath}>"
112                 | "$<SHELL_PATH:$<TARGET_FILE:Qt5::rcc>>" --name "${lower_name}" --output "$<SHELL_PATH:${qrc_srcpath}>" -
113         DEPENDS ${qrc_target}
114         MAIN_DEPENDENCY ${qrc_filepath}
115         OUTPUT ${qrc_srcpath}
116         WORKING_DIRECTORY ${basedir}
117     )
118
119     # Generate library target that can be referenced elsewhere
120     quassel_add_module(Resource::${_name})
121     target_sources(${TARGET} PRIVATE ${qrc_srcpath})
122     set_target_properties(${TARGET} PROPERTIES AUTOMOC OFF AUTOUIC OFF AUTORCC OFF)
123
124     # Set variable for referencing the target from outside
125     set(RESOURCE_TARGET ${TARGET} PARENT_SCOPE)
126 endfunction()
127
128 ###################################################################################################
129 # target_link_if_exists(Target
130 #                       [PUBLIC dep1 dep2...]
131 #                       [PRIVATE dep3 dep4...]
132 # )
133 #
134 # Convenience function to add dependencies to a target only if they exist. This is useful when
135 # handling targets that are conditionally created, e.g. resource libraries depending on -DEMBED_DATA.
136 #
137 # NOTE: In order to link a given target, it must already have been created, i.e its subdirectory
138 #       must already have been added. This is also true for globally visible ALIAS targets that
139 #       can otherwise be linked to regardless of creation order; "if (TARGET...)" does not
140 #       support handling this case correctly.
141 #
142 function(target_link_if_exists _target)
143     set(options )
144     set(oneValueArgs )
145     set(multiValueArgs PUBLIC PRIVATE)
146     cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
147
148     if (ARG_PUBLIC)
149         foreach(dep ${ARG_PUBLIC})
150             if (TARGET ${dep})
151                 target_link_libraries(${_target} PUBLIC ${dep})
152             endif()
153         endforeach()
154     endif()
155
156     if (ARG_PRIVATE)
157         foreach(dep ${ARG_PRIVATE})
158             if (TARGET ${dep})
159                 target_link_libraries(${_target} PRIVATE ${dep})
160             endif()
161         endforeach()
162     endif()
163 endfunction()