# =============================================================================
# Makefile for Stanford CS149 Lecture 13 demos (DSLs and automatic optimization)
# =============================================================================
#
# [What this builds]
#   Two C++ programs: the slides' blur versions timed (01) and an autotuner
#   over tile shapes (02, which also writes autotune_grid.csv for demo 04).
#   Demos 03-05 are Python and need numpy.
#
# [How to use]
#   make            # build
#   make run        # run all demos in lecture order (~15 s; demo 02 is ~5-10 s)
#   make selftest   # self-check of minihalide.py
#   make clean
# -----------------------------------------------------------------------------

CXX      ?= c++
CXXFLAGS := -std=c++20 -O2 -g -Wall -Wextra -pthread
PYTHON   ?= python3
HEADERS  := ../lecture01/cs149_timer.h ../lecture01/machine_info.h blur_kernels.h spin_pool.h
BINS     := 01_blur_ladder 02_blur_autotune

ifeq ($(shell uname -m),x86_64)
CXXFLAGS += -mavx2 -mfma
endif

.PHONY: all run selftest clean
all: $(BINS)

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

run: all
	@echo; ./01_blur_ladder
	@echo; ./02_blur_autotune
	@echo; $(PYTHON) 03_halide_schedules.py
	@echo; $(PYTHON) 04_autoschedule.py
	@echo; $(PYTHON) 05_kernel_checker.py

selftest:
	$(PYTHON) minihalide.py

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