From 09a773985860869854dee19c04cc7530917dcb17 Mon Sep 17 00:00:00 2001 From: Filippo Zallocco Date: Tue, 5 May 2026 16:32:40 -0400 Subject: [PATCH] improved portability for Apple M-series chips --- Makefile | 21 +++++++++++++++++---- README.md | 6 +++++- cmake/webgpu.cmake | 14 ++++---------- examples/Makefile | 4 ++-- third_party/fetchcontent/.gitkeep | 0 5 files changed, 28 insertions(+), 17 deletions(-) delete mode 100644 third_party/fetchcontent/.gitkeep diff --git a/Makefile b/Makefile index 8e5d67b..d7ceb9d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,11 @@ -NUM_JOBS=$(shell nproc) +OS := $(shell uname | tr -d '\n') + +ifeq ($(OS),Darwin) +NUM_JOBS := $(shell sysctl -n hw.ncpu) +else +NUM_JOBS := $(shell nproc) +endif + CXX=clang++ .PHONY: default examples/hello_world/build/hello_world tests libgpu debug build check-clang clean-build clean all watch-tests docs @@ -20,7 +27,7 @@ pch: # TODO(avh): change extension based on platform # Get the current OS name -OS = $(shell uname | tr -d '\n') + # Set the specific variables for each platform LIB_PATH ?= /usr/lib HEADER_PATH ?= /usr/include @@ -89,7 +96,13 @@ RELEASE_FLAGS = $(FLAGS) -DFASTBUILD:BOOL=OFF TARGET_LIB=gpu libgpu-cmake: check-clang check-cmake - $(CMAKE_CMD) $(RELEASE_FLAGS) && make -j$(NUM_JOBS) gpu + $(CMAKE_CMD) $(RELEASE_FLAGS) && cmake --build . --parallel $(NUM_JOBS) --target gpud + mkdir -p third_party/lib +ifeq ($(OS),Darwin) + find third_party/fetchcontent -name "*.dylib" -exec cp {} third_party/lib/ \; +else ifeq ($(OS),Linux) + find third_party/fetchcontent -name "*.so*" -exec cp {} third_party/lib/ \; +endif debug-cmake: check-clang check-cmake $(CMAKE_CMD) $(DEBUG_FLAGS) && make -j$(NUM_JOBS) $(TARGET_ALL) @@ -102,7 +115,7 @@ all-cmake: check-clang check-cmake ################################################################################ clean-dawnlib: - rm -f third_party/lib/libwebgpu_dawn.so third_party/lib/libwebgpu_dawn.dylib + rm -f third_party/lib/*.so third_party/lib/*.so.* third_party/lib/*.dylib clean: read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* diff --git a/README.md b/README.md index 46340b7..566dcc2 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,11 @@ Optionally, Dawn can be built from scratch with gpu.cpp using the cmake build sc After cloning the repo, from the top-level gpu.cpp, you should be able to build and run the hello world GELU example by typing: ``` -make +make libgpu-cmake +cd examples +make clean +make hello_world +make run_hello_world ``` The first time you build and run the project this way, it will download a prebuilt shared library for the Dawn native WebGPU implementation automatically (using the setup.py script). This places the Dawn shared library in the `third_party/lib` directory. Afterwards you should see `libdawn.dylib` on MacOS or `libdawn.so` on Linux. This download only occurs once. diff --git a/cmake/webgpu.cmake b/cmake/webgpu.cmake index c63f1e2..2ef9608 100644 --- a/cmake/webgpu.cmake +++ b/cmake/webgpu.cmake @@ -39,19 +39,13 @@ endif() option(WEBGPU_TAG "WebGPU distribution tag to use") if(NOT WEBGPU_TAG) - set(WEBGPU_TAG "dawn") + set(WEBGPU_TAG "main") endif() message(STATUS "Using WebGPU distribution tag: ${WEBGPU_TAG}") -if(WEBGPU_TAG STREQUAL "dawn") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWEBGPU_BACKEND_DAWN") - # use specific commit set(WEBGPU_TAG - # "1025b977e1927b6d0327e67352f90feb4bcf8274") set(WEBGPU_TAG - # "acf972b7b909f52e183bdae3971b93bb13d4a29e") - # add_compile_options(-UABSL_INTERNAL_AT_LEAST_CXX20) set(CMAKE_CXX_FLAGS - # "${CMAKE_CXX_FLAGS} -UABSL_INTERNAL_AT_LEAST_CXX20") - message(STATUS "Using Dawn backend") -endif() +set(WEBGPU_BACKEND "DAWN" CACHE STRING "WebGPU backend") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWEBGPU_BACKEND_DAWN") +message(STATUS "Using Dawn backend") FetchContent_Declare( webgpu diff --git a/examples/Makefile b/examples/Makefile index 3036e22..929779b 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -14,7 +14,7 @@ else endif FLAGS=-std=c++17 $(STDLIB) -I$(GPUCPP) -I$(GPUCPP)/third_party/headers -L$(GPUCPP)/third_party/lib -LFLAGS=-ldl -lwebgpu_dawn +LFLAGS=-Wl,-rpath,$(GPUCPP)/third_party/lib -ldl -lwebgpu_dawn .PHONY: default all_release all_debug dawnlib run_setup check-python .PHONY: $(addsuffix _release, $(TARGETS)) @@ -57,7 +57,7 @@ $(foreach target,$(TARGETS),$(eval $(call CLEAN_RULES,$(target)))) clean: $(addprefix clean_, $(TARGETS)) -dawnlib: $(if $(wildcard $(GPUCPP)/third_party/lib/libdawn.so $(GPUCPP)/third_party/lib/libdawn.dylib),,run_setup) +dawnlib: $(if $(wildcard $(GPUCPP)/third_party/lib/libwebgpu_dawn.so $(GPUCPP)/third_party/lib/libwebgpu_dawn.dylib),,run_setup) run_setup: check-python cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) diff --git a/third_party/fetchcontent/.gitkeep b/third_party/fetchcontent/.gitkeep deleted file mode 100644 index e69de29..0000000