# ------------------------------------------------------------------------------
# -- The ADP Compiler 
# -- Copyright (C) 2001-2007 Peter Steffen, Marco Ruether, Christian Lang,
# -- Georg Sauthoff
# --
# -- Send comments/bug reports to: P.Steffen <psteffen@techfak.uni-bielefeld.de>.
# -- Updates: http://www.techfak.uni-bielefeld.de/~psteffen/
# ------------------------------------------------------------------------------

# DEFINE YOUR INSTALLATION PATH HERE:
PREFIX = /usr/local
VERSION = 0.8

# default target
all: adpc 
	cd interfacer && $(MAKE)
	cd hsutils-1.2 && $(MAKE)

PERMF = go+r
PERMD = go+rx

install:
	-mkdir $(PREFIX)
	-mkdir $(PREFIX)/bin
	-mkdir $(PREFIX)/share
# adpcompile:
	cp adpcompile $(PREFIX)/bin
	chmod go+rx $(PREFIX)/bin/adpcompile
# adpc:
	echo '#!/bin/sh' > $(PREFIX)/bin/adpc
	echo PREFIX="$(PREFIX)" >> $(PREFIX)/bin/adpc
	echo '# Mini-driver for adpc' >> $(PREFIX)/bin/adpc
	cat interfacer/adpc-line >> $(PREFIX)/bin/adpc
	chmod 755 $(PREFIX)/bin/adpc
# interfacer:
	cp interfacer/adpc-bin $(PREFIX)/bin
	cp interfacer/man2c interfacer/man2lhs interfacer/colecho $(PREFIX)/bin
	chmod $(PERMD) $(PREFIX)/bin/adpc-bin
	chmod 755 $(PREFIX)/bin/man2c
	chmod 755 $(PREFIX)/bin/man2lhs
	chmod 755 $(PREFIX)/bin/colecho
	cp -R interfacer/share/adpc $(PREFIX)/share
	cp interfacer/share/adpc/src/* $(PREFIX)/share/adpc/lib/distr/src/
	cd $(PREFIX)/share/adpc ; find . -type f | xargs chmod $(PERMF)
	cd $(PREFIX)/share/adpc ; find . -type d | xargs chmod $(PERMD)
# layout:
	cp hsutils-1.2/layout $(PREFIX)/bin
	chmod $(PERMD) $(PREFIX)/bin/layout

# Define search-path
PATH_C = -I. -Itab/general -Itab/grasp -Itab/optimal
PATH_LHS = -i.
# Flags and tools
LDFLAGS = # -lm
CFLAGS = -O3 -Wall
CPPFLAGS = $(PATH_C)
CC = gcc
LEX = flex
DOXY = doxygen
DFILE = Doxyfile


# Haskell-source-files of the adpc
LHS = TTCombinators.lhs Constants.lhs Tools.lhs Lex.lhs Poly.lhs SM.lhs      \
      PrettyPrint.lhs MathExp.lhs TLData.lhs Expr.lhs           \
      Syntax.lhs StringLib.lhs Parse.hs Track.lhs Yieldsize.lhs Adptrans.lhs \
      WidthAna.lhs Dss.lhs Range.lhs TL.lhs TLFrame.lhs Depana.lhs           \
      Structs.lhs IL2.lhs LatexRecs.lhs Beautify.lhs Algebras.lhs            \
      Codegen.lhs Codegen1.lhs Phases.lhs Alggen.lhs AlggenParse.hs Compile.lhs Main.lhs    \
      CGI.lhs CGITools.lhs Pretty.lhs \
      LatexRecs_cgi.lhs Optimize_cgi.lhs Compile_cgi.lhs Alggen_cgi.lhs \
      Convert.lhs Lexer.lhs  Parse2.lhs Lift.lhs ParseMonad.lhs  ParseTree.lhs NewParser.hs \
      Man.lhs ListCompr.lhs

HAPPY = happy
HAPPYFLAGS = -a -g -c
HAPPYFLAGSGHC = -fglasgow-exts
%.hs : %.ly
	$(HAPPY) $(HAPPYFLAGS) $(@:.hs=.ly)


# C-Object-files for the ADP-compiler
OBJECTS_C = tabulate.o hook.o tab/optimal/taboptimal_in.o \
  tab/general/asm_runtime.o tab/optimal/functions.o      \
  tab/optimal/output.o tab/general/input.o tab/general/poly_runtime.o \
  tab/general/poly_type.o tab/general/dll.o tab/general/set.o \
  tab/general/graph.o tab/general/bag.o tab/general/index_list.o \
  tab/grasp/adpc_grasp.o tab/grasp/tab_grasp.o tab/general/array_int.o \
  tab/general/time_util.o tab/general/random.o tab/grasp/asm_factory.o \
  tab/general/dep_graph.o tab/general/comb.o \
  tab/general/log.o

DEPS = $(OBJECTS_C:.o=.d)

# Target-depending Settings
ifeq ($(MAKECMDGOALS),windows)
CFLAGS += -D WINDOWS
endif

ifeq ($(MAKECMDGOALS),sparc)
OLDGCCFLAGS = -pgmc gcc-2.95 -pgml gcc-2.95 -pgma gcc-2.95
endif

# GHCMEM =  +RTS -M512m -RTS 
GHCPROFILING = # -prof -auto-all

# Build the ADP-compiler
adpc: $(LHS)
adpc: $(OBJECTS_C)
	ghc $(GHCMEM) $(OLDGCCFLAGS) $(HAPPYFLAGSGHC) $(GHCPROFILING) -fffi --make Main.lhs -o adpcompile -optl $(filter %.o,$^)

sparc: adpc
windows: adpc

# Build ADP-CGI for ADP website
CGI_BIN_TARGET=/vol/bibidev/adp/cgi-bin

CGIGHC = umask 007;ghc

# the ADP-CGI binaries are always compiled under sparc solaris. Therefore we use gcc-2.95 here:
adp_Compile: $(LHS) $(OBJECTS_C)
	$(CGIGHC) -pgmc gcc-2.95 -pgml gcc-2.95 -pgma gcc-2.95 -fffi --make Main.lhs -o adp_Compile -optl $(filter %.o,$^)
	install --mode 775 adp_Compile $(CGI_BIN_TARGET)

# Do not remove Haskell o- and hi-files
clean:
	rm -f $(OBJECTS_C) $(DEPS) adpcompile adpcompile.exe \
	tab/general/adpc_deserialize.o main.o hand

# Remove Haskell o- and hi-files
PARSERHS = AlggenParse.hs NewParser.hs  Parse.hs 
clean_all: clean
	rm -f *.o *.hi $(PARSERHS) adpcompile adpcompile.exe docs doxygen.log

Man.lhs: adpc.pod
	pod2text -q '<>' adpc.pod > adpc.txt
	man2lhs '1-' adpc.txt > Man.lhs

options.tex: adpc.pod
	pod2latex adpc.pod -out tmp.tex
	sed 's/subsection\*/subsection/'  tmp.tex  > options.tex 
	rm tmp.tex

adpc.pdf: options.tex adpc.tex
	echo '\\def\\rsversion{'${VERSION}'}' > version.tex
	pdflatex adpc.tex
	cp adpc.pdf text

# C-Programm um den table design Teil 'per Hand' unabhaengig
# vom adpc mit serialisierten Daten (.i) zu testen
hand : $(OBJECTS_C) tab/general/adpc_deserialize.o main.o
	$(CC) -o $@ $^ -lm -L/vol/gnu/lib -lfl $(LDFLAGS)

########################################
# Documentation
########################################
doc:
	$(DOXY) $(DFILE)

########################################
# Tracking include file dependencies
########################################
# sun cc doesn't know the -M. options  ...
ifneq ($(filter $(CC),gcc icc),)

ifeq ($(findstring $(MAKECMDGOALS),clean clean_all),)
# work around stupid GNU make include bug
# (if make can create some non-existing include files, it should do it
#  and shut up - GNU make does not do that - it complains, but creates
#  the needed files - the -includes directive has the problem, that if
#  make can not create the included file it gives no error and shuts up)
-include $(DEPS)
endif

%.d : %.c
	@$(CC) -MM -MT "$@ $(@:.d=.o)" $(CPPFLAGS) $(CFLAGS) $< > $@
	@echo Create makefile dependencies for $<

endif

########################################
# Distribution
########################################

DISTNAME = adpc-$(VERSION)
OTHER = Parse.ly AlggenParse.ly NewParser.ly  \
        tabulate.c tabulate.h hook.c main.c \
        adpc.pod adpc.txt

distrib: 
	$(MAKE) clean
	$(MAKE) clean_all
	$(MAKE) Parse.hs
	$(MAKE) AlggenParse.hs
	$(MAKE) NewParser.hs
	-mkdir $(DISTNAME)
	-mkdir $(DISTNAME)/src
	-mkdir $(DISTNAME)/src/interfacer
	cp -a $(LHS) $(OTHER) Makefile $(DISTNAME)/src
	cd hsutils-1.2 && $(MAKE) clean
	cp -a tab hsutils-1.2 $(DISTNAME)/src
	cd interfacer && $(MAKE) clean
	cd interfacer && $(MAKE) InterfaceDTD.hs
	cp interfacer/*.lhs interfacer/*.dtd interfacer/Makefile $(DISTNAME)/src/interfacer
	cp interfacer/adpc-line interfacer/*.hs $(DISTNAME)/src/interfacer
	cp -a interfacer/share $(DISTNAME)/src/interfacer
	cp text/* $(DISTNAME)
	cd $(DISTNAME) && find -name .svn | xargs rm -rf
	tar cvfz $(DISTNAME).tgz $(DISTNAME)
#	rm -rf $(DISTNAME)

distclean:
	rm -rf $(LHS) $(OTHER) *.hi *.o *.d tab
	rm -rf interfacer/*.o interfacer/*.hi
	rm -rf interfacer/*.lhs interfacer/*.dtd
	rm -rf interfacer/*.hs
	rm -rf hsutils-1.2/*.hs hsutils-1.2/*.o
	rm -rf hsutils-1.2/*.hi hsutils-1.2/Lexer


