0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / adm / cmake / vtk.cmake
1 # vtk
2
3 if (NOT DEFINED INSTALL_VTK)
4   set (INSTALL_VTK OFF CACHE BOOL "${INSTALL_VTK_DESCR}")
5 endif()
6
7 # vtk directory
8 if (NOT DEFINED 3RDPARTY_VTK_DIR)
9   set (3RDPARTY_VTK_DIR "" CACHE PATH "The directory containing VTK")
10 endif()
11
12 # include occt macros. compiler_bitness, os_wiht_bit, compiler
13 OCCT_INCLUDE_CMAKE_FILE ("adm/cmake/occt_macros")
14
15 # specify VTK folder in connectin with 3RDPARTY_DIR
16 if (3RDPARTY_DIR AND EXISTS "${3RDPARTY_DIR}")
17   #CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_DIR 3RDPARTY_VTK_DIR PATH "The directory containing VTK")
18
19   if (NOT 3RDPARTY_VTK_DIR OR NOT EXISTS "${3RDPARTY_VTK_DIR}")
20     FIND_PRODUCT_DIR ("${3RDPARTY_DIR}" VTK VTK_DIR_NAME)
21     if (VTK_DIR_NAME)
22       set (3RDPARTY_VTK_DIR "${3RDPARTY_DIR}/${VTK_DIR_NAME}" CACHE PATH "The directory containing VTK" FORCE)
23     endif()
24   endif()
25 else()
26   #set (3RDPARTY_VTK_DIR "" CACHE PATH "The directory containing VTK" FORCE)
27 endif()
28
29 # vtk include directory
30 if (NOT DEFINED 3RDPARTY_VTK_INCLUDE_DIR)
31   set (3RDPARTY_VTK_INCLUDE_DIR "" CACHE PATH "The directory containing headers of VTK")
32 endif()
33
34 #if (BUILD_SHARED_LIBS)
35   # vtk library directory
36   if (NOT DEFINED 3RDPARTY_VTK_LIBRARY_DIR)
37     set (3RDPARTY_VTK_LIBRARY_DIR "" CACHE PATH "The directory containing VTK libraries")
38   endif()
39
40   # vtk dll directory
41   if (WIN32 AND NOT DEFINED 3RDPARTY_VTK_DLL_DIR)
42     set (3RDPARTY_VTK_DLL_DIR "" CACHE PATH "The directory containing VTK shared libraries")
43   endif()
44 #endif()
45
46 # check 3RDPARTY_VTK_ paths for consistency with specified 3RDPARTY_VTK_DIR
47 if (3RDPARTY_VTK_DIR AND EXISTS "${3RDPARTY_VTK_DIR}")
48   CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_VTK_DIR 3RDPARTY_VTK_INCLUDE_DIR PATH "The directory containing headers of VTK")
49   if (BUILD_SHARED_LIBS)
50     CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_VTK_DIR 3RDPARTY_VTK_LIBRARY_DIR PATH "The directory containing VTK libraries")
51
52     if (WIN32)
53       CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_VTK_DIR 3RDPARTY_VTK_DLL_DIR PATH "The directory containing VTK shared library")
54     endif()
55   endif()
56 endif()
57
58 if (3RDPARTY_VTK_DIR AND EXISTS "${3RDPARTY_VTK_DIR}")
59   set (CACHED_VTK_DIR $ENV{VTK_DIR})
60   set (ENV{VTK_DIR} "${3RDPARTY_VTK_DIR}")
61 endif()
62
63 find_package(VTK QUIET)
64
65 if (3RDPARTY_VTK_DIR AND EXISTS "${3RDPARTY_VTK_DIR}")
66   set (ENV{VTK_DIR} ${CACHED_VTK_DIR})
67 endif()
68
69 unset (IS_VTK_9XX)
70 if (VTK_FOUND)
71   message ("VTK version (${VTK_VERSION})")
72   if(VTK_MAJOR_VERSION EQUAL 8 AND VTK_MINOR_VERSION GREATER 9 OR VTK_MAJOR_VERSION GREATER 8)
73     set (IS_VTK_9XX 1)
74   else()
75     # add compiler flags, preprocessor definitions, include and link dirs
76     include (${VTK_USE_FILE})
77   endif()
78
79   if (VTK_LIBRARIES)
80
81     set (3RDPARTY_VTK_INCLUDE_DIRS)
82
83 #    if (BUILD_SHARED_LIBS)
84       set (3RDPARTY_VTK_LIBRARY_DIRS)
85       set (3RDPARTY_VTK_DLL_DIRS)
86 #    endif()
87
88     foreach (VTK_LIBRARY ${VTK_LIBRARIES})
89       if (IS_VTK_9XX)
90         string (REGEX MATCH "^VTK::" IS_VTK_LIBRARY ${VTK_LIBRARY})
91       else()
92         string (REGEX MATCH "^vtk" IS_VTK_LIBRARY ${VTK_LIBRARY})
93       endif()
94       if (NOT IS_VTK_LIBRARY OR NOT TARGET ${VTK_LIBRARY})
95         continue()
96       endif()
97
98       # get paths from corresponding variables
99       if (${VTK_LIBRARY}_INCLUDE_DIRS AND EXISTS "${${VTK_LIBRARY}_INCLUDE_DIRS}")
100         list (APPEND 3RDPARTY_VTK_INCLUDE_DIRS "${${VTK_LIBRARY}_INCLUDE_DIRS}")
101       endif()
102
103       if (${VTK_LIBRARY}_LIBRARY_DIRS AND EXISTS "${${VTK_LIBRARY}_LIBRARY_DIRS}")
104         list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${${VTK_LIBRARY}_LIBRARY_DIRS}")
105       endif()
106
107       if (${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS AND EXISTS "${${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS}")
108         list (APPEND 3RDPARTY_VTK_DLL_DIRS "${${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS}")
109         if (NOT WIN32)
110           list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS}")
111         endif()
112       endif()
113
114       # get paths from corresponding properties
115       get_target_property (TARGET_VTK_IMPORT_CONFS ${VTK_LIBRARY} IMPORTED_CONFIGURATIONS)
116       if (TARGET_VTK_IMPORT_CONFS)
117         list (GET TARGET_VTK_IMPORT_CONFS 0 CHOSEN_IMPORT_CONF)
118
119         # todo: choose configuration in connection with the build type
120         #if (CMAKE_BUILD_TYPE)
121         #  foreach (IMPORT_CONF ${TARGET_VTK_IMPORT_CONFS})
122         #  endforeach()
123         #endif()
124
125         # Work-around against link failure in case if VTK contains dependency
126         # on DirectX: its run-time is always present on Windows, but SDK can
127         # be absent on current workstation, while not actually needed for
128         # OCCT linking.
129         # VTK 6.1 for VC 10
130         get_target_property (TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES ${VTK_LIBRARY} IMPORTED_LINK_INTERFACE_LIBRARIES_${CHOSEN_IMPORT_CONF})
131         if(TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES)
132           string (REGEX MATCH "[^;]*d3d[0-9]+[.]lib" HARDCODED_D3D9_LIB "${TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES}")
133           if (HARDCODED_D3D9_LIB)
134             message (STATUS "Warning: ${HARDCODED_D3D9_LIB} has been removed from imported dependencies of ${VTK_LIBRARY}")
135
136             list (REMOVE_ITEM TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES ${HARDCODED_D3D9_LIB})
137             set_target_properties (${VTK_LIBRARY} PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES_${CHOSEN_IMPORT_CONF} "${TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES}")
138           endif()
139         endif()
140         # VTK 6.1 for VC 12, 14
141         get_target_property (TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES ${VTK_LIBRARY} INTERFACE_LINK_LIBRARIES)
142         if(TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES)
143           string (REGEX MATCH "[^;]*d3d[0-9]+[.]lib" HARDCODED_D3D9_LIB "${TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES}")
144           if (HARDCODED_D3D9_LIB)
145             message (STATUS "Warning: ${HARDCODED_D3D9_LIB} has been removed from imported dependencies of ${VTK_LIBRARY}")
146
147             list (REMOVE_ITEM TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES ${HARDCODED_D3D9_LIB})
148             set_target_properties (${VTK_LIBRARY} PROPERTIES INTERFACE_LINK_LIBRARIES "${TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES}")
149           endif()
150         endif()
151
152         get_target_property (TARGET_PROPERTY_IMP_PATH ${VTK_LIBRARY} IMPORTED_IMPLIB_${CHOSEN_IMPORT_CONF})
153         if(TARGET_PROPERTY_IMP_PATH AND EXISTS "${TARGET_PROPERTY_IMP_PATH}")
154           get_filename_component (TARGET_PROPERTY_IMP_DIR "${TARGET_PROPERTY_IMP_PATH}" PATH)
155           list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${TARGET_PROPERTY_IMP_DIR}")
156         endif()
157
158         get_target_property (TARGET_PROPERTY_LOCATION_PATH ${VTK_LIBRARY} IMPORTED_LOCATION_${CHOSEN_IMPORT_CONF})
159         if(TARGET_PROPERTY_LOCATION_PATH AND EXISTS "${TARGET_PROPERTY_LOCATION_PATH}")
160           get_filename_component (TARGET_PROPERTY_LOCATION_DIR "${TARGET_PROPERTY_LOCATION_PATH}" PATH)
161
162           if (WIN32)
163             list (APPEND 3RDPARTY_VTK_DLL_DIRS "${TARGET_PROPERTY_LOCATION_DIR}")
164           else()
165             list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${TARGET_PROPERTY_LOCATION_DIR}")
166           endif()
167         endif()
168       endif()
169     endforeach()
170   endif()
171
172   if (3RDPARTY_VTK_INCLUDE_DIRS)
173     list (REMOVE_DUPLICATES 3RDPARTY_VTK_INCLUDE_DIRS)
174     list (APPEND 3RDPARTY_INCLUDE_DIRS ${3RDPARTY_VTK_INCLUDE_DIRS})
175
176     list (GET 3RDPARTY_VTK_INCLUDE_DIRS 0 3RDPARTY_VTK_INCLUDE_DIR)
177     set (3RDPARTY_VTK_INCLUDE_DIR "${3RDPARTY_VTK_INCLUDE_DIR}" CACHE PATH "The directory containing headers of VTK" FORCE)
178   endif()
179
180 #  if (BUILD_SHARED_LIBS)
181     if (3RDPARTY_VTK_LIBRARY_DIRS)
182       list (REMOVE_DUPLICATES 3RDPARTY_VTK_LIBRARY_DIRS)
183       list (APPEND 3RDPARTY_LIBRARY_DIRS ${3RDPARTY_VTK_LIBRARY_DIRS})
184
185       list (GET 3RDPARTY_VTK_LIBRARY_DIRS 0 3RDPARTY_VTK_LIBRARY_DIR)
186       set (3RDPARTY_VTK_LIBRARY_DIR "${3RDPARTY_VTK_LIBRARY_DIR}" CACHE PATH "The directory containing VTK libraries" FORCE)
187     endif()
188
189     if (WIN32)
190       if (3RDPARTY_VTK_DLL_DIRS)
191         list (REMOVE_DUPLICATES 3RDPARTY_VTK_DLL_DIRS)
192         list (APPEND 3RDPARTY_DLL_DIRS ${3RDPARTY_VTK_DLL_DIRS})
193
194         list (GET 3RDPARTY_VTK_DLL_DIRS 0 3RDPARTY_VTK_DLL_DIR)
195         set (3RDPARTY_VTK_DLL_DIR "${3RDPARTY_VTK_DLL_DIR}" CACHE PATH "The directory containing VTK shared libraries" FORCE)
196       endif()
197     endif()
198 #  endif()
199 endif()
200
201 if (3RDPARTY_VTK_INCLUDE_DIR AND EXISTS "${3RDPARTY_VTK_INCLUDE_DIR}")
202   list (APPEND 3RDPARTY_INCLUDE_DIRS ${3RDPARTY_VTK_INCLUDE_DIR})
203 else()
204   list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_VTK_INCLUDE_DIR)
205 endif()
206
207 if (BUILD_SHARED_LIBS)
208   if (3RDPARTY_VTK_LIBRARY_DIR AND EXISTS "${3RDPARTY_VTK_LIBRARY_DIR}")
209     list (APPEND 3RDPARTY_LIBRARY_DIRS ${3RDPARTY_VTK_LIBRARY_DIR})
210   else()
211     list (APPEND 3RDPARTY_NO_LIBS 3RDPARTY_VTK_LIBRARY_DIR)
212   endif()
213
214   if (WIN32)
215     if (3RDPARTY_VTK_DLL_DIR OR EXISTS "${3RDPARTY_VTK_DLL_DIR}")
216       list (APPEND 3RDPARTY_DLL_DIRS ${3RDPARTY_VTK_DLL_DIR})
217     else()
218       list (APPEND 3RDPARTY_NO_DLLS 3RDPARTY_VTK_DLL_DIR)
219     endif()
220   endif()
221 endif()
222
223 # Install vtk library using vtk targets
224 macro (OCCT_INSTALL_VTK VTK_LIBRARY_NAME)
225   # Check that input library name contains "vtk" prefix
226   string (REGEX MATCH "^vtk" IS_VTK_LIBRARY ${VTK_LIBRARY_NAME})
227   # Check that input library was not already installed
228   list (FIND OCCT_VTK_USED_TARGETS ${VTK_LIBRARY_NAME} VTK_LIBRARY_IS_USED)
229   if (BUILD_SHARED_LIBS AND INSTALL_VTK AND TARGET ${VTK_LIBRARY_NAME} AND VTK_LIBRARY_IS_USED EQUAL -1 AND IS_VTK_LIBRARY)
230     OCCT_MAKE_OS_WITH_BITNESS()
231
232     # Get configuration of vtk
233     get_target_property (TARGET_VTK_IMPORT_CONFS ${VTK_LIBRARY_NAME} IMPORTED_CONFIGURATIONS)
234     list (GET TARGET_VTK_IMPORT_CONFS 0 CHOSEN_IMPORT_CONF)
235
236     # Get dependencies for current input library
237     get_property(VTK_LIBRARY_PATH       TARGET ${VTK_LIBRARY_NAME} PROPERTY LOCATION)
238     get_property(VTK_DEPEND             TARGET ${VTK_LIBRARY_NAME} PROPERTY IMPORTED_LINK_DEPENDENT_LIBRARIES_${CHOSEN_IMPORT_CONF})
239     get_property(VTK_IMPORTED_INTERFACE TARGET ${VTK_LIBRARY_NAME} PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES_${CHOSEN_IMPORT_CONF})
240     get_property(VTK_INTERFACE          TARGET ${VTK_LIBRARY_NAME} PROPERTY INTERFACE_LINK_LIBRARIES)
241     list (APPEND VTK_DEPEND ${VTK_INTERFACE} ${VTK_IMPORTED_INTERFACE})
242
243     # Install
244     if (WIN32)
245       if (SINGLE_GENERATOR)
246         install (FILES "${VTK_LIBRARY_PATH}" DESTINATION "${INSTALL_DIR_BIN}")
247       else()
248         install (FILES "${VTK_LIBRARY_PATH}"
249                  CONFIGURATIONS Release
250                  DESTINATION "${INSTALL_DIR_BIN}")
251         install (FILES "${VTK_LIBRARY_PATH}"
252                  CONFIGURATIONS RelWithDebInfo
253                  DESTINATION "${INSTALL_DIR_BIN}i")
254         install (FILES "${VTK_LIBRARY_PATH}"
255                  CONFIGURATIONS Debug
256                  DESTINATION "${INSTALL_DIR_BIN}d")
257       endif()
258     else()
259       get_filename_component(3RDPARTY_VTK_LIBRARY_ABS ${VTK_LIBRARY_PATH} REALPATH)
260
261       if (SINGLE_GENERATOR)
262         install (FILES "${3RDPARTY_VTK_LIBRARY_ABS}"
263                  DESTINATION "${INSTALL_DIR_LIB}")
264       else()
265         install (FILES "${3RDPARTY_VTK_LIBRARY_ABS}"
266                  CONFIGURATIONS Release
267                  DESTINATION "${INSTALL_DIR_LIB}")
268         install (FILES "${3RDPARTY_VTK_LIBRARY_ABS}"
269                  CONFIGURATIONS RelWithDebInfo
270                  DESTINATION "${INSTALL_DIR_LIB}i")
271         install (FILES "${3RDPARTY_VTK_LIBRARY_ABS}"
272                  CONFIGURATIONS Debug
273                  DESTINATION "${INSTALL_DIR_LIB}d")
274       endif()
275     endif()
276
277     set (USED_3RDPARTY_VTK_DIR "")
278
279     # Mark current library as already installed
280     list (APPEND OCCT_VTK_USED_TARGETS ${VTK_LIBRARY_NAME})
281     set (OCCT_VTK_USED_TARGETS "${OCCT_VTK_USED_TARGETS}" CACHE INTERNAL "" FORCE)
282
283     # Recursively install all depended libraries
284     foreach(VTK_TARGET ${VTK_DEPEND})
285       OCCT_INSTALL_VTK(${VTK_TARGET})
286     endforeach()
287   endif()
288 endmacro()
289
290 if (NOT BUILD_SHARED_LIBS)
291   OCCT_CHECK_AND_UNSET(3RDPARTY_VTK_DLL_DIR)
292   OCCT_CHECK_AND_UNSET(3RDPARTY_VTK_LIBRARY_DIR)
293   OCCT_CHECK_AND_UNSET(INSTALL_VTK)
294 endif()
295
296 # the library directory for using by the executable
297 if (NOT INSTALL_VTK)
298   if (WIN32)
299     set (USED_3RDPARTY_VTK_DIR ${3RDPARTY_VTK_DLL_DIR})
300   else()
301     set (USED_3RDPARTY_VTK_DIR ${3RDPARTY_VTK_LIBRARY_DIR})
302   endif()
303 endif()
304
305 OCCT_CHECK_AND_UNSET (VTK_INCLUDE_DIRS)
306 OCCT_CHECK_AND_UNSET (VTK_LIBRARY_DIRS)
307 OCCT_CHECK_AND_UNSET (VTK_DIR)