Skip to content

Commit 90b3b41

Browse files
committed
Fix custom ffmpeg library/header building
1 parent 50cec73 commit 90b3b41

File tree

1 file changed

+80
-56
lines changed

1 file changed

+80
-56
lines changed

cmake/FindFFmpeg.cmake

Lines changed: 80 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
# Sample usage in the Toolchain file:
6969
# find_program(PKG_CONFIG_EXECUTABLE NAMES ${COMPILER_PREFIX}-pkg-config)
7070
#
71-
# It also can be pointed via ENV:
71+
# It also can be pointed via ENV:
7272
# export PKG_CONFIG=...
7373
#
7474
# Env variables of the pkg-config:
@@ -98,7 +98,7 @@ endif ()
9898
#
9999
# Marks the given component as found if both *_LIBRARIES AND *_INCLUDE_DIRS is present.
100100
#
101-
macro(set_component_found _component )
101+
macro(set_component_found _component)
102102
if (${_component}_LIBRARIES AND ${_component}_INCLUDE_DIRS)
103103
# message(STATUS " - ${_component} found.")
104104
set(${_component}_FOUND TRUE)
@@ -123,54 +123,72 @@ macro(find_component _component _pkgconfig _library _header)
123123
endif ()
124124

125125
find_path(${_component}_INCLUDE_DIRS ${_header}
126-
HINTS
127-
${PC_${_component}_INCLUDEDIR}
128-
${PC_${_component}_INCLUDE_DIRS}
129-
${PC_FFMPEG_INCLUDE_DIRS}
130-
PATH_SUFFIXES
131-
ffmpeg
126+
HINTS
127+
${PC_${_component}_INCLUDEDIR}
128+
${PC_${_component}_INCLUDE_DIRS}
129+
${PC_FFMPEG_INCLUDE_DIRS}
130+
PATH_SUFFIXES
131+
ffmpeg
132132
)
133133

134-
find_library(${_component}_LIBRARY NAMES ${PC_${_component}_LIBRARIES} ${_library}
135-
HINTS
136-
${PC_${_component}_LIBDIR}
137-
${PC_${_component}_LIBRARY_DIRS}
138-
${PC_FFMPEG_LIBRARY_DIRS}
139-
)
134+
if (NOT ("${PC_FFMPEG_LIBRARY_DIRS}" STREQUAL ""))
135+
find_library(${_component}_LIBRARY NAMES ${_library}
136+
HINTS
137+
${PC_FFMPEG_LIBRARY_DIRS}
138+
NO_DEFAULT_PATH # not sure whether this is needed or not
139+
)
140+
set(${_component}_LIBRARY_DIRS ${PC_FFMPEG_LIBRARY_DIRS} CACHE STRING "The ${_component} library dirs.")
141+
set(${_component}_LIBRARIES ${_library} CACHE STRING "The ${_component} libraries.")
142+
else ()
143+
find_library(${_component}_LIBRARY NAMES ${PC_${_component}_LIBRARIES} ${_library}
144+
HINTS
145+
${PC_${_component}_LIBDIR}
146+
${PC_${_component}_LIBRARY_DIRS}
147+
${PC_FFMPEG_LIBRARY_DIRS}
148+
)
140149

141-
#message(STATUS "L0: ${${_component}_LIBRARIES}")
142-
#message(STATUS "L1: ${PC_${_component}_LIBRARIES}")
143-
#message(STATUS "L2: ${_library}")
150+
#message(STATUS "L0: ${${_component}_LIBRARIES}")
151+
#message(STATUS "L1: ${PC_${_component}_LIBRARIES}")
152+
#message(STATUS "L2: ${_library}")
144153

145-
set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.")
146-
set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING "The ${_component} version number.")
147-
set(${_component}_LIBRARY_DIRS ${PC_${_component}_LIBRARY_DIRS} CACHE STRING "The ${_component} library dirs.")
148-
set(${_component}_LIBRARIES ${PC_${_component}_LIBRARIES} CACHE STRING "The ${_component} libraries.")
154+
set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.")
155+
set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING "The ${_component} version number.")
156+
set(${_component}_LIBRARY_DIRS ${PC_${_component}_LIBRARY_DIRS} CACHE STRING "The ${_component} library dirs.")
157+
set(${_component}_LIBRARIES ${PC_${_component}_LIBRARIES} CACHE STRING "The ${_component} libraries.")
158+
endif ()
149159

150160
set_component_found(${_component})
151161

152162
mark_as_advanced(
153-
${_component}_LIBRARY
154-
${_component}_INCLUDE_DIRS
155-
${_component}_LIBRARY_DIRS
156-
${_component}_LIBRARIES
157-
${_component}_DEFINITIONS
158-
${_component}_VERSION)
163+
${_component}_LIBRARY
164+
${_component}_INCLUDE_DIRS
165+
${_component}_LIBRARY_DIRS
166+
${_component}_LIBRARIES
167+
${_component}_DEFINITIONS
168+
${_component}_VERSION)
159169

160170
endmacro()
161171

162172

163173
# Check for cached results. If there are skip the costly part.
164174
if (NOT FFMPEG_LIBRARIES)
165175

176+
if (NOT (${PC_FFMPEG_LIBRARY_DIRS} STREQUAL ""))
177+
set(FFMPEG_LIBRARY_DIRS ${PC_FFMPEG_LIBRARY_DIRS})
178+
endif ()
179+
180+
if (NOT (${PC_FFMPEG_INCLUDE_DIRS} STREQUAL ""))
181+
set(FFMPEG_INCLUDE_DIRS ${PC_FFMPEG_INCLUDE_DIRS})
182+
endif ()
183+
166184
# Check for all possible component.
167-
find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
168-
find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
169-
find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
170-
find_component(AVUTIL libavutil avutil libavutil/avutil.h)
171-
find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h)
172-
find_component(SWSCALE libswscale swscale libswscale/swscale.h)
173-
find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h)
185+
find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
186+
find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
187+
find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
188+
find_component(AVUTIL libavutil avutil libavutil/avutil.h)
189+
find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h)
190+
find_component(SWSCALE libswscale swscale libswscale/swscale.h)
191+
find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h)
174192
find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h)
175193

176194
# Check if the required components were found and add their stuff to the FFMPEG_* vars.
@@ -179,25 +197,31 @@ if (NOT FFMPEG_LIBRARIES)
179197
message(STATUS "Libs: ${${_component}_LIBRARIES} | ${PC_${_component}_LIBRARIES}")
180198

181199
# message(STATUS "Required component ${_component} present.")
182-
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${${_component}_LIBRARY} ${${_component}_LIBRARIES})
183-
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS})
200+
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${${_component}_LIBRARY} ${${_component}_LIBRARIES})
201+
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS})
202+
203+
if (${PC_FFMPEG_LIBRARY_DIRS} STREQUAL "")
204+
list(APPEND FFMPEG_LIBRARY_DIRS ${${_component}_LIBRARY_DIRS})
205+
endif ()
206+
207+
if (${PC_FFMPEG_INCLUDE_DIRS} STREQUAL "")
208+
list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
209+
endif ()
184210

185-
list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
186-
list(APPEND FFMPEG_LIBRARY_DIRS ${${_component}_LIBRARY_DIRS})
187211

188212
string(TOLOWER ${_component} _lowerComponent)
189213
if (NOT TARGET FFmpeg::${_lowerComponent})
190214
add_library(FFmpeg::${_lowerComponent} INTERFACE IMPORTED)
191215
set_target_properties(FFmpeg::${_lowerComponent} PROPERTIES
192-
INTERFACE_COMPILE_OPTIONS "${${_component}_DEFINITIONS}"
193-
INTERFACE_INCLUDE_DIRECTORIES ${${_component}_INCLUDE_DIRS}
194-
INTERFACE_LINK_LIBRARIES "${${_component}_LIBRARY} ${${_component}_LIBRARIES} ${PC_${_component}_LIBRARIES}"
195-
INTERFACE_LINK_DIRECTORIES "${PC_${_component}_LIBDIR} ${PC_${_component}_LIBRARY_DIRS} ${PC_FFMPEG_LIBRARY_DIRS}"
196-
IMPORTED_LINK_INTERFACE_MULTIPLICITY 3)
197-
endif()
198-
else()
216+
INTERFACE_COMPILE_OPTIONS "${${_component}_DEFINITIONS}"
217+
INTERFACE_INCLUDE_DIRECTORIES ${${_component}_INCLUDE_DIRS}
218+
INTERFACE_LINK_LIBRARIES "${${_component}_LIBRARY} ${${_component}_LIBRARIES} ${PC_${_component}_LIBRARIES}"
219+
INTERFACE_LINK_DIRECTORIES "${PC_${_component}_LIBDIR} ${PC_${_component}_LIBRARY_DIRS} ${PC_FFMPEG_LIBRARY_DIRS}"
220+
IMPORTED_LINK_INTERFACE_MULTIPLICITY 3)
221+
endif ()
222+
else ()
199223
# message(STATUS "Required component ${_component} missing.")
200-
endif()
224+
endif ()
201225
endforeach ()
202226

203227
# Build the include path with duplicates removed.
@@ -207,26 +231,26 @@ if (NOT FFMPEG_LIBRARIES)
207231

208232
# cache the vars.
209233
set(FFMPEG_INCLUDE_DIRS ${FFMPEG_INCLUDE_DIRS} CACHE STRING "The FFmpeg include directories." FORCE)
210-
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE STRING "The FFmpeg libraries." FORCE)
211-
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} CACHE STRING "The FFmpeg cflags." FORCE)
234+
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE STRING "The FFmpeg libraries." FORCE)
235+
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} CACHE STRING "The FFmpeg cflags." FORCE)
212236
set(FFMPEG_LIBRARY_DIRS ${FFMPEG_LIBRARY_DIRS} CACHE STRING "The FFmpeg library dirs." FORCE)
213237

214238
mark_as_advanced(FFMPEG_INCLUDE_DIRS
215-
FFMPEG_LIBRARIES
216-
FFMPEG_DEFINITIONS
217-
FFMPEG_LIBRARY_DIRS)
239+
FFMPEG_LIBRARIES
240+
FFMPEG_DEFINITIONS
241+
FFMPEG_LIBRARY_DIRS)
218242

219243
endif ()
220244

221245
if (NOT TARGET FFmpeg::FFmpeg)
222246
add_library(FFmpeg INTERFACE)
223247
set_target_properties(FFmpeg PROPERTIES
224-
INTERFACE_COMPILE_OPTIONS "${FFMPEG_DEFINITIONS}"
225-
INTERFACE_INCLUDE_DIRECTORIES ${FFMPEG_INCLUDE_DIRS}
226-
INTERFACE_LINK_LIBRARIES "${FFMPEG_LIBRARIES}"
227-
INTERFACE_LINK_DIRECTORIES "${FFMPEG_LIBRARY_DIRS}")
248+
INTERFACE_COMPILE_OPTIONS "${FFMPEG_DEFINITIONS}"
249+
INTERFACE_INCLUDE_DIRECTORIES ${FFMPEG_INCLUDE_DIRS}
250+
INTERFACE_LINK_LIBRARIES "${FFMPEG_LIBRARIES}"
251+
INTERFACE_LINK_DIRECTORIES "${FFMPEG_LIBRARY_DIRS}")
228252
add_library(FFmpeg::FFmpeg ALIAS FFmpeg)
229-
endif()
253+
endif ()
230254

231255
# Now set the noncached _FOUND vars for the components.
232256
foreach (_component AVCODEC AVDEVICE AVFORMAT AVUTIL POSTPROCESS SWSCALE)

0 commit comments

Comments
 (0)