ctkMacroValidateBuildOptions.cmake 8.0 KB

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