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 ###########################################################################
21 #! \ingroup CMakeUtilities
22 macro(ctkMacroGetOptionName target_directories_with_target_name target_name option_name_var)
24 foreach(target_info ${target_directories_with_target_name})
25 # extract target_dir and option_name
26 #message("target_info:${target_info}")
27 string(REPLACE "^^" "\\;" target_info_list ${target_info})
28 set(target_info_list ${target_info_list})
29 list(GET target_info_list 0 _target_dir)
30 list(GET target_info_list 1 _option_name)
31 list(GET target_info_list 2 _target_name)
32 if(${_target_name} STREQUAL ${target_name})
33 set(${option_name_var} ${_option_name})
38 #! \ingroup CMakeUtilities
39 macro(ctkMacroValidateBuildOptions dir executable target_directories)
41 message(FATAL_ERROR "Directory ${dir} doesn't exist!")
44 if(NOT EXISTS ${executable})
45 message(FATAL_ERROR "Executable ${executable} doesn't exist!")
49 set(target_directories_with_target_name)
51 # Create target_directories_with_target_name
52 foreach(target_info ${target_directories})
54 # extract target_dir and option_name
55 string(REPLACE "^^" "\\;" target_info ${target_info})
56 set(target_info_list ${target_info})
57 list(GET target_info_list 0 target_dir)
58 list(GET target_info_list 1 option_name)
59 #message(STATUS target_dir:${target_dir})
60 #message(STATUS option_name:${option_name})
61 #message(STATUS option_name-value:${${option_name}})
63 # make sure the directory exists
64 if(NOT EXISTS ${target_dir}/CMakeLists.txt)
65 message(FATAL_ERROR "Target directory ${target_dir}/CMakeLists.txt doesn't exists !")
68 # extract project name from CMakeLists.txt
69 file(STRINGS "${target_dir}/CMakeLists.txt" project_string
70 REGEX "^ *(P|p)(R|r)(O|o)(J|j)(E|e)(C|c)(T|t)\\("
72 string(REGEX MATCH "\\((.*)\\)" target_project_name ${project_string})
73 string(REGEX REPLACE "\\(|\\)" "" target_project_name ${target_project_name})
74 if(${target_project_name} STREQUAL "")
75 message(FATAL_ERROR "Failed to extract project name from ${target_dir}/CMakeLists.txt")
77 #message(STATUS target_project_name:${target_project_name})
79 list(APPEND target_directories_with_target_name
80 ${target_dir}^^${option_name}^^${target_project_name}
83 list(APPEND known_targets ${target_project_name})
86 # This is for external projects using CTK
87 # The variable CTK_EXTERNAL_PLUGIN_LIBRARIES is filled in ctkMacroSetupExternalPlugins
88 # with the help of variables defined in "PluginUseFiles" from external projects.
89 if(CTK_EXTERNAL_PLUGIN_LIBRARIES)
90 list(APPEND known_targets ${CTK_EXTERNAL_PLUGIN_LIBRARIES})
92 # The variable CTK_LIBRARIES is set in CTKConfig.cmake
94 list(APPEND known_targets ${CTK_LIBRARIES})
97 #message("Known targets: ${known_targets}")
99 set(EXTERNAL_TARGETS ) # This is used in CMakeLists.txt
100 foreach(target_info ${target_directories_with_target_name})
102 # extract target_dir and option_name
103 string(REPLACE "^^" "\\;" target_info ${target_info})
104 set(target_info_list ${target_info})
105 list(GET target_info_list 0 target_dir)
106 list(GET target_info_list 1 option_name)
107 #message(STATUS target_dir:${target_dir})
108 #message(STATUS option_name:${option_name})
109 #message(STATUS option_name-value:${${option_name}})
111 # make sure the directory exists
112 if(NOT EXISTS ${target_dir}/CMakeLists.txt)
113 message(FATAL_ERROR "Target directory ${target_dir}/CMakeLists.txt doesn't exists !")
116 # extract project name from CMakeLists.txt
117 file(STRINGS "${target_dir}/CMakeLists.txt" project_string
118 REGEX "^ *(P|p)(R|r)(O|o)(J|j)(E|e)(C|c)(T|t)\\("
120 string(REGEX MATCH "\\((.*)\\)" target_project_name ${project_string})
121 string(REGEX REPLACE "\\(|\\)" "" target_project_name ${target_project_name})
122 if(${target_project_name} STREQUAL "")
123 message(FATAL_ERROR "Failed to extract project name from ${target_dir}/CMakeLists.txt")
125 #message(STATUS target_project_name:${target_project_name})
127 # Obtain dependency path
128 ctkMacroSetPaths("${QT_INSTALLED_LIBRARY_DIR}")
130 COMMAND "${executable}" "${dir}/DGraphInput-alldep-withext.txt" -sort ${target_project_name}
131 WORKING_DIRECTORY ${dir}
132 RESULT_VARIABLE RESULT_VAR
133 OUTPUT_VARIABLE dep_path
135 OUTPUT_STRIP_TRAILING_WHITESPACE
138 message(FATAL_ERROR "Failed to obtain dependence path of ${target_project_name}.\n${RESULT_VAR}\n${CTK_BINARY_DIR}\n${error}")
141 # Set a variable for each target containing its dependencies
142 # Needed for setting individual include directories for plugins,
143 # depending on other plugins.
144 set(${target_project_name}_DEPENDENCIES )
146 # Convert 'dep_path' to a list
147 string(REPLACE " " "\\;" dep_path_list ${dep_path})
148 set(dep_path_list ${dep_path_list})
149 list(REMOVE_ITEM dep_path_list ${target_project_name})
150 list(APPEND ${target_project_name}_DEPENDENCIES ${dep_path_list})
152 #message("path for ${target_project_name} is: ${dep_path_list}")
154 # Check if all internal CTK targets included in the dependency path are enabled
155 set(int_dep_path_list )
156 set(ext_dep_path_list ${dep_path_list})
157 ctkMacroGetAllProjectTargetLibraries("${dep_path_list}" int_dep_path_list)
158 if(int_dep_path_list)
159 list(REMOVE_ITEM ext_dep_path_list ${int_dep_path_list})
162 if(ext_dep_path_list)
163 list(APPEND EXTERNAL_TARGETS ${ext_dep_path_list})
164 # If this macro is called from inside CTK itself, we add the external
165 # targets to the list of known targets (for external projects calling
166 # this macro, targets external to the calling project should be listed
167 # in CTK_LIBRARIES or CTK_EXTERNAL_PLUGIN_LIBRARIES
169 if(${CMAKE_SOURCE_DIR} STREQUAL ${CTK_SOURCE_DIR})
170 list(APPEND known_targets ${ext_dep_path_list})
175 foreach(dep ${int_dep_path_list})
176 list(FIND known_targets ${dep} dep_found)
178 message(FATAL_ERROR "${target_project_name} depends on ${dep}, which does not exist")
181 ctkMacroGetOptionName("${target_directories_with_target_name}" ${dep} dep_option)
182 if(${${option_name}} AND NOT ${${dep_option}})
184 message(STATUS "Enabling option [${dep_option}] required by [${target_project_name}]")
185 set(${dep_option} ON CACHE BOOL "Enable ${target_project_name} library" FORCE)
189 # For enabled targets, check if all external targets included in the dependency path are available
190 if(${${option_name}})
191 foreach(dep ${ext_dep_path_list})
192 list(FIND known_targets ${dep} dep_found)
194 message(FATAL_ERROR "${target_project_name} depends on unknown external targets: ${dep}")
202 list(REMOVE_DUPLICATES EXTERNAL_TARGETS)
205 #message(STATUS "Validated: CTK_LIB_*, CTK_PLUGIN_*, CTK_APP_*")