|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +cmake_minimum_required(VERSION 3.19) |
| 8 | + |
| 9 | +if(NOT EXECUTORCH_ROOT) |
| 10 | + set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) |
| 11 | +endif() |
| 12 | + |
| 13 | +include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake) |
| 14 | + |
| 15 | +# Ensure vulkan_schema is available even when EXECUTORCH_BUILD_VULKAN is OFF. |
| 16 | +# The WebGPU backend reuses the Vulkan FlatBuffer serialization format. |
| 17 | +if(NOT TARGET vulkan_schema) |
| 18 | + # We need the schema generation from the Vulkan backend. Build only the schema |
| 19 | + # target by including the Vulkan CMakeLists.txt. The full Vulkan backend will |
| 20 | + # only build if EXECUTORCH_BUILD_VULKAN is ON (which gates the vulkan_backend |
| 21 | + # target), but vulkan_schema is unconditionally defined. |
| 22 | + add_subdirectory( |
| 23 | + ${CMAKE_CURRENT_SOURCE_DIR}/../vulkan |
| 24 | + ${CMAKE_CURRENT_BINARY_DIR}/_vulkan_schema |
| 25 | + ) |
| 26 | +endif() |
| 27 | + |
| 28 | +set(WEBGPU_SRCS |
| 29 | + runtime/WebGPUBackend.cpp runtime/WebGPUGraph.cpp |
| 30 | + runtime/WebGPUDelegateHeader.cpp runtime/WebGPUDevice.cpp |
| 31 | + runtime/ops/OperatorRegistry.cpp runtime/ops/add/BinaryOp.cpp |
| 32 | +) |
| 33 | + |
| 34 | +add_library(webgpu_backend ${WEBGPU_SRCS}) |
| 35 | + |
| 36 | +target_include_directories( |
| 37 | + webgpu_backend PRIVATE $<BUILD_INTERFACE:${EXECUTORCH_ROOT}/..> |
| 38 | +) |
| 39 | + |
| 40 | +target_link_libraries(webgpu_backend PRIVATE vulkan_schema executorch_core) |
| 41 | + |
| 42 | +# Native build: link against wgpu-native |
| 43 | +set(WGPU_NATIVE_DIR |
| 44 | + "${CMAKE_CURRENT_SOURCE_DIR}/third-party/wgpu-native" |
| 45 | + CACHE PATH "Path to wgpu-native installation" |
| 46 | +) |
| 47 | + |
| 48 | +if(NOT EXISTS "${WGPU_NATIVE_DIR}/lib/libwgpu_native.a") |
| 49 | + message(FATAL_ERROR "wgpu-native not found at ${WGPU_NATIVE_DIR}. " |
| 50 | + "Run: bash backends/webgpu/scripts/setup-wgpu-native.sh" |
| 51 | + ) |
| 52 | +endif() |
| 53 | + |
| 54 | +add_library(wgpu_native STATIC IMPORTED) |
| 55 | +set_target_properties( |
| 56 | + wgpu_native PROPERTIES IMPORTED_LOCATION |
| 57 | + "${WGPU_NATIVE_DIR}/lib/libwgpu_native.a" |
| 58 | +) |
| 59 | + |
| 60 | +target_include_directories( |
| 61 | + webgpu_backend PUBLIC $<BUILD_INTERFACE:${WGPU_NATIVE_DIR}/include> |
| 62 | +) |
| 63 | +target_link_libraries(webgpu_backend PRIVATE wgpu_native) |
| 64 | + |
| 65 | +if(APPLE) |
| 66 | + target_link_libraries( |
| 67 | + webgpu_backend PRIVATE "-framework Metal" "-framework QuartzCore" |
| 68 | + "-framework CoreGraphics" "-framework Foundation" |
| 69 | + ) |
| 70 | +else() |
| 71 | + target_link_libraries(webgpu_backend PRIVATE dl m pthread) |
| 72 | +endif() |
| 73 | + |
| 74 | +target_compile_options(webgpu_backend PRIVATE -fexceptions) |
| 75 | + |
| 76 | +# Link with --whole-archive for static registration of backend + ops |
| 77 | +executorch_target_link_options_shared_lib(webgpu_backend) |
| 78 | + |
| 79 | +set_property(TARGET webgpu_backend PROPERTY CXX_STANDARD 17) |
| 80 | + |
| 81 | +install( |
| 82 | + TARGETS webgpu_backend |
| 83 | + EXPORT ExecuTorchTargets |
| 84 | + DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 85 | +) |
| 86 | + |
| 87 | +# Native test target |
| 88 | +if(EXECUTORCH_BUILD_WEBGPU_TEST) |
| 89 | + add_executable(webgpu_native_test test/test_webgpu_native.cpp) |
| 90 | + |
| 91 | + target_include_directories( |
| 92 | + webgpu_native_test PRIVATE $<BUILD_INTERFACE:${EXECUTORCH_ROOT}/..> |
| 93 | + "${WGPU_NATIVE_DIR}/include" |
| 94 | + ) |
| 95 | + |
| 96 | + target_link_libraries( |
| 97 | + webgpu_native_test |
| 98 | + PRIVATE webgpu_backend |
| 99 | + wgpu_native |
| 100 | + executorch_core |
| 101 | + extension_module_static |
| 102 | + extension_data_loader |
| 103 | + extension_tensor |
| 104 | + portable_kernels |
| 105 | + portable_ops_lib |
| 106 | + ) |
| 107 | + |
| 108 | + if(APPLE) |
| 109 | + target_link_libraries( |
| 110 | + webgpu_native_test PRIVATE "-framework Metal" "-framework QuartzCore" |
| 111 | + "-framework CoreGraphics" |
| 112 | + ) |
| 113 | + else() |
| 114 | + target_link_libraries(webgpu_native_test PRIVATE dl m pthread) |
| 115 | + endif() |
| 116 | + |
| 117 | + target_compile_options(webgpu_native_test PRIVATE -fexceptions) |
| 118 | + set_property(TARGET webgpu_native_test PROPERTY CXX_STANDARD 17) |
| 119 | +endif() |
0 commit comments