0027402: Fix Compilation with MinGw and Additional Libraries
[occt.git] / adm / cmake / 3rdparty_macro.cmake
1 ##
2
3 if(3RDPARTY_MACRO_ALREADY_INCLUDED)
4   return()
5 endif()
6 set(3RDPARTY_MACRO_ALREADY_INCLUDED 1)
7
8
9 macro (THIRDPARTY_PRODUCT PRODUCT_NAME HEADER_NAME LIBRARY_NAME LIBRARY_NAME_DEBUG)
10
11   if (NOT DEFINED INSTALL_${PRODUCT_NAME} AND BUILD_SHARED_LIBS)
12     set (INSTALL_${PRODUCT_NAME} OFF CACHE BOOL "${INSTALL_${PRODUCT_NAME}_DESCR}")
13   endif()
14
15   if (NOT DEFINED 3RDPARTY_${PRODUCT_NAME}_DIR)
16     set (3RDPARTY_${PRODUCT_NAME}_DIR "" CACHE PATH "The directory containing ${PRODUCT_NAME}")
17   endif()
18
19   # include occt macros. compiler_bitness, os_wiht_bit, compiler
20   OCCT_INCLUDE_CMAKE_FILE ("adm/cmake/occt_macros")
21
22   # specify product folder in connectin with 3RDPARTY_DIR
23   if (3RDPARTY_DIR AND EXISTS "${3RDPARTY_DIR}")
24     #CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_DIR 3RDPARTY_${PRODUCT_NAME}_DIR PATH "The directory containing ${PRODUCT_NAME}")
25
26     if (NOT 3RDPARTY_${PRODUCT_NAME}_DIR OR NOT EXISTS "${3RDPARTY_${PRODUCT_NAME}_DIR}")
27       FIND_PRODUCT_DIR ("${3RDPARTY_DIR}" ${PRODUCT_NAME} ${PRODUCT_NAME}_DIR_NAME)
28       if (${PRODUCT_NAME}_DIR_NAME)
29         set (3RDPARTY_${PRODUCT_NAME}_DIR "${3RDPARTY_DIR}/${${PRODUCT_NAME}_DIR_NAME}" CACHE PATH "The directory containing ${PRODUCT_NAME}" FORCE)
30       endif()
31     endif()
32   else()
33     #set (3RDPARTY_${PRODUCT_NAME}_DIR "" CACHE PATH "The directory containing ${PRODUCT_NAME}" FORCE)
34   endif()
35
36   if (NOT DEFINED 3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR)
37     set (3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR           "" CACHE PATH "the path of ${HEADER_NAME}")
38   endif()
39
40   if (BUILD_SHARED_LIBS)
41     if (NOT DEFINED 3RDPARTY_${PRODUCT_NAME}_LIBRARY OR NOT 3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR OR NOT EXISTS "${3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR}")
42       set (3RDPARTY_${PRODUCT_NAME}_LIBRARY               "" CACHE FILEPATH "${PRODUCT_NAME} library" FORCE)
43     endif()
44
45     if (NOT DEFINED 3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR)
46       set (3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR           "" CACHE PATH "The directory containing ${PRODUCT_NAME} library")
47     endif()
48
49     if (WIN32)
50       if (NOT DEFINED 3RDPARTY_${PRODUCT_NAME}_DLL OR NOT 3RDPARTY_${PRODUCT_NAME}_DLL_DIR OR NOT EXISTS "${3RDPARTY_${PRODUCT_NAME}_DLL_DIR}")
51         set (3RDPARTY_${PRODUCT_NAME}_DLL                 "" CACHE FILEPATH "${PRODUCT_NAME} shared library" FORCE)
52       endif()
53     endif()
54
55     if (WIN32)
56       if (NOT DEFINED 3RDPARTY_${PRODUCT_NAME}_DLL_DIR)
57         set (3RDPARTY_${PRODUCT_NAME}_DLL_DIR             "" CACHE PATH "The directory containing ${PRODUCT_NAME} shared library")
58       endif()
59     endif()
60   endif()
61
62   # check 3RDPARTY_${PRODUCT_NAME}_ paths for consistency with specified 3RDPARTY_${PRODUCT_NAME}_DIR
63   if (3RDPARTY_${PRODUCT_NAME}_DIR AND EXISTS "${3RDPARTY_${PRODUCT_NAME}_DIR}")
64     CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_${PRODUCT_NAME}_DIR 3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR PATH "the path to ${PRODUCT_NAME}")
65     if (BUILD_SHARED_LIBS)
66       CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_${PRODUCT_NAME}_DIR 3RDPARTY_${PRODUCT_NAME}_LIBRARY FILEPATH "the path to ${PRODUCT_NAME} library")
67
68       if (3RDPARTY_${PRODUCT_NAME}_LIBRARY AND EXISTS "${3RDPARTY_${PRODUCT_NAME}_LIBRARY}")
69         get_filename_component (3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR "${3RDPARTY_${PRODUCT_NAME}_LIBRARY}" PATH)
70         set (3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR "${3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR}" CACHE PATH "The directory containing ${PRODUCT_NAME} library" FORCE)
71       else()
72         CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_${PRODUCT_NAME}_DIR 3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR PATH "The directory containing ${PRODUCT_NAME} library")
73       endif()
74
75       if (WIN32)
76         CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_${PRODUCT_NAME}_DIR 3RDPARTY_${PRODUCT_NAME}_DLL FILEPATH "the path to ${PRODUCT_NAME} shared library")
77
78         if (3RDPARTY_${PRODUCT_NAME}_DLL AND EXISTS "${3RDPARTY_${PRODUCT_NAME}_DLL}")
79           get_filename_component (3RDPARTY_${PRODUCT_NAME}_DLL_DIR "${3RDPARTY_${PRODUCT_NAME}_DLL}" PATH)
80           set (3RDPARTY_${PRODUCT_NAME}_DLL_DIR "${3RDPARTY_${PRODUCT_NAME}_DLL_DIR}" CACHE PATH "The directory containing ${PRODUCT_NAME} shared library" FORCE)
81         else()
82           CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_${PRODUCT_NAME}_DIR 3RDPARTY_${PRODUCT_NAME}_DLL_DIR PATH "The directory containing ${PRODUCT_NAME} shared library")
83         endif()
84       endif()
85     endif()
86   endif()
87
88   # header
89   if (NOT 3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR OR NOT EXISTS "${3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR}")
90
91     # set 3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR as notfound, otherwise find_library can't assign a new value to 3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR
92     set (3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR "3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR-NOTFOUND" CACHE FILEPATH "the path to ${HEADER_NAME}" FORCE)
93
94     if (3RDPARTY_${PRODUCT_NAME}_DIR AND EXISTS "${3RDPARTY_${PRODUCT_NAME}_DIR}")
95       find_path (3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR NAMES ${HEADER_NAME}
96                                                       PATHS ${3RDPARTY_${PRODUCT_NAME}_DIR}
97                                                       PATH_SUFFIXES include
98                                                       CMAKE_FIND_ROOT_PATH_BOTH
99                                                       NO_DEFAULT_PATH)
100     else()
101       find_path (3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR NAMES ${HEADER_NAME}
102                                                       PATH_SUFFIXES include
103                                                       CMAKE_FIND_ROOT_PATH_BOTH)
104     endif()
105   endif()
106
107   if (3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR AND EXISTS "${3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR}")
108     list (APPEND 3RDPARTY_INCLUDE_DIRS "${3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR}")
109   else()
110     list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR)
111
112     set (3RDPARTY_${PRODUCT_NAME}_INCLUDE_DIR "" CACHE FILEPATH "The path to ${HEADER_NAME}" FORCE)
113   endif()
114
115   if (BUILD_SHARED_LIBS)
116     # library
117     if (NOT 3RDPARTY_${PRODUCT_NAME}_LIBRARY OR NOT EXISTS "${3RDPARTY_${PRODUCT_NAME}_LIBRARY}")
118       set (CMAKE_FIND_LIBRARY_SUFFIXES .lib .so .dylib .a)
119
120       set (${PRODUCT_NAME}_PATH_SUFFIXES lib)
121       if (ANDROID)
122         set (${PRODUCT_NAME}_PATH_SUFFIXES ${${PRODUCT_NAME}_PATH_SUFFIXES} libs/${ANDROID_ABI})
123       endif()
124
125       # set 3RDPARTY_${PRODUCT_NAME}_LIBRARY as notfound, otherwise find_library can't assign a new value to 3RDPARTY_${PRODUCT_NAME}_LIBRARY
126       set (3RDPARTY_${PRODUCT_NAME}_LIBRARY "3RDPARTY_${PRODUCT_NAME}_LIBRARY-NOTFOUND" CACHE FILEPATH "The path to ${PRODUCT_NAME} library" FORCE)
127
128       if (3RDPARTY_${PRODUCT_NAME}_DIR AND EXISTS "${3RDPARTY_${PRODUCT_NAME}_DIR}")
129         find_library (3RDPARTY_${PRODUCT_NAME}_LIBRARY NAMES ${LIBRARY_NAME}
130                                                        PATHS "${3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR}" "${3RDPARTY_${PRODUCT_NAME}_DIR}"
131                                                        PATH_SUFFIXES ${${PRODUCT_NAME}_PATH_SUFFIXES}
132                                                        CMAKE_FIND_ROOT_PATH_BOTH
133                                                        NO_DEFAULT_PATH)
134       else()
135         find_library (3RDPARTY_${PRODUCT_NAME}_LIBRARY NAMES ${LIBRARY_NAME} 
136                                                        PATH_SUFFIXES ${${PRODUCT_NAME}_PATH_SUFFIXES}
137                                                        CMAKE_FIND_ROOT_PATH_BOTH)
138       endif()
139
140       if (3RDPARTY_${PRODUCT_NAME}_LIBRARY AND EXISTS "${3RDPARTY_${PRODUCT_NAME}_LIBRARY}")
141         get_filename_component (3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR "${3RDPARTY_${PRODUCT_NAME}_LIBRARY}" PATH)
142         set (3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR "${3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR}" CACHE PATH "The directory containing ${PRODUCT_NAME} library" FORCE)
143       else()
144         set (3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR "" CACHE PATH "The directory containing ${PRODUCT_NAME} library" FORCE)
145       endif()
146     endif()
147
148     if (3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR AND EXISTS "${3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR}")
149       list (APPEND 3RDPARTY_LIBRARY_DIRS "${3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR}")
150     else()
151       list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR)
152
153       set (3RDPARTY_${PRODUCT_NAME}_LIBRARY "" CACHE FILEPATH "The path to ${PRODUCT_NAME} library" FORCE)
154     endif()
155
156     # shared library
157     if (WIN32)
158       if (NOT 3RDPARTY_${PRODUCT_NAME}_DLL OR NOT EXISTS "${3RDPARTY_${PRODUCT_NAME}_DLL}")
159
160         set (CMAKE_FIND_LIBRARY_SUFFIXES .dll)
161
162         # set 3RDPARTY_${PRODUCT_NAME}_DLL as notfound, otherwise find_library can't assign a new value to 3RDPARTY_${PRODUCT_NAME}_DLL
163         set (3RDPARTY_${PRODUCT_NAME}_DLL "3RDPARTY_${PRODUCT_NAME}_DLL-NOTFOUND" CACHE FILEPATH "The path to ${PRODUCT_NAME} shared library" FORCE)
164
165         if (3RDPARTY_${PRODUCT_NAME}_DIR AND EXISTS "${3RDPARTY_${PRODUCT_NAME}_DIR}")
166           find_library (3RDPARTY_${PRODUCT_NAME}_DLL  NAMES ${LIBRARY_NAME}
167                                                       PATHS "${3RDPARTY_${PRODUCT_NAME}_DIR}"
168                                                       PATH_SUFFIXES bin
169                                                       NO_DEFAULT_PATH)
170         else()
171           find_library (3RDPARTY_${PRODUCT_NAME}_DLL NAMES ${LIBRARY_NAME} PATH_SUFFIXES bin)
172         endif()
173
174         if (3RDPARTY_${PRODUCT_NAME}_DLL AND EXISTS "${3RDPARTY_${PRODUCT_NAME}_DLL}")
175           get_filename_component (3RDPARTY_${PRODUCT_NAME}_DLL_DIR "${3RDPARTY_${PRODUCT_NAME}_DLL}" PATH)
176           set (3RDPARTY_${PRODUCT_NAME}_DLL_DIR "${3RDPARTY_${PRODUCT_NAME}_DLL_DIR}" CACHE PATH "The directory containing ${PRODUCT_NAME} library" FORCE)
177         else()
178           set (3RDPARTY_${PRODUCT_NAME}_DLL_DIR "" CACHE PATH "The directory containing ${PRODUCT_NAME} shared library" FORCE)
179
180           set (3RDPARTY_${PRODUCT_NAME}_DLL "" CACHE FILEPATH "${PRODUCT_NAME} shared library" FORCE)
181         endif()
182       endif()
183
184       if (3RDPARTY_${PRODUCT_NAME}_DLL_DIR OR EXISTS "${3RDPARTY_${PRODUCT_NAME}_DLL_DIR}")
185         list (APPEND 3RDPARTY_DLL_DIRS "${3RDPARTY_${PRODUCT_NAME}_DLL_DIR}")
186       else()
187         list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_${PRODUCT_NAME}_DLL_DIR)
188       endif()
189     endif()
190
191     set (USED_3RDPARTY_${PRODUCT_NAME}_DIR "")
192
193     if (INSTALL_${PRODUCT_NAME})
194       OCCT_MAKE_OS_WITH_BITNESS()
195       OCCT_MAKE_COMPILER_SHORT_NAME()
196
197       if (WIN32)
198         if (SINGLE_GENERATOR)
199           install (FILES "${3RDPARTY_${PRODUCT_NAME}_DLL}" DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}")
200         else()
201           install (FILES "${3RDPARTY_${PRODUCT_NAME}_DLL}"
202                    CONFIGURATIONS Release
203                    DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}")
204           install (FILES "${3RDPARTY_${PRODUCT_NAME}_DLL}"
205                    CONFIGURATIONS RelWithDebInfo
206                    DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}i")
207           install (FILES "${3RDPARTY_${PRODUCT_NAME}_DLL}"
208                    CONFIGURATIONS Debug
209                    DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_BIN}d")
210         endif()
211       else()
212         get_filename_component(ABS_PATH ${3RDPARTY_${PRODUCT_NAME}_LIBRARY} REALPATH)
213
214         if ("${PRODUCT_NAME}" STREQUAL "FREEIMAGE")
215           get_filename_component(FREEIMLIB ${3RDPARTY_${PRODUCT_NAME}_LIBRARY} NAME)
216
217           if (SINGLE_GENERATOR)
218             install (FILES "${ABS_PATH}" DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}" RENAME ${FREEIMLIB}.3)
219           else()
220             install (FILES "${ABS_PATH}"
221                      CONFIGURATIONS Release
222                      DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}"
223                      RENAME ${FREEIMLIB}.3)
224             install (FILES "${ABS_PATH}"
225                      CONFIGURATIONS RelWithDebInfo
226                      DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}i"
227                      RENAME ${FREEIMLIB}.3)
228             install (FILES "${ABS_PATH}"
229                      CONFIGURATIONS Debug
230                      DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}d"
231                      RENAME ${FREEIMLIB}.3)
232           endif()
233         endif()
234
235         if("${PRODUCT_NAME}" STREQUAL "GL2PS")
236           get_filename_component(GL2PSLIB ${3RDPARTY_${PRODUCT_NAME}_LIBRARY} NAME)
237
238           if (SINGLE_GENERATOR)
239             install (FILES "${ABS_PATH}" DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}" RENAME ${GL2PSLIB}.1)
240           else()
241             install (FILES "${ABS_PATH}"
242                      CONFIGURATIONS Release
243                      DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}"
244                      RENAME ${GL2PSLIB}.1)
245             install (FILES "${ABS_PATH}"
246                      CONFIGURATIONS RelWithDebInfo
247                      DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}i"
248                      RENAME ${GL2PSLIB}.1)
249             install (FILES "${ABS_PATH}"
250                      CONFIGURATIONS Debug
251                      DESTINATION "${INSTALL_DIR}/${INSTALL_DIR_LIB}d"
252                      RENAME ${GL2PSLIB}.1)
253           endif()
254         endif()
255       endif()
256     else()
257       # the library directory for using by the executable
258       if (WIN32)
259         set (USED_3RDPARTY_${PRODUCT_NAME}_DIR "${3RDPARTY_${PRODUCT_NAME}_DLL_DIR}")
260       else()
261         set (USED_3RDPARTY_${PRODUCT_NAME}_DIR "${3RDPARTY_${PRODUCT_NAME}_LIBRARY_DIR}")
262       endif()
263     endif()
264
265     mark_as_advanced (3RDPARTY_${PRODUCT_NAME}_LIBRARY 3RDPARTY_${PRODUCT_NAME}_DLL)
266   endif()
267 endmacro()
268
269 macro (COMPLIANCE_PRODUCT_CONSISTENCY LIBNAME)
270   if (3RDPARTY_${LIBNAME}_DIR AND EXISTS "${3RDPARTY_${LIBNAME}_DIR}")
271     # include dir
272     set (DOES_PATH_CONTAIN FALSE)
273     if (3RDPARTY_${LIBNAME}_INCLUDE_DIR AND EXISTS "${3RDPARTY_${LIBNAME}_INCLUDE_DIR}")
274       string (REGEX MATCH "${3RDPARTY_${LIBNAME}_DIR}" DOES_PATH_CONTAIN "${3RDPARTY_${LIBNAME}_INCLUDE_DIR}")
275     endif()
276     if (NOT DOES_PATH_CONTAIN)
277       set (3RDPARTY_${LIBNAME}_INCLUDE_DIR "" CACHE FILEPATH "The directory containing headers of ${LIBNAME}" FORCE)
278     endif()
279
280     if (BUILD_SHARED_LIBS)
281       # library dir
282       set (DOES_PATH_CONTAIN FALSE)
283       if (3RDPARTY_${LIBNAME}_LIBRARY_DIR AND EXISTS "${3RDPARTY_${LIBNAME}_LIBRARY_DIR}")
284         string (REGEX MATCH "${3RDPARTY_${LIBNAME}_DIR}" DOES_PATH_CONTAIN "${3RDPARTY_${LIBNAME}_LIBRARY_DIR}")
285       endif()
286       if (NOT DOES_PATH_CONTAIN)
287         set (3RDPARTY_${LIBNAME}_LIBRARY_DIR "" CACHE FILEPATH "The directory containing ${LIBNAME} library" FORCE)
288       endif()
289
290       # shared library dir
291       if (WIN32)
292         set (DOES_PATH_CONTAIN FALSE)
293         if (3RDPARTY_${LIBNAME}_DLL_DIR AND EXISTS "${3RDPARTY_${LIBNAME}_DLL_DIR}")
294           string (REGEX MATCH "${3RDPARTY_${LIBNAME}_DIR}" DOES_PATH_CONTAIN "${3RDPARTY_${LIBNAME}_DLL_DIR}")
295         endif()
296         if (NOT DOES_PATH_CONTAIN)
297           set (3RDPARTY_${LIBNAME}_DLL_DIR "" CACHE FILEPATH "The directory containing ${LIBNAME} shared library" FORCE)
298         endif()
299       endif()
300     endif()
301   endif()
302
303   if (BUILD_SHARED_LIBS)
304     # check library
305     set (DOES_PATH_CONTAIN FALSE)
306     if (3RDPARTY_${LIBNAME}_LIBRARY_DIR AND EXISTS "${3RDPARTY_${LIBNAME}_LIBRARY_DIR}")
307       if (3RDPARTY_${LIBNAME}_LIBRARY AND EXISTS "${3RDPARTY_${LIBNAME}_LIBRARY}")
308         string (REGEX MATCH "${3RDPARTY_${LIBNAME}_LIBRARY_DIR}" DOES_PATH_CONTAIN "${3RDPARTY_${LIBNAME}_LIBRARY}")
309       endif()
310     endif()
311     if (NOT DOES_PATH_CONTAIN)
312       set (3RDPARTY_${LIBNAME}_LIBRARY "" CACHE FILEPATH "${LIBNAME} library" FORCE)
313     endif()
314
315     # check shared library
316     if (WIN32)
317       set (DOES_PATH_CONTAIN FALSE)
318       if (3RDPARTY_${LIBNAME}_DLL_DIR AND EXISTS "${3RDPARTY_${LIBNAME}_DLL_DIR}")
319         if (3RDPARTY_${LIBNAME}_DLL AND EXISTS "${3RDPARTY_${LIBNAME}_DLL}")
320           string (REGEX MATCH "${3RDPARTY_${LIBNAME}_DLL_DIR}" DOES_PATH_CONTAIN "${3RDPARTY_${LIBNAME}_DLL}")
321         endif()
322       endif()
323       if (NOT DOES_PATH_CONTAIN)
324         set (3RDPARTY_${LIBNAME}_DLL "" CACHE FILEPATH "${LIBNAME} shared library" FORCE)
325       endif()
326     endif()
327   endif()
328 endmacro()