1#-----------------------------------------------------------------------------
2# Private helper macros.
3
4# _vtk_module_config_recurse(<namespace> <module>)
5#
6# Internal macro to recursively load module information into the supplied
7# namespace, this is called from vtk_module_config. It should be noted that
8# _${ns}_${mod}_USED must be cleared if this macro is to work correctly on
9# subsequent invocations. The macro will load the module files using the
10# vtk_module_load, making all of its variables available in the local scope.
11macro(_vtk_module_config_recurse ns mod)
12  if(NOT _${ns}_${mod}_USED)
13    set(_${ns}_${mod}_USED 1)
14    list(APPEND _${ns}_USED_MODULES ${mod})
15    vtk_module_load("${mod}")
16    list(APPEND ${ns}_LIBRARIES ${${mod}_LIBRARIES})
17    list(APPEND ${ns}_INCLUDE_DIRS ${${mod}_INCLUDE_DIRS})
18    list(APPEND ${ns}_LIBRARY_DIRS ${${mod}_LIBRARY_DIRS})
19    list(APPEND ${ns}_RUNTIME_LIBRARY_DIRS ${${mod}_RUNTIME_LIBRARY_DIRS})
20    foreach(iface IN LISTS ${mod}_IMPLEMENTS)
21      list(APPEND _${ns}_AUTOINIT_${iface} ${mod})
22      list(APPEND _${ns}_AUTOINIT ${iface})
23    endforeach()
24    foreach(dep IN LISTS ${mod}_DEPENDS)
25      _vtk_module_config_recurse("${ns}" "${dep}")
26    endforeach()
27  endif()
28endmacro()
29
30#-----------------------------------------------------------------------------
31# Public interface macros.
32
33# vtk_module_load(<module>)
34#
35# Loads variables describing the given module, these include custom variables
36# set by the module along with the standard ones listed below:
37#  <module>_LOADED         = True if the module has been loaded
38#  <module>_DEPENDS        = List of dependencies on other modules
39#  <module>_LIBRARIES      = Libraries to link
40#  <module>_INCLUDE_DIRS   = Header search path
41#  <module>_LIBRARY_DIRS   = Library search path (for outside dependencies)
42#  <module>_RUNTIME_LIBRARY_DIRS = Runtime linker search path
43macro(vtk_module_load mod)
44  if(NOT ${mod}_LOADED)
45    include("${VTK_MODULES_DIR}/${mod}.cmake" OPTIONAL RESULT_VARIABLE _found)
46    if(NOT _found)
47      # When building applications outside VTK, they can provide extra module
48      # config files by simply adding the corresponding locations to the
49      # CMAKE_MODULE_PATH
50      include(${mod} OPTIONAL)
51    endif()
52    if(NOT ${mod}_LOADED)
53      message(FATAL_ERROR "No such module: \"${mod}\"")
54    endif()
55  endif()
56endmacro()
57
58# vtk_module_dep_includes(<module>)
59#
60# Loads the <module>_DEPENDS_INCLUDE_DIRS variable.
61macro(vtk_module_dep_includes mod)
62  vtk_module_load("${mod}")
63  vtk_module_config(_dep_${mod} ${${mod}_DEPENDS})
64  if(_dep_${mod}_INCLUDE_DIRS)
65    set(${mod}_DEPENDS_INCLUDE_DIRS ${_dep_${mod}_INCLUDE_DIRS})
66  endif()
67endmacro()
68
69# vtk_module_headers_load(<module>)
70#
71# Loads variables describing the headers/API of the given module, this is not
72# loaded by vtk_module_config, and is mainly useful for wrapping generation:
73#  <module>_HEADERS_LOADED      = True if the module header info has been loaded
74#  <module>_HEADERS             = List of headers
75#  <module>_HEADER_<header>_EXISTS
76#  <module>_HEADER_<header>_ABSTRACT
77#  <module>_HEADER_<header>_WRAP_EXCLUDE
78#  <module>_HEADER_<header>_WRAP_SPECIAL
79macro(vtk_module_headers_load mod)
80  if(NOT ${mod}_HEADERS_LOADED)
81    include("${VTK_MODULES_DIR}/${mod}-Headers.cmake"
82      OPTIONAL RESULT_VARIABLE _found)
83    if(NOT _found)
84      # When building applications outside VTK, they can provide extra module
85      # config files by simply adding the corresponding locations to the
86      # CMAKE_MODULE_PATH
87      include(${mod}-Headers OPTIONAL)
88    endif()
89    if(NOT ${mod}_HEADERS_LOADED)
90      message(FATAL_ERROR "No such module: \"${mod}\"")
91    endif()
92  endif()
93endmacro()
94
95# vtk_module_config(<namespace> [modules...])
96#
97# Configures variables describing the given modules and their dependencies:
98#  <namespace>_DEFINITIONS  = Preprocessor definitions
99#  <namespace>_LIBRARIES    = Libraries to link
100#  <namespace>_INCLUDE_DIRS = Header search path
101#  <namespace>_LIBRARY_DIRS = Library search path (for outside dependencies)
102#  <namespace>_RUNTIME_LIBRARY_DIRS = Runtime linker search path
103#
104# Calling this macro also recursively calls vtk_module_load for all modules
105# explicitly named, and their dependencies, making them available in the local
106# scope. This means that module level information can be accessed once this
107# macro has been called.
108#
109# Do not name a module as the namespace.
110macro(vtk_module_config ns)
111  set(_${ns}_MISSING ${ARGN})
112  if(_${ns}_MISSING)
113    list(REMOVE_ITEM _${ns}_MISSING ${VTK_MODULES_ENABLED})
114  endif()
115  if(_${ns}_MISSING)
116    set(msg "")
117    foreach(mod ${_${ns}_MISSING})
118      set(msg "${msg}\n  ${mod}")
119    endforeach()
120    message(FATAL_ERROR "Requested modules not available:${msg}")
121  endif()
122
123  set(${ns}_DEFINITIONS "")
124  set(${ns}_LIBRARIES "")
125  set(${ns}_INCLUDE_DIRS "")
126  set(${ns}_LIBRARY_DIRS "")
127  set(${ns}_RUNTIME_LIBRARY_DIRS "")
128  set(_${ns}_AUTOINIT "")
129
130  set(_${ns}_USED_MODULES "")
131  foreach(mod ${ARGN})
132    _vtk_module_config_recurse("${ns}" "${mod}")
133  endforeach()
134  foreach(mod ${_${ns}_USED_MODULES})
135    unset(_${ns}_${mod}_USED)
136  endforeach()
137  unset(_${ns}_USED_MODULES)
138
139  foreach(v ${ns}_LIBRARIES ${ns}_INCLUDE_DIRS ${ns}_LIBRARY_DIRS
140            ${ns}_RUNTIME_LIBRARY_DIRS _${ns}_AUTOINIT)
141    if(${v})
142      list(REMOVE_DUPLICATES ${v})
143    endif()
144  endforeach()
145
146  list(SORT _${ns}_AUTOINIT) # Deterministic order.
147  foreach(mod ${_${ns}_AUTOINIT})
148    list(SORT _${ns}_AUTOINIT_${mod}) # Deterministic order.
149    list(REMOVE_DUPLICATES _${ns}_AUTOINIT_${mod})
150    list(LENGTH _${ns}_AUTOINIT_${mod} _ai_len)
151    string(REPLACE ";" "," _ai "${_ai_len}(${_${ns}_AUTOINIT_${mod}})")
152    if(${_ai_len} GREATER 1 AND "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
153      # VS IDE project files cannot handle a comma (,) in a
154      # preprocessor definition value outside a quoted string.
155      # Generate a header file to do the definition and define
156      # ${mod}_INCLUDE to tell ${mod}Module.h to include it.
157      # Name the file after its content to guarantee uniqueness.
158      string(REPLACE ";" "_" _inc
159        "${CMAKE_BINARY_DIR}/CMakeFiles/${mod}_AUTOINIT_${_${ns}_AUTOINIT_${mod}}.h")
160      set(CMAKE_CONFIGURABLE_FILE_CONTENT "#define ${mod}_AUTOINIT ${_ai}")
161      configure_file(${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in ${_inc})
162      list(APPEND ${ns}_DEFINITIONS "${mod}_INCLUDE=\"${_inc}\"")
163    else()
164      # Directly define ${mod}_AUTOINIT.
165      list(APPEND ${ns}_DEFINITIONS "${mod}_AUTOINIT=${_ai}")
166    endif()
167    unset(_${ns}_AUTOINIT_${mod})
168  endforeach()
169  unset(_${ns}_AUTOINIT)
170endmacro()
171
172# vtk_add_to_module_search_path(<source> <build>)
173#
174# Call to add a single module to the module search path.
175macro(vtk_add_to_module_search_path src bld)
176  list(APPEND vtk_module_search_path "${src},${bld}")
177endmacro()
178