1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
10 This file is included by FindQt4.cmake, don't include it directly.
11 #]=======================================================================]
13 ######################################
15 # Macros for building Qt files
17 ######################################
20 macro (QT4_EXTRACT_OPTIONS _qt4_files _qt4_options _qt4_target)
23 set(_QT4_DOING_OPTIONS FALSE)
24 set(_QT4_DOING_TARGET FALSE)
25 foreach(_currentArg ${ARGN})
26 if ("x${_currentArg}" STREQUAL "xOPTIONS")
27 set(_QT4_DOING_OPTIONS TRUE)
28 elseif ("x${_currentArg}" STREQUAL "xTARGET")
29 set(_QT4_DOING_TARGET TRUE)
32 set(${_qt4_target} "${_currentArg}")
33 elseif(_QT4_DOING_OPTIONS)
34 list(APPEND ${_qt4_options} "${_currentArg}")
36 list(APPEND ${_qt4_files} "${_currentArg}")
43 # macro used to create the names of output files preserving relative dirs
44 macro (QT4_MAKE_OUTPUT_FILE infile prefix ext outfile )
45 string(LENGTH ${CMAKE_CURRENT_BINARY_DIR} _binlength)
46 string(LENGTH ${infile} _infileLength)
47 set(_checkinfile ${CMAKE_CURRENT_SOURCE_DIR})
48 if(_infileLength GREATER _binlength)
49 string(SUBSTRING "${infile}" 0 ${_binlength} _checkinfile)
50 if(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
51 file(RELATIVE_PATH rel ${CMAKE_CURRENT_BINARY_DIR} ${infile})
53 file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
56 file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
58 if(WIN32 AND rel MATCHES "^([a-zA-Z]):(.*)$") # absolute path
59 set(rel "${CMAKE_MATCH_1}_${CMAKE_MATCH_2}")
61 set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${rel}")
62 string(REPLACE ".." "__" _outfile ${_outfile})
63 get_filename_component(outpath ${_outfile} PATH)
64 get_filename_component(_outfile ${_outfile} NAME_WE)
65 file(MAKE_DIRECTORY ${outpath})
66 set(${outfile} ${outpath}/${prefix}${_outfile}.${ext})
70 macro (QT4_GET_MOC_FLAGS _moc_flags)
72 get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
74 foreach(_current ${_inc_DIRS})
75 if("${_current}" MATCHES "\\.framework/?$")
76 string(REGEX REPLACE "/[^/]+\\.framework" "" framework_path "${_current}")
77 set(${_moc_flags} ${${_moc_flags}} "-F${framework_path}")
79 set(${_moc_flags} ${${_moc_flags}} "-I${_current}")
83 get_directory_property(_defines COMPILE_DEFINITIONS)
84 foreach(_current ${_defines})
85 set(${_moc_flags} ${${_moc_flags}} "-D${_current}")
89 set(${_moc_flags} ${${_moc_flags}} -DWIN32)
95 # helper macro to set up a moc rule
96 function (QT4_CREATE_MOC_COMMAND infile outfile moc_flags moc_options moc_target)
97 # For Windows, create a parameters file to work around command line length limit
98 # Pass the parameters in a file. Set the working directory to
99 # be that containing the parameters file and reference it by
100 # just the file name. This is necessary because the moc tool on
101 # MinGW builds does not seem to handle spaces in the path to the
102 # file given with the @ syntax.
103 get_filename_component(_moc_outfile_name "${outfile}" NAME)
104 get_filename_component(_moc_outfile_dir "${outfile}" PATH)
106 set(_moc_working_dir WORKING_DIRECTORY ${_moc_outfile_dir})
108 set (_moc_parameters_file ${outfile}_parameters)
109 set (_moc_parameters ${moc_flags} ${moc_options} -o "${outfile}" "${infile}")
110 string (REPLACE ";" "\n" _moc_parameters "${_moc_parameters}")
113 set (_moc_parameters_file ${_moc_parameters_file}$<$<BOOL:$<CONFIGURATION>>:_$<CONFIGURATION>>)
114 set(targetincludes "$<TARGET_PROPERTY:${moc_target},INCLUDE_DIRECTORIES>")
115 set(targetdefines "$<TARGET_PROPERTY:${moc_target},COMPILE_DEFINITIONS>")
117 set(targetincludes "$<$<BOOL:${targetincludes}>:-I$<JOIN:${targetincludes},\n-I>\n>")
118 set(targetdefines "$<$<BOOL:${targetdefines}>:-D$<JOIN:${targetdefines},\n-D>\n>")
121 OUTPUT ${_moc_parameters_file}
122 CONTENT "${targetdefines}${targetincludes}${_moc_parameters}\n"
128 set(CMAKE_CONFIGURABLE_FILE_CONTENT "${_moc_parameters}")
129 configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
130 "${_moc_parameters_file}" @ONLY)
133 set(_moc_extra_parameters_file @${_moc_parameters_file})
134 add_custom_command(OUTPUT ${outfile}
135 COMMAND Qt4::moc ${_moc_extra_parameters_file}
136 DEPENDS ${infile} ${_moc_parameters_file}
142 macro (QT4_GENERATE_MOC infile outfile )
143 # get include dirs and flags
144 QT4_GET_MOC_FLAGS(moc_flags)
145 get_filename_component(abs_infile ${infile} ABSOLUTE)
146 set(_outfile "${outfile}")
147 if(NOT IS_ABSOLUTE "${outfile}")
148 set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${outfile}")
151 if (${ARGC} GREATER 3 AND "x${ARGV2}" STREQUAL "xTARGET")
152 set(moc_target ${ARGV3})
154 QT4_CREATE_MOC_COMMAND(${abs_infile} ${_outfile} "${moc_flags}" "" "${moc_target}")
155 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
156 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
160 # QT4_WRAP_CPP(outfiles inputfile ... )
162 macro (QT4_WRAP_CPP outfiles )
164 QT4_GET_MOC_FLAGS(moc_flags)
165 QT4_EXTRACT_OPTIONS(moc_files moc_options moc_target ${ARGN})
167 foreach (it ${moc_files})
168 get_filename_component(it ${it} ABSOLUTE)
169 QT4_MAKE_OUTPUT_FILE(${it} moc_ cxx outfile)
170 QT4_CREATE_MOC_COMMAND(${it} ${outfile} "${moc_flags}" "${moc_options}" "${moc_target}")
171 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
172 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
173 set(${outfiles} ${${outfiles}} ${outfile})
179 # QT4_WRAP_UI(outfiles inputfile ... )
181 macro (QT4_WRAP_UI outfiles )
182 QT4_EXTRACT_OPTIONS(ui_files ui_options ui_target ${ARGN})
184 foreach (it ${ui_files})
185 get_filename_component(outfile ${it} NAME_WE)
186 get_filename_component(infile ${it} ABSOLUTE)
187 set(outfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${outfile}.h)
188 add_custom_command(OUTPUT ${outfile}
190 ARGS ${ui_options} -o ${outfile} ${infile}
191 MAIN_DEPENDENCY ${infile} VERBATIM)
192 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
193 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
194 set(${outfiles} ${${outfiles}} ${outfile})
200 # QT4_ADD_RESOURCES(outfiles inputfile ... )
202 macro (QT4_ADD_RESOURCES outfiles )
203 QT4_EXTRACT_OPTIONS(rcc_files rcc_options rcc_target ${ARGN})
205 foreach (it ${rcc_files})
206 get_filename_component(outfilename ${it} NAME_WE)
207 get_filename_component(infile ${it} ABSOLUTE)
208 get_filename_component(rc_path ${infile} PATH)
209 set(outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${outfilename}.cxx)
212 if(EXISTS "${infile}")
213 # parse file for dependencies
214 # all files are absolute paths or relative to the location of the qrc file
215 file(READ "${infile}" _RC_FILE_CONTENTS)
216 string(REGEX MATCHALL "<file[^<]+" _RC_FILES "${_RC_FILE_CONTENTS}")
217 foreach(_RC_FILE ${_RC_FILES})
218 string(REGEX REPLACE "^<file[^>]*>" "" _RC_FILE "${_RC_FILE}")
219 if(NOT IS_ABSOLUTE "${_RC_FILE}")
220 set(_RC_FILE "${rc_path}/${_RC_FILE}")
222 set(_RC_DEPENDS ${_RC_DEPENDS} "${_RC_FILE}")
225 unset(_RC_FILE_CONTENTS)
226 # Since this cmake macro is doing the dependency scanning for these files,
227 # let's make a configured file and add it as a dependency so cmake is run
228 # again when dependencies need to be recomputed.
229 QT4_MAKE_OUTPUT_FILE("${infile}" "" "qrc.depends" out_depends)
230 configure_file("${infile}" "${out_depends}" COPYONLY)
232 # The .qrc file does not exist (yet). Let's add a dependency and hope
233 # that it will be generated later
237 add_custom_command(OUTPUT ${outfile}
239 ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile}
240 MAIN_DEPENDENCY ${infile}
241 DEPENDS ${_RC_DEPENDS} "${out_depends}" VERBATIM)
242 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
243 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
244 set(${outfiles} ${${outfiles}} ${outfile})
250 macro(QT4_ADD_DBUS_INTERFACE _sources _interface _basename)
251 get_filename_component(_infile ${_interface} ABSOLUTE)
252 set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
253 set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
254 set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
256 get_property(_nonamespace SOURCE ${_interface} PROPERTY NO_NAMESPACE)
263 get_property(_classname SOURCE ${_interface} PROPERTY CLASSNAME)
265 set(_params ${_params} -c ${_classname})
268 get_property(_include SOURCE ${_interface} PROPERTY INCLUDE)
270 set(_params ${_params} -i ${_include})
273 add_custom_command(OUTPUT "${_impl}" "${_header}"
274 COMMAND Qt4::qdbusxml2cpp ${_params} -p ${_basename} ${_infile}
275 DEPENDS ${_infile} VERBATIM)
277 set_property(SOURCE ${_impl} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
278 set_property(SOURCE ${_impl} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
280 QT4_GENERATE_MOC("${_header}" "${_moc}")
282 list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
283 MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
288 macro(QT4_ADD_DBUS_INTERFACES _sources)
289 foreach (_current_FILE ${ARGN})
290 get_filename_component(_infile ${_current_FILE} ABSOLUTE)
291 get_filename_component(_basename ${_current_FILE} NAME)
292 # get the part before the ".xml" suffix
293 string(TOLOWER ${_basename} _basename)
294 string(REGEX REPLACE "(.*\\.)?([^\\.]+)\\.xml" "\\2" _basename ${_basename})
295 QT4_ADD_DBUS_INTERFACE(${_sources} ${_infile} ${_basename}interface)
300 macro(QT4_GENERATE_DBUS_INTERFACE _header) # _customName OPTIONS -some -options )
301 QT4_EXTRACT_OPTIONS(_customName _qt4_dbus_options _qt4_dbus_target ${ARGN})
303 get_filename_component(_in_file ${_header} ABSOLUTE)
304 get_filename_component(_basename ${_header} NAME_WE)
307 if (IS_ABSOLUTE ${_customName})
308 get_filename_component(_containingDir ${_customName} PATH)
309 if (NOT EXISTS ${_containingDir})
310 file(MAKE_DIRECTORY "${_containingDir}")
312 set(_target ${_customName})
314 set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_customName})
317 set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.xml)
320 add_custom_command(OUTPUT ${_target}
321 COMMAND Qt4::qdbuscpp2xml ${_qt4_dbus_options} ${_in_file} -o ${_target}
322 DEPENDS ${_in_file} VERBATIM
327 macro(QT4_ADD_DBUS_ADAPTOR _sources _xml_file _include _parentClass) # _optionalBasename _optionalClassName)
328 get_filename_component(_infile ${_xml_file} ABSOLUTE)
330 unset(_optionalBasename)
331 if(${ARGC} GREATER 4)
332 set(_optionalBasename "${ARGV4}")
334 if (_optionalBasename)
335 set(_basename ${_optionalBasename} )
337 string(REGEX REPLACE "(.*[/\\.])?([^\\.]+)\\.xml" "\\2adaptor" _basename ${_infile})
338 string(TOLOWER ${_basename} _basename)
341 unset(_optionalClassName)
342 if(${ARGC} GREATER 5)
343 set(_optionalClassName "${ARGV5}")
345 set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
346 set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
347 set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
349 if(_optionalClassName)
350 add_custom_command(OUTPUT "${_impl}" "${_header}"
351 COMMAND Qt4::qdbusxml2cpp -m -a ${_basename} -c ${_optionalClassName} -i ${_include} -l ${_parentClass} ${_infile}
352 DEPENDS ${_infile} VERBATIM
355 add_custom_command(OUTPUT "${_impl}" "${_header}"
356 COMMAND Qt4::qdbusxml2cpp -m -a ${_basename} -i ${_include} -l ${_parentClass} ${_infile}
357 DEPENDS ${_infile} VERBATIM
361 QT4_GENERATE_MOC("${_header}" "${_moc}")
362 set_property(SOURCE ${_impl} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
363 set_property(SOURCE ${_impl} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
364 MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
366 list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
371 if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
372 message(DEPRECATION "The qt4_automoc macro is obsolete. Use the CMAKE_AUTOMOC feature instead.")
374 QT4_GET_MOC_FLAGS(_moc_INCS)
376 set(_matching_FILES )
377 foreach (_current_FILE ${ARGN})
379 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
380 # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
381 # This is required to make uic work correctly:
382 # we need to add generated .cpp files to the sources (to compile them),
383 # but we cannot let automoc handle them, as the .cpp files don't exist yet when
384 # cmake is run for the very first time on them -> however the .cpp files might
385 # exist at a later run. at that time we need to skip them, so that we don't add two
386 # different rules for the same moc file
387 get_property(_skip SOURCE ${_abs_FILE} PROPERTY SKIP_AUTOMOC)
389 if ( NOT _skip AND EXISTS ${_abs_FILE} )
391 file(READ ${_abs_FILE} _contents)
393 get_filename_component(_abs_PATH ${_abs_FILE} PATH)
395 string(REGEX MATCHALL "# *include +[^ ]+\\.moc[\">]" _match "${_contents}")
397 foreach (_current_MOC_INC ${_match})
398 string(REGEX MATCH "[^ <\"]+\\.moc" _current_MOC "${_current_MOC_INC}")
400 get_filename_component(_basename ${_current_MOC} NAME_WE)
401 if(EXISTS ${_abs_PATH}/${_basename}.hpp)
402 set(_header ${_abs_PATH}/${_basename}.hpp)
404 set(_header ${_abs_PATH}/${_basename}.h)
406 set(_moc ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
407 QT4_CREATE_MOC_COMMAND(${_header} ${_moc} "${_moc_INCS}" "" "")
408 MACRO_ADD_FILE_DEPENDENCIES(${_abs_FILE} ${_moc})
416 macro(QT4_CREATE_TRANSLATION _qm_files)
417 QT4_EXTRACT_OPTIONS(_lupdate_files _lupdate_options _lupdate_target ${ARGN})
422 foreach (_file ${_lupdate_files})
423 get_filename_component(_ext ${_file} EXT)
424 get_filename_component(_abs_FILE ${_file} ABSOLUTE)
425 if(_ext MATCHES "ts")
426 list(APPEND _my_tsfiles ${_abs_FILE})
429 list(APPEND _my_dirs ${_abs_FILE})
431 list(APPEND _my_sources ${_abs_FILE})
435 foreach(_ts_file ${_my_tsfiles})
437 # make a .pro file to call lupdate on, so we don't make our commands too
438 # long for some systems
439 get_filename_component(_ts_name ${_ts_file} NAME)
440 set(_ts_pro ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_ts_name}_lupdate.pro)
442 foreach(_pro_src ${_my_sources})
443 string(APPEND _pro_srcs " \\\n \"${_pro_src}\"")
446 get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
447 list(REMOVE_DUPLICATES _inc_DIRS)
448 foreach(_pro_include ${_inc_DIRS})
449 get_filename_component(_abs_include "${_pro_include}" ABSOLUTE)
450 string(APPEND _pro_includes " \\\n \"${_abs_include}\"")
452 file(GENERATE OUTPUT ${_ts_pro} CONTENT "SOURCES =${_pro_srcs}\nINCLUDEPATH =${_pro_includes}\n")
454 add_custom_command(OUTPUT ${_ts_file}
456 ARGS ${_lupdate_options} ${_ts_pro} ${_my_dirs} -ts ${_ts_file}
457 DEPENDS ${_my_sources} ${_ts_pro} VERBATIM)
459 QT4_ADD_TRANSLATION(${_qm_files} ${_my_tsfiles})
463 macro(QT4_ADD_TRANSLATION _qm_files)
464 foreach (_current_FILE ${ARGN})
465 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
466 get_filename_component(qm ${_abs_FILE} NAME)
467 # everything before the last dot has to be considered the file name (including other dots)
468 string(REGEX REPLACE "\\.[^.]*$" "" FILE_NAME ${qm})
469 get_source_file_property(output_location ${_abs_FILE} OUTPUT_LOCATION)
471 file(MAKE_DIRECTORY "${output_location}")
472 set(qm "${output_location}/${FILE_NAME}.qm")
474 set(qm "${CMAKE_CURRENT_BINARY_DIR}/${FILE_NAME}.qm")
477 add_custom_command(OUTPUT ${qm}
478 COMMAND Qt4::lrelease
479 ARGS ${_abs_FILE} -qm ${qm}
480 DEPENDS ${_abs_FILE} VERBATIM
482 set(${_qm_files} ${${_qm_files}} ${qm})
486 function(qt4_use_modules _target _link_type)
487 if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
488 message(DEPRECATION "The qt4_use_modules function is obsolete. Use target_link_libraries with IMPORTED targets instead.")
490 if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE")
492 set(link_type ${_link_type})
494 set(modules ${_link_type} ${ARGN})
496 foreach(_module ${modules})
497 string(TOUPPER ${_module} _ucmodule)
498 set(_targetPrefix QT_QT${_ucmodule})
499 if (_ucmodule STREQUAL QAXCONTAINER OR _ucmodule STREQUAL QAXSERVER)
500 if (NOT QT_Q${_ucmodule}_FOUND)
501 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
503 set(_targetPrefix QT_Q${_ucmodule})
505 if (NOT QT_QT${_ucmodule}_FOUND)
506 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
508 if ("${_ucmodule}" STREQUAL "MAIN")
509 message(FATAL_ERROR "Can not use \"${_module}\" module with qt4_use_modules.")
512 target_link_libraries(${_target} ${link_type} ${${_targetPrefix}_LIBRARIES})
513 set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${${_targetPrefix}_INCLUDE_DIR} ${QT_HEADERS_DIR} ${QT_MKSPECS_DIR}/default)
514 set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${${_targetPrefix}_COMPILE_DEFINITIONS})