option(COMPILE_CL_CHECKS "Compile CL checks" OFF)

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()
