0025114: CMake-based build tools for OCCT 7.0
[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
22# BUILD_POSTFIX, IS_BUILD_DEBUG variables
23macro (OCCT_MAKE_BUILD_POSTFIX)
24 if ("${BUILD_CONFIGURATION}" STREQUAL "Debug")
25 set (BUILD_POSTFIX "d")
26 set (IS_BUILD_DEBUG "")
27 else()
28 set (BUILD_POSTFIX "")
29 OCCT_CHECK_AND_UNSET (IS_BUILD_DEBUG)
30 endif()
31endmacro()
32
33# COMPILER_BITNESS variable
34macro (OCCT_MAKE_COMPILER_BITNESS)
35 math (EXPR COMPILER_BITNESS "32 + 32*(${CMAKE_SIZEOF_VOID_P}/8)")
36endmacro()
37
38# OS_WITH_BIT
39macro (OCCT_MAKE_OS_WITH_BITNESS)
40
41 OCCT_MAKE_COMPILER_BITNESS()
42
43 if (WIN32)
44 set (OS_WITH_BIT "win${COMPILER_BITNESS}")
45 elseif(APPLE)
46 set (OS_WITH_BIT "mac${COMPILER_BITNESS}")
47 else()
48 set (OS_WITH_BIT "lin${COMPILER_BITNESS}")
49 endif()
50endmacro()
51
52# COMPILER variable
53macro (OCCT_MAKE_COMPILER_SHORT_NAME)
54 if (MSVC)
55 if (MSVC70)
56 set (COMPILER vc7)
57 elseif (MSVC80)
58 set (COMPILER vc8)
59 elseif (MSVC90)
60 set (COMPILER vc9)
61 elseif (MSVC10)
62 set (COMPILER vc10)
63 elseif (MSVC11)
64 set (COMPILER vc11)
65 elseif (MSVC12)
66 set (COMPILER vc12)
67 endif()
68 elseif (DEFINED CMAKE_COMPILER_IS_GNUCC)
69 set (COMPILER gcc)
70 elseif (DEFINED CMAKE_COMPILER_IS_GNUCXX)
71 set (COMPILER gxx)
72 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
73 set (COMPILER clang)
74 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
75 set (COMPILER icc)
76 else()
77 set (COMPILER ${CMAKE_GENERATOR})
78 string (REGEX REPLACE " " "" COMPILER ${COMPILER})
79 endif()
80endmacro()
81
82function (SUBDIRECTORY_NAMES MAIN_DIRECTORY RESULT)
83 file (GLOB SUB_ITEMS "${MAIN_DIRECTORY}/*")
84
85 foreach (ITEM ${SUB_ITEMS})
86 if (IS_DIRECTORY "${ITEM}")
87 get_filename_component (ITEM_NAME "${ITEM}" NAME)
88 list (APPEND LOCAL_RESULT "${ITEM_NAME}")
89 endif()
90 endforeach()
91 set (${RESULT} ${LOCAL_RESULT} PARENT_SCOPE)
92endfunction()
93
94function (FIND_PRODUCT_DIR ROOT_DIR PRODUCT_NAME RESULT)
95 OCCT_MAKE_COMPILER_SHORT_NAME()
96 OCCT_MAKE_COMPILER_BITNESS()
97
98 string (TOLOWER "${PRODUCT_NAME}" lower_PRODUCT_NAME)
99
100 list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*${COMPILER}.*${COMPILER_BITNESS}")
101 list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*[0-9.]+.*${COMPILER}.*${COMPILER_BITNESS}")
102 list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*[0-9.]+.*${COMPILER_BITNESS}")
103 list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}.*[0-9.]+")
104 list (APPEND SEARCH_TEMPLATES "${lower_PRODUCT_NAME}")
105
106 SUBDIRECTORY_NAMES ("${ROOT_DIR}" SUBDIR_NAME_LIST)
107
108 foreach (SEARCH_TEMPLATE ${SEARCH_TEMPLATES})
109 if (LOCAL_RESULT)
110 BREAK()
111 endif()
112
113 foreach (SUBDIR_NAME ${SUBDIR_NAME_LIST})
114 string (TOLOWER "${SUBDIR_NAME}" lower_SUBDIR_NAME)
115
116 string (REGEX MATCH "${SEARCH_TEMPLATE}" DUMMY_VAR "${lower_SUBDIR_NAME}")
117 if (DUMMY_VAR)
118 list (APPEND LOCAL_RESULT ${SUBDIR_NAME})
119 endif()
120 endforeach()
121 endforeach()
122
123 if (LOCAL_RESULT)
124 list (LENGTH "${LOCAL_RESULT}" LOC_LEN)
125 math (EXPR LAST_ELEMENT_INDEX "${LOC_LEN}-1")
126 list (GET LOCAL_RESULT ${LAST_ELEMENT_INDEX} DUMMY)
127 set (${RESULT} ${DUMMY} PARENT_SCOPE)
128 endif()
129endfunction()
130
131macro (OCCT_INSTALL_FILE_OR_DIR BEING_INSTALLED_OBJECT DESTINATION_PATH)
132 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/${BEING_INSTALLED_OBJECT}")
133 if (IS_DIRECTORY "${BUILD_PATCH_DIR}/${BEING_INSTALLED_OBJECT}")
134 # first of all, install original files
135 install (DIRECTORY "${CMAKE_SOURCE_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
136
137 # secondly, rewrite original files with patched ones
138 install (DIRECTORY "${BUILD_PATCH_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
139 else()
140 install (FILES "${BUILD_PATCH_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
141 endif()
142 else()
143 if (IS_DIRECTORY "${CMAKE_SOURCE_DIR}/${BEING_INSTALLED_OBJECT}")
144 install (DIRECTORY "${CMAKE_SOURCE_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
145 else()
146 install (FILES "${CMAKE_SOURCE_DIR}/${BEING_INSTALLED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
147 endif()
148 endif()
149endmacro()
150
151macro (OCCT_CONFIGURE_AND_INSTALL BEING_CONGIRUGED_FILE FINAL_NAME DESTINATION_PATH)
152 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/${BEING_CONGIRUGED_FILE}")
153 configure_file("${BUILD_PATCH_DIR}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
154 else()
155 configure_file("${CMAKE_SOURCE_DIR}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
156 endif()
157
158 install(FILES "${OCCT_BINARY_DIR}/${FINAL_NAME}" DESTINATION "${DESTINATION_PATH}")
159endmacro()
160
161macro (OCCT_COPY_FILE_OR_DIR BEING_COPIED_OBJECT DESTINATION_PATH)
162 # first of all, copy original files
163 file (COPY "${CMAKE_SOURCE_DIR}/${BEING_COPIED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
164
165 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/${BEING_COPIED_OBJECT}")
166 # secondly, rewrite original files with patched ones
167 file (COPY "${BUILD_PATCH_DIR}/${BEING_COPIED_OBJECT}" DESTINATION "${DESTINATION_PATH}")
168 endif()
169endmacro()
170
171macro (OCCT_CONFIGURE BEING_CONGIRUGED_FILE FINAL_NAME)
172 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/${BEING_CONGIRUGED_FILE}")
173 configure_file("${BUILD_PATCH_DIR}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
174 else()
175 configure_file("${CMAKE_SOURCE_DIR}/${BEING_CONGIRUGED_FILE}" "${FINAL_NAME}" @ONLY)
176 endif()
177endmacro()
178
179function (OCCT_IS_PRODUCT_REQUIRED CSF_VAR_NAME USE_PRODUCT)
180 set (${USE_PRODUCT} OFF PARENT_SCOPE)
181
182 if (NOT USED_TOOLKITS)
183 message(STATUS "Warning: the list of being used toolkits is empty")
184 else()
185 foreach (USED_TOOLKIT ${USED_TOOLKITS})
186 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/src/${USED_TOOLKIT}/EXTERNLIB")
187 file (READ "${BUILD_PATCH_DIR}/src/${USED_TOOLKIT}/EXTERNLIB" FILE_CONTENT)
188 elseif (EXISTS "${CMAKE_SOURCE_DIR}/src/${USED_TOOLKIT}/EXTERNLIB")
189 file (READ "${CMAKE_SOURCE_DIR}/src/${USED_TOOLKIT}/EXTERNLIB" FILE_CONTENT)
190 endif()
191
192 string (REGEX MATCH "${CSF_VAR_NAME}" DOES_FILE_CONTAIN "${FILE_CONTENT}")
193
194 if (DOES_FILE_CONTAIN)
195 set (${USE_PRODUCT} ON PARENT_SCOPE)
196 break()
197 endif()
198 endforeach()
199 endif()
200endfunction()
201
202function (FILE_TO_LIST FILE_NAME FILE_CONTENT)
203 set (LOCAL_FILE_CONTENT)
204 if (BUILD_PATCH_DIR AND EXISTS "${BUILD_PATCH_DIR}/${FILE_NAME}")
205 file (STRINGS "${BUILD_PATCH_DIR}/${FILE_NAME}" LOCAL_FILE_CONTENT)
206 elseif (EXISTS "${CMAKE_SOURCE_DIR}/${FILE_NAME}")
207 file (STRINGS "${CMAKE_SOURCE_DIR}/${FILE_NAME}" LOCAL_FILE_CONTENT)
208 endif()
209
210 set (${FILE_CONTENT} ${LOCAL_FILE_CONTENT} PARENT_SCOPE)
211endfunction()
212
213# Function to determine if TOOLKIT is OCCT toolkit
214function (IS_OCCT_TOOLKIT TOOLKIT_NAME MODULES IS_TOOLKIT_FOUND)
215 set (${IS_TOOLKIT_FOUND} OFF PARENT_SCOPE)
216 foreach (MODULE ${${MODULES}})
217 set (TOOLKITS ${${MODULE}_TOOLKITS})
218 list (FIND TOOLKITS ${TOOLKIT_NAME} FOUND)
219
220 if (NOT ${FOUND} EQUAL -1)
221 set (${IS_TOOLKIT_FOUND} ON PARENT_SCOPE)
222 endif()
223 endforeach(MODULE)
224endfunction()
225
226# TOOLKIT_DEPS is defined with dependencies from file src/TOOLKIT_NAME/EXTERNLIB.
227# CSF_ variables are ignored
228function (OCCT_TOOLKIT_DEP TOOLKIT_NAME TOOLKIT_DEPS)
229 FILE_TO_LIST ("src/${TOOLKIT_NAME}/EXTERNLIB" FILE_CONTENT)
230
231 #list (APPEND LOCAL_TOOLKIT_DEPS ${TOOLKIT_NAME})
232 set (LOCAL_TOOLKIT_DEPS)
233 foreach (FILE_CONTENT_LINE ${FILE_CONTENT})
234 string (REGEX MATCH "^TK" TK_FOUND ${FILE_CONTENT_LINE})
235 if ("${FILE_CONTENT_LINE}" STREQUAL "DRAWEXE" OR NOT "${TK_FOUND}" STREQUAL "")
236 list (APPEND LOCAL_TOOLKIT_DEPS ${FILE_CONTENT_LINE})
237 endif()
238 endforeach()
239
240 set (${TOOLKIT_DEPS} ${LOCAL_TOOLKIT_DEPS} PARENT_SCOPE)
241endfunction()
242
243# TOOLKIT_FULL_DEPS is defined with complete dependencies (all levels)
244function (OCCT_TOOLKIT_FULL_DEP TOOLKIT_NAME TOOLKIT_FULL_DEPS)
245 # first level dependencies are stored in LOCAL_TOOLKIT_FULL_DEPS
246 OCCT_TOOLKIT_DEP (${TOOLKIT_NAME} LOCAL_TOOLKIT_FULL_DEPS)
247
248 list (LENGTH LOCAL_TOOLKIT_FULL_DEPS LIST_LENGTH)
249 set (LIST_INDEX 0)
250 while (LIST_INDEX LESS LIST_LENGTH)
251 list (GET LOCAL_TOOLKIT_FULL_DEPS ${LIST_INDEX} CURRENT_TOOLKIT)
252 OCCT_TOOLKIT_DEP (${CURRENT_TOOLKIT} CURRENT_TOOLKIT_DEPS)
253
254 # append toolkits are not contained
255 foreach (CURRENT_TOOLKIT_DEP ${CURRENT_TOOLKIT_DEPS})
256 set (CURRENT_TOOLKIT_DEP_FOUND OFF)
257 foreach (LOCAL_TOOLKIT_FULL_DEP ${LOCAL_TOOLKIT_FULL_DEPS})
258 if ("${CURRENT_TOOLKIT_DEP}" STREQUAL "${LOCAL_TOOLKIT_FULL_DEP}")
259 set (CURRENT_TOOLKIT_DEP_FOUND ON)
260 break()
261 endif()
262 endforeach()
263 if ("${CURRENT_TOOLKIT_DEP_FOUND}" STREQUAL "OFF")
264 list (APPEND LOCAL_TOOLKIT_FULL_DEPS ${CURRENT_TOOLKIT_DEP})
265 endif()
266 endforeach()
267
268 # increment the list index
269 MATH(EXPR LIST_INDEX "${LIST_INDEX}+1")
270
271 # calculate new length
272 list (LENGTH LOCAL_TOOLKIT_FULL_DEPS LIST_LENGTH)
273 endwhile()
274
275 set (${TOOLKIT_FULL_DEPS} ${LOCAL_TOOLKIT_FULL_DEPS} PARENT_SCOPE)
276endfunction()
277
278# Function to get list of modules and toolkits from file adm/MODULES.
279# Creates list <$MODULE_LIST> to store list of MODULES and
280# <NAME_OF_MODULE>_TOOLKITS foreach module to store its toolkits.
281function (OCCT_MODULES_AND_TOOLKITS MODULE_LIST)
282 FILE_TO_LIST ("adm/MODULES" FILE_CONTENT)
283
284 foreach (CONTENT_LINE ${FILE_CONTENT})
285 string (REPLACE " " ";" CONTENT_LINE ${CONTENT_LINE})
286 list (GET CONTENT_LINE 0 MODULE_NAME)
287 list (REMOVE_AT CONTENT_LINE 0)
288 list (APPEND ${MODULE_LIST} ${MODULE_NAME})
289 # (!) REMOVE THE LINE BELOW (implicit variables)
290 set (${MODULE_NAME}_TOOLKITS ${CONTENT_LINE} PARENT_SCOPE)
291 endforeach()
292
293 set (${MODULE_LIST} ${${MODULE_LIST}} PARENT_SCOPE)
294endfunction()
295