# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    Makefile                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: gbaconni@student.42lausanne.ch             +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2022/01/18 15:11:16 by gbaconni          #+#    #+#              #
#    Updated: 2022/04/13 23:03:45 by gbaconni         ###   lausanne.ch        #
#                                                                              #
# **************************************************************************** #
#
# make ft_printf
# make all
# make clean
# make fclean
# make re
# make
#

NAME = ft_printf

LIBFTPRINTF = libftprintf
LFLAGS = -L. -L$(LIBFTPRINTF) -lftprintf

SRC = main.c

INCLUDE = -I. -I$(LIBFTPRINTF)
HDR = $(LIBFTPRINTF)/libftprintf.h

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

RM = rm
RMFLAGS = -f

VALGRIND = valgrind
VALGRINDFLAGS = --quiet --leak-check=full --show-leak-kinds=all

NORMINETTE = norminette
NORMINETTEFLAGS = -o

MAKE = make

all: $(NAME)

$(NAME):
	@$(MAKE) -C $(LIBFTPRINTF) all >/dev/null 2>&1
	@$(CC) $(CFLAGS) $(INCLUDE) $(SRC) $(LFLAGS) -o $@

clean:
	@$(RM) $(RMFLAGS) $(NAME)

fclean: clean
	@$(MAKE) -C $(LIBFTPRINTF) fclean

re: fclean all

check:
	@$(MAKE) -C $(LIBFTPRINTF) check

debug:
	@$(MAKE) -C $(LIBFTPRINTF) re DEBUG=1
	@$(MAKE) re DEBUG=1

fast:
	@$(MAKE) -C $(LIBFTPRINTF) re FAST=1
	@$(MAKE) re FAST=1

test: clean $(NAME)
	@./$(NAME) "%c" C || true
	@./$(NAME) "%s" "42 Lausanne" || true
	@./$(NAME) "%d" 42 || true

test2:
ifneq ($(DEBUG), 1)
	@$(MAKE) test DEBUG=1
endif

test3:
ifneq ($(FAST), 1)
	@$(MAKE) test FAST=1
endif

test4: clean $(NAME)
ifneq ($(DEBUG), 1)
	@$(MAKE) test4 DEBUG=1
else
	@$(VALGRIND) $(VALGRINDFLAGS) ./$(NAME) "%c" C || true
	@$(VALGRIND) $(VALGRINDFLAGS) ./$(NAME) "%s" "42 Lausanne" || true
	@$(VALGRIND) $(VALGRINDFLAGS) ./$(NAME) "%d" 42 || true
endif

update: config online pull

sync: config online pull
	@git status | grep -q 'nothing to commit' || (git add -A && git commit -am "$$(date '+%F %T')" && git push)

online:
	@dig +short vogsphere.baco.net A | grep -qF '213.5.156.25' || echo 'vogsphere.baco.net unresolvable'
	@nc -vzw3 vogsphere.baco.net 443 2>&1 | grep -qF 'succeeded' || echo 'vogsphere.baco.net unreachable'

pull:
	@git pull

config:
	@git config user.name "gbaconni"
	@git config user.email "gbaconni@student.42lausanne.ch"
	@git config pull.rebase false

