From 1d94b4100c42e2a285e469a9bc30c5e9f9c8d28c Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <vital.had@gmail.com>
Date: Mon, 2 Mar 2026 02:28:28 +0800
Subject: [PATCH 4/7] Allow build on Apple with generic CL

---
 include/CL/cl.h        | 4 ++--
 include/CL/cl.hpp      | 7 +++++--
 include/CL/cl_ext.h    | 7 +++++--
 include/CL/cl_gl.h     | 4 ++--
 include/CL/cl_gl_ext.h | 2 +-
 include/CL/opencl.h    | 2 +-
 samples/cldemo.cpp     | 2 +-
 samples/clgalaxy.cpp   | 2 +-
 samples/cllist.cpp     | 2 +-
 samples/cltest.cpp     | 2 +-
 samples/errors.cpp     | 2 +-
 samples/errors.h       | 2 +-
 src/dispatch.h         | 2 +-
 src/icd/icd_loader.h   | 2 +-
 src/platform.cpp       | 2 +-
 15 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/include/CL/cl.h b/include/CL/cl.h
index ca0a3b8..9eb16f5 100644
--- a/include/CL/cl.h
+++ b/include/CL/cl.h
@@ -24,11 +24,11 @@
 #ifndef __OPENCL_CL_H
 #define __OPENCL_CL_H
 
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl_platform.h>
 #else
 #include <CL/cl_platform.h>
-#endif	
+#endif
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/include/CL/cl.hpp b/include/CL/cl.hpp
index 7365c2b..8f5512f 100644
--- a/include/CL/cl.hpp
+++ b/include/CL/cl.hpp
@@ -174,15 +174,18 @@
 //#endif // #if !defined(CL_USE_DEPRECATED_OPENCL_1_1_APIS)
 
 
-#if defined(__APPLE__) || defined(__MACOSX)
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenGL/OpenGL.h>
 #include <OpenCL/opencl.h>
-#include <libkern/OSAtomic.h>
 #else
 #include <GL/gl.h>
 #include <CL/opencl.h>
 #endif // !__APPLE__
 
+#ifdef __APPLE__
+#include <libkern/OSAtomic.h>
+#endif
+
 // To avoid accidentally taking ownership of core OpenCL types
 // such as cl_kernel constructors are made explicit
 // under OpenCL 1.2
diff --git a/include/CL/cl_ext.h b/include/CL/cl_ext.h
index 632cb21..2270690 100644
--- a/include/CL/cl_ext.h
+++ b/include/CL/cl_ext.h
@@ -33,13 +33,16 @@
 extern "C" {
 #endif
 
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 	#include <OpenCL/cl.h>
-    #include <AvailabilityMacros.h>
 #else
 	#include <CL/cl.h>
 #endif
 
+#ifdef __APPLE__
+	#include <AvailabilityMacros.h>
+#endif
+
 /* cl_khr_fp16 extension - no extension #define since it has no functions  */
 #define CL_DEVICE_HALF_FP_CONFIG                    0x1033
 
diff --git a/include/CL/cl_gl.h b/include/CL/cl_gl.h
index eb9ce5c..2a40fd5 100644
--- a/include/CL/cl_gl.h
+++ b/include/CL/cl_gl.h
@@ -24,11 +24,11 @@
 #ifndef __OPENCL_CL_GL_H
 #define __OPENCL_CL_GL_H
 
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl.h>
 #else
 #include <CL/cl.h>
-#endif	
+#endif
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/include/CL/cl_gl_ext.h b/include/CL/cl_gl_ext.h
index 77d5353..be60011 100644
--- a/include/CL/cl_gl_ext.h
+++ b/include/CL/cl_gl_ext.h
@@ -33,7 +33,7 @@
 extern "C" {
 #endif
 
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
     #include <OpenCL/cl_gl.h>
 #else
     #include <CL/cl_gl.h>
diff --git a/include/CL/opencl.h b/include/CL/opencl.h
index 3f00524..784fd9c 100644
--- a/include/CL/opencl.h
+++ b/include/CL/opencl.h
@@ -30,7 +30,7 @@
 extern "C" {
 #endif
 
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 
 #include <OpenCL/cl.h>
 #include <OpenCL/cl_gl.h>
diff --git a/samples/cldemo.cpp b/samples/cldemo.cpp
index ea6c416..006ad78 100644
--- a/samples/cldemo.cpp
+++ b/samples/cldemo.cpp
@@ -18,7 +18,7 @@
 #define __CL_ENABLE_EXCEPTIONS
 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS
 
-#if defined(__APPLE__) || defined(__MACOSX)
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl.hpp>
 #else
 #include <CL/cl.hpp>
diff --git a/samples/clgalaxy.cpp b/samples/clgalaxy.cpp
index 0475e04..80092b6 100644
--- a/samples/clgalaxy.cpp
+++ b/samples/clgalaxy.cpp
@@ -18,7 +18,7 @@
 #define __CL_ENABLE_EXCEPTIONS
 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS
 
-#if defined(__APPLE__) || defined(__MACOSX)
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl.hpp>
 #else
 #include <CL/cl.hpp>
diff --git a/samples/cllist.cpp b/samples/cllist.cpp
index 95f63da..032885d 100644
--- a/samples/cllist.cpp
+++ b/samples/cllist.cpp
@@ -18,7 +18,7 @@
 #define __CL_ENABLE_EXCEPTIONS
 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS
 
-#if defined(__APPLE__) || defined(__MACOSX)
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl.hpp>
 #else
 #include <CL/cl.hpp>
diff --git a/samples/cltest.cpp b/samples/cltest.cpp
index c791c85..d035cb1 100644
--- a/samples/cltest.cpp
+++ b/samples/cltest.cpp
@@ -18,7 +18,7 @@
 #define __CL_ENABLE_EXCEPTIONS
 #define CL_USE_DEPRECATED_OPENCL_1_1_APIS
 
-#if defined(__APPLE__) || defined(__MACOSX)
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl.hpp>
 #else
 #include <CL/cl.hpp>
diff --git a/samples/errors.cpp b/samples/errors.cpp
index 184551d..9549d0e 100644
--- a/samples/errors.cpp
+++ b/samples/errors.cpp
@@ -15,7 +15,7 @@
 	You should have received a copy of the GNU Lesser General Public License
 	along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
-#if defined(__APPLE__) || defined(__MACOSX)
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl_ext.h>
 #else
 #include <CL/cl_ext.h>
diff --git a/samples/errors.h b/samples/errors.h
index 08642c2..b804422 100644
--- a/samples/errors.h
+++ b/samples/errors.h
@@ -18,7 +18,7 @@
 #ifndef __ERRORS_H__
 #define __ERRORS_H__
 
-#if defined(__APPLE__) || defined(__MACOSX)
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl_platform.h>
 #else
 #include <CL/cl_platform.h>
diff --git a/src/dispatch.h b/src/dispatch.h
index 96fba7c..2f972ed 100644
--- a/src/dispatch.h
+++ b/src/dispatch.h
@@ -18,7 +18,7 @@
 #ifndef __FREEOCL_DISPATCH_H__
 #define __FREEOCL_DISPATCH_H__
 
-#if defined(__APPLE__) || defined(__MACOSX)
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl.h>
 #else
 #include <CL/cl.h>
diff --git a/src/icd/icd_loader.h b/src/icd/icd_loader.h
index 7002662..cfd239e 100644
--- a/src/icd/icd_loader.h
+++ b/src/icd/icd_loader.h
@@ -18,7 +18,7 @@
 #ifndef __FREEOCL_ICD_LOADER_H__
 #define __FREEOCL_ICD_LOADER_H__
 
-#if defined(__APPLE__) || defined(__MACOSX)
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl_ext.h>
 #else
 #include <CL/cl_ext.h>
diff --git a/src/platform.cpp b/src/platform.cpp
index fc13a2b..20754ac 100644
--- a/src/platform.cpp
+++ b/src/platform.cpp
@@ -15,7 +15,7 @@
 	You should have received a copy of the GNU Lesser General Public License
 	along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
-#if defined(__APPLE__) || defined(__MACOSX)
+#if defined(__APPLE__) && !defined(USE_KHR_CL)
 #include <OpenCL/cl_ext.h>
 #else
 #include <CL/cl_ext.h>
