##############################################################################
#
# Generic Makefile. Only need to modify the variables for src, obj,
# and bin directories, and the name of the executable.
#
# $Author: crono $
# $Date: 2006/03/27 21:07:03 $
# $Revision: 1.6 $
#
##############################################################################



########################### Directories and Target ###########################
# Source directory:
SRC_DIR = .

# Object directory:
OBJ_DIR = ./obj

# Executable directory:
BIN_DIR = ../../bin

# Name of the executable:
BIN_NAME = common



######################## Compiler and Linker Options #########################
# Compiler:
CC = gcc

# Linker:
LD = ar

# Preprocessor flags:
DFLAGS = 

# Compiler flags:
CFLAGS = -Wall -pedantic -std=gnu99 -O2

# Linker flags:
LDFLAGS = rcs



############################ Other Programs Used #############################
# Dependency generator:
MDEPEND = $(CC) -M

# Make Dir command:
MKDIR = /bin/mkdir -p

# Clean-up command:
RM = /bin/rm -f



######################### Automatic Object Variables #########################
# The list of source files:
SRCS = $(wildcard $(SRC_DIR)/*.c)

# Generated object files:
OBJS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS))
OBDS = $(patsubst $(SRC_DIR)/%.c,%.o,$(SRCS))

# Look for .o files in obj dir:
vpath %.o $(OBJ_DIR)

# Program file:
PROG = $(BIN_DIR)/lib$(BIN_NAME).a



################################### Rules ####################################
# Top-level rule: compile everything
all: $(PROG)

# The program link rule:
$(PROG): $(OBDS) $(BIN_DIR)
	$(LD) $(LDFLAGS) $(PROG) $(OBJS)

# Meta rule for compiling ".c" files
%.o: $(SRC_DIR)/%.c $(OBJ_DIR)
	$(CC) $(CFLAGS) $(DFLAGS) -c -o $(OBJ_DIR)/$@ $<

# Rules for obj and bin dirs:
$(OBJ_DIR):
	$(MKDIR) $(OBJ_DIR)
$(BIN_DIR):
	$(MKDIR) $(BIN_DIR)

# Rule for cleaning up before a recompile:
.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJS) .depend

# Rule for creating dependency lists and writing them into a dependency file:
.depend: $(SRCS)
	$(MDEPEND) $(SRCS) > .depend

#Include dependency list:
include .depend
