Skip to content

Commit 8bc19b1

Browse files
committed
Fix build and run with OpenCL.
* Change PRIVATE to PUBLIC for SSE and OPENCL definitions to propagate them to dependent apps. Fixes failure running `basisu -opencl` when BASISU_OPENCL is set during config. * Change INTERFACE to PRIVATE for OpenCL include directory. basisu_encoder needs this. Apps don't. Fixes build failure on Windows. * Change INTERFACE to PRIVATE for OpenCL link library. Needed if basisu_encoder ever becomes a shared library. When it's a static library, CMake will cause the OpenCL library to be linked to dependent apps. * Set BASISU_SUPPORT_OPENCL=0 when OpenCL not configured. * Make finding OpenCL required for non-Windows when OpenCL configured. Removes need for later checks, allowing code simplification. * Use embedded OpenCL stuff if WIN32 not if MSVC to support use with non-MSVC-front-end compilers.
1 parent 059a04b commit 8bc19b1

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

CMakeLists.txt

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ elseif(LINUX)
5555
find_package(Threads REQUIRED)
5656
endif()
5757

58-
if ((NOT MSVC) AND BASISU_OPENCL)
59-
# With MSVC builds we use the Khronos lib/include files in the project's "OpenCL" directory, to completely avoid requiring fiddly to install vendor SDK's.
58+
if ((NOT WIN32) AND BASISU_OPENCL)
59+
# For Windows builds we use the Khronos lib/include files in the project's "OpenCL" directory, to completely avoid requiring fiddly to install vendor SDK's.
6060
# Otherwise we use the system's (if any).
61-
find_package(OpenCL)
61+
find_package(OpenCL REQUIRED)
6262
message(STATUS "OpenCL found: ${OPENCL_FOUND}")
6363
message(STATUS "OpenCL includes: ${OpenCL_INCLUDE_DIRS}")
6464
message(STATUS "OpenCL libraries: ${OpenCL_LIBRARIES}")
@@ -213,22 +213,38 @@ INTERFACE
213213
target_compile_features(basisu_encoder PUBLIC cxx_std_17)
214214

215215
if (EMSCRIPTEN)
216-
target_compile_definitions(basisu_encoder PRIVATE BASISU_SUPPORT_SSE=0)
216+
target_compile_definitions(basisu_encoder PUBLIC BASISU_SUPPORT_SSE=0)
217217
else()
218+
target_compile_definitions(basisu_encoder PUBLIC
219+
BASISU_SUPPORT_SSE=$<IF:$<BOOL:${BASISU_SSE}>,1,0>
220+
)
218221
target_compile_options(basisu_encoder PRIVATE
219-
"$<IF:$<BOOL:${BASISU_SSE}>,-DBASISU_SUPPORT_SSE=1;$<$<CXX_COMPILER_ID:AppleClang,Clang,GNU>:-msse4.1>,-DBASISU_SUPPORT_SSE=0>"
222+
"$<$<AND:$<BOOL:${BASISU_SSE}>,$<CXX_COMPILER_ID:AppleClang,Clang,GNU>>:-msse4.1>"
220223
)
221224
endif()
222225

223226
target_compile_definitions(basisu_encoder PRIVATE "BASISD_SUPPORT_KTX2_ZSTD=$<IF:$<BOOL:${BASISU_ZSTD}>,1,0>")
224-
if (NOT MSVC)
225-
# For Non-Windows builds, let cmake try and find the system OpenCL headers/libs for us.
226-
if (BASISU_OPENCL AND OPENCL_FOUND)
227-
target_compile_definitions(basisu_encoder PRIVATE BASISU_SUPPORT_OPENCL=1)
228-
229-
target_include_directories(basisu_encoder INTERFACE ${OpenCL_INCLUDE_DIRS})
230-
target_link_libraries(basisu_encoder INTERFACE ${OpenCL_LIBRARIES})
227+
if (BASISU_OPENCL)
228+
# basisu uses this to confirm the library has been compiled with OpenCL support hence PUBLIC.
229+
target_compile_definitions(basisu_encoder PUBLIC BASISU_SUPPORT_OPENCL=1)
230+
if (NOT WIN32) # True when the target system is Windows.
231+
# For Non-Windows builds, use the system OpenCL headers/libs, if cmake found them.
232+
target_include_directories(basisu_encoder PRIVATE ${OpenCL_INCLUDE_DIRS})
233+
target_link_libraries(basisu_encoder PRIVATE ${OpenCL_LIBRARIES})
234+
else()
235+
# For Windows builds, we use our local copies of the OpenCL import lib and Khronos headers.
236+
target_include_directories(basisu_encoder PRIVATE "OpenCL")
237+
if (BASISU_BUILD_X64)
238+
target_link_libraries(basisu_encoder PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/OpenCL/lib/OpenCL64.lib")
239+
else()
240+
target_link_libraries(basisu_encoder PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/OpenCL/lib/OpenCL.lib")
241+
endif()
231242
endif()
243+
else()
244+
target_compile_definitions(basisu_encoder PUBLIC BASISU_SUPPORT_OPENCL=0)
245+
endif()
246+
247+
if (NOT MSVC)
232248
# Only link 'm' on non-Windows platforms (Linux, macOS)
233249
if (NOT WIN32)
234250
target_link_libraries(basisu_encoder INTERFACE m)
@@ -241,18 +257,6 @@ if (NOT MSVC)
241257
if (BASISU_STATIC AND MINGW)
242258
target_link_options(basisu_encoder INTERFACE -static-libgcc -static-libstdc++ -static)
243259
endif()
244-
else()
245-
# For Windows builds, we use our local copies of the OpenCL import lib and Khronos headers.
246-
if (BASISU_OPENCL)
247-
target_compile_definitions(basisu_encoder PRIVATE BASISU_SUPPORT_OPENCL=1)
248-
target_include_directories(basisu_encoder INTERFACE "OpenCL")
249-
250-
if (BASISU_BUILD_X64)
251-
target_link_libraries(basisu_encoder INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/OpenCL/lib/OpenCL64.lib")
252-
else()
253-
target_link_libraries(basisu_encoder INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/OpenCL/lib/OpenCL.lib")
254-
endif()
255-
endif()
256260
endif()
257261

258262
macro(set_common_executable_properties target)

basisu_tool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4673,7 +4673,7 @@ static int main_internal(int argc, const char **argv)
46734673
#ifndef BASISU_SUPPORT_OPENCL
46744674
if (use_opencl)
46754675
{
4676-
fprintf(stderr, "WARNING: -opencl specified, but OpenCL support was not enabled at compile time! With cmake, use -D OPENCL=1. Falling back to CPU compression.\n");
4676+
fprintf(stderr, "WARNING: -opencl specified, but OpenCL support was not enabled at compile time! With cmake, use -D BASISU_OPENCL=1. Falling back to CPU compression.\n");
46774677
}
46784678
#endif
46794679

0 commit comments

Comments
 (0)