Skip to content

Commit 82bda03

Browse files
bashbaugkepatil
authored andcommitted
initial ICD loader environment variable support (#77)
* basic ICD loader env var support for OCL_ICD_VENDORS * added envvars source files * added OCL_ICD_FILENAMES support * added README section describing debug environment variables * refactored platform specific defines into icd_platform.h * disable tracing
1 parent 467f286 commit 82bda03

12 files changed

Lines changed: 379 additions & 130 deletions

File tree

CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,28 @@ find_package (Threads REQUIRED)
3333
# advance. Use it with discretion.
3434
option (BUILD_SHARED_LIBS "Build shared libs" ON)
3535

36+
include(CheckFunctionExists)
37+
check_function_exists(secure_getenv HAVE_SECURE_GETENV)
38+
check_function_exists(__secure_getenv HAVE___SECURE_GETENV)
39+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/loader/icd_cmake_config.h.in
40+
${CMAKE_CURRENT_BINARY_DIR}/icd_cmake_config.h)
41+
3642
set (OPENCL_ICD_LOADER_SOURCES
3743
loader/icd.c
38-
loader/icd_dispatch.c)
44+
loader/icd.h
45+
loader/icd_dispatch.c
46+
loader/icd_dispatch.h
47+
loader/icd_envvars.h
48+
loader/icd_platform.h)
3949

4050
if (WIN32)
4151
list (APPEND OPENCL_ICD_LOADER_SOURCES
4252
loader/windows/icd_windows.c
43-
loader/windows/icd_windows_hkr.c
4453
loader/windows/icd_windows_dxgk.c
54+
loader/windows/icd_windows_dxgk.h
55+
loader/windows/icd_windows_envvars.c
56+
loader/windows/icd_windows_hkr.c
57+
loader/windows/icd_windows_hkr.h
4558
loader/windows/OpenCL.def
4659
loader/windows/OpenCL.rc)
4760
# Only add the DXSDK include directory if the environment variable is
@@ -53,6 +66,7 @@ if (WIN32)
5366
else ()
5467
list (APPEND OPENCL_ICD_LOADER_SOURCES
5568
loader/linux/icd_linux.c
69+
loader/linux/icd_linux_envvars.c
5670
loader/linux/icd_exports.map)
5771
endif ()
5872

@@ -108,7 +122,7 @@ endif ()
108122
include_directories (${OPENCL_ICD_LOADER_HEADERS_DIR})
109123
add_definitions (-DCL_TARGET_OPENCL_VERSION=220)
110124

111-
target_include_directories (OpenCL PRIVATE loader)
125+
target_include_directories (OpenCL PRIVATE ${CMAKE_CURRENT_BINARY_DIR} loader)
112126
target_link_libraries (OpenCL ${CMAKE_DL_LIBS})
113127

114128
include (CTest)

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,13 @@ Please create a GitHub issue to report an issue or ask questions.
115115
## Contributing
116116

117117
Contributions to the OpenCL ICD Loader are welcomed and encouraged.
118-
You will be prompted with a one-time "click-through" CLA dialog as part of submitting your pull request or other contribution to GitHub.
118+
You will be prompted with a one-time "click-through" CLA dialog as part of submitting your pull request or other contribution to GitHub.
119+
120+
## Table of Debug Environment Variables
121+
122+
The following debug environment variables are available for use with the OpenCL ICD loader:
123+
124+
| Environment Variable | Behavior | Example Format |
125+
|:---------------------------------:|---------------------|----------------------|
126+
| OCL_ICD_FILENAMES | Specifies a list of additional ICDs to load. The ICDs will be enumerated first, before any ICDs discovered via default mechanisms. | `export OCL_ICD_FILENAMES=libVendorA.so:libVendorB.so`<br/><br/>`set OCL_ICD_FILENAMES=vendor_a.dll;vendor_b.dll` |
127+
| OCL_ICD_VENDORS | On Linux and Android, specifies a directory to scan for ICDs to enumerate in place of the default `/etc/OpenCL/vendors'. | `export OCL_ICD_VENDORS=/my/local/icd/search/path` |

loader/icd.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "icd.h"
2020
#include "icd_dispatch.h"
21+
#include "icd_envvars.h"
2122
#include <stdlib.h>
2223
#include <string.h>
2324

@@ -188,6 +189,46 @@ void khrIcdVendorAdd(const char *libraryName)
188189
}
189190
}
190191

192+
// Get next file or dirname given a string list or registry key path.
193+
// Note: the input string may be modified!
194+
static char *loader_get_next_path(char *path) {
195+
size_t len;
196+
char *next;
197+
198+
if (path == NULL) return NULL;
199+
next = strchr(path, PATH_SEPARATOR);
200+
if (next == NULL) {
201+
len = strlen(path);
202+
next = path + len;
203+
} else {
204+
*next = '\0';
205+
next++;
206+
}
207+
208+
return next;
209+
}
210+
211+
void khrIcdVendorsEnumerateEnv(void)
212+
{
213+
char* icdFilenames = khrIcd_secure_getenv("OCL_ICD_FILENAMES");
214+
char* cur_file = NULL;
215+
char* next_file = NULL;
216+
if (icdFilenames)
217+
{
218+
KHR_ICD_TRACE("Found OCL_ICD_FILENAMES environment variable.\n");
219+
220+
next_file = icdFilenames;
221+
while (NULL != next_file && *next_file != '\0') {
222+
cur_file = next_file;
223+
next_file = loader_get_next_path(cur_file);
224+
225+
khrIcdVendorAdd(cur_file);
226+
}
227+
228+
khrIcd_free_getenv(icdFilenames);
229+
}
230+
}
231+
191232
void khrIcdContextPropertiesGetPlatform(const cl_context_properties *properties, cl_platform_id *outPlatform)
192233
{
193234
if (properties == NULL && khrIcdVendors != NULL)

loader/icd.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef _ICD_H_
2020
#define _ICD_H_
2121

22+
#include "icd_platform.h"
23+
2224
#ifndef CL_USE_DEPRECATED_OPENCL_1_0_APIS
2325
#define CL_USE_DEPRECATED_OPENCL_1_0_APIS
2426
#endif
@@ -34,10 +36,6 @@
3436
#include <CL/cl.h>
3537
#include <CL/cl_ext.h>
3638

37-
#ifdef _WIN32
38-
#include <tchar.h>
39-
#endif
40-
4139
/*
4240
* type definitions
4341
*/
@@ -101,6 +99,9 @@ void khrIcdInitialize(void);
10199
// n.b, this call is OS-specific
102100
void khrIcdOsVendorsEnumerateOnce(void);
103101

102+
// read vendors from environment variables
103+
void khrIcdVendorsEnumerateEnv(void);
104+
104105
// add a vendor's implementation to the list of libraries
105106
void khrIcdVendorAdd(const char *libraryName);
106107

loader/icd_cmake_config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#cmakedefine HAVE_SECURE_GETENV
2+
#cmakedefine HAVE___SECURE_GETENV

loader/icd_envvars.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2016-2019 The Khronos Group Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
17+
*/
18+
19+
#ifndef _ICD_ENVVARS_H_
20+
#define _ICD_ENVVARS_H_
21+
22+
char *khrIcd_getenv(const char *name);
23+
char *khrIcd_secure_getenv(const char *name);
24+
void khrIcd_free_getenv(char *val);
25+
26+
#endif

loader/icd_platform.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2016-2019 The Khronos Group Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
17+
*/
18+
19+
#ifndef _ICD_PLATFORM_H_
20+
#define _ICD_PLATFORM_H_
21+
22+
#if defined(__linux__) || defined(__APPLE__)
23+
24+
#define PATH_SEPARATOR ':'
25+
#define DIRECTORY_SYMBOL '/'
26+
#ifdef __ANDROID__
27+
#define ICD_VENDOR_PATH "/system/vendor/Khronos/OpenCL/vendors/";
28+
#else
29+
#define ICD_VENDOR_PATH "/etc/OpenCL/vendors/";
30+
#endif // ANDROID
31+
32+
#elif defined(_WIN32)
33+
34+
#define PATH_SEPARATOR ';'
35+
#define DIRECTORY_SYMBOL '\\'
36+
37+
#endif
38+
39+
#endif

0 commit comments

Comments
 (0)