From a178579f9bf5d3f863d35c1b718d3e5cecae137f Mon Sep 17 00:00:00 2001 From: Julian Ng-Thow-Hing Date: Tue, 21 Jul 2026 21:11:18 -0700 Subject: [PATCH] Update [ghstack-poisoned] --- backends/webgpu/CMakeLists.txt | 32 ++++++++++--------- .../webgpu/test/test_cmake_configuration.py | 26 +++++++++++++++ 2 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 backends/webgpu/test/test_cmake_configuration.py diff --git a/backends/webgpu/CMakeLists.txt b/backends/webgpu/CMakeLists.txt index 4c9f1265c4e..c548baaaee2 100644 --- a/backends/webgpu/CMakeLists.txt +++ b/backends/webgpu/CMakeLists.txt @@ -61,22 +61,24 @@ target_include_directories( target_link_libraries(webgpu_backend PRIVATE vulkan_schema executorch_core) -# Native WebGPU backend: Dawn (Tint) + SwiftShader; deps script sets Dawn_DIR. -# Native-only: browser/Emscripten builds use the system webgpu.h and never reach -# this find_package (root CMake gates it via EXECUTORCH_BUILD_WEBGPU). -# dawn::webgpu_dawn's link interface references Threads::Threads. -find_package(Threads REQUIRED) -find_package(Dawn REQUIRED) -set(WEBGPU_GPU_LIB dawn::webgpu_dawn) -target_link_libraries(webgpu_backend PUBLIC ${WEBGPU_GPU_LIB}) - -if(APPLE) - target_link_libraries( - webgpu_backend PRIVATE "-framework Metal" "-framework QuartzCore" - "-framework CoreGraphics" "-framework Foundation" - ) +# WASM gets its WebGPU implementation from emdawnwebgpu at executable link time. +# Native builds link Dawn (Tint) and the platform GPU libraries. +if(EMSCRIPTEN) + target_compile_options(webgpu_backend PUBLIC "--use-port=emdawnwebgpu") else() - target_link_libraries(webgpu_backend PRIVATE dl m pthread) + find_package(Threads REQUIRED) + find_package(Dawn REQUIRED) + set(WEBGPU_GPU_LIB dawn::webgpu_dawn) + target_link_libraries(webgpu_backend PUBLIC ${WEBGPU_GPU_LIB}) + + if(APPLE) + target_link_libraries( + webgpu_backend PRIVATE "-framework Metal" "-framework QuartzCore" + "-framework CoreGraphics" "-framework Foundation" + ) + else() + target_link_libraries(webgpu_backend PRIVATE dl m pthread) + endif() endif() target_compile_options(webgpu_backend PRIVATE -fexceptions) diff --git a/backends/webgpu/test/test_cmake_configuration.py b/backends/webgpu/test/test_cmake_configuration.py new file mode 100644 index 00000000000..39987a076b1 --- /dev/null +++ b/backends/webgpu/test/test_cmake_configuration.py @@ -0,0 +1,26 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +import pathlib +import unittest + + +class TestCMakeConfiguration(unittest.TestCase): + def test_emscripten_uses_port_instead_of_native_dawn(self) -> None: + cmake = pathlib.Path(__file__).parents[1] / "CMakeLists.txt" + source = cmake.read_text() + wasm_branch = """if(EMSCRIPTEN) + target_compile_options(webgpu_backend PUBLIC \"--use-port=emdawnwebgpu\") +else()""" + + self.assertIn(wasm_branch, source) + branch_start = source.index(wasm_branch) + branch_end = source.index("\nendif()", branch_start) + self.assertIn("find_package(Dawn REQUIRED)", source[branch_start:branch_end]) + + +if __name__ == "__main__": + unittest.main()