Automoc4Config.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. # It also adds the following macros
  2. # AUTOMOC4(<target> <SRCS_VAR>)
  3. # Use this to run automoc4 on all files contained in the list <SRCS_VAR>.
  4. #
  5. # AUTOMOC4_MOC_HEADERS(<target> header1.h header2.h ...)
  6. # Use this to add more header files to be processed with automoc4.
  7. #
  8. # AUTOMOC4_ADD_EXECUTABLE(<target_NAME> src1 src2 ...)
  9. # This macro does the same as ADD_EXECUTABLE, but additionally
  10. # adds automoc4 handling for all source files.
  11. #
  12. # AUTOMOC4_ADD_LIBRARY(<target_NAME> src1 src2 ...)
  13. # This macro does the same as ADD_LIBRARY, but additionally
  14. # adds automoc4 handling for all source files.
  15. # Internal helper macro, may change or be removed anytime:
  16. # _ADD_AUTOMOC4_TARGET(<target_NAME> <SRCS_VAR>)
  17. #
  18. # Since version 0.9.88:
  19. # The following two macros are only to be used for KDE4 projects
  20. # and do something which makes sure automoc4 works for KDE. Don't
  21. # use them anywhere else. See kdelibs/cmake/modules/KDE4Macros.cmake.
  22. # _AUTOMOC4_KDE4_PRE_TARGET_HANDLING(<target_NAME> <SRCS_VAR>)
  23. # _AUTOMOC4_KDE4_POST_TARGET_HANDLING(<target_NAME>)
  24. # Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
  25. # Copyright (C) 2008-2009 Alexander Neundorf <neundorf@kde.org>
  26. #
  27. # Redistribution and use in source and binary forms, with or without
  28. # modification, are permitted provided that the following conditions
  29. # are met:
  30. #
  31. # 1. Redistributions of source code must retain the above copyright
  32. # notice, this list of conditions and the following disclaimer.
  33. # 2. Redistributions in binary form must reproduce the above copyright
  34. # notice, this list of conditions and the following disclaimer in the
  35. # documentation and/or other materials provided with the distribution.
  36. #
  37. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  38. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  39. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  40. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  41. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  46. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. # push the current cmake policy settings on the policy stack and pop them again at the
  48. # end of this file, so any policies which are changed in this file don't affect anything
  49. # on the outside (cmake_minimum_required() sets all policies to NEW for version 2.6.4). Alex
  50. if(COMMAND cmake_policy)
  51. cmake_policy(PUSH)
  52. endif(COMMAND cmake_policy)
  53. # 2.6.4 is required because of the get_filename_component(REALPATH)
  54. cmake_minimum_required( VERSION 2.6.4 FATAL_ERROR )
  55. # allow duplicate target names, this is also done in FindKDE4Internal.cmake
  56. cmake_policy(SET CMP0002 OLD)
  57. get_filename_component(_AUTOMOC4_CURRENT_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
  58. # set the automoc version number
  59. include(${_AUTOMOC4_CURRENT_DIR}/Automoc4Version.cmake)
  60. # are we in the source tree or already installed ?
  61. if(EXISTS ${_AUTOMOC4_CURRENT_DIR}/kde4automoc.cpp)
  62. get_target_property(AUTOMOC4_EXECUTABLE automoc4 LOCATION)
  63. # this dependency is required to make parallel builds of kdesupport work:
  64. set(_AUTOMOC4_EXECUTABLE_DEP automoc4)
  65. else(EXISTS ${_AUTOMOC4_CURRENT_DIR}/kde4automoc.cpp)
  66. get_filename_component(_AUTOMOC4_BIN_DIR "${_AUTOMOC4_CURRENT_DIR}" PATH)
  67. get_filename_component(_AUTOMOC4_BIN_DIR "${_AUTOMOC4_BIN_DIR}" PATH)
  68. # This REALPATH here is necessary for the case that the path is a "virtual" drive
  69. # created using "subst", in this case otherwise the drive letter is missing:
  70. if(WIN32)
  71. get_filename_component(_AUTOMOC4_BIN_DIR "${_AUTOMOC4_BIN_DIR}" REALPATH)
  72. endif(WIN32)
  73. find_program(AUTOMOC4_EXECUTABLE automoc4 PATHS "${_AUTOMOC4_BIN_DIR}/bin" NO_DEFAULT_PATH)
  74. set(_AUTOMOC4_EXECUTABLE_DEP)
  75. mark_as_advanced(AUTOMOC4_EXECUTABLE)
  76. endif(EXISTS ${_AUTOMOC4_CURRENT_DIR}/kde4automoc.cpp)
  77. macro (AUTOMOC4_MOC_HEADERS _target_NAME)
  78. set (_headers_to_moc)
  79. foreach (_current_FILE ${ARGN})
  80. get_filename_component(_suffix "${_current_FILE}" EXT)
  81. if (".h" STREQUAL "${_suffix}" OR ".hpp" STREQUAL "${_suffix}" OR ".hxx" STREQUAL "${_suffix}" OR ".H" STREQUAL "${_suffix}")
  82. list(APPEND _headers_to_moc ${_current_FILE})
  83. else (".h" STREQUAL "${_suffix}" OR ".hpp" STREQUAL "${_suffix}" OR ".hxx" STREQUAL "${_suffix}" OR ".H" STREQUAL "${_suffix}")
  84. message(STATUS "AUTOMOC4_MOC_HEADERS: ignoring non-header file ${_current_FILE}")
  85. endif (".h" STREQUAL "${_suffix}" OR ".hpp" STREQUAL "${_suffix}" OR ".hxx" STREQUAL "${_suffix}" OR ".H" STREQUAL "${_suffix}")
  86. endforeach (_current_FILE)
  87. # need to create moc_<filename>.cpp file using automoc4
  88. # and add it to the target
  89. if(_headers_to_moc)
  90. set(_automoc4_headers_${_target_NAME} "${_headers_to_moc}")
  91. set(_automoc4_headers_${_target_NAME}_automoc "${_headers_to_moc}")
  92. endif(_headers_to_moc)
  93. endmacro (AUTOMOC4_MOC_HEADERS)
  94. macro(AUTOMOC4 _target_NAME _SRCS)
  95. set(_moc_files)
  96. # first list all explicitly set headers
  97. foreach(_header_to_moc ${_automoc4_headers_${_target_NAME}} )
  98. get_filename_component(_abs_header ${_header_to_moc} ABSOLUTE)
  99. list(APPEND _moc_files ${_abs_header})
  100. endforeach(_header_to_moc)
  101. # now add all the sources for the automoc
  102. foreach (_current_FILE ${${_SRCS}})
  103. get_filename_component(_abs_current_FILE "${_current_FILE}" ABSOLUTE)
  104. get_source_file_property(_skip "${_abs_current_FILE}" SKIP_AUTOMOC)
  105. get_source_file_property(_generated "${_abs_current_FILE}" GENERATED)
  106. if(NOT _generated AND NOT _skip)
  107. get_filename_component(_suffix "${_current_FILE}" EXT)
  108. # skip every source file that's not C++
  109. if(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C" OR _suffix STREQUAL ".mm")
  110. list(APPEND _moc_files ${_abs_current_FILE})
  111. endif(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C" OR _suffix STREQUAL ".mm")
  112. endif(NOT _generated AND NOT _skip)
  113. endforeach (_current_FILE)
  114. if(_moc_files)
  115. set(_automoc_source "${CMAKE_CURRENT_BINARY_DIR}/${_target_NAME}_automoc.cpp")
  116. get_directory_property(_moc_incs INCLUDE_DIRECTORIES)
  117. get_directory_property(_moc_defs DEFINITIONS)
  118. get_directory_property(_moc_cdefs COMPILE_DEFINITIONS)
  119. # Assume CMAKE_INCLUDE_CURRENT_DIR is set
  120. list(APPEND _moc_incs ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
  121. # configure_file replaces _moc_files, _moc_incs, _moc_cdefs and _moc_defs
  122. configure_file(${_AUTOMOC4_CURRENT_DIR}/automoc4.files.in ${_automoc_source}.files)
  123. add_custom_command(OUTPUT ${_automoc_source}
  124. COMMAND ${AUTOMOC4_EXECUTABLE}
  125. ${_automoc_source}
  126. ${CMAKE_CURRENT_SOURCE_DIR}
  127. ${CMAKE_CURRENT_BINARY_DIR}
  128. ${QT_MOC_EXECUTABLE}
  129. ${CMAKE_COMMAND}
  130. --touch
  131. DEPENDS ${_automoc_source}.files ${_AUTOMOC4_EXECUTABLE_DEP}
  132. COMMENT ""
  133. VERBATIM
  134. )
  135. set(${_SRCS} ${_automoc_source} ${${_SRCS}})
  136. endif(_moc_files)
  137. endmacro(AUTOMOC4)
  138. macro(_ADD_AUTOMOC4_TARGET _target_NAME _SRCS)
  139. set(_moc_files)
  140. set(_moc_headers)
  141. # first list all explicitly set headers
  142. foreach(_header_to_moc ${_automoc4_headers_${_target_NAME}} )
  143. get_filename_component(_abs_header ${_header_to_moc} ABSOLUTE)
  144. list(APPEND _moc_files ${_abs_header})
  145. list(APPEND _moc_headers ${_abs_header})
  146. endforeach(_header_to_moc)
  147. # now add all the sources for the automoc
  148. foreach (_current_FILE ${${_SRCS}})
  149. get_filename_component(_abs_current_FILE "${_current_FILE}" ABSOLUTE)
  150. get_source_file_property(_skip "${_abs_current_FILE}" SKIP_AUTOMOC)
  151. get_source_file_property(_generated "${_abs_current_FILE}" GENERATED)
  152. if(NOT _generated AND NOT _skip)
  153. get_filename_component(_suffix "${_current_FILE}" EXT)
  154. # skip every source file that's not C++
  155. if(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C" OR _suffix STREQUAL ".mm")
  156. get_filename_component(_basename "${_current_FILE}" NAME_WE)
  157. get_filename_component(_abs_path "${_abs_current_FILE}" PATH)
  158. set(_header "${_abs_path}/${_basename}.h")
  159. if(EXISTS "${_header}")
  160. list(APPEND _moc_headers ${_header})
  161. endif(EXISTS "${_header}")
  162. set(_pheader "${_abs_path}/${_basename}_p.h")
  163. if(EXISTS "${_pheader}")
  164. list(APPEND _moc_headers ${_pheader})
  165. endif(EXISTS "${_pheader}")
  166. list(APPEND _moc_files ${_abs_current_FILE})
  167. endif(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C" OR _suffix STREQUAL ".mm")
  168. endif(NOT _generated AND NOT _skip)
  169. endforeach (_current_FILE)
  170. if(_moc_files)
  171. set(_automoc_source "${CMAKE_CURRENT_BINARY_DIR}/${_target_NAME}.cpp")
  172. set(_automoc_dotFiles "${CMAKE_CURRENT_BINARY_DIR}/${_target_NAME}.cpp.files")
  173. get_directory_property(_moc_incs INCLUDE_DIRECTORIES)
  174. get_directory_property(_moc_defs DEFINITIONS)
  175. get_directory_property(_moc_cdefs COMPILE_DEFINITIONS)
  176. # Assume CMAKE_INCLUDE_CURRENT_DIR is set
  177. list(APPEND _moc_incs ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
  178. # configure_file replaces _moc_files, _moc_incs, _moc_cdefs and _moc_defs
  179. configure_file(${_AUTOMOC4_CURRENT_DIR}/automoc4.files.in ${_automoc_dotFiles})
  180. add_custom_target(${_target_NAME}
  181. COMMAND ${AUTOMOC4_EXECUTABLE}
  182. ${_automoc_source}
  183. ${CMAKE_CURRENT_SOURCE_DIR}
  184. ${CMAKE_CURRENT_BINARY_DIR}
  185. ${QT_MOC_EXECUTABLE}
  186. ${CMAKE_COMMAND}
  187. COMMENT ""
  188. VERBATIM
  189. )
  190. if(_AUTOMOC4_EXECUTABLE_DEP)
  191. add_dependencies(${_target_NAME} ${_AUTOMOC4_EXECUTABLE_DEP})
  192. endif(_AUTOMOC4_EXECUTABLE_DEP)
  193. set_source_files_properties(${_automoc_source} PROPERTIES GENERATED TRUE)
  194. get_directory_property(_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  195. list(APPEND _extra_clean_files "${_automoc_source}")
  196. set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${_extra_clean_files}")
  197. set(${_SRCS} ${_automoc_source} ${${_SRCS}})
  198. endif(_moc_files)
  199. endmacro(_ADD_AUTOMOC4_TARGET)
  200. macro(AUTOMOC4_ADD_EXECUTABLE _target_NAME)
  201. set(_SRCS ${ARGN})
  202. set(_add_executable_param)
  203. foreach(_argName "WIN32" "MACOSX_BUNDLE" "EXCLUDE_FROM_ALL")
  204. list(FIND _SRCS ${_argName} _index)
  205. if(_index GREATER -1)
  206. list(APPEND _add_executable_param ${_argName})
  207. list(REMOVE_AT _SRCS ${_index})
  208. endif(_index GREATER -1)
  209. endforeach(_argName)
  210. _add_automoc4_target("${_target_NAME}_automoc" _SRCS)
  211. add_executable(${_target_NAME} ${_add_executable_param} ${_SRCS})
  212. add_dependencies(${_target_NAME} "${_target_NAME}_automoc")
  213. endmacro(AUTOMOC4_ADD_EXECUTABLE)
  214. macro(AUTOMOC4_ADD_LIBRARY _target_NAME)
  215. set(_SRCS ${ARGN})
  216. set(_add_executable_param)
  217. foreach(_argName "STATIC" "SHARED" "MODULE" "EXCLUDE_FROM_ALL")
  218. list(FIND _SRCS ${_argName} _index)
  219. if(_index GREATER -1)
  220. list(APPEND _add_executable_param ${_argName})
  221. list(REMOVE_AT _SRCS ${_index})
  222. endif(_index GREATER -1)
  223. endforeach(_argName)
  224. _add_automoc4_target("${_target_NAME}_automoc" _SRCS)
  225. add_library(${_target_NAME} ${_add_executable_param} ${_SRCS})
  226. add_dependencies(${_target_NAME} "${_target_NAME}_automoc")
  227. endmacro(AUTOMOC4_ADD_LIBRARY)
  228. macro(_AUTOMOC4_KDE4_PRE_TARGET_HANDLING _target _srcs)
  229. _add_automoc4_target("${_target}_automoc" ${_srcs})
  230. endmacro(_AUTOMOC4_KDE4_PRE_TARGET_HANDLING)
  231. macro(_AUTOMOC4_KDE4_POST_TARGET_HANDLING _target)
  232. add_dependencies(${_target} "${_target}_automoc")
  233. endmacro(_AUTOMOC4_KDE4_POST_TARGET_HANDLING)
  234. # restore previous policy settings:
  235. if(COMMAND cmake_policy)
  236. cmake_policy(POP)
  237. endif(COMMAND cmake_policy)