# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    Makefile                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: gbaconni@student.42lausanne.ch             +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2022/01/18 15:11:16 by gbaconni          #+#    #+#              #
#    Updated: 2022/04/13 01:20:20 by gbaconni         ###   lausanne.ch        #
#                                                                              #
# **************************************************************************** #
#
# 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_itoa.c \
	  ft_vprintf_char.c \
	  ft_vprintf_string.c \
	  ft_vprintf_pointer.c \
	  ft_vprintf_decimal.c \
	  ft_vprintf_hexadecimal.c \
	  ft_vprintf_percent.c \
	  ft_vprintf_other.c \
	  ft_vprintf.c \
	  ft_printf.c

SRC_BONUS =

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 += -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

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)
	$(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_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

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

