File FindFFmpeg.cmake of Package decaf-emu

# FindFFMPEG
# ----------
#
# Find the native FFMPEG includes and libraries
#
# This module defines the following variables:
#
#  FFMPEG_INCLUDE_<component>: where to find <component>.h
#  FFMPEG_LIBRARY_<component>: where to find the <component> library
#  FFMPEG_INCLUDES: aggregate all the include paths
#  FFMPEG_LIBRARIES: aggregate all the paths to the libraries
#  FFMPEG_FOUND: True if all components have been found
#
# This module defines the following targets, which are prefered over variables:
#
#  FFMPEG::<component>: Target to use <component> directly, with include path,
#    library and dependencies set up. If you are using a static build, you are
#    responsible for adding any external dependencies (such as zlib, bzlib...).
#
# <component> can be one of:
#   AVCODEC
#   avdevice
#   AVFILTER
#   avformat
#   postproc
#   swresample
#   SWSCALE
#
#  Modified for decaf-emu by XenonPK <pousaduarte@gmail.com>

set(_FFMPEG_ALL_COMPONENTS
  AVCODEC
  #avdevice
  AVFILTER
  #avformat
  AVUTIL
  #postproc
  #swresample
  SWSCALE
)

set(_FFMPEG_DEPS_AVCODEC AVUTIL)
#set(_FFMPEG_DEPS_avdevice AVCODEC avformat AVUTIL)
set(_FFMPEG_DEPS_AVFILTER AVUTIL)
#set(_FFMPEG_DEPS_avformat AVCODEC AVUTIL)
#set(_FFMPEG_DEPS_postproc AVUTIL)
#set(_FFMPEG_DEPS_swresample AVUTIL)
set(_FFMPEG_DEPS_SWSCALE AVUTIL)

function(find_ffmpeg LIBNAME)
  string(TOLOWER ${LIBNAME} PROPER_LIBNAME)
  if(DEFINED ENV{FFMPEG_DIR})
    set(FFMPEG_DIR $ENV{FFMPEG_DIR})
  endif()

  if(FFMPEG_DIR)
    list(APPEND INCLUDE_PATHS
      ${FFMPEG_DIR}
      ${FFMPEG_DIR}/ffmpeg
      ${FFMPEG_DIR}/lib${PROPER_LIBNAME}
      ${FFMPEG_DIR}/include/lib${PROPER_LIBNAME}
      ${FFMPEG_DIR}/include/ffmpeg
      ${FFMPEG_DIR}/include
      NO_DEFAULT_PATH
      NO_CMAKE_FIND_ROOT_PATH
    )
    list(APPEND LIB_PATHS
      ${FFMPEG_DIR}
      ${FFMPEG_DIR}/lib
      ${FFMPEG_DIR}/lib${PROPER_LIBNAME}
      NO_DEFAULT_PATH
      NO_CMAKE_FIND_ROOT_PATH
    )
  else()
    list(APPEND INCLUDE_PATHS
      /usr/local/include/ffmpeg
      /usr/local/include/lib${PROPER_LIBNAME}
      /usr/include/ffmpeg
      /usr/include/lib${PROPER_LIBNAME}
      /usr/include/ffmpeg/lib${PROPER_LIBNAME}
    )

    list(APPEND LIB_PATHS
      /usr/local/lib
      /usr/lib
    )
  endif()

  find_path(FFMPEG_INCLUDE_${LIBNAME} lib${PROPER_LIBNAME}/${PROPER_LIBNAME}.h
    HINTS ${INCLUDE_PATHS}
  )

  find_library(FFMPEG_LIBRARY_${LIBNAME} ${PROPER_LIBNAME}
    HINTS ${LIB_PATHS}
  )

  if(NOT FFMPEG_DIR AND (NOT FFMPEG_LIBRARY_${LIBNAME} OR NOT FFMPEG_INCLUDE_${LIBNAME}))
    # Didn't find it in the usual paths, try pkg-config
    find_package(PkgConfig QUIET)
    pkg_check_modules(FFMPEG_PKGCONFIG_${LIBNAME} REQUIRED QUIET lib${PROPER_LIBNAME})

    find_path(FFMPEG_INCLUDE_${LIBNAME} lib${PROPER_LIBNAME}/${PROPER_LIBNAME}.h
      ${FFMPEG_PKGCONFIG_${LIBNAME}_INCLUDE_DIRS}
    )

    find_library(FFMPEG_LIBRARY_${LIBNAME} ${PROPER_LIBNAME}
      ${FFMPEG_PKGCONFIG_${LIBNAME}_LIBRARY_DIRS}
    )
  endif()

  if(FFMPEG_INCLUDE_${LIBNAME} AND FFMPEG_LIBRARY_${LIBNAME})
    set(FFMPEG_INCLUDE_${LIBNAME} "${FFMPEG_INCLUDE_${LIBNAME}}" PARENT_SCOPE)
    set(FFMPEG_LIBRARY_${LIBNAME} "${FFMPEG_LIBRARY_${LIBNAME}}" PARENT_SCOPE)
    set(FFMPEG_${c}_FOUND TRUE PARENT_SCOPE)
    if(NOT FFMPEG_FIND_QUIETLY)
      message("--  Found ${LIBNAME}: ${FFMPEG_INCLUDE_${LIBNAME}} ${FFMPEG_LIBRARY_${LIBNAME}}")
    endif()
  endif()
endfunction()

foreach(c ${_FFMPEG_ALL_COMPONENTS})
  find_ffmpeg(${c})
endforeach()

foreach(c ${_FFMPEG_ALL_COMPONENTS})
  if(FFMPEG_${c}_FOUND)
    list(APPEND FFMPEG_INCLUDES ${FFMPEG_INCLUDE_${c}})
    list(APPEND FFMPEG_LIBRARIES ${FFMPEG_LIBRARY_${c}})

    add_library(FFMPEG::${c} IMPORTED UNKNOWN)
    set_target_properties(FFMPEG::${c} PROPERTIES
      IMPORTED_LOCATION ${FFMPEG_LIBRARY_${c}}
    )
    if(_FFMPEG_DEPS_${c})
      set(deps)
      foreach(dep ${_FFMPEG_DEPS_${c}})
        list(APPEND deps FFMPEG::${dep})
      endforeach()

      set_target_properties(FFMPEG::${c} PROPERTIES
        INTERFACE_LINK_LIBRARIES "${deps}"
      )
      unset(deps)
    endif()
  endif()
endforeach()

list(REMOVE_DUPLICATES FFMPEG_INCLUDES)

foreach(c ${FFMPEG_FIND_COMPONENTS})
  list(APPEND _FFMPEG_REQUIRED_VARS FFMPEG_INCLUDE_${c} FFMPEG_LIBRARY_${c})
endforeach()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FFMPEG
  REQUIRED_VARS ${_FFMPEG_REQUIRED_VARS}
  HANDLE_COMPONENTS
)

foreach(c ${_FFMPEG_ALL_COMPONENTS})
  unset(_FFMPEG_DEPS_${c})
endforeach()
unset(_FFMPEG_ALL_COMPONENTS)
unset(_FFMPEG_REQUIRED_VARS)
openSUSE Build Service is sponsored by