Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkFunctionWhitelists.cmake
Go to the documentation of this file.
1 ###############################################################################
2 #
3 # mitkFunctionCreateWhitelistPaths
4 #
5 #! Creates advanced cache variables for setting the internal and external
6 #! whitelist directories.
7 #!
8 #! USAGE:
9 #!
10 #! \code
11 #! mitkFunctionCreateWhitelistPaths(<prefix>)
12 #! \endcode
13 #!
14 #! The <prefix> parameter specifies the prefix used for the created variables
15 #! <prefix>_WHITELISTS_INTERNAL_PATH and <prefix>_WHITELISTS_EXTERNAL_PATH.
16 #!
17 #! Default values:
18 #! <prefix>_WHITELISTS_INTERNAL_PATH = <prefix>_SOURCE_DIR/CMake/Whitelists
19 #! <prefix>_WHITELISTS_EXTERNAL_PATH = %HOME%/.mitk/Whitelists
20 #!
21 #! List of variables available after the function is called:
22 #! - <prefix>_WHITELISTS_INTERNAL_PATH
23 #! - <prefix>_WHITELISTS_EXTERNAL_PATH
24 #!
25 #! Parameters:
26 #! \param <prefix> The prefix of the created cache variables.
27 #
28 ###############################################################################
29 
31  set(${ARGV0}_WHITELISTS_INTERNAL_PATH "${${ARGV0}_SOURCE_DIR}/CMake/Whitelists" CACHE PATH "")
32  set(${ARGV0}_WHITELISTS_EXTERNAL_PATH ".mitk/Whitelists")
33 
34  if(WIN32)
35  set(${ARGV0}_WHITELISTS_EXTERNAL_PATH "$ENV{HOMEDRIVE}$ENV{HOMEPATH}/${${ARGV0}_WHITELISTS_EXTERNAL_PATH}")
36  else()
37  set(${ARGV0}_WHITELISTS_EXTERNAL_PATH "$ENV{HOME}/${${ARGV0}_WHITELISTS_EXTERNAL_PATH}")
38  endif()
39 
40  FILE(TO_CMAKE_PATH "${${ARGV0}_WHITELISTS_EXTERNAL_PATH}" ${ARGV0}_WHITELISTS_EXTERNAL_PATH)
41 
42  set(${ARGV0}_WHITELISTS_EXTERNAL_PATH "${${ARGV0}_WHITELISTS_EXTERNAL_PATH}" CACHE PATH "")
43 
44  mark_as_advanced(
45  ${ARGV0}_WHITELISTS_INTERNAL_PATH
46  ${ARGV0}_WHITELISTS_EXTERNAL_PATH
47  )
48 endfunction()
49 
50 ###############################################################################
51 #
52 # mitkFunctionFindWhitelists
53 #
54 #! Adds all whitelists found in specfied whitelist paths to the advanced cache
55 #! variable <prefix>_WHITELIST as enumeration entries.
56 #!
57 #! USAGE:
58 #!
59 #! \code
60 #! mitkFunctionFindWhitelists(<prefix>)
61 #! \endcode
62 #!
63 #! The <prefix> parameter specifies the prefix used for the created
64 #! cache variable <prefix>_WHITELIST. Its default value is "None".
65 #! The function mitkFunctionCreateWhitelistPaths must be called
66 #! with the same <prefix> prior to this function.
67 #!
68 #! Whitelists are *.cmake files which set the two list variables
69 #! enabled_modules and enabled_plugins.
70 #!
71 #! List of variables available after the function is called:
72 #! - <prefix>_WHITELIST
73 #!
74 #! \sa mitkFunctionCreateWhitelistPaths
75 #!
76 #! Parameters:
77 #! \param <prefix> The prefix of the created cache variable.
78 #
79 ###############################################################################
80 
81 function(mitkFunctionFindWhitelists)
82  set(whitelists "None")
83 
84  file(GLOB internalWhitelistFiles "${${ARGV0}_WHITELISTS_INTERNAL_PATH}/*.cmake")
85 
86  foreach(whitelistFile ${internalWhitelistFiles})
87  get_filename_component(whitelistFile "${whitelistFile}" NAME_WE)
88  list(APPEND whitelists "${whitelistFile}")
89  endforeach()
90 
91  file(GLOB externalWhitelistFiles "${${ARGV0}_WHITELISTS_EXTERNAL_PATH}/*.cmake")
92 
93  foreach(whitelistFile ${externalWhitelistFiles})
94  get_filename_component(whitelistFile "${whitelistFile}" NAME_WE)
95  list(APPEND whitelists "${whitelistFile} (external)")
96  endforeach()
97 
98  set(${ARGV0}_WHITELIST "None" CACHE STRING "")
99  set_property(CACHE ${ARGV0}_WHITELIST PROPERTY STRINGS ${whitelists})
100 
101  mark_as_advanced(${ARGV0}_WHITELIST)
102 endfunction()
103 
104 ###############################################################################
105 #
106 # mitkFunctionWhitelistModules
107 #
108 #! Only enables modules which are present in the currently set whitelist or
109 #! all modules if no whitelist is specified at all.
110 #!
111 #! USAGE:
112 #!
113 #! \code
114 #! set(<modules>
115 #! ModuleDir
116 #! AnotherModuleDir
117 #! ...
118 #! )
119 #! mitkFunctionWhitelistModules(<prefix> <modules>)
120 #! \endcode
121 #!
122 #! The <prefix> parameter specifies the prefix used to get the
123 #! currently set whitelist from <prefix>_WHITELIST. Both functions
124 #! mitkFunctionCreateWhitelistPaths and mitkFunctionFindWhitelists
125 #! must be called with the same <prefix> prior to this function.
126 #! The <modules> list must contain the module directory names instead
127 #! of the module names itself, as the entries are used in
128 #! add_directory calls.
129 #!
130 #! \sa mitkFunctionCreateWhitelistPaths
131 #! \sa mitkFunctionFindWhitelists
132 #!
133 #! Parameters:
134 #! \param <prefix> The prefix of the white list cache variable.
135 #! \param <modules> The module directory list variable.
136 #
137 ###############################################################################
138 
139 function(mitkFunctionWhitelistModules)
140  if(${ARGV0}_WHITELIST STREQUAL "None")
141  foreach(module ${${ARGV1}})
142  add_subdirectory(${module})
143  endforeach()
144  else()
145  string(FIND "${${ARGV0}_WHITELIST}" " (external)" index REVERSE)
146 
147  if(${index} EQUAL -1)
148  set(whitelistFile "${${ARGV0}_WHITELISTS_INTERNAL_PATH}/${${ARGV0}_WHITELIST}.cmake")
149  else()
150  string(SUBSTRING "${${ARGV0}_WHITELIST}" 0 ${index} whitelistFile)
151  set(whitelistFile "${${ARGV0}_WHITELISTS_EXTERNAL_PATH}/${whitelistFile}.cmake")
152  endif()
153 
154  include(${whitelistFile})
155 
156  if(NOT DEFINED enabled_modules)
157  message(FATAL_ERROR "Variable 'enabled_modules' not set in whitelist file '${whitelistFile}'!")
158  endif()
159 
160  foreach(module ${${ARGV1}})
161  list(FIND enabled_modules ${module} index)
162  if(NOT index EQUAL -1)
163  add_subdirectory(${module})
164  endif()
165  endforeach()
166  endif()
167 endfunction()
168 
169 ###############################################################################
170 #
171 # mitkFunctionWhitelistPlugins
172 #
173 #! Only enables plugins which are present in the currently set whitelist or
174 #! all plugins if no whitelist is specified at all.
175 #!
176 #! USAGE:
177 #!
178 #! \code
179 #! set(<plugins>
180 #! org.example.plugin:OFF
181 #! org.example.another.plugin:ON
182 #! ...
183 #! )
184 #! mitkFunctionWhitelistPlugins(<prefix> <plugins>)
185 #! \endcode
186 #!
187 #! The <prefix> parameter specifies the prefix used to get the
188 #! currently set whitelist from <prefix>_WHITELIST. Both functions
189 #! mitkFunctionCreateWhitelistPaths and mitkFunctionFindWhitelists
190 #! must be called with the same <prefix> prior to this function.
191 #! The <plugins> list must contain the plugin names. This function
192 #! removes plugins not found in the currently set whitelist from
193 #! the <plugins> variable. Note that plugins which are OFF by
194 #! default are not switched on.
195 #!
196 #! \sa mitkFunctionCreateWhitelistPaths
197 #! \sa mitkFunctionFindWhitelists
198 #!
199 #! Parameters:
200 #! \param <prefix> The prefix of the white list cache variable.
201 #! \param <plugins> The plugin list variable to be modified.
202 #
203 ###############################################################################
204 
205 function(mitkFunctionWhitelistPlugins)
206  if(${ARGV0}_WHITELIST STREQUAL "None")
207  return()
208  endif()
209 
210  string(FIND "${${ARGV0}_WHITELIST}" " (external)" index REVERSE)
211 
212  if(${index} EQUAL -1)
213  set(whitelistFile "${${ARGV0}_WHITELISTS_INTERNAL_PATH}/${${ARGV0}_WHITELIST}.cmake")
214  else()
215  string(SUBSTRING "${${ARGV0}_WHITELIST}" 0 ${index} whitelistFile)
216  set(whitelistFile "${${ARGV0}_WHITELISTS_EXTERNAL_PATH}/${whitelistFile}.cmake")
217  endif()
218 
219  include(${whitelistFile})
220 
221  if(NOT DEFINED enabled_plugins)
222  message(FATAL_ERROR "Variable 'enabled_plugins' not set in whitelist file '${whitelistFile}'!")
223  endif()
224 
225  set(plugins "")
226 
227  foreach(plugin ${${ARGV1}})
228  string(FIND ${plugin} ":" index REVERSE)
229 
230  if (NOT index EQUAL -1)
231  string(SUBSTRING ${plugin} 0 ${index} _plugin)
232  else()
233  set(_plugin ${plugin})
234  endif()
235 
236  list(FIND enabled_plugins ${_plugin} index)
237 
238  if(NOT index EQUAL -1)
239  list(APPEND plugins ${plugin})
240  endif()
241  endforeach()
242 
243  set(${ARGV1} ${plugins} PARENT_SCOPE)
244 endfunction()
mitkFunctionCreateWhitelistPaths()