# 'make depend' uses makedepend to automatically generate dependencies 
#               (dependencies are added to end of Makefile)
# 'make'        build executable file 'mycc'
# 'make clean'  removes all .o and executable files
#

#Linux, gcc
CC = gcc
CXX = g++

### a MiniSat2-compatible solver is required.  Default is Minisat2.
  # choose one of the alternative solvers if you prefer.
  # MINISAT2 *only* specifies if the minisat2(...) builtins are exposed -
  # they are implemented by the chosen solver (by MiniSat2 if nothing else
  # chosen)
MINISAT2 = 1 #comment out to exclude minisat2 builtins.
#GLUEMINISAT = 1 #use GlueMiniSat 2.2.5 *instead* of MiniSat2
#GLUCOSE = 1 #use Glucose 2.2 *instead* of MiniSat2
GLUCOSE3 = 1 #use Glucose 3.0 *instead* of MiniSat2
#EXTERNALSAT = 1 #use solver.sh *instead* of MiniSat2
#EXTSAT_KEEPDIMACS = 1 #don't delete the temporary dimacs files for EXTERNALSAT

#FDIST=1 #formula distance

####### start of reduction-finding options #######
####### note that the choice of MiniSat2-compatible solver affects
####### reduction-finding

REDFIND_DEBUG = 1#comment out to exclude redfind progress output
#REDFIND_DEBUG2 = 1#comment out to exclude extra redfind debugging output

### Reduction-candidate finding can also be done with BDDs using CUDD.
  # Default is to use the MiniSat2 compatible solver chosen above.
  # Counter-example finding is always done with SAT.
#REDFIND_CUDD = 1# comment out to use SAT-based redfind(...), otherwise
	        # use CUDD. 

#REDFIND_OLDRANGE=1 # when looking at a range of sizes, never reset to n1
#REDFIND_MINEX= 1  # comment out to disable greedily-minimizing examples
#REDFIND_MAXEX= 1  # comment out to disable greedily-maximizing examples
#REDFIND_ALTEX= 1  # comment out to disable alternating minimizing,maximizing
		   # examples
#REDFIND_MINRED= 1 # comment out to disable greedily-minimizing hypotheses
		  # REDFIND_MINRED has no effect if REDFIND_CUDD is set
#REDFIND_IGNTF= 1  # when minimizing hypotheses, it's okay to add \f to
		  # clauses (try to remove clauses without lengthening others)
#REDFIND_IGNALL= 1 # not recommended, too slow.
		  # different way to minimize hypotheses, okay to lengthen
		  # clauses.  start by forbidding succ, then others from
		  # one clause at a time. leave only 'essential'
#REDFIND_RANDEX=20 # when getting counter-examples, choose one of this many 
		  # almost-randomly. comment out to disable
  # enabling REDFIND_MINEX and REDFIND_RANDEX chooses the random example first,
  # then minimizes it -- not a random greedily-minimized example

### Sizes for hashtables used in reduction-finding.
  # increase one/both of these sizes (probably RED_MAXVARS) if you get 
  # "Assertion failed: (hash->nodecount < hash->maxcount)"
  # RED_MAXVARS is large because we use entries for each
  # bit of output structures, which tend to be large in the parameters
RED_EXMAXVARS = 16394 #hashtable size for counter-example finding
RED_MAXVARS = 65536 #hashtable size for reduction-candidate finding

REDFIND_SUCC = 1 # enables successor (x=y+1) literals in reduction-finding
REDFIND_FEWVARS = 1 #disables literals "x=i"/"x=max" in reduction-finding
REDFIND_FV_0max = 1 #if REDFIND_FEWVARS is also set, re-enables literals "x=0" 
		    #and "x=max" (otherwise no effect).

####### end of reduction-finding options #######

####### options for translating to qcir/etc ######
TR_MAXIDS = 4194304 #hashtable (max gates) size for translating
####### end of translation options ######

WCFLAGS = -Wextra -pedantic

#OCFLAGS = -O3 -fomit-frame-pointer #-g #-pg#-s 
#OCFLAGS = -O3 -g
OCFLAGS = -O3 #-DDEDEBUG #-DTR_DEBUG2

OCFLAGS += -DRED_MAXVARS=$(RED_MAXVARS) -DRED_EXMAXVARS=$(RED_EXMAXVARS) -DTR_MAXIDS=${TR_MAXIDS} #-DQCIRHACK

ifdef REDFIND_SUCC
OCFLAGS += -DREDFIND_SUCC
endif

ifdef EXTSAT_KEEPDIMACS
OCFLAGS += -DEXTSAT_KEEPDIMACS
endif
ifdef REDFIND_DEBUG
OCFLAGS += -DREDFIND_DEBUG
endif
ifdef REDFIND_DEBUG2
OCFLAGS += -DREDFIND_DEBUG2
endif
ifdef REDFIND_FEWVARS
OCFLAGS += -DREDFIND_FEWVARS
endif
ifdef REDFIND_FV_0max
OCFLAGS += -DREDFIND_FV_0max
endif
ifdef FDIST
OCFLAGS += -DFDISTANCE
endif
ifdef REDFIND_OLDRANGE
OCFLAGS += -DREDFIND_OLDRANGE
endif

ifdef REDFIND_MINEX
OCFLAGS += -DRF_minex=1 -DREDFIND_EXIMPROVE
else
ifdef REDFIND_MAXEX
OCFLAGS += -DRF_maxex=1 -DREDFIND_EXIMPROVE
else
ifdef REDFIND_ALTEX
OCFLAGS += -DRF_altex=1 -DREDFIND_EXIMPROVE
endif
endif
endif

ifdef REDFIND_MINRED
OCFLAGS += -DREDFIND_MINRED -DREDFIND_REDIMPROVE
endif

ifdef REDFIND_IGNALL
OCFLAGS += -DRED_IGNALL
else
ifdef REDFIND_IGNTF
OCFLAGS += -DRED_IGNTF
endif
endif
ifdef REDFIND_RANDEX
OCFLAGS += -DREDFIND_RANDEX=$(REDFIND_RANDEX)
endif

# byacc and flex's output doesn't look good with -Wall -ansi -pedantic :P
	CFL = ${OCFLAGS}
ifdef MINISAT2
	CFL += -DMINISAT2
endif

CFLAGS = ${CFL} ${WCFLAGS}  -Wall 

INCLUDES = -Iinclude  -I.

#LFLAGS = -L../lib

ifdef GLUEMINISAT
LIBS = -L./glueminisat/build/release/lib -lminisat-c -L./glueminisat/minisat/build/release/lib/ -lglueminisat -lm
else
ifdef GLUCOSE
LIBS = -L./glucose2.2/build/release/lib -lminisat-c -L./glucose2.2/minisat/build/release/lib/ -lglucose -lm
else
ifdef GLUCOSE3
LIBS = -L./glucose3/build/release/lib -lminisat-c -L./glucose3/minisat/build/release/lib -lglucose -lm
else
ifdef EXTERNALSAT
else
LIBS = -L./minisat2/build/release/lib -lminisat-c -L./minisat2/minisat/build/release/lib/ -lminisat -lm #pow
endif
endif
endif
endif

ifdef REDFIND_CUDD
        REDFIND_SOURCE = redfind/redfind_bdd.c
	INCLUDES += -Icudd/include
	LIBS += -Lcudd/cudd -lcudd -Lcudd/mtr -lmtr -Lcudd/util -lutil \
		-Lcudd/epd -lepd -Lcudd/st -lst
	OCFLAGS += -DREDFIND_CUDD
	CUDD = ./cudd/cudd/libcudd.a
else
        REDFIND_SOURCE = redfind/redfind.c
endif

ifdef FDIST
	INCLUDES += -Icudd/include
	LIBS += -Lcudd/cudd -lcudd -Lcudd/mtr -lmtr -Lcudd/util -lutil \
                -Lcudd/epd -lepd -Lcudd/st -lst
	CUDD = ./cudd/cudd/libcudd.a
	FDIST_SOURCE = fd/fd.c
endif

# define the C source files
SRCS = y.tab.c lex.yy.c minisat/solver.c reduc/reduc.c cmd/cmd.c file/file.c hash/hash.c init/init.c parse/parse.c env/env.c help/help.c logic/eval.c test/main.c logic/interp.c logic/relation.c util/util.c logic/constant.c logic/tuple.c mace/usemace.c limboole/limboole.c redfind/getex.c ${REDFIND_SOURCE} qbf/trans.c qbf/convert.c logic/check.c ${FDIST_SOURCE}

ifndef GLUEMINISAT
ifndef GLUCOSE
ifndef GLUCOSE3
ifdef EXTERNALSAT
SRCS += externalsat/externalsat.c
INCLUDES += -Iexternalsat
endif
endif
endif
endif

OBJS = $(SRCS:.c=.o)

# define the executable file 
MAIN = de 

ifdef GLUEMINISAT
SATBIND = ./glueminisat/build/release/lib/libminisat-c.a
SAT = ./glueminisat/minisat/build/release/lib/libglueminisat.a
else
ifdef GLUCOSE
SATBIND = ./glucose2.2/build/release/lib/libminisat-c.a
SAT = ./glucose2.2/minisat/build/release/lib/libglucose.a
else
ifdef GLUCOSE3
SATBIND = ./glucose3/build/release/lib/libminisat-c.a
SAT = ./glucose3/minisat/build/release/lib/libglucose.a
else
ifdef EXTERNALSAT
SATBIND =
SAT =
else
SATBIND = ./minisat2/build/release/lib/libminisat-c.a
SAT = ./minisat2/minisat/build/release/lib/libminisat.a
endif
endif
endif
endif

.PHONY: depend clean

all:	${CUDD}  $(MAIN)

./glueminisat/build/release/lib/libminisat-c.a:
	cd glueminisat; ${MAKE} static; cd ..

./glucose2.2/build/release/lib/libminisat-c.a:
	cd glucose2.2; ${MAKE} static; cd ..

./glucose3/build/release/lib/libminisat-c.a:
	cd glucose3; ${MAKE} static; cd ..

./minisat2/build/release/lib/libminisat-c.a:   
	cd minisat2; ${MAKE} static; cd ..

./minisat2/minisat/build/release/lib/libminisat.a:
	cd minisat2/minisat; ${MAKE} lr; cd ../..

./glueminisat/minisat/build/release/lib/libglueminisat.a:
	cd glueminisat/minisat; ${MAKE} lr; cd ../..

./glucose2.2/minisat/build/release/lib/libglucose.a:
	cd glucose2.2/minisat; ${MAKE} lr; cd ../..

./glucose3/minisat/build/release/lib/libglucose.a:
	cd glucose3/minisat; ${MAKE} lr; cd ../..

./cudd/cudd/libcudd.a:
	cd cudd; CC=${CC} CXX=${CXX} ${MAKE}; cd ..

de:   $(CUDD) $(SAT) $(SATBIND) $(OBJS)
	$(CXX) $(CFLAGS) $(INCLUDES) -o $(MAIN) ${OBJS} $(LFLAGS) $(LIBS) 

.cpp.o:
	${CXX} ${CFLAGS} $(INCLUDES) -o ${<:.cpp=.o} -c $<

y.tab.c:
	yacc -d soe.y

y.tab.h:
	yacc -d soe.y

lex.yy.c: y.tab.h
	lex soe.l

y.tab.o: y.tab.c
	${CC} ${CFL} ${INCLUDES} -c -o y.tab.o y.tab.c

lex.yy.o: lex.yy.c
	${CC} ${CFL} ${INCLUDES} -c -o lex.yy.o lex.yy.c

.c.o:
	$(CC) $(CFLAGS) $(INCLUDES) -o ${<:.c=.o} -c $<

clean:
	$(RM) -r ${OBJS} $(MAIN) ./redfind/redfind*.o ./minisat2/build ./minisat2/minisat/build ./glueminisat/build ./glueminisat/minisat/build ./glucose2.2/build ./glucose2.2/minisat/build ./glucose3/build ./glucose3/minisat/build
	rm -f fd/fd.o externalsat/externalsat.o
	cd cudd; ${MAKE} distclean; cd ..
	cd glueminisat/minisat; ./clean.sh; cd ../..

depend: $(SRCS)
	makedepend $(INCLUDES) $^

# DO NOT DELETE THIS LINE -- make depend needs it
