Skip to content

Commit babfc48

Browse files
cmake build support.
This allows either building a distribution of the library and headers via cmake or using it via `FetchContent`.
1 parent cd9d6a2 commit babfc48

File tree

8 files changed

+120
-74
lines changed

8 files changed

+120
-74
lines changed

CMakeLists.txt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
# TODO: Get version of wgpu-native and set that up correctly here.
3+
project(wgpu_native LANGUAGES C)
4+
5+
set(WGPU_NATIVE_USER_CARGO_BUILD_OPTIONS "" CACHE STRING "Additional cargo flags (such as --features) to apply to the build command")
6+
option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
7+
option(WGPU_NATIVE_ALWAYS_BUILD "If cmake should always invoke cargo to build wgpu_native" ON)
8+
9+
# TODO: What to do about RelWithDebInfo?
10+
if(CMAKE_BUILD_TYPE STREQUAL "Release")
11+
set(WGPU_NATIVE_BUILD_TYPE_FLAG "--release")
12+
set(WGPU_NATIVE_BUILD_TYPE "release")
13+
else()
14+
set(WGPU_NATIVE_BUILD_TYPE "debug")
15+
endif()
16+
17+
if(DEFINED ENV{CARGO_BUILD_TARGET})
18+
set(WGPU_NATIVE_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/target/$ENV{CARGO_BUILD_TARGET}/${WGPU_NATIVE_BUILD_TYPE})
19+
else()
20+
set(WGPU_NATIVE_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/target/${WGPU_NATIVE_BUILD_TYPE})
21+
endif()
22+
23+
if(ANDROID)
24+
if(ANDROID_ABI STREQUAL "armeabi-v7a")
25+
set(ANDROID_TARGET "armv7-linux-androideabi")
26+
set(ANDROID_ARCH_SHORT "arm")
27+
elseif(ANDROID_ABI STREQUAL "arm64-v8a")
28+
set(ANDROID_TARGET "aarch64-linux-android")
29+
set(ANDROID_ARCH_SHORT "aarch64")
30+
elseif(ANDROID_ABI STREQUAL "x86")
31+
set(ANDROID_TARGET "i686-linux-android")
32+
set(ANDROID_ARCH_SHORT "i386")
33+
elseif(ANDROID_ABI STREQUAL "x86_64")
34+
set(ANDROID_TARGET "x86_64-linux-android")
35+
set(ANDROID_ARCH_SHORT "x86_64")
36+
endif()
37+
38+
set(WGPU_NATIVE_BUILD_TARGET "--target=${ANDROID_TARGET}")
39+
# TODO: Is this needed given CARGO_BUILD_TARGET above?
40+
set(WGPU_NATIVE_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/target/${ANDROID_TARGET}/${WGPU_NATIVE_BUILD_TYPE})
41+
42+
if(BUILD_SHARED_LIBS)
43+
# TODO: Is this actually right?
44+
message(FATAL_ERROR "Wasmtime cannot be built with BUILD_SHARED_LIBS on Android")
45+
endif()
46+
endif()
47+
48+
# TODO: Add mapping for iOS / etc builds to the right rust targets as well.
49+
50+
if(BUILD_SHARED_LIBS)
51+
if(WIN32)
52+
# TODO: Is this actually right?
53+
set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}wgpu_native.dll.lib)
54+
else()
55+
set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}wgpu_native${CMAKE_SHARED_LIBRARY_SUFFIX})
56+
endif()
57+
else()
58+
set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}wgpu_native${CMAKE_STATIC_LIBRARY_SUFFIX})
59+
endif()
60+
61+
if (WIN32)
62+
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32)
63+
elseif(UNIX AND NOT APPLE)
64+
set(OS_LIBRARIES "-lm -ldl")
65+
elseif(APPLE)
66+
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal")
67+
endif()
68+
69+
find_program(WGPU_NATIVE_CARGO_BINARY cargo)
70+
71+
# TODO: Use Corrosion instead?
72+
include(ExternalProject)
73+
ExternalProject_Add(
74+
wgpu_native-crate
75+
DOWNLOAD_COMMAND ""
76+
CONFIGURE_COMMAND ""
77+
BUILD_COMMAND ${WGPU_NATIVE_PREBUILD_COMMAND} ${WGPU_NATIVE_CARGO_BINARY} build ${WGPU_NATIVE_BUILD_TYPE_FLAG} ${WGPU_NATIVE_USER_CARGO_BUILD_OPTIONS} ${WGPU_NATIVE_BUILD_TARGET}
78+
INSTALL_COMMAND ""
79+
BUILD_ALWAYS ${WGPU_NATIVE_ALWAYS_BUILD}
80+
BUILD_BYPRODUCTS ${WGPU_NATIVE_BUILD_PRODUCT})
81+
82+
add_library(wgpu_native INTERFACE)
83+
# TODO: How should we really name these? I don't know that the _native suffix adds any value for anyone.
84+
add_library(wgpu::wgpu ALIAS wgpu_native)
85+
add_dependencies(wgpu_native wgpu_native-crate)
86+
target_link_libraries(wgpu_native INTERFACE ${WGPU_NATIVE_BUILD_PRODUCT} ${OS_LIBRARIES})
87+
88+
if(BUILD_SHARED_LIBS)
89+
target_compile_definitions(wgpu_native INTERFACE WGPU_SHARED_LIBRARY)
90+
endif()
91+
92+
target_include_directories(wgpu_native INTERFACE
93+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ffi>
94+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ffi/webgpu-headers>
95+
$<INSTALL_INTERFACE:include/webgpu>)
96+
97+
export(TARGETS wgpu_native NAMESPACE wgpu::
98+
FILE "${CMAKE_BINARY_DIR}/lib/cmake/wgpu_native/WgpuNativeTargets.cmake")
99+
100+
# TODO: install(EXPORTS ...)
101+
include(GNUInstallDirs)
102+
install(
103+
FILES
104+
${CMAKE_CURRENT_SOURCE_DIR}/ffi/wgpu.h
105+
${CMAKE_CURRENT_SOURCE_DIR}/ffi/webgpu-headers/webgpu.h
106+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/webgpu)
107+
install(FILES ${WGPU_NATIVE_BUILD_PRODUCT}
108+
DESTINATION ${CMAKE_INSTALL_LIBDIR})

examples/CMakeLists.txt

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,13 @@ if(APPLE)
88
endif()
99

1010
cmake_path(GET CMAKE_SOURCE_DIR PARENT_PATH PROJECT_ROOT_DIR)
11-
12-
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
13-
set(WGPU_NATIVE_LIB_TYPE debug)
14-
else()
15-
set(WGPU_NATIVE_LIB_TYPE release)
16-
endif()
17-
18-
if(DEFINED ENV{CARGO_BUILD_TARGET})
19-
set(WGPU_NATIVE_LIB_DIR ${PROJECT_ROOT_DIR}/target/$ENV{CARGO_BUILD_TARGET}/${WGPU_NATIVE_LIB_TYPE})
20-
else()
21-
set(WGPU_NATIVE_LIB_DIR ${PROJECT_ROOT_DIR}/target/${WGPU_NATIVE_LIB_TYPE})
22-
endif()
23-
24-
find_library(
25-
WGPU_LIBRARY NAMES libwgpu_native.a wgpu_native.lib wgpu_native
26-
HINTS ${WGPU_NATIVE_LIB_DIR}
27-
REQUIRED
11+
include(FetchContent)
12+
FetchContent_Declare(
13+
wgpu_native
14+
SOURCE_DIR ${PROJECT_ROOT_DIR}
2815
)
16+
FetchContent_MakeAvailable(wgpu_native)
17+
set(WGPU_LIBRARY wgpu::wgpu)
2918

3019
add_subdirectory(framework)
3120

examples/capture/CMakeLists.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ endif()
1111

1212
add_definitions(-DSTB_IMAGE_WRITE_IMPLEMENTATION)
1313

14-
include_directories(${CMAKE_SOURCE_DIR}/../ffi)
15-
include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers)
1614
include_directories(${CMAKE_SOURCE_DIR}/framework)
1715

18-
if (WIN32)
19-
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32)
20-
elseif(UNIX AND NOT APPLE)
21-
set(OS_LIBRARIES "-lm -ldl")
22-
elseif(APPLE)
23-
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal")
24-
endif()
25-
26-
target_link_libraries(capture framework ${WGPU_LIBRARY} ${OS_LIBRARIES})
16+
target_link_libraries(capture framework ${WGPU_LIBRARY})

examples/compute/CMakeLists.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ else()
99
add_compile_options(-Wall -Wextra -Wpedantic)
1010
endif()
1111

12-
include_directories(${CMAKE_SOURCE_DIR}/../ffi)
13-
include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers)
1412
include_directories(${CMAKE_SOURCE_DIR}/framework)
1513

16-
if (WIN32)
17-
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32)
18-
elseif(UNIX AND NOT APPLE)
19-
set(OS_LIBRARIES "-lm -ldl")
20-
elseif(APPLE)
21-
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal")
22-
endif()
23-
24-
target_link_libraries(compute framework ${WGPU_LIBRARY} ${OS_LIBRARIES})
14+
target_link_libraries(compute framework ${WGPU_LIBRARY})

examples/enumerate_adapters/CMakeLists.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ else()
99
add_compile_options(-Wall -Wextra -Wpedantic)
1010
endif()
1111

12-
include_directories(${CMAKE_SOURCE_DIR}/../ffi)
13-
include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers)
1412
include_directories(${CMAKE_SOURCE_DIR}/framework)
1513

16-
if (WIN32)
17-
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32)
18-
elseif(UNIX AND NOT APPLE)
19-
set(OS_LIBRARIES "-lm -ldl")
20-
elseif(APPLE)
21-
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal")
22-
endif()
23-
24-
target_link_libraries(enumerate_adapters framework ${WGPU_LIBRARY} ${OS_LIBRARIES})
14+
target_link_libraries(enumerate_adapters framework ${WGPU_LIBRARY})

examples/framework/CMakeLists.txt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,4 @@ else()
99
add_compile_options(-Wall -Wextra -Wpedantic)
1010
endif()
1111

12-
include_directories(${CMAKE_SOURCE_DIR}/../ffi)
13-
include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers)
14-
15-
if (WIN32)
16-
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32)
17-
elseif(UNIX AND NOT APPLE)
18-
set(OS_LIBRARIES "-lm -ldl")
19-
elseif(APPLE)
20-
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal")
21-
endif()
22-
23-
target_link_libraries(framework ${WGPU_LIBRARY} ${OS_LIBRARIES})
12+
target_link_libraries(framework ${WGPU_LIBRARY})

examples/texture_arrays/CMakeLists.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,14 @@ else()
99
add_compile_options(-Wall -Wextra -Wpedantic)
1010
endif()
1111

12-
include_directories(${CMAKE_SOURCE_DIR}/../ffi)
13-
include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers)
1412
include_directories(${CMAKE_SOURCE_DIR}/framework)
1513

1614
if (WIN32)
1715
add_definitions(-DWGPU_TARGET_WINDOWS)
18-
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32)
1916
elseif(UNIX AND NOT APPLE)
2017
add_definitions(-DWGPU_TARGET_LINUX_X11)
21-
set(OS_LIBRARIES "-lm -ldl")
2218
elseif(APPLE)
2319
add_definitions(-DWGPU_TARGET_MACOS)
24-
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal")
2520
endif()
2621

27-
target_link_libraries(texture_arrays framework glfw ${WGPU_LIBRARY} ${OS_LIBRARIES})
22+
target_link_libraries(texture_arrays framework glfw ${WGPU_LIBRARY})

examples/triangle/CMakeLists.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,14 @@ else()
99
add_compile_options(-Wall -Wextra -Wpedantic)
1010
endif()
1111

12-
include_directories(${CMAKE_SOURCE_DIR}/../ffi)
13-
include_directories(${CMAKE_SOURCE_DIR}/../ffi/webgpu-headers)
1412
include_directories(${CMAKE_SOURCE_DIR}/framework)
1513

1614
if (WIN32)
1715
add_definitions(-DWGPU_TARGET_WINDOWS)
18-
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32)
1916
elseif(UNIX AND NOT APPLE)
2017
add_definitions(-DWGPU_TARGET_LINUX_X11)
21-
set(OS_LIBRARIES "-lm -ldl")
2218
elseif(APPLE)
2319
add_definitions(-DWGPU_TARGET_MACOS)
24-
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal")
2520
endif()
2621

27-
target_link_libraries(triangle framework glfw ${WGPU_LIBRARY} ${OS_LIBRARIES})
22+
target_link_libraries(triangle framework glfw ${WGPU_LIBRARY})

0 commit comments

Comments
 (0)