2efb6627dc72c1569a262a352a2c3c651735401e
[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 # vtk library directory
35 if (NOT DEFINED 3RDPARTY_VTK_LIBRARY_DIR)
36   set (3RDPARTY_VTK_LIBRARY_DIR "" CACHE PATH "The directory containing VTK libraries")
37 endif()
38
39 # vtk dll directory
40 if (WIN32 AND NOT DEFINED 3RDPARTY_VTK_DLL_DIR)
41   set (3RDPARTY_VTK_DLL_DIR "" CACHE PATH "The directory containing VTK shared libraries")
42 endif()
43
44 # check 3RDPARTY_VTK_ paths for consistency with specified 3RDPARTY_VTK_DIR
45 if (3RDPARTY_VTK_DIR AND EXISTS "${3RDPARTY_VTK_DIR}")
46   CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_VTK_DIR 3RDPARTY_VTK_INCLUDE_DIR PATH "The directory containing headers of VTK")
47   CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_VTK_DIR 3RDPARTY_VTK_LIBRARY_DIR PATH "The directory containing VTK libraries")
48
49   if (WIN32)
50     CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_VTK_DIR 3RDPARTY_VTK_DLL_DIR PATH "The directory containing VTK shared library")
51   endif()
52 endif()
53
54 if (3RDPARTY_VTK_DIR AND EXISTS "${3RDPARTY_VTK_DIR}")
55   set (CACHED_VTK_DIR $ENV{VTK_DIR})
56   set (ENV{VTK_DIR} "${3RDPARTY_VTK_DIR}")
57 endif()
58
59 find_package(VTK QUIET)
60
61 if (3RDPARTY_VTK_DIR AND EXISTS "${3RDPARTY_VTK_DIR}")
62   set (ENV{VTK_DIR} ${CACHED_VTK_DIR})
63 endif()
64
65 if (VTK_FOUND)
66
67   # add compiler flags, preprocessor definitions, include and link dirs
68   include (${VTK_USE_FILE})
69
70   if (VTK_LIBRARIES)
71
72     set (3RDPARTY_VTK_INCLUDE_DIRS)
73     set (3RDPARTY_VTK_LIBRARY_DIRS)
74     set (3RDPARTY_VTK_DLL_DIRS)
75
76     set (HARDCODED_D3D9_LIB "C:/Program Files (x86)/Microsoft DirectX SDK (June 2010)/Lib/x64/d3d9.lib")
77
78     foreach (VTK_LIBRARY ${VTK_LIBRARIES})
79       string (REGEX MATCH "^vtk" IS_VTK_LIBRARY ${VTK_LIBRARY})
80       if (IS_VTK_LIBRARY)
81         # get paths from corresponding variables
82         if (${VTK_LIBRARY}_INCLUDE_DIRS AND EXISTS "${${VTK_LIBRARY}_INCLUDE_DIRS}")
83           list (APPEND 3RDPARTY_VTK_INCLUDE_DIRS "${${VTK_LIBRARY}_INCLUDE_DIRS}")
84         endif()
85
86         if (${VTK_LIBRARY}_LIBRARY_DIRS AND EXISTS "${${VTK_LIBRARY}_LIBRARY_DIRS}")
87           list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${${VTK_LIBRARY}_LIBRARY_DIRS}")
88         endif()
89
90         if (${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS AND EXISTS "${${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS}")
91           list (APPEND 3RDPARTY_VTK_DLL_DIRS "${${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS}")
92           if (NOT WIN32)
93             list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${${VTK_LIBRARY}_RUNTIME_LIBRARY_DIRS}")
94           endif()
95         endif()
96
97         # get paths from corresponding properties
98         get_target_property (TARGET_VTK_IMPORT_CONFS ${VTK_LIBRARY} IMPORTED_CONFIGURATIONS)
99
100         if (TARGET_VTK_IMPORT_CONFS)
101           list (GET TARGET_VTK_IMPORT_CONFS 0 CHOSEN_IMPORT_CONF)
102           
103           # todo: choose configuration in connection with the build type
104           #if (CMAKE_BUILD_TYPE)
105           #  foreach (IMPORT_CONF ${TARGET_VTK_IMPORT_CONFS})
106           #  endforeach()
107           #endif()
108
109           get_target_property (TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES ${VTK_LIBRARY} IMPORTED_LINK_INTERFACE_LIBRARIES_${CHOSEN_IMPORT_CONF})
110           if(TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES)
111             list (FIND TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES "${HARDCODED_D3D9_LIB}" D3D9_INDEX)
112             if (NOT D3D9_INDEX EQUAL -1)
113               message (STATUS "Warning: ${HARDCODED_D3D9_LIB} has been removed from ${VTK_LIBRARY}")
114               
115               list (REMOVE_AT TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES ${D3D9_INDEX})
116               set_target_properties (${VTK_LIBRARY} PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES_${CHOSEN_IMPORT_CONF} "${TARGET_PROPERTY_IMP_LINK_INTERFACE_LIBRARIES}")
117             endif()            
118           endif()
119
120           get_target_property (TARGET_PROPERTY_IMP_PATH ${VTK_LIBRARY} IMPORTED_IMPLIB_${CHOSEN_IMPORT_CONF})
121           if(TARGET_PROPERTY_IMP_PATH AND EXISTS "${TARGET_PROPERTY_IMP_PATH}")
122             get_filename_component (TARGET_PROPERTY_IMP_DIR "${TARGET_PROPERTY_IMP_PATH}" PATH)
123             list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${TARGET_PROPERTY_IMP_DIR}")
124           endif()
125
126           get_target_property (TARGET_PROPERTY_LOCATION_PATH ${VTK_LIBRARY} IMPORTED_LOCATION_${CHOSEN_IMPORT_CONF})
127           if(TARGET_PROPERTY_LOCATION_PATH AND EXISTS "${TARGET_PROPERTY_LOCATION_PATH}")
128             get_filename_component (TARGET_PROPERTY_LOCATION_DIR "${TARGET_PROPERTY_LOCATION_PATH}" PATH)
129
130             if (WIN32)
131               list (APPEND 3RDPARTY_VTK_DLL_DIRS "${TARGET_PROPERTY_LOCATION_DIR}")
132             else()
133               list (APPEND 3RDPARTY_VTK_LIBRARY_DIRS "${TARGET_PROPERTY_LOCATION_DIR}")
134             endif()
135           endif()
136         endif()
137       endif()
138     endforeach()
139   endif()
140
141   if (3RDPARTY_VTK_INCLUDE_DIRS)
142     list (REMOVE_DUPLICATES 3RDPARTY_VTK_INCLUDE_DIRS)
143     list (APPEND 3RDPARTY_INCLUDE_DIRS ${3RDPARTY_VTK_INCLUDE_DIRS})
144
145     list (GET 3RDPARTY_VTK_INCLUDE_DIRS 0 3RDPARTY_VTK_INCLUDE_DIR)
146     set (3RDPARTY_VTK_INCLUDE_DIR "${3RDPARTY_VTK_INCLUDE_DIR}" CACHE PATH "The directory containing headers of VTK" FORCE)
147   endif()
148
149   if (3RDPARTY_VTK_LIBRARY_DIRS)
150     list (REMOVE_DUPLICATES 3RDPARTY_VTK_LIBRARY_DIRS)
151     list (APPEND 3RDPARTY_LIBRARY_DIRS ${3RDPARTY_VTK_LIBRARY_DIRS})
152
153     list (GET 3RDPARTY_VTK_LIBRARY_DIRS 0 3RDPARTY_VTK_LIBRARY_DIR)
154     set (3RDPARTY_VTK_LIBRARY_DIR "${3RDPARTY_VTK_LIBRARY_DIR}" CACHE PATH "The directory containing VTK libraries" FORCE)
155   endif()
156
157   if (WIN32)
158     if (3RDPARTY_VTK_DLL_DIRS)
159       list (REMOVE_DUPLICATES 3RDPARTY_VTK_DLL_DIRS)
160       list (APPEND 3RDPARTY_DLL_DIRS ${3RDPARTY_VTK_DLL_DIRS})
161
162       list (GET 3RDPARTY_VTK_DLL_DIRS 0 3RDPARTY_VTK_DLL_DIR)
163       set (3RDPARTY_VTK_DLL_DIR "${3RDPARTY_VTK_DLL_DIR}" CACHE PATH "The directory containing VTK shared libraries" FORCE)
164     endif()
165   endif()
166 endif()
167
168 if (3RDPARTY_VTK_INCLUDE_DIR AND EXISTS "${3RDPARTY_VTK_INCLUDE_DIR}")
169   list (APPEND 3RDPARTY_INCLUDE_DIRS ${3RDPARTY_VTK_INCLUDE_DIR})
170 else()
171   list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_VTK_INCLUDE_DIR)
172 endif()
173
174 if (3RDPARTY_VTK_LIBRARY_DIR AND EXISTS "${3RDPARTY_VTK_LIBRARY_DIR}")
175   list (APPEND 3RDPARTY_LIBRARY_DIRS ${3RDPARTY_VTK_LIBRARY_DIR})
176 else()
177   list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_VTK_LIBRARY_DIR)
178 endif()
179
180 if (WIN32)
181   if (3RDPARTY_VTK_DLL_DIR OR EXISTS "${3RDPARTY_VTK_DLL_DIR}")
182     list (APPEND 3RDPARTY_DLL_DIRS ${3RDPARTY_VTK_DLL_DIR})
183   else()
184     list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_VTK_DLL_DIR)
185   endif()
186 endif()
187
188 # the library directory for using by the executable
189 if (WIN32)
190   set (USED_3RDPARTY_VTK_DIR ${3RDPARTY_VTK_DLL_DIR})
191 else()
192   set (USED_3RDPARTY_VTK_DIR ${3RDPARTY_VTK_LIBRARY_DIR})
193 endif()
194
195 OCCT_CHECK_AND_UNSET (VTK_INCLUDE_DIRS)
196 OCCT_CHECK_AND_UNSET (VTK_LIBRARY_DIRS)
197 OCCT_CHECK_AND_UNSET (VTK_DIR)