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_source_files_properties(${outfile} PROPERTIES SKIP_AUTOMOC TRUE) # don't run automoc on this file
158 # QT4_WRAP_CPP(outfiles inputfile ... )
160 macro (QT4_WRAP_CPP outfiles )
162 QT4_GET_MOC_FLAGS(moc_flags)
163 QT4_EXTRACT_OPTIONS(moc_files moc_options moc_target ${ARGN})
165 foreach (it ${moc_files})
166 get_filename_component(it ${it} ABSOLUTE)
167 QT4_MAKE_OUTPUT_FILE(${it} moc_ cxx outfile)
168 QT4_CREATE_MOC_COMMAND(${it} ${outfile} "${moc_flags}" "${moc_options}" "${moc_target}")
169 set(${outfiles} ${${outfiles}} ${outfile})
175 # QT4_WRAP_UI(outfiles inputfile ... )
177 macro (QT4_WRAP_UI outfiles )
178 QT4_EXTRACT_OPTIONS(ui_files ui_options ui_target ${ARGN})
180 foreach (it ${ui_files})
181 get_filename_component(outfile ${it} NAME_WE)
182 get_filename_component(infile ${it} ABSOLUTE)
183 set(outfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${outfile}.h)
184 add_custom_command(OUTPUT ${outfile}
186 ARGS ${ui_options} -o ${outfile} ${infile}
187 MAIN_DEPENDENCY ${infile} VERBATIM)
188 set(${outfiles} ${${outfiles}} ${outfile})
194 # QT4_ADD_RESOURCES(outfiles inputfile ... )
196 macro (QT4_ADD_RESOURCES outfiles )
197 QT4_EXTRACT_OPTIONS(rcc_files rcc_options rcc_target ${ARGN})
199 foreach (it ${rcc_files})
200 get_filename_component(outfilename ${it} NAME_WE)
201 get_filename_component(infile ${it} ABSOLUTE)
202 get_filename_component(rc_path ${infile} PATH)
203 set(outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${outfilename}.cxx)
206 if(EXISTS "${infile}")
207 # parse file for dependencies
208 # all files are absolute paths or relative to the location of the qrc file
209 file(READ "${infile}" _RC_FILE_CONTENTS)
210 string(REGEX MATCHALL "<file[^<]+" _RC_FILES "${_RC_FILE_CONTENTS}")
211 foreach(_RC_FILE ${_RC_FILES})
212 string(REGEX REPLACE "^<file[^>]*>" "" _RC_FILE "${_RC_FILE}")
213 if(NOT IS_ABSOLUTE "${_RC_FILE}")
214 set(_RC_FILE "${rc_path}/${_RC_FILE}")
216 set(_RC_DEPENDS ${_RC_DEPENDS} "${_RC_FILE}")
219 unset(_RC_FILE_CONTENTS)
220 # Since this cmake macro is doing the dependency scanning for these files,
221 # let's make a configured file and add it as a dependency so cmake is run
222 # again when dependencies need to be recomputed.
223 QT4_MAKE_OUTPUT_FILE("${infile}" "" "qrc.depends" out_depends)
224 configure_file("${infile}" "${out_depends}" COPYONLY)
226 # The .qrc file does not exist (yet). Let's add a dependency and hope
227 # that it will be generated later
231 add_custom_command(OUTPUT ${outfile}
233 ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile}
234 MAIN_DEPENDENCY ${infile}
235 DEPENDS ${_RC_DEPENDS} "${out_depends}" VERBATIM)
236 set(${outfiles} ${${outfiles}} ${outfile})
242 macro(QT4_ADD_DBUS_INTERFACE _sources _interface _basename)
243 get_filename_component(_infile ${_interface} ABSOLUTE)
244 set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
245 set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
246 set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
248 get_source_file_property(_nonamespace ${_interface} NO_NAMESPACE)
255 get_source_file_property(_classname ${_interface} CLASSNAME)
257 set(_params ${_params} -c ${_classname})
260 get_source_file_property(_include ${_interface} INCLUDE)
262 set(_params ${_params} -i ${_include})
265 add_custom_command(OUTPUT "${_impl}" "${_header}"
266 COMMAND Qt4::qdbusxml2cpp ${_params} -p ${_basename} ${_infile}
267 DEPENDS ${_infile} VERBATIM)
269 set_source_files_properties("${_impl}" PROPERTIES SKIP_AUTOMOC TRUE)
271 QT4_GENERATE_MOC("${_header}" "${_moc}")
273 list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
274 MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
279 macro(QT4_ADD_DBUS_INTERFACES _sources)
280 foreach (_current_FILE ${ARGN})
281 get_filename_component(_infile ${_current_FILE} ABSOLUTE)
282 get_filename_component(_basename ${_current_FILE} NAME)
283 # get the part before the ".xml" suffix
284 string(TOLOWER ${_basename} _basename)
285 string(REGEX REPLACE "(.*\\.)?([^\\.]+)\\.xml" "\\2" _basename ${_basename})
286 QT4_ADD_DBUS_INTERFACE(${_sources} ${_infile} ${_basename}interface)
291 macro(QT4_GENERATE_DBUS_INTERFACE _header) # _customName OPTIONS -some -options )
292 QT4_EXTRACT_OPTIONS(_customName _qt4_dbus_options _qt4_dbus_target ${ARGN})
294 get_filename_component(_in_file ${_header} ABSOLUTE)
295 get_filename_component(_basename ${_header} NAME_WE)
298 if (IS_ABSOLUTE ${_customName})
299 get_filename_component(_containingDir ${_customName} PATH)
300 if (NOT EXISTS ${_containingDir})
301 file(MAKE_DIRECTORY "${_containingDir}")
303 set(_target ${_customName})
305 set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_customName})
308 set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.xml)
311 add_custom_command(OUTPUT ${_target}
312 COMMAND Qt4::qdbuscpp2xml ${_qt4_dbus_options} ${_in_file} -o ${_target}
313 DEPENDS ${_in_file} VERBATIM
318 macro(QT4_ADD_DBUS_ADAPTOR _sources _xml_file _include _parentClass) # _optionalBasename _optionalClassName)
319 get_filename_component(_infile ${_xml_file} ABSOLUTE)
321 unset(_optionalBasename)
322 if(${ARGC} GREATER 4)
323 set(_optionalBasename "${ARGV4}")
325 if (_optionalBasename)
326 set(_basename ${_optionalBasename} )
328 string(REGEX REPLACE "(.*[/\\.])?([^\\.]+)\\.xml" "\\2adaptor" _basename ${_infile})
329 string(TOLOWER ${_basename} _basename)
332 unset(_optionalClassName)
333 if(${ARGC} GREATER 5)
334 set(_optionalClassName "${ARGV5}")
336 set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
337 set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
338 set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
340 if(_optionalClassName)
341 add_custom_command(OUTPUT "${_impl}" "${_header}"
342 COMMAND Qt4::qdbusxml2cpp -m -a ${_basename} -c ${_optionalClassName} -i ${_include} -l ${_parentClass} ${_infile}
343 DEPENDS ${_infile} VERBATIM
346 add_custom_command(OUTPUT "${_impl}" "${_header}"
347 COMMAND Qt4::qdbusxml2cpp -m -a ${_basename} -i ${_include} -l ${_parentClass} ${_infile}
348 DEPENDS ${_infile} VERBATIM
352 QT4_GENERATE_MOC("${_header}" "${_moc}")
353 set_source_files_properties("${_impl}" PROPERTIES SKIP_AUTOMOC TRUE)
354 MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
356 list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
361 if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
362 message(DEPRECATION "The qt4_automoc macro is obsolete. Use the CMAKE_AUTOMOC feature instead.")
364 QT4_GET_MOC_FLAGS(_moc_INCS)
366 set(_matching_FILES )
367 foreach (_current_FILE ${ARGN})
369 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
370 # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
371 # This is required to make uic work correctly:
372 # we need to add generated .cpp files to the sources (to compile them),
373 # but we cannot let automoc handle them, as the .cpp files don't exist yet when
374 # cmake is run for the very first time on them -> however the .cpp files might
375 # exist at a later run. at that time we need to skip them, so that we don't add two
376 # different rules for the same moc file
377 get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
379 if ( NOT _skip AND EXISTS ${_abs_FILE} )
381 file(READ ${_abs_FILE} _contents)
383 get_filename_component(_abs_PATH ${_abs_FILE} PATH)
385 string(REGEX MATCHALL "# *include +[^ ]+\\.moc[\">]" _match "${_contents}")
387 foreach (_current_MOC_INC ${_match})
388 string(REGEX MATCH "[^ <\"]+\\.moc" _current_MOC "${_current_MOC_INC}")
390 get_filename_component(_basename ${_current_MOC} NAME_WE)
391 if(EXISTS ${_abs_PATH}/${_basename}.hpp)
392 set(_header ${_abs_PATH}/${_basename}.hpp)
394 set(_header ${_abs_PATH}/${_basename}.h)
396 set(_moc ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
397 QT4_CREATE_MOC_COMMAND(${_header} ${_moc} "${_moc_INCS}" "" "")
398 MACRO_ADD_FILE_DEPENDENCIES(${_abs_FILE} ${_moc})
406 macro(QT4_CREATE_TRANSLATION _qm_files)
407 QT4_EXTRACT_OPTIONS(_lupdate_files _lupdate_options _lupdate_target ${ARGN})
412 foreach (_file ${_lupdate_files})
413 get_filename_component(_ext ${_file} EXT)
414 get_filename_component(_abs_FILE ${_file} ABSOLUTE)
415 if(_ext MATCHES "ts")
416 list(APPEND _my_tsfiles ${_abs_FILE})
419 list(APPEND _my_dirs ${_abs_FILE})
421 list(APPEND _my_sources ${_abs_FILE})
425 foreach(_ts_file ${_my_tsfiles})
427 # make a .pro file to call lupdate on, so we don't make our commands too
428 # long for some systems
429 get_filename_component(_ts_name ${_ts_file} NAME_WE)
430 set(_ts_pro ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_ts_name}_lupdate.pro)
432 foreach(_pro_src ${_my_sources})
433 string(APPEND _pro_srcs " \\\n \"${_pro_src}\"")
436 get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
437 list(REMOVE_DUPLICATES _inc_DIRS)
438 foreach(_pro_include ${_inc_DIRS})
439 get_filename_component(_abs_include "${_pro_include}" ABSOLUTE)
440 string(APPEND _pro_includes " \\\n \"${_abs_include}\"")
442 file(GENERATE OUTPUT ${_ts_pro} CONTENT "SOURCES =${_pro_srcs}\nINCLUDEPATH =${_pro_includes}\n")
444 add_custom_command(OUTPUT ${_ts_file}
446 ARGS ${_lupdate_options} ${_ts_pro} ${_my_dirs} -ts ${_ts_file}
447 DEPENDS ${_my_sources} ${_ts_pro} VERBATIM)
449 QT4_ADD_TRANSLATION(${_qm_files} ${_my_tsfiles})
453 macro(QT4_ADD_TRANSLATION _qm_files)
454 foreach (_current_FILE ${ARGN})
455 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
456 get_filename_component(qm ${_abs_FILE} NAME_WE)
457 get_source_file_property(output_location ${_abs_FILE} OUTPUT_LOCATION)
459 file(MAKE_DIRECTORY "${output_location}")
460 set(qm "${output_location}/${qm}.qm")
462 set(qm "${CMAKE_CURRENT_BINARY_DIR}/${qm}.qm")
465 add_custom_command(OUTPUT ${qm}
466 COMMAND Qt4::lrelease
467 ARGS ${_abs_FILE} -qm ${qm}
468 DEPENDS ${_abs_FILE} VERBATIM
470 set(${_qm_files} ${${_qm_files}} ${qm})
474 function(qt4_use_modules _target _link_type)
475 if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
476 message(DEPRECATION "The qt4_use_modules function is obsolete. Use target_link_libraries with IMPORTED targets instead.")
478 if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE")
480 set(link_type ${_link_type})
482 set(modules ${_link_type} ${ARGN})
484 foreach(_module ${modules})
485 string(TOUPPER ${_module} _ucmodule)
486 set(_targetPrefix QT_QT${_ucmodule})
487 if (_ucmodule STREQUAL QAXCONTAINER OR _ucmodule STREQUAL QAXSERVER)
488 if (NOT QT_Q${_ucmodule}_FOUND)
489 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
491 set(_targetPrefix QT_Q${_ucmodule})
493 if (NOT QT_QT${_ucmodule}_FOUND)
494 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
496 if ("${_ucmodule}" STREQUAL "MAIN")
497 message(FATAL_ERROR "Can not use \"${_module}\" module with qt4_use_modules.")
500 target_link_libraries(${_target} ${link_type} ${${_targetPrefix}_LIBRARIES})
501 set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${${_targetPrefix}_INCLUDE_DIR} ${QT_HEADERS_DIR} ${QT_MKSPECS_DIR}/default)
502 set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${${_targetPrefix}_COMPILE_DEFINITIONS})