From 2c4a9b99ae3c02cbf9b36041c539875fc84076d6 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Wed, 17 Jul 2024 21:11:44 -0500 Subject: [PATCH 01/24] Adds windows support --- .gitignore | 5 +- CMakeLists.txt | 62 ++-------- Makefile | 168 +++++++++++++++++++++------- cmake/gpu.cmake | 59 ++++++++++ cmake/webgpu.cmake | 55 +++++++++ examples/gpu_puzzles/CMakeLists.txt | 57 ++++++---- examples/gpu_puzzles/Makefile | 99 +++++++++++++--- examples/hello_world/Makefile | 102 +++++++++++++---- examples/matmul/CMakeLists.txt | 46 ++++++++ examples/matmul/Makefile | 107 ++++++++++++++---- examples/physics/CMakeLists.txt | 46 ++++++++ examples/physics/Makefile | 98 +++++++++++++--- examples/render/CMakeLists.txt | 46 ++++++++ examples/render/Makefile | 99 +++++++++++++--- examples/shadertui/CMakeLists.txt | 46 ++++++++ examples/shadertui/Makefile | 107 ++++++++++++++---- setup.py | 82 +++++++++++--- third_party/fetchcontent/.gitkeep | 0 18 files changed, 1048 insertions(+), 236 deletions(-) create mode 100644 cmake/gpu.cmake create mode 100644 cmake/webgpu.cmake create mode 100644 examples/matmul/CMakeLists.txt create mode 100644 examples/physics/CMakeLists.txt create mode 100644 examples/render/CMakeLists.txt create mode 100644 examples/shadertui/CMakeLists.txt delete mode 100644 third_party/fetchcontent/.gitkeep diff --git a/.gitignore b/.gitignore index 029eb4e..99e35c5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,5 @@ examples/raymarch/build/* docs/html source .DS_Store -third_party/lib/libdawn.* -third_party/lib/*.so -third_party/lib/*.dylib +third_party/lib/* +NUL \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 11a1417..21d6414 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,52 +1,19 @@ cmake_minimum_required(VERSION 3.11) project(gpu) -include(FetchContent) -message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}") -set(FETCHCONTENT_BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/fetchcontent") +include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/webgpu.cmake") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) option(USE_LOCAL_LIBS "Use local libraries instead of fetching from the internet" OFF) -# Define paths for local and remote repositories -set(WEBGPU_DIST_LOCAL_PATH "${CMAKE_CURRENT_SOURCE_DIR}/third_party/local/WebGPU-distribution") - -# Conditional assignment based on USE_LOCAL_LIBS -if(USE_LOCAL_LIBS) - set(WEBGPU_DIST_GIT_REPO ${WEBGPU_DIST_LOCAL_PATH}) - message(STATUS "Using local WebGPU distribution: ${WEBGPU_DIST_LOCAL_PATH}") -else() - set(WEBGPU_DIST_GIT_REPO "https://github.com/eliemichel/WebGPU-distribution") -endif() - - -option(WEBGPU_TAG "WebGPU distribution tag to use") -IF (NOT WEBGPU_TAG) - set(WEBGPU_TAG "dawn") -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") +# Ensure the build type is set +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) endif() -FetchContent_Declare( - webgpu - GIT_REPOSITORY ${WEBGPU_DIST_GIT_REPO} - GIT_TAG ${WEBGPU_TAG} - GIT_SHALLOW TRUE -) -FetchContent_MakeAvailable(webgpu) - option(FASTBUILD "Option to enable fast builds" OFF) if(FASTBUILD) set(CMAKE_BUILD_TYPE None) # Avoid default flags of predefined build types @@ -59,19 +26,14 @@ if(DEBUG) set(CMAKE_CXX_FLAGS "-O0 -g") endif() -# dl for dlopen/dlysm/dlclose -find_library(LIBDL dl REQUIRED) -if(LIBDL) - message(STATUS "Found libdl: ${LIBDL}") -else() - message(FATAL_ERROR "libdl not found") - exit() +if(WIN64) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWEBGPU_BACKEND_DAWN") endif() +include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/gpu.cmake") -# Build the library target (libgpu) +message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}") +message(STATUS "Include directories for wgpu: ${CMAKE_CURRENT_SOURCE_DIR}/third_party/headers") -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) -set(SRC_LIB gpu.h utils/shaders.h utils/array_utils.h utilslogging.h) -add_library(gpu SHARED ${SRC_LIB}) -set_target_properties(gpu PROPERTIES LINKER_LANGUAGE CXX) +# Add subdirectories for examples +add_subdirectory(examples/hello_world) diff --git a/Makefile b/Makefile index 9890711..bc7f56a 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,81 @@ -NUM_JOBS=$(shell nproc) +NUM_JOBS=$(shell nproc 2>/dev/null || echo 1) CXX=clang++ -.PHONY: default examples/hello_world/build/hello_world tests libgpu debug build check-entr check-clang clean-build clean all watch-tests docs - -GPUCPP ?= $(PWD) -LIBDIR ?= $(GPUCPP)/third_party/lib -LIBSPEC ?= . $(GPUCPP)/source - -default: examples/hello_world/build/hello_world - -examples/hello_world/build/hello_world: check-clang dawnlib examples/hello_world/run.cpp check-linux-vulkan - $(LIBSPEC) && cd examples/hello_world && make build/hello_world && ./build/hello_world - -dawnlib: $(if $(wildcard third_party/lib/libdawn.so third_party/lib/libdawn.dylib),,run_setup) +.PHONY: default examples/hello_world/build/hello_world tests libgpu debug build check-entr check-clang clean-build clean clean-dawnlib all watch-tests docs + +# Set up variables for cross-platform compatibility +ifeq ($(OS),Windows_NT) + DETECTED_OS := Windows + MKDIR_CMD := if not exist build mkdir build + RMDIR_CMD := rmdir + SLASH := \\ + LS_CMD := dir + LDLIB_SUFFIX := dll + EXPORT_CMD := set +else + DETECTED_OS := $(shell uname) + MKDIR_CMD := mkdir -p build + RMDIR_CMD := rm -rf + SLASH := / + LS_CMD := ls + LDLIB_SUFFIX := so + EXPORT_CMD := export +endif + +# Determine the architecture +ifeq ($(DETECTED_OS), Windows) + ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) + ARCH := x64 + else + ARCH := x86 + endif +else + ARCH := $(shell uname -m) + ifeq ($(ARCH), x86_64) + ARCH := x64 + else ifneq (,$(findstring arm, $(ARCH))) + ARCH := arm + endif +endif + +# Determine the build type +BUILD_TYPE ?= Release +LOWER_BUILD_TYPE ?= $(shell python3 -c "print('$(BUILD_TYPE)'.lower())") + +# Paths +GPUCPP ?= $(shell pwd) +LIBDIR ?= $(GPUCPP)$(SLASH)third_party$(SLASH)lib +LIBSPEC ?= . $(GPUCPP)$(SLASH)source + +default: examples_hello_world_build_hello_world + +examples_hello_world_build_hello_world: check-clang dawnlib examples/hello_world/run.cpp check-linux-vulkan +ifeq ($(DETECTED_OS), Windows) + cd examples$(SLASH)hello_world && $(MAKE) build_hello_world_$(LOWER_BUILD_TYPE) +else + $(LIBSPEC) && cd examples$(SLASH)hello_world && $(MAKE) build_hello_world_$(LOWER_BUILD_TYPE) +endif + +# We use the custom "shell" based condition to check files cross-platform +dawnlib: +ifeq ($(DETECTED_OS), Windows) + @if not exist "$(LIBDIR)$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(LIBDIR)$(SLASH)libdawn.dll" $(MAKE) run_setup +else + @if [ ! -f "$(LIBDIR)$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(LIBDIR)$(SLASH)libdawn.so" ] && [ ! -f "$(LIBDIR)$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ + $(MAKE) run_setup; \ + fi +endif run_setup: check-python - python3 setup.py + python3 setup.py | python setup.py + all: dawnlib check-clang check-linux-vulkan - cd examples/gpu_puzzles && make build/gpu_puzzles - cd examples/hello_world && make build/hello_world - cd examples/matmul && make build/mm - cd examples/physics && make build/physics - cd examples/render && make build/render + cd examples$(SLASH)gpu_puzzles && make build$(SLASH)gpu_puzzles + cd examples$(SLASH)hello_world && make build$(SLASH)hello_world + cd examples$(SLASH)matmul && make build$(SLASH)mm + cd examples$(SLASH)physics && make build$(SLASH)physics + cd examples$(SLASH)render && make build$(SLASH)render docs: Doxyfile doxygen Doxyfile @@ -31,7 +84,7 @@ docs: Doxyfile # cmake targets (optional - precompiled binaries is preferred) ################################################################################ -CMAKE_CMD = mkdir -p build && cd build && cmake .. +CMAKE_CMD = $(MKDIR_CMD) && cd build && cmake .. # Add --trace to see the cmake commands FLAGS = -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_CXX_COMPILER=$(CXX) -DABSL_INTERNAL_AT_LEAST_CXX20=OFF FASTBUILD_FLAGS = $(FLAGS) -DFASTBUILD:BOOL=ON @@ -53,46 +106,81 @@ all-cmake: check-clang check-cmake ################################################################################ clean-dawnlib: - rm -f third_party/lib/libdawn.so third_party/lib/libdawn.dylib + $(RMDIR_CMD) $(LIBDIR)$(SLASH)libdawn*.* clean: - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* - rm -rf examples/gpu_puzzles/build/* - rm -rf examples/hello_world/build/* +ifeq ($(DETECTED_OS), Windows) + @if exist build $(RMDIR_CMD) build /s /q + @if exist examples$(SLASH)gpu_puzzles$(SLASH)build $(RMDIR_CMD) examples$(SLASH)gpu_puzzles$(SLASH)build /s /q + @if exist examples$(SLASH)hello_world$(SLASH)build $(RMDIR_CMD) examples$(SLASH)hello_world$(SLASH)build /s /q + @if exist examples$(SLASH)matmul$(SLASH)build$(SLASH)mm $(RMDIR_CMD) examples$(SLASH)matmul$(SLASH)build$(SLASH)mm /s /q + @if exist examples$(SLASH)physics$(SLASH)build $(RMDIR_CMD) examples$(SLASH)physics$(SLASH)build /s /q + @if exist examples$(SLASH)render$(SLASH)build $(RMDIR_CMD) examples$(SLASH)render$(SLASH)build /s /q + $(MKDIR_CMD) +else + @command read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* + rm -rf examples/gpu_puzzles/build* + rm -rf examples/hello_world/build* rm -rf examples/matmul/build/mm - rm -rf examples/physics/build/* - rm -rf examples/render/build/* + rm -rf examples/physics/build* + rm -rf examples/render/build* +endif clean-all: - read -r -p "This will delete the contents of build/* and third_party/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* third_party/fetchcontent/* third_party/gpu-build third_party/gpu-subbuild third_party/gpu-src third_party/lib/libdawn.so third_party/lib/libdawn.dylib +ifeq ($(DETECTED_OS), Windows) + @if exist build $(RMDIR_CMD) build /s /q + $(RMDIR_CMD) third_party$(SLASH)fetchcontent /s /q + $(RMDIR_CMD) third_party$(SLASH)gpu-build /s /q + $(RMDIR_CMD) third_party$(SLASH)gpu-subbuild /s /q + $(RMDIR_CMD) third_party$(SLASH)gpu-src /s /q + $(RMDIR_CMD) third_party$(SLASH)lib /s /q + $(MKDIR_CMD) +else + read -r -p "This will delete the contents of build/* and third_party/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* third_party/fetchcontent* third_party/gpu-build third_party/gpu-subbuild third_party/gpu-src third_party/lib/libdawn* third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).* +endif + ################################################################################ # Checks ################################################################################ -# check for the existence of clang++ and cmake +# check for the existence of clang++ check-clang: +ifeq ($(OS),Windows_NT) + @if not exist "$(shell where clang++.exe 2>NUL)" (echo "Please install clang++ with 'sudo apt-get install clang' or 'brew install llvm'" & exit 1) +else @command -v clang++ >/dev/null 2>&1 || { echo >&2 "Please install clang++ with 'sudo apt-get install clang' or 'brew install llvm'"; exit 1; } +endif +# check for the existence of entr check-entr: +ifeq ($(OS),Windows_NT) + @if not exist "$(shell where entr.exe 2>NUL)" (echo "Please install entr with 'brew install entr' or 'sudo apt-get install entr'" & exit 1) +else @command -v entr >/dev/null 2>&1 || { echo >&2 "Please install entr with 'brew install entr' or 'sudo apt-get install entr'"; exit 1; } +endif +# check for the existence of cmake check-cmake: +ifeq ($(OS),Windows_NT) + @if not exist "$(shell where cmake.exe 2>NUL)" (echo "Please install cmake with 'sudo apt-get install cmake' or 'brew install cmake'" & exit 1) +else @command -v cmake >/dev/null 2>&1 || { echo >&2 "Please install cmake with 'sudo apt-get install cmake' or 'brew install cmake'"; exit 1; } +endif +# check for the existence of python3 check-python: - @command -v python3 >/dev/null 2>&1 || { echo >&2 "Python needs to be installed and in your path."; exit 1; } +ifeq ($(OS),Windows_NT) + @if not exist "$(shell where python3.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) +else + @command -v python3 >/dev/null 2>&1 || { echo >&2 "Python needs to be installed and in your path."; exit 1; } +endif +# check the existence of Vulkan (Linux only) check-linux-vulkan: @echo "Checking system type and Vulkan availability..." - @if [ "$$(uname)" = "Linux" ]; then \ - if command -v vulkaninfo >/dev/null 2>&1; then \ - echo "Vulkan is installed."; \ - vulkaninfo; \ - else \ - echo "Vulkan is not installed. Please install Vulkan drivers to continue. On Debian / Ubuntu: sudo apt install libvulkan1 mesa-vulkan-drivers vulkan-tools"; \ - exit 1; \ - fi \ - else \ - echo "Non-Linux system detected. Skipping Vulkan check."; \ - fi +ifeq ($(OS),Linux) + @command -v vulkaninfo >/dev/null 2>&1 && { echo "Vulkan is installed."; vulkaninfo; } || { echo "Vulkan is not installed. Please install Vulkan drivers to continue. On Debian / Ubuntu: sudo apt install libvulkan1 mesa-vulkan-drivers vulkan-tools"; exit 1; } +else + @echo "Non-Linux system detected. Skipping Vulkan check."; +endif \ No newline at end of file diff --git a/cmake/gpu.cmake b/cmake/gpu.cmake new file mode 100644 index 0000000..e257cbf --- /dev/null +++ b/cmake/gpu.cmake @@ -0,0 +1,59 @@ +# Specify the filename to search for +set(FILENAME "gpu.h") + +get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) + +# Construct potential paths +set(FILEPATH_CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}") +set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/${FILENAME}") + +# Check if the file exists in the current directory +if(EXISTS ${FILEPATH_CURRENT_DIR}) + set(TARGET_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +elseif(EXISTS ${FILEPATH_PROJECT_ROOT}) + set(TARGET_FILE_PATH ${PROJECT_ROOT}) +else() + message(FATAL_ERROR "File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../") +endif() + +# Define architecture and build type directories or file names +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(ARCH "x64") +else() + set(ARCH "x86") +endif() + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(BUILD_TYPE "Debug") +else() + set(BUILD_TYPE "Release") +endif() + +add_library(webgpulib SHARED IMPORTED) +add_library(gpu INTERFACE) +add_library(wgpu INTERFACE) +add_dependencies(gpu webgpulib) +# Define the header-only library +target_include_directories(gpu INTERFACE ${TARGET_FILE_PATH}) + +# Add headers webgpu.h +target_include_directories(wgpu INTERFACE ${TARGET_FILE_PATH}/third_party/headers) +if(WIN32) + set(DLL_PATH "${TARGET_FILE_PATH}/third_party/lib/libdawn_${ARCH}_${BUILD_TYPE}.dll") + if(EXISTS ${DLL_PATH}) + file(COPY ${DLL_PATH} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + target_link_libraries(webgpulib INTERFACE ${DLL_PATH}) + else() + message(FATAL_ERROR "libdawn dll not found at: ${DLL_PATH}") +endif() +else() + find_library(LIBDAWN dawn REQUIRED PATHS "${TARGET_FILE_PATH}/third_party/lib") + if(LIBDAWN) + message(STATUS "Found libdawn: ${LIBDAWN}") + # Link against libdawn + target_link_libraries(webgpulib INTERFACE ${LIBDAWN}) + else() + message(FATAL_ERROR "libdawn not found") + endif() +endif() \ No newline at end of file diff --git a/cmake/webgpu.cmake b/cmake/webgpu.cmake new file mode 100644 index 0000000..283ee72 --- /dev/null +++ b/cmake/webgpu.cmake @@ -0,0 +1,55 @@ +# Specify the filename to search for +set(FILENAME "gpu.h") + +get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) + +# Construct potential paths +set(FILEPATH_CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}") +set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/${FILENAME}") + +# Check if the file exists in the current directory +if(EXISTS ${FILEPATH_CURRENT_DIR}) + set(TARGET_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +elseif(EXISTS ${FILEPATH_PROJECT_ROOT}) + set(TARGET_FILE_PATH ${PROJECT_ROOT}) +else() + message(FATAL_ERROR "File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../") +endif() + +include(FetchContent) + + +set(FETCHCONTENT_BASE_DIR "${TARGET_FILE_PATH}/third_party/fetchcontent") +set(WEBGPU_DIST_LOCAL_PATH "${TARGET_FILE_PATH}/third_party/local/WebGPU-distribution") + +if(USE_LOCAL_LIBS) + set(WEBGPU_DIST_GIT_REPO ${WEBGPU_DIST_LOCAL_PATH}) + message(STATUS "Using local WebGPU distribution: ${WEBGPU_DIST_LOCAL_PATH}") +else() + set(WEBGPU_DIST_GIT_REPO "https://github.com/eliemichel/WebGPU-distribution") +endif() + +option(WEBGPU_TAG "WebGPU distribution tag to use") +if (NOT WEBGPU_TAG) + set(WEBGPU_TAG "dawn") +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() + +FetchContent_Declare( + webgpu + GIT_REPOSITORY ${WEBGPU_DIST_GIT_REPO} + GIT_TAG ${WEBGPU_TAG} + GIT_SHALLOW TRUE +) +FetchContent_MakeAvailable(webgpu) \ No newline at end of file diff --git a/examples/gpu_puzzles/CMakeLists.txt b/examples/gpu_puzzles/CMakeLists.txt index 331d695..cc78836 100644 --- a/examples/gpu_puzzles/CMakeLists.txt +++ b/examples/gpu_puzzles/CMakeLists.txt @@ -1,29 +1,46 @@ cmake_minimum_required(VERSION 3.11) project(gpu_puzzles) -include(FetchContent) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) -message(STATUS "CMAKE_CURRENT_SOURCE_DIR: " ${CMAKE_CURRENT_SOURCE_DIR}) -message(STATUS "LIBRARY DIRECTORY: " ${CMAKE_CURRENT_SOURCE_DIR}/../../) +get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) -# For a standalone repo, remove this line and set the path to the repos own -# FetchContent cache directory. Alternatively, don't set FETCHCONTENT_BASE_DIR -# and the repos will be downloaded to the build directory. -set(FETCHCONTENT_BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../third_party") +message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") -FetchContent_Declare( - gpu - # For standalone repo, replace GIT_REPOSITORY with the URL: - # GIT_REPOSITORY https://github.com/AnswerDotAI/gpu.cpp - GIT_REPOSITORY file://${CMAKE_CURRENT_SOURCE_DIR}/../../ - GIT_TAG main - GIT_SHALLOW TRUE -) -FetchContent_MakeAvailable(gpu) +# Ensure the build type is set +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) +endif() -add_executable(gpu_puzzles run.cpp) -target_link_libraries(gpu_puzzles gpu webgpu) -target_include_directories(gpu_puzzles PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../ ) +# Define architecture and build type directories or file names +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(ARCH "x64") +else() + set(ARCH "x86") +endif() +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(BUILD_TYPE "Debug") +else() + set(BUILD_TYPE "Release") +endif() + +if(NOT TARGET gpu) + message(STATUS "GPU_LIB not found") + include("${PROJECT_ROOT}/cmake/webgpu.cmake") + include("${PROJECT_ROOT}/cmake/gpu.cmake") +endif() + +add_executable(${PROJECT_NAME} run.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE gpu) +target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) +target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) + +# Ensure DLL is copied if on Windows +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${DLL_PATH} + $) \ No newline at end of file diff --git a/examples/gpu_puzzles/Makefile b/examples/gpu_puzzles/Makefile index b4bb422..83fa622 100644 --- a/examples/gpu_puzzles/Makefile +++ b/examples/gpu_puzzles/Makefile @@ -1,20 +1,89 @@ -CXX=clang++ -GPUCPP ?= $(PWD)/../.. -LIBDIR ?= $(GPUCPP)/third_party/lib -LIBSPEC ?= . $(GPUCPP)/source -NUM_JOBS?=$(shell nproc) -FLAGS=-stdlib=libc++ -std=c++17 -I$(GPUCPP) -I$(GPUCPP)/third_party/headers -L$(GPUCPP)/third_party/lib run.cpp -ldl -ldawn -TARGET=gpu_puzzles +TARGET = gpu_puzzles -run: ./build/$(TARGET) - $(LIBSPEC) && ./build/$(TARGET) +# Set up variables for cross-platform compatibility +ifeq ($(OS),Windows_NT) + DETECTED_OS := Windows + MKDIR_CMD := if not exist build mkdir build + RMDIR_CMD := rd /s /q + SLASH := \\ + LDLIB_SUFFIX := dll + EXPORT_CMD := set +else + DETECTED_OS := $(shell uname) + MKDIR_CMD := mkdir -p build + RMDIR_CMD := rm -rf + SLASH := / + LDLIB_SUFFIX := so + EXPORT_CMD := export +endif -build/$(TARGET): run.cpp - mkdir -p build && $(CXX) $(FLAGS) -o ./build/$(TARGET) +# Determine the architecture +ifeq ($(DETECTED_OS), Windows) + ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) + ARCH := x64 + else + ARCH := x86 + endif +else + ARCH := $(shell uname -m) + ifeq ($(ARCH), x86_64) + ARCH := x64 + else ifneq (,$(findstring arm, $(ARCH))) + ARCH := arm + endif +endif -watch: - @command -v entr >/dev/null 2>&1 || { echo >&2 "Please install entr with 'brew install entr' or 'sudo apt-get install entr'"; exit 1; } - mkdir -p build && ls | entr -s "rm -f ./build/$(TARGET) && make -j$(NUM_JOBS) ./build/$(TARGET) && $(LIBSPEC) && ./build/$(TARGET)" +# Paths +GPUCPP ?= $(shell pwd) + +default: build_$(TARGET)_debug + +run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) + +build_$(TARGET)_release: +ifeq ($(OS),Windows_NT) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . +endif + +build_$(TARGET)_debug: + @if not exist "build" $(MKDIR_CMD) + cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug + +examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan +ifeq ($(DETECTED_OS), Windows) + cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe +else + cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build +endif + +dawnlib: +ifeq ($(DETECTED_OS), Windows) + @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) +else + @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ + $(MAKE) run_setup; \ + fi +endif + +run_setup: check-python +ifeq ($(OS),Windows_NT) + cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) +else + cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) +endif clean: - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* +ifeq ($(DETECTED_OS), Windows) + $(RMDIR_CMD) build +else + read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* +endif + +check-python: +ifeq ($(OS),Windows_NT) + @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) +else + @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } +endif \ No newline at end of file diff --git a/examples/hello_world/Makefile b/examples/hello_world/Makefile index b5b1eed..96ab52f 100644 --- a/examples/hello_world/Makefile +++ b/examples/hello_world/Makefile @@ -1,31 +1,89 @@ -CXX=clang++ -GPUCPP ?= $(PWD)/../.. -LIBDIR ?= $(GPUCPP)/third_party/lib -LIBSPEC ?= . $(GPUCPP)/source -NUM_JOBS?=$(shell nproc) -FLAGS=-stdlib=libc++ -std=c++17 -I$(GPUCPP) -I$(GPUCPP)/third_party/headers -L$(GPUCPP)/third_party/lib run.cpp -ldl -ldawn -TARGET=hello_world +TARGET = hello_world -run: ./build/$(TARGET) dawnlib - $(LIBSPEC) && ./build/$(TARGET) +# Set up variables for cross-platform compatibility +ifeq ($(OS),Windows_NT) + DETECTED_OS := Windows + MKDIR_CMD := if not exist build mkdir build + RMDIR_CMD := rd /s /q + SLASH := \\ + LDLIB_SUFFIX := dll + EXPORT_CMD := set +else + DETECTED_OS := $(shell uname) + MKDIR_CMD := mkdir -p build + RMDIR_CMD := rm -rf + SLASH := / + LDLIB_SUFFIX := so + EXPORT_CMD := export +endif -dawnlib: $(if $(wildcard $(GPUCPP)/third_party/lib/libdawn.so $(GPUCPP)/third_party/lib/libdawn.dylib),,run_setup) +# Determine the architecture +ifeq ($(DETECTED_OS), Windows) + ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) + ARCH := x64 + else + ARCH := x86 + endif +else + ARCH := $(shell uname -m) + ifeq ($(ARCH), x86_64) + ARCH := x64 + else ifneq (,$(findstring arm, $(ARCH))) + ARCH := arm + endif +endif -run_setup: check-python - cd $(GPUCPP) && python3 setup.py +# Paths +GPUCPP ?= $(shell pwd) + +default: build_$(TARGET)_debug + +run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) + +build_$(TARGET)_release: +ifeq ($(OS),Windows_NT) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . +endif -all: dawnlib check-clang check-linux-vulkan - cd examples/gpu_puzzles && make build/gpu_puzzles - cd examples/hello_world && make build/hello_world - cd examples/matmul && make build/mm - cd examples/physics && make build/physics - cd examples/render && make build/render +build_$(TARGET)_debug: + @if not exist "build" $(MKDIR_CMD) + cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug -build/$(TARGET): run.cpp - mkdir -p build && $(CXX) $(FLAGS) -DNDEBUG -o ./build/$(TARGET) +examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan +ifeq ($(DETECTED_OS), Windows) + cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe +else + cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build +endif + +dawnlib: +ifeq ($(DETECTED_OS), Windows) + @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) +else + @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ + $(MAKE) run_setup; \ + fi +endif + +run_setup: check-python +ifeq ($(OS),Windows_NT) + cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) +else + cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) +endif clean: - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* +ifeq ($(DETECTED_OS), Windows) + $(RMDIR_CMD) build +else + read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* +endif check-python: - @command -v python3 >/dev/null 2>&1 || { echo >&2 "Python needs to be installed and in your path."; exit 1; } +ifeq ($(OS),Windows_NT) + @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) +else + @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } +endif \ No newline at end of file diff --git a/examples/matmul/CMakeLists.txt b/examples/matmul/CMakeLists.txt new file mode 100644 index 0000000..9cb0044 --- /dev/null +++ b/examples/matmul/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required(VERSION 3.11) +project(mm) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) + +message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") + +# Ensure the build type is set +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) +endif() + +# Define architecture and build type directories or file names +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(ARCH "x64") +else() + set(ARCH "x86") +endif() + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(BUILD_TYPE "Debug") +else() + set(BUILD_TYPE "Release") +endif() + +if(NOT TARGET gpu) + message(STATUS "GPU_LIB not found") + include("${PROJECT_ROOT}/cmake/webgpu.cmake") + include("${PROJECT_ROOT}/cmake/gpu.cmake") +endif() + +add_executable(${PROJECT_NAME} run.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE gpu) +target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) +target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) + +# Ensure DLL is copied if on Windows +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${DLL_PATH} + $) \ No newline at end of file diff --git a/examples/matmul/Makefile b/examples/matmul/Makefile index 0528c7e..5c8b6c7 100644 --- a/examples/matmul/Makefile +++ b/examples/matmul/Makefile @@ -1,22 +1,89 @@ -CXX=clang++ -GPUCPP ?= $(PWD)/../.. -LIBDIR ?= $(GPUCPP)/third_party/lib -LIBSPEC ?= . $(GPUCPP)/source -NUM_JOBS?=$(shell nproc) -CODEPATH = find . ../../utils ../../ -maxdepth 1 -type f -FLAGS=-stdlib=libc++ -std=c++17 -I$(GPUCPP) -I$(GPUCPP)/third_party/headers -L$(GPUCPP)/third_party/lib run.cpp -ldl -ldawn -TARGET=mm - -run: ./build/$(TARGET) - $(LIBSPEC) && ./build/$(TARGET) - -# Use clang -v to see the include paths -build/$(TARGET): run.cpp - mkdir -p build && $(CXX) $(FLAGS) -o ./build/$(TARGET) - -watch: - @command -v entr >/dev/null 2>&1 || { echo >&2 "Please install entr with 'brew install entr' or 'sudo apt-get install entr'"; exit 1; } - mkdir -p build && $(CODEPATH) | entr -s "$(LIBSPEC) && rm -f ./build/$(TARGET) && make -j$(NUM_JOBS) ./build/$(TARGET) && ./build/$(TARGET)" +TARGET = mm + +# Set up variables for cross-platform compatibility +ifeq ($(OS),Windows_NT) + DETECTED_OS := Windows + MKDIR_CMD := if not exist build mkdir build + RMDIR_CMD := rd /s /q + SLASH := \\ + LDLIB_SUFFIX := dll + EXPORT_CMD := set +else + DETECTED_OS := $(shell uname) + MKDIR_CMD := mkdir -p build + RMDIR_CMD := rm -rf + SLASH := / + LDLIB_SUFFIX := so + EXPORT_CMD := export +endif + +# Determine the architecture +ifeq ($(DETECTED_OS), Windows) + ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) + ARCH := x64 + else + ARCH := x86 + endif +else + ARCH := $(shell uname -m) + ifeq ($(ARCH), x86_64) + ARCH := x64 + else ifneq (,$(findstring arm, $(ARCH))) + ARCH := arm + endif +endif + +# Paths +GPUCPP ?= $(shell pwd) + +default: build_$(TARGET)_debug + +run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) + +build_$(TARGET)_release: +ifeq ($(OS),Windows_NT) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . +endif + +build_$(TARGET)_debug: + @if not exist "build" $(MKDIR_CMD) + cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug + +examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan +ifeq ($(DETECTED_OS), Windows) + cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe +else + cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build +endif + +dawnlib: +ifeq ($(DETECTED_OS), Windows) + @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) +else + @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ + $(MAKE) run_setup; \ + fi +endif + +run_setup: check-python +ifeq ($(OS),Windows_NT) + cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) +else + cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) +endif clean: - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* +ifeq ($(DETECTED_OS), Windows) + $(RMDIR_CMD) build +else + read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* +endif + +check-python: +ifeq ($(OS),Windows_NT) + @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) +else + @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } +endif \ No newline at end of file diff --git a/examples/physics/CMakeLists.txt b/examples/physics/CMakeLists.txt new file mode 100644 index 0000000..8d6aa3a --- /dev/null +++ b/examples/physics/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required(VERSION 3.11) +project(physics) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) + +message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") + +# Ensure the build type is set +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) +endif() + +# Define architecture and build type directories or file names +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(ARCH "x64") +else() + set(ARCH "x86") +endif() + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(BUILD_TYPE "Debug") +else() + set(BUILD_TYPE "Release") +endif() + +if(NOT TARGET gpu) + message(STATUS "GPU_LIB not found") + include("${PROJECT_ROOT}/cmake/webgpu.cmake") + include("${PROJECT_ROOT}/cmake/gpu.cmake") +endif() + +add_executable(${PROJECT_NAME} run.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE gpu) +target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) +target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) + +# Ensure DLL is copied if on Windows +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${DLL_PATH} + $) \ No newline at end of file diff --git a/examples/physics/Makefile b/examples/physics/Makefile index e3b2465..fde7a22 100644 --- a/examples/physics/Makefile +++ b/examples/physics/Makefile @@ -1,19 +1,89 @@ -CXX=clang++ -GPUCPP ?= $(PWD)/../.. -LIBDIR ?= $(GPUCPP)/third_party/lib -LIBSPEC ?= . $(GPUCPP)/source -NUM_JOBS?=$(shell nproc) -FLAGS=-stdlib=libc++ -std=c++17 -I$(GPUCPP) -I$(GPUCPP)/third_party/headers -L$(GPUCPP)/third_party/lib run.cpp -ldl -ldawn -TARGET=physics +TARGET = physics -run: ./build/$(TARGET) - $(LIBSPEC) && ./build/$(TARGET) +# Set up variables for cross-platform compatibility +ifeq ($(OS),Windows_NT) + DETECTED_OS := Windows + MKDIR_CMD := if not exist build mkdir build + RMDIR_CMD := rd /s /q + SLASH := \\ + LDLIB_SUFFIX := dll + EXPORT_CMD := set +else + DETECTED_OS := $(shell uname) + MKDIR_CMD := mkdir -p build + RMDIR_CMD := rm -rf + SLASH := / + LDLIB_SUFFIX := so + EXPORT_CMD := export +endif -build/$(TARGET): run.cpp - mkdir -p build && $(CXX) $(FLAGS) -o ./build/$(TARGET) +# Determine the architecture +ifeq ($(DETECTED_OS), Windows) + ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) + ARCH := x64 + else + ARCH := x86 + endif +else + ARCH := $(shell uname -m) + ifeq ($(ARCH), x86_64) + ARCH := x64 + else ifneq (,$(findstring arm, $(ARCH))) + ARCH := arm + endif +endif -watch: - mkdir -p build && ls | entr -s "rm -f ./build/$(TARGET) && make -j$(NUM_JOBS) ./build/$(TARGET) && $(LIBSPEC) && ./build/$(TARGET)" +# Paths +GPUCPP ?= $(shell pwd) + +default: build_$(TARGET)_debug + +run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) + +build_$(TARGET)_release: +ifeq ($(OS),Windows_NT) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . +endif + +build_$(TARGET)_debug: + @if not exist "build" $(MKDIR_CMD) + cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug + +examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan +ifeq ($(DETECTED_OS), Windows) + cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe +else + cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build +endif + +dawnlib: +ifeq ($(DETECTED_OS), Windows) + @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) +else + @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ + $(MAKE) run_setup; \ + fi +endif + +run_setup: check-python +ifeq ($(OS),Windows_NT) + cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) +else + cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) +endif clean: - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* +ifeq ($(DETECTED_OS), Windows) + $(RMDIR_CMD) build +else + read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* +endif + +check-python: +ifeq ($(OS),Windows_NT) + @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) +else + @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } +endif \ No newline at end of file diff --git a/examples/render/CMakeLists.txt b/examples/render/CMakeLists.txt new file mode 100644 index 0000000..7ed6b27 --- /dev/null +++ b/examples/render/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required(VERSION 3.11) +project(render) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) + +message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") + +# Ensure the build type is set +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) +endif() + +# Define architecture and build type directories or file names +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(ARCH "x64") +else() + set(ARCH "x86") +endif() + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(BUILD_TYPE "Debug") +else() + set(BUILD_TYPE "Release") +endif() + +if(NOT TARGET gpu) + message(STATUS "GPU_LIB not found") + include("${PROJECT_ROOT}/cmake/webgpu.cmake") + include("${PROJECT_ROOT}/cmake/gpu.cmake") +endif() + +add_executable(${PROJECT_NAME} run.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE gpu) +target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) +target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) + +# Ensure DLL is copied if on Windows +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${DLL_PATH} + $) \ No newline at end of file diff --git a/examples/render/Makefile b/examples/render/Makefile index 4ec8f18..19da402 100644 --- a/examples/render/Makefile +++ b/examples/render/Makefile @@ -1,20 +1,89 @@ -CXX=clang++ -GPUCPP ?= $(PWD)/../.. -LIBDIR ?= $(GPUCPP)/third_party/lib -LIBSPEC ?= . $(GPUCPP)/source -NUM_JOBS?=$(shell nproc) -FLAGS=-stdlib=libc++ -std=c++17 -I$(GPUCPP) -I$(GPUCPP)/third_party/headers -L$(GPUCPP)/third_party/lib run.cpp -ldl -ldawn -TARGET=render +TARGET = render -run: ./build/$(TARGET) - $(LIBSPEC) && ./build/$(TARGET) +# Set up variables for cross-platform compatibility +ifeq ($(OS),Windows_NT) + DETECTED_OS := Windows + MKDIR_CMD := if not exist build mkdir build + RMDIR_CMD := rd /s /q + SLASH := \\ + LDLIB_SUFFIX := dll + EXPORT_CMD := set +else + DETECTED_OS := $(shell uname) + MKDIR_CMD := mkdir -p build + RMDIR_CMD := rm -rf + SLASH := / + LDLIB_SUFFIX := so + EXPORT_CMD := export +endif -build/$(TARGET): run.cpp - mkdir -p build && $(CXX) $(FLAGS) -o ./build/$(TARGET) +# Determine the architecture +ifeq ($(DETECTED_OS), Windows) + ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) + ARCH := x64 + else + ARCH := x86 + endif +else + ARCH := $(shell uname -m) + ifeq ($(ARCH), x86_64) + ARCH := x64 + else ifneq (,$(findstring arm, $(ARCH))) + ARCH := arm + endif +endif -watch: - @command -v entr >/dev/null 2>&1 || { echo >&2 "Please install entr with 'brew install entr' or 'sudo apt-get install entr'"; exit 1; } - mkdir -p build && ls | entr -s "rm -f ./build/$(TARGET) && make -j$(NUM_JOBS) ./build/$(TARGET) && $(LIBSPEC) && ./build/$(TARGET)" +# Paths +GPUCPP ?= $(shell pwd) + +default: build_$(TARGET)_debug + +run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) + +build_$(TARGET)_release: +ifeq ($(OS),Windows_NT) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . +endif + +build_$(TARGET)_debug: + @if not exist "build" $(MKDIR_CMD) + cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug + +examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan +ifeq ($(DETECTED_OS), Windows) + cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe +else + cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build +endif + +dawnlib: +ifeq ($(DETECTED_OS), Windows) + @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) +else + @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ + $(MAKE) run_setup; \ + fi +endif + +run_setup: check-python +ifeq ($(OS),Windows_NT) + cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) +else + cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) +endif clean: - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* +ifeq ($(DETECTED_OS), Windows) + $(RMDIR_CMD) build +else + read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* +endif + +check-python: +ifeq ($(OS),Windows_NT) + @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) +else + @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } +endif \ No newline at end of file diff --git a/examples/shadertui/CMakeLists.txt b/examples/shadertui/CMakeLists.txt new file mode 100644 index 0000000..7dfcea2 --- /dev/null +++ b/examples/shadertui/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required(VERSION 3.11) +project(shadertui) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) + +message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") + +# Ensure the build type is set +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) +endif() + +# Define architecture and build type directories or file names +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(ARCH "x64") +else() + set(ARCH "x86") +endif() + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(BUILD_TYPE "Debug") +else() + set(BUILD_TYPE "Release") +endif() + +if(NOT TARGET gpu) + message(STATUS "GPU_LIB not found") + include("${PROJECT_ROOT}/cmake/webgpu.cmake") + include("${PROJECT_ROOT}/cmake/gpu.cmake") +endif() + +add_executable(${PROJECT_NAME} run.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE gpu) +target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) +target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) + +# Ensure DLL is copied if on Windows +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${DLL_PATH} + $) \ No newline at end of file diff --git a/examples/shadertui/Makefile b/examples/shadertui/Makefile index 746b3dd..e1d0f26 100644 --- a/examples/shadertui/Makefile +++ b/examples/shadertui/Makefile @@ -1,22 +1,89 @@ -CXX=clang++ -GPUCPP ?= $(PWD)/../.. -LIBDIR ?= $(GPUCPP)/third_party/lib -LIBSPEC ?= . $(GPUCPP)/source -NUM_JOBS?=$(shell nproc) -TARGET=shadertui -FLAGS=-stdlib=libc++ -std=c++17 -I$(GPUCPP) -I$(GPUCPP)/third_party/headers -L$(GPUCPP)/third_party/lib run.cpp -ldl -ldawn -CODEPATH = find . ../../utils ../../ -maxdepth 1 -type f - -run: ./build/$(TARGET) - $(LIBSPEC) && ./build/$(TARGET) - -# Use clang -v to see the include paths -build/$(TARGET): run.cpp - mkdir -p build && $(CXX) $(FLAGS) -o ./build/$(TARGET) - -watch: - @command -v entr >/dev/null 2>&1 || { echo >&2 "Please install entr with 'brew install entr' or 'sudo apt-get install entr'"; exit 1; } - mkdir -p build && $(CODEPATH) | entr -s "$(LIBSPEC) && rm -f ./build/$(TARGET) && make -j$(NUM_JOBS) run" +TARGET = shadertui + +# Set up variables for cross-platform compatibility +ifeq ($(OS),Windows_NT) + DETECTED_OS := Windows + MKDIR_CMD := if not exist build mkdir build + RMDIR_CMD := rd /s /q + SLASH := \\ + LDLIB_SUFFIX := dll + EXPORT_CMD := set +else + DETECTED_OS := $(shell uname) + MKDIR_CMD := mkdir -p build + RMDIR_CMD := rm -rf + SLASH := / + LDLIB_SUFFIX := so + EXPORT_CMD := export +endif + +# Determine the architecture +ifeq ($(DETECTED_OS), Windows) + ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) + ARCH := x64 + else + ARCH := x86 + endif +else + ARCH := $(shell uname -m) + ifeq ($(ARCH), x86_64) + ARCH := x64 + else ifneq (,$(findstring arm, $(ARCH))) + ARCH := arm + endif +endif + +# Paths +GPUCPP ?= $(shell pwd) + +default: build_$(TARGET)_debug + +run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) + +build_$(TARGET)_release: +ifeq ($(OS),Windows_NT) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . +endif + +build_$(TARGET)_debug: + @if not exist "build" $(MKDIR_CMD) + cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug + +examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan +ifeq ($(DETECTED_OS), Windows) + cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe +else + cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build +endif + +dawnlib: +ifeq ($(DETECTED_OS), Windows) + @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) +else + @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ + $(MAKE) run_setup; \ + fi +endif + +run_setup: check-python +ifeq ($(OS),Windows_NT) + cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) +else + cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) +endif clean: - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/* +ifeq ($(DETECTED_OS), Windows) + $(RMDIR_CMD) build +else + read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* +endif + +check-python: +ifeq ($(OS),Windows_NT) + @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) +else + @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } +endif \ No newline at end of file diff --git a/setup.py b/setup.py index 4e28348..513ac35 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ def report_progress(block_num, block_size, total_size): nonlocal total_downloaded total_downloaded += block_size print(f"\rDownloaded {total_downloaded // (1024 * 1024)} MB", end="") - + try: ssl._create_default_https_context = ssl._create_stdlib_context urllib.request.urlretrieve(url, output_filename, reporthook=report_progress) @@ -36,54 +36,93 @@ def report_progress(block_num, block_size, total_size): except Exception as e: print(f"\nFailed to download {output_filename}") print(f"Error: {str(e)}") - sys.exit(1) + return False def check_os(os_name): print("\nChecking System") print("===============\n") print(f" Operating System : {os_name}") - supported = {"macOS", "Linux"} + supported = {"macOS", "Linux", "Windows 64-bit", "Windows 32-bit"} if os_name not in supported: print("Unsupported operating system") sys.exit(1) -def download_dawn(os_name): +def download_dawn(os_name, arch, build_type): print("\nDownload Dawn Library") print("=====================\n") + lib_ext = { + "macOS": "dylib", + "Linux": "so", + "Windows 64-bit": "dll", + "Windows 32-bit": "dll", + } + outfile_map = { + "macOS": f"third_party/lib/libdawn_{arch}_{build_type}.dylib", + "Linux": f"third_party/lib/libdawn_{arch}_{build_type}.so", + "Windows 64-bit": f"third_party\\lib\\libdawn_{arch}_{build_type}.dll", + "Windows 32-bit": f"third_party\\lib\\libdawn_{arch}_{build_type}.dll", + } + fallback_map = { "macOS": "third_party/lib/libdawn.dylib", "Linux": "third_party/lib/libdawn.so", + "Windows 64-bit": "third_party\\lib\\libdawn.dll", + "Windows 32-bit": "third_party\\lib\\libdawn.dll", } + url_map = { + "macOS": f"https://github.com/austinvhuang/dawn-artifacts/releases/download/prerelease/libdawn_{arch}_{build_type}.dylib", + "Linux": f"https://github.com/austinvhuang/dawn-artifacts/releases/download/prerelease/libdawn_{arch}_{build_type}.so", + "Windows 64-bit": f"https://github.com/austinvhuang/dawn-artifacts/releases/download/prerelease/libdawn_{arch}_{build_type}.dll", + "Windows 32-bit": f"https://github.com/austinvhuang/dawn-artifacts/releases/download/prerelease/libdawn_{arch}_{build_type}.dll", + } + fallback_url_map = { "macOS": "https://github.com/austinvhuang/dawn-artifacts/releases/download/prerelease/libdawn.dylib", "Linux": "https://github.com/austinvhuang/dawn-artifacts/releases/download/prerelease/libdawn.so", + "Windows 64-bit": "https://github.com/austinvhuang/dawn-artifacts/releases/download/prerelease/libdawn_x64.dll", + "Windows 32-bit": "https://github.com/austinvhuang/dawn-artifacts/releases/download/prerelease/libdawn_x86.dll", } - outfile = outfile_map.get(os_name) + outfile = Path(outfile_map.get(os_name)) + fallback_file = Path(fallback_map.get(os_name)) url = url_map.get(os_name) + fallback_url = fallback_url_map.get(os_name) - if not outfile or not url: - print(f"No download information for {os_name}") - sys.exit(1) + cwd = Path.cwd() + print(f" Output File : {outfile}") + print(f" Current Directory: {cwd}") + print(f" File Exists : {cwd / outfile}") + if outfile.exists(): + print(f" File {outfile} already exists, skipping.") + sys.exit(0) print(f" URL : {url}") print(f" Download File : {outfile}\n") print(" Downloading ...\n") - if Path(outfile).exists(): - print(f" File {outfile} already exists, skipping.") - sys.exit(0) + outfile.parent.mkdir(parents=True, exist_ok=True) + if download_file(url, outfile): + return - Path(outfile).parent.mkdir(parents=True, exist_ok=True) - download_file(url, outfile) + print("\nPrimary file not found, attempting fallback download...\n") + print(f" Fallback URL : {fallback_url}") + print(f" Fallback File : {fallback_file}\n") + + if download_file(fallback_url, fallback_file): + outfile.unlink(missing_ok=True) # Remove partial download if needed + fallback_file.rename(outfile) + return + + print("Failed to download both primary and fallback files.") + sys.exit(1) def setup_env(os_name): print("\nEnvironment Setup") print("=================\n") - current_dir = os.getcwd() - lib_dir = os.path.join(current_dir, "third_party", "lib") + current_dir = Path.cwd() + lib_dir = current_dir / "third_party" / "lib" if os_name == "macOS": print(" Before running the program, run the following command or add it to your shell profile:") @@ -97,13 +136,22 @@ def setup_env(os_name): with open("source", "w") as f: f.write(f"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:{lib_dir}\n") + if os_name.startswith("Windows"): + print(" Before running the program, add the following path to your PATH environment variable:") + print(f" {lib_dir}") + + with open("source.bat", "w") as f: + f.write(f"set PATH=%PATH%;{lib_dir}\n") def main(): os_name = get_os_name() + arch = "x64" if platform.machine().endswith('64') else "x86" + build_type = "Debug" if 'debug' in sys.argv else "Release" + check_os(os_name) - download_dawn(os_name) + download_dawn(os_name, arch, build_type) setup_env(os_name) print() if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/third_party/fetchcontent/.gitkeep b/third_party/fetchcontent/.gitkeep deleted file mode 100644 index e69de29..0000000 From 3b7c8e673143afdf00ad9faedfa9e59ec4a1238b Mon Sep 17 00:00:00 2001 From: MichealReed Date: Wed, 17 Jul 2024 21:11:55 -0500 Subject: [PATCH 02/24] windows support --- examples/hello_world/CMakeLists.txt | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/hello_world/CMakeLists.txt diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt new file mode 100644 index 0000000..23ead3a --- /dev/null +++ b/examples/hello_world/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required(VERSION 3.11) +project(hello_world) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) + +message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") + +# Ensure the build type is set +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) +endif() + +# Define architecture and build type directories or file names +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(ARCH "x64") +else() + set(ARCH "x86") +endif() + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(BUILD_TYPE "Debug") +else() + set(BUILD_TYPE "Release") +endif() + +if(NOT TARGET gpu) + message(STATUS "GPU_LIB not found") + include("${PROJECT_ROOT}/cmake/webgpu.cmake") + include("${PROJECT_ROOT}/cmake/gpu.cmake") +endif() + +add_executable(${PROJECT_NAME} run.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE gpu) +target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) +target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) + +# Ensure DLL is copied if on Windows +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${DLL_PATH} + $) \ No newline at end of file From cb67989970679272fb7db3332f5a8deaf627e3a1 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Wed, 17 Jul 2024 21:15:20 -0500 Subject: [PATCH 03/24] fix python for linux builds --- Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index bc7f56a..2688fed 100644 --- a/Makefile +++ b/Makefile @@ -67,8 +67,11 @@ else endif run_setup: check-python - python3 setup.py | python setup.py - +ifeq ($(DETECTED_OS), Windows) + python3 setup.py +else + python3 >/dev/null 2>&1 && python3 setup.py +endif all: dawnlib check-clang check-linux-vulkan cd examples$(SLASH)gpu_puzzles && make build$(SLASH)gpu_puzzles From 36f031dd037e9d7f9a464474d28a6adecaf3ca9b Mon Sep 17 00:00:00 2001 From: MichealReed Date: Wed, 17 Jul 2024 21:41:15 -0500 Subject: [PATCH 04/24] no need to copy object other than windows --- examples/gpu_puzzles/CMakeLists.txt | 4 +++- examples/hello_world/CMakeLists.txt | 4 +++- examples/matmul/CMakeLists.txt | 4 +++- examples/physics/CMakeLists.txt | 4 +++- examples/render/CMakeLists.txt | 4 +++- examples/shadertui/CMakeLists.txt | 4 +++- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/examples/gpu_puzzles/CMakeLists.txt b/examples/gpu_puzzles/CMakeLists.txt index cc78836..399655a 100644 --- a/examples/gpu_puzzles/CMakeLists.txt +++ b/examples/gpu_puzzles/CMakeLists.txt @@ -39,8 +39,10 @@ target_link_libraries(${PROJECT_NAME} PRIVATE gpu) target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) +if(WIN32) # Ensure DLL is copied if on Windows add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_PATH} - $) \ No newline at end of file + $) +endif() \ No newline at end of file diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt index 23ead3a..f631c29 100644 --- a/examples/hello_world/CMakeLists.txt +++ b/examples/hello_world/CMakeLists.txt @@ -39,8 +39,10 @@ target_link_libraries(${PROJECT_NAME} PRIVATE gpu) target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) +if(WIN32) # Ensure DLL is copied if on Windows add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_PATH} - $) \ No newline at end of file + $) +endif() \ No newline at end of file diff --git a/examples/matmul/CMakeLists.txt b/examples/matmul/CMakeLists.txt index 9cb0044..3b01334 100644 --- a/examples/matmul/CMakeLists.txt +++ b/examples/matmul/CMakeLists.txt @@ -39,8 +39,10 @@ target_link_libraries(${PROJECT_NAME} PRIVATE gpu) target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) +if(WIN32) # Ensure DLL is copied if on Windows add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_PATH} - $) \ No newline at end of file + $) +endif() \ No newline at end of file diff --git a/examples/physics/CMakeLists.txt b/examples/physics/CMakeLists.txt index 8d6aa3a..ea22bd7 100644 --- a/examples/physics/CMakeLists.txt +++ b/examples/physics/CMakeLists.txt @@ -39,8 +39,10 @@ target_link_libraries(${PROJECT_NAME} PRIVATE gpu) target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) +if(WIN32) # Ensure DLL is copied if on Windows add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_PATH} - $) \ No newline at end of file + $) +endif() \ No newline at end of file diff --git a/examples/render/CMakeLists.txt b/examples/render/CMakeLists.txt index 7ed6b27..3f1897d 100644 --- a/examples/render/CMakeLists.txt +++ b/examples/render/CMakeLists.txt @@ -39,8 +39,10 @@ target_link_libraries(${PROJECT_NAME} PRIVATE gpu) target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) +if(WIN32) # Ensure DLL is copied if on Windows add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_PATH} - $) \ No newline at end of file + $) +endif() \ No newline at end of file diff --git a/examples/shadertui/CMakeLists.txt b/examples/shadertui/CMakeLists.txt index 7dfcea2..afb33ef 100644 --- a/examples/shadertui/CMakeLists.txt +++ b/examples/shadertui/CMakeLists.txt @@ -39,8 +39,10 @@ target_link_libraries(${PROJECT_NAME} PRIVATE gpu) target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) +if(WIN32) # Ensure DLL is copied if on Windows add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${DLL_PATH} - $) \ No newline at end of file + $) +endif() \ No newline at end of file From e02b2412983f7d35e83d17270545a168257d5991 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Wed, 17 Jul 2024 21:46:10 -0500 Subject: [PATCH 05/24] fix debug makefiles --- examples/gpu_puzzles/Makefile | 9 ++++++--- examples/hello_world/Makefile | 9 ++++++--- examples/matmul/Makefile | 9 ++++++--- examples/physics/Makefile | 9 ++++++--- examples/render/Makefile | 9 ++++++--- examples/shadertui/Makefile | 9 ++++++--- 6 files changed, 36 insertions(+), 18 deletions(-) diff --git a/examples/gpu_puzzles/Makefile b/examples/gpu_puzzles/Makefile index 83fa622..dff6e7f 100644 --- a/examples/gpu_puzzles/Makefile +++ b/examples/gpu_puzzles/Makefile @@ -41,15 +41,18 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) +ifeq ($(OS),Windows) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: - @if not exist "build" $(MKDIR_CMD) - cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug +ifeq ($(OS),Windows) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . +endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan ifeq ($(DETECTED_OS), Windows) diff --git a/examples/hello_world/Makefile b/examples/hello_world/Makefile index 96ab52f..525e703 100644 --- a/examples/hello_world/Makefile +++ b/examples/hello_world/Makefile @@ -41,15 +41,18 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) +ifeq ($(OS),Windows) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: - @if not exist "build" $(MKDIR_CMD) - cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug +ifeq ($(OS),Windows) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . +endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan ifeq ($(DETECTED_OS), Windows) diff --git a/examples/matmul/Makefile b/examples/matmul/Makefile index 5c8b6c7..01fd048 100644 --- a/examples/matmul/Makefile +++ b/examples/matmul/Makefile @@ -41,15 +41,18 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) +ifeq ($(OS),Windows) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: - @if not exist "build" $(MKDIR_CMD) - cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug +ifeq ($(OS),Windows) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . +endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan ifeq ($(DETECTED_OS), Windows) diff --git a/examples/physics/Makefile b/examples/physics/Makefile index fde7a22..5fd2d1c 100644 --- a/examples/physics/Makefile +++ b/examples/physics/Makefile @@ -41,15 +41,18 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) +ifeq ($(OS),Windows) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: - @if not exist "build" $(MKDIR_CMD) - cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug +ifeq ($(OS),Windows) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . +endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan ifeq ($(DETECTED_OS), Windows) diff --git a/examples/render/Makefile b/examples/render/Makefile index 19da402..d884079 100644 --- a/examples/render/Makefile +++ b/examples/render/Makefile @@ -41,15 +41,18 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) +ifeq ($(OS),Windows) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: - @if not exist "build" $(MKDIR_CMD) - cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug +ifeq ($(OS),Windows) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . +endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan ifeq ($(DETECTED_OS), Windows) diff --git a/examples/shadertui/Makefile b/examples/shadertui/Makefile index e1d0f26..2e5d882 100644 --- a/examples/shadertui/Makefile +++ b/examples/shadertui/Makefile @@ -41,15 +41,18 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) +ifeq ($(OS),Windows) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: - @if not exist "build" $(MKDIR_CMD) - cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Debug +ifeq ($(OS),Windows) + @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release +else + @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . +endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan ifeq ($(DETECTED_OS), Windows) From 4d55073cc6f670bc52af086a4c917de410e0c3ca Mon Sep 17 00:00:00 2001 From: MichealReed Date: Wed, 17 Jul 2024 21:50:49 -0500 Subject: [PATCH 06/24] Windows_NT not Windows --- examples/gpu_puzzles/Makefile | 12 ++++++------ examples/hello_world/Makefile | 12 ++++++------ examples/matmul/Makefile | 12 ++++++------ examples/physics/Makefile | 12 ++++++------ examples/render/Makefile | 12 ++++++------ examples/shadertui/Makefile | 12 ++++++------ 6 files changed, 36 insertions(+), 36 deletions(-) diff --git a/examples/gpu_puzzles/Makefile b/examples/gpu_puzzles/Makefile index dff6e7f..a94414b 100644 --- a/examples/gpu_puzzles/Makefile +++ b/examples/gpu_puzzles/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -41,28 +41,28 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* diff --git a/examples/hello_world/Makefile b/examples/hello_world/Makefile index 525e703..7f8c44a 100644 --- a/examples/hello_world/Makefile +++ b/examples/hello_world/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -41,28 +41,28 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* diff --git a/examples/matmul/Makefile b/examples/matmul/Makefile index 01fd048..8dee12a 100644 --- a/examples/matmul/Makefile +++ b/examples/matmul/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -41,28 +41,28 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* diff --git a/examples/physics/Makefile b/examples/physics/Makefile index 5fd2d1c..0aa4475 100644 --- a/examples/physics/Makefile +++ b/examples/physics/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -41,28 +41,28 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* diff --git a/examples/render/Makefile b/examples/render/Makefile index d884079..8c6f411 100644 --- a/examples/render/Makefile +++ b/examples/render/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -41,28 +41,28 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* diff --git a/examples/shadertui/Makefile b/examples/shadertui/Makefile index 2e5d882..761b2c0 100644 --- a/examples/shadertui/Makefile +++ b/examples/shadertui/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -41,28 +41,28 @@ default: build_$(TARGET)_debug run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif build_$(TARGET)_debug: -ifeq ($(OS),Windows) +ifeq ($(OS),Windows_NT) @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(DETECTED_OS), Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* From 1730a65aa17eadad83c9470b59f57b50cea147a0 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Wed, 17 Jul 2024 23:28:45 -0500 Subject: [PATCH 07/24] abstract to example.cmake --- cmake/example.cmake | 56 +++++++++++++++++++++++++++++ examples/gpu_puzzles/CMakeLists.txt | 48 ++++++------------------- examples/hello_world/CMakeLists.txt | 48 ++++++------------------- examples/hello_world/Makefile | 4 +-- examples/matmul/CMakeLists.txt | 48 ++++++------------------- examples/physics/CMakeLists.txt | 48 ++++++------------------- examples/physics/Makefile | 2 +- examples/render/CMakeLists.txt | 48 ++++++------------------- examples/shadertui/CMakeLists.txt | 48 ++++++------------------- examples/shadertui/Makefile | 2 +- 10 files changed, 126 insertions(+), 226 deletions(-) create mode 100644 cmake/example.cmake diff --git a/cmake/example.cmake b/cmake/example.cmake new file mode 100644 index 0000000..7552300 --- /dev/null +++ b/cmake/example.cmake @@ -0,0 +1,56 @@ +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) + +# Construct potential paths +set(FILEPATH_CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}") +set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/${FILENAME}") + +# Check if the file exists in the current directory +if(EXISTS ${FILEPATH_CURRENT_DIR}) + set(TARGET_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +elseif(EXISTS ${FILEPATH_PROJECT_ROOT}) + set(TARGET_FILE_PATH ${PROJECT_ROOT}) +else() + message(FATAL_ERROR "File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../") +endif() + +# Ensure the build type is set +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) +endif() + +# Define architecture and build type directories or file names +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(ARCH "x64") +else() + set(ARCH "x86") +endif() + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(BUILD_TYPE "Debug") +else() + set(BUILD_TYPE "Release") +endif() + +if(NOT TARGET gpu) + message(STATUS "GPU_LIB not found") + include("${TARGET_FILE_PATH}/cmake/webgpu.cmake") + include("${TARGET_FILE_PATH}/cmake/gpu.cmake") +endif() + +add_executable(${PROJECT_NAME} run.cpp) +target_link_libraries(${PROJECT_NAME} PRIVATE gpu) +target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) +target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) + +if(WIN32) +# Ensure DLL is copied if on Windows +add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${DLL_PATH} + $) +endif() \ No newline at end of file diff --git a/examples/gpu_puzzles/CMakeLists.txt b/examples/gpu_puzzles/CMakeLists.txt index 399655a..6d4f8e1 100644 --- a/examples/gpu_puzzles/CMakeLists.txt +++ b/examples/gpu_puzzles/CMakeLists.txt @@ -1,48 +1,22 @@ cmake_minimum_required(VERSION 3.11) project(gpu_puzzles) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(FILENAME "gpu.h") get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) -message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") +# Construct potential paths +set(FILEPATH_CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}") +set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/${FILENAME}") -# Ensure the build type is set -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) -endif() - -# Define architecture and build type directories or file names -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(ARCH "x64") -else() - set(ARCH "x86") -endif() - -if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(BUILD_TYPE "Debug") +# Check if the file exists in the current directory +if(EXISTS ${FILEPATH_CURRENT_DIR}) + set(TARGET_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +elseif(EXISTS ${FILEPATH_PROJECT_ROOT}) + set(TARGET_FILE_PATH ${PROJECT_ROOT}) else() - set(BUILD_TYPE "Release") -endif() - -if(NOT TARGET gpu) - message(STATUS "GPU_LIB not found") - include("${PROJECT_ROOT}/cmake/webgpu.cmake") - include("${PROJECT_ROOT}/cmake/gpu.cmake") + message(FATAL_ERROR "File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../") endif() -add_executable(${PROJECT_NAME} run.cpp) -target_link_libraries(${PROJECT_NAME} PRIVATE gpu) -target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) -target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) - -if(WIN32) -# Ensure DLL is copied if on Windows -add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${DLL_PATH} - $) -endif() \ No newline at end of file +include("${TARGET_FILE_PATH}/cmake/example.cmake") \ No newline at end of file diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt index f631c29..058ac65 100644 --- a/examples/hello_world/CMakeLists.txt +++ b/examples/hello_world/CMakeLists.txt @@ -1,48 +1,22 @@ cmake_minimum_required(VERSION 3.11) project(hello_world) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(FILENAME "gpu.h") get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) -message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") +# Construct potential paths +set(FILEPATH_CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}") +set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/${FILENAME}") -# Ensure the build type is set -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) -endif() - -# Define architecture and build type directories or file names -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(ARCH "x64") -else() - set(ARCH "x86") -endif() - -if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(BUILD_TYPE "Debug") +# Check if the file exists in the current directory +if(EXISTS ${FILEPATH_CURRENT_DIR}) + set(TARGET_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +elseif(EXISTS ${FILEPATH_PROJECT_ROOT}) + set(TARGET_FILE_PATH ${PROJECT_ROOT}) else() - set(BUILD_TYPE "Release") -endif() - -if(NOT TARGET gpu) - message(STATUS "GPU_LIB not found") - include("${PROJECT_ROOT}/cmake/webgpu.cmake") - include("${PROJECT_ROOT}/cmake/gpu.cmake") + message(FATAL_ERROR "File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../") endif() -add_executable(${PROJECT_NAME} run.cpp) -target_link_libraries(${PROJECT_NAME} PRIVATE gpu) -target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) -target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) - -if(WIN32) -# Ensure DLL is copied if on Windows -add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${DLL_PATH} - $) -endif() \ No newline at end of file +include("${TARGET_FILE_PATH}/cmake/example.cmake") \ No newline at end of file diff --git a/examples/hello_world/Makefile b/examples/hello_world/Makefile index 7f8c44a..07bd0dd 100644 --- a/examples/hello_world/Makefile +++ b/examples/hello_world/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(DETECTED_OS), Windows) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -36,7 +36,7 @@ endif # Paths GPUCPP ?= $(shell pwd) -default: build_$(TARGET)_debug +default: build_$(TARGET)_release run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) diff --git a/examples/matmul/CMakeLists.txt b/examples/matmul/CMakeLists.txt index 3b01334..9b0d9ea 100644 --- a/examples/matmul/CMakeLists.txt +++ b/examples/matmul/CMakeLists.txt @@ -1,48 +1,22 @@ cmake_minimum_required(VERSION 3.11) project(mm) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(FILENAME "gpu.h") get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) -message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") +# Construct potential paths +set(FILEPATH_CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}") +set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/${FILENAME}") -# Ensure the build type is set -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) -endif() - -# Define architecture and build type directories or file names -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(ARCH "x64") -else() - set(ARCH "x86") -endif() - -if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(BUILD_TYPE "Debug") +# Check if the file exists in the current directory +if(EXISTS ${FILEPATH_CURRENT_DIR}) + set(TARGET_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +elseif(EXISTS ${FILEPATH_PROJECT_ROOT}) + set(TARGET_FILE_PATH ${PROJECT_ROOT}) else() - set(BUILD_TYPE "Release") -endif() - -if(NOT TARGET gpu) - message(STATUS "GPU_LIB not found") - include("${PROJECT_ROOT}/cmake/webgpu.cmake") - include("${PROJECT_ROOT}/cmake/gpu.cmake") + message(FATAL_ERROR "File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../") endif() -add_executable(${PROJECT_NAME} run.cpp) -target_link_libraries(${PROJECT_NAME} PRIVATE gpu) -target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) -target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) - -if(WIN32) -# Ensure DLL is copied if on Windows -add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${DLL_PATH} - $) -endif() \ No newline at end of file +include("${TARGET_FILE_PATH}/cmake/example.cmake") \ No newline at end of file diff --git a/examples/physics/CMakeLists.txt b/examples/physics/CMakeLists.txt index ea22bd7..a118a81 100644 --- a/examples/physics/CMakeLists.txt +++ b/examples/physics/CMakeLists.txt @@ -1,48 +1,22 @@ cmake_minimum_required(VERSION 3.11) project(physics) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(FILENAME "gpu.h") get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) -message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") +# Construct potential paths +set(FILEPATH_CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}") +set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/${FILENAME}") -# Ensure the build type is set -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) -endif() - -# Define architecture and build type directories or file names -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(ARCH "x64") -else() - set(ARCH "x86") -endif() - -if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(BUILD_TYPE "Debug") +# Check if the file exists in the current directory +if(EXISTS ${FILEPATH_CURRENT_DIR}) + set(TARGET_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +elseif(EXISTS ${FILEPATH_PROJECT_ROOT}) + set(TARGET_FILE_PATH ${PROJECT_ROOT}) else() - set(BUILD_TYPE "Release") -endif() - -if(NOT TARGET gpu) - message(STATUS "GPU_LIB not found") - include("${PROJECT_ROOT}/cmake/webgpu.cmake") - include("${PROJECT_ROOT}/cmake/gpu.cmake") + message(FATAL_ERROR "File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../") endif() -add_executable(${PROJECT_NAME} run.cpp) -target_link_libraries(${PROJECT_NAME} PRIVATE gpu) -target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) -target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) - -if(WIN32) -# Ensure DLL is copied if on Windows -add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${DLL_PATH} - $) -endif() \ No newline at end of file +include("${TARGET_FILE_PATH}/cmake/example.cmake") \ No newline at end of file diff --git a/examples/physics/Makefile b/examples/physics/Makefile index 0aa4475..2583da3 100644 --- a/examples/physics/Makefile +++ b/examples/physics/Makefile @@ -36,7 +36,7 @@ endif # Paths GPUCPP ?= $(shell pwd) -default: build_$(TARGET)_debug +default: build_$(TARGET)_release run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) diff --git a/examples/render/CMakeLists.txt b/examples/render/CMakeLists.txt index 3f1897d..314b441 100644 --- a/examples/render/CMakeLists.txt +++ b/examples/render/CMakeLists.txt @@ -1,48 +1,22 @@ cmake_minimum_required(VERSION 3.11) project(render) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(FILENAME "gpu.h") get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) -message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") +# Construct potential paths +set(FILEPATH_CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}") +set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/${FILENAME}") -# Ensure the build type is set -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) -endif() - -# Define architecture and build type directories or file names -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(ARCH "x64") -else() - set(ARCH "x86") -endif() - -if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(BUILD_TYPE "Debug") +# Check if the file exists in the current directory +if(EXISTS ${FILEPATH_CURRENT_DIR}) + set(TARGET_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +elseif(EXISTS ${FILEPATH_PROJECT_ROOT}) + set(TARGET_FILE_PATH ${PROJECT_ROOT}) else() - set(BUILD_TYPE "Release") -endif() - -if(NOT TARGET gpu) - message(STATUS "GPU_LIB not found") - include("${PROJECT_ROOT}/cmake/webgpu.cmake") - include("${PROJECT_ROOT}/cmake/gpu.cmake") + message(FATAL_ERROR "File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../") endif() -add_executable(${PROJECT_NAME} run.cpp) -target_link_libraries(${PROJECT_NAME} PRIVATE gpu) -target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) -target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) - -if(WIN32) -# Ensure DLL is copied if on Windows -add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${DLL_PATH} - $) -endif() \ No newline at end of file +include("${TARGET_FILE_PATH}/cmake/example.cmake") \ No newline at end of file diff --git a/examples/shadertui/CMakeLists.txt b/examples/shadertui/CMakeLists.txt index afb33ef..aafa4d1 100644 --- a/examples/shadertui/CMakeLists.txt +++ b/examples/shadertui/CMakeLists.txt @@ -1,48 +1,22 @@ cmake_minimum_required(VERSION 3.11) project(shadertui) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(FILENAME "gpu.h") get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) -message(STATUS "PROJECT_ROOT: ${PROJECT_ROOT}") +# Construct potential paths +set(FILEPATH_CURRENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}") +set(FILEPATH_PROJECT_ROOT "${PROJECT_ROOT}/${FILENAME}") -# Ensure the build type is set -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build: Debug or Release" FORCE) -endif() - -# Define architecture and build type directories or file names -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(ARCH "x64") -else() - set(ARCH "x86") -endif() - -if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(BUILD_TYPE "Debug") +# Check if the file exists in the current directory +if(EXISTS ${FILEPATH_CURRENT_DIR}) + set(TARGET_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +elseif(EXISTS ${FILEPATH_PROJECT_ROOT}) + set(TARGET_FILE_PATH ${PROJECT_ROOT}) else() - set(BUILD_TYPE "Release") -endif() - -if(NOT TARGET gpu) - message(STATUS "GPU_LIB not found") - include("${PROJECT_ROOT}/cmake/webgpu.cmake") - include("${PROJECT_ROOT}/cmake/gpu.cmake") + message(FATAL_ERROR "File ${FILENAME} not found in either ${CMAKE_CURRENT_SOURCE_DIR} or ${CMAKE_CURRENT_SOURCE_DIR}/../../") endif() -add_executable(${PROJECT_NAME} run.cpp) -target_link_libraries(${PROJECT_NAME} PRIVATE gpu) -target_link_libraries(${PROJECT_NAME} PRIVATE wgpu) -target_link_libraries(${PROJECT_NAME} PRIVATE webgpu) - -if(WIN32) -# Ensure DLL is copied if on Windows -add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different - ${DLL_PATH} - $) -endif() \ No newline at end of file +include("${TARGET_FILE_PATH}/cmake/example.cmake") \ No newline at end of file diff --git a/examples/shadertui/Makefile b/examples/shadertui/Makefile index 761b2c0..0ca0908 100644 --- a/examples/shadertui/Makefile +++ b/examples/shadertui/Makefile @@ -36,7 +36,7 @@ endif # Paths GPUCPP ?= $(shell pwd) -default: build_$(TARGET)_debug +default: build_$(TARGET)_release run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) From 7d1c255c6d6931d13345d80a2063ae561eb9f8e9 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Wed, 17 Jul 2024 23:44:39 -0500 Subject: [PATCH 08/24] fix OS detection --- Makefile | 12 ++++++------ cmake/webgpu.cmake | 1 - examples/gpu_puzzles/Makefile | 8 ++++---- examples/hello_world/Makefile | 8 ++++---- examples/matmul/Makefile | 8 ++++---- examples/physics/Makefile | 9 +++++---- examples/render/Makefile | 8 ++++---- examples/shadertui/Makefile | 8 ++++---- 8 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 2688fed..5a0a42b 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows) +ifeq ($(OS),Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -50,7 +50,7 @@ LIBSPEC ?= . $(GPUCPP)$(SLASH)source default: examples_hello_world_build_hello_world examples_hello_world_build_hello_world: check-clang dawnlib examples/hello_world/run.cpp check-linux-vulkan -ifeq ($(DETECTED_OS), Windows) +ifeq ($(OS),Windows_NT) cd examples$(SLASH)hello_world && $(MAKE) build_hello_world_$(LOWER_BUILD_TYPE) else $(LIBSPEC) && cd examples$(SLASH)hello_world && $(MAKE) build_hello_world_$(LOWER_BUILD_TYPE) @@ -58,7 +58,7 @@ endif # We use the custom "shell" based condition to check files cross-platform dawnlib: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(OS),Windows_NT) @if not exist "$(LIBDIR)$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(LIBDIR)$(SLASH)libdawn.dll" $(MAKE) run_setup else @if [ ! -f "$(LIBDIR)$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(LIBDIR)$(SLASH)libdawn.so" ] && [ ! -f "$(LIBDIR)$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -67,7 +67,7 @@ else endif run_setup: check-python -ifeq ($(DETECTED_OS), Windows) +ifeq ($(OS),Windows_NT) python3 setup.py else python3 >/dev/null 2>&1 && python3 setup.py @@ -112,7 +112,7 @@ clean-dawnlib: $(RMDIR_CMD) $(LIBDIR)$(SLASH)libdawn*.* clean: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(OS),Windows_NT) @if exist build $(RMDIR_CMD) build /s /q @if exist examples$(SLASH)gpu_puzzles$(SLASH)build $(RMDIR_CMD) examples$(SLASH)gpu_puzzles$(SLASH)build /s /q @if exist examples$(SLASH)hello_world$(SLASH)build $(RMDIR_CMD) examples$(SLASH)hello_world$(SLASH)build /s /q @@ -130,7 +130,7 @@ else endif clean-all: -ifeq ($(DETECTED_OS), Windows) +ifeq ($(OS),Windows_NT) @if exist build $(RMDIR_CMD) build /s /q $(RMDIR_CMD) third_party$(SLASH)fetchcontent /s /q $(RMDIR_CMD) third_party$(SLASH)gpu-build /s /q diff --git a/cmake/webgpu.cmake b/cmake/webgpu.cmake index 283ee72..5a66cfc 100644 --- a/cmake/webgpu.cmake +++ b/cmake/webgpu.cmake @@ -19,7 +19,6 @@ endif() include(FetchContent) - set(FETCHCONTENT_BASE_DIR "${TARGET_FILE_PATH}/third_party/fetchcontent") set(WEBGPU_DIST_LOCAL_PATH "${TARGET_FILE_PATH}/third_party/local/WebGPU-distribution") diff --git a/examples/gpu_puzzles/Makefile b/examples/gpu_puzzles/Makefile index a94414b..09f3125 100644 --- a/examples/gpu_puzzles/Makefile +++ b/examples/gpu_puzzles/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -55,14 +55,14 @@ else endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* diff --git a/examples/hello_world/Makefile b/examples/hello_world/Makefile index 07bd0dd..6ba7148 100644 --- a/examples/hello_world/Makefile +++ b/examples/hello_world/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows) +ifeq ($(OS),Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -55,14 +55,14 @@ else endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* diff --git a/examples/matmul/Makefile b/examples/matmul/Makefile index 8dee12a..0b64ae9 100644 --- a/examples/matmul/Makefile +++ b/examples/matmul/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -55,14 +55,14 @@ else endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* diff --git a/examples/physics/Makefile b/examples/physics/Makefile index 2583da3..cb727fc 100644 --- a/examples/physics/Makefile +++ b/examples/physics/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -55,14 +55,14 @@ else endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,8 +78,9 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS), Windows_NT) $(RMDIR_CMD) build + $(MKDIR_CMD) else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* endif diff --git a/examples/render/Makefile b/examples/render/Makefile index 8c6f411..f127a78 100644 --- a/examples/render/Makefile +++ b/examples/render/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -55,14 +55,14 @@ else endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* diff --git a/examples/shadertui/Makefile b/examples/shadertui/Makefile index 0ca0908..011ef21 100644 --- a/examples/shadertui/Makefile +++ b/examples/shadertui/Makefile @@ -18,7 +18,7 @@ else endif # Determine the architecture -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ARCH := x64 else @@ -55,14 +55,14 @@ else endif examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe else cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build endif dawnlib: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) else @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ @@ -78,7 +78,7 @@ else endif clean: -ifeq ($(DETECTED_OS), Windows_NT) +ifeq ($(OS),Windows_NT) $(RMDIR_CMD) build else read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* From 3e095490eca7dd80a324736ef43af5b0a685b7de Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 00:54:37 -0500 Subject: [PATCH 09/24] Increase version and fix min max usage for Windows --- CMakeLists.txt | 2 +- cmake/example.cmake | 4 -- examples/gpu_puzzles/CMakeLists.txt | 2 +- examples/hello_world/CMakeLists.txt | 2 +- examples/matmul/CMakeLists.txt | 2 +- examples/physics/CMakeLists.txt | 2 +- examples/render/CMakeLists.txt | 6 ++- examples/render/Makefile | 4 +- examples/render/run.cpp | 34 +++++++++------ examples/shadertui/CMakeLists.txt | 2 +- examples/shadertui/run.cpp | 46 +++++++++++++-------- examples/webgpu_from_scratch/CMakeLists.txt | 2 +- 12 files changed, 65 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 21d6414..36fa4d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11) +cmake_minimum_required(VERSION 3.28) project(gpu) include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/webgpu.cmake") diff --git a/cmake/example.cmake b/cmake/example.cmake index 7552300..efa5711 100644 --- a/cmake/example.cmake +++ b/cmake/example.cmake @@ -1,7 +1,3 @@ -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) diff --git a/examples/gpu_puzzles/CMakeLists.txt b/examples/gpu_puzzles/CMakeLists.txt index 6d4f8e1..1ca57d2 100644 --- a/examples/gpu_puzzles/CMakeLists.txt +++ b/examples/gpu_puzzles/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11) +cmake_minimum_required(VERSION 3.28) project(gpu_puzzles) set(FILENAME "gpu.h") diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt index 058ac65..9feebc9 100644 --- a/examples/hello_world/CMakeLists.txt +++ b/examples/hello_world/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11) +cmake_minimum_required(VERSION 3.28) project(hello_world) set(FILENAME "gpu.h") diff --git a/examples/matmul/CMakeLists.txt b/examples/matmul/CMakeLists.txt index 9b0d9ea..ed66768 100644 --- a/examples/matmul/CMakeLists.txt +++ b/examples/matmul/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11) +cmake_minimum_required(VERSION 3.28) project(mm) set(FILENAME "gpu.h") diff --git a/examples/physics/CMakeLists.txt b/examples/physics/CMakeLists.txt index a118a81..69e9925 100644 --- a/examples/physics/CMakeLists.txt +++ b/examples/physics/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11) +cmake_minimum_required(VERSION 3.28) project(physics) set(FILENAME "gpu.h") diff --git a/examples/render/CMakeLists.txt b/examples/render/CMakeLists.txt index 314b441..e0ad395 100644 --- a/examples/render/CMakeLists.txt +++ b/examples/render/CMakeLists.txt @@ -1,6 +1,10 @@ -cmake_minimum_required(VERSION 3.11) +cmake_minimum_required(VERSION 3.28) project(render) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + set(FILENAME "gpu.h") get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) diff --git a/examples/render/Makefile b/examples/render/Makefile index f127a78..6d62d6c 100644 --- a/examples/render/Makefile +++ b/examples/render/Makefile @@ -36,13 +36,13 @@ endif # Paths GPUCPP ?= $(shell pwd) -default: build_$(TARGET)_debug +default: build_$(TARGET)_release run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) build_$(TARGET)_release: ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release + @if not exist "build" $(MKDIR_CMD) && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=clang++ .. && cmake --build . --config Release else @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . endif diff --git a/examples/render/run.cpp b/examples/render/run.cpp index 12d86e7..ffce7ad 100644 --- a/examples/render/run.cpp +++ b/examples/render/run.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "gpu.h" #include "utils/array_utils.h" @@ -80,21 +81,24 @@ fn main(@builtin(global_invocation_id) GlobalInvocationID: vec3) { } )"; -std::uint32_t getCurrentTimeInMilliseconds() { +std::uint32_t getCurrentTimeInMilliseconds() +{ auto now = std::chrono::system_clock::now(); auto duration = std::chrono::duration_cast( now.time_since_epoch()); return static_cast(duration.count()); } -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ constexpr size_t NROWS = 32; constexpr size_t NCOLS = 64; std::array screen; - struct Params { + struct Params + { float focalLength; uint32_t screenWidth; uint32_t screenHeight; @@ -122,7 +126,8 @@ int main(int argc, char **argv) { Kernel renderKernel = createKernel(ctx, shader, Bindings{devScreen}, cdiv({NCOLS, NROWS, 1}, shader.workgroupSize), params); - while (true) { + while (true) + { std::promise promise; std::future future = promise.get_future(); dispatchKernel(ctx, renderKernel, promise); @@ -142,37 +147,42 @@ int main(int argc, char **argv) { float min = 0.0; float max = params.sphereRadius * 3; - for (size_t i = 0; i < screen.size(); ++i) { + for (size_t i = 0; i < screen.size(); ++i) + { screen[i] = (screen[i] - min) / (max - min); } std::array raster; - for (size_t i = 0; i < screen.size(); ++i) { + for (size_t i = 0; i < screen.size(); ++i) + { size_t index = std::min(sizeof(intensity) - 2, - std::max(0ul, static_cast(screen[i] * - (sizeof(intensity) - 2)))); + std::max(size_t{0}, static_cast(screen[i] * (sizeof(intensity) - 2)))); raster[i] = intensity[index]; } char buffer[(NROWS + 2) * (NCOLS + 2)]; char *offset = buffer; sprintf(offset, "+"); - for (size_t col = 0; col < NCOLS; ++col) { + for (size_t col = 0; col < NCOLS; ++col) + { sprintf(offset + col + 1, "-"); } sprintf(buffer + NCOLS + 1, "+\n"); offset += NCOLS + 3; - for (size_t row = 0; row < NROWS; ++row) { + for (size_t row = 0; row < NROWS; ++row) + { sprintf(offset, "|"); - for (size_t col = 0; col < NCOLS; ++col) { + for (size_t col = 0; col < NCOLS; ++col) + { sprintf(offset + col + 1, "%c", raster[row * NCOLS + col]); } sprintf(offset + NCOLS + 1, "|\n"); offset += NCOLS + 3; } sprintf(offset, "+"); - for (size_t col = 0; col < NCOLS; ++col) { + for (size_t col = 0; col < NCOLS; ++col) + { sprintf(offset + col + 1, "-"); } sprintf(offset + NCOLS + 1, "+\n"); diff --git a/examples/shadertui/CMakeLists.txt b/examples/shadertui/CMakeLists.txt index aafa4d1..083b718 100644 --- a/examples/shadertui/CMakeLists.txt +++ b/examples/shadertui/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11) +cmake_minimum_required(VERSION 3.28) project(shadertui) set(FILENAME "gpu.h") diff --git a/examples/shadertui/run.cpp b/examples/shadertui/run.cpp index 5a3910f..f2a5825 100644 --- a/examples/shadertui/run.cpp +++ b/examples/shadertui/run.cpp @@ -14,19 +14,22 @@ using namespace gpu; template void rasterize(const std::array &values, - std::array &raster) { + std::array &raster) +{ // Can experiment with the rasterization characters here but fewer characters // looks better by imposing temporal coherence whereas more characters can // start to look like noise. // static const char intensity[] = " `.-':_,^=;><+!ngrc*/z?sLTv)J7(|Fi{C}fI31tlu[neoZ5Yxjya]2ESwqkP6h9d4VpOGbUAKXHm8RD#$Bg0MNWQ%&@"; static const char intensity[] = " .`'^-+=*x17X$8#%@"; - for (size_t i = 0; i < rows; ++i) { - for (size_t j = 0; j < cols; ++j) { + for (size_t i = 0; i < rows; ++i) + { + for (size_t j = 0; j < cols; ++j) + { // values ranges b/w 0 and 1 - size_t index = - std::min(sizeof(intensity) - 2, - std::max(0ul, static_cast(values[i * cols + j] * - (sizeof(intensity) - 2)))); + size_t index = std::min(sizeof(intensity) - 2, + std::max(size_t{0}, + static_cast(values[i * cols + j] * + (sizeof(intensity) - 2)))); raster[i * (cols + 1) + j] = intensity[index]; } raster[i * (cols + 1) + cols] = '\n'; @@ -34,31 +37,36 @@ void rasterize(const std::array &values, } float getCurrentTimeInMilliseconds( - std::chrono::time_point &zeroTime) { + std::chrono::time_point &zeroTime) +{ std::chrono::duration duration = std::chrono::high_resolution_clock::now() - zeroTime; return duration.count(); } -void loadShaderCode(const std::string &filename, std::string &codeString) { +void loadShaderCode(const std::string &filename, std::string &codeString) +{ codeString = ""; FILE *file = fopen(filename.c_str(), "r"); - while (!file) { + while (!file) + { fclose(file); std::this_thread::sleep_for(std::chrono::milliseconds(100)); file = fopen(filename.c_str(), "r"); } char buffer[4096]; - while (fgets(buffer, sizeof(buffer), file)) { + while (fgets(buffer, sizeof(buffer), file)) + { codeString += buffer; } fclose(file); } -int main() { +int main() +{ Context ctx = createContext(); - + static constexpr size_t kRows = 64; static constexpr size_t kCols = 96; // static constexpr size_t kRows = 96; @@ -76,7 +84,8 @@ int main() { std::future future = promise.get_future(); std::string codeString; - struct Params { + struct Params + { float time; uint32_t screenWidth; uint32_t screenHeight; @@ -99,10 +108,13 @@ int main() { auto start = std::chrono::high_resolution_clock::now(); std::chrono::duration elapsed; size_t ticks = 0; - while (true) { - if (elapsed.count() - static_cast(ticks) > 1.0) { + while (true) + { + if (elapsed.count() - static_cast(ticks) > 1.0) + { loadShaderCode("shader.wgsl", codeString); - if (codeString != shader.data) { + if (codeString != shader.data) + { shader = createShader(codeString.c_str(), Shape{16, 16, 1}); renderKernel = createKernel(ctx, shader, Bindings{screen}, diff --git a/examples/webgpu_from_scratch/CMakeLists.txt b/examples/webgpu_from_scratch/CMakeLists.txt index 8804628..2c9e57a 100644 --- a/examples/webgpu_from_scratch/CMakeLists.txt +++ b/examples/webgpu_from_scratch/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.11) +cmake_minimum_required(VERSION 3.28) project(wgpu_tutorial) include(FetchContent) From 8b660c9b8299704d0ef605d07640c38a1403b34c Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 00:57:22 -0500 Subject: [PATCH 10/24] algorithm not needed --- examples/render/run.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/render/run.cpp b/examples/render/run.cpp index ffce7ad..ab719c2 100644 --- a/examples/render/run.cpp +++ b/examples/render/run.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include "gpu.h" #include "utils/array_utils.h" From 3936258578c6a1dea2db6dd90364b02ffef1ce69 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 01:32:01 -0500 Subject: [PATCH 11/24] fix standard for all examples --- cmake/example.cmake | 4 ++++ examples/render/CMakeLists.txt | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmake/example.cmake b/cmake/example.cmake index efa5711..ab4cad2 100644 --- a/cmake/example.cmake +++ b/cmake/example.cmake @@ -1,3 +1,7 @@ +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) get_filename_component(PROJECT_ROOT ${PROJECT_ROOT} DIRECTORY) diff --git a/examples/render/CMakeLists.txt b/examples/render/CMakeLists.txt index e0ad395..f3599cf 100644 --- a/examples/render/CMakeLists.txt +++ b/examples/render/CMakeLists.txt @@ -1,10 +1,6 @@ cmake_minimum_required(VERSION 3.28) project(render) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 23) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - set(FILENAME "gpu.h") get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) From 422094c90a934c65c0ab11ea2b74030cdbe57684 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 11:07:26 -0500 Subject: [PATCH 12/24] CMake from root folder builds library --- CMakeLists.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 36fa4d5..1e7ae22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,5 +35,6 @@ include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/gpu.cmake") message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}") message(STATUS "Include directories for wgpu: ${CMAKE_CURRENT_SOURCE_DIR}/third_party/headers") -# Add subdirectories for examples -add_subdirectory(examples/hello_world) +add_library(gpudoth SHARED gpu.h) +set_target_properties(gpudoth PROPERTIES LINKER_LANGUAGE CXX) +install(TARGETS gpudoth) \ No newline at end of file From 1f8eee914b4b130a65912ac320b7205d4cc8d38b Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 12:37:06 -0500 Subject: [PATCH 13/24] Don't forget to link the libraries to the object dude --- CMakeLists.txt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e7ae22..9b3fa28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,9 @@ include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/gpu.cmake") message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}") message(STATUS "Include directories for wgpu: ${CMAKE_CURRENT_SOURCE_DIR}/third_party/headers") -add_library(gpudoth SHARED gpu.h) -set_target_properties(gpudoth PROPERTIES LINKER_LANGUAGE CXX) -install(TARGETS gpudoth) \ No newline at end of file +add_library(gpud SHARED gpu.h) +target_link_libraries(gpud PRIVATE wgpu) +target_link_libraries(gpud PRIVATE webgpu) +target_link_libraries(gpud PRIVATE gpu) +set_target_properties(gpud PROPERTIES LINKER_LANGUAGE CXX) +install(TARGETS gpud) \ No newline at end of file From 00257a14fb5fc96d7c557450ea860da0f85c837e Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 20:27:28 -0500 Subject: [PATCH 14/24] fix conflicts, set properties before link --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b3fa28..0977110 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,8 +36,8 @@ message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}") message(STATUS "Include directories for wgpu: ${CMAKE_CURRENT_SOURCE_DIR}/third_party/headers") add_library(gpud SHARED gpu.h) +set_target_properties(gpud PROPERTIES LINKER_LANGUAGE CXX) target_link_libraries(gpud PRIVATE wgpu) target_link_libraries(gpud PRIVATE webgpu) target_link_libraries(gpud PRIVATE gpu) -set_target_properties(gpud PROPERTIES LINKER_LANGUAGE CXX) install(TARGETS gpud) \ No newline at end of file From 427ea77fd14da2e233dae94781b2ff5df56561ce Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 21:27:43 -0500 Subject: [PATCH 15/24] Unify the example makefiles! --- Makefile | 53 ++++++---------- examples/{gpu_puzzles => }/Makefile | 74 +++++++++++++++++------ examples/hello_world/Makefile | 92 ---------------------------- examples/matmul/Makefile | 92 ---------------------------- examples/physics/Makefile | 93 ----------------------------- examples/render/Makefile | 92 ---------------------------- 6 files changed, 72 insertions(+), 424 deletions(-) rename examples/{gpu_puzzles => }/Makefile (51%) delete mode 100644 examples/hello_world/Makefile delete mode 100644 examples/matmul/Makefile delete mode 100644 examples/physics/Makefile delete mode 100644 examples/render/Makefile diff --git a/Makefile b/Makefile index b4f0611..7dee713 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,11 @@ NUM_JOBS=$(shell nproc 2>/dev/null || echo 1) CXX=clang++ -.PHONY: default examples_hello_world_build_hello_world tests libgpu debug build check-entr check-clang clean-build clean clean-dawnlib all watch-tests docs +.PHONY: default examples_hello_world_build_hello_world tests libgpu debug build check-entr check-clang clean-build clean clean-dawnlib clean-all all watch-tests docs +.PHONY: $(addprefix run_, $(TARGETS)) + +# List of targets (folders in your examples directory) +TARGETS := gpu_puzzles hello_world matmul physics render shadertui # Set up variables for cross-platform compatibility ifeq ($(OS),Windows_NT) @@ -41,29 +45,30 @@ endif # Determine the build type BUILD_TYPE ?= Release LOWER_BUILD_TYPE ?= $(shell python3 -c "print('$(BUILD_TYPE)'.lower())") -pch: - mkdir -p build && $(CXX) -std=c++17 -I$(GPUCPP) -I$(GPUCPP)/third_party/headers -x c++-header gpu.h -o build/gpu.h.pch - -# TODO(avh): change extension based on platform -lib: - mkdir -p build && $(CXX) -std=c++17 -I$(GPUCPP) -I$(GPUCPP)/third_party/headers -L$(LIBDIR) -ldawn -ldl -shared -fPIC gpu.cpp -o build/libgpucpp.dylib # Paths GPUCPP ?= $(shell pwd) LIBDIR ?= $(GPUCPP)$(SLASH)third_party$(SLASH)lib LIBSPEC ?= . $(GPUCPP)$(SLASH)source -default: examples_hello_world_build_hello_world +default: run_hello_world -examples_hello_world_build_hello_world: check-clang dawnlib examples/hello_world/run.cpp check-linux-vulkan +run_%: + @cd examples && $(MAKE) $(@) + +# Build rules for specific targets +define BUILD_RULES +build_$(1): ifeq ($(OS),Windows_NT) - cd examples$(SLASH)hello_world && $(MAKE) build_hello_world_$(LOWER_BUILD_TYPE) + cd examples && $(MAKE) $(1)_$(LOWER_BUILD_TYPE) else - $(LIBSPEC) && cd examples$(SLASH)hello_world && $(MAKE) build_hello_world_$(LOWER_BUILD_TYPE) + cd examples&& $(MAKE) $(1)_$(LOWER_BUILD_TYPE) endif +endef +$(foreach target, $(TARGETS), $(eval $(call BUILD_RULES,$(target)))) # We use the custom "shell" based condition to check files cross-platform -dawnlib: +dawnlib: ifeq ($(OS),Windows_NT) @if not exist "$(LIBDIR)$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(LIBDIR)$(SLASH)libdawn.dll" $(MAKE) run_setup else @@ -89,28 +94,6 @@ all: dawnlib check-clang check-linux-vulkan docs: Doxyfile doxygen Doxyfile -################################################################################ -# cmake targets (optional - precompiled binaries is preferred) -################################################################################ - -CMAKE_CMD = $(MKDIR_CMD) && cd build && cmake .. -# Add --trace to see the cmake commands -FLAGS = -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_CXX_COMPILER=$(CXX) -DABSL_INTERNAL_AT_LEAST_CXX20=OFF -FASTBUILD_FLAGS = $(FLAGS) -DFASTBUILD:BOOL=ON -DEBUG_FLAGS = $(FLAGS) -DDEBUG:BOOL=ON -RELEASE_FLAGS = $(FLAGS) -DFASTBUILD:BOOL=OFF -TARGET_LIB=gpu - -libgpu-cmake: check-clang check-cmake - $(CMAKE_CMD) $(RELEASE_FLAGS) && make -j$(NUM_JOBS) gpu - -debug-cmake: check-clang check-cmake - $(CMAKE_CMD) $(DEBUG_FLAGS) && make -j$(NUM_JOBS) $(TARGET_ALL) - -all-cmake: check-clang check-cmake - $(CMAKE_CMD) $(RELEASE_FLAGS) && make -j$(NUM_JOBS) $(TARGET_ALL) - -################################################################################ # Cleanup ################################################################################ @@ -151,8 +134,6 @@ else read -r -p "This will delete the contents of build/* and third_party/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* third_party/fetchcontent* third_party/gpu-build third_party/gpu-subbuild third_party/gpu-src third_party/lib/libdawn* third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).* endif - -################################################################################ # Checks ################################################################################ diff --git a/examples/gpu_puzzles/Makefile b/examples/Makefile similarity index 51% rename from examples/gpu_puzzles/Makefile rename to examples/Makefile index 09f3125..dfdffe8 100644 --- a/examples/gpu_puzzles/Makefile +++ b/examples/Makefile @@ -1,4 +1,5 @@ -TARGET = gpu_puzzles +# List of targets (folders in your examples directory) +TARGETS := gpu_puzzles hello_world matmul physics render shadertui # Set up variables for cross-platform compatibility ifeq ($(OS),Windows_NT) @@ -36,30 +37,72 @@ endif # Paths GPUCPP ?= $(shell pwd) -default: build_$(TARGET)_debug +.PHONY: default all_release all_debug dawnlib run_setup check-python +.PHONY: $(addsuffix _release, $(TARGETS)) +.PHONY: $(addsuffix _debug, $(TARGETS)) +.PHONY: clean-all $(addprefix clean_, $(TARGETS)) -run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) +default: all_debug -build_$(TARGET)_release: +all_release: $(addsuffix _release, $(TARGETS)) + +all_debug: $(addsuffix _debug, $(TARGETS)) + +# Define Build Rules +define BUILD_RULES +$(1)_release: +ifeq ($(OS),Windows_NT) + @if not exist "$(1)$(SLASH)build" $(MKDIR_CMD) "$(1)$(SLASH)build" + cd $(1) && \ + cmake -B build -DCMAKE_BUILD_TYPE=Release && \ + cmake --build build --config Release +else + $(MKDIR_CMD) $(1)$(SLASH)build + cd $(1) && \ + cmake -B build -DCMAKE_BUILD_TYPE=Release && \ + cmake --build build --config Release +endif + +$(1)_debug: ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release + @if not exist "$(1)$(SLASH)build" $(MKDIR_CMD) "$(1)$(SLASH)build" + cd $(1) && \ + cmake -B build -DCMAKE_BUILD_TYPE=Debug && \ + cmake --build build --config Debug else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . + $(MKDIR_CMD) $(1)$(SLASH)build + cd $(1) && \ + cmake -B build -DCMAKE_BUILD_TYPE=Debug && \ + cmake --build build --config Debug endif +endef +# Apply Build Rules to each target in $(TARGETS) +$(foreach target,$(TARGETS),$(eval $(call BUILD_RULES,$(target)))) -build_$(TARGET)_debug: +# Define Run Rules +define RUN_RULES +run_$(1): ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release + @for /f "delims=" %%i in ('dir /s /b "$(1).exe"') do set EXE=%%i && %%i else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . + @sh -c '$$(find $(1)/build -type f -executable -name "$(1)" | head -n 1 )' endif +endef +# Apply Run Rules to each target in $(TARGETS) +$(foreach target,$(TARGETS),$(eval $(call RUN_RULES,$(target)))) -examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan +# Clean rules for cleaning specific targets +define CLEAN_RULES +clean_$(1): ifeq ($(OS),Windows_NT) - cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe + if exist $(1)$(SLASH)build ( $(RMDIR_CMD) $(1)$(SLASH)build ) else - cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build + find $(1) -name build -type d | xargs rm -rf endif +endef +$(foreach target,$(TARGETS),$(eval $(call CLEAN_RULES,$(target)))) + +clean-all: $(addprefix clean_, $(TARGETS)) dawnlib: ifeq ($(OS),Windows_NT) @@ -77,13 +120,6 @@ else cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) endif -clean: -ifeq ($(OS),Windows_NT) - $(RMDIR_CMD) build -else - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* -endif - check-python: ifeq ($(OS),Windows_NT) @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) diff --git a/examples/hello_world/Makefile b/examples/hello_world/Makefile deleted file mode 100644 index 6ba7148..0000000 --- a/examples/hello_world/Makefile +++ /dev/null @@ -1,92 +0,0 @@ -TARGET = hello_world - -# Set up variables for cross-platform compatibility -ifeq ($(OS),Windows_NT) - DETECTED_OS := Windows - MKDIR_CMD := if not exist build mkdir build - RMDIR_CMD := rd /s /q - SLASH := \\ - LDLIB_SUFFIX := dll - EXPORT_CMD := set -else - DETECTED_OS := $(shell uname) - MKDIR_CMD := mkdir -p build - RMDIR_CMD := rm -rf - SLASH := / - LDLIB_SUFFIX := so - EXPORT_CMD := export -endif - -# Determine the architecture -ifeq ($(OS),Windows_NT) - ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) - ARCH := x64 - else - ARCH := x86 - endif -else - ARCH := $(shell uname -m) - ifeq ($(ARCH), x86_64) - ARCH := x64 - else ifneq (,$(findstring arm, $(ARCH))) - ARCH := arm - endif -endif - -# Paths -GPUCPP ?= $(shell pwd) - -default: build_$(TARGET)_release - -run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) - -build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release -else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . -endif - -build_$(TARGET)_debug: -ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release -else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . -endif - -examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(OS),Windows_NT) - cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe -else - cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build -endif - -dawnlib: -ifeq ($(OS),Windows_NT) - @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) -else - @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ - $(MAKE) run_setup; \ - fi -endif - -run_setup: check-python -ifeq ($(OS),Windows_NT) - cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) -else - cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) -endif - -clean: -ifeq ($(OS),Windows_NT) - $(RMDIR_CMD) build -else - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* -endif - -check-python: -ifeq ($(OS),Windows_NT) - @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) -else - @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } -endif \ No newline at end of file diff --git a/examples/matmul/Makefile b/examples/matmul/Makefile deleted file mode 100644 index 0b64ae9..0000000 --- a/examples/matmul/Makefile +++ /dev/null @@ -1,92 +0,0 @@ -TARGET = mm - -# Set up variables for cross-platform compatibility -ifeq ($(OS),Windows_NT) - DETECTED_OS := Windows - MKDIR_CMD := if not exist build mkdir build - RMDIR_CMD := rd /s /q - SLASH := \\ - LDLIB_SUFFIX := dll - EXPORT_CMD := set -else - DETECTED_OS := $(shell uname) - MKDIR_CMD := mkdir -p build - RMDIR_CMD := rm -rf - SLASH := / - LDLIB_SUFFIX := so - EXPORT_CMD := export -endif - -# Determine the architecture -ifeq ($(OS),Windows_NT) - ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) - ARCH := x64 - else - ARCH := x86 - endif -else - ARCH := $(shell uname -m) - ifeq ($(ARCH), x86_64) - ARCH := x64 - else ifneq (,$(findstring arm, $(ARCH))) - ARCH := arm - endif -endif - -# Paths -GPUCPP ?= $(shell pwd) - -default: build_$(TARGET)_debug - -run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) - -build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release -else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . -endif - -build_$(TARGET)_debug: -ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release -else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . -endif - -examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(OS),Windows_NT) - cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe -else - cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build -endif - -dawnlib: -ifeq ($(OS),Windows_NT) - @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) -else - @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ - $(MAKE) run_setup; \ - fi -endif - -run_setup: check-python -ifeq ($(OS),Windows_NT) - cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) -else - cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) -endif - -clean: -ifeq ($(OS),Windows_NT) - $(RMDIR_CMD) build -else - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* -endif - -check-python: -ifeq ($(OS),Windows_NT) - @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) -else - @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } -endif \ No newline at end of file diff --git a/examples/physics/Makefile b/examples/physics/Makefile deleted file mode 100644 index cb727fc..0000000 --- a/examples/physics/Makefile +++ /dev/null @@ -1,93 +0,0 @@ -TARGET = physics - -# Set up variables for cross-platform compatibility -ifeq ($(OS),Windows_NT) - DETECTED_OS := Windows - MKDIR_CMD := if not exist build mkdir build - RMDIR_CMD := rd /s /q - SLASH := \\ - LDLIB_SUFFIX := dll - EXPORT_CMD := set -else - DETECTED_OS := $(shell uname) - MKDIR_CMD := mkdir -p build - RMDIR_CMD := rm -rf - SLASH := / - LDLIB_SUFFIX := so - EXPORT_CMD := export -endif - -# Determine the architecture -ifeq ($(OS),Windows_NT) - ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) - ARCH := x64 - else - ARCH := x86 - endif -else - ARCH := $(shell uname -m) - ifeq ($(ARCH), x86_64) - ARCH := x64 - else ifneq (,$(findstring arm, $(ARCH))) - ARCH := arm - endif -endif - -# Paths -GPUCPP ?= $(shell pwd) - -default: build_$(TARGET)_release - -run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) - -build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release -else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . -endif - -build_$(TARGET)_debug: -ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release -else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . -endif - -examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(OS),Windows_NT) - cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe -else - cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build -endif - -dawnlib: -ifeq ($(OS),Windows_NT) - @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) -else - @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ - $(MAKE) run_setup; \ - fi -endif - -run_setup: check-python -ifeq ($(OS),Windows_NT) - cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) -else - cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) -endif - -clean: -ifeq ($(OS), Windows_NT) - $(RMDIR_CMD) build - $(MKDIR_CMD) -else - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* -endif - -check-python: -ifeq ($(OS),Windows_NT) - @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) -else - @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } -endif \ No newline at end of file diff --git a/examples/render/Makefile b/examples/render/Makefile deleted file mode 100644 index 6d62d6c..0000000 --- a/examples/render/Makefile +++ /dev/null @@ -1,92 +0,0 @@ -TARGET = render - -# Set up variables for cross-platform compatibility -ifeq ($(OS),Windows_NT) - DETECTED_OS := Windows - MKDIR_CMD := if not exist build mkdir build - RMDIR_CMD := rd /s /q - SLASH := \\ - LDLIB_SUFFIX := dll - EXPORT_CMD := set -else - DETECTED_OS := $(shell uname) - MKDIR_CMD := mkdir -p build - RMDIR_CMD := rm -rf - SLASH := / - LDLIB_SUFFIX := so - EXPORT_CMD := export -endif - -# Determine the architecture -ifeq ($(OS),Windows_NT) - ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) - ARCH := x64 - else - ARCH := x86 - endif -else - ARCH := $(shell uname -m) - ifeq ($(ARCH), x86_64) - ARCH := x64 - else ifneq (,$(findstring arm, $(ARCH))) - ARCH := arm - endif -endif - -# Paths -GPUCPP ?= $(shell pwd) - -default: build_$(TARGET)_release - -run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) - -build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=clang++ .. && cmake --build . --config Release -else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . -endif - -build_$(TARGET)_debug: -ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release -else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . -endif - -examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(OS),Windows_NT) - cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe -else - cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build -endif - -dawnlib: -ifeq ($(OS),Windows_NT) - @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) -else - @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ - $(MAKE) run_setup; \ - fi -endif - -run_setup: check-python -ifeq ($(OS),Windows_NT) - cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) -else - cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) -endif - -clean: -ifeq ($(OS),Windows_NT) - $(RMDIR_CMD) build -else - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* -endif - -check-python: -ifeq ($(OS),Windows_NT) - @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) -else - @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } -endif \ No newline at end of file From ec934b3aef99eec1ef23ffd3af603704e618c2cc Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 21:33:07 -0500 Subject: [PATCH 16/24] rule to cleanup specific examples --- Makefile | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Makefile b/Makefile index 7dee713..14f69cb 100644 --- a/Makefile +++ b/Makefile @@ -97,6 +97,17 @@ docs: Doxyfile # Cleanup ################################################################################ +# Clean rules for cleaning specific targets +define CLEAN_RULES +clean_$(1): +ifeq ($(OS),Windows_NT) + @if exist examples$(SLASH)$(1)$(SLASH)build ( $(RMDIR_CMD) /s examples$(SLASH)$(1)$(SLASH)build ) +else + find examples$(SLASH)$(1) -name build -type d | xargs rm -rf +endif +endef +$(foreach target,$(TARGETS),$(eval $(call CLEAN_RULES,$(target)))) + clean-dawnlib: $(RMDIR_CMD) $(LIBDIR)$(SLASH)libdawn*.* From e50a1e218db1f718e1b6d69c1b1083bcd011f281 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 22:38:30 -0500 Subject: [PATCH 17/24] Fix run rules --- Makefile | 9 +++++++-- examples/Makefile | 22 +++++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 14f69cb..2a8b4e8 100644 --- a/Makefile +++ b/Makefile @@ -53,8 +53,13 @@ LIBSPEC ?= . $(GPUCPP)$(SLASH)source default: run_hello_world -run_%: - @cd examples && $(MAKE) $(@) +# Define Run Rules +define RUN_RULES +run_$(1): + @cd examples && $(MAKE) run_$(1) +endef +# Apply Run Rules to each target in $(TARGETS) +$(foreach target,$(TARGETS),$(eval $(call RUN_RULES,$(target)))) # Build rules for specific targets define BUILD_RULES diff --git a/examples/Makefile b/examples/Makefile index dfdffe8..d9888bf 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -79,13 +79,29 @@ endef # Apply Build Rules to each target in $(TARGETS) $(foreach target,$(TARGETS),$(eval $(call BUILD_RULES,$(target)))) -# Define Run Rules +# Define Run Rules with Build if Executable Not Found define RUN_RULES run_$(1): ifeq ($(OS),Windows_NT) - @for /f "delims=" %%i in ('dir /s /b "$(1).exe"') do set EXE=%%i && %%i + @if exist $(1)$(SLASH)build$(SLASH)Release$(SLASH)$(1).exe ( \ + echo "Running $(1)" && \ + $(1)$(SLASH)build$(SLASH)Release$(SLASH)$(1).exe \ + ) else ( \ + echo "Executable not found; building $(1) first" && \ + $(MAKE) $(1)_release && \ + $(1)$(SLASH)build$(SLASH)Release$(SLASH)$(1).exe \ + ) else - @sh -c '$$(find $(1)/build -type f -executable -name "$(1)" | head -n 1 )' + @if [ -x "$(shell find $(1)/build -type f -executable -name $(1) | head -n 1)" ]; then \ + exe_path=$(shell find $(1)/build -type f -executable -name $(1) | head -n 1) && \ + echo "Running $(1)" && \ + $$exe_path; \ + else \ + echo "Executable not found; building $(1) first" && \ + cd $(1) && $(MAKE) $(1)_release && \ + exe_path=$(shell find $(1)/build -type f -executable -name $(1) | head -n 1) && \ + $$exe_path; \ + fi endif endef # Apply Run Rules to each target in $(TARGETS) From e39d8597ec1c46b433c028fd43b4f254ca0b9821 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 22:40:34 -0500 Subject: [PATCH 18/24] Fix linux run --- examples/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Makefile b/examples/Makefile index d9888bf..8308ac7 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -98,7 +98,7 @@ else $$exe_path; \ else \ echo "Executable not found; building $(1) first" && \ - cd $(1) && $(MAKE) $(1)_release && \ + $(MAKE) $(1)_release && \ exe_path=$(shell find $(1)/build -type f -executable -name $(1) | head -n 1) && \ $$exe_path; \ fi From 3803f392b17eea764ff5ad011d292d177e8d04e1 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 23:00:48 -0500 Subject: [PATCH 19/24] Use CPP 17 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0977110..6d4a743 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(gpu) include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/webgpu.cmake") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) option(USE_LOCAL_LIBS "Use local libraries instead of fetching from the internet" OFF) From 679faedfa0f1f5b1aae75c514b49e7d5a5077763 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 23:19:05 -0500 Subject: [PATCH 20/24] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f77cddf..e5cc1cc 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,10 @@ To build a gpu.cpp project, you will need to have installed on your system: make to build the project. - `make` to build the project. - Only on Linux systems - Vulkan drivers. If Vulkan is not installed, you can run `sudo apt install libvulkan1 mesa-vulkan-drivers vulkan-tools` to install them. +- On Windows, Vulkan, make, cmake, and a Visual Studio build environment with C++ tooling are required. +- WSL (Windows Subsystem for Linux) is also supported on Windows. You can install the required packages in WSL with `sudo apt install build-essential python3 libx11-xcb-dev libx11-dev libxi-dev libxcursor-dev libxinerama-dev libxrandr-dev libx11-6 libc++-dev libvulkan1 mesa-vulkan-drivers vulkan-tools clang`. -The only library dependency of gpu.cpp is a WebGPU implementation. Currently we support the Dawn native backend, but we plan to support other targets and WebGPU implementations (web browsers or other native implementations such as wgpu). Currently we support MacOS, Linux, and Windows (via WSL). +The only library dependency of gpu.cpp is a WebGPU implementation. Currently we support the Dawn native backend, but we plan to support other targets and WebGPU implementations (web browsers or other native implementations such as wgpu). Currently we support MacOS, Linux, and Windows, including WSL. Optionally, Dawn can be built from scratch with gpu.cpp using the cmake build scripts provided - see the -cmake targets in the Makefile. However, this is recommended for advanced users only. Building Dawn dependencies with cmake takes much longer than using the precompiled Dawn shared library. @@ -45,7 +47,7 @@ After cloning the repo, from the top-level gpu.cpp, you should be able to build make ``` -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. +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_ARCH_BUILD_TYPE.dylib` on MacOS, `libdawn_ARCH_BUILD_TYPE.so` on Linux/WSL or `libdawn_ARCH_BUILD_TYPE.dll` on Windows. This download only occurs once. The build process itself should take a few seconds. If the build and executions is successful, you should see the output of the GELU computation: From d4452ee54e26b2e7247c011df565a06abf67e26b Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 23:21:56 -0500 Subject: [PATCH 21/24] Update README.md --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index e5cc1cc..f22c1d2 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,19 @@ Hello gpu.cpp! Computed 10000 values of GELU(x) ``` +If you want to run other examples, you can run: + +``` +make run_example_name + +# For example: + +make run_matmul + +or + +make run_gpu_puzzles +``` If you need to clean up the build artifacts, you can run: @@ -78,6 +91,12 @@ If you need to clean up the build artifacts, you can run: make clean ``` +or for a specific example: + +``` +make clean_example_name +``` + ## Hello World Tutorial: A GELU Kernel As a real-world example for how to use gpu.cpp, let's start with a practical-but-simple example of a GPU kernel from neural networks. From 0d630b7f1bf617efeec50f9c05b4413450ea203a Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 23:27:33 -0500 Subject: [PATCH 22/24] Fix matmul --- examples/matmul/CMakeLists.txt | 2 +- examples/matmul/run.cpp | 4 ++ examples/shadertui/Makefile | 91 ---------------------------------- 3 files changed, 5 insertions(+), 92 deletions(-) delete mode 100644 examples/shadertui/Makefile diff --git a/examples/matmul/CMakeLists.txt b/examples/matmul/CMakeLists.txt index ed66768..5b4f3f9 100644 --- a/examples/matmul/CMakeLists.txt +++ b/examples/matmul/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.28) -project(mm) +project(matmul) set(FILENAME "gpu.h") diff --git a/examples/matmul/run.cpp b/examples/matmul/run.cpp index 4e61968..2e0cbc6 100644 --- a/examples/matmul/run.cpp +++ b/examples/matmul/run.cpp @@ -7,6 +7,10 @@ #include "gpu.h" // createContext, createTensor, createKernel, dispatchKernel, // wait, resetCommandBuffer, toCPU +#ifndef M_PI + #define M_PI 3.14159265358979323846 +#endif + #include "llmc/reference_impls.h" // for CPU reference implementation #include "utils/array_utils.h" // show, isclose, randn, randint #include "utils/logging.h" // LOG diff --git a/examples/shadertui/Makefile b/examples/shadertui/Makefile deleted file mode 100644 index be4e4a5..0000000 --- a/examples/shadertui/Makefile +++ /dev/null @@ -1,91 +0,0 @@ -TARGET = shadertui -# Set up variables for cross-platform compatibility -ifeq ($(OS),Windows_NT) - DETECTED_OS := Windows - MKDIR_CMD := if not exist build mkdir build - RMDIR_CMD := rd /s /q - SLASH := \\ - LDLIB_SUFFIX := dll - EXPORT_CMD := set -else - DETECTED_OS := $(shell uname) - MKDIR_CMD := mkdir -p build - RMDIR_CMD := rm -rf - SLASH := / - LDLIB_SUFFIX := so - EXPORT_CMD := export -endif - -# Determine the architecture -ifeq ($(OS),Windows_NT) - ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) - ARCH := x64 - else - ARCH := x86 - endif -else - ARCH := $(shell uname -m) - ifeq ($(ARCH), x86_64) - ARCH := x64 - else ifneq (,$(findstring arm, $(ARCH))) - ARCH := arm - endif -endif - -# Paths -GPUCPP ?= $(shell pwd) - -default: build_$(TARGET)_release - -run: build_$(TARGET)_debug dawnlib examples_$(TARGET)_build_$(TARGET) - -build_$(TARGET)_release: -ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release -else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . -endif - -build_$(TARGET)_debug: -ifeq ($(OS),Windows_NT) - @if not exist "build" $(MKDIR_CMD) && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . --config Release -else - @command mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && cmake --build . -endif - -examples_$(TARGET)_build_$(TARGET): check-clang dawnlib check-linux-vulkan -ifeq ($(OS),Windows_NT) - cd examples$(SLASH)$(TARGET) && examples$(SLASH)$(TARGET)$(SLASH)build$(SLASH)$(TARGET).exe -else - cd examples/$(TARGET) && $(MAKE) build/$(TARGET) && ./build/$(TARGET)/build -endif - -dawnlib: -ifeq ($(OS),Windows_NT) - @if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(GPUCPP)$(SLASH)third_party$(SLASH)lib$(SLASH)libdawn.dll" (make run_setup) -else - @if [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn.so" ] && [ ! -f "$(GPUCPP)/third_party/lib/libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \ - $(MAKE) run_setup; \ - fi -endif - -run_setup: check-python -ifeq ($(OS),Windows_NT) - cd $(GPUCPP) && if exist "python3.exe" (python3 setup.py) else (if exist "python.exe" (python setup.py) else (echo "Python needs to be installed and in your path." & exit 1)) -else - cd $(GPUCPP) && (command -v python3 >/dev/null 2>&1 && python3 setup.py || python setup.py) -endif - -clean: -ifeq ($(OS),Windows_NT) - $(RMDIR_CMD) build -else - read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build* -endif - -check-python: -ifeq ($(OS),Windows_NT) - @if not exist "$(shell where python3.exe 2>NUL)" if not exist "$(shell where python.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1) -else - @command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1 || { echo "Python needs to be installed and in your path."; exit 1; } -endif \ No newline at end of file From 15c54152516b5320f344048c8e55a058e19286e4 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 23:36:59 -0500 Subject: [PATCH 23/24] fix clean matmul --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2a8b4e8..0b40684 100644 --- a/Makefile +++ b/Makefile @@ -121,7 +121,7 @@ ifeq ($(OS),Windows_NT) @if exist build $(RMDIR_CMD) build /s /q @if exist examples$(SLASH)gpu_puzzles$(SLASH)build $(RMDIR_CMD) examples$(SLASH)gpu_puzzles$(SLASH)build /s /q @if exist examples$(SLASH)hello_world$(SLASH)build $(RMDIR_CMD) examples$(SLASH)hello_world$(SLASH)build /s /q - @if exist examples$(SLASH)matmul$(SLASH)build$(SLASH)mm $(RMDIR_CMD) examples$(SLASH)matmul$(SLASH)build$(SLASH)mm /s /q + @if exist examples$(SLASH)matmul$(SLASH)build $(RMDIR_CMD) examples$(SLASH)matmul$(SLASH)build /s /q @if exist examples$(SLASH)physics$(SLASH)build $(RMDIR_CMD) examples$(SLASH)physics$(SLASH)build /s /q @if exist examples$(SLASH)render$(SLASH)build $(RMDIR_CMD) examples$(SLASH)render$(SLASH)build /s /q @if exist build$(SLASH)gpu.h.pch del build$(SLASH)gpu.h.pch From 8e247539186cfc6e5f4aee5b18c70b4dea1f52b9 Mon Sep 17 00:00:00 2001 From: MichealReed Date: Thu, 18 Jul 2024 23:55:03 -0500 Subject: [PATCH 24/24] Fix example standard --- cmake/example.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/example.cmake b/cmake/example.cmake index ab4cad2..7779a30 100644 --- a/cmake/example.cmake +++ b/cmake/example.cmake @@ -1,5 +1,5 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # export compile_commands.json to use with LSP -set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)