0027514: Configuration, CMake - relative paths are not correctly handled for INSTALL_DIR
[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 "${CURRENT_CSF}" STREQUAL "")
223           # prepare a list from a string with whitespaces
224           separate_arguments (CURRENT_CSF)
225           list (APPEND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT ${CURRENT_CSF})
226         endif()
227       endif()
228     endif()
229   endif()
230 endforeach()
231
232 if (APPLE)
233   list (FIND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT X11 IS_X11_FOUND)
234   if (NOT ${IS_X11_FOUND} EQUAL -1)
235     find_package (X11 COMPONENTS X11 Xext Xmu Xi)
236     if (NOT X11_FOUND)
237       message (STATUS "Warning: X11 is not found. It's required to install The XQuartz project: http://www.xquartz.org")
238     endif()
239   endif()
240 endif()
241
242 # Update list of used VTK libraries if OpenGL2 Rendering BackEnd is used.
243 # Add VTK_OPENGL2_BACKEND definition.
244 if("${VTK_RENDERING_BACKEND}" STREQUAL "OpenGL2")
245   add_definitions(-DVTK_OPENGL2_BACKEND)
246   foreach (VTK_EXCLUDE_LIBRARY vtkRenderingOpenGL vtkRenderingFreeTypeOpenGL)
247     list (FIND USED_TOOLKITS_BY_CURRENT_PROJECT "${VTK_EXCLUDE_LIBRARY}" IS_VTK_OPENGL_FOUND)
248     if (NOT ${IS_VTK_OPENGL_FOUND} EQUAL -1)
249       list (REMOVE_ITEM USED_TOOLKITS_BY_CURRENT_PROJECT ${VTK_EXCLUDE_LIBRARY})
250       if (${VTK_EXCLUDE_LIBRARY} STREQUAL vtkRenderingOpenGL)
251         list (APPEND USED_TOOLKITS_BY_CURRENT_PROJECT vtkRenderingOpenGL2)
252       endif()
253     endif()
254   endforeach()
255 endif()
256
257 if (BUILD_SHARED_LIBS)
258   target_link_libraries (${PROJECT_NAME} ${USED_TOOLKITS_BY_CURRENT_PROJECT} ${USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT})
259 endif()