Update Makefile

Fixed edge case in serve target by properly handling frontend process cleanup

- Replaced `kill %1` with `kill $$FRONTEND_PID` to ensure correct process termination.
- Captures frontend process PID (`$$!`) explicitly for better reliability.
- Prevents potential issues when multiple background jobs exist.
- Improved Makefile structure for readability and maintainability.

Signed-off-by: Yehowshua <yehowshua@joyofhardware.com>
This commit is contained in:
Yehowshua 2025-02-02 21:06:29 +00:00
parent 919c1772e7
commit dafd0ceedf

View file

@ -9,6 +9,7 @@ ELM_MAKE := elm make
CARGO_BUILD := cargo build
CARGO_RUN := cargo run
# Ensure exactly one of DEBUG or RELEASE is set
ifeq ($(DEBUG)$(RELEASE),) # Both are empty
$(error You must set exactly one of DEBUG=1 or RELEASE=1)
endif
@ -20,14 +21,15 @@ endif
# Targets
.PHONY: all frontend backend clean serve
# Ensure PUBLIC_DIR exists
$(PUBLIC_DIR):
mkdir -p $(PUBLIC_DIR)
# Copy index.html into PUBLIC_DIR
$(PUBLIC_DIR)/index.html: $(FRONTEND_DIR)/index.html $(PUBLIC_DIR)
cp $< $@
.PHONY: frontend backend
# Frontend build target
frontend: $(PUBLIC_DIR)/index.html $(PUBLIC_DIR)
mkdir -p $(PUBLIC_DIR)/assets
make -C $(FRONTEND_DIR)
@ -35,14 +37,21 @@ frontend: $(PUBLIC_DIR)/index.html $(PUBLIC_DIR)
cp $(FRONTEND_DIR)/src/ports.websocket.js $(PUBLIC_DIR)/
cp $(FRONTEND_DIR)/assets/CourierPrime-Regular.ttf $(PUBLIC_DIR)/assets/
# Backend build target
backend:
$(CARGO_BUILD) --manifest-path=$(BACKEND_DIR)/Cargo.toml
# Clean target (optional, assuming we want to remove build artifacts)
clean:
rm -rf $(PUBLIC_DIR)
# Serve: Runs both frontend and backend
serve: frontend backend
ifeq ($(DEBUG),1)
$(MAKE) -C $(FRONTEND_DIR) serve DEBUG=1 & \
FRONTEND_PID=$$!; \
RUST_LOG=info,actix_web=debug $(CARGO_RUN) --manifest-path=$(BACKEND_DIR)/Cargo.toml; \
kill %1
kill $$FRONTEND_PID
else ifeq ($(RELEASE),1)
$(CARGO_RUN) --release --manifest-path=$(BACKEND_DIR)/Cargo.toml
endif