0032315: Configuration, CMake - fix building Draw Harness plugins as static libraries
[occt.git] / adm / cmake / occt_toolkit.cmake
1 # script for each OCCT toolkit
2
3 # filling some variables by default values(src) or using custom(tools, samples)
4 set (RELATIVE_SOURCES_DIR "${RELATIVE_DIR}")
5 if ("${RELATIVE_SOURCES_DIR}" STREQUAL "")
6   #if it is not defined, use default directory
7   set (RELATIVE_SOURCES_DIR "src")
8 endif()
9
10 set (OCC_MODULES_LIST "${MODULES_LIST}")
11 if ("${OCC_MODULES_LIST}" STREQUAL "")
12   set (OCC_MODULES_LIST ${OCCT_MODULES})
13 endif()
14
15 set (OCC_TARGET_FOLDER "${TARGET_FOLDER}")
16 if ("${OCC_TARGET_FOLDER}" STREQUAL "")
17   set (OCC_TARGET_FOLDER "Modules")
18 endif()
19
20 set (OCCT_TOOLKITS_NAME_SUFFIX "${TOOLKITS_NAME_SUFFIX}")
21 if ("${OCCT_TOOLKITS_NAME_SUFFIX}" STREQUAL "")
22   set (OCCT_TOOLKITS_NAME_SUFFIX "TOOLKITS")
23 endif()
24
25 # parse PACKAGES file
26 FILE_TO_LIST ("${RELATIVE_SOURCES_DIR}/${PROJECT_NAME}/PACKAGES" USED_PACKAGES)
27 if ("${USED_PACKAGES}" STREQUAL "")
28   set (USED_PACKAGES ${PROJECT_NAME})
29 endif()
30
31 if (USE_QT)
32   # Qt dependencies
33   OCCT_INCLUDE_CMAKE_FILE (adm/cmake/qt_macro)
34   FIND_QT_PACKAGE(PROJECT_LIBRARIES_DEBUG PROJECT_LIBRARIES_RELEASE PROJECT_INCLUDES)
35   include_directories("${PROJECT_INCLUDES}")
36 endif(USE_QT)
37
38 set (PRECOMPILED_DEFS)
39
40 if (NOT BUILD_SHARED_LIBS)
41   list (APPEND PRECOMPILED_DEFS "-DOCCT_NO_PLUGINS")
42   if (WIN32 AND NOT EXECUTABLE_PROJECT)
43     list (APPEND PRECOMPILED_DEFS "-DOCCT_STATIC_BUILD")
44   endif()
45 endif()
46
47 # Get all used packages from toolkit
48 UNSET(RESOURCE_FILES)
49 foreach (OCCT_PACKAGE ${USED_PACKAGES})
50
51   #remove part after "/" in the OCCT_PACKAGE variable if exists
52   string (FIND "${OCCT_PACKAGE}" "/" _index)
53   if (_index GREATER -1)
54     math (EXPR _index "${_index}")
55     string (SUBSTRING "${OCCT_PACKAGE}" 0 ${_index} OCCT_PACKAGE_NAME)
56   else()
57     set (OCCT_PACKAGE_NAME "${OCCT_PACKAGE}")
58   endif()
59
60   if (WIN32)
61     list (APPEND PRECOMPILED_DEFS "-D__${OCCT_PACKAGE_NAME}_DLL")
62   endif()
63
64   set (SOURCE_FILES)
65   set (HEADER_FILES)
66
67   # Generate Flex and Bison files
68   if (${BUILD_YACCLEX})
69     # flex files
70     OCCT_ORIGIN_AND_PATCHED_FILES ("${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}" "*[.]lex" SOURCE_FILES_FLEX)
71     list (LENGTH SOURCE_FILES_FLEX SOURCE_FILES_FLEX_LEN)
72
73     # bison files
74     OCCT_ORIGIN_AND_PATCHED_FILES ("${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}" "*[.]yacc" SOURCE_FILES_BISON)
75     list (LENGTH SOURCE_FILES_BISON SOURCE_FILES_BISON_LEN)
76
77     if (${SOURCE_FILES_FLEX_LEN} EQUAL ${SOURCE_FILES_BISON_LEN} AND NOT ${SOURCE_FILES_FLEX_LEN} EQUAL 0)
78       
79       list (SORT SOURCE_FILES_FLEX)
80       list (SORT SOURCE_FILES_BISON)
81
82       math (EXPR SOURCE_FILES_FLEX_LEN "${SOURCE_FILES_FLEX_LEN} - 1")
83       foreach (FLEX_FILE_INDEX RANGE ${SOURCE_FILES_FLEX_LEN})
84
85         list (GET SOURCE_FILES_FLEX ${FLEX_FILE_INDEX} CURRENT_FLEX_FILE)
86         get_filename_component (CURRENT_FLEX_FILE_NAME ${CURRENT_FLEX_FILE} NAME_WE)
87
88         list (GET SOURCE_FILES_BISON ${FLEX_FILE_INDEX} CURRENT_BISON_FILE)
89         get_filename_component (CURRENT_BISON_FILE_NAME ${CURRENT_BISON_FILE} NAME_WE)
90           
91         string (COMPARE EQUAL ${CURRENT_FLEX_FILE_NAME} ${CURRENT_BISON_FILE_NAME} ARE_FILES_EQUAL)
92
93         if (EXISTS "${CURRENT_FLEX_FILE}" AND EXISTS "${CURRENT_BISON_FILE}" AND ${ARE_FILES_EQUAL})
94
95           # Note: files are generated in original source directory (not in patch!)
96           set (FLEX_BISON_TARGET_DIR "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}")
97
98           # choose appropriate extension for generated files: "cxx" if source file contains
99           # instruction to generate C++ code, "c" otherwise
100           set (BISON_OUTPUT_FILE_EXT "c")
101           set (FLEX_OUTPUT_FILE_EXT "c")
102           file (STRINGS "${CURRENT_BISON_FILE}" FILE_BISON_CONTENT)
103           foreach (FILE_BISON_CONTENT_LINE ${FILE_BISON_CONTENT})
104             string (REGEX MATCH "%language \"C\\+\\+\"" CXX_BISON_LANGUAGE_FOUND ${FILE_BISON_CONTENT_LINE})
105             if (CXX_BISON_LANGUAGE_FOUND)
106               set (BISON_OUTPUT_FILE_EXT "cxx")
107             endif()
108           endforeach()
109           file (STRINGS "${CURRENT_FLEX_FILE}" FILE_FLEX_CONTENT)
110           foreach (FILE_FLEX_CONTENT_LINE ${FILE_FLEX_CONTENT})
111             string (REGEX MATCH "%option c\\+\\+" CXX_FLEX_LANGUAGE_FOUND ${FILE_FLEX_CONTENT_LINE})
112             if (CXX_FLEX_LANGUAGE_FOUND)
113               set (FLEX_OUTPUT_FILE_EXT "cxx")
114
115               # install copy of FlexLexer.h locally to allow further building without flex
116               if (FLEX_INCLUDE_DIR AND EXISTS "${FLEX_INCLUDE_DIR}/FlexLexer.h")
117                 configure_file("${FLEX_INCLUDE_DIR}/FlexLexer.h" "${FLEX_BISON_TARGET_DIR}/FlexLexer.h" @ONLY NEWLINE_STYLE LF)
118               endif()
119             endif()
120           endforeach()
121           set (BISON_OUTPUT_FILE ${CURRENT_BISON_FILE_NAME}.tab.${BISON_OUTPUT_FILE_EXT})
122           set (FLEX_OUTPUT_FILE lex.${CURRENT_FLEX_FILE_NAME}.${FLEX_OUTPUT_FILE_EXT})
123
124           BISON_TARGET (Parser_${CURRENT_BISON_FILE_NAME} ${CURRENT_BISON_FILE} "${FLEX_BISON_TARGET_DIR}/${BISON_OUTPUT_FILE}"
125                         COMPILE_FLAGS "-p ${CURRENT_BISON_FILE_NAME} -l -M ${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/=")
126           FLEX_TARGET  (Scanner_${CURRENT_FLEX_FILE_NAME} ${CURRENT_FLEX_FILE} "${FLEX_BISON_TARGET_DIR}/${FLEX_OUTPUT_FILE}"
127                         COMPILE_FLAGS "-P${CURRENT_FLEX_FILE_NAME} -L")
128           ADD_FLEX_BISON_DEPENDENCY (Scanner_${CURRENT_FLEX_FILE_NAME} Parser_${CURRENT_BISON_FILE_NAME})
129            
130           list (APPEND SOURCE_FILES ${BISON_OUTPUT_FILE} ${FLEX_OUTPUT_FILE})
131         endif()
132       endforeach()
133     endif()
134   endif()
135
136   # header files
137   if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES")
138     file (STRINGS "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES" HEADER_FILES_M   REGEX ".+[.]h")
139     file (STRINGS "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES" HEADER_FILES_LXX REGEX ".+[.]lxx")
140     file (STRINGS "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES" HEADER_FILES_GXX REGEX ".+[.]gxx")
141
142     file (STRINGS "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES" SOURCE_FILES_C REGEX ".+[.]c")
143     if(APPLE)
144       file (STRINGS "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES" SOURCE_FILES_M REGEX ".+[.]mm")
145     endif()
146   else()
147     file (STRINGS "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES"     HEADER_FILES_M   REGEX ".+[.]h")
148     file (STRINGS "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES"     HEADER_FILES_LXX REGEX ".+[.]lxx")
149     file (STRINGS "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES"     HEADER_FILES_GXX REGEX ".+[.]gxx")
150
151     file (STRINGS "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES"     SOURCE_FILES_C REGEX ".+[.]c")
152     if(APPLE)
153       file (STRINGS "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/FILES"   SOURCE_FILES_M REGEX ".+[.]mm")
154     endif()
155   endif()
156     
157   list (APPEND HEADER_FILES ${HEADER_FILES_M} ${HEADER_FILES_LXX} ${SOURCE_FILES_GXX})
158   list (APPEND SOURCE_FILES ${SOURCE_FILES_C})
159   if(APPLE)
160     list (APPEND SOURCE_FILES ${SOURCE_FILES_M})
161   endif()
162
163   foreach(HEADER_FILE ${HEADER_FILES})
164     if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${HEADER_FILE}")
165       message (STATUS "Info: consider patched file: ${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${HEADER_FILE}")
166       list (APPEND USED_INCFILES "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${HEADER_FILE}")
167       SOURCE_GROUP ("Header Files\\${OCCT_PACKAGE_NAME}" FILES "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${HEADER_FILE}")
168     else()
169       list (APPEND USED_INCFILES "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${HEADER_FILE}")
170       SOURCE_GROUP ("Header Files\\${OCCT_PACKAGE_NAME}" FILES "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${HEADER_FILE}")
171     endif()
172   endforeach()
173
174   foreach(SOURCE_FILE ${SOURCE_FILES})
175     if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${SOURCE_FILE}")
176       message (STATUS "Info: consider patched file: ${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${SOURCE_FILE}")
177       list (APPEND USED_SRCFILES "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${SOURCE_FILE}")
178       SOURCE_GROUP ("Source Files\\${OCCT_PACKAGE_NAME}" FILES "${BUILD_PATCH}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${SOURCE_FILE}")
179     else()
180       list (APPEND USED_SRCFILES "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${SOURCE_FILE}")
181       SOURCE_GROUP ("Source Files\\${OCCT_PACKAGE_NAME}" FILES "${CMAKE_SOURCE_DIR}/${RELATIVE_SOURCES_DIR}/${OCCT_PACKAGE}/${SOURCE_FILE}")
182     endif()
183   endforeach()
184
185   if (USE_QT)
186     FIND_AND_INSTALL_QT_RESOURCES (${OCCT_PACKAGE} RESOURCE_FILES)
187     #message("Qt Resource files are: ${QT_RESOURCE_FILES} in ${OCCT_PACKAGE}")
188   endif(USE_QT)
189
190   #message("Resource files are: ${RESOURCE_FILES} in ${OCCT_PACKAGE}")
191   foreach(RESOURCE_FILE ${RESOURCE_FILES})
192     SOURCE_GROUP ("Resource Files\\${OCCT_PACKAGE_NAME}" FILES "${RESOURCE_FILE}")
193   endforeach()
194 endforeach()
195 string (REGEX REPLACE ";" " " PRECOMPILED_DEFS "${PRECOMPILED_DEFS}")
196
197 set (USED_RCFILE "")
198 if (MSVC)
199   set (USED_RCFILE "${CMAKE_BINARY_DIR}/resources/${PROJECT_NAME}.rc")
200
201   if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/adm/templates/occt_toolkit.rc.in")
202     configure_file("${APPLY_OCCT_PATCH_DIR}/adm/templates/occt_toolkit.rc.in" "${USED_RCFILE}" @ONLY)
203   else()
204     configure_file("${CMAKE_SOURCE_DIR}/adm/templates/occt_toolkit.rc.in" "${USED_RCFILE}" @ONLY)
205   endif()
206 endif()
207
208 set (CURRENT_MODULE)
209 foreach (OCCT_MODULE ${OCC_MODULES_LIST})
210   list (FIND ${OCCT_MODULE}_${OCCT_TOOLKITS_NAME_SUFFIX} ${PROJECT_NAME} CURRENT_PROJECT_IS_BUILT)
211
212   if (NOT ${CURRENT_PROJECT_IS_BUILT} EQUAL -1)
213     set (CURRENT_MODULE ${OCCT_MODULE})
214   endif()
215 endforeach()
216
217 if (MSVC)
218   OCCT_INSERT_CODE_FOR_TARGET ()
219 endif()
220
221 if (USE_QT)
222   FIND_AND_WRAP_MOC_FILES("${USED_INCFILES}" "${PROJECT_NAME}_MOC_FILES")
223   #message("MOC files: ${${PROJECT_NAME}_MOC_FILES}")
224 endif (USE_QT)
225
226 if (EXECUTABLE_PROJECT)
227   add_executable (${PROJECT_NAME} ${USED_SRCFILES} ${USED_INCFILES} ${USED_RCFILE} ${RESOURCE_FILES} ${${PROJECT_NAME}_MOC_FILES})
228
229   install (TARGETS ${PROJECT_NAME}
230            DESTINATION "${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}")
231
232   if (EMSCRIPTEN)
233     install(FILES ${CMAKE_BINARY_DIR}/${OS_WITH_BIT}/${COMPILER}/bin\${OCCT_INSTALL_BIN_LETTER}/${PROJECT_NAME}.wasm DESTINATION "${INSTALL_DIR_BIN}/${OCCT_INSTALL_BIN_LETTER}")
234   endif()
235 else()
236   add_library (${PROJECT_NAME} ${USED_SRCFILES} ${USED_INCFILES} ${USED_RCFILE} ${RESOURCE_FILES} ${${PROJECT_NAME}_MOC_FILES})
237
238   if (MSVC)
239     install (FILES  ${CMAKE_BINARY_DIR}/${OS_WITH_BIT}/${COMPILER}/bin\${OCCT_INSTALL_BIN_LETTER}/${PROJECT_NAME}.pdb
240              CONFIGURATIONS Debug RelWithDebInfo
241              DESTINATION "${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}")
242   endif()
243
244   if (BUILD_SHARED_LIBS AND NOT "${BUILD_SHARED_LIBRARY_NAME_POSTFIX}" STREQUAL "")
245     set (CMAKE_SHARED_LIBRARY_SUFFIX_DEFAULT ${CMAKE_SHARED_LIBRARY_SUFFIX})
246     set (CMAKE_SHARED_LIBRARY_SUFFIX "${BUILD_SHARED_LIBRARY_NAME_POSTFIX}${CMAKE_SHARED_LIBRARY_SUFFIX}")
247   endif()
248
249   install (TARGETS ${PROJECT_NAME}
250            EXPORT OpenCASCADE${CURRENT_MODULE}Targets
251            RUNTIME DESTINATION "${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}"
252            ARCHIVE DESTINATION "${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}"
253            LIBRARY DESTINATION "${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}")
254
255   if (NOT WIN32)
256     if (BUILD_SHARED_LIBS AND NOT "${BUILD_SHARED_LIBRARY_NAME_POSTFIX}" STREQUAL "")
257       set (LINK_NAME    "${INSTALL_DIR}/${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}/lib${PROJECT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX_DEFAULT}")
258       set (LIBRARY_NAME "${INSTALL_DIR}/${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}/lib${PROJECT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}")
259       OCCT_CREATE_SYMLINK_TO_FILE (${LIBRARY_NAME} ${LINK_NAME})
260     endif()
261   endif()
262 endif()
263
264 if (CURRENT_MODULE)
265   set_target_properties (${PROJECT_NAME} PROPERTIES FOLDER "${OCC_TARGET_FOLDER}/${CURRENT_MODULE}")
266   set_target_properties (${PROJECT_NAME} PROPERTIES MODULE "${CURRENT_MODULE}")
267   if (APPLE)
268     if (NOT "${INSTALL_NAME_DIR}" STREQUAL "")
269       set_target_properties (${PROJECT_NAME} PROPERTIES BUILD_WITH_INSTALL_RPATH 1 INSTALL_NAME_DIR "${INSTALL_NAME_DIR}")
270     endif()
271   endif()
272 endif()
273
274 get_property (OCC_VERSION_MAJOR GLOBAL PROPERTY OCC_VERSION_MAJOR)
275 get_property (OCC_VERSION_MINOR GLOBAL PROPERTY OCC_VERSION_MINOR)
276 get_property (OCC_VERSION_MAINTENANCE GLOBAL PROPERTY OCC_VERSION_MAINTENANCE)
277
278 if (ANDROID)
279   # do not append version to the filename
280   set_target_properties (${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${PRECOMPILED_DEFS}")
281 else()
282   set_target_properties (${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${PRECOMPILED_DEFS}"
283                                                     SOVERSION     "${OCC_VERSION_MAJOR}"
284                                                     VERSION       "${OCC_VERSION_MAJOR}.${OCC_VERSION_MINOR}.${OCC_VERSION_MAINTENANCE}")
285 endif()
286
287 set (USED_TOOLKITS_BY_CURRENT_PROJECT)
288 set (USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT)
289
290 # parse EXTERNLIB file
291 if (CUSTOM_EXTERNLIB)
292   set (USED_EXTERNLIB_AND_TOOLKITS ${CUSTOM_EXTERNLIB})
293 else()
294   FILE_TO_LIST ("${RELATIVE_SOURCES_DIR}/${PROJECT_NAME}/EXTERNLIB" USED_EXTERNLIB_AND_TOOLKITS)
295 endif()
296 foreach (USED_ITEM ${USED_EXTERNLIB_AND_TOOLKITS})
297   string (REGEX MATCH "^ *#" COMMENT_FOUND ${USED_ITEM})
298   if (NOT COMMENT_FOUND)
299     string (REGEX MATCH "^TK" TK_FOUND ${USED_ITEM})
300     string (REGEX MATCH "^vtk" VTK_FOUND ${USED_ITEM})
301     
302     if (NOT "${TK_FOUND}" STREQUAL "" OR NOT "${VTK_FOUND}" STREQUAL "")
303       list (APPEND USED_TOOLKITS_BY_CURRENT_PROJECT ${USED_ITEM})
304       if (NOT "${VTK_FOUND}" STREQUAL "" AND BUILD_SHARED_LIBS AND INSTALL_VTK AND COMMAND OCCT_INSTALL_VTK)
305         OCCT_INSTALL_VTK(${USED_ITEM})
306       endif()
307     else()
308       string (REGEX MATCH "^CSF_" CSF_FOUND ${USED_ITEM})
309       if ("${CSF_FOUND}" STREQUAL "")
310         message (STATUS "Info: ${USED_ITEM} from ${PROJECT_NAME} skipped due to it is empty")
311       else() # get CSF_ value
312         set (CURRENT_CSF ${${USED_ITEM}})
313         if (NOT "x${CURRENT_CSF}" STREQUAL "x")
314           if ("${CURRENT_CSF}" STREQUAL "CSF_OpenGlLibs")
315             add_definitions (-DHAVE_OPENGL)
316           endif()
317           if ("${CURRENT_CSF}" STREQUAL "CSF_OpenGlesLibs")
318             add_definitions (-DHAVE_GLES2)
319           endif()
320
321           set (LIBRARY_FROM_CACHE 0)
322           separate_arguments (CURRENT_CSF)
323           foreach (CSF_LIBRARY ${CURRENT_CSF})
324             string (TOLOWER "${CSF_LIBRARY}" CSF_LIBRARY)
325             string (REPLACE "+" "[+]" CSF_LIBRARY "${CSF_LIBRARY}")
326             string (REPLACE "." "" CSF_LIBRARY "${CSF_LIBRARY}")
327             get_cmake_property(ALL_CACHE_VARIABLES CACHE_VARIABLES)
328             string (REGEX MATCHALL "(^|;)3RDPARTY_[^;]+_LIBRARY[^;]*" ALL_CACHE_VARIABLES "${ALL_CACHE_VARIABLES}")
329             foreach (CACHE_VARIABLE ${ALL_CACHE_VARIABLES})
330               set (CURRENT_CACHE_LIBRARY ${${CACHE_VARIABLE}})
331               string (TOLOWER "${CACHE_VARIABLE}" CACHE_VARIABLE)
332
333               if (EXISTS "${CURRENT_CACHE_LIBRARY}" AND NOT IS_DIRECTORY "${CURRENT_CACHE_LIBRARY}")
334                 string (REGEX MATCH "_${CSF_LIBRARY}$" IS_ENDING "${CACHE_VARIABLE}")
335                 string (REGEX MATCH "^([a-z]+)" CSF_WO_VERSION "${CSF_LIBRARY}")
336                 string (REGEX MATCH "_${CSF_WO_VERSION}$" IS_ENDING_WO_VERSION "${CACHE_VARIABLE}")
337                 if ("3rdparty_${CSF_LIBRARY}_library" STREQUAL "${CACHE_VARIABLE}" OR
338                     "3rdparty_${CSF_WO_VERSION}_library" STREQUAL "${CACHE_VARIABLE}" OR
339                     NOT "x${IS_ENDING}" STREQUAL "x" OR
340                     NOT "x${IS_ENDING_WO_VERSION}" STREQUAL "x")
341                   list (APPEND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT "${CURRENT_CACHE_LIBRARY}")
342                   set (LIBRARY_FROM_CACHE 1)
343                 endif()
344               endif()
345             endforeach()
346           endforeach()
347
348           if (NOT ${LIBRARY_FROM_CACHE})
349             # prepare a list from a string with whitespaces
350             separate_arguments (CURRENT_CSF)
351             list (APPEND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT ${CURRENT_CSF})
352           endif()
353         endif()
354       endif()
355     endif()
356   endif()
357 endforeach()
358
359 if (APPLE)
360   list (FIND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT X11 IS_X11_FOUND)
361   if (NOT ${IS_X11_FOUND} EQUAL -1)
362     find_package (X11 COMPONENTS X11 Xext Xmu Xi)
363     if (NOT X11_FOUND)
364       message (STATUS "Warning: X11 is not found. It's required to install The XQuartz project: http://www.xquartz.org")
365     endif()
366   endif()
367 endif()
368
369 # Update list of used VTK libraries if OpenGL2 Rendering BackEnd is used.
370 # Add VTK_OPENGL2_BACKEND definition.
371 if("${VTK_RENDERING_BACKEND}" STREQUAL "OpenGL2" OR IS_VTK_9XX)
372   add_definitions(-DVTK_OPENGL2_BACKEND)
373   foreach (VTK_EXCLUDE_LIBRARY vtkRenderingOpenGL vtkRenderingFreeTypeOpenGL)
374     list (FIND USED_TOOLKITS_BY_CURRENT_PROJECT "${VTK_EXCLUDE_LIBRARY}" IS_VTK_OPENGL_FOUND)
375     if (NOT ${IS_VTK_OPENGL_FOUND} EQUAL -1)
376       list (REMOVE_ITEM USED_TOOLKITS_BY_CURRENT_PROJECT ${VTK_EXCLUDE_LIBRARY})
377       if (${VTK_EXCLUDE_LIBRARY} STREQUAL vtkRenderingOpenGL)
378         list (APPEND USED_TOOLKITS_BY_CURRENT_PROJECT vtkRenderingOpenGL2)
379         if(VTK_MAJOR_VERSION GREATER 6)
380           list (APPEND USED_TOOLKITS_BY_CURRENT_PROJECT vtkRenderingGL2PSOpenGL2)
381         endif()
382       endif()
383     endif()
384   endforeach()
385 else()
386   if(VTK_MAJOR_VERSION EQUAL 6 AND VTK_MINOR_VERSION GREATER 2 OR VTK_MAJOR_VERSION GREATER 6)
387     list (FIND USED_TOOLKITS_BY_CURRENT_PROJECT "vtkRenderingFreeTypeOpenGL" IS_VTK_RENDER_FREETYPE_FOUND)
388     if (NOT ${IS_VTK_RENDER_FREETYPE_FOUND} EQUAL -1)
389       list (REMOVE_ITEM USED_TOOLKITS_BY_CURRENT_PROJECT "vtkRenderingFreeTypeOpenGL")
390     endif()
391   endif()
392 endif()
393
394 if (BUILD_SHARED_LIBS OR EXECUTABLE_PROJECT)
395   if(IS_VTK_9XX)
396     string (REGEX REPLACE "vtk" "VTK::" USED_TOOLKITS_BY_CURRENT_PROJECT "${USED_TOOLKITS_BY_CURRENT_PROJECT}")
397   endif()
398   target_link_libraries (${PROJECT_NAME} ${USED_TOOLKITS_BY_CURRENT_PROJECT} ${USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT})
399 endif()
400
401 if (USE_QT)
402   foreach (PROJECT_LIBRARY_DEBUG ${PROJECT_LIBRARIES_DEBUG})
403     target_link_libraries (${PROJECT_NAME} debug ${PROJECT_LIBRARY_DEBUG})
404   endforeach()
405   foreach (PROJECT_LIBRARY_RELEASE ${PROJECT_LIBRARIES_RELEASE})
406     target_link_libraries (${PROJECT_NAME} optimized ${PROJECT_LIBRARY_RELEASE})
407   endforeach()
408 endif()
409
410 # suppress deprecation warnings inside OCCT itself for old gcc versions with unavailable Standard_DISABLE_DEPRECATION_WARNINGS
411 if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
412   if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6.0)
413     add_definitions("-DOCCT_NO_DEPRECATED")
414     message (STATUS "Warning: internal deprecation warnings by Standard_DEPRECATED have been disabled due to old gcc version being used")
415   endif()
416 endif()
417
418 # use Cotire to accelerate build via usage of precompiled headers
419 if (BUILD_USE_PCH)
420   if (WIN32)
421     # prevent definition of min and max macros through inclusion of Windows.h
422     # (for cotire builds)
423     add_definitions("-DNOMINMAX")
424     # avoid warnings on deprecated names from standard C library (see strsafe.h)
425     add_definitions("-DSTRSAFE_NO_DEPRECATE")
426     # avoid "std::Equal1" warning in QANCollection_Stl.cxx in debug mode
427     # suggesting using msvc "Checked Iterators"
428     add_definitions("-D_SCL_SECURE_NO_WARNINGS")
429   endif()
430
431   # Exclude system-provided glext.h.
432   # These macros are already defined within OpenGl_GlFunctions.hxx,
433   # however we have to duplicate them here for building TKOpenGl with PCH.
434   add_definitions("-DGL_GLEXT_LEGACY")
435   add_definitions("-DGLX_GLXEXT_LEGACY")
436
437   # workaround for old gcc
438   if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
439     add_definitions("-D__STDC_CONSTANT_MACROS")
440     add_definitions("-D__STDC_FORMAT_MACROS")
441   endif()
442
443   # unity builds are not used since they do not add speed but cause conflicts
444   # in TKV3d
445   set_target_properties(${PROJECT_NAME} PROPERTIES COTIRE_ADD_UNITY_BUILD FALSE)
446
447   cotire(${PROJECT_NAME})
448 endif()