# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    Makefile                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: gbaconni <gbaconni@42lausanne.ch>          +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2021/10/22 09:10:00 by gbaconni          #+#    #+#              #
#    Updated: 2021/10/29 17:38:08 by gbaconni         ###   ########.fr        #
#                                                                              #
# **************************************************************************** #
#
# make all
# make clean
# make fclean
# make re
# make
#

OUT = munit.out

LIBRARY = libft
LIBS = -lft

INCLUDE = munit
HDR = munit.h

SRC = \
	  munit.c \
	  $(INCLUDE)/munit.c

CC = gcc
CFLAGS = -Wall -Wextra -Werror
ifeq ($(DEBUG), 1)
	CFLAGS += -g -fsanitize=address -O1
endif
ifeq ($(FAST), 1)
	CFLAGS += -v -pipe -O3 -ffast-math -fomit-frame-pointer -funroll-loops
endif
ifeq ($(DEBUG), 1)
	CFLAGS += -v -g -O1 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow
	#CFLAGS += -v -g -O1 -fsanitize=address
endif

RM = rm
RMFLAGS = -f

all: compile

debug:
	@make -C $(LIBRARY) re DEBUG=1
	$(CC) $(CFLAGS) -I$(INCLUDE) $(SRC) -I${LIBRARY} -L${LIBRARY} ${LIBS} -DMUNIT_DISABLE_TIMING=1 -o $(OUT) 

fast:
	@make -C $(LIBRARY) re FAST=1
	$(CC) $(CFLAGS) -I$(INCLUDE) $(SRC) -I${LIBRARY} -L${LIBRARY} ${LIBS} -DMUNIT_DISABLE_TIMING=1 -o $(OUT) 

setup:
	@echo "Set the location of your libft to:"
	@ln -snvf $$(find ~ -mindepth 2 -maxdepth 4 -name libft.a -type f -exec dirname "{}" \; 2>/dev/null | head -n1) libft
	@echo ""
	@echo "If the above is not the correct location of your libft, use one of the following command to fix it:"
	@find ~ -mindepth 2 -maxdepth 4 -name libft.h -type f -exec dirname "{}" \; 2>/dev/null | xargs -I{} echo ln -snvf {} libft

help:
	@echo "a: nemequ/µnit"
	@echo "b: Tripouille/libftTester"
	@echo "c: jtoty/Libftest"
	@echo "d: alelievr/libft-unit-test (tests)"
	@echo "e: alelievr/libft-unit-test (bench)"
	@echo "f: ysoroko/ft_split_tester"
	@echo "test: a b c d e f"
	@echo "bonus: only test bonuses"
	@echo "update: git pull"

_munit:
	@./$(OUT) || true

a: update compile _munit
	@echo ""
	@echo "Summary:"
	@./$(OUT) 2>&1 | grep -i -e 'error' -e 'tests successful'|| true

_libftTester:
	@grep -q 'update message' libftTester/Makefile && sed -i'.orig' -e 's/update \(message\)/\1/g' libftTester/Makefile || true
	@grep -q '/libft$$' libftTester/Makefile || sed -i'.orig' -e 's/^\(PARENT_DIR.*\)/\1\/libft/' libftTester/Makefile || true

b: compile _libftTester
	@make -C libftTester m || true

_Libftest:
	@patch -d Libftest -p1 --dry-run < Libftest.patch >/dev/null 2>&1 && patch -d Libftest -p1 < Libftest.patch || true
	@test -f Libftest/my_config.sh || (cd Libftest && ./grademe.sh -u >/dev/null 2>&1) || true
	@grep -q '^PATH_LIBFT=\.\./libft' Libftest/my_config.sh || sed -i'.orig' -e 's/^\(PATH_LIBFT\)=.*/\1=..\/libft/' Libftest/my_config.sh || true
	@grep -q "'ft_memccpy'" Libftest/srcs/variables/functions/part_1.sh && sed -i'.orig' -e "/'ft_memccpy'/d" Libftest/srcs/variables/functions/part_1.sh || true
	@rm -fr Libftest/tests/Part1_functions/ft_memccpy || true

c: compile _Libftest
	@cd Libftest && ./grademe.sh -u -b || true
	@echo ""
	@echo "For more details do:"
	@echo "less Libftest/deepthought"
	@echo ""

_libft-unit-test:
	@grep -q 'ft_memccpy' libft-unit-test/src/init.c && sed -i'.orig' -e '/ft_memccpy/d' libft-unit-test/src/init.c || true
	@grep -q 'ft_memccpy' libft-unit-test/hardcore-mode/hardcore-main.c && sed -i'.orig' -e '/ft_memccpy/d' libft-unit-test/hardcore-mode/hardcore-main.c || true
	@grep -q 'test-memccpy' libft-unit-test/Makefile && sed -i'.orig' -e '/test-memccpy/d' libft-unit-test/Makefile || true
	@rm -f libft-unit-test/hardcore-mode/test-memccpy.c || true

d: compile _libft-unit-test
	@make -C libft-unit-test f | awk 'BEGIN { hide=0 } { if(/\/~~~\\/) hide=1; if(hide!=1) print; }' || true
	@rm -f libft/libft.so
	@tput init
	@echo "For more details do:"
	@echo "less libft-unit-test/result.log"
	@echo ""

e: fast _libft-unit-test
	@make -C libft-unit-test b || true

_FT_SPLIT_TESTER:
	@patch -d FT_SPLIT_TESTER -p1 --dry-run < FT_SPLIT_TESTER.patch >/dev/null 2>&1 && patch -d FT_SPLIT_TESTER -p1 < FT_SPLIT_TESTER.patch || true

f: compile _FT_SPLIT_TESTER
	@make -C FT_SPLIT_TESTER all || true

test: a b c d e f

bonus: _libftTester _Libftest _libft-unit-test
	@make -C libft bonus
	@cd Libftest && ./grademe.sh -u -ob || true
	@echo ""
	@echo "For more details do:"
	@echo "less Libftest/deepthought"
	@echo ""
	@echo ""
	@make -C libftTester b || true
	@make -C libft-unit-test f | grep -a -B2 -A999 'Bonus part' || true
	@echo ""
	@echo "For more details do:"
	@echo "less libft-unit-test/result.log"
	@rm -f libft/libft.so

update:
	@git pull

compile:
	@make -C $(LIBRARY) all
	$(CC) $(CFLAGS) -I$(INCLUDE) $(SRC) -I${LIBRARY} -L${LIBRARY} ${LIBS} -DMUNIT_DISABLE_TIMING=1 -o $(OUT) 

clean:
	@make -C $(LIBRARY) clean || true
	@make -C libftTester clean || true
	@make -C libft-unit-test clean || true

fclean: clean
	@make -C $(LIBRARY) fclean || true
	@make -C libftTester fclean || true
	@make -C libft-unit-test fclean || true
	@$(RM) $(RMFLAGS) $(OUT)

reset:
	@rm -fr Libftest
	@rm -fr libft-unit-test
	@rm -fr libftTester
	@rm -fr FT_SPLIT_TESTER
	@rm -fr munit
	@git submodule update --init

re: fclean all

