option(COMPILE_CL_CHECKS "Compile CL checks" OFF)
option(COMPILE_PCL_TOOLS "Compile PCL-based validation tools" ON)

add_executable(rseqsliceprobe rseqsliceprobe.cpp)

if(COMPILE_CL_CHECKS)
	# Find OpenCL: try find_package first, fall back to pkg-config
	find_package(OpenCL 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")
	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")
	endif()

	add_executable(clhostshmemptrcheck clhostshmemptrcheck.cpp)
	target_include_directories(clhostshmemptrcheck
		PUBLIC ${OPENCL_INCLUDE_DIRS})
	target_link_libraries(clhostshmemptrcheck
		${OPENCL_LIBRARIES})
	add_executable(clshmemlatency clshmemlatency.cpp)
	target_include_directories(clshmemlatency
		PUBLIC ${OPENCL_INCLUDE_DIRS})
	target_link_libraries(clshmemlatency
		${OPENCL_LIBRARIES})
	add_executable(clshmemlatency_callback clshmemlatency_callback.cpp)
	target_include_directories(clshmemlatency_callback
		PUBLIC ${OPENCL_INCLUDE_DIRS})
	target_link_libraries(clshmemlatency_callback
		${OPENCL_LIBRARIES})
	add_executable(clshmemcheck clshmemcheck.cpp)
	target_include_directories(clshmemcheck
		PUBLIC ${OPENCL_INCLUDE_DIRS})
	target_link_libraries(clshmemcheck
		${OPENCL_LIBRARIES})
	add_executable(clzerocopycheck clzerocopycheck.cpp)
	target_include_directories(clzerocopycheck
		PUBLIC ${OPENCL_INCLUDE_DIRS})
	target_link_libraries(clzerocopycheck
		${OPENCL_LIBRARIES})
endif()

if(COMPILE_PCL_TOOLS)
	enable_language(C)
	find_package(MPI REQUIRED COMPONENTS C)
	find_package(PCL QUIET COMPONENTS common io surface features search kdtree)
	if(PCL_FOUND)
		add_executable(meshFromPcd meshFromPcd.cpp)
		target_include_directories(meshFromPcd PUBLIC ${PCL_INCLUDE_DIRS})
		target_link_directories(meshFromPcd PUBLIC ${PCL_LIBRARY_DIRS})
		target_link_libraries(meshFromPcd ${PCL_LIBRARIES})
		target_compile_options(meshFromPcd PRIVATE ${PCL_DEFINITIONS})
	else()
		message(WARNING "PCL not found; skipping meshFromPcd build")
	endif()
endif()
