CTK 0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
Loading...
Searching...
No Matches
ctkFunctionGenerateDGraphInput.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#! Generate a DGrapgh input file expected by DGraph executable.
23#!
24#! \ingroup CMakeAPI
25function(ctkFunctionGenerateDGraphInput dir target_directories)
26 if(NOT EXISTS ${dir})
27 message(FATAL_ERROR "Directory ${dir} doesn't exist!")
28 endif()
29
30 CtkMacroParseArguments(MY
31 ""
32 "WITH_OPTION;WITH_EXTERNALS"
33 ${ARGN}
34 )
35
36 set(dgraph_list )
37
38 set(edges)
39 set(vertices)
40 set(isolated_vertex_candidates)
41
42 foreach(target_info ${target_directories})
43
44 # extract target_dir and option_name
45 string(REPLACE "^^" "\\;" target_info ${target_info})
46 set(target_info_list ${target_info})
47 list(GET target_info_list 0 target_dir)
48 list(GET target_info_list 1 option_name)
49 #message(STATUS target_dir:${target_dir})
50 #message(STATUS option_name:${option_name})
51 #message(STATUS option_name-value:${${option_name}})
52
53 # make sure the directory exists
54 if(NOT EXISTS ${target_dir}/CMakeLists.txt)
55 message(FATAL_ERROR "Target directory ${target_dir}/CMakeLists.txt doesn't exists !")
56 endif()
57
58 set(include_dep 1)
59 if(MY_WITH_OPTION)
60 set(include_dep ${${option_name}})
61 endif()
62 if(${include_dep})
63 # extract project name from CMakeLists.txt
64 file(STRINGS "${target_dir}/CMakeLists.txt" project_string
65 REGEX "^ *(P|p)(R|r)(O|o)(J|j)(E|e)(C|c)(T|t)\\‍("
66 LIMIT_COUNT 10)
67 string(REGEX MATCH "\\‍((.*)\\‍)" target_project_name ${project_string})
68 string(REGEX REPLACE "\\‍(|\\‍)" "" target_project_name ${target_project_name})
69 if(${target_project_name} STREQUAL "")
70 message(FATAL_ERROR "Failed to extract project name from ${target_dir}/CMakeLists.txt")
71 endif()
72 #message(STATUS target_project_name:${target_project_name})
73
74 # Make sure the variable is cleared
75 set(dependencies )
76
77 # get dependencies
78 ctkFunctionCollectTargetLibraryNames(${target_dir} dependencies)
79 if(${target_project_name}_OPTIONAL_DEPENDENCIES)
80 list(APPEND dependencies ${${target_project_name}_OPTIONAL_DEPENDENCIES})
81 endif()
82
83 # Make sure the variable is cleared
84 set(ctk_dependencies)
85
86 if(MY_WITH_EXTERNALS)
87 set(ctk_dependencies ${dependencies})
88 else()
89 # filter dependencies starting with CTK org org_commontk_
90 ctkMacroGetAllProjectTargetLibraries("${dependencies}" ctk_dependencies)
91 endif()
92
93 if(ctk_dependencies)
94 list(APPEND vertices ${target_project_name})
95 else()
96 # isolated vertex candidate
97 list(APPEND isolated_vertex_candidates ${target_project_name})
98 endif()
99
100 # Generate XML related to the dependencies
101 foreach(dependency_name ${ctk_dependencies})
102 list(APPEND edges ${dependency_name})
103 set(dgraph_list ${dgraph_list} "${target_project_name} ${dependency_name}\n")
104 list(APPEND vertices ${dependency_name})
105 endforeach()
106
107 endif()
108
109 endforeach()
110
111 foreach(isolated_vertex_candidate ${isolated_vertex_candidates})
112 set(_found 0)
113 foreach(dgraph_entry ${dgraph_list})
114 string(REPLACE "\n" "" dgraph_entry "${dgraph_entry}")
115 string(REPLACE " " ";" dgraph_entry "${dgraph_entry}")
116 list(FIND dgraph_entry ${isolated_vertex_candidate} _index)
117 if(_index GREATER -1)
118 set(_found 1)
119 break()
120 endif()
121 endforeach()
122
123 if(NOT _found)
124 list(APPEND vertices ${isolated_vertex_candidate})
125 set(dgraph_list "${isolated_vertex_candidate}\n" ${dgraph_list})
126 endif()
127 endforeach()
128
129 if(vertices)
130 list(REMOVE_DUPLICATES vertices)
131 endif()
132 list(LENGTH vertices numberOfVertices)
133 list(LENGTH edges numberOfEdges)
134
135 set(dgraph_list "${numberOfVertices} ${numberOfEdges}\n" ${dgraph_list})
136
137 if(MY_WITH_OPTION)
138 set(filename "${dir}/DGraphInput.txt")
139 elseif(MY_WITH_EXTERNALS)
140 set(filename "${dir}/DGraphInput-alldep-withext.txt")
141 else()
142 set(filename "${dir}/DGraphInput-alldep.txt")
143 endif()
144
145 file(WRITE ${filename} ${dgraph_list})
146 message(STATUS "Generated: ${filename}")
147endfunction()