Add option to enable building the WebGPU example

The example is now not built by default. Move code to fetch pre-compiled
`wgpu_native` library to `CMake/modules/FindWebGPU.cmake`.
This commit is contained in:
Sebastian Emanuel Dawid 2025-11-14 15:18:14 +01:00
parent 0e80ca651b
commit fb0828d828
2 changed files with 100 additions and 79 deletions

View File

@ -0,0 +1,84 @@
set(WebGPU_FOUND True) # since we fetch the depencency it will always be found
set(URL_ARCH)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(URL_ARCH "x86_64")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
set(URL_ARCH "i686")
endif ()
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|armv8|arm)$")
set(URL_ARCH "aarch64")
else ()
message(FATAL_ERROR "Unsopported CPU architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif ()
set(URL_OS)
set(WEBGPU_LIBNAME "libwgpu_native.so")
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(URL_OS "windows")
set(WEBGPU_LIBNAME "wgpu_native.dll")
if (MSVC)
set(URL_ARCH "${URL_ARCH}-msvc")
else ()
set(URL_ARCH "${URL_ARCH}-gnu")
endif ()
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(WEBGPU_LIBNAME "libwgpu_native.dylib")
set(URL_OS "macos")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(URL_OS "linux")
else ()
message(FATAL_ERROR "Unsopported OS: ${CMAKE_SYSTEM_NAME}")
endif ()
set(WEBGPU_URL "https://github.com/gfx-rs/wgpu-native/releases/download/v24.0.3.1/wgpu-${URL_OS}-${URL_ARCH}-release.zip")
include(FetchContent)
FetchContent_Declare(webgpu_bins
URL ${WEBGPU_URL}
)
FetchContent_MakeAvailable(webgpu_bins)
add_library(WebGPU::WebGPU SHARED IMPORTED GLOBAL)
set(WEBGPU_RUNTIME_LIB "${webgpu_bins_SOURCE_DIR}/lib/${WEBGPU_LIBNAME}")
set_target_properties(WebGPU::WebGPU PROPERTIES
IMPORTED_LOCATION "${WEBGPU_RUNTIME_LIB}"
)
target_include_directories(WebGPU::WebGPU INTERFACE
"${webgpu_bins_SOURCE_DIR}/include"
"${webgpu_bins_SOURCE_DIR}/include/webgpu"
)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
if (MSVC)
set_target_properties(WebGPU::WebGPU PROPERTIES
IMPORTED_IMPLIB ${webgpu_bins_SOURCE_DIR}/lib/${WEBGPU_LIBNAME}.lib
)
else ()
set_target_properties(WebGPU::WebGPU PROPERTIES
IMPORTED_IMPLIB ${webgpu_bins_SOURCE_DIR}/lib/lib${WEBGPU_LIBNAME}.a
)
endif ()
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set_target_properties(WebGPU::WebGPU PROPERTIES IMPORTED_NO_SONAME True)
else () # macOS
if (URL_ARCH STREQUAL "aarch64")
set(URL_ARCH "x86_64")
else ()
set(URL_ARCH "aarch64")
endif ()
set(WEBGPU_URL "https://github.com/gfx-rs/wgpu-native/releases/download/v24.0.3.1/wgpu-macos-${URL_ARCH}-release.zip")
FetchContent_Declare(webgpu_other_bins
URL ${WEBGPU_URL}
)
FetchContent_MakeAvailable(webgpu_other_bins)
set(WEBGPU_RUNTIME_LIB_OTHER "${webgpu_other_bins_SOURCE_DIR}/lib/${WEBGPU_LIBNAME}")
execute_process(COMMAND lipo -create ${WEBGPU_RUNTIME_LIB} ${WEBGPU_RUNTIME_LIB_OTHER} -output ${WEBGPU_RUNTIME_LIB})
endif ()
unset(URL_ARCH)
unset(URL_OS)
unset(WEBGPU_URL)
unset(WEBGPU_RUNTIME_LIB)
unset(WEBGPU_RUNTIME_LIB_OTHER)

View File

@ -25,81 +25,9 @@ set(GETOPT "${GLFW_SOURCE_DIR}/deps/getopt.h"
set(TINYCTHREAD "${GLFW_SOURCE_DIR}/deps/tinycthread.h"
"${GLFW_SOURCE_DIR}/deps/tinycthread.c")
set(URL_ARCH)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(URL_ARCH "x86_64")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
set(URL_ARCH "i686")
endif ()
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|armv8|arm)$")
set(URL_ARCH "aarch64")
else ()
message(FATAL_ERROR "Unsopported CPU architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif ()
set(URL_OS)
set(WEBGPU_LIBNAME "libwgpu_native.so")
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(URL_OS "windows")
set(WEBGPU_LIBNAME "wgpu_native.dll")
if (MSVC)
set(URL_ARCH "${URL_ARCH}-msvc")
else ()
set(URL_ARCH "${URL_ARCH}-gnu")
endif ()
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(WEBGPU_LIBNAME "libwgpu_native.dylib")
set(URL_OS "macos")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(URL_OS "linux")
else ()
message(FATAL_ERROR "Unsopported OS: ${CMAKE_SYSTEM_NAME}")
endif ()
set(WEBGPU_URL "https://github.com/gfx-rs/wgpu-native/releases/download/v24.0.3.1/wgpu-${URL_OS}-${URL_ARCH}-release.zip")
include(FetchContent)
FetchContent_Declare(webgpu_bins
URL ${WEBGPU_URL}
)
FetchContent_MakeAvailable(webgpu_bins)
add_library(webgpu SHARED IMPORTED GLOBAL)
set(WEBGPU_RUNTIME_LIB "${webgpu_bins_SOURCE_DIR}/lib/${WEBGPU_LIBNAME}")
set_target_properties(webgpu PROPERTIES
IMPORTED_LOCATION "${WEBGPU_RUNTIME_LIB}"
)
target_include_directories(webgpu INTERFACE
"${webgpu_bins_SOURCE_DIR}/include"
"${webgpu_bins_SOURCE_DIR}/include/webgpu"
)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
if (MSVC)
set_target_properties(webgpu PROPERTIES
IMPORTED_IMPLIB ${webgpu_bins_SOURCE_DIR}/lib/${WEBGPU_LIBNAME}.lib
)
else ()
set_target_properties(webgpu PROPERTIES
IMPORTED_IMPLIB ${webgpu_bins_SOURCE_DIR}/lib/lib${WEBGPU_LIBNAME}.a
)
endif ()
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set_target_properties(webgpu PROPERTIES IMPORTED_NO_SONAME True)
else () # macOS
if (URL_ARCH STREQUAL "aarch64")
set(URL_ARCH "x86_64")
else ()
set(URL_ARCH "aarch64")
endif ()
set(WEBGPU_URL "https://github.com/gfx-rs/wgpu-native/releases/download/v24.0.3.1/wgpu-macos-${URL_ARCH}-release.zip")
FetchContent_Declare(webgpu_other_bins
URL ${WEBGPU_URL}
)
FetchContent_MakeAvailable(webgpu_other_bins)
set(WEBGPU_RUNTIME_LIB_OTHER "${webgpu_other_bins_SOURCE_DIR}/lib/${WEBGPU_LIBNAME}")
execute_process(COMMAND lipo -create ${WEBGPU_RUNTIME_LIB} ${WEBGPU_RUNTIME_LIB_OTHER} -output ${WEBGPU_RUNTIME_LIB})
option(BUILD_WEBGPU_EXAMPLE "Build WebGPU example" False)
if (BUILD_WEBGPU_EXAMPLE)
find_package(WebGPU)
endif ()
add_executable(boing WIN32 MACOSX_BUNDLE boing.c ${ICON} ${GLAD_GL})
@ -111,7 +39,9 @@ add_executable(sharing WIN32 MACOSX_BUNDLE sharing.c ${ICON} ${GLAD_GL})
add_executable(splitview WIN32 MACOSX_BUNDLE splitview.c ${ICON} ${GLAD_GL})
add_executable(triangle-opengl WIN32 MACOSX_BUNDLE triangle-opengl.c ${ICON} ${GLAD_GL})
add_executable(triangle-opengles WIN32 MACOSX_BUNDLE triangle-opengles.c ${ICON} ${GLAD_GLES2})
add_executable(triangle-webgpu WIN32 MACOSX_BUNDLE triangle-webgpu.c ${ICON})
if (BUILD_WEBGPU_EXAMPLE AND WebGPU_FOUND)
add_executable(triangle-webgpu WIN32 MACOSX_BUNDLE triangle-webgpu.c ${ICON})
endif ()
add_executable(wave WIN32 MACOSX_BUNDLE wave.c ${ICON} ${GLAD_GL})
add_executable(windows WIN32 MACOSX_BUNDLE windows.c ${ICON} ${GLAD_GL})
@ -120,10 +50,15 @@ if (RT_LIBRARY)
target_link_libraries(particles "${RT_LIBRARY}")
endif()
target_link_libraries(triangle-webgpu webgpu)
if (BUILD_WEBGPU_EXAMPLE AND WebGPU_FOUND)
target_link_libraries(triangle-webgpu WebGPU::WebGPU)
endif ()
set(GUI_ONLY_BINARIES boing gears heightmap particles sharing splitview
triangle-opengl triangle-opengles triangle-webgpu wave windows)
triangle-opengl triangle-opengles wave windows)
if (BUILD_WEBGPU_EXAMPLE AND WebGPU_FOUND)
list(APPEND GUI_ONLY_BINARIES triangle-webgpu)
endif ()
set(CONSOLE_BINARIES offscreen)
set_target_properties(${GUI_ONLY_BINARIES} ${CONSOLE_BINARIES} PROPERTIES
@ -148,7 +83,9 @@ if (APPLE)
set_target_properties(sharing PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Sharing")
set_target_properties(triangle-opengl PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "OpenGL Triangle")
set_target_properties(triangle-opengles PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "OpenGL ES Triangle")
set_target_properties(triangle-webgpu PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "WebGPU Triangle")
if (BUILD_WEBGPU_EXAMPLE AND WebGPU_FOUND)
set_target_properties(triangle-webgpu PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "WebGPU Triangle")
endif ()
set_target_properties(splitview PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "SplitView")
set_target_properties(wave PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Wave")
set_target_properties(windows PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Windows")