# Makefile du TP de la Seance 2 -- make : tout compiler ; make test : les tests ; make ubsan : tests et etape 3 avec
# -fsanitize=undefined ; make asan : tests avec AddressSanitizer ; make valgrind ; make optim : etape 3 bis en -O0 et -O2 ;
# make clean && make CFLAGS+=-Wconversion : etape 7 (override : les options du cours restent)
CC = gcc
override CFLAGS += -std=c17 -Wall -Wextra -O0 -g
LDLIBS = -lm
PROGS = test_repr etape1_octets etape3_montant etape4_drapeau etape5_flottants etape6_bmp

all: $(PROGS)

repr.o: repr.c repr.h
	$(CC) $(CFLAGS) -c repr.c -o repr.o

%: %.c repr.o repr.h
	$(CC) $(CFLAGS) $< repr.o -o $@ $(LDLIBS)

test: test_repr
	./test_repr

optim: etape3b_optim.c
	$(CC) $(CFLAGS) etape3b_optim.c -o etape3b_O0
	$(CC) $(subst -O0,-O2,$(CFLAGS)) etape3b_optim.c -o etape3b_O2
	./etape3b_O0
	./etape3b_O2

ubsan: test_repr.c etape3_montant.c repr.c repr.h
	$(CC) $(CFLAGS) -fsanitize=undefined test_repr.c repr.c -o test_repr_ubsan $(LDLIBS)
	$(CC) $(CFLAGS) -fsanitize=undefined etape3_montant.c repr.c -o etape3_montant_ubsan $(LDLIBS)
	-./test_repr_ubsan
	-./etape3_montant_ubsan

asan: test_repr.c repr.c repr.h
	$(CC) $(CFLAGS) -fsanitize=address -fno-omit-frame-pointer test_repr.c repr.c -o test_repr_asan $(LDLIBS)
	./test_repr_asan

valgrind: all
	for p in $(PROGS); do valgrind -q --error-exitcode=1 ./$$p > /dev/null || exit 1; done

clean:
	rm -f $(PROGS) etape3b_O0 etape3b_O2 *_ubsan *_asan *.o

.PHONY: all test optim ubsan asan valgrind clean
