Skip to content

Commit bbc3add

Browse files
committed
More simplification
1 parent b748399 commit bbc3add

File tree

4 files changed

+70
-129
lines changed

4 files changed

+70
-129
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# This only builds a shared lib, see cmake/example.cmake
2+
# and cmake/gpu.cmake for more details
13
cmake_minimum_required(VERSION 3.28)
24
project(gpu)
35

cmake/example.cmake

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,45 @@
1-
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with
2-
# LSP
3-
set(CMAKE_CXX_STANDARD 20)
4-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1+
# Getting Started with CMAKE
2+
# Each example includes this and sets PROJECT_NAME
3+
# cd examples/hello_world
4+
# cmake -S . build/ -DCMAKE_BUILD_TYPE=Release
5+
# cmake --build build/ --config Release
6+
# ./build/hello_world
7+
8+
if(NOT MSVC)
9+
set(CMAKE_CXX_STANDARD 17)
10+
else()
11+
set(CMAKE_CXX_STANDARD 20)
12+
endif()
513

14+
# Path finding logic to find our root recipes from nested folders
615
get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
716
get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY)
817

9-
# Construct potential paths
10-
set(FILEPATH_CURRENT_DIR "${DIRECTORY}/")
11-
set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/")
12-
13-
# Include file finding utility script
14-
include("${FILEPATH_PROJECT_ROOT}/cmake/find_gpu.cmake")
15-
16-
# Check if the file exists in the current directory
17-
find_project_root(${CMAKE_CURRENT_SOURCE_DIR} ${FILENAME}
18-
TARGET_FILE_PATH)
19-
if("${TARGET_FILE_PATH}" STREQUAL "")
20-
find_project_root(${FILEPATH_CURRENT_DIR} ${FILENAME}
21-
TARGET_FILE_PATH)
22-
if("${TARGET_FILE_PATH}" STREQUAL "")
23-
message(
24-
FATAL_ERROR
25-
"File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../"
26-
)
27-
endif()
28-
endif()
29-
3018
# Ensure the build type is set
3119
if(NOT CMAKE_BUILD_TYPE)
3220
set(CMAKE_BUILD_TYPE
3321
Release
3422
CACHE STRING "Choose the type of build: Debug or Release" FORCE)
3523
endif()
3624

37-
# Define architecture and build type directories or file names
38-
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
39-
set(ARCH "x64")
40-
else()
41-
set(ARCH "x86")
42-
endif()
43-
44-
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
45-
set(BUILD_TYPE "Debug")
46-
else()
47-
set(BUILD_TYPE "Release")
48-
endif()
25+
# Include the gpu.cpp + Dawn library
26+
include("${PROJECT_ROOT}/cmake/gpu.cmake")
4927

50-
if(NOT TARGET gpu)
51-
message(STATUS "GPU_LIB not found")
52-
include("${TARGET_FILE_PATH}/cmake/gpu.cmake")
53-
endif()
28+
# Create the executable
5429
add_executable(${PROJECT_NAME} run.cpp)
30+
31+
# Link gpu + dawn library
5532
target_link_libraries(${PROJECT_NAME} PRIVATE gpu)
56-
target_link_libraries(${PROJECT_NAME} PRIVATE ${WEBGPU_DAWN})
5733

34+
# Certain platforms need to copy the library files to the build directory
5835
if(MSVC)
59-
# Copy webgpu_dawn.dll to the build directory
36+
# Copy webgpu_dawn.dll to the build directory
37+
# CMake multigenerators like MSVC need --config Release on
38+
# the cmake --build command or they will output to /Debug
6039
add_custom_command(
6140
TARGET ${PROJECT_NAME} POST_BUILD
6241
COMMAND ${CMAKE_COMMAND} -E copy
6342
${DAWN_INSTALL_PREFIX}/${CMAKE_BUILD_TYPE}/webgpu_dawn.dll
64-
$<TARGET_FILE_DIR:${PROJECT_NAME}>
65-
)
43+
$<TARGET_FILE_DIR:${PROJECT_NAME}>)
6644
endif()
6745

cmake/find_gpu.cmake

Lines changed: 0 additions & 30 deletions
This file was deleted.

cmake/gpu.cmake

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
set(FILENAME "gpu.hpp")
22

3+
# Setup project root here.
34
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}")
4-
set(FILEPATH_PROJECT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
5+
set(PROJECT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
56
else()
67
get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
78
get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY)
8-
9-
set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/")
9+
10+
set(PROJECT_ROOT "${PROJECT_ROOT}/")
1011
endif()
1112

13+
message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}")
14+
1215

1316
include(FetchContent)
1417

15-
set(FETCHCONTENT_BASE_DIR "${FILEPATH_PROJECT_ROOT}/third_party/fetchcontent/_deps")
18+
set(FETCHCONTENT_BASE_DIR "${PROJECT_ROOT}/third_party/fetchcontent/_deps")
1619
set(DAWN_INSTALL_PREFIX "${FETCHCONTENT_BASE_DIR}/dawn-build/out/${CMAKE_BUILD_TYPE}" CACHE INTERNAL "Dawn install location" FORCE)
1720

1821

1922
# Before fetching, set configuration options for Dawn.
20-
# These CMake variables are “global” (cached INTERNAL) so that Dawn’s own CMakeLists.txt
21-
# will pick them up. Adjust them as needed.
22-
set(DAWN_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE INTERNAL "Dawn build type" FORCE)
2323
set(DCMAKE_INSTALL_PREFIX ${DAWN_INSTALL_PREFIX} CACHE INTERNAL "Dawn install location" FORCE)
24+
set(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE INTERNAL "Dawn configuration types" FORCE)
2425

25-
# Dawn options
26-
set(DAWN_FETCH_DEPENDENCIES ON CACHE INTERNAL "Fetch Dawn dependencies" FORCE)
27-
set(DAWN_ENABLE_INSTALL ON CACHE INTERNAL "Enable Dawn installation" FORCE)
28-
set(DAWN_BUILD_MONOLITHIC_LIBRARY OFF CACHE INTERNAL "Build Dawn monolithically" FORCE)
26+
# Dawn options for more,
27+
# see https://dawn.googlesource.com/dawn/+/refs/heads/main/CMakeLists.txt
28+
set(DAWN_ALWAYS_ASSERT OFF CACHE INTERNAL "Always assert in Dawn" FORCE)
29+
set(DAWN_BUILD_MONOLITHIC_LIBRARY ON CACHE INTERNAL "Build Dawn monolithically" FORCE)
2930
set(DAWN_BUILD_EXAMPLES OFF CACHE INTERNAL "Build Dawn examples" FORCE)
3031
set(DAWN_BUILD_SAMPLES OFF CACHE INTERNAL "Build Dawn samples" FORCE)
3132
set(DAWN_BUILD_TESTS OFF CACHE INTERNAL "Build Dawn tests" FORCE)
32-
set(DAWN_BUILD_UTILS OFF CACHE INTERNAL "Build Dawn utilities" FORCE)
33+
set(DAWN_ENABLE_INSTALL ON CACHE INTERNAL "Enable Dawn installation" FORCE)
34+
set(DAWN_FETCH_DEPENDENCIES ON CACHE INTERNAL "Fetch Dawn dependencies" FORCE)
35+
3336
set(TINT_BUILD_TESTS OFF CACHE INTERNAL "Build Tint Tests" FORCE)
3437
set(TINT_BUILD_IR_BINARY OFF CACHE INTERNAL "Build Tint IR binary" FORCE)
3538
set(TINT_BUILD_CMD_TOOLS OFF CACHE INTERNAL "Build Tint command line tools" FORCE)
36-
set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "Build shared libraries" FORCE)
37-
3839

39-
# Set up an install location for Dawn – you can change this to a specific location.
40+
set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "Build shared libraries" FORCE)
4041

4142

43+
# Fetch Setup
4244
FetchContent_Declare(
4345
dawn
4446
DOWNLOAD_COMMAND
@@ -49,67 +51,56 @@ FetchContent_Declare(
4951
)
5052

5153

52-
# This call will download the repository and add it as a subdirectory.
54+
# Download the repository and add it as a subdirectory.
5355
FetchContent_MakeAvailable(dawn)
5456

5557

56-
# At this point, assuming Dawn’s CMakeLists.txt is written so that an install step is available,
57-
# we trigger a build of its install target. This custom target will build (and install) Dawn
58-
# into ${DAWN_INSTALL_PREFIX}. (If Dawn already adds an install target, you may simply depend on it.)
59-
add_custom_target(build_dawn_config ALL
58+
# Since we require Dawn to be built before linking against it, we need to configure it now.
59+
execute_process(
6060
COMMAND ${CMAKE_COMMAND} ${FETCHCONTENT_BASE_DIR}/dawn-src
6161
-B ${DAWN_INSTALL_PREFIX}
6262
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
63-
-DDAWN_FETCH_DEPENDENCIES=ON
64-
-DDAWN_ENABLE_INSTALL=ON
65-
-DDAWN_BUILD_MONOLITHIC_LIBRARY=OFF
66-
-DDAWN_BUILD_EXAMPLES=OFF
67-
-DDAWN_BUILD_SAMPLES=OFF
68-
-DDAWN_BUILD_TESTS=OFF
69-
-DDAWN_BUILD_UTILS=OFF
70-
-DTINT_BUILD_TESTS=OFF
71-
-DTINT_BUILD_IR_BINARY=OFF
72-
-DTINT_BUILD_CMD_TOOLS=OFF
73-
-DBUILD_SHARED_LIBS=OFF
7463
-G "${CMAKE_GENERATOR}"
75-
COMMENT "Configuring Dawn build with custom options in ${DAWN_INSTALL_PREFIX}"
7664
)
7765

78-
add_custom_target(build_dawn_install ALL
79-
COMMAND ${CMAKE_COMMAND} --build ${DAWN_INSTALL_PREFIX} --target install
80-
COMMENT "Installing Dawn into ${DAWN_INSTALL_PREFIX}"
66+
# Build Dawn
67+
execute_process(
68+
WORKING_DIRECTORY ${FETCHCONTENT_BASE_DIR}/dawn-src
69+
COMMAND ${CMAKE_COMMAND} --build ${DAWN_INSTALL_PREFIX} --config ${CMAKE_BUILD_TYPE}
8170
)
8271

83-
include(${FETCHCONTENT_BASE_DIR}/dawn-build/cmake/DawnTargets.cmake)
84-
72+
# Add sources
8573
set(GPU_SOURCES
86-
"${FILEPATH_PROJECT_ROOT}/gpu.cpp"
87-
"${FILEPATH_PROJECT_ROOT}/numeric_types/half.cpp"
74+
"${PROJECT_ROOT}/gpu.cpp"
75+
"${PROJECT_ROOT}/numeric_types/half.cpp"
8876
)
8977

78+
# Add headers
9079
set(GPU_HEADERS
91-
"${FILEPATH_PROJECT_ROOT}/gpu.hpp"
92-
"${FILEPATH_PROJECT_ROOT}/utils/logging.hpp"
93-
"${FILEPATH_PROJECT_ROOT}/utils/array_utils.hpp"
94-
"${FILEPATH_PROJECT_ROOT}/numeric_types/half.hpp"
80+
"${PROJECT_ROOT}/gpu.hpp"
81+
"${PROJECT_ROOT}/utils/logging.hpp"
82+
"${PROJECT_ROOT}/utils/array_utils.hpp"
83+
"${PROJECT_ROOT}/numeric_types/half.hpp"
9584
)
9685

86+
# Emscripten includes a header automatically
9787
if(EMSCRIPTEN)
98-
file(REMOVE "${FILEPATH_PROJECT_ROOT}/webgpu/webgpu.h")
88+
file(REMOVE "${PROJECT_ROOT}/webgpu/webgpu.h")
9989
else()
100-
list(APPEND GPU_HEADERS "${DAWN_INSTALL_PREFIX}/gen/webgpu-headers/webgpu.h")
90+
list(APPEND GPU_HEADERS "${PROJECT_ROOT}/third_party/headers/webgpu/webgpu.h")
10191
endif()
10292

10393

104-
# Create the INTERFACE library gpu
94+
# Create the STATIC library for gpu
10595
add_library(gpu STATIC ${GPU_SOURCES} ${GPU_HEADERS})
106-
target_include_directories(gpu PUBLIC "${FILEPATH_PROJECT_ROOT}")
107-
target_include_directories(gpu PUBLIC "${FILEPATH_PROJECT_ROOT}/third_party/headers")
96+
target_include_directories(gpu PUBLIC "${PROJECT_ROOT}")
97+
target_include_directories(gpu PUBLIC "${PROJECT_ROOT}/third_party/headers")
10898

109-
# Ensure that the gpu target is built only after Dawn has been installed.
110-
add_dependencies(gpu build_dawn_install)
111-
112-
find_library(WEBGPU_DAWN
99+
# Find the monolithic library for Dawn
100+
find_library(WEBGPU_DAWN_MONOLITHIC
113101
NAMES webgpu_dawn
114-
HINTS "${DAWN_INSTALL_PREFIX}/src/dawn/native/Debug/"
115-
)
102+
HINTS "${DAWN_INSTALL_PREFIX}/src/dawn/native/${CMAKE_BUILD_TYPE}"
103+
)
104+
105+
# Link the monolithic library
106+
target_link_libraries(gpu PRIVATE ${WEBGPU_DAWN_MONOLITHIC})

0 commit comments

Comments
 (0)