#------------------------------------------#
# Makefile for the PdTRAV distribution kit #*
# Version 3.1                              #
#------------------------------------------#

# Commands Available:
# make
#    all (the default)
#    it makes all: compiles the external libraries, creates the pdtrav library, makes pdtrav
# make clean
#    it cleans pdtrav
# make distclean
#    it cleans pdtrav and all other packages

#------------#
# For safety #
#------------#

SHELL = /bin/sh
.SUFFIXES:

#----------------------------------------#
# Configuration Section                  #
# uncomment the desired options/sections #
#----------------------------------------#

.PHONY : default static

default: LDFLAGS =
static : LDFLAGS = -static

default: all
static : all

ifdef (PDT32)
DEFINE_PDT32 = PDT32=yes
else
DEFINE_PDT32 =
endif

# Memory libraries
#MNEMLIB	=
# MNEMLIB	= /usr/lib/libdmalloc.a

# ReadLine Library
#READLINEFLAGS =
#READLINEFLAGS = -DHAVE_LIBREADLINE -DHAVE_READLINE_READLINE_H -DHAVE_READLINE_HISTORY_H
#PDTRAVLIBS += -lreadline
#READLINEFLAGS = -DHAVE_LIBREADLINE -DHAVE_READLINE_READLINE_H
#PDTRAVLIBS += -lreadline

# Level of Self-Checking and Verbosity of the CUDD Package

#DDEBUG =
#DDDEBUG = -DDD_DEBUG -DDD_VERBOSE -DDD_STATS -DDD_CACHE_PROFILE -DDD_UNIQUE_PROFILE -DDD_COUNT -DDDDMP_DEBUG 
#DDDEBUG = -DDDDMP_DEBUG

# Specify compilation flags (ICFLAGS are for CUDD only!)
CC =		g++
CUDD_CC =	gcc
#CUDD_CC =	g++
#GCC =		gcc

ifdef PDT32
# StQ: 32-bit (2 rows) OK x cudd-2.4.1 OK ANCHE PER cudd-2.4.2 ma compilato con gcc e non con g++
CUDD_ICFLAGS = 	-O6 -m32 -Wall -pthread 
# TOLTE O MESSE SEMBRA NON FARE DIFFERENZA -Wall -fPIC  -pthread 
CUDD_XCFLAGS =	-mtune=native -fPIC -DHAVE_IEEE_754 -DBSD
# TOLTE O MESSE SEMBRA NON FACCIA DIFFERENZA --> -DSTDC_HEADERS -DHAVE_STRSTR 
#XC_FLAGS = 	-pthread -m32 -mtune=native -DHAVE_IEEE_754 -DBSD
XC_FLAGS = 	
# StQ: 32-bit
#CFLAGS =	-Wall -g3 -m32 -pthread -static
CFLAGS =	-DLIN -g -m32 -pthread
else
# StQ: 64-bit (2 rows)
CUDD_ICFLAGS = 	-O6 -Wall -fPIC -pthread
CUDD_XCFLAGS =	-ffloat-store -mtune=native -malign-double -DHAVE_IEEE_754 -DBSD -DSIZEOF_VOID_P=8 -DSIZEOF_LONG=8 -DSIZEOF_INT=4 
#ICFLAGS = 	-Wall -O6 -fPIC -pthread -mtune=native -malign-double -DSIZEOF_VOID_P=8 -DSIZEOF_LONG=8
XC_FLAGS =	-DHAVE_IEEE_754=1 -DBSD=1 -DSTDC_HEADERS=1 -DHAVE_STRSTR=1 
CFLAGS =	-DLIN64 -g -Wall -fPIC -pthread
endif
LINKER =	g++
#LDFLAGS =	-pthread -static --enable-static --disable-shared
#LDFLAGS =	-pthread -static -ltcmalloc
#LDFLAGS =	-pthread -static
#LDFLAGS =	-pthread 
#LDFLAGS =	-static

CFLAGS += 	$(CUDD_XCFLAGS)
OTHERLIBS =	-lreadline -lncurses -ltermcap -ldl -lrt -lm
#OTHERLIBS =	-lreadline -ldl -lrt -lm
YACC =		bison -y
LEX =		flex
NAWK =		gawk
AR =		ar
RANLIB =	ranlib
RM =		rm -f

ALLCFLAGS = $(CFLAGS) $(XC_FLAGS)

#-------------#
# Directories #
#-------------#

pdtrav_dir 	= .
PDTRAV_DIR 	= $(shell cd $(pdtrav_dir);pwd)

fbv_dir 	= ./src/fbv
FBV_DIR 	= $(shell cd $(fbv_dir);pwd)

cudd_dir	= ../cudd-2.5.0
CUDD_DIR 	= $(shell cd $(cudd_dir);pwd)

minisat_dir	= ../minisat-p_v1.14
MINISAT_DIR 	= $(shell cd $(minisat_dir);pwd)

manysat_dir	= ../manysat-2.0/coreDir
MANYSAT_DIR 	= $(shell cd $(manysat_dir);pwd)
MANYSATFLAGS 	= -lz -fopenmp

abc_dir		= ../abc
ABC_DIR 	= $(shell cd $(abc_dir);pwd)

aiger_dir	= ../aiger-1.9.3
AIGER_DIR 	= $(shell cd $(aiger_dir);pwd)

# Directory where object files will be placed during the build
objectdir 	= $(PDTRAV_DIR)/obj

# Directory where libraries will be placed during the build
libdir 		= $(PDTRAV_DIR)/lib

# Directory where executable files will be placed during the build
bindir 		= $(PDTRAV_DIR)/bin

# Directory where header files will be placed during the build
includedir 	= $(PDTRAV_DIR)/include

#-------------------------#
# Library paths and names #
#-------------------------#

PDTLIB = $(libdir)/pdtlib.a

MNEMLIB	= $(CUDD_DIR)/mnemosyne/libmnem.a

cuddlib = $(CUDD_DIR)/dddmp/libdddmp.a \
	$(CUDD_DIR)/cudd/libcudd.a \
	$(CUDD_DIR)/mtr/libmtr.a \
	$(CUDD_DIR)/st/libst.a  \
	$(CUDD_DIR)/util/libutil.a \
	$(CUDD_DIR)/epd/libepd.a

aigerlib = $(AIGER_DIR)/libaiger.a

minisatlib = $(MINISAT_DIR)/libminisat.a

manysatlib = $(MANYSAT_DIR)/libmanysat.a

abclib = $(ABC_DIR)/libabc.a

LIBS = $(cuddlib) $(aigerlib) $(minisatlib) $(manysatlib) $(abclib)

#-----------------------------------------#
# The name of the product and its version #
#-----------------------------------------#

PRODUCT = PdTRAV
VERSION = 3.2.0
EXE = fbv

# Compile and version information
#
# COMPILE_DATE and PDTRAV_VERSION are strings surrounded by double 
# quotes that contain spaces, e.g., "PdTRAV 2.5"

VERDATE := -DCOMPILE_DATE="\"$(shell date)\"" -DPDTRAV_VERSION="\"$(PRODUCT) $(VERSION)\""

#---------------------------------#
# The list of packages to compile #
#---------------------------------#

PKGS = pdtutil cuplus ddi fsm mc part tr trav expr baig 

EXESRC = $(FBV_DIR)/fbv.c \
	$(FBV_DIR)/fbvThrd.c \
	$(FBV_DIR)/fbvAig.c \
	$(FBV_DIR)/fbvMc.c \
	$(FBV_DIR)/fbvProj.c \
	$(FBV_DIR)/fbvComb.c \
	$(FBV_DIR)/fbvAbcLink.c

EXEHDR = $(FBV_DIR)/fbv.h  $(FBV_DIR)/fbvInt.h

#--------------------------------------------------#
# Include the make templates from all the packages #
#--------------------------------------------------#

MAKEINCLUDES = $(foreach package, $(PKGS), \
	$(PDTRAV_DIR)/src/$(package)/$(package).make)

include $(MAKEINCLUDES)

OBJECTS = $(addprefix $(objectdir)/,$(GENERATEDCSRC:.c=.o) $(CSRC:.c=.o))
EXEOBJ = $(EXESRC:.c=.o)

#--------------------------------------------------------------#
# Form the the list of directories to search for header files. #
#--------------------------------------------------------------#

VPATH = $(PDTRAV_DIR): \
	$(objectdir): \
	$(addprefix :$(PDTRAV_DIR)/src/,$(PKGS))

INCLUDEDIRS = -I$(includedir)

#----------------#
# Implicit rules #
#----------------#

# For compiling a source file into the object directory

$(objectdir)/%.o : %.c 
	umask 2 ; $(CC) -c $(ALLCFLAGS) $(INCLUDEDIRS) -o $@ $<

%.o : %.c $(EXEHDR)
	umask 2 ; $(CC) -c $(ALLCFLAGS) $(VERDATE) $(INCLUDEDIRS) -o $@ $<

# Place object files into an archive

%.a :<
	rm -f $@
	umask 2; $(AR) cq $@ $^
	$(RANLIB) $@

#----------------------------------#
# Rules for generating directories #
#----------------------------------#

$(objectdir) :
	- umask 2 ; mkdir $(objectdir)

$(libdir) :
	- umask 2 ; mkdir $(libdir)

$(bindir) :
	- umask 2 ; mkdir $(bindir)

$(includedir) :
	- umask 2 ; mkdir $(includedir) ; mkdir -p $(includedir)/misc/vec ; mkdir -p $(includedir)/misc/util ; mkdir -p $(includedir)/misc/extra ; mkdir -p $(includedir)/aig/saig ; mkdir -p $(includedir)/aig/aig ; mkdir -p $(includedir)/aig/gia ; mkdir -p $(includedir)/sat/cnf ; mkdir -p $(includedir)/opt/dar ; mkdir -p $(includedir)/sat/bsat ; mkdir -p $(includedir)/proof/int ;
	ln -s $(CUDD_DIR)/include/*.h $(includedir)
	ln -s $(AIGER_DIR)/*.h $(includedir)
	ln -s $(MINISAT_DIR)/*.h $(includedir)
	ln -s $(ABC_DIR)/src/proof/int/*.h $(includedir)
	ln -s $(ABC_DIR)/src/aig/saig/*.h $(includedir)
	ln -s $(ABC_DIR)/src/sat/cnf/*.h $(includedir)
	ln -s $(ABC_DIR)/src/aig/aig/*.h $(includedir)
	ln -s $(ABC_DIR)/src/aig/gia/*.h $(includedir)
	ln -s $(ABC_DIR)/src/misc/extra/*.h $(includedir)
	ln -s $(ABC_DIR)/src/misc/vec/*.h $(includedir)
	ln -s $(ABC_DIR)/src/opt/dar/*.h $(includedir)
	ln -s $(ABC_DIR)/src/misc/util/abc_global.h $(includedir)
	ln -s $(ABC_DIR)/src/misc/util/utilCex.h $(includedir)
	ln -s $(ABC_DIR)/src/sat/bsat/*.h $(includedir)
	ln -s $(ABC_DIR)/src/proof/int/*.h $(includedir)/proof/int
	ln -s $(ABC_DIR)/src/aig/saig/*.h $(includedir)/aig/saig
	ln -s $(ABC_DIR)/src/sat/cnf/*.h $(includedir)/sat/cnf
	ln -s $(ABC_DIR)/src/aig/aig/*.h $(includedir)/aig/aig
	ln -s $(ABC_DIR)/src/aig/gia/*.h $(includedir)/aig/gia
	ln -s $(ABC_DIR)/src/misc/extra/*.h $(includedir)/misc/extra
	ln -s $(ABC_DIR)/src/misc/vec/*.h $(includedir)/misc/vec
	ln -s $(ABC_DIR)/src/opt/dar/*.h $(includedir)/opt/dar
	ln -s $(ABC_DIR)/src/misc/util/abc_global.h $(includedir)/misc/util
	ln -s $(ABC_DIR)/src/misc/util/utilCex.h $(includedir)/misc/util
	ln -s $(ABC_DIR)/src/sat/bsat/*.h $(includedir)/sat/bsat
	@for dir in $(PKGS); do \
		(ln -s $(PDTRAV_DIR)/src/$$dir/*.h $(includedir); )\
	done
	ln -s $(FBV_DIR)/*.h $(includedir)

#------------------------------------------------------#
# Rules to compile and build libraries and executables #
#------------------------------------------------------#

$(PDTLIB): $(OBJECTS) 
	$(AR) cr $@ $?
	$(RANLIB) $@

cudd:
	${MAKE} CC=$(CUDD_CC) RANLIB=$(RANLIB) ICFLAGS="$(CUDD_ICFLAGS)" XCFLAGS="$(CUDD_XCFLAGS)" -C ${CUDD_DIR} cuddlibs
# x cudd-2.4.2
#	${MAKE} CC=$(GCC) XCFLAGS="$(XC_FLAGS)" -C ${CUDD_DIR} cuddlibs

abc: 
	${MAKE} -C ${ABC_DIR} $(DEFINE_PDT32) libabc.a 

aiger: 
	@if [ -e ${AIGER_DIR}/makefile ] ; then \
		(echo -n "") ; \
	else \
	        (echo "Configuring aiger"; cd ${AIGER_DIR}; ./configure $(DEFINE_PDT32)) \
	fi
	${MAKE} -C ${AIGER_DIR} libaiger.a


minisat:
	${MAKE} -C ${MINISAT_DIR} $(DEFINE_PDT32) libminisat.a

manysat:
	${MAKE} -C ${MANYSAT_DIR} libs

pdtlib:	$(objectdir) $(libdir) $(bindir) $(includedir) $(PDTLIB)

$(EXE): $(EXESRC) $(EXEHDR) $(EXEOBJ) $(PDTLIB) $(LIBS) 
	umask 2 ; $(LINKER) $(ALLCFLAGS) $(LDFLAGS) -o $(bindir)/$(EXE) $(INCLUDEDIRS) $(EXEOBJ) $(PDTLIB) $(LIBS) $(MANYSATFLAGS) $(OTHERLIBS) 

#$(EXE): $(EXESRC) $(EXEHDR) $(EXEOBJ) $(PDTLIB) $(LIBS)
#	umask 2 ; $(LINKER) $(ALLCFLAGS) -o $(bindir)/$(EXE) $(INCLUDEDIRS) $(EXESRC) $(PDTLIB) $(LIBS) $(OTHERLIBS)

all : abc aiger minisat manysat cudd pdtlib $(EXE) 

#--------------------#
# Rules for cleaning #
#--------------------#

clean: pdtrav_clean 

#distclean: aiger_clean cudd_clean minisat_clean pdtrav_clean 
distclean: abc_clean aiger_clean cudd_clean minisat_clean manysat_clean pdtrav_clean 

pdtrav_clean: 
	rm -rf $(objectdir) $(libdir) $(bindir) $(includedir)
	${RM} $(PDTRAV_DIR)/src/*/#*
	${RM} $(PDTRAV_DIR)/src/*/*~
	${RM} $(FBV_DIR)/*.o

abc_clean: 
	${MAKE} -C ${abc_dir} clean 
	${RM} ${abc_dir}/*.a

aiger_clean: 
	${MAKE} -C ${AIGER_DIR} clean 
	${RM} ${AIGER_DIR}/*.a

cudd_clean:
	${MAKE} -C ${CUDD_DIR} distclean

minisat_clean:
	${MAKE} -C ${MINISAT_DIR} clean
	${RM} ${MINISAT_DIR}/*.a

manysat_clean:
	${MAKE} -C ${MANYSAT_DIR} clean
	${RM} ${MANYSAT_DIR}/*.a
