Skip to content

Commit

Permalink
Split rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
n3vu0r committed Aug 24, 2023
1 parent 8419e44 commit bd5ca77
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions examples/wasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,71 @@ EXAMPLES := exponential_map gliding_clamp constellation_clamp egui

.PHONY: help
help:
@printf "make install Installs required tools.\n"
@printf "make wasm -j Builds optimized WASM examples.\n"
@printf "make serve Runs local HTTP server.\n"
@printf "make clean Removes ./target directory.\n"
@printf "make [help] Prints this help.\n"
@printf "make install Installs required tools except rustup and cargo.\n"
@printf "make wasm [-j] Builds optimized WASM examples.\n"
@printf "make html [-j] Generates HTML for examples.\n"
@printf "make host [-j] Runs local HTTP server (host: wasm html).\n"
@printf "make clean Removes ./target directory.\n"

.PHONY: install
install:
command -v apt && sudo apt install binaryen || true
command -v dnf && sudo dnf install binaryen || true
command -v yum && sudo yum install binaryen || true
command -v pacman && sudo pacman -S --needed binaryen || true
command -v apt && sudo apt install binaryen jq || true
command -v dnf && sudo dnf install binaryen jq || true
command -v yum && sudo yum install binaryen jq || true
command -v pacman && sudo pacman -S --needed binaryen jq || true
rustup target add wasm32-unknown-unknown
cargo install wasm-bindgen-cli basic-http-server

.PHONY: wasm
wasm: $(EXAMPLES:%=target/%.wasm)
.PHONY: html
html: $(EXAMPLES:%=target/%.html) target/index.html

target:
mkdir -p target
ln -sf $(word 1,$(EXAMPLES)).html target/index.html
mkdir target

.PHONY: update
udpate:;

target/metadata: ../../Cargo.toml | target
cargo metadata --no-deps --format-version 1 > $@

.PHONY: build
build: | target
@for example in $(EXAMPLES); do \
printf " \033[1;32mChecking\033[0m $$example(example)\n"; \
features=$$(cargo metadata --no-deps --format-version 1 | \
jq --raw-output ".packages[] | select(.name==\"bevy_trackball\") | .targets[] | \
select(.name==\"$$example\") | .\"required-features\" | join(\",\")"); \
cargo build --profile wasm-release --target wasm32-unknown-unknown \
--features $${features:-,} --example $$example; \
jq -Rr @html ../../examples/$$example.rs > target/$$example.code; \
sed -e 's/{EXAMPLE}/'$$example'/g' -e '/{SOURCE}/{r target/'$$example.code -e 'd}' \
example.html > target/$$example.html; \
done

../../target/wasm32-unknown-unknown/wasm-release/examples/%.wasm: build
@true
target/%.features: target/metadata
cat $< | jq -r ".packages[] | select(.name==\"bevy_trackball\") | .targets[] | \
select(.name==\"$*\") | .\"required-features\" | join(\",\")" > $@

../../target/wasm32-unknown-unknown/wasm-release/examples/%.wasm: target/%.features update
@printf " \033[1;32mUpdating\033[0m $*(example)\n"
cargo build --profile wasm-release --target wasm32-unknown-unknown -F $$(cat $<), --example $*

target/%_bg.wasm: ../../target/wasm32-unknown-unknown/wasm-release/examples/%.wasm
@printf " \033[1;32mBinding\033[0m $*(example)\n"; \
@printf " \033[1;32mBinding\033[0m $*(example)\n"
wasm-bindgen --target web --out-dir target --out-name $* $<

target/%.wasm: target/%_bg.wasm
@printf " \033[1;32mOptimizing\033[0m $*(example)\n"; \
@printf " \033[1;32mOptimizing\033[0m $*(example)\n"
mv $< $@
wasm-opt -Oz --output $< $@
touch $@

.PHONY: serve
serve: wasm
@printf " \033[1;32mServing\033[0m examples\n"; \
target/index.html:
ln -sf $(word 1,$(EXAMPLES)).html target/index.html

target/input.code: ../../src/lib.rs | target
sed -n '/| Keyboard |/,/| `Return` |/p' $< | jq -Rr @html > $@

target/%.code: ../%.rs target/input.code
cat target/input.code > $@
printf '%s\n' '//!' >> $@
jq -Rr @html $< >> $@

target/%.html: target/%.code example.html
sed -e 's/{EXAMPLE}/'$*'/g' -e '/{SOURCE}/{r '$< -e 'd}' example.html > $@

.PHONY: host
host: wasm html
@printf " \033[1;32mServing\033[0m examples\n"
basic-http-server target

.PHONY: clean
Expand Down

0 comments on commit bd5ca77

Please sign in to comment.