0029056: Configuration - It is not possible to install VTK products
[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 if (VTK_FOUND)
70
71   # add compiler flags, preprocessor definitions, include and link dirs
72   include (${VTK_USE_FILE})
73
74   if (VTK_LIBRARIES)
75
76     set (3RDPARTY_VTK_INCLUDE_DIRS)
77
78     if (BUILD_SHARED_LIBS)
79       set (3RDPARTY_VTK_LIBRARY_DIRS)
80       set (3RDPARTY_VTK_DLL_DIRS)
81     endif()
82
83     foreach (VTK_LIBRARY ${VTK_LIBRARIES})
84       string (REGEX MATCH "^vtk" IS_VTK_LIBRARY ${VTK_LIBRARY})
85       if (IS_VTK_LIBRARY AND TARGET ${VTK_LIBRARY})
86         # get paths from corresponding variables
87         if (${VTK_LIBRARY}_INCLUDE_DIRS AND EXISTS "${${VTK_LIBRARY}_INCLUDE_DIRS}")
88           list (APPEND 3RDPARTY_VTK_INCLUDE_DIRS "${${VTK_LIBRARY}_INCLUDE_DIRS}")
89         endif()
90
91           if (${VTK_LIBRARY}_LIBRARY_DIRS AND EXISTS "${${VTK_LIBRARY}_LIBRARY_DIRS}")
92             list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${${VTK_LIBRARY}_LIBRARY_DIRS}")
93           endif()
94
95           if (${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS AND EXISTS "${${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS}")
96             list (APPEND 3RDPARTY_VTK_DLL_DIRS "${${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS}")
97             if (NOT WIN32)
98               list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS}")
99             endif()
100           endif()
101
102           # get paths from corresponding properties
103           get_target_property (TARGET_VTK_IMPORT_CONFS ${VTK_LIBRARY} IMPORTED_CONFIGURATIONS)
104
105           if (TARGET_VTK_IMPORT_CONFS)
106             list (GET TARGET_VTK_IMPORT_CONFS 0 CHOSEN_IMPORT_CONF)
107
108             # todo: choose configuration in connection with the build type
109             #if (CMAKE_BUILD_TYPE)
110             #  foreach (IMPORT_CONF ${TARGET_VTK_IMPORT_CONFS})
111             #  endforeach()
112             #endif()
113
114             # Work-around against link failure in case if VTK contains dependency
115             # on DirectX: its run-time is always present on Windows, but SDK can
116             # be absent on current workstation, while not actually needed for 
117             # OCCT linking.
118             # VTK 6.1 for VC 10
119             get_target_property (TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES ${VTK_LIBRARY} IMPORTED_LINK_INTERFACE_LIBRARIES_${CHOSEN_IMPORT_CONF})
120             if(TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES)
121               string (REGEX MATCH "[^;]*d3d[0-9]+[.]lib" HARDCODED_D3D9_LIB "${TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES}")
122               if (HARDCODED_D3D9_LIB)
123                 message (STATUS "Warning: ${HARDCODED_D3D9_LIB} has been removed from imported dependencies of ${VTK_LIBRARY}")
124
125                 list (REMOVE_ITEM TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES ${HARDCODED_D3D9_LIB})
126                 set_target_properties (${VTK_LIBRARY} PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES_${CHOSEN_IMPORT_CONF} "${TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES}")
127               endif()
128             endif()
129             # VTK 6.1 for VC 12, 14
130             get_target_property (TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES ${VTK_LIBRARY} INTERFACE_LINK_LIBRARIES)
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 INTERFACE_LINK_LIBRARIES "${TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES}")
138               endif()
139             endif()
140
141             get_target_property (TARGET_PROPERTY_IMP_PATH ${VTK_LIBRARY} IMPORTED_IMPLIB_${CHOSEN_IMPORT_CONF})
142             if(TARGET_PROPERTY_IMP_PATH AND EXISTS "${TARGET_PROPERTY_IMP_PATH}")
143               get_filename_component (TARGET_PROPERTY_IMP_DIR "${TARGET_PROPERTY_IMP_PATH}" PATH)
144               list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${TARGET_PROPERTY_IMP_DIR}")
145             endif()
146
147             get_target_property (TARGET_PROPERTY_LOCATION_PATH ${VTK_LIBRARY} IMPORTED_LOCATION_${CHOSEN_IMPORT_CONF})
148             if(TARGET_PROPERTY_LOCATION_PATH AND EXISTS "${TARGET_PROPERTY_LOCATION_PATH}")
149               get_filename_component (TARGET_PROPERTY_LOCATION_DIR "${TARGET_PROPERTY_LOCATION_PATH}" PATH)
150
151               if (WIN32)
152                 list (APPEND 3RDPARTY_VTK_DLL_DIRS "${TARGET_PROPERTY_LOCATION_DIR}")
153               else()
154                 list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${TARGET_PROPERTY_LOCATION_DIR}")
155               endif()
156             endif()
157           endif()
158         endif()
159       endif()
160     endforeach()
161   endif()
162
163   if (3RDPARTY_VTK_INCLUDE_DIRS)
164     list (REMOVE_DUPLICATES 3RDPARTY_VTK_INCLUDE_DIRS)
165     list (APPEND 3RDPARTY_INCLUDE_DIRS ${3RDPARTY_VTK_INCLUDE_DIRS})
166
167     list (GET 3RDPARTY_VTK_INCLUDE_DIRS 0 3RDPARTY_VTK_INCLUDE_DIR)
168     set (3RDPARTY_VTK_INCLUDE_DIR "${3RDPARTY_VTK_INCLUDE_DIR}" CACHE PATH "The directory containing headers of VTK" FORCE)
169   endif()
170
171   if (BUILD_SHARED_LIBS)
172     if (3RDPARTY_VTK_LIBRARY_DIRS)
173       list (REMOVE_DUPLICATES 3RDPARTY_VTK_LIBRARY_DIRS)
174       list (APPEND 3RDPARTY_LIBRARY_DIRS ${3RDPARTY_VTK_LIBRARY_DIRS})
175
176       list (GET 3RDPARTY_VTK_LIBRARY_DIRS 0 3RDPARTY_VTK_LIBRARY_DIR)
177       set (3RDPARTY_VTK_LIBRARY_DIR "${3RDPARTY_VTK_LIBRARY_DIR}" CACHE PATH "The directory containing VTK libraries" FORCE)
178     endif()
179
180     if (WIN32)
181       if (3RDPARTY_VTK_DLL_DIRS)
182         list (REMOVE_DUPLICATES 3RDPARTY_VTK_DLL_DIRS)
183         list (APPEND 3RDPARTY_DLL_DIRS ${3RDPARTY_VTK_DLL_DIRS})
184
185         list (GET 3RDPARTY_VTK_DLL_DIRS 0 3RDPARTY_VTK_DLL_DIR)
186         set (3RDPARTY_VTK_DLL_DIR "${3RDPARTY_VTK_DLL_DIR}" CACHE PATH "The directory containing VTK shared libraries" FORCE)
187       endif()
188     endif()
189   endif()
190
191 if (3RDPARTY_VTK_INCLUDE_DIR AND EXISTS "${3RDPARTY_VTK_INCLUDE_DIR}")
192   list (APPEND 3RDPARTY_INCLUDE_DIRS ${3RDPARTY_VTK_INCLUDE_DIR})
193 else()
194   list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_VTK_INCLUDE_DIR)
195 endif()
196
197 if (BUILD_SHARED_LIBS)
198   if (3RDPARTY_VTK_LIBRARY_DIR AND EXISTS "${3RDPARTY_VTK_LIBRARY_DIR}")
199     list (APPEND 3RDPARTY_LIBRARY_DIRS ${3RDPARTY_VTK_LIBRARY_DIR})
200   else()
201     list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_VTK_LIBRARY_DIR)
202   endif()
203
204   if (WIN32)
205     if (3RDPARTY_VTK_DLL_DIR OR EXISTS "${3RDPARTY_VTK_DLL_DIR}")
206       list (APPEND 3RDPARTY_DLL_DIRS ${3RDPARTY_VTK_DLL_DIR})
207     else()
208       list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_VTK_DLL_DIR)
209     endif()
210   endif()
211 endif()
212
213 # Install vtk library using vtk targets
214 macro (OCCT_INSTALL_VTK VTK_LIBRARY_NAME)
215   # Check that input library name contains "vtk" prefix
216   string (REGEX MATCH "^vtk" IS_VTK_LIBRARY ${VTK_LIBRARY_NAME})
217   # Check that input library was not already installed
218   list (FIND OCCT_VTK_USED_TARGETS ${VTK_LIBRARY_NAME} VTK_LIBRARY_IS_USED)
219   if (BUILD_SHARED_LIBS AND INSTALL_VTK AND TARGET ${VTK_LIBRARY_NAME} AND VTK_LIBRARY_IS_USED EQUAL -1 AND IS_VTK_LIBRARY)
220     OCCT_MAKE_OS_WITH_BITNESS()
221
222     # Get configuration of vtk
223     get_target_property (TARGET_VTK_IMPORT_CONFS ${VTK_LIBRARY_NAME} IMPORTED_CONFIGURATIONS)
224     list (GET TARGET_VTK_IMPORT_CONFS 0 CHOSEN_IMPORT_CONF)
225
226     # Get dependencies for current input library
227     get_property(VTK_LIBRARY_PATH       TARGET ${VTK_LIBRARY_NAME} PROPERTY LOCATION)
228     get_property(VTK_DEPEND             TARGET ${VTK_LIBRARY_NAME} PROPERTY IMPORTED_LINK_DEPENDENT_LIBRARIES_${CHOSEN_IMPORT_CONF})
229     get_property(VTK_IMPORTED_INTERFACE TARGET ${VTK_LIBRARY_NAME} PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES_${CHOSEN_IMPORT_CONF})
230     get_property(VTK_INTERFACE          TARGET ${VTK_LIBRARY_NAME} PROPERTY INTERFACE_LINK_LIBRARIES)
231     list (APPEND VTK_DEPEND ${VTK_INTERFACE} ${VTK_IMPORTED_INTERFACE})
232
233     # Install
234     if (WIN32)
235       if (SINGLE_GENERATOR)
236         install (FILES "${VTK_LIBRARY_PATH}" DESTINATION "${INSTALL_DIR_BIN}")
237       else()
238         install (FILES "${VTK_LIBRARY_PATH}"
239                  CONFIGURATIONS Release
240                  DESTINATION "${INSTALL_DIR_BIN}")
241         install (FILES "${VTK_LIBRARY_PATH}"
242                  CONFIGURATIONS RelWithDebInfo
243                  DESTINATION "${INSTALL_DIR_BIN}i")
244         install (FILES "${VTK_LIBRARY_PATH}"
245                  CONFIGURATIONS Debug
246                  DESTINATION "${INSTALL_DIR_BIN}d")
247       endif()
248     else()
249       get_filename_component(3RDPARTY_VTK_LIBRARY_ABS ${VTK_LIBRARY_PATH} REALPATH)
250
251       if (SINGLE_GENERATOR)
252         install (FILES "${3RDPARTY_VTK_LIBRARY_ABS}"
253                  DESTINATION "${INSTALL_DIR_LIB}")
254       else()
255         install (FILES "${3RDPARTY_VTK_LIBRARY_ABS}"
256                  CONFIGURATIONS Release
257                  DESTINATION "${INSTALL_DIR_LIB}")
258         install (FILES "${3RDPARTY_VTK_LIBRARY_ABS}"
259                  CONFIGURATIONS RelWithDebInfo
260                  DESTINATION "${INSTALL_DIR_LIB}i")
261         install (FILES "${3RDPARTY_VTK_LIBRARY_ABS}"
262                  CONFIGURATIONS Debug
263                  DESTINATION "${INSTALL_DIR_LIB}d")
264       endif()
265     endif()
266
267     set (USED_3RDPARTY_VTK_DIR "")
268
269     # Mark current library as already installed
270     list (APPEND OCCT_VTK_USED_TARGETS ${VTK_LIBRARY_NAME})
271     set (OCCT_VTK_USED_TARGETS "${OCCT_VTK_USED_TARGETS}" CACHE INTERNAL "" FORCE)
272
273     # Recursively install all depended libraries
274     foreach(VTK_TARGET ${VTK_DEPEND})
275       OCCT_INSTALL_VTK(${VTK_TARGET})
276     endforeach()
277   endif()
278 endmacro()
279
280 if (NOT BUILD_SHARED_LIBS)
281   OCCT_CHECK_AND_UNSET(3RDPARTY_VTK_DLL_DIR)
282   OCCT_CHECK_AND_UNSET(3RDPARTY_VTK_LIBRARY_DIR)
283   OCCT_CHECK_AND_UNSET(INSTALL_VTK)
284 endif()
285
286 # the library directory for using by the executable
287 if (NOT INSTALL_VTK)
288   if (WIN32)
289     set (USED_3RDPARTY_VTK_DIR ${3RDPARTY_VTK_DLL_DIR})
290   else()
291     set (USED_3RDPARTY_VTK_DIR ${3RDPARTY_VTK_LIBRARY_DIR})
292   endif()
293 endif()
294
295 OCCT_CHECK_AND_UNSET (VTK_INCLUDE_DIRS)
296 OCCT_CHECK_AND_UNSET (VTK_LIBRARY_DIRS)
297 OCCT_CHECK_AND_UNSET (VTK_DIR)