0024816: Tool for upgrading OCCT and dependent code
[occt.git] / adm / cmake / occt_macros.cmake
CommitLineData
19286d7c 1#
2macro (OCCT_CHECK_AND_UNSET VARNAME)
3 if (DEFINED ${VARNAME})
4 unset (${VARNAME} CACHE)
5 endif()
6endmacro()
7
8macro (OCCT_CHECK_AND_UNSET_GROUP VARNAME)
9 OCCT_CHECK_AND_UNSET ("${VARNAME}_DIR")
10
11 OCCT_CHECK_AND_UNSET ("${VARNAME}_INCLUDE_DIR")
12
13 OCCT_CHECK_AND_UNSET ("${VARNAME}_LIBRARY")
14 OCCT_CHECK_AND_UNSET ("${VARNAME}_LIBRARY_DIR")
15
16 if (WIN32)
17 OCCT_CHECK_AND_UNSET ("${VARNAME}_DLL")
18 OCCT_CHECK_AND_UNSET ("${VARNAME}_DLL_DIR")
19 endif()
20endmacro()
21
19286d7c 22# COMPILER_BITNESS variable
23macro (OCCT_MAKE_COMPILER_BITNESS)
24 math (EXPR COMPILER_BITNESS "32 + 32*(${CMAKE_SIZEOF_VOID_P}/8)")
25endmacro()
26
27# OS_WITH_BIT
28macro (OCCT_MAKE_OS_WITH_BITNESS)
29
30 OCCT_MAKE_COMPILER_BITNESS()
31
32 if (WIN32)
33 set (OS_WITH_BIT "win${COMPILER_BITNESS}")
34 elseif(APPLE)
35 set (OS_WITH_BIT "mac${COMPILER_BITNESS}")
36 else()
37 set (OS_WITH_BIT "lin${COMPILER_BITNESS}")
38 endif()
39endmacro()
40
41# COMPILER variable
42macro (OCCT_MAKE_COMPILER_SHORT_NAME)
43 if (MSVC)
44 if (MSVC70)
45 set (COMPILER vc7)
46 elseif (MSVC80)
47 set (COMPILER vc8)
48 elseif (MSVC90)
49 set (COMPILER vc9)
50 elseif (MSVC10)
51 set (COMPILER vc10)
52 elseif (MSVC11)
53 set (COMPILER vc11)
54 elseif (MSVC12)
55 set (COMPILER vc12)
56 endif()
57 elseif (DEFINED CMAKE_COMPILER_IS_GNUCC)
58 set (COMPILER gcc)
59 elseif (DEFINED CMAKE_COMPILER_IS_GNUCXX)
60 set (COMPILER gxx)
61 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
62 set (COMPILER clang)
63 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
64 set (COMPILER icc)
65 else()
66 set (COMPILER ${CMAKE_GENERATOR})
67 string (REGEX REPLACE " " "" COMPILER ${COMPILER})
68 endif()
69endmacro()
70
71function (SUBDIRECTORY_NAMES MAIN_DIRECTORY RESULT)
72 file (GLOB SUB_ITEMS "${MAIN_DIRECTORY}/*")
73
74 foreach (ITEM ${SUB_ITEMS})
75 if (IS_DIRECTORY "${ITEM}")
76 get_filename_component (ITEM_NAME "${ITEM}" NAME)
77 list (APPEND LOCAL_RESULT "${ITEM_NAME}")
78 endif()
79 endforeach()
80 set (${RESULT} ${LOCAL_RESULT} PARENT_SCOPE)
81endfunction()
82
83function (FIND_PRODUCT_DIR ROOT_DIR PRODUCT_NAME RESULT)
84 OCCT_MAKE_COMPILER_SHORT_NAME()
85 OCCT_MAKE_COMPILER_BITNESS()
86
87 string (TOLOWER "${PRODUCT_NAME}" lower_PRODUCT_NAME)
88
89 list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*${COMPILER}.*${COMPILER_BITNESS}")
90 list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*[0-9.]+.*${COMPILER}.*${COMPILER_BITNESS}")
91 list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*[0-9.]+.*${COMPILER_BITNESS}")
92 list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*[0-9.]+")
93 list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}")
94
95 SUBDIRECTORY_NAMES ("${ROOT_DIR}" SUBDIR_NAME_LIST)
96
97 foreach (SEARCH_TEMPLATE ${SEARCH_TEMPLATES})
98 if (LOCAL_RESULT)
99 BREAK()
100 endif()
101
102 foreach (SUBDIR_NAME ${SUBDIR_NAME_LIST})
103 string (TOLOWER "${SUBDIR_NAME}" lower_SUBDIR_NAME)
104
105 string (REGEX MATCH "${SEARCH_TEMPLATE}" DUMMY_VAR "${lower_SUBDIR_NAME}")
106 if (DUMMY_VAR)
107 list (APPEND LOCAL_RESULT ${SUBDIR_NAME})
108 endif()
109 endforeach()
110 endforeach()
111
112 if (LOCAL_RESULT)
113 list (LENGTH "${LOCAL_RESULT}" LOC_LEN)
114 math (EXPR LAST_ELEMENT_INDEX "${LOC_LEN}-1")
115 list (GET LOCAL_RESULT ${LAST_ELEMENT_INDEX} DUMMY)
116 set (${RESULT} ${DUMMY} PARENT_SCOPE)
117 endif()
118endfunction()
119
120macro (OCCT_INSTALL_FILE_OR_DIR BEING_INSTALLED_OBJECT DESTINATION_PATH)
121 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/${BEING_INSTALLED_OBJECT}")
122 if (IS_DIRECTORY "${BUILD_PATCH_DIR}/${BEING_INSTALLED_OBJECT}")
123 # first of all, install original files
124 install (DIRECTORY "${CMAKE_SOURCE_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
125
126 # secondly, rewrite original files with patched ones
127 install (DIRECTORY "${BUILD_PATCH_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
128 else()
129 install (FILES "${BUILD_PATCH_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
130 endif()
131 else()
132 if (IS_DIRECTORY "${CMAKE_SOURCE_DIR}/${BEING_INSTALLED_OBJECT}")
133 install (DIRECTORY "${CMAKE_SOURCE_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
134 else()
135 install (FILES "${CMAKE_SOURCE_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
136 endif()
137 endif()
138endmacro()
139
140macro (OCCT_CONFIGURE_AND_INSTALL BEING_CONGIRUGED_FILE FINAL_NAME DESTINATION_PATH)
141 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/${BEING_CONGIRUGED_FILE}")
142 configure_file("${BUILD_PATCH_DIR}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
143 else()
144 configure_file("${CMAKE_SOURCE_DIR}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
145 endif()
146
147 install(FILES "${OCCT_BINARY_DIR}/${FINAL_NAME}" DESTINATION "${DESTINATION_PATH}")
148endmacro()
149
94829f84 150macro (COLLECT_AND_INSTALL_OCCT_HEADER_FILES ROOT_OCCT_DIR TEMPLATE_HEADER_PATH ROOT_TARGET_OCCT_DIR OCCT_USED_TOOLKITS)
151 set (OCCT_SOURCE_DIRS)
152
153 foreach (OCCT_USED_TOOLKIT ${OCCT_USED_TOOLKITS})
154 # append parent folder
155 list (APPEND OCCT_SOURCE_DIRS ${OCCT_USED_TOOLKIT})
156
157 # append all required package folders
158 set (OCCT_USED_TOOLKIT_DEPS)
159 if (EXISTS "${ROOT_OCCT_DIR}/src/${OCCT_USED_TOOLKIT}/PACKAGES")
160 file (STRINGS "${ROOT_OCCT_DIR}/src/${OCCT_USED_TOOLKIT}/PACKAGES" OCCT_USED_TOOLKIT_DEPS)
161 endif()
162
163 foreach (OCCT_USED_TOOLKIT_DEP ${OCCT_USED_TOOLKIT_DEPS})
164 list (APPEND OCCT_SOURCE_DIRS ${OCCT_USED_TOOLKIT_DEP})
165 endforeach()
166 endforeach()
167
168 foreach (OCCT_SOURCE_DIR ${OCCT_SOURCE_DIRS})
169 # get all header files from each src folder
170 file (GLOB OCCT_HEADER_FILES "${ROOT_OCCT_DIR}/src/${OCCT_SOURCE_DIR}/*.[hgl]xx" "${ROOT_OCCT_DIR}/src/${OCCT_SOURCE_DIR}/*.h")
171
172 install (FILES ${OCCT_HEADER_FILES} DESTINATION "${INSTALL_DIR}/inc")
173
174 # create new file including found header
175 foreach (OCCT_HEADER_FILE ${OCCT_HEADER_FILES})
176 get_filename_component (HEADER_FILE_NAME ${OCCT_HEADER_FILE} NAME)
177 configure_file ("${TEMPLATE_HEADER_PATH}" "${ROOT_TARGET_OCCT_DIR}/inc/${HEADER_FILE_NAME}" @ONLY)
178 endforeach()
179 endforeach()
180endmacro()
181
19286d7c 182macro (OCCT_COPY_FILE_OR_DIR BEING_COPIED_OBJECT DESTINATION_PATH)
183 # first of all, copy original files
184 file (COPY "${CMAKE_SOURCE_DIR}/${BEING_COPIED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
185
186 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/${BEING_COPIED_OBJECT}")
187 # secondly, rewrite original files with patched ones
188 file (COPY "${BUILD_PATCH_DIR}/${BEING_COPIED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
189 endif()
190endmacro()
191
192macro (OCCT_CONFIGURE BEING_CONGIRUGED_FILE FINAL_NAME)
193 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/${BEING_CONGIRUGED_FILE}")
194 configure_file("${BUILD_PATCH_DIR}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
195 else()
196 configure_file("${CMAKE_SOURCE_DIR}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
197 endif()
198endmacro()
199
200function (OCCT_IS_PRODUCT_REQUIRED CSF_VAR_NAME USE_PRODUCT)
201 set (${USE_PRODUCT} OFF PARENT_SCOPE)
202
203 if (NOT USED_TOOLKITS)
204 message(STATUS "Warning: the list of being used toolkits is empty")
205 else()
206 foreach (USED_TOOLKIT ${USED_TOOLKITS})
207 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/src/${USED_TOOLKIT}/EXTERNLIB")
208 file (READ "${BUILD_PATCH_DIR}/src/${USED_TOOLKIT}/EXTERNLIB" FILE_CONTENT)
209 elseif (EXISTS "${CMAKE_SOURCE_DIR}/src/${USED_TOOLKIT}/EXTERNLIB")
210 file (READ "${CMAKE_SOURCE_DIR}/src/${USED_TOOLKIT}/EXTERNLIB" FILE_CONTENT)
211 endif()
212
213 string (REGEX MATCH "${CSF_VAR_NAME}" DOES_FILE_CONTAIN "${FILE_CONTENT}")
214
215 if (DOES_FILE_CONTAIN)
216 set (${USE_PRODUCT} ON PARENT_SCOPE)
217 break()
218 endif()
219 endforeach()
220 endif()
221endfunction()
222
223function (FILE_TO_LIST FILE_NAME FILE_CONTENT)
224 set (LOCAL_FILE_CONTENT)
225 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/${FILE_NAME}")
226 file (STRINGS "${BUILD_PATCH_DIR}/${FILE_NAME}" LOCAL_FILE_CONTENT)
227 elseif (EXISTS "${CMAKE_SOURCE_DIR}/${FILE_NAME}")
228 file (STRINGS "${CMAKE_SOURCE_DIR}/${FILE_NAME}" LOCAL_FILE_CONTENT)
229 endif()
230
231 set (${FILE_CONTENT} ${LOCAL_FILE_CONTENT} PARENT_SCOPE)
232endfunction()
233
234# Function to determine if TOOLKIT is OCCT toolkit
235function (IS_OCCT_TOOLKIT TOOLKIT_NAME MODULES IS_TOOLKIT_FOUND)
236 set (${IS_TOOLKIT_FOUND} OFF PARENT_SCOPE)
237 foreach (MODULE ${${MODULES}})
238 set (TOOLKITS ${${MODULE}_TOOLKITS})
239 list (FIND TOOLKITS ${TOOLKIT_NAME} FOUND)
240
241 if (NOT ${FOUND} EQUAL -1)
242 set (${IS_TOOLKIT_FOUND} ON PARENT_SCOPE)
243 endif()
244 endforeach(MODULE)
245endfunction()
246
247# TOOLKIT_DEPS is defined with dependencies from file src/TOOLKIT_NAME/EXTERNLIB.
248# CSF_ variables are ignored
249function (OCCT_TOOLKIT_DEP TOOLKIT_NAME TOOLKIT_DEPS)
250 FILE_TO_LIST ("src/${TOOLKIT_NAME}/EXTERNLIB" FILE_CONTENT)
251
252 #list (APPEND LOCAL_TOOLKIT_DEPS ${TOOLKIT_NAME})
253 set (LOCAL_TOOLKIT_DEPS)
254 foreach (FILE_CONTENT_LINE ${FILE_CONTENT})
255 string (REGEX MATCH "^TK" TK_FOUND ${FILE_CONTENT_LINE})
256 if ("${FILE_CONTENT_LINE}" STREQUAL "DRAWEXE" OR NOT "${TK_FOUND}" STREQUAL "")
257 list (APPEND LOCAL_TOOLKIT_DEPS ${FILE_CONTENT_LINE})
258 endif()
259 endforeach()
260
261 set (${TOOLKIT_DEPS} ${LOCAL_TOOLKIT_DEPS} PARENT_SCOPE)
262endfunction()
263
264# TOOLKIT_FULL_DEPS is defined with complete dependencies (all levels)
265function (OCCT_TOOLKIT_FULL_DEP TOOLKIT_NAME TOOLKIT_FULL_DEPS)
266 # first level dependencies are stored in LOCAL_TOOLKIT_FULL_DEPS
267 OCCT_TOOLKIT_DEP (${TOOLKIT_NAME} LOCAL_TOOLKIT_FULL_DEPS)
268
269 list (LENGTH LOCAL_TOOLKIT_FULL_DEPS LIST_LENGTH)
270 set (LIST_INDEX 0)
271 while (LIST_INDEX LESS LIST_LENGTH)
272 list (GET LOCAL_TOOLKIT_FULL_DEPS ${LIST_INDEX} CURRENT_TOOLKIT)
273 OCCT_TOOLKIT_DEP (${CURRENT_TOOLKIT} CURRENT_TOOLKIT_DEPS)
274
275 # append toolkits are not contained
276 foreach (CURRENT_TOOLKIT_DEP ${CURRENT_TOOLKIT_DEPS})
277 set (CURRENT_TOOLKIT_DEP_FOUND OFF)
278 foreach (LOCAL_TOOLKIT_FULL_DEP ${LOCAL_TOOLKIT_FULL_DEPS})
279 if ("${CURRENT_TOOLKIT_DEP}" STREQUAL "${LOCAL_TOOLKIT_FULL_DEP}")
280 set (CURRENT_TOOLKIT_DEP_FOUND ON)
281 break()
282 endif()
283 endforeach()
284 if ("${CURRENT_TOOLKIT_DEP_FOUND}" STREQUAL "OFF")
285 list (APPEND LOCAL_TOOLKIT_FULL_DEPS ${CURRENT_TOOLKIT_DEP})
286 endif()
287 endforeach()
288
289 # increment the list index
290 MATH(EXPR LIST_INDEX "${LIST_INDEX}+1")
291
292 # calculate new length
293 list (LENGTH LOCAL_TOOLKIT_FULL_DEPS LIST_LENGTH)
294 endwhile()
295
296 set (${TOOLKIT_FULL_DEPS} ${LOCAL_TOOLKIT_FULL_DEPS} PARENT_SCOPE)
297endfunction()
298
299# Function to get list of modules and toolkits from file adm/MODULES.
300# Creates list <$MODULE_LIST> to store list of MODULES and
301# <NAME_OF_MODULE>_TOOLKITS foreach module to store its toolkits.
302function (OCCT_MODULES_AND_TOOLKITS MODULE_LIST)
303 FILE_TO_LIST ("adm/MODULES" FILE_CONTENT)
304
305 foreach (CONTENT_LINE ${FILE_CONTENT})
306 string (REPLACE " " ";" CONTENT_LINE ${CONTENT_LINE})
307 list (GET CONTENT_LINE 0 MODULE_NAME)
308 list (REMOVE_AT CONTENT_LINE 0)
309 list (APPEND ${MODULE_LIST} ${MODULE_NAME})
310 # (!) REMOVE THE LINE BELOW (implicit variables)
311 set (${MODULE_NAME}_TOOLKITS ${CONTENT_LINE} PARENT_SCOPE)
312 endforeach()
313
314 set (${MODULE_LIST} ${${MODULE_LIST}} PARENT_SCOPE)
315endfunction()
316
adc33035 317# Returns OCCT version string from file Standard_Version.hxx (if available)
318function (OCCT_VERSION OCCT_VERSION_VAR)
319 set (OCC_VERSION_COMPLETE "7.1.0")
320 set (OCC_VERSION_DEVELOPMENT "")
321
322 set (OCCT_VERSION_LOCALVAR "${OCC_VERSION_COMPLETE}.${OCC_VERSION_DEVELOPMENT}")
323
324 set (STANDARD_VERSION_FILE "${CMAKE_SOURCE_DIR}/src/Standard/Standard_Version.hxx")
325 if (EXISTS "${STANDARD_VERSION_FILE}")
326 foreach (SOUGHT_VERSION OCC_VERSION_COMPLETE OCC_VERSION_DEVELOPMENT)
327 file (STRINGS "${STANDARD_VERSION_FILE}" ${SOUGHT_VERSION} REGEX "^#define ${SOUGHT_VERSION} .*")
328 string (REGEX REPLACE ".*${SOUGHT_VERSION} .*\"([^ ]+)\".*" "\\1" ${SOUGHT_VERSION} "${${SOUGHT_VERSION}}" )
329 endforeach()
330
331 if (NOT "${OCC_VERSION_DEVELOPMENT}" STREQUAL "")
332 set (OCCT_VERSION_LOCALVAR "${OCC_VERSION_COMPLETE}.${OCC_VERSION_DEVELOPMENT}")
333 else()
334 set (OCCT_VERSION_LOCALVAR "${OCC_VERSION_COMPLETE}")
335 endif()
336 endif()
337
338 set (${OCCT_VERSION_VAR} "${OCCT_VERSION_LOCALVAR}" PARENT_SCOPE)
339endfunction()
340