ctkMacroTargetLibraries.cmake 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #! This macro could be invoked using two different signatures:
  22. #! ctkFunctionGetTargetLibraries(TARGET_LIBS)
  23. #! or
  24. #! ctkFunctionGetTargetLibraries(TARGET_LIBS "/path/to/ctk_target_dir")
  25. #!
  26. #! Without specifying the second argument, the current folder will be used.
  27. #!
  28. #! \ingroup CMakeUtilities
  29. FUNCTION(ctkFunctionGetTargetLibraries varname)
  30. SET(expanded_target_library_list)
  31. SET(TARGET_DIRECTORY ${ARGV1})
  32. IF("${TARGET_DIRECTORY}" STREQUAL "")
  33. SET(TARGET_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  34. ENDIF()
  35. SET(filepath ${TARGET_DIRECTORY}/target_libraries.cmake)
  36. SET(manifestpath ${TARGET_DIRECTORY}/manifest_headers.cmake)
  37. # Check if "target_libraries.cmake" or "manifest_headers.cmake" file exists
  38. IF(NOT EXISTS ${filepath} AND NOT EXISTS ${manifestpath})
  39. MESSAGE(FATAL_ERROR "${filepath} or ${manifestpath} doesn't exists !")
  40. ENDIF()
  41. # Make sure the variable is cleared
  42. SET(target_libraries )
  43. SET(Require-Plugin )
  44. IF(EXISTS ${filepath})
  45. # Let's make sure target_libraries contains only strings
  46. FILE(STRINGS "${filepath}" stringtocheck) # read content of 'filepath' into 'stringtocheck'
  47. STRING(REGEX MATCHALL "[^\\#]\\$\\{.*\\}" incorrect_elements ${stringtocheck})
  48. FOREACH(incorrect_element ${incorrect_elements})
  49. STRING(REGEX REPLACE "\\$|\\{|\\}" "" correct_element ${incorrect_element})
  50. MESSAGE(FATAL_ERROR "In ${filepath}, ${incorrect_element} should be replaced by ${correct_element}")
  51. ENDFOREACH()
  52. INCLUDE(${filepath})
  53. # Loop over all target library, if it does *NOT* start with "CTK",
  54. # let's resolve the variable to access its content
  55. FOREACH(target_library ${target_libraries})
  56. IF(${target_library} MATCHES "^CTK[a-zA-Z0-9]+$" OR
  57. ${target_library} MATCHES "^org_commontk_[a-zA-Z0-9_]+$")
  58. LIST(APPEND expanded_target_library_list ${target_library})
  59. ELSE()
  60. LIST(APPEND expanded_target_library_list "${${target_library}}")
  61. ENDIF()
  62. ENDFOREACH()
  63. ENDIF()
  64. IF(EXISTS ${manifestpath})
  65. # Let's make sure Require-Plugins contains only strings
  66. FILE(STRINGS "${manifestpath}" stringtocheck) # read content of 'manifestpath' into 'stringtocheck'
  67. STRING(REGEX MATCHALL "[^\\#]\\$\\{.*\\}" incorrect_elements ${stringtocheck})
  68. FOREACH(incorrect_element ${incorrect_elements})
  69. STRING(REGEX REPLACE "\\$|\\{|\\}" "" correct_element ${incorrect_element})
  70. MESSAGE(FATAL_ERROR "In ${manifestpath}, ${incorrect_element} should be replaced by ${correct_element}")
  71. ENDFOREACH()
  72. INCLUDE(${manifestpath})
  73. # Loop over all plugin dependencies,
  74. FOREACH(plugin_symbolicname ${Require-Plugin})
  75. STRING(REPLACE "." "_" plugin_library ${plugin_symbolicname})
  76. LIST(APPEND expanded_target_library_list ${plugin_library})
  77. ENDFOREACH()
  78. ENDIF()
  79. # Pass the list of target libraries to the caller
  80. SET(${varname} ${expanded_target_library_list} PARENT_SCOPE)
  81. ENDFUNCTION()
  82. #! \ingroup CMakeUtilities
  83. FUNCTION(ctkFunctionCollectTargetLibraryNames target_dir varname)
  84. SET(target_library_list)
  85. #MESSAGE(STATUS target:${target})
  86. SET(lib_targets)
  87. SET(filepath ${target_dir}/target_libraries.cmake)
  88. SET(manifestpath ${target_dir}/manifest_headers.cmake)
  89. # Check if "target_libraries.cmake" or "manifest_headers.cmake" file exists
  90. IF(NOT EXISTS ${filepath} AND NOT EXISTS ${manifestpath})
  91. MESSAGE(FATAL_ERROR "${filepath} or ${manifestpath} doesn't exists !")
  92. ENDIF()
  93. # Make sure the variable is cleared
  94. SET(target_libraries )
  95. SET(Require-Plugin )
  96. IF(EXISTS ${filepath})
  97. # Let's make sure target_libraries contains only strings
  98. FILE(STRINGS "${filepath}" stringtocheck) # read content of 'filepath' into 'stringtocheck'
  99. STRING(REGEX MATCHALL "[^\\#]\\$\\{.*\\}" incorrect_elements ${stringtocheck})
  100. FOREACH(incorrect_element ${incorrect_elements})
  101. STRING(REGEX REPLACE "\\$|\\{|\\}" "" correct_element ${incorrect_element})
  102. MESSAGE(FATAL_ERROR "In ${filepath}, ${incorrect_element} should be replaced by ${correct_element}")
  103. ENDFOREACH()
  104. INCLUDE(${filepath})
  105. LIST(APPEND target_library_list ${target_libraries})
  106. ENDIF()
  107. IF(EXISTS ${manifestpath})
  108. # Let's make sure Require-Plugins contains only strings
  109. FILE(STRINGS "${manifestpath}" stringtocheck) # read content of 'manifestpath' into 'stringtocheck'
  110. STRING(REGEX MATCHALL "[^\\#]\\$\\{.*\\}" incorrect_elements ${stringtocheck})
  111. FOREACH(incorrect_element ${incorrect_elements})
  112. STRING(REGEX REPLACE "\\$|\\{|\\}" "" correct_element ${incorrect_element})
  113. MESSAGE(FATAL_ERROR "In ${manifestpath}, ${incorrect_element} should be replaced by ${correct_element}")
  114. ENDFOREACH()
  115. INCLUDE(${manifestpath})
  116. # Loop over all plugin dependencies
  117. FOREACH(plugin_symbolicname ${Require-Plugin})
  118. STRING(REPLACE "." "_" plugin_library ${plugin_symbolicname})
  119. LIST(APPEND target_library_list ${plugin_library})
  120. ENDFOREACH()
  121. ENDIF()
  122. LIST(REMOVE_DUPLICATES target_library_list)
  123. # Pass the list of target libraries to the caller
  124. SET(${varname} ${target_library_list} PARENT_SCOPE)
  125. ENDFUNCTION()
  126. #! \ingroup CMakeUtilities
  127. MACRO(ctkMacroCollectAllTargetLibraries targets subdir varname)
  128. SET(option_prefix)
  129. IF(${subdir} STREQUAL "Libs")
  130. SET(option_prefix CTK_LIB_)
  131. ELSEIF(${subdir} STREQUAL "Plugins")
  132. SET(option_prefix CTK_PLUGIN_)
  133. ELSEIF(${subdir} STREQUAL "Applications")
  134. SET(option_prefix CTK_APP_)
  135. ELSE()
  136. MESSAGE(FATAL_ERROR "Unknown subdir:${subdir}, expected value are: 'Libs, 'Plugins' or 'Applications'")
  137. ENDIF()
  138. FOREACH(target ${targets})
  139. # Make sure the variable is cleared
  140. SET(target_libraries )
  141. SET(option_name ${option_prefix}${target})
  142. #MESSAGE(STATUS option_name:${option_name})
  143. SET(target_dir "${CTK_SOURCE_DIR}/${subdir}/${target}")
  144. #MESSAGE(STATUS target_dir:${target_dir})
  145. SET(target_libraries)
  146. # Collect target libraries only if option is ON
  147. IF(${option_name})
  148. ctkFunctionCollectTargetLibraryNames(${target_dir} target_libraries)
  149. ENDIF()
  150. IF(target_libraries)
  151. LIST(APPEND ${varname} ${target_libraries})
  152. LIST(REMOVE_DUPLICATES ${varname})
  153. ENDIF()
  154. ENDFOREACH()
  155. ENDMACRO()
  156. #!
  157. #! Extract all library names which are build within this project
  158. #!
  159. #! \ingroup CMakeUtilities
  160. MACRO(ctkMacroGetAllProjectTargetLibraries all_target_libraries varname)
  161. # Allow external projects to override the set of internal targets
  162. IF(COMMAND GetMyTargetLibraries)
  163. GetMyTargetLibraries("${all_target_libraries}" ${varname})
  164. ELSE()
  165. SET(re_ctklib "^(c|C)(t|T)(k|K)[a-zA-Z0-9]+$")
  166. SET(re_ctkplugin "^org_commontk_[a-zA-Z0-9_]+$")
  167. SET(_tmp_list)
  168. LIST(APPEND _tmp_list ${all_target_libraries})
  169. #MESSAGE("calling ctkMacroListFilter with varname:${varname}")
  170. ctkMacroListFilter(_tmp_list re_ctklib re_ctkplugin OUTPUT_VARIABLE ${varname})
  171. #MESSAGE(STATUS "getallctklibs from ${all_target_libraries}")
  172. #MESSAGE(STATUS varname:${varname}:${${varname}})
  173. ENDIF()
  174. ENDMACRO()
  175. #
  176. # Extract all library names *NOT* being build within this project
  177. #
  178. MACRO(ctkMacroGetAllNonProjectTargetLibraries all_target_libraries varname)
  179. ctkMacroGetAllProjectTargetLibraries("${all_target_libraries}" all_project_libraries)
  180. SET(_tmp_list ${all_target_libraries})
  181. IF(all_project_libraries)
  182. LIST(REMOVE_ITEM _tmp_list ${all_project_libraries})
  183. ENDIF()
  184. SET(${varname} ${_tmp_list})
  185. #MESSAGE(STATUS varname:${varname}:${${varname}})
  186. ENDMACRO()
  187. #! \ingroup CMakeUtilities
  188. MACRO(ctkMacroShouldAddExternalProject libraries_variable_name resultvar)
  189. IF(NOT DEFINED NON_CTK_DEPENDENCIES)
  190. MESSAGE(FATAL_ERROR "Variable NON_CTK_DEPENDENCIES is undefined !")
  191. ENDIF()
  192. LIST(FIND NON_CTK_DEPENDENCIES ${libraries_variable_name} index)
  193. SET(${resultvar} FALSE)
  194. IF(${index} GREATER -1)
  195. SET(${resultvar} TRUE)
  196. ENDIF()
  197. ENDMACRO()