0029589: Configuration - allow MFC samples to be built when OCCT is linked statically
[occt.git] / adm / cmake / ffmpeg.cmake
1 # FFmpeg
2
3 if (NOT DEFINED INSTALL_FFMPEG)
4   set (INSTALL_FFMPEG OFF CACHE BOOL "${INSTALL_FFMPEG_DESCR}")
5 endif()
6
7 if (NOT DEFINED 3RDPARTY_FFMPEG_DIR)
8   set (3RDPARTY_FFMPEG_DIR "" CACHE PATH "The directory containing FFmpeg")
9 endif()
10
11 # include occt macros. compiler_bitness, os_with_bit, compiler
12 OCCT_INCLUDE_CMAKE_FILE ("adm/cmake/occt_macros")
13
14 # specify FFMPEG folder in connection with 3RDPARTY_DIR
15 if (3RDPARTY_DIR AND EXISTS "${3RDPARTY_DIR}")
16   if (NOT 3RDPARTY_FFMPEG_DIR OR NOT EXISTS "${3RDPARTY_FFMPEG_DIR}")
17     FIND_PRODUCT_DIR ("${3RDPARTY_DIR}" FFMPEG FFMPEG_DIR_NAME)
18     if (FFMPEG_DIR_NAME)
19       set (3RDPARTY_FFMPEG_DIR "${3RDPARTY_DIR}/${FFMPEG_DIR_NAME}" CACHE PATH "The directory containing FFmpeg" FORCE)
20     endif()
21   endif()
22 else()
23 endif()
24
25 # define required FFMPEG variables
26 if (NOT DEFINED 3RDPARTY_FFMPEG_INCLUDE_DIR)
27   set (3RDPARTY_FFMPEG_INCLUDE_DIR  "" CACHE PATH "the path of headers directory")
28 endif()
29
30 separate_arguments (CSF_FFmpeg)
31 foreach (LIBRARY_NAME ${CSF_FFmpeg})
32   if (NOT DEFINED 3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME} OR NOT 3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME} OR NOT EXISTS "${3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME}}")
33     set (3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME}               "" CACHE FILEPATH "FFmpeg framework (${LIBRARY_NAME})" FORCE)
34   endif()
35
36   if (NOT DEFINED 3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME})
37     set (3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME}           "" CACHE PATH "The directory containing FFmpeg framework (${LIBRARY_NAME})")
38   endif()
39
40   if (WIN32)
41     if (NOT DEFINED 3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME} OR NOT 3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME} OR NOT EXISTS "${3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME}}")
42       set (3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}                 "" CACHE FILEPATH "FFmpeg shared libraries (${LIBRARY_NAME})" FORCE)
43     endif()
44   endif()
45
46   if (WIN32)
47     if (NOT DEFINED 3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME})
48       set (3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME}               "" CACHE PATH "The directory containing FFmpeg shared libraries (${LIBRARY_NAME})")
49     endif()
50   endif()
51
52   # check 3RDPARTY_${PRODUCT_NAME}_ paths for consistency with specified 3RDPARTY_${PRODUCT_NAME}_DIR
53   if (3RDPARTY_FFMPEG_DIR AND EXISTS "${3RDPARTY_FFMPEG_DIR}")
54     CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_FFMPEG_DIR 3RDPARTY_FFMPEG_INCLUDE_DIR PATH "the path to FFmpeg")
55     CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_FFMPEG_DIR 3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME} FILEPATH "the path to FFmpeg framework (${LIBRARY_NAME})")
56
57     if (3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME} AND EXISTS "${3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME}}")
58       get_filename_component (3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME} "${3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME}}" PATH)
59       set (3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME} "${3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME}}" CACHE PATH "The directory containing FFmpeg library (${LIBRARY_NAME})" FORCE)
60     else()
61       CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_FFMPEG_DIR 3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME} PATH "The directory containing FFmpeg library (${LIBRARY_NAME})")
62     endif()
63
64     if (WIN32)
65       CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_FFMPEG_DIR 3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME} FILEPATH "the path to FFmpeg shared library (${LIBRARY_NAME})")
66
67       if (3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME} AND EXISTS "${3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}}")
68         get_filename_component (3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME} "${3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}}" PATH)
69         set (3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME} "${3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME}}" CACHE PATH "The directory containing FFmpeg shared library (${LIBRARY_NAME})" FORCE)
70       else()
71         CHECK_PATH_FOR_CONSISTENCY (3RDPARTY_FFMPEG_DIR 3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME} PATH "The directory containing FFmpeg shared library (${LIBRARY_NAME})")
72       endif()
73     endif()
74   endif()
75 endforeach()
76
77 # header
78 if (NOT 3RDPARTY_FFMPEG_INCLUDE_DIR OR NOT EXISTS "${3RDPARTY_FFMPEG_INCLUDE_DIR}")
79   set (HEADER_NAMES avutil.h libavutil/avutil.h)
80
81   # set 3RDPARTY_FFMPEG_INCLUDE_DIR as notfound, otherwise find_library can't assign a new value to 3RDPARTY_FFMPEG_INCLUDE_DIR
82   set (3RDPARTY_FFMPEG_INCLUDE_DIR "3RDPARTY_FFMPEG_INCLUDE_DIR-NOTFOUND" CACHE FILEPATH "the path to header directory" FORCE)
83
84   if (3RDPARTY_FFMPEG_DIR AND EXISTS "${3RDPARTY_FFMPEG_DIR}")
85     find_path (3RDPARTY_FFMPEG_INCLUDE_DIR NAMES ${HEADER_NAMES}
86                                                  PATHS ${3RDPARTY_FFMPEG_DIR}
87                                                  PATH_SUFFIXES include
88                                                  CMAKE_FIND_ROOT_PATH_BOTH
89                                                  NO_DEFAULT_PATH)
90   else()
91     find_path (3RDPARTY_FFMPEG_INCLUDE_DIR NAMES ${HEADER_NAMES}
92                                                  PATH_SUFFIXES include
93                                                  CMAKE_FIND_ROOT_PATH_BOTH)
94   endif()
95 endif()
96
97 if (3RDPARTY_FFMPEG_INCLUDE_DIR AND EXISTS "${3RDPARTY_FFMPEG_INCLUDE_DIR}")
98   list (APPEND 3RDPARTY_INCLUDE_DIRS "${3RDPARTY_FFMPEG_INCLUDE_DIR}")
99 else()
100   list (APPEND 3RDPARTY_NOT_INCLUDED 3RDPARTY_FFMPEG_INCLUDE_DIR)
101
102   set (3RDPARTY_FFMPEG_INCLUDE_DIR "" CACHE FILEPATH "the path to avutil.h" FORCE)
103 endif()
104
105 # libraries
106 foreach (LIBRARY_NAME ${CSF_FFmpeg})
107   if (NOT 3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME} OR NOT EXISTS "${3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME}}")
108     set (CMAKE_FIND_LIBRARY_SUFFIXES .lib .so .dylib .a)
109
110     set (FFMPEG_PATH_SUFFIXES lib)
111     if (ANDROID)
112       set (FFMPEG_PATH_SUFFIXES ${FFMPEG_PATH_SUFFIXES} libs/${ANDROID_ABI})
113     elseif(APPLE)
114       set (FFMPEG_PATH_SUFFIXES ${FFMPEG_PATH_SUFFIXES} Frameworks)
115     endif()
116
117     # set 3RDPARTY_FFMPEG_LIBRARY as notfound, otherwise find_library can't assign a new value to 3RDPARTY_FFMPEG_LIBRARY
118     set (3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME} "3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME}-NOTFOUND" CACHE FILEPATH "The path to FFmpeg library (${LIBRARY_NAME})" FORCE)
119
120     if (3RDPARTY_FFMPEG_DIR AND EXISTS "${3RDPARTY_FFMPEG_DIR}")
121       find_library (3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME} NAMES ${LIBRARY_NAME}
122                     PATHS "${3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME}}" "${3RDPARTY_FFMPEG_DIR}"
123                     PATH_SUFFIXES ${FFMPEG_PATH_SUFFIXES}
124                     CMAKE_FIND_ROOT_PATH_BOTH
125                     NO_DEFAULT_PATH)
126     else()
127       find_library (3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME} NAMES ${LIBRARY_NAME}
128                     PATH_SUFFIXES ${FFMPEG_PATH_SUFFIXES}
129                     CMAKE_FIND_ROOT_PATH_BOTH)
130     endif()
131
132     if (3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME} AND EXISTS "${3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME}}")
133       get_filename_component (3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME} "${3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME}}" PATH)
134       set (3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME} "${3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME}}" CACHE PATH "The directory containing FFmpeg library (${LIBRARY_NAME})" FORCE)
135     else()
136       set (3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME} "" CACHE PATH "The directory containing FFmpeg library (${LIBRARY_NAME})" FORCE)
137     endif()
138   endif()
139
140   if (3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME} AND EXISTS "${3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME}}")
141     list (APPEND 3RDPARTY_LIBRARY_DIRS "${3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME}}")
142   else()
143     list (APPEND 3RDPARTY_NO_LIBS 3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME})
144
145     set (3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME} "" CACHE FILEPATH "The path to FFmpeg library (${LIBRARY_NAME})" FORCE)
146   endif()
147
148   # shared library
149   if (WIN32)
150     if (NOT 3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME} OR NOT EXISTS "${3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}}")
151
152       set (CMAKE_FIND_LIBRARY_SUFFIXES .dll)
153       set (3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME} "3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}-NOTFOUND" CACHE FILEPATH "The path to FFmpeg shared library (${LIBRARY_NAME})" FORCE)
154
155       # find FFmpeg shared library
156       file (GLOB 3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME} "${3RDPARTY_FFMPEG_DIR}/bin/${LIBRARY_NAME}[-][0-9]*")
157
158       if (3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME} AND EXISTS "${3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}}")
159         set (3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME} "${3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}}" CACHE FILEPATH "FFmpeg shared library (${LIBRARY_NAME})" FORCE)
160         get_filename_component (3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME} "${3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}}" PATH)
161         set (3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME} "${3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME}}" CACHE PATH "The directory containing FFmpeg library (${LIBRARY_NAME})" FORCE)
162       else()
163         set (3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME} "" CACHE PATH "The directory containing FFmpeg shared library (${LIBRARY_NAME})" FORCE)
164       endif()
165     endif()
166
167     if (3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME} OR EXISTS "${3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME}}")
168       list (APPEND 3RDPARTY_DLL_DIRS "${3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME}}")
169     else()
170       list (APPEND 3RDPARTY_NO_DLLS 3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME})
171     endif()
172   endif()
173
174   # install instructions
175   if (INSTALL_FFMPEG)
176     OCCT_MAKE_OS_WITH_BITNESS()
177     OCCT_MAKE_COMPILER_SHORT_NAME()
178
179     if (WIN32)
180       if (DEFINED INSTALL_BIN_DIR)
181         install (FILES "${3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}}" DESTINATION "${INSTALL_BIN_DIR}")
182       else()
183         install (FILES "${3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}}"
184                  CONFIGURATIONS Release
185                  DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/bin")
186         install (FILES "${3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}}"
187                  CONFIGURATIONS RelWithDebInfo
188                  DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/bini")
189         install (FILES "${3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME}}"
190                  CONFIGURATIONS Debug
191                  DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/bind")
192       endif()
193     else()
194       get_filename_component(3RDPARTY_FFMPEG_LIBRARY_ABS ${3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME}} REALPATH)
195       get_filename_component(3RDPARTY_FFMPEG_LIBRARY_NAME ${3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME}} NAME)
196
197       if (DEFINED INSTALL_LIB_DIR)
198         install (FILES "${3RDPARTY_FFMPEG_LIBRARY_ABS}"
199                  DESTINATION "${INSTALL_LIB_DIR}"
200                  RENAME ${3RDPARTY_FFMPEG_LIBRARY_NAME}.6)
201       else()
202         install (FILES "${3RDPARTY_FFMPEG_LIBRARY_ABS}"
203                  CONFIGURATIONS Release
204                  DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/lib"
205                  RENAME ${3RDPARTY_FFMPEG_LIBRARY_NAME}.6)
206         install (FILES "${3RDPARTY_FFMPEG_LIBRARY_ABS}"
207                  CONFIGURATIONS RelWithDebInfo
208                  DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/libi"
209                  RENAME ${3RDPARTY_FFMPEG_LIBRARY_NAME}.6)
210         install (FILES "${3RDPARTY_FFMPEG_LIBRARY_ABS}"
211                  CONFIGURATIONS Debug
212                  DESTINATION "${INSTALL_DIR}/${OS_WITH_BIT}/${COMPILER}/libd"
213                  RENAME ${3RDPARTY_FFMPEG_LIBRARY_NAME}.6)
214       endif()
215     endif()
216
217     set (USED_3RDPARTY_FFMPEG_DIR "")
218   else()
219     # the library directory for using by the executable
220     if (WIN32)
221       set (USED_3RDPARTY_FFMPEG_DIR ${3RDPARTY_FFMPEG_DLL_DIR_${LIBRARY_NAME}})
222     else()
223       set (USED_3RDPARTY_FFMPEG_DIR ${3RDPARTY_FFMPEG_LIBRARY_DIR_${LIBRARY_NAME}})
224     endif()
225   endif()
226
227   mark_as_advanced (3RDPARTY_FFMPEG_LIBRARY_${LIBRARY_NAME} 3RDPARTY_FFMPEG_DLL_${LIBRARY_NAME})
228 endforeach()
229
230 # unset all redundant variables
231 OCCT_CHECK_AND_UNSET (FFMPEG_INCLUDE_DIRS)
232 OCCT_CHECK_AND_UNSET (FFMPEG_LIBRARY_DIRS)
233 OCCT_CHECK_AND_UNSET (FFMPEG_DIR)