cd4103e0bdfb3b63cdd15c07c7e6192c77acdaec
[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 # the library directory for using by the executable
214 if (WIN32)
215   set (USED_3RDPARTY_VTK_DIR ${3RDPARTY_VTK_DLL_DIR})
216 else()
217   set (USED_3RDPARTY_VTK_DIR ${3RDPARTY_VTK_LIBRARY_DIR})
218 endif()
219
220 OCCT_CHECK_AND_UNSET (VTK_INCLUDE_DIRS)
221 OCCT_CHECK_AND_UNSET (VTK_LIBRARY_DIRS)
222 OCCT_CHECK_AND_UNSET (VTK_DIR)