Skip to content

Commit 1695fd5

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 1695fd5

File tree

8 files changed

+115
-74
lines changed

8 files changed

+115
-74
lines changed

CMakeLists.txt

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
set(WGPU_NATIVE_TARGET_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/target/${WGPU_NATIVE_BUILD_TYPE})
18+
19+
if(ANDROID)
20+
if(ANDROID_ABI STREQUAL "armeabi-v7a")
21+
set(ANDROID_TARGET "armv7-linux-androideabi")
22+
set(ANDROID_ARCH_SHORT "arm")
23+
elseif(ANDROID_ABI STREQUAL "arm64-v8a")
24+
set(ANDROID_TARGET "aarch64-linux-android")
25+
set(ANDROID_ARCH_SHORT "aarch64")
26+
elseif(ANDROID_ABI STREQUAL "x86")
27+
set(ANDROID_TARGET "i686-linux-android")
28+
set(ANDROID_ARCH_SHORT "i386")
29+
elseif(ANDROID_ABI STREQUAL "x86_64")
30+
set(ANDROID_TARGET "x86_64-linux-android")
31+
set(ANDROID_ARCH_SHORT "x86_64")
32+
endif()
33+
34+
set(WGPU_NATIVE_BUILD_TARGET "--target=${ANDROID_TARGET}")
35+
set(WGPU_NATIVE_TARGET_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/target/${ANDROID_TARGET}/${WGPU_NATIVE_BUILD_TYPE})
36+
37+
if(BUILD_SHARED_LIBS)
38+
# TODO: Is this actually right?
39+
message(FATAL_ERROR "Wasmtime cannot be built with BUILD_SHARED_LIBS on Android")
40+
endif()
41+
endif()
42+
43+
# TODO: Add mapping for iOS / etc builds to the right rust targets as well.
44+
45+
if(BUILD_SHARED_LIBS)
46+
if(WIN32)
47+
# TODO: Is this actually right?
48+
set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_TARGET_DIRECTORY}/${CMAKE_SHARED_LIBRARY_PREFIX}wgpu_native.dll.lib)
49+
else()
50+
set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_TARGET_DIRECTORY}/${CMAKE_SHARED_LIBRARY_PREFIX}wgpu_native${CMAKE_SHARED_LIBRARY_SUFFIX})
51+
endif()
52+
else()
53+
set(WGPU_NATIVE_BUILD_PRODUCT ${WGPU_NATIVE_TARGET_DIRECTORY}/${CMAKE_STATIC_LIBRARY_PREFIX}wgpu_native${CMAKE_STATIC_LIBRARY_SUFFIX})
54+
endif()
55+
56+
if (WIN32)
57+
set(OS_LIBRARIES d3dcompiler ws2_32 userenv bcrypt ntdll opengl32)
58+
elseif(UNIX AND NOT APPLE)
59+
set(OS_LIBRARIES "-lm -ldl")
60+
elseif(APPLE)
61+
set(OS_LIBRARIES "-framework CoreFoundation -framework QuartzCore -framework Metal")
62+
endif()
63+
64+
find_program(WGPU_NATIVE_CARGO_BINARY cargo)
65+
66+
# TODO: Use Corrosion instead?
67+
include(ExternalProject)
68+
ExternalProject_Add(
69+
wgpu_native-crate
70+
DOWNLOAD_COMMAND ""
71+
CONFIGURE_COMMAND ""
72+
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}
73+
INSTALL_COMMAND ""
74+
BUILD_ALWAYS ${WGPU_NATIVE_ALWAYS_BUILD}
75+
BUILD_BYPRODUCTS ${WGPU_NATIVE_BUILD_PRODUCT})
76+
77+
add_library(wgpu_native INTERFACE)
78+
# TODO: How should we really name these? I don't know that the _native suffix adds any value for anyone.
79+
add_library(wgpu::wgpu ALIAS wgpu_native)
80+
add_dependencies(wgpu_native wgpu_native-crate)
81+
target_link_libraries(wgpu_native INTERFACE ${WGPU_NATIVE_BUILD_PRODUCT} ${OS_LIBRARIES})
82+
83+
if(BUILD_SHARED_LIBS)
84+
target_compile_definitions(wgpu_native INTERFACE WGPU_SHARED_LIBRARY)
85+
endif()
86+
87+
target_include_directories(wgpu_native INTERFACE
88+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ffi>
89+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ffi/webgpu-headers>
90+
$<INSTALL_INTERFACE:include/webgpu>)
91+
92+
export(TARGETS wgpu_native NAMESPACE wgpu::
93+
FILE "${CMAKE_BINARY_DIR}/lib/cmake/wgpu_native/WgpuNativeTargets.cmake")
94+
95+
# TODO: install(EXPORTS ...)
96+
include(GNUInstallDirs)
97+
install(
98+
FILES
99+
${CMAKE_CURRENT_SOURCE_DIR}/ffi/wgpu.h
100+
${CMAKE_CURRENT_SOURCE_DIR}/ffi/webgpu-headers/webgpu.h
101+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/webgpu)
102+
install(FILES ${WGPU_NATIVE_BUILD_PRODUCT}
103+
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)