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