CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkMacroTargetLibraries.cmake
Go to the documentation of this file.
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 #!
22 #! This macro could be invoked using two different signatures:
23 #! ctkFunctionGetTargetLibraries(TARGET_LIBS)
24 #! or
25 #! ctkFunctionGetTargetLibraries(TARGET_LIBS "/path/to/ctk_target_dir")
26 #!
27 #! Without specifying the second argument, the current folder will be used.
28 #!
29 #! \ingroup CMakeUtilities
30 function(ctkFunctionGetTargetLibraries varname)
31 
32  set(expanded_target_library_list)
33 
34  set(TARGET_DIRECTORY ${ARGV1})
35  set(_target_name )
36  if("${TARGET_DIRECTORY}" STREQUAL "")
37  set(TARGET_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
38  set(_target_name ${PROJECT_NAME})
39  endif()
40 
41  set(filepath ${TARGET_DIRECTORY}/target_libraries.cmake)
42  set(manifestpath ${TARGET_DIRECTORY}/manifest_headers.cmake)
43 
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 !")
47  endif()
48 
49  # Make sure the variable is cleared
50  set(target_libraries )
51  set(Require-Plugin )
52 
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}")
60  endforeach()
61 
62  include(${filepath})
63 
64  if(_target_name)
65  list(APPEND target_libraries "${${_target_name}_OPTIONAL_DEPENDENCIES}")
66  endif()
67 
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})
74  else()
75  list(APPEND expanded_target_library_list "${${target_library}}")
76  endif()
77  endforeach()
78  endif()
79 
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}")
87  endforeach()
88 
89  include(${manifestpath})
90 
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})
95  endforeach()
96  endif()
97 
98  # Pass the list of target libraries to the caller
99  set(${varname} ${expanded_target_library_list} PARENT_SCOPE)
100 
101 endfunction()
102 
103 #! \ingroup CMakeUtilities
104 function(ctkFunctionCollectTargetLibraryNames target_dir varname)
105 
106  set(target_library_list)
107  #message(STATUS target:${target})
108  set(lib_targets)
109 
110  set(filepath ${target_dir}/target_libraries.cmake)
111  set(manifestpath ${target_dir}/manifest_headers.cmake)
112 
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 !")
116  endif()
117 
118  # Make sure the variable is cleared
119  set(target_libraries )
120  set(Require-Plugin )
121 
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}")
129  endforeach()
130 
131  include(${filepath})
132 
133  list(APPEND target_library_list ${target_libraries})
134  endif()
135 
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}")
143  endforeach()
144 
145  include(${manifestpath})
146 
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})
151  endforeach()
152  endif()
153 
154  if(target_library_list)
155  list(REMOVE_DUPLICATES target_library_list)
156  endif()
157 
158  # Pass the list of target libraries to the caller
159  set(${varname} ${target_library_list} PARENT_SCOPE)
160 endfunction()
161 
162 #! \ingroup CMakeUtilities
163 macro(ctkMacroCollectAllTargetLibraries targets subdir varname)
164 
165  set(option_prefix)
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_)
172  else()
173  message(FATAL_ERROR "Unknown subdir:${subdir}, expected value are: 'Libs, 'Plugins' or 'Applications'")
174  endif()
175 
176  foreach(target ${targets})
177 
178  # Make sure the variable is cleared
179  set(target_libraries )
180 
181  set(option_name ${option_prefix}${target})
182  #message(STATUS option_name:${option_name})
183 
184  if(${target}_SOURCE_DIR)
185  set(target_dir "${${target}_SOURCE_DIR}")
186  else()
187  set(target_dir "${CTK_SOURCE_DIR}/${subdir}/${target}")
188  endif()
189  #message(STATUS target_dir:${target_dir})
190 
191  set(target_libraries)
192 
193  # Collect target libraries only if option is ON
194  if(${option_name})
195  ctkFunctionCollectTargetLibraryNames(${target_dir} target_libraries)
196  endif()
197 
198  if(target_libraries)
199  list(APPEND ${varname} ${target_libraries})
200  list(REMOVE_DUPLICATES ${varname})
201  endif()
202  endforeach()
203 
204 endmacro()
205 
206 #!
207 #! Extract all library names which are build within this project
208 #!
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})
214  else()
215  set(re_ctklib "^(c|C)(t|T)(k|K)[a-zA-Z0-9]+$")
216  set(re_ctkplugin "^org_commontk_[a-zA-Z0-9_]+$")
217  set(_tmp_list)
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}})
223  endif()
224 endmacro()
225 
226 #!
227 #! Extract all library names *NOT* being build within this project
228 #!
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})
235  endif()
236  set(${varname} ${_tmp_list})
237  #message(varname:${varname}:${${varname}})
238 endmacro()
239 
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)
245 
246  if(${index} GREATER -1)
247  set(${resultvar} TRUE)
248  endif()
249  endif()
250 endmacro()