# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    Makefile                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: gbaconni@student.42lausanne.ch             +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2022/01/18 15:11:16 by gbaconni          #+#    #+#              #
#    Updated: 2022/04/29 14:18:53 by gbaconni         ###   ########.fr        #
#                                                                              #
# **************************************************************************** #
#
# make libftprintf.a
# make all
# make clean
# make fclean
# make re
# make
#

NAME = libftprintf.a

LIB = libftprintf.so
LIBFT = libft
LFLAGS = -L.

SRC = \
	  $(LIBFT)/ft_memset.c \
	  $(LIBFT)/ft_bzero.c \
	  $(LIBFT)/ft_calloc.c \
	  $(LIBFT)/ft_strlen.c \
	  $(LIBFT)/ft_strchr.c \
	  $(LIBFT)/ft_isdigit.c \
	  $(LIBFT)/ft_itoa.c \
	  ft_eoflags.c \
	  ft_strrev.c \
	  ft_ltoa_base.c \
	  ft_putchar.c \
	  ft_puts.c \
	  ft_va_char.c \
	  ft_va_string.c \
	  ft_va_pointer.c \
	  ft_va_decimal.c \
	  ft_va_hexadecimal.c \
	  ft_va_percent.c \
	  ft_va_escape.c \
	  ft_va_other.c \
	  ft_vprintf.c \
	  ft_printf.c

SRC_BONUS = \
			ft_va_flags_bonus.c \
			ft_va_precision_bonus.c \
			ft_va_string_bonus.c \
			ft_va_decimal_bonus.c \
			ft_va_hexadecimal_bonus.c

OBJ = ${SRC:.c=.o}
OBJ_BONUS = ${SRC_BONUS:.c=.o}

INCLUDE = -I.
HDR = libftprintf.h

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

VALGRIND = valgrind
VALGRINDFLAGS = --quiet --leak-check=full --show-leak-kinds=all
ifeq ($(LEAK), 1)
	CFLAGS += -g -O0
	PREFIX += $(VALGRIND) $(VALGRINDFLAGS)
endif

AR = ar
ARFLAGS = -rcs

RM = rm
RMFLAGS = -vf

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

NORMINETTE = norminette
NORMINETTEFLAGS = -o

MAKE = make

all: $(NAME)

$(NAME): $(OBJ) $(OBJ_BONUS)
	$(AR) $(ARFLAGS) $@ $^

%.o: %.c
	$(CC) $(CFLAGS) $(INCLUDE) $< -c -o $@

clean:
	$(RM) $(RMFLAGS) $(OBJ) $(OBJ_BONUS)
	@$(RM) $(RMFLAGS) $(LIB) || true

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

re: fclean all

bonus: $(OBJ) $(OBJ_BONUS)
	$(AR) $(ARFLAGS) $(NAME) $(OBJ_BONUS)

rebonus: fclean bonus

so: $(OBJ) $(OBJ_BONUS)
	@$(CC) $(CFLAGS) -shared -fPIC -o $(LIB) $(OBJ) $(OBJ_BONUS)

check:
	@$(NORMINETTE) $(NORMINETTEFLAGS) $(HDR) $(SRC) ${SRC_BONUS} 2>&1 | grep -v '\.[ch]: OK!' || true

leak:
	@$(MAKE) re LEAK=1

debug:
	@$(MAKE) re DEBUG=1

fast:
	@$(MAKE) re FAST=1

func:
	@echo "external functions:"
	@nm -o *.o | grep ' U _' | grep -v ' U _ft' || true
	@echo ""
	@echo "allowed functions:"
	@nm -o *.o | grep ' U _' | grep -e malloc -e free -e write -e va_start -e va_arg -e va_copy -e va_end || true
	@echo ""
	@echo "forbidden functions:"
	@nm -o *.o | grep ' U _' | grep -v ' U _ft' | grep -v -e malloc -e free -e write -e va_start -e va_arg -e va_copy -e va_end || true
	@echo ""

ft:
	@echo "objects:"
	@ar -t $(NAME) | grep '\.o$$'
	@echo ""
	@echo "functions:"
	@nm $(NAME) | grep T | grep -o 'ft_.*' | sort -u

watch:
	@read -p "cmd: " cmd; while :; do clear; date "+%F %T (every 2.0s)"; echo; sh -c "$${cmd} 2>&1" | tail -n 20 || true; sleep 2; done

update: sync

sync: online pull
	@git ls-files --others --exclude-standard | xargs diff -Naur '' | less -F
	@git status | grep -q 'nothing to commit' || (git diff; read -p "Comment: " comment; git add -A; git commit -am "$${comment-$$(date '+%F %T')}"; git push)

online:
	@dig +short vogsphere.42lausanne.ch A | grep -qE '(185\.25\.195\.180|10\.51\.1\.5)' || echo 'vogsphere.42lausanne.ch unresolvable'
	@nc -vzw3 vogsphere.42lausanne.ch 443 >/dev/null 2>&1 || echo 'vogsphere.42lausanne.ch unreachable'

pull: config
	@git pull

status:
	@git status

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

