# This file is a component of SDPA
# Copyright (C) 2004 SDPA Project

#    This program is free software; 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, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# This file is Make file of SDPA
# $Id: Makefile,v 1.3 2004/09/02 01:14:19 makoto Exp $


LAPACK = $(HOME)/lapack
RSDPA  = .

LAPACK_LIB =  -llapack -lcblaswr -lcblas \
              -lf77blas -lI77 -lF77 -latlas

# if you use BLAS in clapack.tgz, use next line
# and set NON_ATLAS_SDPA in rsdpa_include.h to 1.
# 
# LAPACK_LIB =  -llapack -lblas -lF77
#

RSDPA_LIB_NAME = sdpa
#RSDPA_LIB_NAME = rsdpa
RSDPA_LIB = -l$(RSDPA_LIB_NAME)

CC = g++
OPTION = -O3 # -g # -Wall

TARGET_EXE = $(RSDPA_LIB_NAME)
#TARGET_EXE = rsdpa_main.exe
TARGET_LIB = lib$(RSDPA_LIB_NAME).a
TARGET_HEADER = rsdpa_class.h sdpa-lib.hpp sdpa-lib2.hpp
EXAMPLES = example1.r.exe example2.r.exe
SDPA_EXAMPLES = example1-1.exe example1-2.exe \
	example2-1.exe example2-2.exe \
	example3.exe example4.exe \
	example5.exe example6.exe \

OBJS = rsdpa_tool.o rsdpa_algebra.o rsdpa_struct.o \
		rsdpa_parts.o rsdpa_io.o rsdpa_dpotrf.o
BACKUP_IP_ADRRESS = 

now: $(TARGET_EXE)

all: $(TARGET_EXE) $(TARGET_HEADER) $(TARGET_LIB)

examples: $(EXAMPLES) $(SDPA_EXAMPLES)

sdpa: rsdpa_main.o $(OBJS)
	$(CC) $(OPTION) -o $@ $< $(OBJS) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)

rsdpa_main.exe: rsdpa_main.o $(OBJS)
	$(CC) $(OPTION) -o $@ $< $(OBJS) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)

librsdpa.a: rsdpa_lib.o $(OBJS) rsdpa_class.h
	ar r $@ $< $(OBJS)
	ranlib $@

libsdpa.a: rsdpa_lib.o $(OBJS) rsdpa_class.h
	ar r $@ $< $(OBJS)
	ranlib $@

example1.r.exe: example1.r.o $(TARGET_LIB) 
	$(CC) $(OPTION) -o $@ $< -L$(RSDPA) $(RSDPA_LIB) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)	

example2.r.exe: example2.r.o $(TARGET_LIB)
	$(CC) $(OPTION) -o $@ $< -L$(RSDPA) $(RSDPA_LIB) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)	

example1-1.exe: example1-1.o $(TARGET_LIB)
	$(CC) $(OPTION) -o $@ $< -L$(RSDPA) $(RSDPA_LIB) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)	

example1-2.exe: example1-2.o $(TARGET_LIB)
	$(CC) $(OPTION) -o $@ $< -L$(RSDPA) $(RSDPA_LIB) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)	

example2-1.exe: example2-1.o $(TARGET_LIB)
	$(CC) $(OPTION) -o $@ $< -L$(RSDPA) $(RSDPA_LIB) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)	

example2-2.exe: example2-2.o $(TARGET_LIB)
	$(CC) $(OPTION) -o $@ $< -L$(RSDPA) $(RSDPA_LIB) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)	

example3.exe: example3.o $(TARGET_LIB)
	$(CC) $(OPTION) -o $@ $< -L$(RSDPA) $(RSDPA_LIB) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)	

example4.exe: example4.o $(TARGET_LIB)
	$(CC) $(OPTION) -o $@ $< -L$(RSDPA) $(RSDPA_LIB) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)	

example5.exe: example5.o $(TARGET_LIB)
	$(CC) $(OPTION) -o $@ $< -L$(RSDPA) $(RSDPA_LIB) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)	

example6.exe: example6.o $(TARGET_LIB)
	$(CC) $(OPTION) -o $@ $< -L$(RSDPA) $(RSDPA_LIB) \
	  -L$(LAPACK)/lib $(LAPACK_LIB)	

.cpp.o:
	$(CC) $(OPTION) -c -I$(RSDPA) -I$(LAPACK)/include $<

force: clean now
forceall: cleanall all

clean:
	/bin/rm -f *.o *~ core r r2 p

exe: cleanexe $(TARGET_EXE)
lib: cleanlib $(TARGET_LIB)

cleanexe:
	/bin/rm -f $(TARGET_EXE)
cleanlib:
	/bin/rm -f $(TARGET_LIB)
cleanexamples:
	/bin/rm -f $(EXAMPLES) $(SDPA_EXAMPLES)
cleanheader:
	/bin/rm -f $(TARGET_HEADER)
cleanall: clean cleanexe cleanlib cleanexamples cleanheader

backup: clean cleanexe cleanlib cleanexamples
	(cd ..; tar czvf backuprsdpa.tar.gz sdpa/; \
         rcp backuprsdpa.tar.gz $(BACKUP_IP_ADRRESS):tmp)

rsdpa_class.h: rsdpa_tool.h
	/bin/rm -f rsdpa_class.h
	echo -e "/*-----------------------------------------\n" \
	        "  rsdpa_class.h                            \n" \
	        "-----------------------------------------*/\n" \
	        "#ifndef __rsdpa_class_h__                  \n" \
	        "#define __rsdpa_class_h__                  \n" \
		> rsdpa_class.h
	cat rsdpa_right.h rsdpa_tool.h rsdpa_include.h \
		rsdpa_struct.h rsdpa_dpotrf.h rsdpa_algebra.h \
		rsdpa_parts.h rsdpa_io.h | \
		grep -v include.*rsdpa \
		>> rsdpa_class.h
	echo -e "#endif // __rsdpa_class_h__                \n" \
		>> rsdpa_class.h

sdpa-lib.hpp: rsdpa_class.h
	cp $< $@
sdpa-lib2.hpp: rsdpa_lib.h
	cp $< $@
