ctkMacroValidateBuildOptions.cmake 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.commontk.org/LICENSE
  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. #! \ingroup CMakeUtilities
  21. MACRO(ctkMacroGetOptionName target_directories_with_target_name target_name option_name_var)
  22. FOREACH(target_info ${target_directories_with_target_name})
  23. # extract target_dir and option_name
  24. #MESSAGE("target_info:${target_info}")
  25. STRING(REPLACE "^^" "\\;" target_info_list ${target_info})
  26. SET(target_info_list ${target_info_list})
  27. LIST(GET target_info_list 0 _target_dir)
  28. LIST(GET target_info_list 1 _option_name)
  29. LIST(GET target_info_list 2 _target_name)
  30. IF(${_target_name} STREQUAL ${target_name})
  31. SET(${option_name_var} ${_option_name})
  32. ENDIF()
  33. ENDFOREACH()
  34. ENDMACRO()
  35. #! \ingroup CMakeUtilities
  36. MACRO(ctkMacroValidateBuildOptions dir executable target_directories)
  37. IF(NOT EXISTS ${dir})
  38. MESSAGE(FATAL_ERROR "Directory ${dir} doesn't exist!")
  39. ENDIF()
  40. IF(NOT EXISTS ${executable})
  41. MESSAGE(FATAL_ERROR "Executable ${executable} doesn't exist!")
  42. ENDIF()
  43. SET(known_targets)
  44. SET(target_directories_with_target_name)
  45. # Create target_directories_with_target_name
  46. FOREACH(target_info ${target_directories})
  47. # extract target_dir and option_name
  48. STRING(REPLACE "^^" "\\;" target_info ${target_info})
  49. SET(target_info_list ${target_info})
  50. LIST(GET target_info_list 0 target_dir)
  51. LIST(GET target_info_list 1 option_name)
  52. #MESSAGE(STATUS target_dir:${target_dir})
  53. #MESSAGE(STATUS option_name:${option_name})
  54. #MESSAGE(STATUS option_name-value:${${option_name}})
  55. # make sure the directory exists
  56. IF(NOT EXISTS ${target_dir}/CMakeLists.txt)
  57. MESSAGE(FATAL_ERROR "Target directory ${target_dir}/CMakeLists.txt doesn't exists !")
  58. ENDIF()
  59. # extract project name from CMakeLists.txt
  60. FILE(STRINGS "${target_dir}/CMakeLists.txt" project_string
  61. REGEX "^ *(P|p)(R|r)(O|o)(J|j)(E|e)(C|c)(T|t)\\("
  62. LIMIT_COUNT 10)
  63. STRING(REGEX MATCH "\\((.*)\\)" target_project_name ${project_string})
  64. STRING(REGEX REPLACE "\\(|\\)" "" target_project_name ${target_project_name})
  65. IF(${target_project_name} STREQUAL "")
  66. MESSAGE(FATAL_ERROR "Failed to extract project name from ${target_dir}/CMakeLists.txt")
  67. ENDIF()
  68. #MESSAGE(STATUS target_project_name:${target_project_name})
  69. LIST(APPEND target_directories_with_target_name
  70. ${target_dir}^^${option_name}^^${target_project_name}
  71. )
  72. LIST(APPEND known_targets ${target_project_name})
  73. ENDFOREACH()
  74. # This is for external projects using CTK
  75. # The variables CTK_PLUGIN_LIBRARIES and CTK_LIBRARIES are set in CTKConfig.cmake
  76. IF(CTK_PLUGIN_LIBRARIES)
  77. LIST(APPEND known_targets ${CTK_PLUGIN_LIBRARIES})
  78. ENDIF()
  79. IF(CTK_LIBRARIES)
  80. LIST(APPEND known_targets ${CTK_LIBRARIES})
  81. ENDIF()
  82. #MESSAGE("Known targets: ${known_targets}")
  83. SET(EXTERNAL_TARGETS ) # This is used in CMakeLists.txt
  84. FOREACH(target_info ${target_directories_with_target_name})
  85. # extract target_dir and option_name
  86. STRING(REPLACE "^^" "\\;" target_info ${target_info})
  87. SET(target_info_list ${target_info})
  88. LIST(GET target_info_list 0 target_dir)
  89. LIST(GET target_info_list 1 option_name)
  90. #MESSAGE(STATUS target_dir:${target_dir})
  91. #MESSAGE(STATUS option_name:${option_name})
  92. #MESSAGE(STATUS option_name-value:${${option_name}})
  93. # make sure the directory exists
  94. IF(NOT EXISTS ${target_dir}/CMakeLists.txt)
  95. MESSAGE(FATAL_ERROR "Target directory ${target_dir}/CMakeLists.txt doesn't exists !")
  96. ENDIF()
  97. # extract project name from CMakeLists.txt
  98. FILE(STRINGS "${target_dir}/CMakeLists.txt" project_string
  99. REGEX "^ *(P|p)(R|r)(O|o)(J|j)(E|e)(C|c)(T|t)\\("
  100. LIMIT_COUNT 10)
  101. STRING(REGEX MATCH "\\((.*)\\)" target_project_name ${project_string})
  102. STRING(REGEX REPLACE "\\(|\\)" "" target_project_name ${target_project_name})
  103. IF(${target_project_name} STREQUAL "")
  104. MESSAGE(FATAL_ERROR "Failed to extract project name from ${target_dir}/CMakeLists.txt")
  105. ENDIF()
  106. #MESSAGE(STATUS target_project_name:${target_project_name})
  107. IF(${${option_name}})
  108. # Obtain dependency path
  109. ctkMacroSetPaths("${QT_INSTALLED_LIBRARY_DIR}")
  110. EXECUTE_PROCESS(
  111. COMMAND "${executable}" "${dir}/DGraphInput-alldep-withext.txt" -sort ${target_project_name}
  112. WORKING_DIRECTORY ${dir}
  113. RESULT_VARIABLE RESULT_VAR
  114. OUTPUT_VARIABLE dep_path
  115. ERROR_VARIABLE error
  116. OUTPUT_STRIP_TRAILING_WHITESPACE
  117. )
  118. IF(RESULT_VAR)
  119. MESSAGE(FATAL_ERROR "Failed to obtain dependence path of ${target_project_name}.\n${RESULT_VAR}\n${CTK_BINARY_DIR}\n${error}")
  120. ENDIF()
  121. # Set a variable for each target containing its dependencies
  122. # Needed for setting individual include directories for plugins,
  123. # depending on other plugins.
  124. SET(${target_project_name}_DEPENDENCIES )
  125. # Convert 'dep_path' to a list
  126. STRING(REPLACE " " "\\;" dep_path_list ${dep_path})
  127. SET(dep_path_list ${dep_path_list})
  128. LIST(REMOVE_ITEM dep_path_list ${target_project_name})
  129. LIST(APPEND ${target_project_name}_DEPENDENCIES ${dep_path_list})
  130. #MESSAGE("path for ${target_project_name} is: ${dep_path_list}")
  131. # Check if all internal CTK targets included in the dependency path are enabled
  132. SET(int_dep_path_list )
  133. SET(ext_dep_path_list ${dep_path_list})
  134. ctkMacroGetAllProjectTargetLibraries("${dep_path_list}" int_dep_path_list)
  135. IF(int_dep_path_list)
  136. LIST(REMOVE_ITEM ext_dep_path_list ${int_dep_path_list})
  137. ENDIF()
  138. IF(ext_dep_path_list)
  139. LIST(APPEND EXTERNAL_TARGETS ${ext_dep_path_list})
  140. # If this macro is called from inside CTK itself, we add the external
  141. # targets to the list of known targets (for external projects calling
  142. # this macro, targets external to the calling project should be listed
  143. # in CTK_LIBRARIES or CTK_PLUGIN_LIBRARIES
  144. IF(CTK_SOURCE_DIR)
  145. IF(${CMAKE_SOURCE_DIR} STREQUAL ${CTK_SOURCE_DIR})
  146. LIST(APPEND known_targets ${ext_dep_path_list})
  147. ENDIF()
  148. ENDIF()
  149. ENDIF()
  150. FOREACH(dep ${int_dep_path_list})
  151. LIST(FIND known_targets ${dep} dep_found)
  152. IF(dep_found LESS 0)
  153. MESSAGE(FATAL_ERROR "${target_project_name} depends on ${dep}, which does not exist")
  154. ENDIF()
  155. ctkMacroGetOptionName("${target_directories_with_target_name}" ${dep} dep_option)
  156. IF(NOT ${${dep_option}})
  157. # Enable option
  158. MESSAGE(STATUS "Enabling option [${dep_option}] required by [${target_project_name}]")
  159. SET(${dep_option} ON CACHE BOOL "Enable ${target_project_name} library" FORCE)
  160. ENDIF()
  161. ENDFOREACH()
  162. # Check if all external targets included in the dependency path are available
  163. FOREACH(dep ${ext_dep_path_list})
  164. LIST(FIND known_targets ${dep} dep_found)
  165. IF(dep_found LESS 0)
  166. MESSAGE(FATAL_ERROR "${target_project_name} depends on unknown external targets: ${dep}")
  167. ENDIF()
  168. ENDFOREACH()
  169. ENDIF()
  170. ENDFOREACH()
  171. IF(EXTERNAL_TARGETS)
  172. LIST(REMOVE_DUPLICATES EXTERNAL_TARGETS)
  173. ENDIF()
  174. #MESSAGE(STATUS "Validated: CTK_LIB_*, CTK_PLUGIN_*, CTK_APP_*")
  175. ENDMACRO()