From 8772b34742c888b5c6bf6783f8166a47bd57bec4 Mon Sep 17 00:00:00 2001 From: Elie Michel Date: Mon, 10 Apr 2023 22:17:39 +0200 Subject: [PATCH 1/7] Add distribution template dir --- distribution/CMakeLists.txt | 114 +++++++++++++++++++++++++++ distribution/README.md | 69 ++++++++++++++++ distribution/bin/.gitkeep | 0 distribution/include/webgpu/.gitkeep | 0 4 files changed, 183 insertions(+) create mode 100644 distribution/CMakeLists.txt create mode 100644 distribution/README.md create mode 100644 distribution/bin/.gitkeep create mode 100644 distribution/include/webgpu/.gitkeep diff --git a/distribution/CMakeLists.txt b/distribution/CMakeLists.txt new file mode 100644 index 00000000..3594d6ab --- /dev/null +++ b/distribution/CMakeLists.txt @@ -0,0 +1,114 @@ +cmake_minimum_required(VERSION 3.0.0...3.24 FATAL_ERROR) +project(webgpu-backend-wgpu VERSION 1.0.0) + +message(STATUS "Using wgpu-native backend for WebGPU") + +if (EMSCRIPTEN) + + add_library(webgpu INTERFACE) + + target_include_directories(webgpu INTERFACE + "${CMAKE_CURRENT_SOURCE_DIR}/include-emscripten" + ) + + # This is used to advertise the flavor of WebGPU that this zip provides + target_compile_definitions(webgpu INTERFACE WEBGPU_BACKEND_EMSCRIPTEN) + + function(target_copy_webgpu_binaries Target) + endfunction() + +else (EMSCRIPTEN) + + set(WGPU ${CMAKE_CURRENT_SOURCE_DIR}) + if (NOT ARCH) + set(ARCH ${CMAKE_SYSTEM_PROCESSOR}) + if (ARCH STREQUAL "AMD64") + set(ARCH "x86_64") + endif() + endif() + + # A pre-compiled target (IMPORTED) that is a dynamically + # linked library (SHARED, meaning .dll, .so or .dylib). + add_library(webgpu SHARED IMPORTED GLOBAL) + + # This is used to advertise the flavor of WebGPU that this zip provides + target_compile_definitions(webgpu INTERFACE WEBGPU_BACKEND_WGPU) + + if(CMAKE_SYSTEM_NAME STREQUAL "Windows") + + set(WGPU_RUNTIME_LIB ${WGPU}/bin/windows-${ARCH}/wgpu_native.dll) + set_target_properties( + webgpu + PROPERTIES + IMPORTED_LOCATION "${WGPU_RUNTIME_LIB}" + IMPORTED_IMPLIB "${WGPU}/bin/windows-${ARCH}/wgpu_native.lib" + INTERFACE_INCLUDE_DIRECTORIES "${WGPU}/include" + ) + + elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") + + set(WGPU_RUNTIME_LIB ${WGPU}/bin/linux-${ARCH}/libwgpu_native.so) + set_target_properties( + webgpu + PROPERTIES + IMPORTED_LOCATION "${WGPU_RUNTIME_LIB}" + INTERFACE_INCLUDE_DIRECTORIES "${WGPU}/include" + ) + + elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + + set(WGPU_RUNTIME_LIB ${WGPU}/bin/macos-${ARCH}/libwgpu_native.dylib) + set_target_properties( + webgpu + PROPERTIES + IMPORTED_LOCATION "${WGPU_RUNTIME_LIB}" + INTERFACE_INCLUDE_DIRECTORIES "${WGPU}/include" + ) + + else() + + message(FATAL_ERROR "Plateform not supported by this release of WebGPU. You may consider building it yourself from its source (see https://github.com/gfx-rs/wgpu-native)") + + endif() + + message(STATUS "Using WebGPU runtime from '${WGPU_RUNTIME_LIB}'") + set(WGPU_RUNTIME_LIB ${WGPU_RUNTIME_LIB} PARENT_SCOPE) + set(WGPU_RUNTIME_LIB ${WGPU_RUNTIME_LIB} CACHE INTERNAL "Path to the WebGPU library binary") + + # The application's binary must find wgpu.dll or libwgpu.so at runtime, + # so we automatically copy it (it's called WGPU_RUNTIME_LIB in general) + # next to the binary. + # Also make sure that the binarie's RPATH is set to find this shared library. + function(target_copy_webgpu_binaries Target) + add_custom_command( + TARGET ${Target} POST_BUILD + COMMAND + ${CMAKE_COMMAND} -E copy_if_different + ${WGPU_RUNTIME_LIB} + $ + COMMENT + "Copying '${WGPU_RUNTIME_LIB}' to '$'..." + ) + set_target_properties(${Target} PROPERTIES INSTALL_RPATH "./") + + if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + # Bug fix, there might be a cleaner way to do this but no INSTALL_RPATH + # or related target properties seem to be a solution. + if(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") + set(ARCH_DIR aarch64) + else() + set(ARCH_DIR ${CMAKE_SYSTEM_PROCESSOR}) + endif() + add_custom_command( + TARGET ${Target} POST_BUILD + COMMAND + ${CMAKE_INSTALL_NAME_TOOL} "-change" + "/Users/runner/work/wgpu-native/wgpu-native/target/${ARCH_DIR}-apple-darwin/release/deps/libwgpu_native.dylib" + "@executable_path/libwgpu_native.dylib" + "$" + VERBATIM + ) + endif() + endfunction() + +endif (EMSCRIPTEN) diff --git a/distribution/README.md b/distribution/README.md new file mode 100644 index 00000000..bc61e642 --- /dev/null +++ b/distribution/README.md @@ -0,0 +1,69 @@ +WebGPU distribution +=================== + +This is pre-compiled distribution of [WebGPU native](https://github.com/webgpu-native/webgpu-headers) based on [`wgpu-native`](https://github.com/gfx-rs/wgpu-native) and ready to be used in a [CMake](https://cmake.org/) project. + +Suggested usage +--------------- + + 1. Unzip the content of this archive into your source tree in a directory called `webgpu/` + + 2. Edit you source tree's `CMakeLists.txt`: + +```CMake +# [...] Define your application target, called for instance `App` + +# Include this archive as a subdirectory +add_subdirectory(webgpu) + +# You now have a target `webgpu` against which you can link your application: +target_link_library(App PRIVATE webgpu) + +# The application's binary must find wgpu.dll or libwgpu.so at runtime, +# so we automatically copy it next to the binary. +target_copy_webgpu_binaries(App) + +# (Alternatively you can use the ${WGPU_RUNTIME_LIB} variable to get the +# binary that must be copied and handle it yourselves.) +``` + +Notes +----- + +### Preprocessor variable + +In order to statically distinguish this distribution of WebGPU from other possible backends, it defines the following preprocessor variable: + +```C +#define WEBGPU_BACKEND_WGPU +``` + +### Emscripten support + +The CMakeLists provided with this distribution is designed to transparently work with emscripten's `emcmake`: + +```bash +# Activate your emscripten installation +source /path/to/emsdk_env.sh +# Configure the project by prefixing with "emcmake" +emcmake cmake -B build-wasm +# Build the project as usual +cmake --build build-wasm +``` + +Reference +--------- + +When adding this distribution as a subdirectory in your CMake project, you get: + + - `webgpu`: A CMake target that provides the standard `webgpu.h` header and non-standard extensions `wgpu.h` as well as their implementation as a runtime library. + - `${WGPU_RUNTIME_LIB}`: A CMake variable containing the path to the runtime library that your program needs to locate (.dll on Windows, .so on linux, .dylib on macOS). + - `target_copy_webgpu_binaries(Target)`: A helper CMake function to automatically copy the runtime library next to the compiled application produced by target `Target`, so that it finds it out of the box. + - `#define WEBGPU_BACKEND_WGPU`: A preprocessor variable defined in any target linking agains `webgpu` that can be used to detect that this distribution is based on a `wgpu-native` backend. + +The `webgpu` target provides the following headers: + +```C +#include // standard webgpu-native header +#include // non-standard extensions +``` diff --git a/distribution/bin/.gitkeep b/distribution/bin/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/distribution/include/webgpu/.gitkeep b/distribution/include/webgpu/.gitkeep new file mode 100644 index 00000000..e69de29b From f6b4983987a3f9e32b70ba71621ba939a36de56e Mon Sep 17 00:00:00 2001 From: Elie Michel Date: Mon, 10 Apr 2023 22:50:52 +0200 Subject: [PATCH 2/7] Autogen distribution upon release --- .github/workflows/cd.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 11555f73..d241b73f 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -281,6 +281,8 @@ jobs: if: success() && contains(github.ref, 'tags/v') steps: - uses: actions/checkout@v3 + with: + submodules: 'true' - name: set version (which gets used as release name) run: | echo "WGPU_NATIVE_VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV @@ -314,3 +316,23 @@ jobs: release-tag: ${{ env.WGPU_NATIVE_VERSION }} files: 'dist/*.zip;dist/commit-sha' repo-token: ${{ secrets.GITHUB_TOKEN }} + - name: Prepare distribution + run: | + cp dist/commit-sha distribution + cp ffi/webgpu-headers/webgpu.h distribution/include/webgpu + cp ffi/wgpu.h distribution/include/webgpu + for zipfile in wgpu-*-release.zip + arch=$(echo $zipfile | cut -d- -f2-3) + mkdir -p distribution/bin/$arch + unzip dist/wgpu-*-release.zip distribution/bin/ + unzip -d distribution/bin $zipfile *.so *.dylib *.dll + done + mv distribution webgpu + zip -r wgpu-distribution.zip webgpu + - name: Upload Release Distribution + # Move back to official action after fix https://github.com/actions/upload-release-asset/issues/4 + uses: AButler/upload-release-assets@v2.0 + with: + release-tag: ${{ env.WGPU_NATIVE_VERSION }} + files: 'wgpu-distribution.zip' + repo-token: ${{ secrets.GITHUB_TOKEN }} From e75cde949eda3b7eea9ee6977286a82de06e1209 Mon Sep 17 00:00:00 2001 From: Elie Michel Date: Mon, 10 Apr 2023 23:11:34 +0200 Subject: [PATCH 3/7] Separate cmake dist gen from publish --- .github/workflows/cd.yml | 96 +++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 22 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index d241b73f..181d22e5 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -271,12 +271,80 @@ jobs: path: dist name: dist + distribution-release: + name: Create easy-to-integrate distributions (release) + needs: [linux-x86_64, linux-i686, windows-x86_64, windows-i686, macos-x86_64, macos-arm64] + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v3 + - name: Download assets + uses: actions/download-artifact@v3 + with: + name: dist + - name: Create commit-sha file + env: + GITHUB_SHA: ${{ github.sha }} + run: | + echo $GITHUB_SHA > dist/commit-sha + - name: Prepare distribution + run: | + cp dist/commit-sha distribution + cp ffi/webgpu-headers/webgpu.h distribution/include/webgpu + cp ffi/wgpu.h distribution/include/webgpu + for zipfile in wgpu-*-release.zip + arch=$(echo $zipfile | cut -d- -f2-3) + mkdir -p distribution/bin/$arch + unzip dist/wgpu-*-release.zip distribution/bin/ + unzip -d distribution/bin $zipfile *.so *.dylib *.dll + done + mv distribution webgpu + zip -r wgpu-distribution.zip webgpu + - name: Upload + uses: actions/upload-artifact@v3 + with: + path: wgpu-distribution.zip + name: dist-cmake + + distribution-debug: + name: Create easy-to-integrate distributions (debug) + needs: [linux-x86_64, linux-i686, windows-x86_64, windows-i686, macos-x86_64, macos-arm64] + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v3 + - name: Download assets + uses: actions/download-artifact@v3 + with: + name: dist + - name: Create commit-sha file + env: + GITHUB_SHA: ${{ github.sha }} + run: | + echo $GITHUB_SHA > dist/commit-sha + - name: Prepare distribution + run: | + cp dist/commit-sha distribution + cp ffi/webgpu-headers/webgpu.h distribution/include/webgpu + cp ffi/wgpu.h distribution/include/webgpu + for zipfile in wgpu-*-debug.zip + arch=$(echo $zipfile | cut -d- -f2-3) + mkdir -p distribution/bin/$arch + unzip dist/wgpu-*-debug.zip distribution/bin/ + unzip -d distribution/bin $zipfile *.so *.dylib *.dll + done + mv distribution webgpu + zip -r wgpu-distribution.zip webgpu + - name: Upload + uses: actions/upload-artifact@v3 + with: + path: wgpu-distribution.zip + name: dist-cmake + # Create a Github release and upload the binary libs that we just built. # There should be a release and debug build for each platform (win32, win64, MacOS64, Linux32, Linux64), # plus a file containing the commit sha. publish: name: Publish Github release - needs: [linux-x86_64, linux-i686, windows-x86_64, windows-i686, macos-x86_64, macos-arm64] + needs: [linux-x86_64, linux-i686, windows-x86_64, windows-i686, macos-x86_64, macos-arm64, distribution-release, distribution-debug] runs-on: ubuntu-18.04 if: success() && contains(github.ref, 'tags/v') steps: @@ -291,6 +359,10 @@ jobs: uses: actions/download-artifact@v3 with: name: dist + - name: Download distributions + uses: actions/download-artifact@v3 + with: + name: dist-cmake - name: Create commit-sha file env: GITHUB_SHA: ${{ github.sha }} @@ -314,25 +386,5 @@ jobs: uses: AButler/upload-release-assets@v2.0 with: release-tag: ${{ env.WGPU_NATIVE_VERSION }} - files: 'dist/*.zip;dist/commit-sha' - repo-token: ${{ secrets.GITHUB_TOKEN }} - - name: Prepare distribution - run: | - cp dist/commit-sha distribution - cp ffi/webgpu-headers/webgpu.h distribution/include/webgpu - cp ffi/wgpu.h distribution/include/webgpu - for zipfile in wgpu-*-release.zip - arch=$(echo $zipfile | cut -d- -f2-3) - mkdir -p distribution/bin/$arch - unzip dist/wgpu-*-release.zip distribution/bin/ - unzip -d distribution/bin $zipfile *.so *.dylib *.dll - done - mv distribution webgpu - zip -r wgpu-distribution.zip webgpu - - name: Upload Release Distribution - # Move back to official action after fix https://github.com/actions/upload-release-asset/issues/4 - uses: AButler/upload-release-assets@v2.0 - with: - release-tag: ${{ env.WGPU_NATIVE_VERSION }} - files: 'wgpu-distribution.zip' + files: 'dist/*.zip;dist-cmake/*.zip;dist/commit-sha' repo-token: ${{ secrets.GITHUB_TOKEN }} From c3127c42aebcf3f3fd11e4677c5d560174afc71d Mon Sep 17 00:00:00 2001 From: Elie Michel Date: Tue, 11 Apr 2023 23:14:39 +0200 Subject: [PATCH 4/7] Update distribution/README.md Co-authored-by: Almar Klein --- distribution/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distribution/README.md b/distribution/README.md index bc61e642..b737f414 100644 --- a/distribution/README.md +++ b/distribution/README.md @@ -1,7 +1,7 @@ WebGPU distribution =================== -This is pre-compiled distribution of [WebGPU native](https://github.com/webgpu-native/webgpu-headers) based on [`wgpu-native`](https://github.com/gfx-rs/wgpu-native) and ready to be used in a [CMake](https://cmake.org/) project. +This is a pre-compiled distribution of [WebGPU native](https://github.com/webgpu-native/webgpu-headers) based on [`wgpu-native`](https://github.com/gfx-rs/wgpu-native) and ready to be used in a [CMake](https://cmake.org/) project. Suggested usage --------------- From 18ed83182757db74baa0bb158d01240d38e7cafa Mon Sep 17 00:00:00 2001 From: Elie Michel Date: Wed, 12 Apr 2023 08:25:52 +0200 Subject: [PATCH 5/7] Avoid name conflict between release and debug dist --- .github/workflows/cd.yml | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 8a614e42..7f00147a 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -298,12 +298,12 @@ jobs: unzip -d distribution/bin $zipfile *.so *.dylib *.dll done mv distribution webgpu - zip -r wgpu-distribution.zip webgpu + zip -r wgpu-distribution-release.zip webgpu - name: Upload uses: actions/upload-artifact@v3 with: path: wgpu-distribution.zip - name: dist-cmake + name: dist distribution-debug: name: Create easy-to-integrate distributions (debug) @@ -332,12 +332,12 @@ jobs: unzip -d distribution/bin $zipfile *.so *.dylib *.dll done mv distribution webgpu - zip -r wgpu-distribution.zip webgpu + zip -r wgpu-distribution-debug.zip webgpu - name: Upload uses: actions/upload-artifact@v3 with: path: wgpu-distribution.zip - name: dist-cmake + name: dist # Create a Github release and upload the binary libs that we just built. # There should be a release and debug build for each platform (win32, win64, MacOS64, Linux32, Linux64), @@ -360,11 +360,6 @@ jobs: with: name: dist path: dist - - name: Download distributions - uses: actions/download-artifact@v3 - with: - name: dist-cmake - path: dist-cmake - name: Create commit-sha file env: GITHUB_SHA: ${{ github.sha }} @@ -388,5 +383,5 @@ jobs: uses: AButler/upload-release-assets@v2.0 with: release-tag: ${{ env.WGPU_NATIVE_VERSION }} - files: 'dist/*.zip;dist-cmake/*.zip;dist/commit-sha' + files: 'dist/*.zip;dist/commit-sha' repo-token: ${{ secrets.GITHUB_TOKEN }} From a0195f99a24815848d950a3ee2f2f498845215a4 Mon Sep 17 00:00:00 2001 From: Elie Michel Date: Thu, 13 Apr 2023 22:33:08 +0200 Subject: [PATCH 6/7] Update .github/workflows/cd.yml Co-authored-by: Almar Klein --- .github/workflows/cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 7f00147a..4998791d 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -302,7 +302,7 @@ jobs: - name: Upload uses: actions/upload-artifact@v3 with: - path: wgpu-distribution.zip + path: wgpu-distribution-release.zip name: dist distribution-debug: From 566cf8a50ef7accf636fc2be812e3794941333cf Mon Sep 17 00:00:00 2001 From: Elie Michel Date: Thu, 13 Apr 2023 22:33:20 +0200 Subject: [PATCH 7/7] Update .github/workflows/cd.yml Co-authored-by: Almar Klein --- .github/workflows/cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 4998791d..6df8a7dd 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -336,7 +336,7 @@ jobs: - name: Upload uses: actions/upload-artifact@v3 with: - path: wgpu-distribution.zip + path: wgpu-distribution-debug.zip name: dist # Create a Github release and upload the binary libs that we just built.