Skip to content

Commit 053a26d

Browse files
committed
Fix custom ffmpeg library/header building
1 parent 50cec73 commit 053a26d

File tree

1 file changed

+85
-55
lines changed

1 file changed

+85
-55
lines changed

cmake/FindFFmpeg.cmake

Lines changed: 85 additions & 55 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,79 @@ 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}
134+
find_path(${_component}_INCLUDE_DIRS ${_header}
135+
PATHS
136+
${PC_FFMPEG_INCLUDE_DIRS}
137+
NO_DEFAULT_PATH
138+
REQUIRED # perhaps?
139139
)
140140

141-
#message(STATUS "L0: ${${_component}_LIBRARIES}")
142-
#message(STATUS "L1: ${PC_${_component}_LIBRARIES}")
143-
#message(STATUS "L2: ${_library}")
141+
if (NOT ("${PC_FFMPEG_LIBRARY_DIRS}" STREQUAL ""))
142+
find_library(${_component}_LIBRARY NAMES ${_library}
143+
HINTS
144+
${PC_FFMPEG_LIBRARY_DIRS}
145+
NO_DEFAULT_PATH # not sure whether this is needed or not
146+
)
147+
set(${_component}_LIBRARY_DIRS ${PC_FFMPEG_LIBRARY_DIRS} CACHE STRING "The ${_component} library dirs.")
148+
set(${_component}_LIBRARIES ${_library} CACHE STRING "The ${_component} libraries.")
149+
else ()
150+
find_library(${_component}_LIBRARY NAMES ${PC_${_component}_LIBRARIES} ${_library}
151+
HINTS
152+
${PC_${_component}_LIBDIR}
153+
${PC_${_component}_LIBRARY_DIRS}
154+
${PC_FFMPEG_LIBRARY_DIRS}
155+
)
156+
157+
#message(STATUS "L0: ${${_component}_LIBRARIES}")
158+
#message(STATUS "L1: ${PC_${_component}_LIBRARIES}")
159+
#message(STATUS "L2: ${_library}")
144160

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.")
161+
set(${_component}_DEFINITIONS ${PC_${_component}_CFLAGS_OTHER} CACHE STRING "The ${_component} CFLAGS.")
162+
set(${_component}_VERSION ${PC_${_component}_VERSION} CACHE STRING "The ${_component} version number.")
163+
set(${_component}_LIBRARY_DIRS ${PC_${_component}_LIBRARY_DIRS} CACHE STRING "The ${_component} library dirs.")
164+
set(${_component}_LIBRARIES ${PC_${_component}_LIBRARIES} CACHE STRING "The ${_component} libraries.")
165+
endif ()
149166

150167
set_component_found(${_component})
151168

152169
mark_as_advanced(
153-
${_component}_LIBRARY
154-
${_component}_INCLUDE_DIRS
155-
${_component}_LIBRARY_DIRS
156-
${_component}_LIBRARIES
157-
${_component}_DEFINITIONS
158-
${_component}_VERSION)
170+
${_component}_LIBRARY
171+
${_component}_INCLUDE_DIRS
172+
${_component}_LIBRARY_DIRS
173+
${_component}_LIBRARIES
174+
${_component}_DEFINITIONS
175+
${_component}_VERSION)
159176

160177
endmacro()
161178

162179

163180
# Check for cached results. If there are skip the costly part.
164181
if (NOT FFMPEG_LIBRARIES)
165182

183+
if (DEFINED PC_FFMPEG_LIBRARY_DIRS AND NOT "${PC_FFMPEG_LIBRARY_DIRS}" STREQUAL "")
184+
set(FFMPEG_LIBRARY_DIRS "${PC_FFMPEG_LIBRARY_DIRS}")
185+
endif()
186+
187+
if (DEFINED PC_FFMPEG_INCLUDE_DIRS AND NOT "${PC_FFMPEG_INCLUDE_DIRS}" STREQUAL "")
188+
set(FFMPEG_INCLUDE_DIRS "${PC_FFMPEG_INCLUDE_DIRS}")
189+
endif()
190+
166191
# 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)
192+
find_component(AVCODEC libavcodec avcodec libavcodec/avcodec.h)
193+
find_component(AVFORMAT libavformat avformat libavformat/avformat.h)
194+
find_component(AVDEVICE libavdevice avdevice libavdevice/avdevice.h)
195+
find_component(AVUTIL libavutil avutil libavutil/avutil.h)
196+
find_component(AVFILTER libavfilter avfilter libavfilter/avfilter.h)
197+
find_component(SWSCALE libswscale swscale libswscale/swscale.h)
198+
find_component(POSTPROC libpostproc postproc libpostproc/postprocess.h)
174199
find_component(SWRESAMPLE libswresample swresample libswresample/swresample.h)
175200

176201
# Check if the required components were found and add their stuff to the FFMPEG_* vars.
@@ -179,25 +204,30 @@ if (NOT FFMPEG_LIBRARIES)
179204
message(STATUS "Libs: ${${_component}_LIBRARIES} | ${PC_${_component}_LIBRARIES}")
180205

181206
# message(STATUS "Required component ${_component} present.")
182-
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${${_component}_LIBRARY} ${${_component}_LIBRARIES})
183-
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS})
207+
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} ${${_component}_LIBRARY} ${${_component}_LIBRARIES})
208+
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} ${${_component}_DEFINITIONS})
184209

185-
list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
186-
list(APPEND FFMPEG_LIBRARY_DIRS ${${_component}_LIBRARY_DIRS})
210+
if (NOT DEFINED PC_FFMPEG_LIBRARY_DIRS OR "${PC_FFMPEG_LIBRARY_DIRS}" STREQUAL "")
211+
list(APPEND FFMPEG_LIBRARY_DIRS ${${_component}_LIBRARY_DIRS})
212+
endif ()
213+
214+
if (NOT DEFINED PC_FFMPEG_INCLUDE_DIRS OR "${PC_FFMPEG_INCLUDE_DIRS}" STREQUAL "")
215+
list(APPEND FFMPEG_INCLUDE_DIRS ${${_component}_INCLUDE_DIRS})
216+
endif ()
187217

188218
string(TOLOWER ${_component} _lowerComponent)
189219
if (NOT TARGET FFmpeg::${_lowerComponent})
190220
add_library(FFmpeg::${_lowerComponent} INTERFACE IMPORTED)
191221
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()
222+
INTERFACE_COMPILE_OPTIONS "${${_component}_DEFINITIONS}"
223+
INTERFACE_INCLUDE_DIRECTORIES ${${_component}_INCLUDE_DIRS}
224+
INTERFACE_LINK_LIBRARIES "${${_component}_LIBRARY} ${${_component}_LIBRARIES} ${PC_${_component}_LIBRARIES}"
225+
INTERFACE_LINK_DIRECTORIES "${PC_${_component}_LIBDIR} ${PC_${_component}_LIBRARY_DIRS} ${PC_FFMPEG_LIBRARY_DIRS}"
226+
IMPORTED_LINK_INTERFACE_MULTIPLICITY 3)
227+
endif ()
228+
else ()
199229
# message(STATUS "Required component ${_component} missing.")
200-
endif()
230+
endif ()
201231
endforeach ()
202232

203233
# Build the include path with duplicates removed.
@@ -207,26 +237,26 @@ if (NOT FFMPEG_LIBRARIES)
207237

208238
# cache the vars.
209239
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)
240+
set(FFMPEG_LIBRARIES ${FFMPEG_LIBRARIES} CACHE STRING "The FFmpeg libraries." FORCE)
241+
set(FFMPEG_DEFINITIONS ${FFMPEG_DEFINITIONS} CACHE STRING "The FFmpeg cflags." FORCE)
212242
set(FFMPEG_LIBRARY_DIRS ${FFMPEG_LIBRARY_DIRS} CACHE STRING "The FFmpeg library dirs." FORCE)
213243

214244
mark_as_advanced(FFMPEG_INCLUDE_DIRS
215-
FFMPEG_LIBRARIES
216-
FFMPEG_DEFINITIONS
217-
FFMPEG_LIBRARY_DIRS)
245+
FFMPEG_LIBRARIES
246+
FFMPEG_DEFINITIONS
247+
FFMPEG_LIBRARY_DIRS)
218248

219249
endif ()
220250

221251
if (NOT TARGET FFmpeg::FFmpeg)
222252
add_library(FFmpeg INTERFACE)
223253
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}")
254+
INTERFACE_COMPILE_OPTIONS "${FFMPEG_DEFINITIONS}"
255+
INTERFACE_INCLUDE_DIRECTORIES ${FFMPEG_INCLUDE_DIRS}
256+
INTERFACE_LINK_LIBRARIES "${FFMPEG_LIBRARIES}"
257+
INTERFACE_LINK_DIRECTORIES "${FFMPEG_LIBRARY_DIRS}")
228258
add_library(FFmpeg::FFmpeg ALIAS FFmpeg)
229-
endif()
259+
endif ()
230260

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

0 commit comments

Comments
 (0)