cmake_dependent_option(ENABLE_STIMBUFFAPI_livoxGen1
	"Enable Livox Gen1 LiDAR stim buff API" ON
	"ENABLE_LIB_livoxProto1" OFF)

if(ENABLE_STIMBUFFAPI_livoxGen1)
	# Set CONFIG variable for config.h
	set(CONFIG_STIMBUFFAPI_LIVOXGEN1_ENABLED 1)

	# Find liburing using pkg-config
	pkg_check_modules(URING REQUIRED liburing)

	# Find OpenCL 1.2 or higher: try find_package first, fall back to pkg-config
	find_package(OpenCL 1.2 QUIET)
	if(OpenCL_FOUND)
		# Normalize find_package variables to match pkg_check_modules naming
		set(OPENCL_FOUND TRUE)
		set(OPENCL_INCLUDE_DIRS ${OpenCL_INCLUDE_DIRS})
		# Handle both OpenCL_LIBRARY (singular) and OpenCL_LIBRARIES (plural)
		if(OpenCL_LIBRARIES)
			set(OPENCL_LIBRARIES ${OpenCL_LIBRARIES})
		else()
			set(OPENCL_LIBRARIES ${OpenCL_LIBRARY})
		endif()
		set(OPENCL_LIBRARY_DIRS "")
		message(STATUS "Found OpenCL using find_package")
		
		# Check if version is available and validate
		if(OpenCL_VERSION)
			if(OpenCL_VERSION VERSION_LESS "1.2")
				message(FATAL_ERROR
					"OpenCL version ${OpenCL_VERSION} found, but 1.2 or higher is required")
			endif()
			message(STATUS "OpenCL version: ${OpenCL_VERSION}")
		else()
			message(WARNING
				"OpenCL version could not be determined. "
				"Version 1.2+ is required at runtime.")
		endif()
	else()
		# Fall back to pkg-config
		pkg_check_modules(OPENCL OpenCL)
		if(NOT OPENCL_FOUND)
			message(FATAL_ERROR
				"Failed to find OpenCL: both find_package and "
				"pkg_check_modules failed. Try installing the "
				"'ocl-icd-opencl-dev' package (or the appropriate "
				"OpenCL development package for your system)."
			)
		endif()
		message(STATUS "Found OpenCL using pkg-config")
		message(WARNING
			"OpenCL version could not be determined via pkg-config. "
			"Version 1.2+ is required at runtime.")
	endif()

	# Enable assembly language
	enable_language(ASM)

	add_library(livoxGen1 SHARED
		livoxGen1.cpp
		stagingBuffer.cpp
		pcloudStimulusProducer.cpp
		ioUringAssemblyEngine.cpp
		openClCollatingAndMeshingEngine.cpp
		openClKernels.cl.S
	)

	# Set assembler working directory so .incbin can find the .cl file
	# Also declare dependency on collateDgrams.cl and slotCompactor.cl
	set_source_files_properties(openClKernels.cl.S PROPERTIES
		COMPILE_FLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}"
		OBJECT_DEPENDS
			"${CMAKE_CURRENT_SOURCE_DIR}/collateDgrams.cl;${CMAKE_CURRENT_SOURCE_DIR}/slotCompactor.cl"
	)

	target_include_directories(livoxGen1 PUBLIC
		${Boost_INCLUDE_DIRS}
		${CMAKE_SOURCE_DIR}/commonLibs
		${URING_INCLUDE_DIRS}
		${OPENCL_INCLUDE_DIRS}
	)
	target_link_libraries(livoxGen1 PUBLIC
		Boost::system
		Boost::log
		${URING_LIBRARIES}
		${OPENCL_LIBRARIES}
		attachmentSupport
	)
	target_link_directories(livoxGen1 PUBLIC
		${URING_LIBRARY_DIRS}
		${OPENCL_LIBRARY_DIRS}
	)

	# Verify Boost dynamic dependencies after build
	add_custom_command(TARGET livoxGen1 POST_BUILD
		COMMAND ${CMAKE_COMMAND} -DVERIFY_FILE="$<TARGET_FILE:livoxGen1>"
			-P ${CMAKE_SOURCE_DIR}/cmake/VerifyBoostDynamic.cmake
		COMMENT "Verifying Boost dynamic dependencies for livoxGen1"
	)

	# Install rules
	install(TARGETS livoxGen1 DESTINATION lib)
endif()
