1
- NUM_JOBS =$(shell nproc)
1
+ NUM_JOBS =$(shell nproc 2>/dev/null || echo 1 )
2
2
CXX =clang++
3
3
4
- .PHONY : default examples/hello_world/build/hello_world tests libgpu debug build check-clang clean-build clean all watch-tests docs
5
-
6
- GPUCPP ?= $(PWD )
7
- LIBDIR ?= $(GPUCPP ) /third_party/lib
8
- LIBSPEC ?= . $(GPUCPP ) /source
9
-
10
- default : examples/hello_world/build/hello_world
11
-
12
- pch :
13
- mkdir -p build && $(CXX ) -std=c++17 -I$(GPUCPP ) -I$(GPUCPP ) /third_party/headers -x c++-header gpu.h -o build/gpu.h.pch
14
-
15
- # TODO(avh): change extension based on platform
16
- lib :
17
- 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
18
-
19
- examples/hello_world/build/hello_world : check-clang dawnlib examples/hello_world/run.cpp check-linux-vulkan
20
- $(LIBSPEC ) && cd examples/hello_world && make build/hello_world && ./build/hello_world
21
-
22
- dawnlib : $(if $(wildcard third_party/lib/libdawn.so third_party/lib/libdawn.dylib) ,,run_setup)
4
+ .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
5
+ .PHONY : $(addprefix run_, $(TARGETS ) )
6
+
7
+ # List of targets (folders in your examples directory)
8
+ TARGETS := gpu_puzzles hello_world matmul physics render shadertui
9
+
10
+ # Set up variables for cross-platform compatibility
11
+ ifeq ($(OS ) ,Windows_NT)
12
+ DETECTED_OS := Windows
13
+ MKDIR_CMD := if not exist build mkdir build
14
+ RMDIR_CMD := rmdir
15
+ SLASH := \\
16
+ LS_CMD := dir
17
+ LDLIB_SUFFIX := dll
18
+ EXPORT_CMD := set
19
+ else
20
+ DETECTED_OS := $(shell uname)
21
+ MKDIR_CMD := mkdir -p build
22
+ RMDIR_CMD := rm -rf
23
+ SLASH := /
24
+ LS_CMD := ls
25
+ LDLIB_SUFFIX := so
26
+ EXPORT_CMD := export
27
+ endif
28
+
29
+ # Determine the architecture
30
+ ifeq ($(OS ) ,Windows_NT)
31
+ ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
32
+ ARCH := x64
33
+ else
34
+ ARCH := x86
35
+ endif
36
+ else
37
+ ARCH := $(shell uname -m)
38
+ ifeq ($(ARCH), x86_64)
39
+ ARCH := x64
40
+ else ifneq (,$(findstring arm, $(ARCH)))
41
+ ARCH := arm
42
+ endif
43
+ endif
44
+
45
+ # Determine the build type
46
+ BUILD_TYPE ?= Release
47
+ LOWER_BUILD_TYPE ?= $(shell python3 -c "print('$(BUILD_TYPE ) '.lower() )")
48
+
49
+ # Paths
50
+ GPUCPP ?= $(shell pwd)
51
+ LIBDIR ?= $(GPUCPP )$(SLASH ) third_party$(SLASH ) lib
52
+ LIBSPEC ?= . $(GPUCPP )$(SLASH ) source
53
+
54
+ default : run_hello_world
55
+
56
+ # Define Run Rules
57
+ define RUN_RULES
58
+ run_$(1 ) :
59
+ @cd examples && $(MAKE ) run_$(1 )
60
+ endef
61
+ # Apply Run Rules to each target in $(TARGETS)
62
+ $(foreach target,$(TARGETS),$(eval $(call RUN_RULES,$(target))))
63
+
64
+ # Build rules for specific targets
65
+ define BUILD_RULES
66
+ build_$(1 ) :
67
+ ifeq ($(OS ) ,Windows_NT)
68
+ cd examples && $(MAKE) $(1)_$(LOWER_BUILD_TYPE)
69
+ else
70
+ cd examples&& $(MAKE) $(1)_$(LOWER_BUILD_TYPE)
71
+ endif
72
+ endef
73
+ $(foreach target, $(TARGETS), $(eval $(call BUILD_RULES,$(target))))
74
+
75
+ # We use the custom "shell" based condition to check files cross-platform
76
+ dawnlib :
77
+ ifeq ($(OS ) ,Windows_NT)
78
+ @if not exist "$(LIBDIR)$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dll" if not exist "$(LIBDIR)$(SLASH)libdawn.dll" $(MAKE) run_setup
79
+ else
80
+ @if [ ! -f "$(LIBDIR)$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).so" ] && [ ! -f "$(LIBDIR)$(SLASH)libdawn.so" ] && [ ! -f "$(LIBDIR)$(SLASH)libdawn_$(ARCH)_$(BUILD_TYPE).dylib" ]; then \
81
+ $(MAKE) run_setup; \
82
+ fi
83
+ endif
23
84
24
85
run_setup : check-python
86
+ ifeq ($(OS ) ,Windows_NT)
25
87
python3 setup.py
88
+ else
89
+ python3 >/dev/null 2>&1 && python3 setup.py
90
+ endif
26
91
27
- all : dawnlib check-clang check-linux-vulkan lib pch
28
- cd examples/ gpu_puzzles && make build/ gpu_puzzles
29
- cd examples/ hello_world && make build/ hello_world
30
- cd examples/ matmul && make build/ mm
31
- cd examples/ physics && make build/ physics
32
- cd examples/ render && make build/ render
92
+ all : dawnlib check-clang check-linux-vulkan
93
+ cd examples$( SLASH ) gpu_puzzles && make build$( SLASH ) gpu_puzzles
94
+ cd examples$( SLASH ) hello_world && make build$( SLASH ) hello_world
95
+ cd examples$( SLASH ) matmul && make build$( SLASH ) mm
96
+ cd examples$( SLASH ) physics && make build$( SLASH ) physics
97
+ cd examples$( SLASH ) render && make build$( SLASH ) render
33
98
34
99
docs : Doxyfile
35
100
doxygen Doxyfile
36
101
37
- # ###############################################################################
38
- # cmake targets (optional - precompiled binaries is preferred)
39
- # ###############################################################################
40
-
41
- CMAKE_CMD = mkdir -p build && cd build && cmake ..
42
- # Add --trace to see the cmake commands
43
- FLAGS = -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_CXX_COMPILER=$(CXX ) -DABSL_INTERNAL_AT_LEAST_CXX20=OFF
44
- FASTBUILD_FLAGS = $(FLAGS ) -DFASTBUILD:BOOL=ON
45
- DEBUG_FLAGS = $(FLAGS ) -DDEBUG:BOOL=ON
46
- RELEASE_FLAGS = $(FLAGS ) -DFASTBUILD:BOOL=OFF
47
- TARGET_LIB =gpu
48
-
49
- libgpu-cmake : check-clang check-cmake
50
- $(CMAKE_CMD ) $(RELEASE_FLAGS ) && make -j$(NUM_JOBS ) gpu
51
-
52
- debug-cmake : check-clang check-cmake
53
- $(CMAKE_CMD ) $(DEBUG_FLAGS ) && make -j$(NUM_JOBS ) $(TARGET_ALL )
54
-
55
- all-cmake : check-clang check-cmake
56
- $(CMAKE_CMD ) $(RELEASE_FLAGS ) && make -j$(NUM_JOBS ) $(TARGET_ALL )
57
-
58
- # ###############################################################################
59
102
# Cleanup
60
103
# ###############################################################################
61
104
105
+ # Clean rules for cleaning specific targets
106
+ define CLEAN_RULES
107
+ clean_$(1 ) :
108
+ ifeq ($(OS ) ,Windows_NT)
109
+ @if exist examples$(SLASH)$(1)$(SLASH)build ( $(RMDIR_CMD) /s examples$(SLASH)$(1)$(SLASH)build )
110
+ else
111
+ find examples$(SLASH)$(1) -name build -type d | xargs rm -rf
112
+ endif
113
+ endef
114
+ $(foreach target,$(TARGETS),$(eval $(call CLEAN_RULES,$(target))))
115
+
62
116
clean-dawnlib :
63
- rm -f third_party/lib/ libdawn.so third_party/lib/libdawn.dylib
117
+ $( RMDIR_CMD ) $( LIBDIR )$( SLASH ) libdawn* . *
64
118
65
119
clean :
66
- read -r -p " This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build/*
67
- rm -rf examples/gpu_puzzles/build/*
68
- rm -rf examples/hello_world/build/*
120
+ ifeq ($(OS ) ,Windows_NT)
121
+ @if exist build $(RMDIR_CMD) build /s /q
122
+ @if exist examples$(SLASH)gpu_puzzles$(SLASH)build $(RMDIR_CMD) examples$(SLASH)gpu_puzzles$(SLASH)build /s /q
123
+ @if exist examples$(SLASH)hello_world$(SLASH)build $(RMDIR_CMD) examples$(SLASH)hello_world$(SLASH)build /s /q
124
+ @if exist examples$(SLASH)matmul$(SLASH)build $(RMDIR_CMD) examples$(SLASH)matmul$(SLASH)build /s /q
125
+ @if exist examples$(SLASH)physics$(SLASH)build $(RMDIR_CMD) examples$(SLASH)physics$(SLASH)build /s /q
126
+ @if exist examples$(SLASH)render$(SLASH)build $(RMDIR_CMD) examples$(SLASH)render$(SLASH)build /s /q
127
+ @if exist build$(SLASH)gpu.h.pch del build$(SLASH)gpu.h.pch
128
+ $(MKDIR_CMD)
129
+ else
130
+ @command read -r -p "This will delete the contents of build/*. Are you sure? [CTRL-C to abort] " response && rm -rf build*
131
+ rm -rf examples/gpu_puzzles/build*
132
+ rm -rf examples/hello_world/build*
69
133
rm -rf examples/matmul/build/mm
70
134
rm -rf examples/physics/build/*
71
135
rm -rf examples/render/build/*
72
136
rm -f build/gpu.h.pch
73
137
rm -f build/libgpucpp.so
138
+ endif
74
139
75
140
clean-all :
76
- 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
141
+ ifeq ($(OS ) ,Windows_NT)
142
+ @if exist build $(RMDIR_CMD) build /s /q
143
+ $(RMDIR_CMD) third_party$(SLASH)fetchcontent /s /q
144
+ $(RMDIR_CMD) third_party$(SLASH)gpu-build /s /q
145
+ $(RMDIR_CMD) third_party$(SLASH)gpu-subbuild /s /q
146
+ $(RMDIR_CMD) third_party$(SLASH)gpu-src /s /q
147
+ $(RMDIR_CMD) third_party$(SLASH)lib /s /q
148
+ $(MKDIR_CMD)
149
+ else
150
+ 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).*
151
+ endif
77
152
78
- # ###############################################################################
79
153
# Checks
80
154
# ###############################################################################
81
155
82
- # check for the existence of clang++ and cmake
156
+ # check for the existence of clang++
83
157
check-clang :
158
+ ifeq ($(OS ) ,Windows_NT)
159
+ @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)
160
+ else
84
161
@command -v clang++ >/dev/null 2>&1 || { echo >&2 "Please install clang++ with 'sudo apt-get install clang' or 'brew install llvm'"; exit 1; }
162
+ endif
163
+
164
+ # check for the existence of entr
165
+ check-entr :
166
+ ifeq ($(OS ) ,Windows_NT)
167
+ @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)
168
+ else
169
+ @command -v entr >/dev/null 2>&1 || { echo >&2 "Please install entr with 'brew install entr' or 'sudo apt-get install entr'"; exit 1; }
170
+ endif
85
171
172
+ # check for the existence of cmake
86
173
check-cmake :
174
+ ifeq ($(OS ) ,Windows_NT)
175
+ @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)
176
+ else
87
177
@command -v cmake >/dev/null 2>&1 || { echo >&2 "Please install cmake with 'sudo apt-get install cmake' or 'brew install cmake'"; exit 1; }
178
+ endif
88
179
180
+ # check for the existence of python3
89
181
check-python :
90
- @command -v python3 > /dev/null 2>&1 || { echo >&2 " Python needs to be installed and in your path." ; exit 1; }
182
+ ifeq ($(OS ) ,Windows_NT)
183
+ @if not exist "$(shell where python3.exe 2>NUL)" (echo "Python needs to be installed and in your path." & exit 1)
184
+ else
185
+ @command -v python3 >/dev/null 2>&1 || { echo >&2 "Python needs to be installed and in your path."; exit 1; }
186
+ endif
91
187
188
+ # check the existence of Vulkan (Linux only)
92
189
check-linux-vulkan :
93
190
@echo " Checking system type and Vulkan availability..."
94
- @if [ " $$ (uname)" = " Linux" ]; then \
95
- if command -v vulkaninfo > /dev/null 2>&1 ; then \
96
- echo " Vulkan is installed." ; \
97
- vulkaninfo; \
98
- else \
99
- echo "Vulkan is not installed. Please install Vulkan drivers to continue. On Debian / Ubuntu : sudo apt install libvulkan1 mesa-vulkan-drivers vulkan-tools"; \
100
- exit 1; \
101
- fi \
102
- else \
103
- echo " Non-Linux system detected. Skipping Vulkan check." ; \
104
- fi
191
+ ifeq ($(OS ) ,Linux)
192
+ @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; }
193
+ else
194
+ @echo "Non-Linux system detected. Skipping Vulkan check.";
195
+ endif
0 commit comments