# makefile for QCL
#
# This file is part of the Quantum Computation Language QCL.
# 
# (c) Copyright by Bernhard Oemer <oemer@tph.tuwien.ac.at>, 1998
# 
# This program comes without any warranty; without even the implied 
# warranty of merchantability or fitness for any particular purpose.
# 
#      This program is free software under the terms of the 
#      GNU General Public Licence (GPL) version 2 or higher

VERSION=0.5.1

# Directory for Standard .qcl files

QCLDIR = /usr/local/lib/qcl

# Path for qcl binaries

QCLBIN = /usr/local/bin

# Debugging options

#DEBUG = -g -pg -DQCL_DEBUG -DQC_DEBUG
DEBUG = -O2 -g -DQCL_DEBUG -DQC_DEBUG
#DEBUG = -O2

# Plotting support 
#
# Comment out if you don't have GNU libplotter and X

PLOPT = -DQCL_PLOT
PLLIB = -L/usr/X11/lib -lplotter -lXaw -lXmu -lXt -lXext -lXpm -lSM -lICE -lX11
#PLLIB = -L/usr/X11/lib -lplotter -lXaw -lXmu -lXt -lXext -lX11 
#PLLIB = -L/usr/X11/lib -lplotter -lXaw -lXmu -Xt -lSM -ICE -lXext -lX11 

# Readline support
#
# Comment out if you don't have GNU readline on your system
# explicit linking against libtermcap or libncurses may be required

RLOPT = -DQCL_USE_READLINE
#RLLIB = -lreadline
RLLIB = -lreadline -lncurses

# Interrupt support
#
# Comment out if your system doesn't support ANSI C signal handling

IRQOPT = -DQCL_IRQ

# Replace with lex and yacc on non-GNU systems (untested)

LEX = flex
YACC = bison 
INSTALL = install

##### You shouldn't have to edit the stuff below #####

DATE = `date +"%y.%m.%d-%H%M"`

QCDIR = qc
QCLIB = $(QCDIR)/libqc.a
QCLINC = lib

CC = g++
CPP = $(CC) -E
CFLAGS = -c -Wall $(DEBUG) $(PLOPT) $(RLOPT) $(IRQOPT) -I$(QCDIR) -DDEF_INCLUDE_PATH="\"$(QCLDIR)\""
LFLAGS = -L$(QCDIR) $(DEBUG) $(PLLIB) -lm -lfl -lqc $(RLLIB) 

FILESCC = $(wildcard *.cc)
FILESH = $(wildcard *.h)

SOURCE = $(FILESCC) $(FILESH) qcl.lex qcl.y Makefile

OBJECTS = types.o syntax.o typcheck.o symbols.o error.o \
          lex.o yacc.o print.o quheap.o extern.o eval.o exec.o \
          parse.o options.o debug.o cond.o dump.o plot.o format.o

all: do-it-all

ifeq (.depend,$(wildcard .depend))
include .depend
do-it-all: build
else
do-it-all: dep
	make
endif

#### Rules for depend

dep: lex.cc yacc.cc yacc.h $(QCLIB)
	for i in *.cc; do \
	  $(CPP) -I$(QCDIR) -MM $$i; \
	  echo -e '\t$(CC) $(CFLAGS)' $$i '\n'; \
	done > .depend

lex.cc: qcl.lex
	$(LEX) -olex.cc qcl.lex

yacc.cc: qcl.y
	$(YACC) -t -d -o yacc.cc qcl.y

yacc.h: yacc.cc
	mv yacc.cc.h yacc.h

$(QCLIB):
	cd $(QCDIR) && make libqc.a

#### Rules for build

build: qcl $(QCLINC)/default.qcl

qcl: $(OBJECTS) qcl.o $(QCLIB)
	$(CC) $(OBJECTS) qcl.o $(LFLAGS) -o qcl

qcl-static: $(OBJECTS) qcl.o $(QCLIB)
	$(CC) -static $(OBJECTS) qcl.o $(LFLAGS) -o qcl-static
	strip qcl-static

$(QCLINC)/default.qcl: extern.cc
	grep "^//!" extern.cc | cut -c5- > $(QCLINC)/default.qcl

install: build
	$(INSTALL) -m 0755 -d $(QCLBIN) $(QCLDIR)
	$(INSTALL) -m 0755 ./qcl $(QCLBIN)
	$(INSTALL) -m 0644 ./lib/*.qcl $(QCLDIR)

uninstall:
	rm -f $(QCLBIN)
	rm -f $(QCLDIR)/*.qcl
	rmdir $(QCLDIR)

#### Other Functions

#SOURCEFILES = `ls $(SOURCE)`

tags: $(SOURCE)
	ctags $(SOURCE)

edit: tags
#	nedit -tags tags -iconic $(SOURCEFILES) &
	nedit -iconic $(SOURCE) &

clean:
	rm -f *.o lex.* yacc.* 
	cd $(QCDIR) && make clean

clear: clean
	rm -f qcl qcl/default.qcl .depend
	cd $(QCDIR) && make clear

dist-src: dep
	mkdir qcl-$(VERSION)
	cp README CHANGES COPYING .depend $(SOURCE) qcl-$(VERSION) 
	mkdir qcl-$(VERSION)/qc
	cp qc/Makefile qc/*.h qc/*.cc qcl-$(VERSION)/qc
	cp -r lib qcl-$(VERSION)
	tar czf qcl-$(VERSION).tgz --owner=0 --group=0 qcl-$(VERSION)
	rm -r qcl-$(VERSION)

dist-bin: build qcl-static
	mkdir qcl-$(VERSION)-bin
	cp Makefile README CHANGES COPYING qcl qcl-static qcl-$(VERSION)-bin 
	cp -r lib qcl-$(VERSION)-bin
	tar czf qcl-$(VERSION)-bin.tgz --owner=0 --group=0 qcl-$(VERSION)-bin
	rm -r qcl-$(VERSION)-bin

upload: dist-src dist-bin
	scp qcl-$(VERSION).tgz oemer@tph.tuwien.ac.at:html/tgz
	scp qcl-$(VERSION)-bin.tgz oemer@tph.tuwien.ac.at:html/tgz

scp: dist-src
	scp qcl-$(VERSION).tgz oemer@tph.tuwien.ac.at:bak/qcl-$(DATE).tgz
