# Semana 14: grafos y union-find. Se usa dentro del entorno del curso.
#
#   make           compila los programas de la clase en build/
#   make lab       compila el laboratorio en build/lab/
#   make test      corre las pruebas de los ejemplos y de los apuntes
#   make test-uf     paso 1: UnionFind, contra la versión ingenua
#   make test-maze   paso 2: el generador
#   make test-search paso 3: el grafo, BFS y DFS
#   make test-lab  prueba todo su laboratorio; falla hasta que lo terminen
#   make compare   paso 4: la tabla para su informe
#   make clean     borra lo generado
#
# Los programas de errors/ tienen errores a propósito. No se compilan con make:
# los compilan las pruebas, y ustedes, con los comandos de los apuntes.

CXX = g++
CXXFLAGS = -Wall -Wextra -std=c++20 -g -fsanitize=address,undefined
BENCHFLAGS = -Wall -Wextra -std=c++20 -O2
DATA ?= ../../data
LAB_DIR ?= lab
BUILD = build
PROGRAMS = bfs_trace dfs_demo representations uf_demo uf_costs test_structures
BENCHES = map_tour online_connectivity bfs_scaling
COMMON_HEADERS = graph.h matrix_graph.h traversals.h sample_graphs.h map_file.h counting_forest.h bench.h
LAB_HEADERS = $(LAB_DIR)/union_find.h $(LAB_DIR)/maze.h $(LAB_DIR)/solver.h lab/graph.h lab/naive_union_find.h lab/not_implemented.h

.PHONY: all lab test test-uf test-maze test-search test-lab compare clean

all: $(PROGRAMS:%=$(BUILD)/%) $(BENCHES:%=$(BUILD)/%)

$(PROGRAMS:%=$(BUILD)/%): $(BUILD)/%: %.cpp $(COMMON_HEADERS) | $(BUILD)
	$(CXX) $(CXXFLAGS) $< -o $@

# Las mediciones van con -O2 y sin sanitizers.
$(BENCHES:%=$(BUILD)/%): $(BUILD)/%: %.cpp $(COMMON_HEADERS) | $(BUILD)
	$(CXX) $(BENCHFLAGS) $< -o $@

# LAB_DIR va primero: así los programas toman los archivos de esa carpeta.
$(BUILD)/$(LAB_DIR)/lab_tests: lab/tests/lab_tests.cpp $(LAB_HEADERS) | $(BUILD)/$(LAB_DIR)
	$(CXX) $(CXXFLAGS) -I$(LAB_DIR) -Ilab $< -o $@

$(BUILD)/$(LAB_DIR)/maze: lab/tests/maze_main.cpp $(LAB_HEADERS) | $(BUILD)/$(LAB_DIR)
	$(CXX) $(CXXFLAGS) -I$(LAB_DIR) -Ilab $< -o $@

$(BUILD)/$(LAB_DIR)/compare_mazes: lab/tests/compare_mazes.cpp $(LAB_HEADERS) | $(BUILD)/$(LAB_DIR)
	$(CXX) $(CXXFLAGS) -I$(LAB_DIR) -Ilab $< -o $@

$(BUILD) $(BUILD)/$(LAB_DIR):
	mkdir -p $@

lab: $(BUILD)/$(LAB_DIR)/lab_tests $(BUILD)/$(LAB_DIR)/maze $(BUILD)/$(LAB_DIR)/compare_mazes

# Si existe solution/, también se comprueba la solución del laboratorio.
test: all
	@if [ -d solution ]; then $(MAKE) --no-print-directory LAB_DIR=solution lab; fi
	DATA=$(DATA) python3 -m unittest discover -p 'test_*.py'

test-uf: lab
	LAB_DIR=$(LAB_DIR) python3 -m unittest -v test_lab.TestUnionFind

test-maze: lab
	LAB_DIR=$(LAB_DIR) python3 -m unittest -v test_lab.TestGenerator

test-search: lab
	LAB_DIR=$(LAB_DIR) python3 -m unittest -v test_lab.TestSearch

test-lab: all lab
	LAB_DIR=$(LAB_DIR) python3 -m unittest -v test_lab

compare: $(BUILD)/$(LAB_DIR)/compare_mazes
	$<

clean:
	rm -rf $(BUILD) __pycache__
