|
1 | | -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with |
2 | | - # LSP |
3 | | -set(CMAKE_CXX_STANDARD 17) |
4 | | -set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 1 | +# Getting Started with CMAKE |
| 2 | +# Each example includes this and sets PROJECT_NAME. |
| 3 | +# |
| 4 | +# Example usage: |
| 5 | +# cd examples/hello_world |
| 6 | +# cmake -S . build/ -DCMAKE_BUILD_TYPE=Release |
| 7 | +# cmake --build build/ --config Release |
| 8 | +# ./build/hello_world (or serve the output .js/.wasm for Emscripten) |
| 9 | +# or for emscripten |
| 10 | +# emcmake cmake -S . -B ./build_web -DCMAKE_BUILD_TYPE=Release |
| 11 | +# cmake --build build_web --config Release |
| 12 | +# python3 -m http.server 8080 --d build_web |
5 | 13 |
|
| 14 | +if(NOT MSVC) |
| 15 | + set(CMAKE_CXX_STANDARD 17) |
| 16 | +else() |
| 17 | + set(CMAKE_CXX_STANDARD 20) |
| 18 | +endif() |
| 19 | + |
| 20 | +# Locate the project root (two levels up from the current source dir) |
6 | 21 | get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) |
7 | 22 | get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) |
8 | 23 |
|
9 | | -# Construct potential paths |
10 | | -set(FILEPATH_CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}") |
11 | | -set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/${FILENAME}") |
| 24 | +# Include external libraries and helper scripts (dawn and gpu) |
| 25 | +include("${PROJECT_ROOT}/cmake/dawn.cmake") |
| 26 | +include("${PROJECT_ROOT}/cmake/gpu.cmake") |
12 | 27 |
|
13 | | -# Include file finding utility script |
14 | | -include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/find_gpu.cmake") |
| 28 | +# Create the executable |
| 29 | +add_executable(${PROJECT_NAME} run.cpp) |
15 | 30 |
|
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() |
| 31 | +# Platform-specific linking & build settings |
| 32 | +if(EMSCRIPTEN) |
| 33 | + # Emscripten-specific configuration |
29 | 34 |
|
30 | | -# Ensure the build type is set |
31 | | -if(NOT CMAKE_BUILD_TYPE) |
32 | | - set(CMAKE_BUILD_TYPE |
33 | | - Release |
34 | | - CACHE STRING "Choose the type of build: Debug or Release" FORCE) |
35 | | -endif() |
| 35 | + # Define a web output directory (adjust as needed) |
| 36 | + set(WEB_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/web_build") |
36 | 37 |
|
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() |
| 38 | + # If necessary, include the generated WebGPU include dirs first. |
| 39 | + include_directories(BEFORE "${DAWN_BUILD_DIR}/gen/src/emdawnwebgpu/include/") |
43 | 40 |
|
44 | | -if(CMAKE_BUILD_TYPE STREQUAL "Debug") |
45 | | - set(BUILD_TYPE "Debug") |
46 | | -else() |
47 | | - set(BUILD_TYPE "Release") |
48 | | -endif() |
| 41 | + # Create a helper library for WebGPU support. |
| 42 | + add_library(webgpu_web "${DAWN_DIR}/third_party/emdawnwebgpu/webgpu.cpp") |
| 43 | + target_link_libraries(${PROJECT_NAME} PRIVATE webgpu_web) |
| 44 | + |
| 45 | + # Set Emscripten-specific link flags that enable WASM output and expose certain symbols. |
| 46 | + # Needed to use updated version, emdawnwebgpu |
| 47 | + set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "\ |
| 48 | + -sUSE_WEBGPU=0 \ |
| 49 | + -sWASM=1 \ |
| 50 | + -DDAWN_EMSCRIPTEN_TOOLCHAIN=${EMSCRIPTEN_DIR} \ |
| 51 | + -sEXPORTED_FUNCTIONS=_main,_malloc,_free,_memcpy \ |
| 52 | + -sEXPORTED_RUNTIME_METHODS=ccall \ |
| 53 | + -sUSE_GLFW=3 \ |
| 54 | + -sALLOW_MEMORY_GROWTH=1 -sSTACK_SIZE=5MB \ |
| 55 | + -sASYNCIFY \ |
| 56 | + --js-library=${DAWN_BUILD_DIR}/gen/src/emdawnwebgpu/library_webgpu_enum_tables.js \ |
| 57 | + --js-library=${DAWN_BUILD_DIR}/gen/src/emdawnwebgpu/library_webgpu_generated_struct_info.js \ |
| 58 | + --js-library=${DAWN_BUILD_DIR}/gen/src/emdawnwebgpu/library_webgpu_generated_sig_info.js \ |
| 59 | + --js-library=${DAWN_DIR}/third_party/emdawnwebgpu/library_webgpu.js \ |
| 60 | + --closure-args=--externs=${EMSCRIPTEN_DIR}/src/closure-externs/webgpu-externs.js \ |
| 61 | + ") |
49 | 62 |
|
50 | | -if(NOT TARGET gpu) |
51 | | - message(STATUS "GPU_LIB not found") |
52 | | - include("${TARGET_FILE_PATH}/cmake/webgpu.cmake") |
53 | | - include("${TARGET_FILE_PATH}/cmake/gpu.cmake") |
| 63 | +else() |
| 64 | + # Non-Emscripten (desktop) linking |
| 65 | + if(MSVC) |
| 66 | + target_link_libraries(gpu |
| 67 | + PRIVATE |
| 68 | + $<$<CONFIG:Debug>:${WEBGPU_DAWN_DEBUG}> |
| 69 | + $<$<CONFIG:Release>:${WEBGPU_DAWN_RELEASE}> |
| 70 | + ) |
| 71 | + else() |
| 72 | + target_link_libraries(gpu PRIVATE webgpu_dawn) |
| 73 | + endif() |
54 | 74 | endif() |
55 | 75 |
|
56 | | -add_executable(${PROJECT_NAME} run.cpp) |
| 76 | +# Link the gpu/dawn library to the executable. |
57 | 77 | target_link_libraries(${PROJECT_NAME} PRIVATE gpu) |
58 | | -target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) |
59 | | -target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) |
60 | 78 |
|
61 | | -if(WIN32) |
62 | | - # Ensure DLL is copied if on Windows |
| 79 | +# Platform-specific post-build actions (e.g. copying DLLs for MSVC) |
| 80 | +if(MSVC) |
63 | 81 | add_custom_command( |
64 | | - TARGET ${PROJECT_NAME} |
65 | | - POST_BUILD |
66 | | - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_PATH} |
67 | | - $<TARGET_FILE_DIR:${PROJECT_NAME}>) |
| 82 | + TARGET ${PROJECT_NAME} POST_BUILD |
| 83 | + COMMAND ${CMAKE_COMMAND} -E copy |
| 84 | + ${DAWN_BUILD_DIR}/$<CONFIG>/webgpu_dawn.dll |
| 85 | + $<TARGET_FILE_DIR:${PROJECT_NAME}> |
| 86 | + COMMENT "Copying webgpu_dawn.dll to the build directory" |
| 87 | + ) |
| 88 | +endif() |
| 89 | + |
| 90 | +if(EMSCRIPTEN) |
| 91 | + |
| 92 | + # Configure the HTML file by replacing @PROJECT_NAME@ with the actual target name. |
| 93 | + configure_file(${PROJECT_ROOT}cmake/templates/index.html.in |
| 94 | + ${CMAKE_CURRENT_BINARY_DIR}/index.html |
| 95 | + @ONLY) |
| 96 | + |
68 | 97 | endif() |
0 commit comments