# =============================================================================
# Makefile for Stanford CS149 Lecture 10 demos (hardware specialization)
# =============================================================================
#
# [What this builds]
#   One C++ program (macOS on Apple silicon): the same matrix multiply on the
#   NEON cores (../lecture09/gemm_neon.h) and on the M4's matrix hardware via
#   Apple's Accelerate. The three Python demos need numpy (demo 01 only the
#   standard library).
#
# [How to use]
#   make            # build
#   make run        # run all demos in lecture order (~5 s)
#   make clean
# -----------------------------------------------------------------------------

CXX      ?= c++
CXXFLAGS := -std=c++20 -O2 -g -Wall -Wextra -pthread
LDLIBS   := -framework Accelerate
PYTHON   ?= python3
HEADERS  := ../lecture01/cs149_timer.h ../lecture01/machine_info.h ../lecture09/gemm_neon.h
BINS     := 04_matrix_unit
OK_HOST  := $(and $(filter Darwin,$(shell uname -s)),$(filter arm64,$(shell uname -m)))

.PHONY: all run clean
ifeq ($(OK_HOST),)
all:
	@echo "04_matrix_unit needs macOS on Apple silicon (NEON + Accelerate); skipping it."
else
all: $(BINS)
endif

$(BINS): %: %.cpp $(HEADERS)
	$(CXX) $(CXXFLAGS) -o $@ $< $(LDLIBS)

run: all
	@echo; $(PYTHON) 01_energy_model.py
	@echo; $(PYTHON) 02_systolic_array.py
	@echo; $(PYTHON) 03_number_formats.py
	@if [ -x ./04_matrix_unit ]; then echo; ./04_matrix_unit; fi

clean:
	rm -f $(BINS)
	rm -rf *.dSYM __pycache__
