# Copyright (C) 2009 Christofer Jonsson
#
# This makefile is part of FNM.
#
# This makefile is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This makefile is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this makefile.  If not, see
# <http://www.gnu.org/licenses/>.
#

#=====================================================================
# application
#=====================================================================

APP = fnm

#=====================================================================
# paths
#=====================================================================

BIN = bin
OBJ = obj
SRC = src

#=====================================================================
# variables
#=====================================================================

FLAVOR ?= release

#=====================================================================
# gcc
#=====================================================================

CC     = gcc
CFLAGS = -Wall -Wextra -pedantic -Wno-variadic-macros

ifeq ($(FLAVOR),release)
  DEFINES += -DNDEBUG
endif

ifeq ($(FLAVOR),debug)
  CFLAGS  += -g
  DEFINES += -DDEBUG
endif

INCLUDES = $(addprefix -I,$(SRC))
SRCFILES = $(wildcard $(SRC)/*.c)
OBJFILES = $(addprefix $(OBJ)/,$(patsubst %.c,%.o,$(notdir $(SRCFILES))))

#=====================================================================
# commands
#=====================================================================	

ECHO  = echo
MKDIR = mkdir
RM    = rm -fr

#=====================================================================
# targets
#=====================================================================

# phony
.PHONY: clean all

# silent
.SILENT:

# clean
clean:
	$(RM) $(OBJ) $(BIN)

# all
all: $(OBJ) $(BIN) $(BIN)/$(APP)
	$(ECHO) "Build done"

# obj
$(OBJ):
	$(ECHO) "Creating $@"
	$(MKDIR) $(OBJ)

# bin
$(BIN):
	$(ECHO) "Creating $@"
	$(MKDIR) $(BIN)

# link
$(BIN)/$(APP): $(OBJFILES)
	$(ECHO) "Linking $@"
	$(CC) -o $@ $(OBJFILES)

# compile
$(OBJ)/%.o: $(SRC)/%.c
	$(ECHO) "Compiling $<" 
	$(CC) $(DEFINES) $(CFLAGS) $(INCLUDES) -c $< -o $@
