# =============================================================================
# This is the Makefile of EGlib
# Revision 2003-4-14
# 2003-11-18 - Update to automatic dependency generation
# =============================================================================

# =============================================================================
# Here we read some configurations, you should edit this file instead 
# of this makefile
include make.conf

# =============================================================================
# Path for each kind of file
# =============================================================================
SOURCE_DIR := . lib lib/EGlib lib/planar_boyer SAnjeeb_KArger
INCLUDE_DIR := . lib lib/EGlib lib/planar_boyer SAnjeeb_KArger
OBJ_DIR := obj
DEP_DIR := dep
LIB_DIR := lib

# =============================================================================
# Here we define the source files (not need to put the prefix) that will
# generate objective files. DO NOT INCLUDE files that don't generate objective
# files here, they will be automagically computed
# =============================================================================
SOURCE_FILE := eg_mempool.c graphdual.c eg_bellford.c \
							 eg_menger.c eg_pdp.c eg_util.c eg_ddomino.c eg_dpseparator.c \
							 bc_spanning.c bc_allocrus.c bc_safe_io.c eg_ddpconstraint.c \
							 eg_nettype.c eg_list.c eg_dgraph.c \
							 eg_heap.c eg_menger_app.c eg_dijkstra.c bc_util.c \
							 eg_dijkstra_app.c bc_zeit.c eg_equiset.c eg_ugraph.c \
							 eg_bit.c cookInterface.c graphEmbed.c \
							 graphIO.c graphIsolator.c graphNonplanar.c graphPreprocess.c \
							 graphStructure.c graphTests.c listcoll.c stack.c \
							 eg_1pchecker.c eg_emptyhandles.c eg_dptighten.c \
							 eg_bbtree.c eg_compare.c eg_greedykp.c \
							 eg_kppairs.c eg_kpseparator.c eg_2pchecker.c eg_2ptighten.c \
							 karger.c eg_greedysample.c

ifdef HAVE_CPLEX
ifneq ($(HAVE_CPLEX),0)
SOURCE_FILE +=	eg_cplex.c 
endif
endif

# =============================================================================
# Here we define extra include/dependency files to be passed to the
# documentation tool, note that the dependencies are computed automagically.
# This will only affect the behavior of the 'doc' target.
# =============================================================================
INCLUDE_FILE := $(SOURCE_FILE:.c=.h)	\
	eg_cplexTypes.h \
	eg_config.h \
	eg_listint.h \
	eg_macros.h eg_timer.h

# =============================================================================
# You shoudn't edit bellow this point. Other configuration is found in
# 'make.conf', see that file for more details.
# =============================================================================

# =============================================================================
# definitions of OS types
LINUX := 0
SUN := 1

# =============================================================================
# Compiler 
# CC = gcc
CC := gcc
CTAGS := ctags
AR := ar
#linker
# ranlib utility
RANLIB = ranlib
#architecture dependent configuration
ARCH = $(shell arch)
HOST = $(shell uname)
ifeq ($(ARCH),x86_64)
ARCH := k8
endif
ifeq ($(ARCH),sun4)
ARCH := v9
endif
ifeq ($(HOST),Linux)
OS := $(LINUX)
endif
ifeq ($(HOST),SunOS)
OS := $(SUN)
endif

# =============================================================================
# main configuration options
MAINOPT += -DDEBUG=$(DEBUG) -DOS=$(OS)
ifdef HAVE_CPLEX
ifneq ($(HAVE_CPLEX),0)
MAINOPT += -DHAVE_CPLEX
endif
endif

# =============================================================================
# Optimization Flags 
SW := -std=c99 -g3 -Wall -Winline -Wshadow -Wwrite-strings -Wpointer-arith \
      -Wundef -Wcast-qual -Wbad-function-cast -Wnested-externs \
      -Wstrict-prototypes -Wunused-function -Wunused-label -Wunused-variable \
      -Wunused-value -Wunused-parameter -Wfloat-equal -Wconversion \
			-Wsign-compare -Wwrite-strings 
ifdef OPTIMIZE
ifneq ($(OPTIMIZE),0)
SW += -O3 -fforce-addr -funroll-loops -frerun-cse-after-loop \
      -finline-limit=1000 -frerun-loop-opt -march=$(ARCH) -Wuninitialized \
      -falign-jumps=4 -falign-functions=4 -falign-labels=4 -falign-loops=4 \
      --param large-function-growth=1000 --param inline-unit-growth=1000 \
      --param max-inline-insns-single=1000
else
SW += -fno-inline
ifdef PROFILE
ifeq ($(PROFILE),0)
ifeq ($(DEBUG),0)
SW += -fomit-frame-pointer 
endif
endif
endif
endif
endif
ifdef PROFILE
ifneq ($(PROFILE),0)
SW += -pg -ftest-coverage -fprofile-arcs
ifdef BRANCHPF
ifneq ($(BRANCHPF),0)
SW += -fbranch-probabilities 
endif
endif
endif
endif
ifeq ($(HOST),SunOS)
SW += -m64
endif

# =============================================================================
# Libraries flags 
LIB_FLAGS := -lm -lpthread -lresolv -lnsl
ifeq ($(HOST),SunOS)
LIB_FLAGS += -lsocket
endif
ifdef HAVE_CPLEX
ifneq ($(HAVE_CPLEX),0)
LIB_FLAGS += -lcplex
LIB_PATH += $(CPLEXLIBRARY)
endif
endif

# objective files =============================================================
OBJ_FILE := $(addprefix $(OBJ_DIR)/,$(SOURCE_FILE:.c=.o))

# profile files ===============================================================
BRANCH_FILE := $(OBJ_FILE:.o=.bb) $(OBJ_FILE:.o=.bbg) 

# branch probability files ====================================================
PROB_FILE := $(OBJ_FILE:.o=.da)

# dependency files ====================================================
DEP_FILE := $(addprefix $(DEP_DIR)/,$(SOURCE_FILE:.c=.d))

#==============================================================================
# Default search paths for each type of file
#==============================================================================
vpath %.c $(SOURCE_DIR)
vpath %.h $(INCLUDE_DIR) $(CPLEXINCLUDE)
vpath %.o $(OBJ_DIR)
vpath %.d $(DEP_DIR)
vpath %.da $(OBJ_DIR)
vpath %.so $(LIB_DIR) $(LIB_PATH)

#==============================================================================
# Path rules for CC
#==============================================================================
CCFLAGS += $(patsubst %,-I%,$(subst :, ,$(INCLUDE_DIR) $(CPLEXINCLUDE)))
CCFLAGS += $(patsubst %,-L%,$(subst :, ,$(LIB_DIR) $(LIB_PATH) $(CPLEXLIBRARY)))

#==============================================================================
# Default targets to do
#==============================================================================
.PHONY: indent doc clean selftest default 
DEFAULT := selftest tags $(OBJ_FILE) twopie domino domino.a domboss domgrunt
default: $(DEFAULT) 
make.conf: ;
Makefile : ;

#==============================================================================
# Some extra dependencies
#==============================================================================
$(OBJ_FILE): make.conf

#==============================================================================
# Default rules for each type of file
#==============================================================================

# rule to make .o files
$(OBJ_FILE): $(OBJ_DIR)/%.o : $(DEP_DIR)/%.d
$(OBJ_FILE): $(OBJ_DIR)/%.o : %.c
	@echo Compiling $(patsubst %.o,%.c,$(notdir $@))
	@$(CC) $(SW) $(CCFLAGS) $(MAINOPT) -c $< -o $@ 

# rule to make .d files
$(DEP_FILE): $(DEP_DIR)/%.d : %.c
	@echo Making Dependencies for $(patsubst %.d,%.c,$(notdir $@)) 
	@$(CC) -MM -MF $@ -MT tags -MT $@ -MT $(OBJ_DIR)/$(patsubst %.d,%.o,$(notdir $@))\
	$(SW) $(CCFLAGS) $(MAINOPT) $<

tags: $(TEMPLATE_SFILE) $(TEMPLATE_SFILE:.c=.h) $(TEMPLATE_MAIN_SFILE)
	@echo Building vi $@
	@$(CTAGS) $^

# check some stuff in here
selftest: 
ifndef OS
	@echo "Unknown OS system, this may cause errors at compile time"
endif
ifeq ($(PROFILE),0)
	@rm -f $(PROB_FILE) $(BRANCH_FILE)
endif
ifeq ($(BRANCHPF),0)
	@rm -f $(PROB_FILE) 
endif
ifndef PROFILE
	@rm -f $(PROB_FILE) $(BRANCH_FILE)
endif
ifndef BRANCHPF
	@rm -f $(PROB_FILE) 
endif

# note that if the .da file does not exist, we must take care of it declaring it
# phony 
ifdef PROFILE
ifneq ($(PROFILE),0)
ifdef BRANCHPF
ifneq ($(BRANCHPF),0)
$(DEP_DIR)/make.phony: FORCE
	@echo "Creating PHONY targets" 
	@echo "" > $(DEP_DIR)/make.phony
	@for i in $(PROB_FILE); do \
		if [ -f $$i ]; then \
			echo "$$i: \$$(OBJ_DIR)/%.da : %.c ; rm -f $$i" >>  $(DEP_DIR)/make.phony ; \
		else \
			echo ".PHONY : $$i" >> $(DEP_DIR)/make.phony && \
			echo "$$i: ; " >> $(DEP_DIR)/make.phony ;\
		fi ; \
	done 
include $(DEP_DIR)/make.phony
$(OBJ_FILE): %.o : %.da
endif
endif
endif
endif

# rule to force an action
FORCE:

#==============================================================================
# include files for dependencies
#==============================================================================
include $(DEP_FILE)

#==============================================================================
# Targets to be done
#==============================================================================

doc: FORCE
	@echo "Generating Documentation..." && doxygen

domino.a: $(OBJ_FILE)
	@echo Linking $@
	@rm -f $@
	@$(AR) cq $@ $(OBJ_FILE)
	@if ( test -f $(RANLIB) -o -f /usr/bin/ranlib -o \
		-f /bin/ranlib -o -f /usr/ccs/bin/ranlib ) ; then \
		echo $(RANLIB) $@ ; \
		$(RANLIB) $@ ; \
	fi

clean:
	-rm -f *.lp *.log *.a *.so test_* doc/*/* *.{bb,bbg,da} graph.*.x gmon.*
	-rm -f $(OBJ_FILE) $(BRANCH_FILE) $(PROB_FILE) $(DEFAULT) $(DEP_FILE) 

IDENTFLAGS := -bli0 -i2 -l80 -lc80 -lps -sc -cdw -bfda -psl -nbc -nbc -npsl \
							-ut -ts2 -bc
indent:
	-@for j in $(SOURCE_DIR); do for i in $$j/*.c; do indent $$i -o _ind_ $(IDENTFLAGS) && mv _ind_ $$i && echo "Indenting $$i"; done ; done
	-@for j in $(INCLUDE_DIR); do for i in $$j/*.h; do indent $$i -o _ind_ $(IDENTFLAGS) && mv _ind_ $$i && echo "Indenting $$i"; done; done

test: main/main_planarize.c $(OBJ_FILE)
	@echo Compiling $@
	@$(CC) $(SW) $(INCLUDEDIR) $(MAINOPT) $(OBJ_FILE) $(CCFLAGS) $(LIB_FLAGS) $< -o $@ 

twopie: main_2p.c $(OBJ_FILE)
	@echo Compiling $@
	@$(CC) $(SW) $(INCLUDEDIR) $(MAINOPT) $(OBJ_FILE) $(CCFLAGS) $(LIB_FLAGS) $< -o $@ 

domino: main.c $(OBJ_FILE)
	@echo Compiling $@
	@$(CC) $(SW) $(INCLUDEDIR) $(MAINOPT) $(OBJ_FILE) $(CCFLAGS) $(LIB_FLAGS) $< -o $@ 

domboss: bc_domboss.c $(OBJ_FILE)
	@echo Compiling $@
	@$(CC) $(SW) $(INCLUDEDIR) $(MAINOPT) $(OBJ_FILE) $(CCFLAGS) $(LIB_FLAGS) $< -o $@ 

domgrunt: bc_domgrunt.c $(OBJ_FILE)
	@echo Compiling $@
	@$(CC) $(SW) $(INCLUDEDIR) $(MAINOPT) $(OBJ_FILE) $(CCFLAGS) $(LIB_FLAGS) $< -o $@ 

# =============================================================================
# end of Makefile
# =============================================================================
