@@ -7,58 +7,107 @@ find_package(Torch REQUIRED)
77set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Werror ${TORCH_CXX_FLAGS} " )
88find_package (Python3 ${PYTHON_VERSION} EXACT COMPONENTS Development)
99
10- function (make_torchcodec_library library_name ffmpeg_target)
11- set (
12- sources
13- FFMPEGCommon.h
14- FFMPEGCommon.cpp
15- VideoDecoder.h
16- VideoDecoder.cpp
17- VideoDecoderOps.h
18- VideoDecoderOps.cpp
19- DeviceInterface.h
20- )
21- if (ENABLE_CUDA)
22- list (APPEND sources CudaDevice.cpp)
23- else ()
24- list (APPEND sources CPUOnlyDevice.cpp)
25- endif ()
26- add_library (${library_name} SHARED ${sources} )
27- set_property (TARGET ${library_name} PROPERTY CXX_STANDARD 17)
10+ function (make_torchcodec_sublibrary
11+ library_name
12+ sources
13+ dependent_libraries
14+ ffmpeg_include_dirs)
2815
29- target_include_directories (
30- ${library_name}
16+ add_library (${library_name} SHARED ${sources} )
17+ set_target_properties (${library_name} PROPERTIES CXX_STANDARD 17)
18+ target_include_directories (${library_name}
3119 PRIVATE
3220 ./../../../../
3321 "${TORCH_INSTALL_PREFIX} /include"
3422 ${Python3_INCLUDE_DIRS}
23+ ${ffmpeg_include_dirs}
3524 )
3625
37- set (NEEDED_LIBRARIES ${ffmpeg_target} ${TORCH_LIBRARIES}
38- ${Python3_LIBRARIES} )
39- if (ENABLE_CUDA)
40- list (APPEND NEEDED_LIBRARIES
41- ${CUDA_nppi_LIBRARY} ${CUDA_nppicc_LIBRARY} )
42- endif ()
26+ # Avoid adding the "lib" prefix which we already add explicitly.
27+ set_target_properties (${library_name} PROPERTIES PREFIX "" )
28+
4329 target_link_libraries (
4430 ${library_name}
4531 PUBLIC
46- ${NEEDED_LIBRARIES }
32+ ${dependent_libraries }
4733 )
34+ endfunction ()
4835
49- # We already set the library_name to be libtorchcodecN, so we don't want
50- # cmake to add another "lib" prefix. We do it this way because it makes it
51- # easier to find references to libtorchcodec in the code (e.g. via `git
52- # grep`)
53- set_target_properties (${library_name} PROPERTIES PREFIX "" )
36+ function (make_torchcodec_libraries
37+ ffmpeg_major_version
38+ ffmpeg_target
39+ ffmpeg_include_dirs)
40+
41+ # Create libtorchcodec_decoderN.so
42+ set (decoder_library_name "libtorchcodec_decoder${ffmpeg_major_version} " )
43+ set (decoder_sources FFMPEGCommon.cpp VideoDecoder.cpp)
44+
45+ if (ENABLE_CUDA)
46+ list (APPEND decoder_sources CudaDevice.cpp)
47+ else ()
48+ list (APPEND decoder_sources CPUOnlyDevice.cpp)
49+ endif ()
50+
51+ set (decoder_dependent_libraries
52+ ${ffmpeg_target}
53+ ${TORCH_LIBRARIES}
54+ ${Python3_LIBRARIES}
55+ )
56+
57+ if (ENABLE_CUDA)
58+ list (APPEND decoder_dependent_libraries
59+ ${CUDA_nppi_LIBRARY}
60+ ${CUDA_nppicc_LIBRARY}
61+ )
62+ endif ()
63+
64+ make_torchcodec_sublibrary(
65+ "${decoder_library_name} "
66+ "${decoder_sources} "
67+ "${decoder_dependent_libraries} "
68+ "${ffmpeg_include_dirs} "
69+ )
70+
71+ # Create libtorchcodec_custom_opsN.so
72+ set (custom_ops_library_name "libtorchcodec_custom_ops${ffmpeg_major_version} " )
73+ set (custom_ops_sources VideoDecoderOps.cpp)
74+ make_torchcodec_sublibrary(
75+ "${custom_ops_library_name} "
76+ "${custom_ops_sources} "
77+ "${decoder_library_name} "
78+ "${ffmpeg_include_dirs} "
79+ )
80+
81+ # Create libtorchcodec_pybind_opsN.so
82+ set (pybind_ops_library_name "libtorchcodec_pybind_ops${ffmpeg_major_version} " )
83+ set (pybind_ops_sources PyBindOps.cpp)
84+ make_torchcodec_sublibrary(
85+ "${pybind_ops_library_name} "
86+ "${pybind_ops_sources} "
87+ "${decoder_library_name} "
88+ "${ffmpeg_include_dirs} "
89+ )
90+ target_compile_definitions (
91+ ${pybind_ops_library_name}
92+ PUBLIC
93+ TORCHCODEC_PYBIND=_torchcodec_pybind_ops${ffmpeg_major_version}
94+ )
95+
96+ # Install all libraries.
97+ set (
98+ all_libraries
99+ ${decoder_library_name}
100+ ${custom_ops_library_name}
101+ ${pybind_ops_library_name}
102+ )
54103
55104 # The install step is invoked within CMakeBuild.build_library() in
56105 # setup.py and just copies the built .so files from the temp
57106 # cmake/setuptools build folder into the CMAKE_INSTALL_PREFIX folder. We
58107 # still need to manually pass "DESTINATION ..." for cmake to copy those
59108 # files in CMAKE_INSTALL_PREFIX instead of CMAKE_INSTALL_PREFIX/lib.
60109 install (
61- TARGETS ${library_name }
110+ TARGETS ${all_libraries }
62111 LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}
63112 )
64113endfunction ()
@@ -76,11 +125,10 @@ if(DEFINED ENV{BUILD_AGAINST_ALL_FFMPEG_FROM_S3})
76125 ${CMAKE_CURRENT_SOURCE_DIR} /fetch_and_expose_non_gpl_ffmpeg_libs.cmake
77126 )
78127
79-
80- make_torchcodec_library(libtorchcodec4 ffmpeg4)
81- make_torchcodec_library(libtorchcodec7 ffmpeg7)
82- make_torchcodec_library(libtorchcodec6 ffmpeg6)
83- make_torchcodec_library(libtorchcodec5 ffmpeg5)
128+ make_torchcodec_libraries(4 ffmpeg4 $ffmpeg4_INCLUDE_DIRS)
129+ make_torchcodec_libraries(7 ffmpeg7 $ffmpeg7_INCLUDE_DIRs)
130+ make_torchcodec_libraries(6 ffmpeg6 $ffmpeg6_INCLUDE_DIRS)
131+ make_torchcodec_libraries(5 ffmpeg5 $ffmpeg5_INCLUDE_DIRS)
84132
85133else ()
86134 message (
@@ -120,10 +168,11 @@ else()
120168 )
121169 endif ()
122170
123- set (libtorchcodec_target_name libtorchcodec${ffmpeg_major_version} )
124- # Make libtorchcodec_target_name available in the parent's scope, for the
125- # test's CMakeLists.txt
126- set (libtorchcodec_target_name ${libtorchcodec_target_name} PARENT_SCOPE)
171+ make_torchcodec_libraries(${ffmpeg_major_version} PkgConfig::LIBAV ${LIBAV_INCLUDE_DIRS} )
127172
128- make_torchcodec_library(${libtorchcodec_target_name} PkgConfig::LIBAV)
173+ # Expose these values updwards so that the test compilation does not need
174+ # to re-figure it out. FIXME: it's not great that we just copy-paste the
175+ # library name.
176+ set (libtorchcodec_library_name "libtorchcodec_decoder${ffmpeg_major_version} " PARENT_SCOPE)
177+ set (libav_include_dirs ${LIBAV_INCLUDE_DIRS} PARENT_SCOPE)
129178endif ()
0 commit comments