-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
182 lines (148 loc) · 4.95 KB
/
Makefile
File metadata and controls
182 lines (148 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# abc def
# === common
#
# Parameters
TRUNK = trunk
WASM_OPT = wasm-opt
WASM_STRIP = wasm-strip
# Paths
DIST_DIR = dist
SHADERS_DIR = shaders
SHADER_FILE = $(SHADERS_DIR)/shader.frag
INDEX_HTML = $(DIST_DIR)/index.html
# Comma
comma := ,
# Checks two given strings for equality.
eq = $(if $(or $(1),$(2)),$(and $(findstring $(1),$(2)),\
$(findstring $(2),$(1))),1)
#
# === Install dependencies commands
#
# The main command to install all dependencies
deps-install: trunk-install binaryen-install wabt-install
# Installation of Trunk using Cargo
# instructions: ttps://trunkrs.dev/#install
trunk-install:
@which trunk || (echo "Installing Trunk..." && \
cargo install trunk)
# Installation of Binaryen (for wasm-opt)
# instructions: https://github.com/WebAssembly/wabt
binaryen-install:
@which wasm-opt || (echo "Installing Binaryen tools..." && \
curl -L https://github.com/WebAssembly/binaryen/releases/download/version_119/binaryen-version_119-x86_64-linux.tar.gz | tar xz && \
sudo mv binaryen-version_119/bin/* /usr/local/bin/ && \
rm -rf binaryen-version_119)
# Installation of WABT (for wasm-strip)
# instructions: https://github.com/WebAssembly/binaryen?tab=readme-ov-file#releases
wabt-install:
@which wasm-strip || (echo "Installing WABT tools..." && \
curl -L https://github.com/WebAssembly/wabt/releases/download/1.0.36/wabt-1.0.36-ubuntu-20.04.tar.gz | tar xz && \
sudo mv wabt-1.0.36/bin/* /usr/local/bin/ && \
rm -rf wabt-1.0.36)
#
# === External cargo crates commands
#
# Check vulnerabilities with cargo-audit.
#
# Usage :
# make audit
audit:
cargo audit
#
# === General commands
#
# Lint Rust sources with Clippy.
#
# Usage :
# make lint [warnings=(no|yes)] [manifest_path=(|[path])]
lint :
cargo clippy --all-features \
$(if $(call eq,$(manifest_path),),--manifest-path ./Cargo.toml,--manifest-path $(manifest_path)) \
$(if $(call eq,$(warnings),no),-- -D warnings,)
# Check Rust sources `check`.
#
# Usage :
# make check [manifest_path=(|[path])]
check :
cargo check \
$(if $(call eq,$(manifest_path),),--manifest-path ./Cargo.toml,--manifest-path $(manifest_path))
# Format and lint Rust sources.
#
# Usage :
# make normalize
normalize : fmt lint
# Format Rust sources with rustfmt.
#
# Usage :
# make fmt [check=(no|yes)]
fmt :
cargo +nightly fmt --all $(if $(call eq,$(check),yes),-- --check,)
# Run project Rust sources with Cargo.
#
# Usage :
# make clean
clean :
cargo clean && rm -rf Cargo.lock && cargo cache -a && cargo update
#
# === Build package commands
#
# Default build
build: build-wasm optimize-wasm update-js-filename replace-shader
# 1. Build wasm with Trunk
build-wasm:
@echo "Building project with Trunk..."
$(TRUNK) build --release
# 2. Optimization of wasm: wasm-strip + wasm-opt
optimize-wasm:
@for file in $(wildcard $(DIST_DIR)/*.wasm); do \
echo "Compressing $$file..."; \
$(WASM_STRIP) $$file; \
$(WASM_OPT) -Os -o $$file $$file; \
done
# 3. Updating js filenames of generated files in index.html
update-js-filename:
@for js_file in $(wildcard $(DIST_DIR)/*.js); do \
echo "Correcting JS filename in index.html: $$js_file"; \
filename=$$(basename $$js_file); \
sed -i -e "s/dynamic_javascript_filename.js/$$filename/g" $(INDEX_HTML); \
done
# 4. Replacing default shader code in index.html
replace-shader: $(SHADER_FILE)
@echo "Reading shader code from $(SHADER_FILE)..."
@shader_text=$$(sed ':a;N;$$!ba;s/\n/\\\\n\\\\\\n/g;s/\//\\\//g' < $<); \
echo "Replacing default shader code in index.html..."; \
sed -i -e "s/default_shader_code_text/$$shader_text/g" $(INDEX_HTML)
# Видалення згенерованих файлів (якщо потрібно)
clean-dist:
rm -rf $(DIST_DIR)
# === Check Development Environment ===
# Check if the development environment is ready and all dependencies are installed.
# Usage:
# make check-env
check-env:
@echo "Checking development environment..."
@missing_tools="" ; \
if ! command -v rustc > /dev/null 2>&1; then missing_tools="$$missing_tools rust"; fi ; \
if ! command -v cargo > /dev/null 2>&1; then missing_tools="$$missing_tools cargo"; fi ; \
if ! command -v trunk > /dev/null 2>&1; then missing_tools="$$missing_tools trunk"; fi ; \
if ! command -v wasm-opt > /dev/null 2>&1; then missing_tools="$$missing_tools wasm-opt"; fi ; \
if ! command -v wasm-strip > /dev/null 2>&1; then missing_tools="$$missing_tools wasm-strip"; fi ; \
if ! rustup target list --installed | grep -q wasm32-unknown-unknown; then \
missing_tools="$$missing_tools wasm32-unknown-unknown"; \
fi ; \
if [ -n "$$missing_tools" ]; then \
echo "The following tools are missing: $$missing_tools"; \
echo "Please install the missing tools to proceed."; \
else \
echo "All necessary tools are installed."; \
fi
# Command list
.PHONY: deps-install trunk-install binaryen-install wabt-install \
check-env \
build build-wasm optimize-wasm update-js-filename replace-shader clean-dist \
audit \
lint \
check \
fmt \
normalize \
clean