Debian Codegraph @

cmake/modules/Macros.cmake

raw
1 option(DBUS_USE_WINE "set to 1 or ON to support running test cases with Wine" OFF)
2
3 if((DBUS_ENABLE_MODULAR_TESTS OR DBUS_ENABLE_INTRUSIVE_TESTS) AND CMAKE_CROSSCOMPILING AND CMAKE_SYSTEM_NAME STREQUAL "Windows")
4 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
5 find_file(WINE_EXECUTABLE
6 NAMES wine
7 PATHS /usr/bin /usr/local/bin
8 NO_CMAKE_FIND_ROOT_PATH
9 )
10 find_file(BINFMT_WINE_SUPPORT_FILE
11 NAMES DOSWin wine Wine windows Windows
12 PATHS /proc/sys/fs/binfmt_misc
13 NO_SYSTEM_PATH NO_CMAKE_FIND_ROOT_PATH
14 )
15 if(EXISTS BINFMT_WINE_SUPPORT_FILE)
16 file(READ ${BINFMT_WINE_SUPPORT_FILE} CONTENT)
17 if(${CONTENT} MATCHES "enabled")
18 set(HAVE_BINFMT_WINE_SUPPORT 1)
19 endif()
20 endif()
21 if(WINE_EXECUTABLE)
22 list(APPEND FOOTNOTES "NOTE: The requirements to run cross compiled applications on your host system are achieved. You may run 'make check'.")
23 endif()
24 if(NOT WINE_EXECUTABLE)
25 list(APPEND FOOTNOTES "NOTE: You may install the Windows emulator 'wine' to be able to run cross compiled test applications.")
26 endif()
27 if(NOT HAVE_BINFMT_WINE_SUPPORT)
28 list(APPEND FOOTNOTES "NOTE: You may activate binfmt_misc support for wine to be able to run cross compiled test applications directly.")
29 endif()
30 else()
31 list(APPEND FOOTNOTES "NOTE: You will not be able to run cross compiled applications on your host system.")
32 endif()
33
34 # setup z drive required by wine
35 set(Z_DRIVE_IF_WINE "z:")
36 if(DBUS_USE_WINE AND WINE_EXECUTABLE)
37 set(TEST_WRAPPER "${WINE_EXECUTABLE}")
38 endif()
39 endif()
40
41 #
42 # unit test setup
43 #
44 macro(setup_unit_tests)
45 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
46 set(DBUS_PATH_DELIMITER ";")
47 else()
48 set(DBUS_PATH_DELIMITER ":")
49 endif()
50
51 # Tests in bus/config-parser.c rely on these specific values for XDG_*
52 set(DBUS_TEST_XDG_DATA_DIRS "${Z_DRIVE_IF_WINE}${PROJECT_BINARY_DIR}/test/XDG_DATA_DIRS" "${Z_DRIVE_IF_WINE}${PROJECT_BINARY_DIR}/test/XDG_DATA_DIRS2")
53 set(DBUS_TEST_XDG_DATA_HOME "${Z_DRIVE_IF_WINE}${PROJECT_BINARY_DIR}/test/XDG_DATA_HOME")
54 set(DBUS_TEST_XDG_RUNTIME_DIR "${Z_DRIVE_IF_WINE}${PROJECT_BINARY_DIR}/test/XDG_RUNTIME_DIR")
55 list(JOIN DBUS_TEST_XDG_DATA_DIRS "${DBUS_PATH_DELIMITER}" DBUS_TEST_XDG_DATA_DIRS_JOINED)
56
57 # the test environment expects these directories to be present
58 foreach(_dir ${DBUS_TEST_XDG_RUNTIME_DIR})
59 if(NOT EXISTS ${_dir})
60 message(STATUS "creating directory '${_dir}' for test environment")
61 file(MAKE_DIRECTORY ${_dir})
62 if(NOT WIN32)
63 file(CHMOD ${_dir} DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE)
64 endif()
65 endif()
66 endforeach()
67 endmacro()
68
69 #
70 # add dbus specific test
71 #
72 # @param _name test name
73 # @param _target cmake target to use with this test
74 # @param ARGS <args> additional arguments added to the test command in front of the target file
75 # @param ENV <env> additional environment variables to provide to the running test
76 #
77 macro(add_unit_test _name _target)
78 set(options)
79 set(oneValueArgs)
80 set(multiValueArgs ARGS ENV)
81 cmake_parse_arguments(_ "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
82 add_test(
83 NAME ${_name}
84 COMMAND ${TEST_WRAPPER} ${__ARGS} ${Z_DRIVE_IF_WINE}$<TARGET_FILE:${_target}> --tap
85 WORKING_DIRECTORY ${DBUS_TEST_WORKING_DIR}
86 )
87
88 if(NOT setup_unit_tests_called)
89 setup_unit_tests()
90 set(setup_unit_test_called 1)
91 endif()
92
93 set(_env)
94 list(APPEND _env "DBUS_SESSION_BUS_ADDRESS=")
95 list(APPEND _env "DBUS_FATAL_WARNINGS=1")
96 list(APPEND _env "DBUS_TEST_BUILDDIR=${Z_DRIVE_IF_WINE}${PROJECT_BINARY_DIR}/test")
97 list(APPEND _env "DBUS_TEST_DAEMON=${DBUS_TEST_DAEMON}")
98 list(APPEND _env "DBUS_TEST_DATA=${DBUS_TEST_DATA}")
99 list(APPEND _env "DBUS_TEST_DBUS_LAUNCH=${DBUS_TEST_DBUS_LAUNCH}")
100 list(APPEND _env "DBUS_TEST_EXEC=${DBUS_TEST_EXEC}")
101 list(APPEND _env "DBUS_TEST_HOMEDIR=${DBUS_TEST_HOMEDIR}")
102 list(APPEND _env "DBUS_TEST_UNINSTALLED=1")
103 # used by GLib-based tests to implement g_test_build_filename(), etc.
104 list(APPEND _env "G_TEST_BUILDDIR=${Z_DRIVE_IF_WINE}${PROJECT_BINARY_DIR}/test")
105 list(APPEND _env "G_TEST_SRCDIR=${Z_DRIVE_IF_WINE}${PROJECT_SOURCE_DIR}/test")
106 list(APPEND _env "XDG_DATA_DIRS=${DBUS_TEST_XDG_DATA_DIRS_JOINED}")
107 list(APPEND _env "XDG_DATA_HOME=${DBUS_TEST_XDG_DATA_HOME}")
108 list(APPEND _env "XDG_RUNTIME_DIR=${DBUS_TEST_XDG_RUNTIME_DIR}")
109 list(APPEND _env ${__ENV})
110 set_tests_properties(${_name} PROPERTIES ENVIRONMENT "${_env}")
111 endmacro()
112
113 #
114 # create executable and add an associated unit test
115 #
116 # see @ref add_helper_executable for supported parameters
117 #
118 macro(add_test_executable _target _source)
119 add_helper_executable(${_target} "${_source}" ${ARGN})
120 add_unit_test(${_target} ${_target})
121 endmacro()
122
123 #
124 # create an executable
125 #
126 # The executable file is named _target and is created
127 # from the list of files provided with _source.
128 # Other optional parameters assume that it is a
129 # library to which this executable is linked.
130 #
131 # On Windows, a manifest is added to the executable
132 # file to avoid triggering user access control (uac).
133 #
134 # @param _target target name
135 # @param _source sources to add to this target
136 #
137 macro(add_helper_executable _target _source)
138 set(_sources "${_source}")
139 if(WIN32 AND NOT MSVC)
140 # avoid triggering UAC
141 add_uac_manifest(_sources)
142 endif()
143 add_executable(${_target} ${_sources})
144 target_link_libraries(${_target} ${ARGN})
145 endmacro()
146
147 #
148 # create executable and add an associated unit test with dbus session setup
149 #
150 # see @ref add_helper_executable for supported parameters
151 #
152 macro(add_session_test_executable _target _source)
153 add_helper_executable(${_target} "${_source}" ${ARGN})
154 add_unit_test(${_target} ${_target}
155 ARGS
156 ${DBUS_TEST_RUN_SESSION}
157 --config-file=${DBUS_TEST_DATA}/valid-config-files/tmp-session.conf
158 --dbus-daemon=${DBUS_TEST_DAEMON}
159 ENV
160 "DBUS_SESSION_BUS_PID="
161 )
162 endmacro()
163
164 include(CheckCCompilerFlag)
165 include(CheckCXXCompilerFlag)
166 function(check_compiler_warning_flag _flag _result _cxx)
167 string(MAKE_C_IDENTIFIER "${_flag}" _varname)
168 # required to get errors
169 list(APPEND _flag -Werror)
170 if(_flag MATCHES "-Wformat-.*")
171 list(APPEND _flag -Wformat)
172 endif()
173 if (_cxx)
174 check_cxx_compiler_flag("${_flag}" HAVE_CXX_FLAG${_varname})
175 set(${_result} ${HAVE_CXX_FLAG${_varname}} PARENT_SCOPE)
176 else()
177 check_c_compiler_flag("${_flag}" HAVE_C_FLAG${_varname})
178 set(${_result} ${HAVE_C_FLAG${_varname}} PARENT_SCOPE)
179 endif()
180 endfunction()
181
182 # generate compiler flags from MSVC warning identifiers (e.g. '4114') or gcc warning keys (e.g. 'pointer-sign')
183 # Options:
184 # [DISABLED <list>] list of warnings to disable
185 # [ERRORS <list>] list of warnings to report as error
186 # RESULTVAR <var> variable name to get results
187 # WARNINGS <list> list of warnings to add
188 # [CXX] Check if the flag is supported using the C++ compiler instead of the C compiler
189 #
190 macro(generate_compiler_warning_flags)
191 # Support if() IN_LIST operator
192 cmake_policy(SET CMP0057 NEW)
193 set(options CXX)
194 set(oneValueArgs RESULTVAR)
195 set(multiValueArgs WARNINGS DISABLED ERRORS)
196 cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
197
198 unset(USED)
199 unset(USED_WARNINGS)
200 unset(USED_DISABLED)
201 unset(USED_UNSUPPORTED)
202
203 if(MSVC)
204 # level 1 is default
205 set(enabled_prefix "/w1")
206 set(error_prefix "/we")
207 set(disabled_prefix "/wd")
208 else()
209 set(enabled_prefix "-W")
210 set(error_prefix "-Werror=")
211 set(disabled_prefix "-Wno-")
212 endif()
213
214 set(temp)
215 foreach(warning ${ARGS_ERRORS})
216 check_compiler_warning_flag("${error_prefix}${warning}" _flag_supported "${ARGS_CXX}")
217 if(_flag_supported)
218 set(temp "${temp} ${error_prefix}${warning}")
219 list(APPEND USED ${warning})
220 elseif(NOT warning IN_LIST USED_UNSUPPORTED)
221 list(APPEND USED_UNSUPPORTED ${warning})
222 endif()
223 endforeach()
224
225 foreach(warning ${ARGS_WARNINGS})
226 if(warning IN_LIST ARGS_ERRORS)
227 message(WARNING "warning '${warning}' already specified as error, ignored")
228 elseif(warning IN_LIST ARGS_DISABLED)
229 message(WARNING "warning '${warning}' already specified as disabled, ignored")
230 elseif(NOT warning IN_LIST USED)
231 check_compiler_warning_flag("${enabled_prefix}${warning}" _flag_supported "${ARGS_CXX}")
232 if(_flag_supported)
233 set(temp "${temp} ${enabled_prefix}${warning}")
234 list(APPEND USED_WARNINGS ${warning})
235 list(APPEND USED ${warning})
236 elseif(NOT warning IN_LIST USED_UNSUPPORTED)
237 list(APPEND USED_UNSUPPORTED ${warning})
238 endif()
239 endif()
240 endforeach()
241
242 foreach(warning ${ARGS_DISABLED})
243 if(warning IN_LIST ARGS_ERRORS)
244 message(WARNING "disabled warning '${warning}' already specified as error, ignored")
245 elseif(warning IN_LIST ARGS_WARNINGS)
246 message(WARNING "disabled warning '${warning}' already specified as warning, ignored")
247 elseif(NOT warning IN_LIST USED)
248 check_compiler_warning_flag("${disabled_prefix}${warning}" _flag_supported "${ARGS_CXX}")
249 if(_flag_supported)
250 set(temp "${temp} ${disabled_prefix}${warning}")
251 list(APPEND USED_DISABLED ${warning})
252 list(APPEND USED ${warning})
253 elseif(NOT warning IN_LIST USED_UNSUPPORTED)
254 list(APPEND USED_UNSUPPORTED ${warning})
255 endif()
256 endif()
257 endforeach()
258
259 set(${ARGS_RESULTVAR} "${temp}")
260 message(STATUS "effectively used warnings for '${ARGS_RESULTVAR}': ${USED_WARNINGS}")
261 message(STATUS "effectively used disabled warnings for '${ARGS_RESULTVAR}': ${USED_DISABLED}")
262 message(STATUS "unsupported warnings for '${ARGS_RESULTVAR}': ${USED_UNSUPPORTED}")
263 endmacro()
264
265 #
266 # Avoid triggering UAC
267 #
268 # This macro adds an rc file to _sources that is
269 # linked to a target and prevents UAC from making
270 # requests for administrator access.
271 #
272 macro(add_uac_manifest _sources)
273 # 1 is the resource ID, ID_MANIFEST
274 # 24 is the resource type, RT_MANIFEST
275 # constants are used because of a bug in windres
276 # see https://stackoverflow.com/questions/33000158/embed-manifest-file-to-require-administrator-execution-level-with-mingw32
277 get_filename_component(UAC_FILE ${PROJECT_SOURCE_DIR}/tools/Win32.Manifest REALPATH)
278 set(outfile ${PROJECT_BINARY_DIR}/disable-uac.rc)
279 if(NOT EXISTS outfile)
280 file(WRITE ${outfile} "1 24 \"${UAC_FILE}\"\n")
281 endif()
282 list(APPEND ${_sources} ${outfile})
283 endmacro()
284
285 macro(add_executable_version_info _sources _name)
286 set(DBUS_VER_INTERNAL_NAME "${_name}")
287 set(DBUS_VER_ORIGINAL_NAME "${DBUS_VER_INTERNAL_NAME}${CMAKE_EXECUTABLE_SUFFIX}")
288 set(DBUS_VER_FILE_TYPE "VFT_APP")
289 configure_file(${PROJECT_SOURCE_DIR}/dbus/versioninfo.rc.in ${CMAKE_CURRENT_BINARY_DIR}/versioninfo-${DBUS_VER_INTERNAL_NAME}.rc)
290 # version info and uac manifest can be combined in a binary because they use different resource types
291 list(APPEND ${_sources} ${CMAKE_CURRENT_BINARY_DIR}/versioninfo-${DBUS_VER_INTERNAL_NAME}.rc)
292 endmacro()
293
294 macro(add_library_version_info _sources _name)
295 set(DBUS_VER_INTERNAL_NAME "${_name}")
296 set(DBUS_VER_ORIGINAL_NAME "${DBUS_VER_INTERNAL_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}")
297 set(DBUS_VER_FILE_TYPE "VFT_DLL")
298 configure_file(${PROJECT_SOURCE_DIR}/dbus/versioninfo.rc.in ${CMAKE_CURRENT_BINARY_DIR}/versioninfo-${DBUS_VER_INTERNAL_NAME}.rc)
299 # version info and uac manifest can be combined in a binary because they use different resource types
300 list(APPEND ${_sources} ${CMAKE_CURRENT_BINARY_DIR}/versioninfo-${DBUS_VER_INTERNAL_NAME}.rc)
301 endmacro()
302
303 #
304 # provide option with three states AUTO, ON, OFF
305 #
306 macro(add_auto_option _name _text _default)
307 if(NOT DEFINED ${_name})
308 set(${_name} ${_default} CACHE STRING "${_text}" FORCE)
309 else()
310 set(${_name} ${_default} CACHE STRING "${_text}")
311 endif()
312 set_property(CACHE ${_name} PROPERTY STRINGS AUTO ON OFF)
313 endmacro()
314
315
316 #
317 # Ensure that if a tristate ON/OFF/AUTO feature is set to ON,
318 # its requirements have been met. Fail with a fatal error if not.
319 #
320 # _name: name of a variable ENABLE_FOO representing a tristate ON/OFF/AUTO feature
321 # _text: human-readable description of the feature enabled by _name, for the
322 # error message
323 # _var: name of a variable representing a system property we checked for,
324 # such as an executable that must exist for the feature enabled by _name to work
325 # _vartext: what we checked for, for the error message
326 #
327 macro(check_auto_option _name _text _var _vartext)
328 set(_nameval ${${_name}})
329 set(_varval ${${_var}})
330 #message("debug: _name ${_name} ${_nameval} _var ${_var} ${_varval}")
331 if(${_nameval} AND NOT ${_nameval} STREQUAL "AUTO" AND NOT ${_varval})
332 message(FATAL_ERROR "${_text} requested but ${_vartext} not found")
333 endif()
334 endmacro()
335
336 #
337 # Provide option that takes a path
338 #
339 macro(add_path_option _name _text _default)
340 if(NOT DEFINED ${_name})
341 set(${_name} ${_default} CACHE STRING "${_text}" FORCE)
342 else()
343 set(${_name} ${_default} CACHE STRING "${_text}")
344 endif()
345 endmacro()
346
347 #
348 # create directory on install
349 #
350 macro(install_dir filepath)
351 install(CODE "
352 set(_path \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${filepath}\")
353 if(NOT EXISTS \"\${_path}\")
354 execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory \"\${_path}\")
355 message(\"-- Creating directory: \${_path}\")
356 else()
357 message(\"-- Up-to-date: \${_path}\")
358 endif()
359 ")
360 endmacro()
361
362 #
363 # create symlink on install
364 #
365 macro(install_symlink filepath sympath)
366 install(CODE "
367 set(_sympath \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${sympath}\")
368 file(REMOVE \"\${_sympath}\")
369 execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink \"${filepath}\" \"\${_sympath}\" RESULT_VARIABLE result)
370 if(NOT result)
371 message(\"-- Creating symlink: \${_sympath} -> ${filepath}\")
372 else()
373 message(FATAL ERROR \"-- Failed to create symlink: \${_sympath} -> ${filepath}\")
374 endif()
375 ")
376 endmacro()
377
378 #
379 # add system service <file> PATH <install path> LINKS [multi-user.target.wants [...]]
380 #
381 macro(add_systemd_service file)
382 set(options)
383 set(oneValueArgs PATH)
384 set(multiValueArgs LINKS)
385 cmake_parse_arguments(_ "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
386 set(_targetdir ${__PATH})
387 install(FILES ${file} DESTINATION ${_targetdir})
388 get_filename_component(_name ${file} NAME)
389 foreach(l ${__LINKS})
390 set(_linkdir ${_targetdir}/${l})
391 install_dir(${_linkdir})
392 install_symlink(../${_name} ${_linkdir}/${_name})
393 endforeach()
394 endmacro()