0029589: Configuration - allow MFC samples to be built when OCCT is linked statically
[occt.git] / adm / cmake / tk.cmake
1 # tk
2
3 if (NOT DEFINED INSTALL_TK AND BUILD_SHARED_LIBS)
4   set (INSTALL_TK OFF CACHE BOOL "${INSTALL_TK_DESCR}")
5 endif()
6
7 # tk directory
8 if (NOT DEFINED 3RDPARTY_TK_DIR)
9   set (3RDPARTY_TK_DIR "" CACHE PATH "The directory containing tk")
10 endif ()
11
12 if (NOT 3RDPARTY_TK_DIR AND 3RDPARTY_TCLTK_DIR)
13   set (3RDPARTY_TK_DIR "${3RDPARTY_TCLTK_DIR}" CACHE PATH "The directory containing tk" FORCE)
14 endif()
15
16 # tk include directory
17 if (NOT DEFINED 3RDPARTY_TK_INCLUDE_DIR)
18   set (3RDPARTY_TK_INCLUDE_DIR "" CACHE FILEPATH "The directory containing headers of tk")
19 endif()
20
21 if (BUILD_SHARED_LIBS)
22   # tk library file (with absolute path)
23   if (NOT DEFINED 3RDPARTY_TK_LIBRARY OR NOT 3RDPARTY_TK_LIBRARY_DIR)
24     set (3RDPARTY_TK_LIBRARY "" CACHE FILEPATH "tk library" FORCE)
25   endif()
26
27   # tk library directory
28   if (NOT DEFINED 3RDPARTY_TK_LIBRARY_DIR)
29     set (3RDPARTY_TK_LIBRARY_DIR "" CACHE FILEPATH "The directory containing tk library")
30   endif()
31
32   # tk shared library (with absolute path)
33   if (WIN32)
34     if (NOT DEFINED 3RDPARTY_TK_DLL OR NOT 3RDPARTY_TK_DLL_DIR)
35       set (3RDPARTY_TK_DLL "" CACHE FILEPATH "tk shared library" FORCE)
36     endif()
37   endif()
38
39   # tk shared library directory
40   if (WIN32 AND NOT DEFINED 3RDPARTY_TK_DLL_DIR)
41     set (3RDPARTY_TK_DLL_DIR "" CACHE FILEPATH "The directory containing tk shared library")
42   endif()
43 endif()
44
45 # search for tk in user defined directory
46 if (NOT 3RDPARTY_TK_DIR AND 3RDPARTY_DIR)
47   FIND_PRODUCT_DIR("${3RDPARTY_DIR}" tk TK_DIR_NAME)
48   if (TK_DIR_NAME)
49     set (3RDPARTY_TK_DIR "${3RDPARTY_DIR}/${TK_DIR_NAME}" CACHE PATH "The directory containing tk" FORCE)
50   endif()
51 endif()
52
53 # define paths for default engine
54 if (3RDPARTY_TK_DIR AND EXISTS "${3RDPARTY_TK_DIR}")
55   set (TK_INCLUDE_PATH "${3RDPARTY_TK_DIR}/include")
56 endif()
57
58 # check tk include dir, library dir and shared library dir
59 COMPLIANCE_PRODUCT_CONSISTENCY(TK)
60
61 # use default (CMake) TCL search
62 find_package(TCL QUIET)
63
64 # tk include dir
65 if (NOT 3RDPARTY_TK_INCLUDE_DIR)
66   if (TK_INCLUDE_PATH AND EXISTS "${TK_INCLUDE_PATH}")
67     set (3RDPARTY_TK_INCLUDE_DIR "${TK_INCLUDE_PATH}" CACHE FILEPATH "The directory containing headers of TK" FORCE)
68   endif()
69 endif()
70
71 if (BUILD_SHARED_LIBS)
72   # tk dir and library
73   if (NOT 3RDPARTY_TK_LIBRARY)
74     if (TK_LIBRARY AND EXISTS "${TK_LIBRARY}")
75       set (3RDPARTY_TK_LIBRARY "${TK_LIBRARY}" CACHE FILEPATH "TK library" FORCE)
76
77       if (NOT 3RDPARTY_TK_LIBRARY_DIR)
78         get_filename_component (3RDPARTY_TK_LIBRARY_DIR "${3RDPARTY_TK_LIBRARY}" PATH)
79         set (3RDPARTY_TK_LIBRARY_DIR "${3RDPARTY_TK_LIBRARY_DIR}" CACHE FILEPATH "The directory containing TK library" FORCE)
80       endif()
81     endif()
82   endif()
83
84
85   if (WIN32)
86     if (NOT 3RDPARTY_TK_DLL)
87         set (CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".dll" ".a")
88
89         set (DLL_FOLDER_FOR_SEARCH "")
90         if (3RDPARTY_TK_DLL_DIR)
91           set (DLL_FOLDER_FOR_SEARCH "${3RDPARTY_TK_DLL_DIR}")
92         elseif (3RDPARTY_TK_DIR)
93           set (DLL_FOLDER_FOR_SEARCH "${3RDPARTY_TK_DIR}/bin")
94         elseif (3RDPARTY_TK_LIBRARY_DIR)
95           get_filename_component (3RDPARTY_TK_LIBRARY_DIR_PARENT "${3RDPARTY_TK_LIBRARY_DIR}" PATH)
96           set (DLL_FOLDER_FOR_SEARCH "${3RDPARTY_TK_LIBRARY_DIR_PARENT}/bin")
97         endif()
98
99         set (3RDPARTY_TK_DLL "3RDPARTY_TK_DLL-NOTFOUND" CACHE FILEPATH "TK shared library" FORCE)
100         find_library (3RDPARTY_TK_DLL NAMES ${CSF_TclTkLibs}
101                                             PATHS "${DLL_FOLDER_FOR_SEARCH}"
102                                             NO_DEFAULT_PATH)
103     endif()
104   endif()
105 endif()
106
107 COMPLIANCE_PRODUCT_CONSISTENCY(TK)
108
109 if (BUILD_SHARED_LIBS)
110   # tk dir and library
111   if (NOT 3RDPARTY_TK_LIBRARY)
112     set (3RDPARTY_TK_LIBRARY "3RDPARTY_TK_LIBRARY-NOTFOUND" CACHE FILEPATH "TK library" FORCE)
113     find_library (3RDPARTY_TK_LIBRARY NAMES ${CSF_TclTkLibs}
114                                             PATHS "${3RDPARTY_TK_LIBRARY_DIR}"
115                                             NO_DEFAULT_PATH)
116
117     # search in another place if previous search doesn't find anything
118     find_library (3RDPARTY_TK_LIBRARY NAMES ${CSF_TclTkLibs}
119                                             PATHS "${3RDPARTY_TK_DIR}/lib"
120                                             NO_DEFAULT_PATH)
121
122
123     if (NOT 3RDPARTY_TK_LIBRARY OR NOT EXISTS "${3RDPARTY_TK_LIBRARY}")
124       set (3RDPARTY_TK_LIBRARY "" CACHE FILEPATH "TK library" FORCE)
125     endif()
126
127     if (NOT 3RDPARTY_TK_LIBRARY_DIR AND 3RDPARTY_TK_LIBRARY)
128       get_filename_component (3RDPARTY_TK_LIBRARY_DIR "${3RDPARTY_TK_LIBRARY}" PATH)
129       set (3RDPARTY_TK_LIBRARY_DIR "${3RDPARTY_TK_LIBRARY_DIR}" CACHE FILEPATH "The directory containing TK library" FORCE)
130     endif()
131   endif()
132
133   set (3RDPARTY_TK_LIBRARY_VERSION "")
134   if (3RDPARTY_TK_LIBRARY AND EXISTS "${3RDPARTY_TK_LIBRARY}")
135     get_filename_component (TK_LIBRARY_NAME "${3RDPARTY_TK_LIBRARY}" NAME)
136     string(REGEX REPLACE "^.*tk([0-9]\\.*[0-9]).*$" "\\1" TK_LIBRARY_VERSION "${TK_LIBRARY_NAME}")
137
138     if (NOT "${TK_LIBRARY_VERSION}" STREQUAL "${TK_LIBRARY_NAME}")
139       set (3RDPARTY_TK_LIBRARY_VERSION "${TK_LIBRARY_VERSION}")
140     else() # if the version isn't found - seek other library with 8.6 or 8.5 version in the same dir
141       message (STATUS "Info: TK version isn't found")
142     endif()
143   endif()
144
145   set (3RDPARTY_TK_LIBRARY_VERSION_WITH_DOT "")
146   if (3RDPARTY_TK_LIBRARY_VERSION)
147     string (REGEX REPLACE "^.*([0-9])[^0-9]*[0-9].*$" "\\1" 3RDPARTY_TK_MAJOR_VERSION "${3RDPARTY_TK_LIBRARY_VERSION}")
148     string (REGEX REPLACE "^.*[0-9][^0-9]*([0-9]).*$" "\\1" 3RDPARTY_TK_MINOR_VERSION "${3RDPARTY_TK_LIBRARY_VERSION}")
149     set (3RDPARTY_TK_LIBRARY_VERSION_WITH_DOT "${3RDPARTY_TK_MAJOR_VERSION}.${3RDPARTY_TK_MINOR_VERSION}")
150   endif()
151
152   if (WIN32)
153     if (NOT 3RDPARTY_TK_DLL)
154       set (CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".dll" ".a")
155
156       set (DLL_FOLDER_FOR_SEARCH "")
157       if (3RDPARTY_TK_DLL_DIR)
158         set (DLL_FOLDER_FOR_SEARCH "${3RDPARTY_TK_DLL_DIR}")
159       elseif (3RDPARTY_TK_DIR)
160         set (DLL_FOLDER_FOR_SEARCH "${3RDPARTY_TK_DIR}/bin")
161       else()
162         get_filename_component (3RDPARTY_TK_LIBRARY_DIR_PARENT "${3RDPARTY_TK_LIBRARY_DIR}" PATH)
163         set (DLL_FOLDER_FOR_SEARCH "${3RDPARTY_TK_LIBRARY_DIR_PARENT}/bin")
164       endif()
165
166       set (3RDPARTY_TK_DLL "3RDPARTY_TK_DLL-NOTFOUND" CACHE FILEPATH "TK shared library" FORCE)
167       find_library (3RDPARTY_TK_DLL NAMES tk${3RDPARTY_TK_LIBRARY_VERSION}
168                                           PATHS "${DLL_FOLDER_FOR_SEARCH}"
169                                           NO_DEFAULT_PATH)
170
171       if (NOT 3RDPARTY_TK_DLL OR NOT EXISTS "${3RDPARTY_TK_DLL}")
172         set (3RDPARTY_TK_DLL "" CACHE FILEPATH "TK shared library" FORCE)
173       endif()
174     endif()
175     if (NOT 3RDPARTY_TK_DLL_DIR AND 3RDPARTY_TK_DLL)
176       get_filename_component (3RDPARTY_TK_DLL_DIR "${3RDPARTY_TK_DLL}" PATH)
177       set (3RDPARTY_TK_DLL_DIR "${3RDPARTY_TK_DLL_DIR}" CACHE FILEPATH "The directory containing TK shared library" FORCE)
178     endif()
179   endif()
180 endif()
181
182 # include found paths to common variables
183 if (3RDPARTY_TK_INCLUDE_DIR AND EXISTS "${3RDPARTY_TK_INCLUDE_DIR}")
184   list (APPEND 3RDPARTY_INCLUDE_DIRS "${3RDPARTY_TK_INCLUDE_DIR}")
185 else()
186   list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_TK_INCLUDE_DIR)
187 endif()
188
189 if (BUILD_SHARED_LIBS)
190   if (3RDPARTY_TK_LIBRARY AND EXISTS "${3RDPARTY_TK_LIBRARY}")
191     list (APPEND 3RDPARTY_LIBRARY_DIRS "${3RDPARTY_TK_LIBRARY_DIR}")
192   else()
193     list (APPEND 3RDPARTY_NO_LIBS 3RDPARTY_TK_LIBRARY_DIR)
194   endif()
195
196   if (WIN32)
197     if (3RDPARTY_TK_DLL OR EXISTS "${3RDPARTY_TK_DLL}")
198       list (APPEND 3RDPARTY_DLL_DIRS "${3RDPARTY_TK_DLL_DIR}")
199     else()
200       list (APPEND 3RDPARTY_NO_DLLS 3RDPARTY_TK_DLL_DIR)
201     endif()
202   endif()
203
204   # install tk
205   if (INSTALL_TK)
206     # include occt macros. compiler_bitness, os_wiht_bit, compiler
207     OCCT_INCLUDE_CMAKE_FILE ("adm/cmake/occt_macros")
208
209     OCCT_MAKE_OS_WITH_BITNESS()
210     OCCT_MAKE_COMPILER_SHORT_NAME()
211
212     if (WIN32)
213       # tk 8.6 requires zlib. install all dlls from tk bin folder that may contain zlib also
214
215       # collect and install all dlls from tk dll dirs
216       file (GLOB TK_DLLS  "${3RDPARTY_TK_DLL_DIR}/*.dll")
217
218       if (SINGLE_GENERATOR)
219         install (FILES ${TK_DLLS} DESTINATION "${INSTALL_DIR_BIN}")
220       else()
221         install (FILES ${TK_DLLS}
222                  CONFIGURATIONS Release
223                  DESTINATION "${INSTALL_DIR_BIN}")
224         install (FILES ${TK_DLLS}
225                  CONFIGURATIONS RelWithDebInfo
226                  DESTINATION "${INSTALL_DIR_BIN}i")
227         install (FILES ${TK_DLLS}
228                  CONFIGURATIONS Debug
229                  DESTINATION "${INSTALL_DIR_BIN}d")
230       endif()
231     else()
232       get_filename_component(3RDPARTY_TK_LIBRARY_REALPATH ${3RDPARTY_TK_LIBRARY} REALPATH)
233
234       if (SINGLE_GENERATOR)
235         install (FILES ${3RDPARTY_TK_LIBRARY_REALPATH} DESTINATION "${INSTALL_DIR_LIB}")
236       else()
237         install (FILES ${3RDPARTY_TK_LIBRARY_REALPATH}
238                  CONFIGURATIONS Release
239                  DESTINATION "${INSTALL_DIR_LIB}/")
240         install (FILES ${3RDPARTY_TK_LIBRARY_REALPATH}
241                  CONFIGURATIONS RelWithDebInfo
242                  DESTINATION "${INSTALL_DIR_LIB}/i")
243         install (FILES ${3RDPARTY_TK_LIBRARY_REALPATH}
244                  CONFIGURATIONS Debug
245                  DESTINATION "${INSTALL_DIR_LIB}d")
246       endif()
247     endif()
248
249     if (TCL_TCLSH_VERSION)
250       # tk is required to install in lib folder (without)
251       install (DIRECTORY "${3RDPARTY_TK_LIBRARY_DIR}/tk${TCL_TCLSH_VERSION}"  DESTINATION "${INSTALL_DIR_LIB}")
252     else()
253       message (STATUS "\nWarning: tkX.X subdir won't be copyied during the installation process.")
254       message (STATUS "Try seeking tk within another folder by changing 3RDPARTY_TK_DIR variable.")
255     endif()
256
257     set (USED_3RDPARTY_TK_DIR "")
258   else()
259     # the library directory for using by the executable
260     if (WIN32)
261       set (USED_3RDPARTY_TK_DIR ${3RDPARTY_TK_DLL_DIR})
262     else()
263       set (USED_3RDPARTY_TK_DIR ${3RDPARTY_TK_LIBRARY_DIR})
264     endif()
265   endif()
266
267   mark_as_advanced (3RDPARTY_TK_LIBRARY 3RDPARTY_TK_DLL)
268 endif()
269
270 # unset all redundant variables
271 #TCL
272 OCCT_CHECK_AND_UNSET (TCL_LIBRARY)
273 OCCT_CHECK_AND_UNSET (TCL_INCLUDE_PATH)
274 OCCT_CHECK_AND_UNSET (TCL_TCLSH)
275 #TK
276 OCCT_CHECK_AND_UNSET (TK_LIBRARY)
277 OCCT_CHECK_AND_UNSET (TK_INCLUDE_PATH)
278 OCCT_CHECK_AND_UNSET (TK_WISH)
279
280 if (NOT BUILD_SHARED_LIBS)
281   OCCT_CHECK_AND_UNSET (3RDPARTY_TK_LIBRARY)
282   OCCT_CHECK_AND_UNSET (3RDPARTY_TK_LIBRARY_DIR)
283   OCCT_CHECK_AND_UNSET (3RDPARTY_TK_DLL)
284   OCCT_CHECK_AND_UNSET (3RDPARTY_TK_DLL_DIR)
285   OCCT_CHECK_AND_UNSET (INSTALL_TK)
286 endif()