cmake_minimum_required(VERSION 3.18)
project(ntsc_encoder LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)

# We only need cv::Mat, so link against OpenCV *core* only.
# This avoids pulling in highgui (which can clash with the OpenCV bundled in opencv-python/cv2).
find_package(OpenCV REQUIRED COMPONENTS core)

# NOTE: wrapper.cpp currently includes encoder.cpp, so encoder.cpp must NOT be
# compiled as a separate translation unit (otherwise you get duplicate symbols).
pybind11_add_module(ntsc_encoder src/wrapper.cpp)

if (TARGET OpenCV::opencv_core)
  target_link_libraries(ntsc_encoder PRIVATE OpenCV::opencv_core)
elseif (TARGET opencv_core)
  target_link_libraries(ntsc_encoder PRIVATE opencv_core)
else()
  # Fallback (should still only contain core due to COMPONENTS core)
  target_link_libraries(ntsc_encoder PRIVATE ${OpenCV_LIBS})
endif()

# scikit-build-core builds wheels from CMake install() output.
# The extension must be installed into the Python platlib directory.
# (SKBUILD_PLATLIB_DIR is provided by scikit-build-core during builds.)
if (DEFINED SKBUILD_PLATLIB_DIR)
  set(_PY_PLATLIB_DIR "${SKBUILD_PLATLIB_DIR}")
else()
  # Fallback for manual CMake builds.
  set(_PY_PLATLIB_DIR ".")
endif()

install(TARGETS ntsc_encoder
  LIBRARY DESTINATION "${_PY_PLATLIB_DIR}"
  RUNTIME DESTINATION "${_PY_PLATLIB_DIR}"
)
