Qt4Macros.cmake 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. # This file is included by FindQt4.cmake, don't include it directly.
  2. #=============================================================================
  3. # Copyright 2005-2009 Kitware, Inc.
  4. #
  5. # Distributed under the OSI-approved BSD License (the "License");
  6. # see accompanying file Copyright.txt for details.
  7. #
  8. # This software is distributed WITHOUT ANY WARRANTY; without even the
  9. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. # See the License for more information.
  11. #=============================================================================
  12. # (To distribute this file outside of CMake, substitute the full
  13. # License text for the above reference.)
  14. ######################################
  15. #
  16. # Macros for building Qt files
  17. #
  18. ######################################
  19. macro (QT4_EXTRACT_OPTIONS _qt4_files _qt4_options)
  20. set(${_qt4_files})
  21. set(${_qt4_options})
  22. set(_QT4_DOING_OPTIONS FALSE)
  23. foreach(_currentArg ${ARGN})
  24. if ("${_currentArg}" STREQUAL "OPTIONS")
  25. set(_QT4_DOING_OPTIONS TRUE)
  26. else ()
  27. if(_QT4_DOING_OPTIONS)
  28. list(APPEND ${_qt4_options} "${_currentArg}")
  29. else()
  30. list(APPEND ${_qt4_files} "${_currentArg}")
  31. endif()
  32. endif ()
  33. endforeach()
  34. endmacro ()
  35. # macro used to create the names of output files preserving relative dirs
  36. macro (QT4_MAKE_OUTPUT_FILE infile prefix ext outfile )
  37. string(LENGTH ${CMAKE_CURRENT_BINARY_DIR} _binlength)
  38. string(LENGTH ${infile} _infileLength)
  39. set(_checkinfile ${CMAKE_CURRENT_SOURCE_DIR})
  40. if(_infileLength GREATER _binlength)
  41. string(SUBSTRING "${infile}" 0 ${_binlength} _checkinfile)
  42. if(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
  43. file(RELATIVE_PATH rel ${CMAKE_CURRENT_BINARY_DIR} ${infile})
  44. else()
  45. file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
  46. endif()
  47. else()
  48. file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
  49. endif()
  50. if(WIN32 AND rel MATCHES "^[a-zA-Z]:") # absolute path
  51. string(REGEX REPLACE "^([a-zA-Z]):(.*)$" "\\1_\\2" rel "${rel}")
  52. endif()
  53. set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${rel}")
  54. string(REPLACE ".." "__" _outfile ${_outfile})
  55. get_filename_component(outpath ${_outfile} PATH)
  56. get_filename_component(_outfile ${_outfile} NAME_WE)
  57. file(MAKE_DIRECTORY ${outpath})
  58. set(${outfile} ${outpath}/${prefix}${_outfile}.${ext})
  59. endmacro ()
  60. macro (QT4_GET_MOC_FLAGS _moc_flags)
  61. set(${_moc_flags})
  62. get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
  63. foreach(_current ${_inc_DIRS})
  64. if("${_current}" MATCHES "\\.framework/?$")
  65. string(REGEX REPLACE "/[^/]+\\.framework" "" framework_path "${_current}")
  66. set(${_moc_flags} ${${_moc_flags}} "-F${framework_path}")
  67. else()
  68. set(${_moc_flags} ${${_moc_flags}} "-I${_current}")
  69. endif()
  70. endforeach()
  71. get_directory_property(_defines COMPILE_DEFINITIONS)
  72. foreach(_current ${_defines})
  73. set(${_moc_flags} ${${_moc_flags}} "-D${_current}")
  74. endforeach()
  75. if(Q_WS_WIN)
  76. set(${_moc_flags} ${${_moc_flags}} -DWIN32)
  77. endif()
  78. endmacro()
  79. # helper macro to set up a moc rule
  80. macro (QT4_CREATE_MOC_COMMAND infile outfile moc_flags moc_options)
  81. # For Windows, create a parameters file to work around command line length limit
  82. if (WIN32)
  83. # Pass the parameters in a file. Set the working directory to
  84. # be that containing the parameters file and reference it by
  85. # just the file name. This is necessary because the moc tool on
  86. # MinGW builds does not seem to handle spaces in the path to the
  87. # file given with the @ syntax.
  88. get_filename_component(_moc_outfile_name "${outfile}" NAME)
  89. get_filename_component(_moc_outfile_dir "${outfile}" PATH)
  90. if(_moc_outfile_dir)
  91. set(_moc_working_dir WORKING_DIRECTORY ${_moc_outfile_dir})
  92. endif()
  93. set (_moc_parameters_file ${outfile}_parameters)
  94. set (_moc_parameters ${moc_flags} ${moc_options} -o "${outfile}" "${infile}")
  95. string (REPLACE ";" "\n" _moc_parameters "${_moc_parameters}")
  96. file (WRITE ${_moc_parameters_file} "${_moc_parameters}")
  97. add_custom_command(OUTPUT ${outfile}
  98. COMMAND ${QT_MOC_EXECUTABLE} @${_moc_outfile_name}_parameters
  99. DEPENDS ${infile}
  100. ${_moc_working_dir}
  101. VERBATIM)
  102. else ()
  103. add_custom_command(OUTPUT ${outfile}
  104. COMMAND ${QT_MOC_EXECUTABLE}
  105. ARGS ${moc_flags} ${moc_options} -o ${outfile} ${infile}
  106. DEPENDS ${infile} VERBATIM)
  107. endif ()
  108. endmacro ()
  109. macro (QT4_GENERATE_MOC infile outfile )
  110. # get include dirs and flags
  111. QT4_GET_MOC_FLAGS(moc_flags)
  112. get_filename_component(abs_infile ${infile} ABSOLUTE)
  113. set(_outfile "${outfile}")
  114. if(NOT IS_ABSOLUTE "${outfile}")
  115. set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${outfile}")
  116. endif()
  117. QT4_CREATE_MOC_COMMAND(${abs_infile} ${_outfile} "${moc_flags}" "")
  118. set_source_files_properties(${outfile} PROPERTIES SKIP_AUTOMOC TRUE) # dont run automoc on this file
  119. endmacro ()
  120. # QT4_WRAP_CPP(outfiles inputfile ... )
  121. macro (QT4_WRAP_CPP outfiles )
  122. # get include dirs
  123. QT4_GET_MOC_FLAGS(moc_flags)
  124. QT4_EXTRACT_OPTIONS(moc_files moc_options ${ARGN})
  125. foreach (it ${moc_files})
  126. get_filename_component(it ${it} ABSOLUTE)
  127. QT4_MAKE_OUTPUT_FILE(${it} moc_ cxx outfile)
  128. QT4_CREATE_MOC_COMMAND(${it} ${outfile} "${moc_flags}" "${moc_options}")
  129. set(${outfiles} ${${outfiles}} ${outfile})
  130. endforeach()
  131. endmacro ()
  132. # QT4_WRAP_UI(outfiles inputfile ... )
  133. macro (QT4_WRAP_UI outfiles )
  134. QT4_EXTRACT_OPTIONS(ui_files ui_options ${ARGN})
  135. foreach (it ${ui_files})
  136. get_filename_component(outfile ${it} NAME_WE)
  137. get_filename_component(infile ${it} ABSOLUTE)
  138. set(outfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${outfile}.h)
  139. add_custom_command(OUTPUT ${outfile}
  140. COMMAND ${QT_UIC_EXECUTABLE}
  141. ARGS ${ui_options} -o ${outfile} ${infile}
  142. MAIN_DEPENDENCY ${infile} VERBATIM)
  143. set(${outfiles} ${${outfiles}} ${outfile})
  144. endforeach ()
  145. endmacro ()
  146. # QT4_ADD_RESOURCES(outfiles inputfile ... )
  147. macro (QT4_ADD_RESOURCES outfiles )
  148. QT4_EXTRACT_OPTIONS(rcc_files rcc_options ${ARGN})
  149. foreach (it ${rcc_files})
  150. get_filename_component(outfilename ${it} NAME_WE)
  151. get_filename_component(infile ${it} ABSOLUTE)
  152. get_filename_component(rc_path ${infile} PATH)
  153. set(outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${outfilename}.cxx)
  154. set(_RC_DEPENDS)
  155. if(EXISTS "${infile}")
  156. # parse file for dependencies
  157. # all files are absolute paths or relative to the location of the qrc file
  158. file(READ "${infile}" _RC_FILE_CONTENTS)
  159. string(REGEX MATCHALL "<file[^<]+" _RC_FILES "${_RC_FILE_CONTENTS}")
  160. foreach(_RC_FILE ${_RC_FILES})
  161. string(REGEX REPLACE "^<file[^>]*>" "" _RC_FILE "${_RC_FILE}")
  162. if(NOT IS_ABSOLUTE "${_RC_FILE}")
  163. set(_RC_FILE "${rc_path}/${_RC_FILE}")
  164. endif()
  165. set(_RC_DEPENDS ${_RC_DEPENDS} "${_RC_FILE}")
  166. endforeach()
  167. unset(_RC_FILES)
  168. unset(_RC_FILE_CONTENTS)
  169. # Since this cmake macro is doing the dependency scanning for these files,
  170. # let's make a configured file and add it as a dependency so cmake is run
  171. # again when dependencies need to be recomputed.
  172. QT4_MAKE_OUTPUT_FILE("${infile}" "" "qrc.depends" out_depends)
  173. configure_file("${infile}" "${out_depends}" COPY_ONLY)
  174. else()
  175. # The .qrc file does not exist (yet). Let's add a dependency and hope
  176. # that it will be generated later
  177. set(out_depends)
  178. endif()
  179. add_custom_command(OUTPUT ${outfile}
  180. COMMAND ${QT_RCC_EXECUTABLE}
  181. ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile}
  182. MAIN_DEPENDENCY ${infile}
  183. DEPENDS ${_RC_DEPENDS} "${out_depends}" VERBATIM)
  184. set(${outfiles} ${${outfiles}} ${outfile})
  185. endforeach ()
  186. endmacro ()
  187. macro(QT4_ADD_DBUS_INTERFACE _sources _interface _basename)
  188. get_filename_component(_infile ${_interface} ABSOLUTE)
  189. set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
  190. set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
  191. set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
  192. get_source_file_property(_nonamespace ${_interface} NO_NAMESPACE)
  193. if(_nonamespace)
  194. set(_params -N -m)
  195. else()
  196. set(_params -m)
  197. endif()
  198. get_source_file_property(_classname ${_interface} CLASSNAME)
  199. if(_classname)
  200. set(_params ${_params} -c ${_classname})
  201. endif()
  202. get_source_file_property(_include ${_interface} INCLUDE)
  203. if(_include)
  204. set(_params ${_params} -i ${_include})
  205. endif()
  206. add_custom_command(OUTPUT "${_impl}" "${_header}"
  207. COMMAND ${QT_DBUSXML2CPP_EXECUTABLE} ${_params} -p ${_basename} ${_infile}
  208. DEPENDS ${_infile} VERBATIM)
  209. set_source_files_properties("${_impl}" PROPERTIES SKIP_AUTOMOC TRUE)
  210. QT4_GENERATE_MOC("${_header}" "${_moc}")
  211. list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
  212. MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
  213. endmacro()
  214. macro(QT4_ADD_DBUS_INTERFACES _sources)
  215. foreach (_current_FILE ${ARGN})
  216. get_filename_component(_infile ${_current_FILE} ABSOLUTE)
  217. get_filename_component(_basename ${_current_FILE} NAME)
  218. # get the part before the ".xml" suffix
  219. string(TOLOWER ${_basename} _basename)
  220. string(REGEX REPLACE "(.*\\.)?([^\\.]+)\\.xml" "\\2" _basename ${_basename})
  221. QT4_ADD_DBUS_INTERFACE(${_sources} ${_infile} ${_basename}interface)
  222. endforeach ()
  223. endmacro()
  224. macro(QT4_GENERATE_DBUS_INTERFACE _header) # _customName OPTIONS -some -options )
  225. QT4_EXTRACT_OPTIONS(_customName _qt4_dbus_options ${ARGN})
  226. get_filename_component(_in_file ${_header} ABSOLUTE)
  227. get_filename_component(_basename ${_header} NAME_WE)
  228. if (_customName)
  229. if (IS_ABSOLUTE ${_customName})
  230. get_filename_component(_containingDir ${_customName} PATH)
  231. if (NOT EXISTS ${_containingDir})
  232. file(MAKE_DIRECTORY "${_containingDir}")
  233. endif()
  234. set(_target ${_customName})
  235. else()
  236. set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_customName})
  237. endif()
  238. else ()
  239. set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.xml)
  240. endif ()
  241. add_custom_command(OUTPUT ${_target}
  242. COMMAND ${QT_DBUSCPP2XML_EXECUTABLE} ${_qt4_dbus_options} ${_in_file} -o ${_target}
  243. DEPENDS ${_in_file} VERBATIM
  244. )
  245. endmacro()
  246. macro(QT4_ADD_DBUS_ADAPTOR _sources _xml_file _include _parentClass) # _optionalBasename _optionalClassName)
  247. get_filename_component(_infile ${_xml_file} ABSOLUTE)
  248. set(_optionalBasename "${ARGV4}")
  249. if (_optionalBasename)
  250. set(_basename ${_optionalBasename} )
  251. else ()
  252. string(REGEX REPLACE "(.*[/\\.])?([^\\.]+)\\.xml" "\\2adaptor" _basename ${_infile})
  253. string(TOLOWER ${_basename} _basename)
  254. endif ()
  255. set(_optionalClassName "${ARGV5}")
  256. set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
  257. set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
  258. set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
  259. if(_optionalClassName)
  260. add_custom_command(OUTPUT "${_impl}" "${_header}"
  261. COMMAND ${QT_DBUSXML2CPP_EXECUTABLE} -m -a ${_basename} -c ${_optionalClassName} -i ${_include} -l ${_parentClass} ${_infile}
  262. DEPENDS ${_infile} VERBATIM
  263. )
  264. else()
  265. add_custom_command(OUTPUT "${_impl}" "${_header}"
  266. COMMAND ${QT_DBUSXML2CPP_EXECUTABLE} -m -a ${_basename} -i ${_include} -l ${_parentClass} ${_infile}
  267. DEPENDS ${_infile} VERBATIM
  268. )
  269. endif()
  270. QT4_GENERATE_MOC("${_header}" "${_moc}")
  271. set_source_files_properties("${_impl}" PROPERTIES SKIP_AUTOMOC TRUE)
  272. MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
  273. list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
  274. endmacro()
  275. macro(QT4_AUTOMOC)
  276. QT4_GET_MOC_FLAGS(_moc_INCS)
  277. set(_matching_FILES )
  278. foreach (_current_FILE ${ARGN})
  279. get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
  280. # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
  281. # This is required to make uic work correctly:
  282. # we need to add generated .cpp files to the sources (to compile them),
  283. # but we cannot let automoc handle them, as the .cpp files don't exist yet when
  284. # cmake is run for the very first time on them -> however the .cpp files might
  285. # exist at a later run. at that time we need to skip them, so that we don't add two
  286. # different rules for the same moc file
  287. get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
  288. if ( NOT _skip AND EXISTS ${_abs_FILE} )
  289. file(READ ${_abs_FILE} _contents)
  290. get_filename_component(_abs_PATH ${_abs_FILE} PATH)
  291. string(REGEX MATCHALL "# *include +[^ ]+\\.moc[\">]" _match "${_contents}")
  292. if(_match)
  293. foreach (_current_MOC_INC ${_match})
  294. string(REGEX MATCH "[^ <\"]+\\.moc" _current_MOC "${_current_MOC_INC}")
  295. get_filename_component(_basename ${_current_MOC} NAME_WE)
  296. if(EXISTS ${_abs_PATH}/${_basename}.hpp)
  297. set(_header ${_abs_PATH}/${_basename}.hpp)
  298. else()
  299. set(_header ${_abs_PATH}/${_basename}.h)
  300. endif()
  301. set(_moc ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
  302. QT4_CREATE_MOC_COMMAND(${_header} ${_moc} "${_moc_INCS}" "")
  303. MACRO_ADD_FILE_DEPENDENCIES(${_abs_FILE} ${_moc})
  304. endforeach ()
  305. endif()
  306. endif ()
  307. endforeach ()
  308. endmacro()
  309. macro(QT4_CREATE_TRANSLATION _qm_files)
  310. QT4_EXTRACT_OPTIONS(_lupdate_files _lupdate_options ${ARGN})
  311. set(_my_sources)
  312. set(_my_dirs)
  313. set(_my_tsfiles)
  314. set(_ts_pro)
  315. foreach (_file ${_lupdate_files})
  316. get_filename_component(_ext ${_file} EXT)
  317. get_filename_component(_abs_FILE ${_file} ABSOLUTE)
  318. if(_ext MATCHES "ts")
  319. list(APPEND _my_tsfiles ${_abs_FILE})
  320. else()
  321. if(NOT _ext)
  322. list(APPEND _my_dirs ${_abs_FILE})
  323. else()
  324. list(APPEND _my_sources ${_abs_FILE})
  325. endif()
  326. endif()
  327. endforeach()
  328. foreach(_ts_file ${_my_tsfiles})
  329. if(_my_sources)
  330. # make a .pro file to call lupdate on, so we don't make our commands too
  331. # long for some systems
  332. get_filename_component(_ts_name ${_ts_file} NAME_WE)
  333. set(_ts_pro ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_ts_name}_lupdate.pro)
  334. set(_pro_srcs)
  335. foreach(_pro_src ${_my_sources})
  336. set(_pro_srcs "${_pro_srcs} \"${_pro_src}\"")
  337. endforeach()
  338. set(_pro_includes)
  339. get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
  340. foreach(_pro_include ${_inc_DIRS})
  341. get_filename_component(_abs_include "${_pro_include}" ABSOLUTE)
  342. set(_pro_includes "${_pro_includes} \"${_abs_include}\"")
  343. endforeach()
  344. file(WRITE ${_ts_pro} "SOURCES = ${_pro_srcs}\nINCLUDEPATH = ${_pro_includes}\n")
  345. endif()
  346. add_custom_command(OUTPUT ${_ts_file}
  347. COMMAND ${QT_LUPDATE_EXECUTABLE}
  348. ARGS ${_lupdate_options} ${_ts_pro} ${_my_dirs} -ts ${_ts_file}
  349. DEPENDS ${_my_sources} ${_ts_pro} VERBATIM)
  350. endforeach()
  351. QT4_ADD_TRANSLATION(${_qm_files} ${_my_tsfiles})
  352. endmacro()
  353. macro(QT4_ADD_TRANSLATION _qm_files)
  354. foreach (_current_FILE ${ARGN})
  355. get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
  356. get_filename_component(qm ${_abs_FILE} NAME_WE)
  357. get_source_file_property(output_location ${_abs_FILE} OUTPUT_LOCATION)
  358. if(output_location)
  359. file(MAKE_DIRECTORY "${output_location}")
  360. set(qm "${output_location}/${qm}.qm")
  361. else()
  362. set(qm "${CMAKE_CURRENT_BINARY_DIR}/${qm}.qm")
  363. endif()
  364. add_custom_command(OUTPUT ${qm}
  365. COMMAND ${QT_LRELEASE_EXECUTABLE}
  366. ARGS ${_abs_FILE} -qm ${qm}
  367. DEPENDS ${_abs_FILE} VERBATIM
  368. )
  369. set(${_qm_files} ${${_qm_files}} ${qm})
  370. endforeach ()
  371. endmacro()
  372. function(qt4_use_modules _target _link_type)
  373. if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE")
  374. set(modules ${ARGN})
  375. set(link_type ${_link_type})
  376. else()
  377. set(modules ${_link_type} ${ARGN})
  378. endif()
  379. foreach(_module ${modules})
  380. string(TOUPPER ${_module} _ucmodule)
  381. set(_targetPrefix QT_QT${_ucmodule})
  382. if (_ucmodule STREQUAL QAXCONTAINER OR _ucmodule STREQUAL QAXSERVER)
  383. if (NOT QT_Q${_ucmodule}_FOUND)
  384. message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
  385. endif()
  386. set(_targetPrefix QT_Q${_ucmodule})
  387. else()
  388. if (NOT QT_QT${_ucmodule}_FOUND)
  389. message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
  390. endif()
  391. if ("${_ucmodule}" STREQUAL "MAIN")
  392. message(FATAL_ERROR "Can not use \"${_module}\" module with qt4_use_modules.")
  393. endif()
  394. endif()
  395. target_link_libraries(${_target} ${link_type} ${${_targetPrefix}_LIBRARIES})
  396. set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${${_targetPrefix}_INCLUDE_DIR} ${QT_HEADERS_DIR} ${QT_MKSPECS_DIR}/default)
  397. set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${${_targetPrefix}_COMPILE_DEFINITIONS})
  398. endforeach()
  399. endfunction()