1702611e471ae1df54d9f5b6db44db08ecaba316
[occt.git] / adm / cmake / occt_toolkit.cmake
1 # script for each OCCT toolkit
2
3 # parce PACKAGES file
4 if ("${PROJECT_NAME}" STREQUAL DRAWEXE)
5   set (USED_PACKAGES DRAWEXE)
6 else()
7   FILE_TO_LIST ("src/${PROJECT_NAME}/PACKAGES" USED_PACKAGES)
8 endif()
9
10 set (PRECOMPILED_DEFS)
11
12 if (NOT BUILD_SHARED_LIBS)
13   list (APPEND PRECOMPILED_DEFS "-DOCCT_NO_PLUGINS")
14   if (WIN32)
15     list (APPEND PRECOMPILED_DEFS "-DHAVE_NO_DLL")
16   endif()
17 endif()
18
19 # Get all used packages from toolkit
20 foreach (OCCT_PACKAGE ${USED_PACKAGES})
21   
22   # TKService contains platform-dependent packages: Xw and WNT
23   if ((WIN32 AND "${OCCT_PACKAGE}" STREQUAL "Xw") OR (NOT WIN32 AND "${OCCT_PACKAGE}" STREQUAL "WNT"))
24     # do nothing
25   else()
26
27     if (WIN32)
28       list (APPEND PRECOMPILED_DEFS "-D__${OCCT_PACKAGE}_DLL")
29     endif()
30
31     set (SOURCE_FILES)
32     set (HEADER_FILES)
33
34     # Generate Flex and Bison files
35     if (${BUILD_YACCLEX})
36
37       # flex files
38       OCCT_ORIGIN_AND_PATCHED_FILES ("src/${OCCT_PACKAGE}" "*[.]lex" SOURCE_FILES_FLEX)
39       list (LENGTH SOURCE_FILES_FLEX SOURCE_FILES_FLEX_LEN)
40
41       # bison files
42       OCCT_ORIGIN_AND_PATCHED_FILES ("src/${OCCT_PACKAGE}" "*[.]yacc" SOURCE_FILES_BISON)
43       list (LENGTH SOURCE_FILES_BISON SOURCE_FILES_BISON_LEN)
44
45       if (${SOURCE_FILES_FLEX_LEN} EQUAL ${SOURCE_FILES_BISON_LEN} AND NOT ${SOURCE_FILES_FLEX_LEN} EQUAL 0)
46       
47         list (SORT SOURCE_FILES_FLEX)
48         list (SORT SOURCE_FILES_BISON)
49
50         math (EXPR SOURCE_FILES_FLEX_LEN "${SOURCE_FILES_FLEX_LEN} - 1")
51         foreach (FLEX_FILE_INDEX RANGE ${SOURCE_FILES_FLEX_LEN})
52
53           list (GET SOURCE_FILES_FLEX ${FLEX_FILE_INDEX} CURRENT_FLEX_FILE)
54           get_filename_component (CURRENT_FLEX_FILE_NAME ${CURRENT_FLEX_FILE} NAME_WE)
55
56           list (GET SOURCE_FILES_BISON ${FLEX_FILE_INDEX} CURRENT_BISON_FILE)
57           get_filename_component (CURRENT_BISON_FILE_NAME ${CURRENT_BISON_FILE} NAME_WE)
58           
59           string (COMPARE EQUAL ${CURRENT_FLEX_FILE_NAME} ${CURRENT_BISON_FILE_NAME} ARE_FILES_EQUAL)
60
61           if (EXISTS "${CURRENT_FLEX_FILE}" AND EXISTS "${CURRENT_BISON_FILE}" AND ${ARE_FILES_EQUAL})
62             set (BISON_OUTPUT_FILE ${CURRENT_BISON_FILE_NAME}.tab.c)
63             set (FLEX_OUTPUT_FILE lex.${CURRENT_FLEX_FILE_NAME}.c)
64             BISON_TARGET (Parser_${CURRENT_BISON_FILE_NAME} ${CURRENT_BISON_FILE} ${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/${BISON_OUTPUT_FILE} COMPILE_FLAGS "-p ${CURRENT_BISON_FILE_NAME}")
65             FLEX_TARGET  (Scanner_${CURRENT_FLEX_FILE_NAME} ${CURRENT_FLEX_FILE} ${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/${FLEX_OUTPUT_FILE} COMPILE_FLAGS "-P${CURRENT_FLEX_FILE_NAME}")
66             ADD_FLEX_BISON_DEPENDENCY (Scanner_${CURRENT_FLEX_FILE_NAME} Parser_${CURRENT_BISON_FILE_NAME})
67            
68             list (APPEND SOURCE_FILES ${BISON_OUTPUT_FILE} ${FLEX_OUTPUT_FILE})
69           endif()
70         endforeach()
71       endif()
72     endif()
73
74     # header files
75     if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES")
76       file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" HEADER_FILES_M   REGEX ".+[.]h")
77       file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" HEADER_FILES_LXX REGEX ".+[.]lxx")
78       file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" HEADER_FILES_GXX REGEX ".+[.]gxx")
79
80       file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" SOURCE_FILES_C REGEX ".+[.]c")
81       if(APPLE)
82         file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" SOURCE_FILES_M REGEX ".+[.]mm")
83       endif()
84     else()
85       file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/FILES"     HEADER_FILES_M   REGEX ".+[.]h")
86       file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/FILES"     HEADER_FILES_LXX REGEX ".+[.]lxx")
87       file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/FILES"     HEADER_FILES_GXX REGEX ".+[.]gxx")
88
89       file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/FILES"     SOURCE_FILES_C REGEX ".+[.]c")
90       if(APPLE)
91         file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/FILES"   SOURCE_FILES_M REGEX ".+[.]mm")
92       endif()
93     endif()
94     
95     list (APPEND HEADER_FILES ${HEADER_FILES_M} ${HEADER_FILES_LXX} ${SOURCE_FILES_GXX})
96     list (APPEND SOURCE_FILES ${SOURCE_FILES_C})
97     if(APPLE)
98       list (APPEND SOURCE_FILES ${SOURCE_FILES_M})
99     endif()
100
101     foreach(HEADER_FILE ${HEADER_FILES})
102       if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
103         message (STATUS "Info: consider patched file: ${BUILD_PATCH}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
104         list (APPEND USED_INCFILES "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
105         SOURCE_GROUP ("Header Files\\${OCCT_PACKAGE}" FILES "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
106       else()
107         list (APPEND USED_INCFILES "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
108         SOURCE_GROUP ("Header Files\\${OCCT_PACKAGE}" FILES "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
109       endif()
110     endforeach()
111
112     foreach(SOURCE_FILE ${SOURCE_FILES})
113       if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
114         message (STATUS "Info: consider patched file: ${BUILD_PATCH}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
115         list (APPEND USED_SRCFILES "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
116         SOURCE_GROUP ("Source Files\\${OCCT_PACKAGE}" FILES "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
117       else()
118         list (APPEND USED_SRCFILES "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
119         SOURCE_GROUP ("Source Files\\${OCCT_PACKAGE}" FILES "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
120       endif()
121     endforeach()
122   endif()
123 endforeach()
124 string (REGEX REPLACE ";" " " PRECOMPILED_DEFS "${PRECOMPILED_DEFS}")
125
126 set (USED_RCFILE "")
127 if (MSVC)
128   set (USED_RCFILE "${CMAKE_BINARY_DIR}/resources/${PROJECT_NAME}.rc")
129
130   if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/adm/templates/occt_toolkit.rc.in")
131     configure_file("${APPLY_OCCT_PATCH_DIR}/adm/templates/occt_toolkit.rc.in" "${USED_RCFILE}" @ONLY)
132   else()
133     configure_file("${CMAKE_SOURCE_DIR}/adm/templates/occt_toolkit.rc.in" "${USED_RCFILE}" @ONLY)
134   endif()
135 endif()
136
137 set (CURRENT_MODULE)
138 foreach (OCCT_MODULE ${OCCT_MODULES})
139   list (FIND ${OCCT_MODULE}_TOOLKITS ${PROJECT_NAME} CURRENT_PROJECT_IS_BUILT)
140   if (NOT ${CURRENT_PROJECT_IS_BUILT} EQUAL -1)
141     set (CURRENT_MODULE ${OCCT_MODULE})
142   endif()
143 endforeach()
144
145 if (WIN32)
146   OCCT_INSERT_CODE_FOR_TARGET ()
147 endif()
148
149 if ("${PROJECT_NAME}" STREQUAL "DRAWEXE")
150   add_executable (${PROJECT_NAME} ${USED_SRCFILES} ${USED_INCFILES} ${USED_RCFILE})
151
152   install (TARGETS ${PROJECT_NAME}
153            DESTINATION "${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}")
154 else()
155   add_library (${PROJECT_NAME} ${USED_SRCFILES} ${USED_INCFILES} ${USED_RCFILE})
156
157   if (MSVC)
158     install (FILES  ${CMAKE_BINARY_DIR}/${OS_WITH_BIT}/${COMPILER}/bin\${OCCT_INSTALL_BIN_LETTER}/${PROJECT_NAME}.pdb
159              CONFIGURATIONS Debug RelWithDebInfo
160              DESTINATION "${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}")
161   endif()
162
163   if (BUILD_SHARED_LIBS AND NOT "${BUILD_SHARED_LIBRARY_NAME_POSTFIX}" STREQUAL "")
164     set (CMAKE_SHARED_LIBRARY_SUFFIX_DEFAULT ${CMAKE_SHARED_LIBRARY_SUFFIX})
165     set (CMAKE_SHARED_LIBRARY_SUFFIX "${BUILD_SHARED_LIBRARY_NAME_POSTFIX}${CMAKE_SHARED_LIBRARY_SUFFIX}")
166   endif()
167
168   install (TARGETS ${PROJECT_NAME}
169            EXPORT OpenCASCADE${CURRENT_MODULE}Targets
170            RUNTIME DESTINATION "${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}"
171            ARCHIVE DESTINATION "${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}"
172            LIBRARY DESTINATION "${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}")
173
174   if (NOT WIN32)
175     if (BUILD_SHARED_LIBS AND NOT "${BUILD_SHARED_LIBRARY_NAME_POSTFIX}" STREQUAL "")
176       set (LINK_NAME    "${INSTALL_DIR}/${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}/lib${PROJECT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX_DEFAULT}")
177       set (LIBRARY_NAME "${INSTALL_DIR}/${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}/lib${PROJECT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}")
178       OCCT_CREATE_SYMLINK_TO_FILE (${LIBRARY_NAME} ${LINK_NAME})
179     endif()
180   endif()
181 endif()
182
183 if (CURRENT_MODULE)
184   set_target_properties (${PROJECT_NAME} PROPERTIES FOLDER "Modules/${CURRENT_MODULE}")
185   set_target_properties (${PROJECT_NAME} PROPERTIES MODULE "${CURRENT_MODULE}")
186   if (APPLE)
187     if (NOT "${INSTALL_NAME_DIR}" STREQUAL "")
188       set_target_properties (${PROJECT_NAME} PROPERTIES BUILD_WITH_INSTALL_RPATH 1 INSTALL_NAME_DIR "${INSTALL_NAME_DIR}")
189     endif()
190   endif()
191 endif()
192
193 get_property (OCC_VERSION_MAJOR GLOBAL PROPERTY OCC_VERSION_MAJOR)
194 get_property (OCC_VERSION_MINOR GLOBAL PROPERTY OCC_VERSION_MINOR)
195 get_property (OCC_VERSION_MAINTENANCE GLOBAL PROPERTY OCC_VERSION_MAINTENANCE)
196
197 if (ANDROID)
198   # do not append version to the filename
199   set_target_properties (${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${PRECOMPILED_DEFS}")
200 else()
201   set_target_properties (${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${PRECOMPILED_DEFS}"
202                                                     SOVERSION     "${OCC_VERSION_MAJOR}"
203                                                     VERSION       "${OCC_VERSION_MAJOR}.${OCC_VERSION_MINOR}.${OCC_VERSION_MAINTENANCE}")
204 endif()
205
206 set (USED_TOOLKITS_BY_CURRENT_PROJECT)
207 set (USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT)
208
209 # parce EXTERNLIB file
210 FILE_TO_LIST ("src/${PROJECT_NAME}/EXTERNLIB" USED_EXTERNLIB_AND_TOOLKITS)
211 foreach (USED_ITEM ${USED_EXTERNLIB_AND_TOOLKITS})
212   string (REGEX MATCH "^ *#" COMMENT_FOUND ${USED_ITEM})
213   if (NOT COMMENT_FOUND)
214     string (REGEX MATCH "^TK" TK_FOUND ${USED_ITEM})
215     string (REGEX MATCH "^vtk" VTK_FOUND ${USED_ITEM})
216     
217     if (NOT "${TK_FOUND}" STREQUAL "" OR NOT "${VTK_FOUND}" STREQUAL "")
218       list (APPEND USED_TOOLKITS_BY_CURRENT_PROJECT ${USED_ITEM})
219       if (NOT "${VTK_FOUND}" STREQUAL "" AND BUILD_SHARED_LIBS AND INSTALL_VTK AND COMMAND OCCT_INSTALL_VTK)
220         OCCT_INSTALL_VTK(${USED_ITEM})
221       endif()
222     else()
223       string (REGEX MATCH "^CSF_" CSF_FOUND ${USED_ITEM})
224       if ("${CSF_FOUND}" STREQUAL "")
225         message (STATUS "Info: ${USED_ITEM} from ${PROJECT_NAME} skipped due to it is empty")
226       else() # get CSF_ value
227         set (CURRENT_CSF ${${USED_ITEM}})
228         if (NOT "x${CURRENT_CSF}" STREQUAL "x")
229           set (LIBRARY_FROM_CACHE 0)
230           separate_arguments (CURRENT_CSF)
231           foreach (CSF_LIBRARY ${CURRENT_CSF})
232             string (TOLOWER "${CSF_LIBRARY}" CSF_LIBRARY)
233             string (REPLACE "+" "[+]" CSF_LIBRARY "${CSF_LIBRARY}")
234             string (REPLACE "." "" CSF_LIBRARY "${CSF_LIBRARY}")
235             get_cmake_property(ALL_CACHE_VARIABLES CACHE_VARIABLES)
236             string (REGEX MATCHALL "(^|;)3RDPARTY_[^;]+_LIBRARY[^;]*" ALL_CACHE_VARIABLES "${ALL_CACHE_VARIABLES}")
237             foreach (CACHE_VARIABLE ${ALL_CACHE_VARIABLES})
238               set (CURRENT_CACHE_LIBRARY ${${CACHE_VARIABLE}})
239               string (TOLOWER "${CACHE_VARIABLE}" CACHE_VARIABLE)
240
241               if (EXISTS "${CURRENT_CACHE_LIBRARY}" AND NOT IS_DIRECTORY "${CURRENT_CACHE_LIBRARY}")
242                 string (REGEX MATCH "_${CSF_LIBRARY}$" IS_ENDING "${CACHE_VARIABLE}")
243                 string (REGEX MATCH "^([a-z]+)" CSF_WO_VERSION "${CSF_LIBRARY}")
244                 string (REGEX MATCH "_${CSF_WO_VERSION}$" IS_ENDING_WO_VERSION "${CACHE_VARIABLE}")
245                 if ("3rdparty_${CSF_LIBRARY}_library" STREQUAL "${CACHE_VARIABLE}" OR
246                     "3rdparty_${CSF_WO_VERSION}_library" STREQUAL "${CACHE_VARIABLE}" OR
247                     NOT "x${IS_ENDING}" STREQUAL "x" OR
248                     NOT "x${IS_ENDING_WO_VERSION}" STREQUAL "x")
249                   list (APPEND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT "${CURRENT_CACHE_LIBRARY}")
250                   set (LIBRARY_FROM_CACHE 1)
251                 endif()
252               endif()
253             endforeach()
254           endforeach()
255
256           if (NOT ${LIBRARY_FROM_CACHE})
257             # prepare a list from a string with whitespaces
258             separate_arguments (CURRENT_CSF)
259             list (APPEND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT ${CURRENT_CSF})
260           endif()
261         endif()
262       endif()
263     endif()
264   endif()
265 endforeach()
266
267 if (APPLE)
268   list (FIND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT X11 IS_X11_FOUND)
269   if (NOT ${IS_X11_FOUND} EQUAL -1)
270     find_package (X11 COMPONENTS X11 Xext Xmu Xi)
271     if (NOT X11_FOUND)
272       message (STATUS "Warning: X11 is not found. It's required to install The XQuartz project: http://www.xquartz.org")
273     endif()
274   endif()
275 endif()
276
277 # Update list of used VTK libraries if OpenGL2 Rendering BackEnd is used.
278 # Add VTK_OPENGL2_BACKEND definition.
279 if("${VTK_RENDERING_BACKEND}" STREQUAL "OpenGL2")
280   add_definitions(-DVTK_OPENGL2_BACKEND)
281   foreach (VTK_EXCLUDE_LIBRARY vtkRenderingOpenGL vtkRenderingFreeTypeOpenGL)
282     list (FIND USED_TOOLKITS_BY_CURRENT_PROJECT "${VTK_EXCLUDE_LIBRARY}" IS_VTK_OPENGL_FOUND)
283     if (NOT ${IS_VTK_OPENGL_FOUND} EQUAL -1)
284       list (REMOVE_ITEM USED_TOOLKITS_BY_CURRENT_PROJECT ${VTK_EXCLUDE_LIBRARY})
285       if (${VTK_EXCLUDE_LIBRARY} STREQUAL vtkRenderingOpenGL)
286         list (APPEND USED_TOOLKITS_BY_CURRENT_PROJECT vtkRenderingOpenGL2)
287         if(VTK_MAJOR_VERSION GREATER 6)
288           list (APPEND USED_TOOLKITS_BY_CURRENT_PROJECT vtkRenderingGL2PSOpenGL2)
289         endif()
290       endif()
291     endif()
292   endforeach()
293 else()
294   if(VTK_MAJOR_VERSION EQUAL 6 AND VTK_MINOR_VERSION GREATER 2 OR VTK_MAJOR_VERSION GREATER 6)
295     list (FIND USED_TOOLKITS_BY_CURRENT_PROJECT "vtkRenderingFreeTypeOpenGL" IS_VTK_RENDER_FREETYPE_FOUND)
296     if (NOT ${IS_VTK_RENDER_FREETYPE_FOUND} EQUAL -1)
297       list (REMOVE_ITEM USED_TOOLKITS_BY_CURRENT_PROJECT "vtkRenderingFreeTypeOpenGL")
298     endif()
299   endif()
300 endif()
301
302 if (BUILD_SHARED_LIBS)
303   target_link_libraries (${PROJECT_NAME} ${USED_TOOLKITS_BY_CURRENT_PROJECT} ${USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT})
304 endif()
305
306 # suppress deprecation warnings inside OCCT itself for old gcc versions with unavailable Standard_DISABLE_DEPRECATION_WARNINGS
307 if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
308   if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6.0)
309     add_definitions("-DOCCT_NO_DEPRECATED")
310     message (STATUS "Warning: internal deprecation warnings by Standard_DEPRECATED have been disabled due to old gcc version being used")
311   endif()
312 endif()
313
314 # use Cotire to accelerate build via usage of precompiled headers
315 if (BUILD_USE_PCH)
316   if (WIN32)
317     # prevent definition of min and max macros through inclusion of Windows.h
318     # (for cotire builds)
319     add_definitions("-DNOMINMAX")
320     # avoid warnings on deprecated names from standard C library (see strsafe.h)
321     add_definitions("-DSTRSAFE_NO_DEPRECATE")
322     # avoid "std::Equal1" warning in QANCollection_Stl.cxx in debug mode
323     # suggesting using msvc "Checked Iterators"
324     add_definitions("-D_SCL_SECURE_NO_WARNINGS")
325   endif()
326
327   # Exclude system-provided glext.h.
328   # These macros are already defined within OpenGl_GlFunctions.hxx,
329   # however we have to duplicate them here for building TKOpenGl with PCH.
330   add_definitions("-DGL_GLEXT_LEGACY")
331   add_definitions("-DGLX_GLXEXT_LEGACY")
332
333   # workaround for old gcc
334   if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
335     add_definitions("-D__STDC_CONSTANT_MACROS")
336     add_definitions("-D__STDC_FORMAT_MACROS")
337   endif()
338
339   # unity builds are not used since they do not add speed but cause conflicts
340   # in TKV3d
341   set_target_properties(${PROJECT_NAME} PROPERTIES COTIRE_ADD_UNITY_BUILD FALSE)
342
343   cotire(${PROJECT_NAME})
344 endif()