ctkMacroTargetLibraries.cmake 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #!
  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. #! \ingroup CMakeUtilities
  179. macro(ctkMacroGetAllNonProjectTargetLibraries all_target_libraries varname)
  180. ctkMacroGetAllProjectTargetLibraries("${all_target_libraries}" all_project_libraries)
  181. set(_tmp_list ${all_target_libraries})
  182. if(all_project_libraries)
  183. list(REMOVE_ITEM _tmp_list ${all_project_libraries})
  184. endif()
  185. set(${varname} ${_tmp_list})
  186. #message(varname:${varname}:${${varname}})
  187. endmacro()
  188. #! \ingroup CMakeUtilities
  189. macro(ctkMacroShouldAddExternalProject libraries_variable_name resultvar)
  190. set(${resultvar} FALSE)
  191. if(DEFINED NON_CTK_DEPENDENCIES)
  192. list(FIND NON_CTK_DEPENDENCIES ${libraries_variable_name} index)
  193. if(${index} GREATER -1)
  194. set(${resultvar} TRUE)
  195. endif()
  196. endif()
  197. endmacro()