ctkMacroValidateBuildOptions.cmake 7.9 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. # Obtain dependency path
  110. ctkMacroSetPaths("${QT_INSTALLED_LIBRARY_DIR}")
  111. execute_process(
  112. COMMAND "${executable}" "${dir}/DGraphInput-alldep-withext.txt" -sort ${target_project_name}
  113. WORKING_DIRECTORY ${dir}
  114. RESULT_VARIABLE RESULT_VAR
  115. OUTPUT_VARIABLE dep_path
  116. ERROR_VARIABLE error
  117. OUTPUT_STRIP_TRAILING_WHITESPACE
  118. )
  119. if(RESULT_VAR)
  120. message(FATAL_ERROR "Failed to obtain dependence path of ${target_project_name}.\n${RESULT_VAR}\n${CTK_BINARY_DIR}\n${error}")
  121. endif()
  122. # Set a variable for each target containing its dependencies
  123. # Needed for setting individual include directories for plugins,
  124. # depending on other plugins.
  125. set(${target_project_name}_DEPENDENCIES )
  126. # Convert 'dep_path' to a list
  127. string(REPLACE " " "\\;" dep_path_list ${dep_path})
  128. set(dep_path_list ${dep_path_list})
  129. list(REMOVE_ITEM dep_path_list ${target_project_name})
  130. list(APPEND ${target_project_name}_DEPENDENCIES ${dep_path_list})
  131. #message("path for ${target_project_name} is: ${dep_path_list}")
  132. # Check if all internal CTK targets included in the dependency path are enabled
  133. set(int_dep_path_list )
  134. set(ext_dep_path_list ${dep_path_list})
  135. ctkMacroGetAllProjectTargetLibraries("${dep_path_list}" int_dep_path_list)
  136. if(int_dep_path_list)
  137. list(REMOVE_ITEM ext_dep_path_list ${int_dep_path_list})
  138. endif()
  139. if(ext_dep_path_list)
  140. list(APPEND EXTERNAL_TARGETS ${ext_dep_path_list})
  141. # If this macro is called from inside CTK itself, we add the external
  142. # targets to the list of known targets (for external projects calling
  143. # this macro, targets external to the calling project should be listed
  144. # in CTK_LIBRARIES or CTK_EXTERNAL_PLUGIN_LIBRARIES
  145. if(CTK_SOURCE_DIR)
  146. if(${CMAKE_SOURCE_DIR} STREQUAL ${CTK_SOURCE_DIR})
  147. list(APPEND known_targets ${ext_dep_path_list})
  148. endif()
  149. endif()
  150. endif()
  151. foreach(dep ${int_dep_path_list})
  152. list(FIND known_targets ${dep} dep_found)
  153. if(dep_found LESS 0)
  154. message(FATAL_ERROR "${target_project_name} depends on ${dep}, which does not exist")
  155. endif()
  156. ctkMacroGetOptionName("${target_directories_with_target_name}" ${dep} dep_option)
  157. if(${${option_name}} AND NOT ${${dep_option}})
  158. # Enable option
  159. message(STATUS "Enabling option [${dep_option}] required by [${target_project_name}]")
  160. set(${dep_option} ON CACHE BOOL "Enable ${target_project_name} library" FORCE)
  161. endif()
  162. endforeach()
  163. # For enabled targets, check if all external targets included in the dependency path are available
  164. if(${${option_name}})
  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()