ctkMacroValidateBuildOptions.cmake 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #! \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 variable CTK_EXTERNAL_PLUGIN_LIBRARIES is filled in ctkMacroSetupExternalPlugins
  76. # with the help of variables defined in "PluginUseFiles" from external projects.
  77. IF(CTK_EXTERNAL_PLUGIN_LIBRARIES)
  78. LIST(APPEND known_targets ${CTK_EXTERNAL_PLUGIN_LIBRARIES})
  79. ENDIF()
  80. # The variable CTK_LIBRARIES is set in CTKConfig.cmake
  81. IF(CTK_LIBRARIES)
  82. LIST(APPEND known_targets ${CTK_LIBRARIES})
  83. ENDIF()
  84. #MESSAGE("Known targets: ${known_targets}")
  85. SET(EXTERNAL_TARGETS ) # This is used in CMakeLists.txt
  86. FOREACH(target_info ${target_directories_with_target_name})
  87. # extract target_dir and option_name
  88. STRING(REPLACE "^^" "\\;" target_info ${target_info})
  89. SET(target_info_list ${target_info})
  90. LIST(GET target_info_list 0 target_dir)
  91. LIST(GET target_info_list 1 option_name)
  92. #MESSAGE(STATUS target_dir:${target_dir})
  93. #MESSAGE(STATUS option_name:${option_name})
  94. #MESSAGE(STATUS option_name-value:${${option_name}})
  95. # make sure the directory exists
  96. IF(NOT EXISTS ${target_dir}/CMakeLists.txt)
  97. MESSAGE(FATAL_ERROR "Target directory ${target_dir}/CMakeLists.txt doesn't exists !")
  98. ENDIF()
  99. # extract project name from CMakeLists.txt
  100. FILE(STRINGS "${target_dir}/CMakeLists.txt" project_string
  101. REGEX "^ *(P|p)(R|r)(O|o)(J|j)(E|e)(C|c)(T|t)\\("
  102. LIMIT_COUNT 10)
  103. STRING(REGEX MATCH "\\((.*)\\)" target_project_name ${project_string})
  104. STRING(REGEX REPLACE "\\(|\\)" "" target_project_name ${target_project_name})
  105. IF(${target_project_name} STREQUAL "")
  106. MESSAGE(FATAL_ERROR "Failed to extract project name from ${target_dir}/CMakeLists.txt")
  107. ENDIF()
  108. #MESSAGE(STATUS target_project_name:${target_project_name})
  109. IF(${${option_name}})
  110. # Obtain dependency path
  111. ctkMacroSetPaths("${QT_INSTALLED_LIBRARY_DIR}")
  112. EXECUTE_PROCESS(
  113. COMMAND "${executable}" "${dir}/DGraphInput-alldep-withext.txt" -sort ${target_project_name}
  114. WORKING_DIRECTORY ${dir}
  115. RESULT_VARIABLE RESULT_VAR
  116. OUTPUT_VARIABLE dep_path
  117. ERROR_VARIABLE error
  118. OUTPUT_STRIP_TRAILING_WHITESPACE
  119. )
  120. IF(RESULT_VAR)
  121. MESSAGE(FATAL_ERROR "Failed to obtain dependence path of ${target_project_name}.\n${RESULT_VAR}\n${CTK_BINARY_DIR}\n${error}")
  122. ENDIF()
  123. # Set a variable for each target containing its dependencies
  124. # Needed for setting individual include directories for plugins,
  125. # depending on other plugins.
  126. SET(${target_project_name}_DEPENDENCIES )
  127. # Convert 'dep_path' to a list
  128. STRING(REPLACE " " "\\;" dep_path_list ${dep_path})
  129. SET(dep_path_list ${dep_path_list})
  130. LIST(REMOVE_ITEM dep_path_list ${target_project_name})
  131. LIST(APPEND ${target_project_name}_DEPENDENCIES ${dep_path_list})
  132. #MESSAGE("path for ${target_project_name} is: ${dep_path_list}")
  133. # Check if all internal CTK targets included in the dependency path are enabled
  134. SET(int_dep_path_list )
  135. SET(ext_dep_path_list ${dep_path_list})
  136. ctkMacroGetAllProjectTargetLibraries("${dep_path_list}" int_dep_path_list)
  137. IF(int_dep_path_list)
  138. LIST(REMOVE_ITEM ext_dep_path_list ${int_dep_path_list})
  139. ENDIF()
  140. IF(ext_dep_path_list)
  141. LIST(APPEND EXTERNAL_TARGETS ${ext_dep_path_list})
  142. # If this macro is called from inside CTK itself, we add the external
  143. # targets to the list of known targets (for external projects calling
  144. # this macro, targets external to the calling project should be listed
  145. # in CTK_LIBRARIES or CTK_EXTERNAL_PLUGIN_LIBRARIES
  146. IF(CTK_SOURCE_DIR)
  147. IF(${CMAKE_SOURCE_DIR} STREQUAL ${CTK_SOURCE_DIR})
  148. LIST(APPEND known_targets ${ext_dep_path_list})
  149. ENDIF()
  150. ENDIF()
  151. ENDIF()
  152. FOREACH(dep ${int_dep_path_list})
  153. LIST(FIND known_targets ${dep} dep_found)
  154. IF(dep_found LESS 0)
  155. MESSAGE(FATAL_ERROR "${target_project_name} depends on ${dep}, which does not exist")
  156. ENDIF()
  157. ctkMacroGetOptionName("${target_directories_with_target_name}" ${dep} dep_option)
  158. IF(NOT ${${dep_option}})
  159. # Enable option
  160. MESSAGE(STATUS "Enabling option [${dep_option}] required by [${target_project_name}]")
  161. SET(${dep_option} ON CACHE BOOL "Enable ${target_project_name} library" FORCE)
  162. ENDIF()
  163. ENDFOREACH()
  164. # Check if all external targets included in the dependency path are available
  165. FOREACH(dep ${ext_dep_path_list})
  166. LIST(FIND known_targets ${dep} dep_found)
  167. IF(dep_found LESS 0)
  168. MESSAGE(FATAL_ERROR "${target_project_name} depends on unknown external targets: ${dep}")
  169. ENDIF()
  170. ENDFOREACH()
  171. ENDIF()
  172. ENDFOREACH()
  173. IF(EXTERNAL_TARGETS)
  174. LIST(REMOVE_DUPLICATES EXTERNAL_TARGETS)
  175. ENDIF()
  176. #MESSAGE(STATUS "Validated: CTK_LIB_*, CTK_PLUGIN_*, CTK_APP_*")
  177. ENDMACRO()