Skip to content

Commit 7f554f3

Browse files
committed
Merge pull request opencv#10155 from alalek:ocl_update_loader
2 parents 91c1d76 + c38620e commit 7f554f3

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

modules/core/src/opencl/runtime/opencl_core.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ CV_SUPPRESS_DEPRECATED_END
6363
#define ERROR_MSG_CANT_LOAD "Failed to load OpenCL runtime\n"
6464
#define ERROR_MSG_INVALID_VERSION "Failed to load OpenCL runtime (expected version 1.1+)\n"
6565

66+
static const char* getRuntimePath(const char* defaultPath)
67+
{
68+
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
69+
if (envPath)
70+
{
71+
static const char disabled_str[] = "disabled";
72+
if ((strlen(envPath) == sizeof(disabled_str) - 1) &&
73+
(memcmp(envPath, disabled_str, sizeof(disabled_str) - 1) == 0))
74+
return NULL;
75+
return envPath;
76+
}
77+
return defaultPath;
78+
}
79+
6680
#if defined(__APPLE__)
6781
#include <dlfcn.h>
6882

@@ -75,14 +89,13 @@ static void* AppleCLGetProcAddress(const char* name)
7589
cv::AutoLock lock(cv::getInitializationMutex());
7690
if (!initialized)
7791
{
78-
const char* path = "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL";
79-
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
80-
if (envPath)
81-
path = envPath;
82-
handle = dlopen(oclpath, RTLD_LAZY | RTLD_GLOBAL);
92+
const char* defaultPath = "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL";
93+
const char* path = getRuntimePath(defaultPath);
94+
if (path)
95+
handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
8396
if (handle == NULL)
8497
{
85-
if (envPath)
98+
if (path != NULL && path != defaultPath)
8699
fprintf(stderr, ERROR_MSG_CANT_LOAD);
87100
}
88101
else if (dlsym(handle, OPENCL_FUNC_TO_CHECK_1_1) == NULL)
@@ -115,14 +128,13 @@ static void* WinGetProcAddress(const char* name)
115128
handle = GetModuleHandleA("OpenCL.dll");
116129
if (!handle)
117130
{
118-
const char* path = "OpenCL.dll";
119-
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
120-
if (envPath)
121-
path = envPath;
122-
handle = LoadLibraryA(path);
131+
const char* defaultPath = "OpenCL.dll";
132+
const char* path = getRuntimePath(defaultPath);
133+
if (path)
134+
handle = LoadLibraryA(path);
123135
if (!handle)
124136
{
125-
if (envPath)
137+
if (path != NULL && path != defaultPath)
126138
fprintf(stderr, ERROR_MSG_CANT_LOAD);
127139
}
128140
else if (GetProcAddress(handle, OPENCL_FUNC_TO_CHECK_1_1) == NULL)
@@ -173,18 +185,18 @@ static void* GetProcAddress(const char* name)
173185
cv::AutoLock lock(cv::getInitializationMutex());
174186
if (!initialized)
175187
{
176-
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
177-
if (envPath)
178-
{
179-
handle = GetHandle(envPath);
180-
if (!handle)
181-
fprintf(stderr, ERROR_MSG_CANT_LOAD);
182-
}
183-
else
188+
const char* defaultPath = "libOpenCL.so";
189+
const char* path = getRuntimePath(defaultPath);
190+
if (path)
184191
{
185-
handle = GetHandle("libOpenCL.so");
192+
handle = GetHandle(path);
186193
if (!handle)
187-
handle = GetHandle("libOpenCL.so.1");
194+
{
195+
if (path == defaultPath)
196+
handle = GetHandle("libOpenCL.so.1");
197+
else
198+
fprintf(stderr, ERROR_MSG_CANT_LOAD);
199+
}
188200
}
189201
initialized = true;
190202
}

0 commit comments

Comments
 (0)