1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
10 # This file is included by FindQt4.cmake, don't include it directly.
12 ######################################
14 # Macros for building Qt files
16 ######################################
19 macro (QT4_EXTRACT_OPTIONS _qt4_files _qt4_options _qt4_target)
22 set(_QT4_DOING_OPTIONS FALSE)
23 set(_QT4_DOING_TARGET FALSE)
24 foreach(_currentArg ${ARGN})
25 if ("x${_currentArg}" STREQUAL "xOPTIONS")
26 set(_QT4_DOING_OPTIONS TRUE)
27 elseif ("x${_currentArg}" STREQUAL "xTARGET")
28 set(_QT4_DOING_TARGET TRUE)
31 set(${_qt4_target} "${_currentArg}")
32 elseif(_QT4_DOING_OPTIONS)
33 list(APPEND ${_qt4_options} "${_currentArg}")
35 list(APPEND ${_qt4_files} "${_currentArg}")
42 # macro used to create the names of output files preserving relative dirs
43 macro (QT4_MAKE_OUTPUT_FILE infile prefix ext outfile )
44 string(LENGTH ${CMAKE_CURRENT_BINARY_DIR} _binlength)
45 string(LENGTH ${infile} _infileLength)
46 set(_checkinfile ${CMAKE_CURRENT_SOURCE_DIR})
47 if(_infileLength GREATER _binlength)
48 string(SUBSTRING "${infile}" 0 ${_binlength} _checkinfile)
49 if(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
50 file(RELATIVE_PATH rel ${CMAKE_CURRENT_BINARY_DIR} ${infile})
52 file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
55 file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
57 if(WIN32 AND rel MATCHES "^([a-zA-Z]):(.*)$") # absolute path
58 set(rel "${CMAKE_MATCH_1}_${CMAKE_MATCH_2}")
60 set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${rel}")
61 string(REPLACE ".." "__" _outfile ${_outfile})
62 get_filename_component(outpath ${_outfile} PATH)
63 get_filename_component(_outfile ${_outfile} NAME_WE)
64 file(MAKE_DIRECTORY ${outpath})
65 set(${outfile} ${outpath}/${prefix}${_outfile}.${ext})
69 macro (QT4_GET_MOC_FLAGS _moc_flags)
71 get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
73 foreach(_current ${_inc_DIRS})
74 if("${_current}" MATCHES "\\.framework/?$")
75 string(REGEX REPLACE "/[^/]+\\.framework" "" framework_path "${_current}")
76 set(${_moc_flags} ${${_moc_flags}} "-F${framework_path}")
78 set(${_moc_flags} ${${_moc_flags}} "-I${_current}")
82 get_directory_property(_defines COMPILE_DEFINITIONS)
83 foreach(_current ${_defines})
84 set(${_moc_flags} ${${_moc_flags}} "-D${_current}")
88 set(${_moc_flags} ${${_moc_flags}} -DWIN32)
94 # helper macro to set up a moc rule
95 function (QT4_CREATE_MOC_COMMAND infile outfile moc_flags moc_options moc_target)
96 # For Windows, create a parameters file to work around command line length limit
97 # Pass the parameters in a file. Set the working directory to
98 # be that containing the parameters file and reference it by
99 # just the file name. This is necessary because the moc tool on
100 # MinGW builds does not seem to handle spaces in the path to the
101 # file given with the @ syntax.
102 get_filename_component(_moc_outfile_name "${outfile}" NAME)
103 get_filename_component(_moc_outfile_dir "${outfile}" PATH)
105 set(_moc_working_dir WORKING_DIRECTORY ${_moc_outfile_dir})
107 set (_moc_parameters_file ${outfile}_parameters)
108 set (_moc_parameters ${moc_flags} ${moc_options} -o "${outfile}" "${infile}")
109 string (REPLACE ";" "\n" _moc_parameters "${_moc_parameters}")
112 set (_moc_parameters_file ${_moc_parameters_file}$<$<BOOL:$<CONFIGURATION>>:_$<CONFIGURATION>>)
113 set(targetincludes "$<TARGET_PROPERTY:${moc_target},INCLUDE_DIRECTORIES>")
114 set(targetdefines "$<TARGET_PROPERTY:${moc_target},COMPILE_DEFINITIONS>")
116 set(targetincludes "$<$<BOOL:${targetincludes}>:-I$<JOIN:${targetincludes},\n-I>\n>")
117 set(targetdefines "$<$<BOOL:${targetdefines}>:-D$<JOIN:${targetdefines},\n-D>\n>")
120 OUTPUT ${_moc_parameters_file}
121 CONTENT "${targetdefines}${targetincludes}${_moc_parameters}\n"
127 set(CMAKE_CONFIGURABLE_FILE_CONTENT "${_moc_parameters}")
128 configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
129 "${_moc_parameters_file}" @ONLY)
132 set(_moc_extra_parameters_file @${_moc_parameters_file})
133 add_custom_command(OUTPUT ${outfile}
134 COMMAND Qt4::moc ${_moc_extra_parameters_file}
135 DEPENDS ${infile} ${_moc_parameters_file}
141 macro (QT4_GENERATE_MOC infile outfile )
142 # get include dirs and flags
143 QT4_GET_MOC_FLAGS(moc_flags)
144 get_filename_component(abs_infile ${infile} ABSOLUTE)
145 set(_outfile "${outfile}")
146 if(NOT IS_ABSOLUTE "${outfile}")
147 set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${outfile}")
150 if (${ARGC} GREATER 3 AND "x${ARGV2}" STREQUAL "xTARGET")
151 set(moc_target ${ARGV3})
153 QT4_CREATE_MOC_COMMAND(${abs_infile} ${_outfile} "${moc_flags}" "" "${moc_target}")
154 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
155 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
159 # QT4_WRAP_CPP(outfiles inputfile ... )
161 macro (QT4_WRAP_CPP outfiles )
163 QT4_GET_MOC_FLAGS(moc_flags)
164 QT4_EXTRACT_OPTIONS(moc_files moc_options moc_target ${ARGN})
166 foreach (it ${moc_files})
167 get_filename_component(it ${it} ABSOLUTE)
168 QT4_MAKE_OUTPUT_FILE(${it} moc_ cxx outfile)
169 QT4_CREATE_MOC_COMMAND(${it} ${outfile} "${moc_flags}" "${moc_options}" "${moc_target}")
170 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
171 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
172 set(${outfiles} ${${outfiles}} ${outfile})
178 # QT4_WRAP_UI(outfiles inputfile ... )
180 macro (QT4_WRAP_UI outfiles )
181 QT4_EXTRACT_OPTIONS(ui_files ui_options ui_target ${ARGN})
183 foreach (it ${ui_files})
184 get_filename_component(outfile ${it} NAME_WE)
185 get_filename_component(infile ${it} ABSOLUTE)
186 set(outfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${outfile}.h)
187 add_custom_command(OUTPUT ${outfile}
189 ARGS ${ui_options} -o ${outfile} ${infile}
190 MAIN_DEPENDENCY ${infile} VERBATIM)
191 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
192 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
193 set(${outfiles} ${${outfiles}} ${outfile})
199 # QT4_ADD_RESOURCES(outfiles inputfile ... )
201 macro (QT4_ADD_RESOURCES outfiles )
202 QT4_EXTRACT_OPTIONS(rcc_files rcc_options rcc_target ${ARGN})
204 foreach (it ${rcc_files})
205 get_filename_component(outfilename ${it} NAME_WE)
206 get_filename_component(infile ${it} ABSOLUTE)
207 get_filename_component(rc_path ${infile} PATH)
208 set(outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${outfilename}.cxx)
211 if(EXISTS "${infile}")
212 # parse file for dependencies
213 # all files are absolute paths or relative to the location of the qrc file
214 file(READ "${infile}" _RC_FILE_CONTENTS)
215 string(REGEX MATCHALL "<file[^<]+" _RC_FILES "${_RC_FILE_CONTENTS}")
216 foreach(_RC_FILE ${_RC_FILES})
217 string(REGEX REPLACE "^<file[^>]*>" "" _RC_FILE "${_RC_FILE}")
218 if(NOT IS_ABSOLUTE "${_RC_FILE}")
219 set(_RC_FILE "${rc_path}/${_RC_FILE}")
221 set(_RC_DEPENDS ${_RC_DEPENDS} "${_RC_FILE}")
224 unset(_RC_FILE_CONTENTS)
225 # Since this cmake macro is doing the dependency scanning for these files,
226 # let's make a configured file and add it as a dependency so cmake is run
227 # again when dependencies need to be recomputed.
228 QT4_MAKE_OUTPUT_FILE("${infile}" "" "qrc.depends" out_depends)
229 configure_file("${infile}" "${out_depends}" COPYONLY)
231 # The .qrc file does not exist (yet). Let's add a dependency and hope
232 # that it will be generated later
236 add_custom_command(OUTPUT ${outfile}
238 ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile}
239 MAIN_DEPENDENCY ${infile}
240 DEPENDS ${_RC_DEPENDS} "${out_depends}" VERBATIM)
241 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
242 set_property(SOURCE ${outfile} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
243 set(${outfiles} ${${outfiles}} ${outfile})
249 macro(QT4_ADD_DBUS_INTERFACE _sources _interface _basename)
250 get_filename_component(_infile ${_interface} ABSOLUTE)
251 set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
252 set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
253 set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
255 get_property(_nonamespace SOURCE ${_interface} PROPERTY NO_NAMESPACE)
262 get_property(_classname SOURCE ${_interface} PROPERTY CLASSNAME)
264 set(_params ${_params} -c ${_classname})
267 get_property(_include SOURCE ${_interface} PROPERTY INCLUDE)
269 set(_params ${_params} -i ${_include})
272 add_custom_command(OUTPUT "${_impl}" "${_header}"
273 COMMAND Qt4::qdbusxml2cpp ${_params} -p ${_basename} ${_infile}
274 DEPENDS ${_infile} VERBATIM)
276 set_property(SOURCE ${_impl} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
277 set_property(SOURCE ${_impl} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
279 QT4_GENERATE_MOC("${_header}" "${_moc}")
281 list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
282 MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
287 macro(QT4_ADD_DBUS_INTERFACES _sources)
288 foreach (_current_FILE ${ARGN})
289 get_filename_component(_infile ${_current_FILE} ABSOLUTE)
290 get_filename_component(_basename ${_current_FILE} NAME)
291 # get the part before the ".xml" suffix
292 string(TOLOWER ${_basename} _basename)
293 string(REGEX REPLACE "(.*\\.)?([^\\.]+)\\.xml" "\\2" _basename ${_basename})
294 QT4_ADD_DBUS_INTERFACE(${_sources} ${_infile} ${_basename}interface)
299 macro(QT4_GENERATE_DBUS_INTERFACE _header) # _customName OPTIONS -some -options )
300 QT4_EXTRACT_OPTIONS(_customName _qt4_dbus_options _qt4_dbus_target ${ARGN})
302 get_filename_component(_in_file ${_header} ABSOLUTE)
303 get_filename_component(_basename ${_header} NAME_WE)
306 if (IS_ABSOLUTE ${_customName})
307 get_filename_component(_containingDir ${_customName} PATH)
308 if (NOT EXISTS ${_containingDir})
309 file(MAKE_DIRECTORY "${_containingDir}")
311 set(_target ${_customName})
313 set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_customName})
316 set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.xml)
319 add_custom_command(OUTPUT ${_target}
320 COMMAND Qt4::qdbuscpp2xml ${_qt4_dbus_options} ${_in_file} -o ${_target}
321 DEPENDS ${_in_file} VERBATIM
326 macro(QT4_ADD_DBUS_ADAPTOR _sources _xml_file _include _parentClass) # _optionalBasename _optionalClassName)
327 get_filename_component(_infile ${_xml_file} ABSOLUTE)
329 unset(_optionalBasename)
330 if(${ARGC} GREATER 4)
331 set(_optionalBasename "${ARGV4}")
333 if (_optionalBasename)
334 set(_basename ${_optionalBasename} )
336 string(REGEX REPLACE "(.*[/\\.])?([^\\.]+)\\.xml" "\\2adaptor" _basename ${_infile})
337 string(TOLOWER ${_basename} _basename)
340 unset(_optionalClassName)
341 if(${ARGC} GREATER 5)
342 set(_optionalClassName "${ARGV5}")
344 set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
345 set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
346 set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
348 if(_optionalClassName)
349 add_custom_command(OUTPUT "${_impl}" "${_header}"
350 COMMAND Qt4::qdbusxml2cpp -m -a ${_basename} -c ${_optionalClassName} -i ${_include} -l ${_parentClass} ${_infile}
351 DEPENDS ${_infile} VERBATIM
354 add_custom_command(OUTPUT "${_impl}" "${_header}"
355 COMMAND Qt4::qdbusxml2cpp -m -a ${_basename} -i ${_include} -l ${_parentClass} ${_infile}
356 DEPENDS ${_infile} VERBATIM
360 QT4_GENERATE_MOC("${_header}" "${_moc}")
361 set_property(SOURCE ${_impl} PROPERTY SKIP_AUTOMOC TRUE) # don't run automoc on this file
362 set_property(SOURCE ${_impl} PROPERTY SKIP_AUTOUIC TRUE) # don't run autouic on this file
363 MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
365 list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
370 if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
371 message(DEPRECATION "The qt4_automoc macro is obsolete. Use the CMAKE_AUTOMOC feature instead.")
373 QT4_GET_MOC_FLAGS(_moc_INCS)
375 set(_matching_FILES )
376 foreach (_current_FILE ${ARGN})
378 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
379 # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
380 # This is required to make uic work correctly:
381 # we need to add generated .cpp files to the sources (to compile them),
382 # but we cannot let automoc handle them, as the .cpp files don't exist yet when
383 # cmake is run for the very first time on them -> however the .cpp files might
384 # exist at a later run. at that time we need to skip them, so that we don't add two
385 # different rules for the same moc file
386 get_property(_skip SOURCE ${_abs_FILE} PROPERTY SKIP_AUTOMOC)
388 if ( NOT _skip AND EXISTS ${_abs_FILE} )
390 file(READ ${_abs_FILE} _contents)
392 get_filename_component(_abs_PATH ${_abs_FILE} PATH)
394 string(REGEX MATCHALL "# *include +[^ ]+\\.moc[\">]" _match "${_contents}")
396 foreach (_current_MOC_INC ${_match})
397 string(REGEX MATCH "[^ <\"]+\\.moc" _current_MOC "${_current_MOC_INC}")
399 get_filename_component(_basename ${_current_MOC} NAME_WE)
400 if(EXISTS ${_abs_PATH}/${_basename}.hpp)
401 set(_header ${_abs_PATH}/${_basename}.hpp)
403 set(_header ${_abs_PATH}/${_basename}.h)
405 set(_moc ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
406 QT4_CREATE_MOC_COMMAND(${_header} ${_moc} "${_moc_INCS}" "" "")
407 MACRO_ADD_FILE_DEPENDENCIES(${_abs_FILE} ${_moc})
415 macro(QT4_CREATE_TRANSLATION _qm_files)
416 QT4_EXTRACT_OPTIONS(_lupdate_files _lupdate_options _lupdate_target ${ARGN})
421 foreach (_file ${_lupdate_files})
422 get_filename_component(_ext ${_file} EXT)
423 get_filename_component(_abs_FILE ${_file} ABSOLUTE)
424 if(_ext MATCHES "ts")
425 list(APPEND _my_tsfiles ${_abs_FILE})
428 list(APPEND _my_dirs ${_abs_FILE})
430 list(APPEND _my_sources ${_abs_FILE})
434 foreach(_ts_file ${_my_tsfiles})
436 # make a .pro file to call lupdate on, so we don't make our commands too
437 # long for some systems
438 get_filename_component(_ts_name ${_ts_file} NAME_WE)
439 set(_ts_pro ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_ts_name}_lupdate.pro)
441 foreach(_pro_src ${_my_sources})
442 string(APPEND _pro_srcs " \\\n \"${_pro_src}\"")
445 get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
446 list(REMOVE_DUPLICATES _inc_DIRS)
447 foreach(_pro_include ${_inc_DIRS})
448 get_filename_component(_abs_include "${_pro_include}" ABSOLUTE)
449 string(APPEND _pro_includes " \\\n \"${_abs_include}\"")
451 file(GENERATE OUTPUT ${_ts_pro} CONTENT "SOURCES =${_pro_srcs}\nINCLUDEPATH =${_pro_includes}\n")
453 add_custom_command(OUTPUT ${_ts_file}
455 ARGS ${_lupdate_options} ${_ts_pro} ${_my_dirs} -ts ${_ts_file}
456 DEPENDS ${_my_sources} ${_ts_pro} VERBATIM)
458 QT4_ADD_TRANSLATION(${_qm_files} ${_my_tsfiles})
462 macro(QT4_ADD_TRANSLATION _qm_files)
463 foreach (_current_FILE ${ARGN})
464 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
465 get_filename_component(qm ${_abs_FILE} NAME_WE)
466 get_property(output_location SOURCE ${_abs_FILE} PROPERTY OUTPUT_LOCATION)
468 file(MAKE_DIRECTORY "${output_location}")
469 set(qm "${output_location}/${qm}.qm")
471 set(qm "${CMAKE_CURRENT_BINARY_DIR}/${qm}.qm")
474 add_custom_command(OUTPUT ${qm}
475 COMMAND Qt4::lrelease
476 ARGS ${_abs_FILE} -qm ${qm}
477 DEPENDS ${_abs_FILE} VERBATIM
479 set(${_qm_files} ${${_qm_files}} ${qm})
483 function(qt4_use_modules _target _link_type)
484 if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
485 message(DEPRECATION "The qt4_use_modules function is obsolete. Use target_link_libraries with IMPORTED targets instead.")
487 if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE")
489 set(link_type ${_link_type})
491 set(modules ${_link_type} ${ARGN})
493 foreach(_module ${modules})
494 string(TOUPPER ${_module} _ucmodule)
495 set(_targetPrefix QT_QT${_ucmodule})
496 if (_ucmodule STREQUAL QAXCONTAINER OR _ucmodule STREQUAL QAXSERVER)
497 if (NOT QT_Q${_ucmodule}_FOUND)
498 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
500 set(_targetPrefix QT_Q${_ucmodule})
502 if (NOT QT_QT${_ucmodule}_FOUND)
503 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
505 if ("${_ucmodule}" STREQUAL "MAIN")
506 message(FATAL_ERROR "Can not use \"${_module}\" module with qt4_use_modules.")
509 target_link_libraries(${_target} ${link_type} ${${_targetPrefix}_LIBRARIES})
510 set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${${_targetPrefix}_INCLUDE_DIR} ${QT_HEADERS_DIR} ${QT_MKSPECS_DIR}/default)
511 set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${${_targetPrefix}_COMPILE_DEFINITIONS})