# SPDX-License-Identifier: BSD-2-Clause
#
# daedalus-v4l2 userspace daemon — CMake build.
#
# Notes:
#   - FFmpeg is dlopen'd at runtime (Option γ), so we link only
#     -ldl + -lpthread.  Headers from libavformat-dev /
#     libavcodec-dev / libavutil-dev are used at compile time
#     for struct definitions.
#   - Strict warnings enforced.

cmake_minimum_required(VERSION 3.20)
project(daedalus_v4l2_daemon C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

if (NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE Release)
endif()

add_compile_options(-Wall -Wextra -Wpedantic
		    -Wno-unused-parameter)

# FFmpeg headers (we dlopen the libs but include the headers for
# struct definitions).  pkg-config is the canonical way.
find_package(PkgConfig REQUIRED)
pkg_check_modules(FFMPEG REQUIRED IMPORTED_TARGET
	libavformat libavcodec libavutil)

# daedalus-fourier — VC VII (V3D) + ARM NEON back-end kernel library.
# Linked statically.  Today only the no-QPU smoke-test path is wired
# (a ctx_create_no_qpu at daemon startup, log-and-destroy at exit);
# follow-up patches (per daedalus-v4l2#11) substitute the
# `daedalus_recipe_dispatch_h264_*` family for libavcodec's per-MB
# pixel primitives, one cycle at a time.
#
# We bypass IMPORTED_TARGET and consume pkg-config's static variables
# (--static --libs path) directly so we control the link order:
# libdaedalus_core.a must precede -lvulkan because the static archive
# references vulkan symbols and the linker resolves left-to-right.
pkg_check_modules(DAEDALUS_FOURIER REQUIRED daedalus-fourier)
find_package(Vulkan REQUIRED)

add_executable(daedalus_v4l2_daemon
	src/main.c
	src/ffmpeg_loader.c
	src/log.c
	src/parser.c
	src/decoder.c
	src/chardev_client.c
	src/dmabuf_capture.c
	src/bitstream_writer.c
	src/h264_nal_synth.c
	src/av1_obu_synth.c
)

target_include_directories(daedalus_v4l2_daemon
	PRIVATE
		src
		${CMAKE_CURRENT_SOURCE_DIR}/../include
		${FFMPEG_INCLUDE_DIRS}
		${DAEDALUS_FOURIER_INCLUDE_DIRS}
)

# dl for dlopen, pthread for future threading work.
target_link_directories(daedalus_v4l2_daemon
	PRIVATE
		${DAEDALUS_FOURIER_LIBRARY_DIRS}
)

target_link_libraries(daedalus_v4l2_daemon
	PRIVATE
		dl
		pthread
		# Order matters: libdaedalus_core.a first (so its undefined
		# vulkan symbols register), then -lvulkan to satisfy them.
		${DAEDALUS_FOURIER_LIBRARIES}
		Vulkan::Vulkan
)

install(TARGETS daedalus_v4l2_daemon
	RUNTIME DESTINATION /usr/local/bin)

# --- Unit tests (opt-in) -------------------------------------------------
#
# DAEDALUS_BUILD_TESTS=ON enables standalone test executables that run on
# the build host (no V4L2 / FFmpeg / Vulkan dependency).  Used by CI to
# gate bitstream synthesis modules against regressions.

option(DAEDALUS_BUILD_TESTS "Build daemon unit tests" OFF)

if (DAEDALUS_BUILD_TESTS)
	add_executable(test_av1_obu_synth
		src/test_av1_obu_synth.c
		src/av1_obu_synth.c
		src/bitstream_writer.c
	)
	target_include_directories(test_av1_obu_synth PRIVATE src)
	# Test binary does not link FFmpeg / Vulkan / dl — it exercises
	# pure-C encoders against in-memory inputs.

	enable_testing()
	add_test(NAME av1_obu_synth COMMAND test_av1_obu_synth)
endif()
