0027176: Configuration, CMake - INSTALL_DIR is a common prefix for all other install...
[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 # Get all used packages from toolkit
13 foreach (OCCT_PACKAGE ${USED_PACKAGES})
14   
15   # TKService contains platform-dependent packages: Xw and WNT
16   if ((WIN32 AND "${OCCT_PACKAGE}" STREQUAL "Xw") OR (NOT WIN32 AND "${OCCT_PACKAGE}" STREQUAL "WNT"))
17     # do nothing
18   else()
19
20     if (WIN32)
21       list (APPEND PRECOMPILED_DEFS "-D__${OCCT_PACKAGE}_DLL")
22     endif()
23
24     set (SOURCE_FILES)
25     set (HEADER_FILES)
26
27     # Generate Flex and Bison files
28     if (${BUILD_YACCLEX})
29
30       # flex files
31       OCCT_ORIGIN_AND_PATCHED_FILES ("src/${OCCT_PACKAGE}" "*[.]lex" SOURCE_FILES_FLEX)
32       list (LENGTH SOURCE_FILES_FLEX SOURCE_FILES_FLEX_LEN)
33
34       # bison files
35       OCCT_ORIGIN_AND_PATCHED_FILES ("src/${OCCT_PACKAGE}" "*[.]yacc" SOURCE_FILES_BISON)
36       list (LENGTH SOURCE_FILES_BISON SOURCE_FILES_BISON_LEN)
37
38       if (${SOURCE_FILES_FLEX_LEN} EQUAL ${SOURCE_FILES_BISON_LEN} AND NOT ${SOURCE_FILES_FLEX_LEN} EQUAL 0)
39       
40         list (SORT SOURCE_FILES_FLEX)
41         list (SORT SOURCE_FILES_BISON)
42
43         math (EXPR SOURCE_FILES_FLEX_LEN "${SOURCE_FILES_FLEX_LEN} - 1")
44         foreach (FLEX_FILE_INDEX RANGE ${SOURCE_FILES_FLEX_LEN})
45
46           list (GET SOURCE_FILES_FLEX ${FLEX_FILE_INDEX} CURRENT_FLEX_FILE)
47           get_filename_component (CURRENT_FLEX_FILE_NAME ${CURRENT_FLEX_FILE} NAME_WE)
48
49           list (GET SOURCE_FILES_BISON ${FLEX_FILE_INDEX} CURRENT_BISON_FILE)
50           get_filename_component (CURRENT_BISON_FILE_NAME ${CURRENT_BISON_FILE} NAME_WE)
51           
52           string (COMPARE EQUAL ${CURRENT_FLEX_FILE_NAME} ${CURRENT_BISON_FILE_NAME} ARE_FILES_EQUAL)
53
54           if (EXISTS "${CURRENT_FLEX_FILE}" AND EXISTS "${CURRENT_BISON_FILE}" AND ${ARE_FILES_EQUAL})
55             set (BISON_OUTPUT_FILE ${CURRENT_BISON_FILE_NAME}.tab.c)
56             set (FLEX_OUTPUT_FILE lex.${CURRENT_FLEX_FILE_NAME}.c)
57             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}")
58             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}")
59             ADD_FLEX_BISON_DEPENDENCY (Scanner_${CURRENT_FLEX_FILE_NAME} Parser_${CURRENT_BISON_FILE_NAME})
60            
61             list (APPEND SOURCE_FILES ${BISON_OUTPUT_FILE} ${FLEX_OUTPUT_FILE})
62           endif()
63         endforeach()
64       endif()
65     endif()
66
67     # header files
68     if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES")
69       file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" HEADER_FILES_M   REGEX ".+[.]h")
70       file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" HEADER_FILES_LXX REGEX ".+[.]lxx")
71       file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" HEADER_FILES_GXX REGEX ".+[.]gxx")
72
73       file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" SOURCE_FILES_C REGEX ".+[.]c")
74       if(APPLE)
75         file (STRINGS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/FILES" SOURCE_FILES_M REGEX ".+[.]mm")
76       endif()
77     else()
78       file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/FILES"     HEADER_FILES_M   REGEX ".+[.]h")
79       file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/FILES"     HEADER_FILES_LXX REGEX ".+[.]lxx")
80       file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/FILES"     HEADER_FILES_GXX REGEX ".+[.]gxx")
81
82       file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/FILES"     SOURCE_FILES_C REGEX ".+[.]c")
83       if(APPLE)
84         file (STRINGS "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/FILES"   SOURCE_FILES_M REGEX ".+[.]mm")
85       endif()
86     endif()
87     
88     list (APPEND HEADER_FILES ${HEADER_FILES_M} ${HEADER_FILES_LXX} ${SOURCE_FILES_GXX})
89     list (APPEND SOURCE_FILES ${SOURCE_FILES_C})
90     if(APPLE)
91       list (APPEND SOURCE_FILES ${SOURCE_FILES_M})
92     endif()
93
94     foreach(HEADER_FILE ${HEADER_FILES})
95       if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
96         message (STATUS "Info: consider patched file: ${BUILD_PATCH}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
97         list (APPEND USED_INCFILES "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
98         SOURCE_GROUP ("Header Files\\${OCCT_PACKAGE}" FILES "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
99       else()
100         list (APPEND USED_INCFILES "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
101         SOURCE_GROUP ("Header Files\\${OCCT_PACKAGE}" FILES "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/${HEADER_FILE}")
102       endif()
103     endforeach()
104
105     foreach(SOURCE_FILE ${SOURCE_FILES})
106       if (BUILD_PATCH AND EXISTS "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
107         message (STATUS "Info: consider patched file: ${BUILD_PATCH}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
108         list (APPEND USED_SRCFILES "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
109         SOURCE_GROUP ("Source Files\\${OCCT_PACKAGE}" FILES "${BUILD_PATCH}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
110       else()
111         list (APPEND USED_SRCFILES "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
112         SOURCE_GROUP ("Source Files\\${OCCT_PACKAGE}" FILES "${CMAKE_SOURCE_DIR}/src/${OCCT_PACKAGE}/${SOURCE_FILE}")
113       endif()
114     endforeach()
115   endif()
116 endforeach()
117 string (REGEX REPLACE ";" " " PRECOMPILED_DEFS "${PRECOMPILED_DEFS}")
118
119 set (USED_RCFILE "")
120 if (MSVC)
121   set (USED_RCFILE "${CMAKE_BINARY_DIR}/resources/${PROJECT_NAME}.rc")
122
123   if (APPLY_OCCT_PATCH_DIR AND EXISTS "${APPLY_OCCT_PATCH_DIR}/adm/templates/occt_toolkit.rc.in")
124     configure_file("${APPLY_OCCT_PATCH_DIR}/adm/templates/occt_toolkit.rc.in" "${USED_RCFILE}" @ONLY)
125   else()
126     configure_file("${CMAKE_SOURCE_DIR}/adm/templates/occt_toolkit.rc.in" "${USED_RCFILE}" @ONLY)
127   endif()
128 endif()
129
130 if ("${PROJECT_NAME}" STREQUAL "DRAWEXE")
131   add_executable (${PROJECT_NAME} ${USED_SRCFILES} ${USED_INCFILES} ${USED_RCFILE})
132
133   if (SINGLE_GENERATOR)
134     install (TARGETS ${PROJECT_NAME} DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}")
135   else()
136     install (TARGETS ${PROJECT_NAME}
137              CONFIGURATIONS Release
138              DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}")
139     install (TARGETS ${PROJECT_NAME}
140              CONFIGURATIONS RelWithDebInfo
141              DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}i")
142     install (TARGETS ${PROJECT_NAME}
143              CONFIGURATIONS Debug
144              DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}d")
145   endif()
146 else()
147   add_library (${PROJECT_NAME} ${USED_SRCFILES} ${USED_INCFILES} ${USED_RCFILE})
148
149   if (SINGLE_GENERATOR)
150     install (TARGETS ${PROJECT_NAME}
151              RUNTIME DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}"
152              ARCHIVE DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}"
153              LIBRARY DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}")
154   else()
155     install (TARGETS ${PROJECT_NAME}
156              CONFIGURATIONS Release
157              RUNTIME DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}"
158              ARCHIVE DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}"
159              LIBRARY DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}")
160     install (TARGETS ${PROJECT_NAME}
161              CONFIGURATIONS RelWithDebInfo
162              RUNTIME DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}i"
163              ARCHIVE DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}i"
164              LIBRARY DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}i")
165     install (TARGETS ${PROJECT_NAME}
166              CONFIGURATIONS Debug
167              RUNTIME DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}d"
168              ARCHIVE DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}d"
169              LIBRARY DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}d")
170
171     if (MSVC)
172       install (FILES  ${CMAKE_BINARY_DIR}/${OS_WITH_BIT}/${COMPILER}/bind/${PROJECT_NAME}.pdb
173                CONFIGURATIONS Debug
174                DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}d")
175     endif()
176   endif()
177 endif()
178
179 set (CURRENT_MODULE)
180 foreach (OCCT_MODULE ${OCCT_MODULES})
181   list (FIND ${OCCT_MODULE}_TOOLKITS ${PROJECT_NAME} CURRENT_PROJECT_IS_BUILT)
182   if (NOT ${CURRENT_PROJECT_IS_BUILT} EQUAL -1)
183     set (CURRENT_MODULE ${OCCT_MODULE})
184   endif()
185 endforeach()
186
187 if (CURRENT_MODULE)
188   set_target_properties (${PROJECT_NAME} PROPERTIES FOLDER "Modules/${CURRENT_MODULE}")
189 endif()
190
191 get_property (OCC_VERSION_MAJOR GLOBAL PROPERTY OCC_VERSION_MAJOR)
192 get_property (OCC_VERSION_MINOR GLOBAL PROPERTY OCC_VERSION_MINOR)
193 get_property (OCC_VERSION_MAINTENANCE GLOBAL PROPERTY OCC_VERSION_MAINTENANCE)
194
195 if (ANDROID)
196   # do not append version to the filename
197   set_target_properties (${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${PRECOMPILED_DEFS}")
198 else()
199   set_target_properties (${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${PRECOMPILED_DEFS}"
200                                                     SOVERSION     "${OCC_VERSION_MAJOR}"
201                                                     VERSION       "${OCC_VERSION_MAJOR}.${OCC_VERSION_MINOR}.${OCC_VERSION_MAINTENANCE}")
202 endif()
203
204 set (USED_TOOLKITS_BY_CURRENT_PROJECT)
205 set (USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT)
206
207 # parce EXTERNLIB file
208 FILE_TO_LIST ("src/${PROJECT_NAME}/EXTERNLIB" USED_EXTERNLIB_AND_TOOLKITS)
209 foreach (USED_ITEM ${USED_EXTERNLIB_AND_TOOLKITS})
210   string (REGEX MATCH "^ *#" COMMENT_FOUND ${USED_ITEM})
211   if (NOT COMMENT_FOUND)
212     string (REGEX MATCH "^TK" TK_FOUND ${USED_ITEM})
213     string (REGEX MATCH "^vtk" VTK_FOUND ${USED_ITEM})
214     
215     if (NOT "${TK_FOUND}" STREQUAL "" OR NOT "${VTK_FOUND}" STREQUAL "")
216       list (APPEND USED_TOOLKITS_BY_CURRENT_PROJECT ${USED_ITEM})
217     else()
218       string (REGEX MATCH "^CSF_" CSF_FOUND ${USED_ITEM})
219       if ("${CSF_FOUND}" STREQUAL "")
220         message (STATUS "Info: ${USED_ITEM} from ${PROJECT_NAME} skipped due to it is empty")
221       else() # get CSF_ value
222         set (CURRENT_CSF ${${USED_ITEM}})
223         if (NOT "${CURRENT_CSF}" STREQUAL "")
224           # prepare a list from a string with whitespaces
225           separate_arguments (CURRENT_CSF)
226           list (APPEND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT ${CURRENT_CSF})
227         endif()
228       endif()
229     endif()
230   endif()
231 endforeach()
232
233 if (APPLE)
234   list (FIND USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT X11 IS_X11_FOUND)
235   if (NOT ${IS_X11_FOUND} EQUAL -1)
236     find_package (X11 COMPONENTS X11 Xext Xmu Xi)
237     if (NOT X11_FOUND)
238       message (STATUS "Warning: X11 is not found. It's required to install The XQuartz project: http://www.xquartz.org")
239     endif()
240   endif()
241 endif()
242
243 target_link_libraries (${PROJECT_NAME} ${USED_TOOLKITS_BY_CURRENT_PROJECT} ${USED_EXTERNAL_LIBS_BY_CURRENT_PROJECT})