1 ###########################################################################
5 # Copyright (c) Kitware Inc.
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
11 # http://www.apache.org/licenses/LICENSE-2.0.txt
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.
19 ###########################################################################
22 #! This macro could be invoked using two different signatures:
23 #! ctkFunctionGetTargetLibraries(TARGET_LIBS)
25 #! ctkFunctionGetTargetLibraries(TARGET_LIBS "/path/to/ctk_target_dir")
27 #! Without specifying the second argument, the current folder will be used.
29 #! \ingroup CMakeUtilities
30 function(ctkFunctionGetTargetLibraries varname)
32 set(expanded_target_library_list)
34 set(TARGET_DIRECTORY ${ARGV1})
36 if("${TARGET_DIRECTORY}" STREQUAL "")
37 set(TARGET_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
38 set(_target_name ${PROJECT_NAME})
41 set(filepath ${TARGET_DIRECTORY}/target_libraries.cmake)
42 set(manifestpath ${TARGET_DIRECTORY}/manifest_headers.cmake)
44 # Check if "target_libraries.cmake" or "manifest_headers.cmake" file exists
45 if(NOT EXISTS ${filepath} AND NOT EXISTS ${manifestpath})
46 message(FATAL_ERROR "${filepath} or ${manifestpath} doesn't exists !")
49 # Make sure the variable is cleared
50 set(target_libraries )
53 if(EXISTS ${filepath})
54 # Let's make sure target_libraries contains only strings
55 file(STRINGS "${filepath}" stringtocheck) # read content of 'filepath' into 'stringtocheck'
56 string(REGEX MATCHALL "[^\\#]\\$\\{.*\\}" incorrect_elements ${stringtocheck})
57 foreach(incorrect_element ${incorrect_elements})
58 string(REGEX REPLACE "\\$|\\{|\\}" "" correct_element ${incorrect_element})
59 message(FATAL_ERROR "In ${filepath}, ${incorrect_element} should be replaced by ${correct_element}")
65 list(APPEND target_libraries "${${_target_name}_OPTIONAL_DEPENDENCIES}")
68 # Loop over all target library, if it does *NOT* start with "CTK",
69 # let's resolve the variable to access its content
70 foreach(target_library ${target_libraries})
71 if(${target_library} MATCHES "^CTK[a-zA-Z0-9]+$" OR
72 ${target_library} MATCHES "^org_commontk_[a-zA-Z0-9_]+$")
73 list(APPEND expanded_target_library_list ${target_library})
75 list(APPEND expanded_target_library_list "${${target_library}}")
80 if(EXISTS ${manifestpath})
81 # Let's make sure Require-Plugins contains only strings
82 file(STRINGS "${manifestpath}" stringtocheck) # read content of 'manifestpath' into 'stringtocheck'
83 string(REGEX MATCHALL "[^\\#]\\$\\{.*\\}" incorrect_elements ${stringtocheck})
84 foreach(incorrect_element ${incorrect_elements})
85 string(REGEX REPLACE "\\$|\\{|\\}" "" correct_element ${incorrect_element})
86 message(FATAL_ERROR "In ${manifestpath}, ${incorrect_element} should be replaced by ${correct_element}")
89 include(${manifestpath})
91 # Loop over all plugin dependencies,
92 foreach(plugin_symbolicname ${Require-Plugin})
93 string(REPLACE "." "_" plugin_library ${plugin_symbolicname})
94 list(APPEND expanded_target_library_list ${plugin_library})
98 # Pass the list of target libraries to the caller
99 set(${varname} ${expanded_target_library_list} PARENT_SCOPE)
103 #! \ingroup CMakeUtilities
104 function(ctkFunctionCollectTargetLibraryNames target_dir varname)
106 set(target_library_list)
107 #message(STATUS target:${target})
110 set(filepath ${target_dir}/target_libraries.cmake)
111 set(manifestpath ${target_dir}/manifest_headers.cmake)
113 # Check if "target_libraries.cmake" or "manifest_headers.cmake" file exists
114 if(NOT EXISTS ${filepath} AND NOT EXISTS ${manifestpath})
115 message(FATAL_ERROR "${filepath} or ${manifestpath} doesn't exists !")
118 # Make sure the variable is cleared
119 set(target_libraries )
122 if(EXISTS ${filepath})
123 # Let's make sure target_libraries contains only strings
124 file(STRINGS "${filepath}" stringtocheck) # read content of 'filepath' into 'stringtocheck'
125 string(REGEX MATCHALL "[^\\#]\\$\\{.*\\}" incorrect_elements ${stringtocheck})
126 foreach(incorrect_element ${incorrect_elements})
127 string(REGEX REPLACE "\\$|\\{|\\}" "" correct_element ${incorrect_element})
128 message(FATAL_ERROR "In ${filepath}, ${incorrect_element} should be replaced by ${correct_element}")
133 list(APPEND target_library_list ${target_libraries})
136 if(EXISTS ${manifestpath})
137 # Let's make sure Require-Plugins contains only strings
138 file(STRINGS "${manifestpath}" stringtocheck) # read content of 'manifestpath' into 'stringtocheck'
139 string(REGEX MATCHALL "[^\\#]\\$\\{.*\\}" incorrect_elements ${stringtocheck})
140 foreach(incorrect_element ${incorrect_elements})
141 string(REGEX REPLACE "\\$|\\{|\\}" "" correct_element ${incorrect_element})
142 message(FATAL_ERROR "In ${manifestpath}, ${incorrect_element} should be replaced by ${correct_element}")
145 include(${manifestpath})
147 # Loop over all plugin dependencies
148 foreach(plugin_symbolicname ${Require-Plugin})
149 string(REPLACE "." "_" plugin_library ${plugin_symbolicname})
150 list(APPEND target_library_list ${plugin_library})
154 if(target_library_list)
155 list(REMOVE_DUPLICATES target_library_list)
158 # Pass the list of target libraries to the caller
159 set(${varname} ${target_library_list} PARENT_SCOPE)
162 #! \ingroup CMakeUtilities
163 macro(ctkMacroCollectAllTargetLibraries targets subdir varname)
166 if(${subdir} STREQUAL "Libs")
167 set(option_prefix CTK_LIB_)
168 elseif(${subdir} STREQUAL "Plugins")
169 set(option_prefix CTK_PLUGIN_)
170 elseif(${subdir} STREQUAL "Applications")
171 set(option_prefix CTK_APP_)
173 message(FATAL_ERROR "Unknown subdir:${subdir}, expected value are: 'Libs, 'Plugins' or 'Applications'")
176 foreach(target ${targets})
178 # Make sure the variable is cleared
179 set(target_libraries )
181 set(option_name ${option_prefix}${target})
182 #message(STATUS option_name:${option_name})
184 if(${target}_SOURCE_DIR)
185 set(target_dir "${${target}_SOURCE_DIR}")
187 set(target_dir "${CTK_SOURCE_DIR}/${subdir}/${target}")
189 #message(STATUS target_dir:${target_dir})
191 set(target_libraries)
193 # Collect target libraries only if option is ON
195 ctkFunctionCollectTargetLibraryNames(${target_dir} target_libraries)
199 list(APPEND ${varname} ${target_libraries})
200 list(REMOVE_DUPLICATES ${varname})
207 #! Extract all library names which are build within this project
209 #! \ingroup CMakeUtilities
210 macro(ctkMacroGetAllProjectTargetLibraries all_target_libraries varname)
211 # Allow external projects to override the set of internal targets
212 if(COMMAND GetMyTargetLibraries)
213 GetMyTargetLibraries("${all_target_libraries}" ${varname})
215 set(re_ctklib "^(c|C)(t|T)(k|K)[a-zA-Z0-9]+$")
216 set(re_ctkplugin "^org_commontk_[a-zA-Z0-9_]+$")
218 list(APPEND _tmp_list ${all_target_libraries})
219 #message("calling ctkMacroListFilter with varname:${varname}")
220 ctkMacroListFilter(_tmp_list re_ctklib re_ctkplugin OUTPUT_VARIABLE ${varname})
221 #message(STATUS "getallctklibs from ${all_target_libraries}")
222 #message(STATUS varname:${varname}:${${varname}})
227 #! Extract all library names *NOT* being build within this project
229 #! \ingroup CMakeUtilities
230 macro(ctkMacroGetAllNonProjectTargetLibraries all_target_libraries varname)
231 ctkMacroGetAllProjectTargetLibraries("${all_target_libraries}" all_project_libraries)
232 set(_tmp_list ${all_target_libraries})
233 if(all_project_libraries)
234 list(REMOVE_ITEM _tmp_list ${all_project_libraries})
236 set(${varname} ${_tmp_list})
237 #message(varname:${varname}:${${varname}})
240 #! \ingroup CMakeUtilities
241 macro(ctkMacroShouldAddExternalProject libraries_variable_name resultvar)
242 set(${resultvar} FALSE)
243 if(DEFINED NON_CTK_DEPENDENCIES)
244 list(FIND NON_CTK_DEPENDENCIES ${libraries_variable_name} index)
246 if(${index} GREATER -1)
247 set(${resultvar} TRUE)