1 # This file is included by FindQt4.cmake, don't include it directly.
3 #=============================================================================
4 # Copyright 2005-2009 Kitware, Inc.
6 # Distributed under the OSI-approved BSD License (the "License");
7 # see accompanying file Copyright.txt for details.
9 # This software is distributed WITHOUT ANY WARRANTY; without even the
10 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # See the License for more information.
12 #=============================================================================
13 # (To distribute this file outside of CMake, substitute the full
14 # License text for the above reference.)
17 ######################################
19 # Macros for building Qt files
21 ######################################
24 macro (QT4_EXTRACT_OPTIONS _qt4_files _qt4_options _qt4_target)
27 set(_QT4_DOING_OPTIONS FALSE)
28 set(_QT4_DOING_TARGET FALSE)
29 foreach(_currentArg ${ARGN})
30 if ("x${_currentArg}" STREQUAL "xOPTIONS")
31 set(_QT4_DOING_OPTIONS TRUE)
32 elseif ("x${_currentArg}" STREQUAL "xTARGET")
33 set(_QT4_DOING_TARGET TRUE)
36 set(${_qt4_target} "${_currentArg}")
37 elseif(_QT4_DOING_OPTIONS)
38 list(APPEND ${_qt4_options} "${_currentArg}")
40 list(APPEND ${_qt4_files} "${_currentArg}")
47 # macro used to create the names of output files preserving relative dirs
48 macro (QT4_MAKE_OUTPUT_FILE infile prefix ext outfile )
49 string(LENGTH ${CMAKE_CURRENT_BINARY_DIR} _binlength)
50 string(LENGTH ${infile} _infileLength)
51 set(_checkinfile ${CMAKE_CURRENT_SOURCE_DIR})
52 if(_infileLength GREATER _binlength)
53 string(SUBSTRING "${infile}" 0 ${_binlength} _checkinfile)
54 if(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
55 file(RELATIVE_PATH rel ${CMAKE_CURRENT_BINARY_DIR} ${infile})
57 file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
60 file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
62 if(WIN32 AND rel MATCHES "^[a-zA-Z]:") # absolute path
63 string(REGEX REPLACE "^([a-zA-Z]):(.*)$" "\\1_\\2" rel "${rel}")
65 set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${rel}")
66 string(REPLACE ".." "__" _outfile ${_outfile})
67 get_filename_component(outpath ${_outfile} PATH)
68 get_filename_component(_outfile ${_outfile} NAME_WE)
69 file(MAKE_DIRECTORY ${outpath})
70 set(${outfile} ${outpath}/${prefix}${_outfile}.${ext})
74 macro (QT4_GET_MOC_FLAGS _moc_flags)
76 get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
78 foreach(_current ${_inc_DIRS})
79 if("${_current}" MATCHES "\\.framework/?$")
80 string(REGEX REPLACE "/[^/]+\\.framework" "" framework_path "${_current}")
81 set(${_moc_flags} ${${_moc_flags}} "-F${framework_path}")
83 set(${_moc_flags} ${${_moc_flags}} "-I${_current}")
87 get_directory_property(_defines COMPILE_DEFINITIONS)
88 foreach(_current ${_defines})
89 set(${_moc_flags} ${${_moc_flags}} "-D${_current}")
93 set(${_moc_flags} ${${_moc_flags}} -DWIN32)
99 # helper macro to set up a moc rule
100 macro (QT4_CREATE_MOC_COMMAND infile outfile moc_flags moc_options moc_target)
101 # For Windows, create a parameters file to work around command line length limit
102 # Pass the parameters in a file. Set the working directory to
103 # be that containing the parameters file and reference it by
104 # just the file name. This is necessary because the moc tool on
105 # MinGW builds does not seem to handle spaces in the path to the
106 # file given with the @ syntax.
107 get_filename_component(_moc_outfile_name "${outfile}" NAME)
108 get_filename_component(_moc_outfile_dir "${outfile}" PATH)
110 set(_moc_working_dir WORKING_DIRECTORY ${_moc_outfile_dir})
112 set (_moc_parameters_file ${outfile}_parameters)
113 set (_moc_parameters ${moc_flags} ${moc_options} -o "${outfile}" "${infile}")
114 string (REPLACE ";" "\n" _moc_parameters "${_moc_parameters}")
117 set(targetincludes "$<TARGET_PROPERTY:${moc_target},INCLUDE_DIRECTORIES>")
118 set(targetdefines "$<TARGET_PROPERTY:${moc_target},COMPILE_DEFINITIONS>")
120 set(targetincludes "$<$<BOOL:${targetincludes}>:-I$<JOIN:${targetincludes},\n-I>\n>")
121 set(targetdefines "$<$<BOOL:${targetdefines}>:-D$<JOIN:${targetdefines},\n-D>\n>")
124 OUTPUT ${_moc_parameters_file}
125 CONTENT "${targetdefines}${targetincludes}${_moc_parameters}\n"
131 file(WRITE ${_moc_parameters_file} "${_moc_parameters}\n")
134 set(_moc_extra_parameters_file @${_moc_parameters_file})
135 add_custom_command(OUTPUT ${outfile}
136 COMMAND ${QT_MOC_EXECUTABLE} ${_moc_extra_parameters_file}
143 macro (QT4_GENERATE_MOC infile outfile )
144 # get include dirs and flags
145 QT4_GET_MOC_FLAGS(moc_flags)
146 get_filename_component(abs_infile ${infile} ABSOLUTE)
147 set(_outfile "${outfile}")
148 if(NOT IS_ABSOLUTE "${outfile}")
149 set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${outfile}")
152 if ("x${ARGV2}" STREQUAL "xTARGET")
153 set(moc_target ${ARGV3})
155 QT4_CREATE_MOC_COMMAND(${abs_infile} ${_outfile} "${moc_flags}" "" "${moc_target}")
156 set_source_files_properties(${outfile} PROPERTIES SKIP_AUTOMOC TRUE) # dont run automoc 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(${outfiles} ${${outfiles}} ${outfile})
177 # QT4_WRAP_UI(outfiles inputfile ... )
179 macro (QT4_WRAP_UI outfiles )
180 QT4_EXTRACT_OPTIONS(ui_files ui_options ui_target ${ARGN})
182 foreach (it ${ui_files})
183 get_filename_component(outfile ${it} NAME_WE)
184 get_filename_component(infile ${it} ABSOLUTE)
185 set(outfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${outfile}.h)
186 add_custom_command(OUTPUT ${outfile}
187 COMMAND ${QT_UIC_EXECUTABLE}
188 ARGS ${ui_options} -o ${outfile} ${infile}
189 MAIN_DEPENDENCY ${infile} VERBATIM)
190 set(${outfiles} ${${outfiles}} ${outfile})
196 # QT4_ADD_RESOURCES(outfiles inputfile ... )
198 macro (QT4_ADD_RESOURCES outfiles )
199 QT4_EXTRACT_OPTIONS(rcc_files rcc_options rcc_target ${ARGN})
201 foreach (it ${rcc_files})
202 get_filename_component(outfilename ${it} NAME_WE)
203 get_filename_component(infile ${it} ABSOLUTE)
204 get_filename_component(rc_path ${infile} PATH)
205 set(outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${outfilename}.cxx)
208 if(EXISTS "${infile}")
209 # parse file for dependencies
210 # all files are absolute paths or relative to the location of the qrc file
211 file(READ "${infile}" _RC_FILE_CONTENTS)
212 string(REGEX MATCHALL "<file[^<]+" _RC_FILES "${_RC_FILE_CONTENTS}")
213 foreach(_RC_FILE ${_RC_FILES})
214 string(REGEX REPLACE "^<file[^>]*>" "" _RC_FILE "${_RC_FILE}")
215 if(NOT IS_ABSOLUTE "${_RC_FILE}")
216 set(_RC_FILE "${rc_path}/${_RC_FILE}")
218 set(_RC_DEPENDS ${_RC_DEPENDS} "${_RC_FILE}")
221 unset(_RC_FILE_CONTENTS)
222 # Since this cmake macro is doing the dependency scanning for these files,
223 # let's make a configured file and add it as a dependency so cmake is run
224 # again when dependencies need to be recomputed.
225 QT4_MAKE_OUTPUT_FILE("${infile}" "" "qrc.depends" out_depends)
226 configure_file("${infile}" "${out_depends}" COPY_ONLY)
228 # The .qrc file does not exist (yet). Let's add a dependency and hope
229 # that it will be generated later
233 add_custom_command(OUTPUT ${outfile}
234 COMMAND ${QT_RCC_EXECUTABLE}
235 ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile}
236 MAIN_DEPENDENCY ${infile}
237 DEPENDS ${_RC_DEPENDS} "${out_depends}" VERBATIM)
238 set(${outfiles} ${${outfiles}} ${outfile})
244 macro(QT4_ADD_DBUS_INTERFACE _sources _interface _basename)
245 get_filename_component(_infile ${_interface} ABSOLUTE)
246 set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
247 set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
248 set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
250 get_source_file_property(_nonamespace ${_interface} NO_NAMESPACE)
257 get_source_file_property(_classname ${_interface} CLASSNAME)
259 set(_params ${_params} -c ${_classname})
262 get_source_file_property(_include ${_interface} INCLUDE)
264 set(_params ${_params} -i ${_include})
267 add_custom_command(OUTPUT "${_impl}" "${_header}"
268 COMMAND ${QT_DBUSXML2CPP_EXECUTABLE} ${_params} -p ${_basename} ${_infile}
269 DEPENDS ${_infile} VERBATIM)
271 set_source_files_properties("${_impl}" PROPERTIES SKIP_AUTOMOC TRUE)
273 QT4_GENERATE_MOC("${_header}" "${_moc}")
275 list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
276 MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
281 macro(QT4_ADD_DBUS_INTERFACES _sources)
282 foreach (_current_FILE ${ARGN})
283 get_filename_component(_infile ${_current_FILE} ABSOLUTE)
284 get_filename_component(_basename ${_current_FILE} NAME)
285 # get the part before the ".xml" suffix
286 string(TOLOWER ${_basename} _basename)
287 string(REGEX REPLACE "(.*\\.)?([^\\.]+)\\.xml" "\\2" _basename ${_basename})
288 QT4_ADD_DBUS_INTERFACE(${_sources} ${_infile} ${_basename}interface)
293 macro(QT4_GENERATE_DBUS_INTERFACE _header) # _customName OPTIONS -some -options )
294 QT4_EXTRACT_OPTIONS(_customName _qt4_dbus_options _qt4_dbus_target ${ARGN})
296 get_filename_component(_in_file ${_header} ABSOLUTE)
297 get_filename_component(_basename ${_header} NAME_WE)
300 if (IS_ABSOLUTE ${_customName})
301 get_filename_component(_containingDir ${_customName} PATH)
302 if (NOT EXISTS ${_containingDir})
303 file(MAKE_DIRECTORY "${_containingDir}")
305 set(_target ${_customName})
307 set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_customName})
310 set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.xml)
313 add_custom_command(OUTPUT ${_target}
314 COMMAND ${QT_DBUSCPP2XML_EXECUTABLE} ${_qt4_dbus_options} ${_in_file} -o ${_target}
315 DEPENDS ${_in_file} VERBATIM
320 macro(QT4_ADD_DBUS_ADAPTOR _sources _xml_file _include _parentClass) # _optionalBasename _optionalClassName)
321 get_filename_component(_infile ${_xml_file} ABSOLUTE)
323 set(_optionalBasename "${ARGV4}")
324 if (_optionalBasename)
325 set(_basename ${_optionalBasename} )
327 string(REGEX REPLACE "(.*[/\\.])?([^\\.]+)\\.xml" "\\2adaptor" _basename ${_infile})
328 string(TOLOWER ${_basename} _basename)
331 set(_optionalClassName "${ARGV5}")
332 set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
333 set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
334 set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
336 if(_optionalClassName)
337 add_custom_command(OUTPUT "${_impl}" "${_header}"
338 COMMAND ${QT_DBUSXML2CPP_EXECUTABLE} -m -a ${_basename} -c ${_optionalClassName} -i ${_include} -l ${_parentClass} ${_infile}
339 DEPENDS ${_infile} VERBATIM
342 add_custom_command(OUTPUT "${_impl}" "${_header}"
343 COMMAND ${QT_DBUSXML2CPP_EXECUTABLE} -m -a ${_basename} -i ${_include} -l ${_parentClass} ${_infile}
344 DEPENDS ${_infile} VERBATIM
348 QT4_GENERATE_MOC("${_header}" "${_moc}")
349 set_source_files_properties("${_impl}" PROPERTIES SKIP_AUTOMOC TRUE)
350 MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
352 list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
357 if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
358 if(CMAKE_WARN_DEPRECATED)
359 set(messageType WARNING)
361 if(CMAKE_ERROR_DEPRECATED)
362 set(messageType FATAL_ERROR)
365 message(${messageType} "The qt4_automoc macro is obsolete. Use the CMAKE_AUTOMOC feature instead.")
368 QT4_GET_MOC_FLAGS(_moc_INCS)
370 set(_matching_FILES )
371 foreach (_current_FILE ${ARGN})
373 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
374 # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
375 # This is required to make uic work correctly:
376 # we need to add generated .cpp files to the sources (to compile them),
377 # but we cannot let automoc handle them, as the .cpp files don't exist yet when
378 # cmake is run for the very first time on them -> however the .cpp files might
379 # exist at a later run. at that time we need to skip them, so that we don't add two
380 # different rules for the same moc file
381 get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
383 if ( NOT _skip AND EXISTS ${_abs_FILE} )
385 file(READ ${_abs_FILE} _contents)
387 get_filename_component(_abs_PATH ${_abs_FILE} PATH)
389 string(REGEX MATCHALL "# *include +[^ ]+\\.moc[\">]" _match "${_contents}")
391 foreach (_current_MOC_INC ${_match})
392 string(REGEX MATCH "[^ <\"]+\\.moc" _current_MOC "${_current_MOC_INC}")
394 get_filename_component(_basename ${_current_MOC} NAME_WE)
395 if(EXISTS ${_abs_PATH}/${_basename}.hpp)
396 set(_header ${_abs_PATH}/${_basename}.hpp)
398 set(_header ${_abs_PATH}/${_basename}.h)
400 set(_moc ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
401 QT4_CREATE_MOC_COMMAND(${_header} ${_moc} "${_moc_INCS}" "" "")
402 MACRO_ADD_FILE_DEPENDENCIES(${_abs_FILE} ${_moc})
410 macro(QT4_CREATE_TRANSLATION _qm_files)
411 QT4_EXTRACT_OPTIONS(_lupdate_files _lupdate_options _lupdate_target ${ARGN})
416 foreach (_file ${_lupdate_files})
417 get_filename_component(_ext ${_file} EXT)
418 get_filename_component(_abs_FILE ${_file} ABSOLUTE)
419 if(_ext MATCHES "ts")
420 list(APPEND _my_tsfiles ${_abs_FILE})
423 list(APPEND _my_dirs ${_abs_FILE})
425 list(APPEND _my_sources ${_abs_FILE})
429 foreach(_ts_file ${_my_tsfiles})
431 # make a .pro file to call lupdate on, so we don't make our commands too
432 # long for some systems
433 get_filename_component(_ts_name ${_ts_file} NAME_WE)
434 set(_ts_pro ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_ts_name}_lupdate.pro)
436 foreach(_pro_src ${_my_sources})
437 set(_pro_srcs "${_pro_srcs} \"${_pro_src}\"")
440 get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
441 foreach(_pro_include ${_inc_DIRS})
442 get_filename_component(_abs_include "${_pro_include}" ABSOLUTE)
443 set(_pro_includes "${_pro_includes} \"${_abs_include}\"")
445 file(WRITE ${_ts_pro} "SOURCES = ${_pro_srcs}\nINCLUDEPATH = ${_pro_includes}\n")
447 add_custom_command(OUTPUT ${_ts_file}
448 COMMAND ${QT_LUPDATE_EXECUTABLE}
449 ARGS ${_lupdate_options} ${_ts_pro} ${_my_dirs} -ts ${_ts_file}
450 DEPENDS ${_my_sources} ${_ts_pro} VERBATIM)
452 QT4_ADD_TRANSLATION(${_qm_files} ${_my_tsfiles})
456 macro(QT4_ADD_TRANSLATION _qm_files)
457 foreach (_current_FILE ${ARGN})
458 get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
459 get_filename_component(qm ${_abs_FILE} NAME_WE)
460 get_source_file_property(output_location ${_abs_FILE} OUTPUT_LOCATION)
462 file(MAKE_DIRECTORY "${output_location}")
463 set(qm "${output_location}/${qm}.qm")
465 set(qm "${CMAKE_CURRENT_BINARY_DIR}/${qm}.qm")
468 add_custom_command(OUTPUT ${qm}
469 COMMAND ${QT_LRELEASE_EXECUTABLE}
470 ARGS ${_abs_FILE} -qm ${qm}
471 DEPENDS ${_abs_FILE} VERBATIM
473 set(${_qm_files} ${${_qm_files}} ${qm})
477 function(qt4_use_modules _target _link_type)
478 if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
479 if(CMAKE_WARN_DEPRECATED)
480 set(messageType WARNING)
482 if(CMAKE_ERROR_DEPRECATED)
483 set(messageType FATAL_ERROR)
486 message(${messageType} "The qt4_use_modules function is obsolete. Use target_link_libraries with IMPORTED targets instead.")
489 if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE")
491 set(link_type ${_link_type})
493 set(modules ${_link_type} ${ARGN})
495 foreach(_module ${modules})
496 string(TOUPPER ${_module} _ucmodule)
497 set(_targetPrefix QT_QT${_ucmodule})
498 if (_ucmodule STREQUAL QAXCONTAINER OR _ucmodule STREQUAL QAXSERVER)
499 if (NOT QT_Q${_ucmodule}_FOUND)
500 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
502 set(_targetPrefix QT_Q${_ucmodule})
504 if (NOT QT_QT${_ucmodule}_FOUND)
505 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
507 if ("${_ucmodule}" STREQUAL "MAIN")
508 message(FATAL_ERROR "Can not use \"${_module}\" module with qt4_use_modules.")
511 target_link_libraries(${_target} ${link_type} ${${_targetPrefix}_LIBRARIES})
512 set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${${_targetPrefix}_INCLUDE_DIR} ${QT_HEADERS_DIR} ${QT_MKSPECS_DIR}/default)
513 set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${${_targetPrefix}_COMPILE_DEFINITIONS})