Qt4Macros.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. # Since this cmake macro is doing the dependency scanning for these files,
  168. # let's make a configured file and add it as a dependency so cmake is run
  169. # again when dependencies need to be recomputed.
  170. QT4_MAKE_OUTPUT_FILE("${infile}" "" "qrc.depends" out_depends)
  171. configure_file("${infile}" "${out_depends}" COPY_ONLY)
  172. else()
  173. # The .qrc file does not exist (yet). Let's add a dependency and hope
  174. # that it will be generated later
  175. set(out_depends)
  176. endif()
  177. add_custom_command(OUTPUT ${outfile}
  178. COMMAND ${QT_RCC_EXECUTABLE}
  179. ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile}
  180. MAIN_DEPENDENCY ${infile}
  181. DEPENDS ${_RC_DEPENDS} "${out_depends}" VERBATIM)
  182. set(${outfiles} ${${outfiles}} ${outfile})
  183. endforeach ()
  184. endmacro ()
  185. macro(QT4_ADD_DBUS_INTERFACE _sources _interface _basename)
  186. get_filename_component(_infile ${_interface} ABSOLUTE)
  187. set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
  188. set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
  189. set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
  190. get_source_file_property(_nonamespace ${_interface} NO_NAMESPACE)
  191. if(_nonamespace)
  192. set(_params -N -m)
  193. else()
  194. set(_params -m)
  195. endif()
  196. get_source_file_property(_classname ${_interface} CLASSNAME)
  197. if(_classname)
  198. set(_params ${_params} -c ${_classname})
  199. endif()
  200. get_source_file_property(_include ${_interface} INCLUDE)
  201. if(_include)
  202. set(_params ${_params} -i ${_include})
  203. endif()
  204. add_custom_command(OUTPUT "${_impl}" "${_header}"
  205. COMMAND ${QT_DBUSXML2CPP_EXECUTABLE} ${_params} -p ${_basename} ${_infile}
  206. DEPENDS ${_infile} VERBATIM)
  207. set_source_files_properties("${_impl}" PROPERTIES SKIP_AUTOMOC TRUE)
  208. QT4_GENERATE_MOC("${_header}" "${_moc}")
  209. list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
  210. MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
  211. endmacro()
  212. macro(QT4_ADD_DBUS_INTERFACES _sources)
  213. foreach (_current_FILE ${ARGN})
  214. get_filename_component(_infile ${_current_FILE} ABSOLUTE)
  215. get_filename_component(_basename ${_current_FILE} NAME)
  216. # get the part before the ".xml" suffix
  217. string(TOLOWER ${_basename} _basename)
  218. string(REGEX REPLACE "(.*\\.)?([^\\.]+)\\.xml" "\\2" _basename ${_basename})
  219. QT4_ADD_DBUS_INTERFACE(${_sources} ${_infile} ${_basename}interface)
  220. endforeach ()
  221. endmacro()
  222. macro(QT4_GENERATE_DBUS_INTERFACE _header) # _customName OPTIONS -some -options )
  223. QT4_EXTRACT_OPTIONS(_customName _qt4_dbus_options ${ARGN})
  224. get_filename_component(_in_file ${_header} ABSOLUTE)
  225. get_filename_component(_basename ${_header} NAME_WE)
  226. if (_customName)
  227. if (IS_ABSOLUTE ${_customName})
  228. get_filename_component(_containingDir ${_customName} PATH)
  229. if (NOT EXISTS ${_containingDir})
  230. file(MAKE_DIRECTORY "${_containingDir}")
  231. endif()
  232. set(_target ${_customName})
  233. else()
  234. set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_customName})
  235. endif()
  236. else ()
  237. set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.xml)
  238. endif ()
  239. add_custom_command(OUTPUT ${_target}
  240. COMMAND ${QT_DBUSCPP2XML_EXECUTABLE} ${_qt4_dbus_options} ${_in_file} -o ${_target}
  241. DEPENDS ${_in_file} VERBATIM
  242. )
  243. endmacro()
  244. macro(QT4_ADD_DBUS_ADAPTOR _sources _xml_file _include _parentClass) # _optionalBasename _optionalClassName)
  245. get_filename_component(_infile ${_xml_file} ABSOLUTE)
  246. set(_optionalBasename "${ARGV4}")
  247. if (_optionalBasename)
  248. set(_basename ${_optionalBasename} )
  249. else ()
  250. string(REGEX REPLACE "(.*[/\\.])?([^\\.]+)\\.xml" "\\2adaptor" _basename ${_infile})
  251. string(TOLOWER ${_basename} _basename)
  252. endif ()
  253. set(_optionalClassName "${ARGV5}")
  254. set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
  255. set(_impl "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
  256. set(_moc "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
  257. if(_optionalClassName)
  258. add_custom_command(OUTPUT "${_impl}" "${_header}"
  259. COMMAND ${QT_DBUSXML2CPP_EXECUTABLE} -m -a ${_basename} -c ${_optionalClassName} -i ${_include} -l ${_parentClass} ${_infile}
  260. DEPENDS ${_infile} VERBATIM
  261. )
  262. else()
  263. add_custom_command(OUTPUT "${_impl}" "${_header}"
  264. COMMAND ${QT_DBUSXML2CPP_EXECUTABLE} -m -a ${_basename} -i ${_include} -l ${_parentClass} ${_infile}
  265. DEPENDS ${_infile} VERBATIM
  266. )
  267. endif()
  268. QT4_GENERATE_MOC("${_header}" "${_moc}")
  269. set_source_files_properties("${_impl}" PROPERTIES SKIP_AUTOMOC TRUE)
  270. MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
  271. list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
  272. endmacro()
  273. macro(QT4_AUTOMOC)
  274. QT4_GET_MOC_FLAGS(_moc_INCS)
  275. set(_matching_FILES )
  276. foreach (_current_FILE ${ARGN})
  277. get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
  278. # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
  279. # This is required to make uic work correctly:
  280. # we need to add generated .cpp files to the sources (to compile them),
  281. # but we cannot let automoc handle them, as the .cpp files don't exist yet when
  282. # cmake is run for the very first time on them -> however the .cpp files might
  283. # exist at a later run. at that time we need to skip them, so that we don't add two
  284. # different rules for the same moc file
  285. get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
  286. if ( NOT _skip AND EXISTS ${_abs_FILE} )
  287. file(READ ${_abs_FILE} _contents)
  288. get_filename_component(_abs_PATH ${_abs_FILE} PATH)
  289. string(REGEX MATCHALL "# *include +[^ ]+\\.moc[\">]" _match "${_contents}")
  290. if(_match)
  291. foreach (_current_MOC_INC ${_match})
  292. string(REGEX MATCH "[^ <\"]+\\.moc" _current_MOC "${_current_MOC_INC}")
  293. get_filename_component(_basename ${_current_MOC} NAME_WE)
  294. if(EXISTS ${_abs_PATH}/${_basename}.hpp)
  295. set(_header ${_abs_PATH}/${_basename}.hpp)
  296. else()
  297. set(_header ${_abs_PATH}/${_basename}.h)
  298. endif()
  299. set(_moc ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
  300. QT4_CREATE_MOC_COMMAND(${_header} ${_moc} "${_moc_INCS}" "")
  301. MACRO_ADD_FILE_DEPENDENCIES(${_abs_FILE} ${_moc})
  302. endforeach ()
  303. endif()
  304. endif ()
  305. endforeach ()
  306. endmacro()
  307. macro(QT4_CREATE_TRANSLATION _qm_files)
  308. QT4_EXTRACT_OPTIONS(_lupdate_files _lupdate_options ${ARGN})
  309. set(_my_sources)
  310. set(_my_dirs)
  311. set(_my_tsfiles)
  312. set(_ts_pro)
  313. foreach (_file ${_lupdate_files})
  314. get_filename_component(_ext ${_file} EXT)
  315. get_filename_component(_abs_FILE ${_file} ABSOLUTE)
  316. if(_ext MATCHES "ts")
  317. list(APPEND _my_tsfiles ${_abs_FILE})
  318. else()
  319. if(NOT _ext)
  320. list(APPEND _my_dirs ${_abs_FILE})
  321. else()
  322. list(APPEND _my_sources ${_abs_FILE})
  323. endif()
  324. endif()
  325. endforeach()
  326. foreach(_ts_file ${_my_tsfiles})
  327. if(_my_sources)
  328. # make a .pro file to call lupdate on, so we don't make our commands too
  329. # long for some systems
  330. get_filename_component(_ts_name ${_ts_file} NAME_WE)
  331. set(_ts_pro ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_ts_name}_lupdate.pro)
  332. set(_pro_srcs)
  333. foreach(_pro_src ${_my_sources})
  334. set(_pro_srcs "${_pro_srcs} \"${_pro_src}\"")
  335. endforeach()
  336. set(_pro_includes)
  337. get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
  338. foreach(_pro_include ${_inc_DIRS})
  339. get_filename_component(_abs_include "${_pro_include}" ABSOLUTE)
  340. set(_pro_includes "${_pro_includes} \"${_abs_include}\"")
  341. endforeach()
  342. file(WRITE ${_ts_pro} "SOURCES = ${_pro_srcs}\nINCLUDEPATH = ${_pro_includes}\n")
  343. endif()
  344. add_custom_command(OUTPUT ${_ts_file}
  345. COMMAND ${QT_LUPDATE_EXECUTABLE}
  346. ARGS ${_lupdate_options} ${_ts_pro} ${_my_dirs} -ts ${_ts_file}
  347. DEPENDS ${_my_sources} ${_ts_pro} VERBATIM)
  348. endforeach()
  349. QT4_ADD_TRANSLATION(${_qm_files} ${_my_tsfiles})
  350. endmacro()
  351. macro(QT4_ADD_TRANSLATION _qm_files)
  352. foreach (_current_FILE ${ARGN})
  353. get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
  354. get_filename_component(qm ${_abs_FILE} NAME_WE)
  355. get_source_file_property(output_location ${_abs_FILE} OUTPUT_LOCATION)
  356. if(output_location)
  357. file(MAKE_DIRECTORY "${output_location}")
  358. set(qm "${output_location}/${qm}.qm")
  359. else()
  360. set(qm "${CMAKE_CURRENT_BINARY_DIR}/${qm}.qm")
  361. endif()
  362. add_custom_command(OUTPUT ${qm}
  363. COMMAND ${QT_LRELEASE_EXECUTABLE}
  364. ARGS ${_abs_FILE} -qm ${qm}
  365. DEPENDS ${_abs_FILE} VERBATIM
  366. )
  367. set(${_qm_files} ${${_qm_files}} ${qm})
  368. endforeach ()
  369. endmacro()