#-----------------------------------------------------------------------

#  Makefile for mootools

VERSION = 1.0svn$(SVN_REV)

#-----------------------------------------------------------------------

#                        Copyright (c) 2007, 2008, 2009, 2010
#           Manuel Lopez-Ibanez  <manuel.lopez-ibanez@manchester.ac.uk>

#  This program is free software (software libre); 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 2 of the
#  License, or (at your option) any later version.

#  This program 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 program; if not, you can obtain a copy of the GNU
#  General Public License at:
#                  http://www.gnu.org/copyleft/gpl.html
#  or by writing to:
#            Free Software Foundation, Inc., 59 Temple Place,
#                  Suite 330, Boston, MA 02111-1307 USA
#
#-----------------------------------------------------------------------
# IMPORTANT NOTE: Please be aware that the fact that this program is
# released as Free Software does not excuse you from scientific
# propriety, which obligates you to give appropriate credit! If you
# write a scientific paper describing research that made substantive
# use of this program, it is your obligation as a scientist to
# acknowledge its use.  Moreover, as a personal note, I would
# appreciate it if you would email manuel.lopez-ibanez@manchester.ac.uk with
# citations of papers referencing this work so I can mention them to
# my funding agent and tenure committee.
#-----------------------------------------------------------------------
DEBUG ?=1

BINDIR :=.

## Define source files
SRCS  = igd.c epsilon.c dominatedsets.c nondominated.c io.c ndsort.c hv_contrib.c hv.c pareto.c
HEADERS = io.h io_priv.h common.h gcc_attribs.h igd.h epsilon.h nondominated.h hv.h cmdline.h
OBJS  = $(SRCS:.c=.o)
DIST_OTHER_FILES = Makefile README LICENSE svn_version Makefile.libhv

EXE_LDFLAGS=-lm

DELETE = @rm -f
ECHO = @echo "$(1)"

ifdef march
MARCH=$(march)
endif

## Do we have svnversion?
ifeq ($(shell sh -c 'which svnversion 1> /dev/null 2>&1 && echo y'),y)
  ## Is this a working copy?
  ifeq ($(shell sh -c 'LC_ALL=C svnversion -n . | grep -q ^[0-9] && echo y'),y)
    $(shell sh -c 'svnversion -n . > svn_version')
  endif
endif
## Set version information:
SVN_REV = $(shell sh -c 'cat svn_version 2> /dev/null')

OPT_CFLAGS = -O3
ifneq ($(DEBUG),0)
CDEBUG = -g3
OPT_CFLAGS = -O0 -fsanitize=undefined -fsanitize=address
endif

WARN_CFLAGS=-Wall -Wextra

gcc-guess-march = $(strip $(shell $(CC) $(CFLAGS) -march=$(MARCH) -x c -S -\#\#\# - < /dev/null 2>&1  | \
                grep -m 1 -o -e "march=[^'\"]*" | \
                sed 's,march=,,'))

ifneq ($(MARCH),none)
  ifndef MARCH
    MARCH = native
  endif
  ARCH := $(gcc-guess-march)
  OPT_CFLAGS += -march=$(ARCH)
endif

EXE_CFLAGS += $(OPT_CFLAGS) -std=gnu99 $(WARNFLAGS) \
	-D_GNU_SOURCE -DDEBUG=$(DEBUG) $(CDEBUG) \
	-DMARCH='"$(gcc-guess-march)"' -DVERSION=\"$(VERSION)\"

igd = $(BINDIR)/igd$(EXE)
epsilon = $(BINDIR)/epsilon$(EXE)
dominatedsets = $(BINDIR)/dominatedsets$(EXE)
nondominated = $(BINDIR)/nondominated$(EXE)
ndsort = $(BINDIR)/ndsort$(EXE)

.PHONY : all clean distclean dist default 

default : $(igd) $(epsilon) $(dominatedsets) $(nondominated) $(ndsort)

$(igd) : OBJS=igd.o io.o
$(igd) : igd.o io.o $(HEADERS)

$(epsilon) : OBJS=epsilon.o io.o
$(epsilon) : epsilon.o io.o $(HEADERS)

$(dominatedsets) : OBJS=dominatedsets.o io.o
$(dominatedsets) : dominatedsets.o io.o $(HEADERS)


$(nondominated) : OBJS=nondominated.o io.o
$(nondominated) : nondominated.o io.o $(HEADERS)

include Makefile.libhv

$(ndsort) : OBJS=ndsort.o pareto.o io.o hv_contrib.c $(HV_OBJS)
$(ndsort) : $(OBJS) $(HEADERS)

$(igd) $(epsilon) $(nondominated) $(dominatedsets) $(ndsort):
	@mkdir -p $(BINDIR)
	$(call ECHO,--> Building $@ version $(VERSION) <---)
	$(CC) -o $@  $(OBJS) $(EXE_CFLAGS) $(CFLAGS) $(EXE_LDFLAGS)

$(OBJS) : $(HEADERS)


clean :
	$(call ECHO,---> Removing $(igd) <---)
	$(DELETE) $(igd)

	$(call ECHO,---> Removing $(epsilon) <---)
	$(DELETE) $(epsilon)

	$(call ECHO,---> Removing $(dominatedsets) <---)
	$(DELETE) $(dominatedsets)

	$(call ECHO,---> Removing $(nondominated) <---)
	$(DELETE) $(nondominated)

	$(call ECHO,---> Removing $(ndsort) <---)
	$(DELETE) $(ndsort)

	$(call ECHO,---> Removing object files: $(OBJS) <---)
	$(DELETE) $(OBJS) $(HV_OBJS)

all :  clean default

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

DIST_SRC_FILES =  $(DIST_OTHER_FILES) $(OBJS:.o=.c) $(HEADERS) 
DIST_SRC := mo-tools-$(VERSION)-src
dist : DEBUG=0
dist : CDEBUG=
dist : all
	@(mkdir -p ../$(DIST_SRC) \
	&& rsync -rlpC --exclude=.svn $(DIST_SRC_FILES) ../$(DIST_SRC)/ \
	&& cd .. \
	&& tar cf - $(DIST_SRC) | gzip -f9 > $(DIST_SRC).tar.gz \
	&& rm -rf ./$(DIST_SRC)/* && rmdir ./$(DIST_SRC)/ \
	&& echo "$(DIST_SRC).tar.gz created." && cd $(PWD) )

TAGS: *.c *.h
	etags *.c *.h
