ctkMacroTargetLibraries.cmake 8.8 KB

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