Simplify some vector size range checks
[openal-soft.git] / cmake / FindFFmpeg.cmake
blob96cbb6ed021dfbf50c1b0f04768c6b9f7699dffa
1 # vim: ts=2 sw=2
2 # - Try to find the required ffmpeg components(default: AVFORMAT, AVUTIL, AVCODEC)
4 # Once done this will define
5 #  FFMPEG_FOUND         - System has the all required components.
6 #  FFMPEG_INCLUDE_DIRS  - Include directory necessary for using the required components headers.
7 #  FFMPEG_LIBRARIES     - Link these to use the required ffmpeg components.
8 #  FFMPEG_DEFINITIONS   - Compiler switches required for using the required ffmpeg components.
10 # For each of the components it will additionaly set.
11 #   - AVCODEC
12 #   - AVDEVICE
13 #   - AVFORMAT
14 #   - AVUTIL
15 #   - POSTPROC
16 #   - SWSCALE
17 #   - SWRESAMPLE
18 # the following variables will be defined
19 #  <component>_FOUND        - System has <component>
20 #  <component>_INCLUDE_DIRS - Include directory necessary for using the <component> headers
21 #  <component>_LIBRARIES    - Link these to use <component>
22 #  <component>_DEFINITIONS  - Compiler switches required for using <component>
23 #  <component>_VERSION      - The components version
25 # Copyright (c) 2006, Matthias Kretz, <kretz@kde.org>
26 # Copyright (c) 2008, Alexander Neundorf, <neundorf@kde.org>
27 # Copyright (c) 2011, Michael Jansen, <kde@michael-jansen.biz>
29 # Redistribution and use is allowed according to the terms of the BSD license.
31 include(FindPackageHandleStandardArgs)
33 if(NOT FFmpeg_FIND_COMPONENTS)
34     set(FFmpeg_FIND_COMPONENTS AVFORMAT AVCODEC AVUTIL)
35 endif()
38 ### Macro: set_component_found
40 # Marks the given component as found if both *_LIBRARIES AND *_INCLUDE_DIRS is present.
42 macro(set_component_found _component)
43     if(${_component}_LIBRARIES AND ${_component}_INCLUDE_DIRS)
44         # message(STATUS "  - ${_component} found.")
45         set(${_component}_FOUND TRUE)
46     else()
47         # message(STATUS "  - ${_component} not found.")
48     endif()
49 endmacro()
52 ### Macro: find_component
54 # Checks for the given component by invoking pkgconfig and then looking up the libraries and
55 # include directories.
57 macro(find_component _component _pkgconfig _library _header)
58     if(NOT WIN32)
59         # use pkg-config to get the directories and then use these values
60         # in the FIND_PATH() and FIND_LIBRARY() calls
61         find_package(PkgConfig)
62         if(PKG_CONFIG_FOUND)
63             pkg_check_modules(PC_${_component} ${_pkgconfig})
64         endif()
65     endif()
67     find_path(${_component}_INCLUDE_DIRS ${_header}
68         HINTS
69             ${FFMPEGSDK_INC}
70             ${PC_LIB${_component}_INCLUDEDIR}
71             ${PC_LIB${_component}_INCLUDE_DIRS}
72         PATH_SUFFIXES
73             ffmpeg
74     )
76     find_library(${_component}_LIBRARIES NAMES ${_library}
77         HINTS
78             ${FFMPEGSDK_LIB}
79             ${PC_LIB${_component}_LIBDIR}
80             ${PC_LIB${_component}_LIBRARY_DIRS}
81     )
83     STRING(REGEX REPLACE "/.*" "/version.h" _ver_header ${_header})
84     if(EXISTS "${${_component}_INCLUDE_DIRS}/${_ver_header}")
85         file(STRINGS "${${_component}_INCLUDE_DIRS}/${_ver_header}" version_str REGEX "^#define[\t ]+LIB${_component}_VERSION_M.*")
87         foreach(_str "${version_str}")
88             if(NOT version_maj)
89                 string(REGEX REPLACE "^.*LIB${_component}_VERSION_MAJOR[\t ]+([0-9]*).*$" "\\1" version_maj "${_str}")
90             endif()
91             if(NOT version_min)
92                 string(REGEX REPLACE "^.*LIB${_component}_VERSION_MINOR[\t ]+([0-9]*).*$" "\\1" version_min "${_str}")
93             endif()
94             if(NOT version_mic)
95                 string(REGEX REPLACE "^.*LIB${_component}_VERSION_MICRO[\t ]+([0-9]*).*$" "\\1" version_mic "${_str}")
96             endif()
97         endforeach()
98         unset(version_str)
100         set(${_component}_VERSION "${version_maj}.${version_min}.${version_mic}" CACHE STRING "The ${_component} version number.")
101         unset(version_maj)
102         unset(version_min)
103         unset(version_mic)
104     endif(EXISTS "${${_component}_INCLUDE_DIRS}/${_ver_header}")
105     set(${_component}_VERSION     ${PC_${_component}_VERSION}      CACHE STRING "The ${_component} version number.")
106     set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.")
108     set_component_found(${_component})
110     mark_as_advanced(
111         ${_component}_INCLUDE_DIRS
112         ${_component}_LIBRARIES
113         ${_component}_DEFINITIONS
114         ${_component}_VERSION)
115 endmacro()
118 set(FFMPEGSDK $ENV{FFMPEG_HOME})
119 if(FFMPEGSDK)
120     set(FFMPEGSDK_INC "${FFMPEGSDK}/include")
121     set(FFMPEGSDK_LIB "${FFMPEGSDK}/lib")
122 endif()
124 # Check for all possible components.
125 find_component(AVCODEC    libavcodec    avcodec    libavcodec/avcodec.h)
126 find_component(AVFORMAT   libavformat   avformat   libavformat/avformat.h)
127 find_component(AVDEVICE   libavdevice   avdevice   libavdevice/avdevice.h)
128 find_component(AVUTIL     libavutil     avutil     libavutil/avutil.h)
129 find_component(SWSCALE    libswscale    swscale    libswscale/swscale.h)
130 find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h)
131 find_component(POSTPROC   libpostproc   postproc   libpostproc/postprocess.h)
133 # Check if the required components were found and add their stuff to the FFMPEG_* vars.
134 foreach(_component ${FFmpeg_FIND_COMPONENTS})
135     if(${_component}_FOUND)
136         # message(STATUS "Required component ${_component} present.")
137         set(FFMPEG_LIBRARIES   ${FFMPEG_LIBRARIES}   ${${_component}_LIBRARIES})
138         set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS})
139         list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
140     else()
141         # message(STATUS "Required component ${_component} missing.")
142     endif()
143 endforeach()
145 # Build the include path and library list with duplicates removed.
146 if(FFMPEG_INCLUDE_DIRS)
147     list(REMOVE_DUPLICATES FFMPEG_INCLUDE_DIRS)
148 endif()
150 if(FFMPEG_LIBRARIES)
151     list(REMOVE_DUPLICATES FFMPEG_LIBRARIES)
152 endif()
154 # cache the vars.
155 set(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} CACHE STRING "The FFmpeg include directories." FORCE)
156 set(FFMPEG_LIBRARIES    ${FFMPEG_LIBRARIES}    CACHE STRING "The FFmpeg libraries." FORCE)
157 set(FFMPEG_DEFINITIONS  ${FFMPEG_DEFINITIONS}  CACHE STRING "The FFmpeg cflags." FORCE)
159 mark_as_advanced(FFMPEG_INCLUDE_DIRS FFMPEG_LIBRARIES FFMPEG_DEFINITIONS)
161 # Now set the noncached _FOUND vars for the components.
162 foreach(_component AVCODEC AVDEVICE AVFORMAT AVUTIL POSTPROCESS SWRESAMPLE SWSCALE)
163     set_component_found(${_component})
164 endforeach ()
166 # Compile the list of required vars
167 set(_FFmpeg_REQUIRED_VARS FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS)
168 foreach(_component ${FFmpeg_FIND_COMPONENTS})
169     list(APPEND _FFmpeg_REQUIRED_VARS ${_component}_LIBRARIES ${_component}_INCLUDE_DIRS)
170 endforeach()
172 # Give a nice error message if some of the required vars are missing.
173 find_package_handle_standard_args(FFmpeg DEFAULT_MSG ${_FFmpeg_REQUIRED_VARS})