Automoc4Config.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. get_filename_component(_AUTOMOC4_CURRENT_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
  48. # set the automoc version number
  49. include(${_AUTOMOC4_CURRENT_DIR}/Automoc4Version.cmake)
  50. # are we in the source tree or already installed ?
  51. if(EXISTS ${_AUTOMOC4_CURRENT_DIR}/kde4automoc.cpp)
  52. get_target_property(AUTOMOC4_EXECUTABLE automoc4 LOCATION)
  53. # this dependency is required to make parallel builds of kdesupport work:
  54. set(_AUTOMOC4_EXECUTABLE_DEP automoc4)
  55. else(EXISTS ${_AUTOMOC4_CURRENT_DIR}/kde4automoc.cpp)
  56. get_filename_component(_AUTOMOC4_BIN_DIR "${_AUTOMOC4_CURRENT_DIR}" PATH)
  57. get_filename_component(_AUTOMOC4_BIN_DIR "${_AUTOMOC4_BIN_DIR}" PATH)
  58. find_program(AUTOMOC4_EXECUTABLE automoc4 PATHS "${_AUTOMOC4_BIN_DIR}/bin" NO_DEFAULT_PATH)
  59. set(_AUTOMOC4_EXECUTABLE_DEP)
  60. mark_as_advanced(AUTOMOC4_EXECUTABLE)
  61. endif(EXISTS ${_AUTOMOC4_CURRENT_DIR}/kde4automoc.cpp)
  62. macro (AUTOMOC4_MOC_HEADERS _target_NAME)
  63. set (_headers_to_moc)
  64. foreach (_current_FILE ${ARGN})
  65. get_filename_component(_suffix "${_current_FILE}" EXT)
  66. if (".h" STREQUAL "${_suffix}" OR ".hpp" STREQUAL "${_suffix}" OR ".hxx" STREQUAL "${_suffix}" OR ".H" STREQUAL "${_suffix}")
  67. list(APPEND _headers_to_moc ${_current_FILE})
  68. else (".h" STREQUAL "${_suffix}" OR ".hpp" STREQUAL "${_suffix}" OR ".hxx" STREQUAL "${_suffix}" OR ".H" STREQUAL "${_suffix}")
  69. message(STATUS "AUTOMOC4_MOC_HEADERS: ignoring non-header file ${_current_FILE}")
  70. endif (".h" STREQUAL "${_suffix}" OR ".hpp" STREQUAL "${_suffix}" OR ".hxx" STREQUAL "${_suffix}" OR ".H" STREQUAL "${_suffix}")
  71. endforeach (_current_FILE)
  72. # need to create moc_<filename>.cpp file using automoc4
  73. # and add it to the target
  74. if(_headers_to_moc)
  75. set(_automoc4_headers_${_target_NAME} "${_headers_to_moc}")
  76. set(_automoc4_headers_${_target_NAME}_automoc "${_headers_to_moc}")
  77. endif(_headers_to_moc)
  78. endmacro (AUTOMOC4_MOC_HEADERS)
  79. macro(AUTOMOC4 _target_NAME _SRCS)
  80. set(_moc_files)
  81. # first list all explicitly set headers
  82. foreach(_header_to_moc ${_automoc4_headers_${_target_NAME}} )
  83. get_filename_component(_abs_header ${_header_to_moc} ABSOLUTE)
  84. list(APPEND _moc_files ${_abs_header})
  85. endforeach(_header_to_moc)
  86. # now add all the sources for the automoc
  87. foreach (_current_FILE ${${_SRCS}})
  88. get_filename_component(_abs_current_FILE "${_current_FILE}" ABSOLUTE)
  89. get_source_file_property(_skip "${_abs_current_FILE}" SKIP_AUTOMOC)
  90. get_source_file_property(_generated "${_abs_current_FILE}" GENERATED)
  91. if(NOT _generated AND NOT _skip)
  92. get_filename_component(_suffix "${_current_FILE}" EXT)
  93. # skip every source file that's not C++
  94. if(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C")
  95. list(APPEND _moc_files ${_abs_current_FILE})
  96. endif(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C")
  97. endif(NOT _generated AND NOT _skip)
  98. endforeach (_current_FILE)
  99. if(_moc_files)
  100. set(_automoc_source "${CMAKE_CURRENT_BINARY_DIR}/${_target_NAME}_automoc.cpp")
  101. get_directory_property(_moc_incs INCLUDE_DIRECTORIES)
  102. get_directory_property(_moc_defs DEFINITIONS)
  103. get_directory_property(_moc_cdefs COMPILE_DEFINITIONS)
  104. # configure_file replaces _moc_files, _moc_incs, _moc_cdefs and _moc_defs
  105. configure_file(${_AUTOMOC4_CURRENT_DIR}/automoc4.files.in ${_automoc_source}.files)
  106. add_custom_command(OUTPUT ${_automoc_source}
  107. COMMAND ${AUTOMOC4_EXECUTABLE}
  108. ${_automoc_source}
  109. ${CMAKE_CURRENT_SOURCE_DIR}
  110. ${CMAKE_CURRENT_BINARY_DIR}
  111. ${QT_MOC_EXECUTABLE}
  112. ${CMAKE_COMMAND}
  113. --touch
  114. DEPENDS ${_automoc_source}.files ${_AUTOMOC4_EXECUTABLE_DEP}
  115. COMMENT ""
  116. VERBATIM
  117. )
  118. set(${_SRCS} ${_automoc_source} ${${_SRCS}})
  119. endif(_moc_files)
  120. endmacro(AUTOMOC4)
  121. macro(_ADD_AUTOMOC4_TARGET _target_NAME _SRCS)
  122. set(_moc_files)
  123. set(_moc_headers)
  124. # first list all explicitly set headers
  125. foreach(_header_to_moc ${_automoc4_headers_${_target_NAME}} )
  126. get_filename_component(_abs_header ${_header_to_moc} ABSOLUTE)
  127. list(APPEND _moc_files ${_abs_header})
  128. list(APPEND _moc_headers ${_abs_header})
  129. endforeach(_header_to_moc)
  130. # now add all the sources for the automoc
  131. foreach (_current_FILE ${${_SRCS}})
  132. get_filename_component(_abs_current_FILE "${_current_FILE}" ABSOLUTE)
  133. get_source_file_property(_skip "${_abs_current_FILE}" SKIP_AUTOMOC)
  134. get_source_file_property(_generated "${_abs_current_FILE}" GENERATED)
  135. if(NOT _generated AND NOT _skip)
  136. get_filename_component(_suffix "${_current_FILE}" EXT)
  137. # skip every source file that's not C++
  138. if(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C")
  139. get_filename_component(_basename "${_current_FILE}" NAME_WE)
  140. get_filename_component(_abs_path "${_abs_current_FILE}" PATH)
  141. set(_header "${_abs_path}/${_basename}.h")
  142. if(EXISTS "${_header}")
  143. list(APPEND _moc_headers ${_header})
  144. endif(EXISTS "${_header}")
  145. set(_pheader "${_abs_path}/${_basename}_p.h")
  146. if(EXISTS "${_pheader}")
  147. list(APPEND _moc_headers ${_pheader})
  148. endif(EXISTS "${_pheader}")
  149. list(APPEND _moc_files ${_abs_current_FILE})
  150. endif(_suffix STREQUAL ".cpp" OR _suffix STREQUAL ".cc" OR _suffix STREQUAL ".cxx" OR _suffix STREQUAL ".C")
  151. endif(NOT _generated AND NOT _skip)
  152. endforeach (_current_FILE)
  153. if(_moc_files)
  154. set(_automoc_source "${CMAKE_CURRENT_BINARY_DIR}/${_target_NAME}.cpp")
  155. set(_automoc_dotFiles "${CMAKE_CURRENT_BINARY_DIR}/${_target_NAME}.cpp.files")
  156. get_directory_property(_moc_incs INCLUDE_DIRECTORIES)
  157. get_directory_property(_moc_defs DEFINITIONS)
  158. get_directory_property(_moc_cdefs COMPILE_DEFINITIONS)
  159. # configure_file replaces _moc_files, _moc_incs, _moc_cdefs and _moc_defs
  160. configure_file(${_AUTOMOC4_CURRENT_DIR}/automoc4.files.in ${_automoc_dotFiles})
  161. add_custom_target(${_target_NAME}
  162. COMMAND ${AUTOMOC4_EXECUTABLE}
  163. ${_automoc_source}
  164. ${CMAKE_CURRENT_SOURCE_DIR}
  165. ${CMAKE_CURRENT_BINARY_DIR}
  166. ${QT_MOC_EXECUTABLE}
  167. ${CMAKE_COMMAND}
  168. COMMENT ""
  169. VERBATIM
  170. )
  171. if(_AUTOMOC4_EXECUTABLE_DEP)
  172. add_dependencies(${_target_NAME} ${_AUTOMOC4_EXECUTABLE_DEP})
  173. endif(_AUTOMOC4_EXECUTABLE_DEP)
  174. set_source_files_properties(${_automoc_source} PROPERTIES GENERATED TRUE)
  175. set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${_automoc_source})
  176. set(${_SRCS} ${_automoc_source} ${${_SRCS}})
  177. endif(_moc_files)
  178. endmacro(_ADD_AUTOMOC4_TARGET)
  179. macro(AUTOMOC4_ADD_EXECUTABLE _target_NAME)
  180. set(_SRCS ${ARGN})
  181. set(_add_executable_param)
  182. foreach(_argName "WIN32" "MACOSX_BUNDLE" "EXCLUDE_FROM_ALL")
  183. list(FIND _SRCS ${_argName} _index)
  184. if(_index GREATER -1)
  185. list(APPEND _add_executable_param ${_argName})
  186. list(REMOVE_AT _SRCS ${_index})
  187. endif(_index GREATER -1)
  188. endforeach(_argName)
  189. _add_automoc4_target("${_target_NAME}_automoc" _SRCS)
  190. add_executable(${_target_NAME} ${_add_executable_param} ${_SRCS})
  191. add_dependencies(${_target_NAME} "${_target_NAME}_automoc")
  192. endmacro(AUTOMOC4_ADD_EXECUTABLE)
  193. macro(AUTOMOC4_ADD_LIBRARY _target_NAME)
  194. set(_SRCS ${ARGN})
  195. set(_add_executable_param)
  196. foreach(_argName "STATIC" "SHARED" "MODULE" "EXCLUDE_FROM_ALL")
  197. list(FIND _SRCS ${_argName} _index)
  198. if(_index GREATER -1)
  199. list(APPEND _add_executable_param ${_argName})
  200. list(REMOVE_AT _SRCS ${_index})
  201. endif(_index GREATER -1)
  202. endforeach(_argName)
  203. _add_automoc4_target("${_target_NAME}_automoc" _SRCS)
  204. add_library(${_target_NAME} ${_add_executable_param} ${_SRCS})
  205. add_dependencies(${_target_NAME} "${_target_NAME}_automoc")
  206. endmacro(AUTOMOC4_ADD_LIBRARY)
  207. macro(_AUTOMOC4_KDE4_PRE_TARGET_HANDLING _target _srcs)
  208. _add_automoc4_target("${_target}_automoc" ${_srcs})
  209. endmacro(_AUTOMOC4_KDE4_PRE_TARGET_HANDLING)
  210. macro(_AUTOMOC4_KDE4_POST_TARGET_HANDLING _target)
  211. add_dependencies(${_target} "${_target}_automoc")
  212. endmacro(_AUTOMOC4_KDE4_POST_TARGET_HANDLING)