b62d8052c59432dbd5d1475d598d1eba13ab7361
[occt.git] / adm / cmake / occt_toolkit_tool.cmake
1 # script for each OCCT tool toolkit
2
3 # Qt dependencies
4 OCCT_INCLUDE_CMAKE_FILE (adm/cmake/qt_macro)
5
6 FIND_QT_PACKAGE(PROJECT_LIBRARIES_DEBUG PROJECT_LIBRARIES_RELEASE PROJECT_INCLUDES)
7 include_directories("${PROJECT_INCLUDES}"
8                     "${CMAKE_BINARY_DIR}/${INSTALL_DIR_INCLUDE}/tools")
9
10 FILE_TO_LIST ("tools/${PROJECT_NAME}/PACKAGES" USED_PACKAGES)
11 # parce PACKAGES file
12
13 if ("${PROJECT_NAME}" STREQUAL TInspectorEXE)
14   set (USED_PACKAGES ${PROJECT_NAME})
15   set (PACKAGE_PREFIX_DIR "samples/tools")
16   include_directories("${PROJECT_INCLUDES}"
17                       "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${PROJECT_NAME}")
18 else()
19   FILE_TO_LIST ("tools/${PROJECT_NAME}/PACKAGES" USED_PACKAGES)
20   set (PACKAGE_PREFIX_DIR "tools")
21 endif()
22
23
24 SET (RCC_FILES)
25 foreach (OCCT_PACKAGE ${USED_PACKAGES})
26   file (STRINGS "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/FILES" SOURCE_FILES_C REGEX ".+[.]c")
27
28   #message("Sources are ${SOURCE_FILES_C}")
29   FIND_AND_WRAP_MOC_FILES("${SOURCE_FILES_C}")
30
31   #message("Dir is ${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/${OCCT_PACKAGE}.qrc")
32   FIND_AND_WRAP_RESOURCE_FILE("${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/${OCCT_PACKAGE}.qrc" RCC_FILES)
33 endforeach()
34 #message("RCC_FILES for ${PROJECT_NAME} are ${RCC_FILES}")
35
36 set (PRECOMPILED_DEFS)
37
38 if (NOT BUILD_SHARED_LIBS)
39   list (APPEND PRECOMPILED_DEFS "-DOCCT_NO_PLUGINS")
40   if (WIN32)
41     list (APPEND PRECOMPILED_DEFS "-DHAVE_NO_DLL")
42   endif()
43 endif()
44
45 # Get all used packages from toolkit
46 foreach (OCCT_PACKAGE ${USED_PACKAGES})
47
48   ADD_DEFINITIONS(-D${OCCT_PACKAGE}_EXPORTS)
49   
50   # TKService contains platform-dependent packages: Xw and WNT
51   if ((WIN32 AND "${OCCT_PACKAGE}" STREQUAL "Xw") OR (NOT WIN32 AND "${OCCT_PACKAGE}" STREQUAL "WNT"))
52     # do nothing
53   else()
54
55     if (WIN32)
56       list (APPEND PRECOMPILED_DEFS "-D__${OCCT_PACKAGE}_DLL")
57     endif()
58
59     set (SOURCE_FILES)
60     set (HEADER_FILES)
61
62     # Generate Flex and Bison files
63     if (${BUILD_YACCLEX})
64
65       # flex files
66       OCCT_ORIGIN_AND_PATCHED_FILES ("tools/${OCCT_PACKAGE}" "*[.]lex" SOURCE_FILES_FLEX)
67       list (LENGTH SOURCE_FILES_FLEX SOURCE_FILES_FLEX_LEN)
68
69       # bison files
70       OCCT_ORIGIN_AND_PATCHED_FILES ("tools/${OCCT_PACKAGE}" "*[.]yacc" SOURCE_FILES_BISON)
71       list (LENGTH SOURCE_FILES_BISON SOURCE_FILES_BISON_LEN)
72
73       if (${SOURCE_FILES_FLEX_LEN} EQUAL ${SOURCE_FILES_BISON_LEN} AND NOT ${SOURCE_FILES_FLEX_LEN} EQUAL 0)
74       
75         list (SORT SOURCE_FILES_FLEX)
76         list (SORT SOURCE_FILES_BISON)
77
78         math (EXPR SOURCE_FILES_FLEX_LEN "${SOURCE_FILES_FLEX_LEN} - 1")
79         foreach (FLEX_FILE_INDEX RANGE ${SOURCE_FILES_FLEX_LEN})
80
81           list (GET SOURCE_FILES_FLEX ${FLEX_FILE_INDEX} CURRENT_FLEX_FILE)
82           get_filename_component (CURRENT_FLEX_FILE_NAME ${CURRENT_FLEX_FILE} NAME_WE)
83
84           list (GET SOURCE_FILES_BISON ${FLEX_FILE_INDEX} CURRENT_BISON_FILE)
85           get_filename_component (CURRENT_BISON_FILE_NAME ${CURRENT_BISON_FILE} NAME_WE)
86           
87           string (COMPARE EQUAL ${CURRENT_FLEX_FILE_NAME} ${CURRENT_BISON_FILE_NAME} ARE_FILES_EQUAL)
88
89           if (EXISTS "${CURRENT_FLEX_FILE}" AND EXISTS "${CURRENT_BISON_FILE}" AND ${ARE_FILES_EQUAL})
90             set (BISON_OUTPUT_FILE ${CURRENT_BISON_FILE_NAME}.tab.c)
91             set (FLEX_OUTPUT_FILE lex.${CURRENT_FLEX_FILE_NAME}.c)
92             BISON_TARGET (Parser_${CURRENT_BISON_FILE_NAME} ${CURRENT_BISON_FILE} ${CMAKE_SOURCE_DIR}/tools/${OCCT_PACKAGE}/${BISON_OUTPUT_FILE} COMPILE_FLAGS "-p ${CURRENT_BISON_FILE_NAME}")
93             FLEX_TARGET  (Scanner_${CURRENT_FLEX_FILE_NAME} ${CURRENT_FLEX_FILE} ${CMAKE_SOURCE_DIR}/tools/${OCCT_PACKAGE}/${FLEX_OUTPUT_FILE} COMPILE_FLAGS "-P${CURRENT_FLEX_FILE_NAME}")
94             ADD_FLEX_BISON_DEPENDENCY (Scanner_${CURRENT_FLEX_FILE_NAME} Parser_${CURRENT_BISON_FILE_NAME})
95            
96             list (APPEND SOURCE_FILES ${BISON_OUTPUT_FILE} ${FLEX_OUTPUT_FILE})
97           endif()
98         endforeach()
99       endif()
100     endif()
101
102     # header files
103     if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/FILES")
104       file (STRINGS "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/FILES" HEADER_FILES_M   REGEX ".+[.]h")
105       file (STRINGS "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/FILES" HEADER_FILES_LXX REGEX ".+[.]lxx")
106       file (STRINGS "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/FILES" HEADER_FILES_GXX REGEX ".+[.]gxx")
107
108       file (STRINGS "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/FILES" SOURCE_FILES_C REGEX ".+[.]c")
109       if(APPLE)
110         file (STRINGS "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/FILES" SOURCE_FILES_M REGEX ".+[.]mm")
111       endif()
112     else()
113       file (STRINGS "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/FILES"     HEADER_FILES_M   REGEX ".+[.]h")
114       file (STRINGS "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/FILES"     HEADER_FILES_LXX REGEX ".+[.]lxx")
115       file (STRINGS "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/FILES"     HEADER_FILES_GXX REGEX ".+[.]gxx")
116
117       file (STRINGS "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/FILES"     SOURCE_FILES_C REGEX ".+[.]c")
118       if(APPLE)
119         file (STRINGS "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/FILES"   SOURCE_FILES_M REGEX ".+[.]mm")
120       endif()
121     endif()
122
123
124     
125     list (APPEND HEADER_FILES ${HEADER_FILES_M} ${HEADER_FILES_LXX} ${SOURCE_FILES_GXX})
126     list (APPEND SOURCE_FILES ${SOURCE_FILES_C})
127     if(APPLE)
128       list (APPEND SOURCE_FILES ${SOURCE_FILES_M})
129     endif()
130
131     foreach(HEADER_FILE ${HEADER_FILES})
132       if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/${HEADER_FILE}")
133         message (STATUS "Info: consider patched file: ${BUILD_PATCH}/tools/${OCCT_PACKAGE}/${HEADER_FILE}")
134         list (APPEND USED_INCFILES "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/${HEADER_FILE}")
135         SOURCE_GROUP ("Header Files\\${OCCT_PACKAGE}" FILES "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/${HEADER_FILE}")
136       else()
137         list (APPEND USED_INCFILES "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/${HEADER_FILE}")
138         SOURCE_GROUP ("Header Files\\${OCCT_PACKAGE}" FILES "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/${HEADER_FILE}")
139       endif()
140     endforeach()
141
142
143     foreach(SOURCE_FILE ${SOURCE_FILES})
144       if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/${SOURCE_FILE}")
145         message (STATUS "Info: consider patched file: ${BUILD_PATCH}/tools/${OCCT_PACKAGE}/${SOURCE_FILE}")
146         list (APPEND USED_SRCFILES "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/${SOURCE_FILE}")
147         SOURCE_GROUP ("Source Files\\${OCCT_PACKAGE}" FILES "${BUILD_PATCH}/tools/${OCCT_PACKAGE}/${SOURCE_FILE}")
148       else()
149         list (APPEND USED_SRCFILES "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/${SOURCE_FILE}")
150         SOURCE_GROUP ("Source Files\\${OCCT_PACKAGE}" FILES "${CMAKE_SOURCE_DIR}/${PACKAGE_PREFIX_DIR}/${OCCT_PACKAGE}/${SOURCE_FILE}")
151       endif()
152     endforeach()
153   endif()
154 endforeach()
155 string (REGEX REPLACE ";" " " PRECOMPILED_DEFS "${PRECOMPILED_DEFS}")
156
157 set (USED_RCFILE "")
158 if (MSVC)
159   set (USED_RCFILE "${CMAKE_BINARY_DIR}/resources/${PROJECT_NAME}.rc")
160
161   if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/adm/templates/occt_toolkit.rc.in")
162     configure_file("${APPLY_OCCT_PATCH_DIR}/adm/templates/occt_toolkit.rc.in" "${USED_RCFILE}" @ONLY)
163   else()
164     configure_file("${CMAKE_SOURCE_DIR}/adm/templates/occt_toolkit.rc.in" "${USED_RCFILE}" @ONLY)
165   endif()
166 endif()
167
168 set (CURRENT_MODULE)
169 foreach (OCCT_MODULE ${OCCT_TOOLS})
170   list (FIND ${OCCT_MODULE}_TOOL_TOOLKITS ${PROJECT_NAME} CURRENT_PROJECT_IS_BUILT)
171   if (NOT ${CURRENT_PROJECT_IS_BUILT} EQUAL -1)
172     set (CURRENT_MODULE ${OCCT_MODULE})
173   endif()
174 endforeach()
175
176 if (NOT SINGLE_GENERATOR)
177   OCCT_INSERT_CODE_FOR_TARGET ()
178 endif()
179
180 if ("${PROJECT_NAME}" STREQUAL TInspectorEXE)
181   add_executable (${PROJECT_NAME} ${USED_SRCFILES} ${USED_INCFILES} ${USED_RCFILE} ${RCC_FILES})
182
183   install (TARGETS ${PROJECT_NAME}
184            DESTINATION "${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}")
185 else()
186   add_library (${PROJECT_NAME} ${USED_SRCFILES} ${USED_INCFILES} ${USED_RCFILE} ${RCC_FILES})
187
188   if (MSVC)
189     install (FILES  ${CMAKE_BINARY_DIR}/${OS_WITH_BIT}/${COMPILER}/bin\${OCCT_INSTALL_BIN_LETTER}/${PROJECT_NAME}.pdb
190              CONFIGURATIONS Debug RelWithDebInfo
191              DESTINATION "${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}")
192   endif()
193
194   if (BUILD_SHARED_LIBS AND NOT "${BUILD_SHARED_LIBRARY_NAME_POSTFIX}" STREQUAL "")
195     set (CMAKE_SHARED_LIBRARY_SUFFIX_DEFAULT ${CMAKE_SHARED_LIBRARY_SUFFIX})
196     set (CMAKE_SHARED_LIBRARY_SUFFIX "${BUILD_SHARED_LIBRARY_NAME_POSTFIX}${CMAKE_SHARED_LIBRARY_SUFFIX}")
197   endif()
198
199   install (TARGETS ${PROJECT_NAME}
200            EXPORT OpenCASCADE${CURRENT_MODULE}Targets
201            RUNTIME DESTINATION "${INSTALL_DIR_BIN}\${OCCT_INSTALL_BIN_LETTER}"
202            ARCHIVE DESTINATION "${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}"
203            LIBRARY DESTINATION "${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}")
204
205
206   if (NOT WIN32)
207     if (BUILD_SHARED_LIBS AND NOT "${BUILD_SHARED_LIBRARY_NAME_POSTFIX}" STREQUAL "")
208       set (LINK_NAME    "${INSTALL_DIR}/${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}/lib${PROJECT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX_DEFAULT}/tools")
209       set (LIBRARY_NAME "${INSTALL_DIR}/${INSTALL_DIR_LIB}\${OCCT_INSTALL_BIN_LETTER}/lib${PROJECT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}/tools")
210       OCCT_CREATE_SYMLINK_TO_FILE (${LIBRARY_NAME} ${LINK_NAME})
211     endif()
212   endif()
213 endif()
214
215 if (CURRENT_MODULE)
216   set_target_properties (${PROJECT_NAME} PROPERTIES FOLDER "Tools/${CURRENT_MODULE}")
217   set_target_properties (${PROJECT_NAME} PROPERTIES MODULE "${CURRENT_MODULE}")
218   if (APPLE)
219     if (NOT "${INSTALL_NAME_DIR}" STREQUAL "")
220       set_target_properties (${PROJECT_NAME} PROPERTIES BUILD_WITH_INSTALL_RPATH 1 INSTALL_NAME_DIR "${INSTALL_NAME_DIR}")
221     endif()
222   endif()
223 endif()
224
225 get_property (OCC_VERSION_MAJOR GLOBAL PROPERTY OCC_VERSION_MAJOR)
226 get_property (OCC_VERSION_MINOR GLOBAL PROPERTY OCC_VERSION_MINOR)
227 get_property (OCC_VERSION_MAINTENANCE GLOBAL PROPERTY OCC_VERSION_MAINTENANCE)
228
229 if (ANDROID)
230   # do not append version to the filename
231   set_target_properties (${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${PRECOMPILED_DEFS}")
232 else()
233   set_target_properties (${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${PRECOMPILED_DEFS}"
234                                                     SOVERSION     "${OCC_VERSION_MAJOR}"
235                                                     VERSION       "${OCC_VERSION_MAJOR}.${OCC_VERSION_MINOR}.${OCC_VERSION_MAINTENANCE}")
236 endif()
237
238 set (USED_TOOLKITS_BY_CURRENT_PROJECT)
239 set (USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT)
240
241 # parce EXTERNLIB file
242 FILE_TO_LIST ("${PACKAGE_PREFIX_DIR}/${PROJECT_NAME}/EXTERNLIB" USED_EXTERNLIB_AND_TOOLKITS)
243 foreach (USED_ITEM ${USED_EXTERNLIB_AND_TOOLKITS})
244   string (REGEX MATCH "^ *#" COMMENT_FOUND ${USED_ITEM})
245   if (NOT COMMENT_FOUND)
246     string (REGEX MATCH "^TK" TK_FOUND ${USED_ITEM})
247     string (REGEX MATCH "^vtk" VTK_FOUND ${USED_ITEM})
248     
249     if (NOT "${TK_FOUND}" STREQUAL "" OR NOT "${VTK_FOUND}" STREQUAL "")
250       list (APPEND USED_TOOLKITS_BY_CURRENT_PROJECT ${USED_ITEM})
251       if (NOT "${VTK_FOUND}" STREQUAL "" AND BUILD_SHARED_LIBS AND INSTALL_VTK AND COMMAND OCCT_INSTALL_VTK)
252         OCCT_INSTALL_VTK(${USED_ITEM})
253       endif()
254     else()
255       string (REGEX MATCH "^CSF_" CSF_FOUND ${USED_ITEM})
256       if ("${CSF_FOUND}" STREQUAL "")
257         message (STATUS "Info: ${USED_ITEM} from ${PROJECT_NAME} skipped due to it is empty")
258       else() # get CSF_ value
259         set (CURRENT_CSF ${${USED_ITEM}})
260         if (NOT "x${CURRENT_CSF}" STREQUAL "x")
261           # prepare a list from a string with whitespaces
262           separate_arguments (CURRENT_CSF)
263           list (APPEND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT ${CURRENT_CSF})
264         endif()
265       endif()
266     endif()
267   endif()
268 endforeach()
269
270 if (APPLE)
271   list (FIND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT X11 IS_X11_FOUND)
272   if (NOT ${IS_X11_FOUND} EQUAL -1)
273     find_package (X11 COMPONENTS X11 Xext Xmu Xi)
274     if (NOT X11_FOUND)
275       message (STATUS "Warning: X11 is not found. It's required to install The XQuartz project: http://www.xquartz.org")
276     endif()
277   endif()
278 endif()
279
280 # Update list of used VTK libraries if OpenGL2 Rendering BackEnd is used.
281 # Add VTK_OPENGL2_BACKEND definition.
282 if("${VTK_RENDERING_BACKEND}" STREQUAL "OpenGL2")
283   add_definitions(-DVTK_OPENGL2_BACKEND)
284   foreach (VTK_EXCLUDE_LIBRARY vtkRenderingOpenGL vtkRenderingFreeTypeOpenGL)
285     list (FIND USED_TOOLKITS_BY_CURRENT_PROJECT "${VTK_EXCLUDE_LIBRARY}" IS_VTK_OPENGL_FOUND)
286     if (NOT ${IS_VTK_OPENGL_FOUND} EQUAL -1)
287       list (REMOVE_ITEM USED_TOOLKITS_BY_CURRENT_PROJECT ${VTK_EXCLUDE_LIBRARY})
288       if (${VTK_EXCLUDE_LIBRARY} STREQUAL vtkRenderingOpenGL)
289         list (APPEND USED_TOOLKITS_BY_CURRENT_PROJECT vtkRenderingOpenGL2)
290       endif()
291     endif()
292   endforeach()
293 endif()
294
295 if (BUILD_SHARED_LIBS)
296   target_link_libraries (${PROJECT_NAME} ${USED_TOOLKITS_BY_CURRENT_PROJECT} ${USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT})
297 endif()
298
299 # Set Qt dependencies
300 target_link_libraries(${PROJECT_NAME} "${PROJECT_LIBRARIES}")
301
302 target_link_libraries(${PROJECT_NAME} debug "${PROJECT_LIBRARIES_DEBUG}")
303 target_link_libraries(${PROJECT_NAME} optimized "${PROJECT_LIBRARIES_RELEASE}")
304