ctkMacroCheckExternalProjectDependency.cmake 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. ###########################################################################
  2. #
  3. # Library: CTK
  4. #
  5. # Copyright (c) Kitware Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0.txt
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. ###########################################################################
  20. if(NOT EXISTS "${EXTERNAL_PROJECT_DIR}")
  21. set(EXTERNAL_PROJECT_DIR ${${CMAKE_PROJECT_NAME}_SOURCE_DIR}/SuperBuild)
  22. endif()
  23. if(NOT DEFINED EXTERNAL_PROJECT_FILE_PREFIX)
  24. set(EXTERNAL_PROJECT_FILE_PREFIX "External_")
  25. endif()
  26. #
  27. # superbuild_include_once()
  28. #
  29. # superbuild_include_once() is a macro intented to be used as include guard.
  30. #
  31. # It ensures that the CMake code placed after the include guard in a CMake file included
  32. # using either 'include(/path/to/file.cmake)' or 'include(cmake_module)' will be executed
  33. # once.
  34. #
  35. # It internally set the global property '<CMAKE_CURRENT_LIST_FILENAME>_FILE_INCLUDED' to check if
  36. # a file has already been included.
  37. #
  38. macro(superbuild_include_once)
  39. # Make sure this file is included only once
  40. get_filename_component(CMAKE_CURRENT_LIST_FILENAME ${CMAKE_CURRENT_LIST_FILE} NAME_WE)
  41. set(_property_name ${CMAKE_CURRENT_LIST_FILENAME}_FILE_INCLUDED)
  42. get_property(${_property_name} GLOBAL PROPERTY ${_property_name})
  43. if(${_property_name})
  44. return()
  45. endif()
  46. set_property(GLOBAL PROPERTY ${_property_name} 1)
  47. endmacro()
  48. macro(_epd_status txt)
  49. if(NOT __epd_first_pass)
  50. message(STATUS ${txt})
  51. endif()
  52. endmacro()
  53. macro(ctkMacroCheckExternalProjectDependency proj)
  54. # Set indent variable if needed
  55. if(NOT DEFINED __indent)
  56. set(__indent "")
  57. else()
  58. set(__indent "${__indent} ")
  59. endif()
  60. # Sanity checks
  61. if(NOT DEFINED ${proj}_DEPENDENCIES)
  62. message(FATAL_ERROR "${__indent}${proj}_DEPENDENCIES variable is NOT defined !")
  63. endif()
  64. # Keep track of the projects
  65. list(APPEND __epd_${CMAKE_PROJECT_NAME}_projects ${proj})
  66. # Is this the first run ? (used to set the <CMAKE_PROJECT_NAME>_USE_SYSTEM_* variables)
  67. if(${proj} STREQUAL ${CMAKE_PROJECT_NAME} AND NOT DEFINED __epd_first_pass)
  68. message(STATUS "SuperBuild - First pass")
  69. set(__epd_first_pass TRUE)
  70. endif()
  71. # Set message strings
  72. set(__${proj}_indent ${__indent})
  73. set(__${proj}_superbuild_message "SuperBuild - ${__indent}${proj}[OK]")
  74. if(${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj})
  75. set(__${proj}_superbuild_message "${__${proj}_superbuild_message} (SYSTEM)")
  76. endif()
  77. # Display dependency of project being processed
  78. if("${${proj}_DEPENDENCIES}" STREQUAL "")
  79. _epd_status(${__${proj}_superbuild_message})
  80. else()
  81. set(dependency_str " ")
  82. foreach(dep ${${proj}_DEPENDENCIES})
  83. get_property(_is_included GLOBAL PROPERTY ${EXTERNAL_PROJECT_FILE_PREFIX}${dep}_FILE_INCLUDED)
  84. if(_is_included)
  85. set(dependency_str "${dependency_str}${dep}[INCLUDED], ")
  86. else()
  87. set(dependency_str "${dependency_str}${dep}, ")
  88. endif()
  89. endforeach()
  90. _epd_status("SuperBuild - ${__indent}${proj} => Requires${dependency_str}")
  91. endif()
  92. foreach(dep ${${proj}_DEPENDENCIES})
  93. if(${${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj}})
  94. set(${CMAKE_PROJECT_NAME}_USE_SYSTEM_${dep} ${${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj}})
  95. endif()
  96. #if(__epd_first_pass)
  97. # message("${CMAKE_PROJECT_NAME}_USE_SYSTEM_${dep} set to [${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj}:${${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj}}]")
  98. #endif()
  99. endforeach()
  100. # Include dependencies
  101. foreach(dep ${${proj}_DEPENDENCIES})
  102. get_property(_is_included GLOBAL PROPERTY External_${dep}_FILE_INCLUDED)
  103. if(NOT _is_included)
  104. # XXX - Refactor - Add a single variable named 'EXTERNAL_PROJECT_DIRS'
  105. if(EXISTS "${EXTERNAL_PROJECT_DIR}/${EXTERNAL_PROJECT_FILE_PREFIX}${dep}.cmake")
  106. include(${EXTERNAL_PROJECT_DIR}/${EXTERNAL_PROJECT_FILE_PREFIX}${dep}.cmake)
  107. elseif(EXISTS "${${dep}_FILEPATH}")
  108. include(${${dep}_FILEPATH})
  109. elseif(EXISTS "${EXTERNAL_PROJECT_ADDITIONAL_DIR}/${EXTERNAL_PROJECT_FILE_PREFIX}${dep}.cmake")
  110. include(${EXTERNAL_PROJECT_ADDITIONAL_DIR}/${EXTERNAL_PEXCLUDEDROJECT_FILE_PREFIX}${dep}.cmake)
  111. else()
  112. message(FATAL_ERROR "Can't find ${EXTERNAL_PROJECT_FILE_PREFIX}${dep}.cmake")
  113. endif()
  114. endif()
  115. endforeach()
  116. # If project being process has dependencies, indicates it has also been added.
  117. if(NOT "${${proj}_DEPENDENCIES}" STREQUAL "")
  118. _epd_status(${__${proj}_superbuild_message})
  119. endif()
  120. # Update indent variable
  121. string(LENGTH "${__indent}" __indent_length)
  122. math(EXPR __indent_length "${__indent_length}-2")
  123. if(NOT ${__indent_length} LESS 0)
  124. string(SUBSTRING "${__indent}" 0 ${__indent_length} __indent)
  125. endif()
  126. if(${proj} STREQUAL ${CMAKE_PROJECT_NAME} AND __epd_first_pass)
  127. message(STATUS "SuperBuild - First pass - done")
  128. unset(__indent)
  129. if(${CMAKE_PROJECT_NAME}_SUPERBUILD)
  130. set(__epd_first_pass FALSE)
  131. endif()
  132. unset(${CMAKE_PROJECT_NAME}_DEPENDENCIES) # XXX - Refactor
  133. foreach(possible_proj ${__epd_${CMAKE_PROJECT_NAME}_projects})
  134. if(NOT ${possible_proj} STREQUAL ${CMAKE_PROJECT_NAME})
  135. set_property(GLOBAL PROPERTY ${EXTERNAL_PROJECT_FILE_PREFIX}${possible_proj}_FILE_INCLUDED 0)
  136. # XXX - Refactor - The following code should be re-organized
  137. if(DEFINED ${possible_proj}_enabling_variable)
  138. ctkMacroShouldAddExternalproject(${${possible_proj}_enabling_variable} add_project)
  139. if(${add_project})
  140. list(APPEND ${CMAKE_PROJECT_NAME}_DEPENDENCIES ${possible_proj})
  141. else()
  142. # XXX HACK
  143. if(${possible_proj} STREQUAL "VTK"
  144. AND CTK_LIB_Scripting/Python/Core_PYTHONQT_USE_VTK)
  145. list(APPEND ${CMAKE_PROJECT_NAME}_DEPENDENCIES VTK)
  146. else()
  147. unset(${${possible_proj}_enabling_variable}_INCLUDE_DIRS)
  148. unset(${${possible_proj}_enabling_variable}_LIBRARY_DIRS)
  149. unset(${${possible_proj}_enabling_variable}_FIND_PACKAGE_CMD)
  150. if(${CMAKE_PROJECT_NAME}_SUPERBUILD)
  151. message(STATUS "SuperBuild - ${possible_proj}[OPTIONAL]")
  152. endif()
  153. endif()
  154. endif()
  155. else()
  156. list(APPEND ${CMAKE_PROJECT_NAME}_DEPENDENCIES ${possible_proj})
  157. endif()
  158. # XXX
  159. else()
  160. endif()
  161. endforeach()
  162. list(REMOVE_DUPLICATES ${CMAKE_PROJECT_NAME}_DEPENDENCIES)
  163. if(${CMAKE_PROJECT_NAME}_SUPERBUILD)
  164. ctkMacroCheckExternalProjectDependency(${CMAKE_PROJECT_NAME})
  165. endif()
  166. endif()
  167. if(__epd_first_pass)
  168. return()
  169. endif()
  170. endmacro()