# Makefile for the simulator
# Copyright 2004-2007 Nanorex, Inc.  See LICENSE file for details. 
# $Id: Makefile 14229 2008-09-15 17:52:11Z ericmessick $

UNAME := $(shell uname)
# PYPATH is left in in case a way to run versus another python is needed
PYPATH = ""
# dotted python version (2.3, 2.4)
PYDVER := $(shell $(PYPATH)python -c "import sys; print sys.version[:3]")
# un-dotted python version (23, 24)
PYVER := $(shell $(PYPATH)python -c "import sys; print sys.version[0]+sys.version[2]")

LODE=
USEODE=
#LODE=-lode
#USEODE=-DUSE_ODE

ifeq ($(OS),Windows_NT)
#---------------------------------------- Start Windows stuff
# One dollar sign for DOS and two for Cygwin
UNAME_A=$(shell ver)
# UNAME_A=$$(shell ver)   # Cygwin: but in this case use 'uname -a' anyway
# Location of the MinGW compiler
CCDIR = C:/MinGW
#CCDIR = C:/Dev-Cpp
CC = "$(CCDIR)/bin/gcc.exe"
#CFLAGS=-g -I"$(CCDIR)/include" -I"C:/cygwin/usr/local/include" -I"C:/Python$(PYVER)/include" -Disnan=_isnan

CFLAGS=-g -I"$(CCDIR)/include" -I"C:/ode-win32-0.7/include" -I"C:/Python$(PYVER)/include" -Disnan=_isnan $(USEODE)

LDFLAGS=-L"$(CCDIR)/lib" -L"C:/ode-win32-0.7/lib/releasedll" $(LODE)
PYREXTARGET=sim.dll
STDC99=
SIMTARGET=simulator.exe
PYREXC=$(PYPATH)python c:/Python$(PYVER)/Scripts/pyrexc.py
#---------------------------------------- End of Windows stuff
else
#---------------------------------------- Start Unix/Mac stuff
UNAME_A=$$(uname -a)
CC=gcc
PYREXTARGET=sim.so
STDC99=-std=c99
SIMTARGET=simulator
CFLAGS=-pthread -fno-strict-aliasing -g -I/usr/include/python2.3 -fno-strict-aliasing -Wall -Wmissing-prototypes -Wstrict-prototypes -fPIC
#CFLAGS:=$(shell python distutils_compile_options.py compiler_so)
ifeq ($(strip $(UNAME)),Darwin)
#---------------------------------------- Mac
CFLAGS+=-arch i386 -arch ppc -force_cpusubtype_ALL -isysroot /Developer/SDKs/MacOSX10.4u.sdk -I/Library/Frameworks/Python.framework/Versions/$(PYDVER)/lib/python$(PYDVER)/config \
    -I/Library/Frameworks/Python.framework/Versions/$(PYDVER)/include/python$(PYDVER)/

LDFLAGSDIST= $(shell $(PYPATH)python -c "import sys,os.path;print '-L%s/lib -L%s/lib/python$(PYDVER)/config -lpython%s' % (os.path.normpath(sys.prefix), os.path.normpath(sys.prefix), $(PYDVER))")

LDFLAGS=-arch i386 -arch ppc -force_cpusubtype_ALL -isysroot /Developer/SDKs/MacOSX10.4u.sdk $(LDFLAGSDIST)  -Wl,-F.,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk -framework Python

LDSHARED=gcc -bundle
else
#---------------------------------------- Unix
PYBASE:=$(shell which python | sed "s%/bin/python%%")
CFLAGS+=-I$(PYBASE)/include/python$(PYDVER) $(USEODE)
LDFLAGS=-L$(PYBASE)/lib/python$(PYDVER)/config $(LODE) -lm -lpython$(PYDVER)
LDSHARED=gcc -shared
#---------------------------------------- End of Unix
endif
PYREXC=$(shell $(PYPATH)python -c "import findpyrex; print findpyrex.find_pyrexc()")
LDFLAGS+=-L/usr/lib -lm
CFLAGS+=-fno-strict-aliasing -DNDEBUG -g -Wall -Wmissing-prototypes \
  -Wstrict-prototypes -fPIC
# These CFLAGS and LDFLAGS are not used by distutils. If asked to
# compile or link, Pyrex uses distutils, and will therefore not
# use these CFLAGS and LDFLAGS.
#---------------------------------------- End of Unix/Mac stuff
endif

COMMONOBJS=\
	allocate.o \
	amber_patterns.o \
	dynamics.o \
	globals.o \
	hashtable.o \
	interpolate.o \
	jigs.o \
	lin-alg.o \
	minimize.o \
	minstructure.o \
	newtables.o \
	part.o \
	pam5_patterns.o \
	pattern.o \
	potential.o \
	printers.o \
	printGromacsTopology.o \
	read_amber_itp.o \
	readmmp.o \
	readxyz.o \
	rigid.o \
	rigid-ode.o \
	structcompare.o \
	writemovie.o

ifeq ($(OS),Windows_NT)
	COMMONOBJS+= strtok_r.o
endif

PYREXSRCS=$(COMMONOBJS:.o=.c)

SIMOBJS=$(COMMONOBJS) simulator.o

PYREXOBJS=$(COMMONOBJS) sim.o

EXE_DIRECTORY=../../cad/bin
#EXE_DIRECTORY=~/bin

ifeq ($(PROFILING),1)
CFLAGS+=-pg
LDFLAGS+=-pg
endif

ifeq ($(WWDEBUG),1)
CFLAGS+=-DWWDEBUG
endif

# It's important not to change these two targets.
all: $(SIMTARGET)

help:
	echo $(DISTU)

# It's important not to change these two targets.
pyx: $(PYREXTARGET)

.PHONY: all lall noopt pyx install install-linux clean dist_clean depend depends

lall: all pyx glviewer

SUFFIXES: .c .h .o ;

.c.o:
	$(CC) -c $(CFLAGS) -o $@ $(@:.o=.c)

#bruce 051230 added dependency on Makefile to some rules (not all, and not as many are
# affected as I'd hoped);
# this is desirable since Makefile defines CFLAGS (and full remake doesn't take very long);
# feel free to do this in a cleaner way, or not at all if it causes problems.

# -std-c99 gets us isnormal()
structcompare.o: structcompare.c simulator.h
	$(CC) -c $(STDC99) $(CFLAGS) -o structcompare.o structcompare.c

ifeq ($(OS),Windows_NT)
# We need sim.c rule to build for Windows
sim.c: simhelp.c sim.pyx
	$(PYREXC) sim.pyx

libpython$(PYVER).a: libpython$(PYVER).a.gz
	gunzip < libpython$(PYVER).a.gz > libpython$(PYVER).a

sim.dll: sim.c $(PYREXOBJS) libpython$(PYVER).a version.h
	gcc -shared -I"C:/Python$(PYVER)/include" -o sim.dll $(PYREXOBJS) \
		-Wl,--output-def,sim.def -L. -lpython$(PYVER) -L"C:/ode-win32-0.7/lib/releasedll" $(LODE)
else
# here's the distutils version:
#sim.so: Makefile $(PYREXSRCS) version.h bends.gen bonds.gen sim.pyx simhelp.c
#	python setup.py build_ext --inplace

sim.c: sim.pyx simhelp.c
	$(PYREXC) sim.pyx

sim.so: $(PYREXOBJS)
	$(LDSHARED) $(PYREXOBJS) -o sim.so $(LDFLAGS)
endif

tables.c: gentab.py
	$(PYPATH)python gentab.py > tables.c

bonds.gen: stretch.py stretch.parms
	$(PYPATH)python stretch.py stretch.parms > bonds.gen

bends.gen: bend.py bending.parms
	$(PYPATH)python bend.py bending.parms > bends.gen

# There is something about bends.gen that the compiler doesn't want to
# optimize. It's getting tripped up by the bend names; if they are all
# the same it doesn't get stuck.
newtables.o: newtables.c
	$(CC) -o newtables.o $(CFLAGS) -c -g newtables.c

version.h:
	$(PYPATH)python makehelp.py "$(CFLAGS)" "$(LDFLAGS)" "$(UNAME_A)" \
		| grep -v "^running " > version.h

noopt:
	make CFLAGS=-g $(SIMTARGET)

$(SIMTARGET): $(SIMOBJS) version.h
	$(CC) -o $(SIMTARGET) $(LDFLAGS) $(SIMOBJS)

testminimize: minimize.c allocate.o
	$(CC) -o testminimize.o $(CFLAGS) -DTEST -c minimize.c
	$(CC) -o testminimize $(LDFLAGS) testminimize.o allocate.o
teststructcompare: structcompare.c minimize.o allocate.o lin-alg.o
	$(CC) -o teststructcompare.o $(CFLAGS) -DTEST -c structcompare.c
	$(CC) -o teststructcompare $(LDFLAGS) teststructcompare.o minimize.o allocate.o lin-alg.o

glviewer: glviewer.c allocate.o
	$(CC) -o glviewer.o $(CFLAGS) -c glviewer.c
	$(CC) -o glviewer $(LDFLAGS) -L/usr/X11R6/lib -lGL -lGLU glviewer.o allocate.o

install: $(SIMTARGET)
	if [ ! -d $$HOME/bin ] ; then \
		mkdir $$HOME/bin ;\
	fi
	cp -f $(SIMTARGET) $$HOME/bin
	if [ ! -d $(EXE_DIRECTORY) ] ; then \
		mkdir $(EXE_DIRECTORY) ;\
	fi
	cp $(SIMTARGET) $(EXE_DIRECTORY) 

install-linux: install $(PYREXTARGET)
	cp sim.so $(EXE_DIRECTORY)

clean:
	rm -f version.h bonds.gen bends.gen
	rm -f $(SIMTARGET) moldisp molsim physeng sim.so sim.c
	rm -f glviewer
	rm -f *.o *.a *.dll *.def sim.c *.so \.\#* *~ fmc.mmp dumpstruct.xyz *.pyc *.gcno
	rm -rf html gmon.out build obj
	rm -f tests/*/*.trcnew
	rm -f tests/*/*.altout
	rm -f tests/*/*.diff
	rm -f tests/*/*.xyz
	rm -f tests/*/*.dpb

dist_clean: clean
	rm -f TAGS

TAGS:
	etags *.c *.h


doxy:
	doxygen Doxyfile

depend: version.h
	head -`egrep -n "^# BEGIN" Makefile | sed 's/:.*//'` Makefile > tmp.mk
	makedepend -f tmp.mk -Y. *.[ch]
	mv -f tmp.mk Makefile
	rm tmp.mk.bak

depends: depend

# BEGIN DEPENDENCIES
# DO NOT DELETE

allocate.o: allocate.h
amber_patterns.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h
amber_patterns.o: minimize.h structcompare.h part.h newtables.h interpolate.h
amber_patterns.o: readmmp.h readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h
amber_patterns.o: amber_patterns.h pam5_patterns.h pattern.h potential.h
amber_patterns.o: minstructure.h writemovie.h rigid.h printGromacsTopology.h
amber_patterns.o: globals.h
dynamics.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
dynamics.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
dynamics.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
dynamics.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
dynamics.o: rigid.h printGromacsTopology.h globals.h
globals.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
globals.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
globals.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
globals.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
globals.o: rigid.h printGromacsTopology.h globals.h
glviewer.o: allocate.h
hashtable.o: allocate.h hashtable.h
interpolate.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h
interpolate.o: minimize.h structcompare.h part.h newtables.h interpolate.h
interpolate.o: readmmp.h readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h
interpolate.o: amber_patterns.h pam5_patterns.h pattern.h potential.h
interpolate.o: minstructure.h writemovie.h rigid.h printGromacsTopology.h
interpolate.o: globals.h
jigs.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
jigs.o: structcompare.h part.h newtables.h interpolate.h readmmp.h read_amber_itp.h readxyz.h
jigs.o: printers.h dynamics.h jigs.h amber_patterns.h pam5_patterns.h
jigs.o: pattern.h potential.h minstructure.h writemovie.h rigid.h
jigs.o: printGromacsTopology.h globals.h
lin-alg.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
lin-alg.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
lin-alg.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
lin-alg.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
lin-alg.o: rigid.h printGromacsTopology.h globals.h
minimize.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
minimize.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
minimize.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
minimize.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
minimize.o: rigid.h printGromacsTopology.h globals.h
minstructure.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h
minstructure.o: minimize.h structcompare.h part.h newtables.h interpolate.h
minstructure.o: readmmp.h readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h
minstructure.o: amber_patterns.h pam5_patterns.h pattern.h potential.h
minstructure.o: minstructure.h writemovie.h rigid.h printGromacsTopology.h
minstructure.o: globals.h
newtables.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
newtables.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
newtables.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
newtables.o: pam5_patterns.h pattern.h potential.h minstructure.h
newtables.o: writemovie.h rigid.h printGromacsTopology.h globals.h bonds.gen
newtables.o: bends.gen
pam5_patterns.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h
pam5_patterns.o: minimize.h structcompare.h part.h newtables.h interpolate.h
pam5_patterns.o: readmmp.h readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h
pam5_patterns.o: amber_patterns.h pam5_patterns.h pattern.h potential.h
pam5_patterns.o: minstructure.h writemovie.h rigid.h printGromacsTopology.h
pam5_patterns.o: globals.h
part.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
part.o: structcompare.h part.h newtables.h interpolate.h readmmp.h read_amber_itp.h readxyz.h
part.o: printers.h dynamics.h jigs.h amber_patterns.h pam5_patterns.h
part.o: pattern.h potential.h minstructure.h writemovie.h rigid.h
part.o: printGromacsTopology.h globals.h
pattern.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
pattern.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
pattern.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
pattern.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
pattern.o: rigid.h printGromacsTopology.h globals.h
potential.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
potential.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
potential.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
potential.o: pam5_patterns.h pattern.h potential.h minstructure.h
potential.o: writemovie.h rigid.h printGromacsTopology.h globals.h
printers.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
printers.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
printers.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
printers.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
printers.o: rigid.h printGromacsTopology.h globals.h
printGromacsTopology.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h
printGromacsTopology.o: minimize.h structcompare.h part.h newtables.h
printGromacsTopology.o: interpolate.h readmmp.h readxyz.h read_amber_itp.h printers.h
printGromacsTopology.o: dynamics.h jigs.h amber_patterns.h pam5_patterns.h
printGromacsTopology.o: pattern.h potential.h minstructure.h writemovie.h
printGromacsTopology.o: rigid.h printGromacsTopology.h globals.h
read_amber_itp.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h
read_amber_itp.o: minimize.h structcompare.h part.h newtables.h interpolate.h
read_amber_itp.o: readmmp.h readxyz.h read_amber_itp.h printers.h dynamics.h
read_amber_itp.o: jigs.h amber_patterns.h pam5_patterns.h pattern.h
read_amber_itp.o: potential.h minstructure.h writemovie.h rigid.h
read_amber_itp.o: printGromacsTopology.h globals.h
readers.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
readers.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
readers.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
readers.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
readers.o: rigid.h printGromacsTopology.h globals.h
readmmp.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
readmmp.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
readmmp.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
readmmp.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
readmmp.o: rigid.h printGromacsTopology.h globals.h
readxyz.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
readxyz.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
readxyz.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
readxyz.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
readxyz.o: rigid.h printGromacsTopology.h globals.h
rigid.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
rigid.o: structcompare.h part.h newtables.h interpolate.h readmmp.h read_amber_itp.h readxyz.h
rigid.o: printers.h dynamics.h jigs.h amber_patterns.h pam5_patterns.h
rigid.o: pattern.h potential.h minstructure.h writemovie.h rigid.h
rigid.o: printGromacsTopology.h globals.h rigid-ode.h
rigid-ode.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
rigid-ode.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
rigid-ode.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
rigid-ode.o: pam5_patterns.h pattern.h potential.h minstructure.h
rigid-ode.o: writemovie.h rigid.h printGromacsTopology.h globals.h
rigid-ode.o: rigid-ode.h
sim.o: simhelp.c simulator.h debug.h lin-alg.h allocate.h hashtable.h
sim.o: minimize.h structcompare.h part.h newtables.h interpolate.h readmmp.h
sim.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
sim.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
sim.o: rigid.h printGromacsTopology.h globals.h version.h
simhelp.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
simhelp.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
simhelp.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
simhelp.o: pam5_patterns.h pattern.h potential.h minstructure.h writemovie.h
simhelp.o: rigid.h printGromacsTopology.h globals.h version.h
simulator.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
simulator.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
simulator.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
simulator.o: pam5_patterns.h pattern.h potential.h minstructure.h
simulator.o: writemovie.h rigid.h printGromacsTopology.h globals.h version.h
simulator.o: debug.h lin-alg.h allocate.h hashtable.h minimize.h
simulator.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
simulator.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
simulator.o: pam5_patterns.h pattern.h potential.h minstructure.h
simulator.o: writemovie.h rigid.h printGromacsTopology.h globals.h
structcompare.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h
structcompare.o: minimize.h structcompare.h part.h newtables.h interpolate.h
structcompare.o: readmmp.h readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h
structcompare.o: amber_patterns.h pam5_patterns.h pattern.h potential.h
structcompare.o: minstructure.h writemovie.h rigid.h printGromacsTopology.h
structcompare.o: globals.h
writemovie.o: simulator.h debug.h lin-alg.h allocate.h hashtable.h minimize.h
writemovie.o: structcompare.h part.h newtables.h interpolate.h readmmp.h
writemovie.o: readxyz.h read_amber_itp.h printers.h dynamics.h jigs.h amber_patterns.h
writemovie.o: pam5_patterns.h pattern.h potential.h minstructure.h
writemovie.o: writemovie.h rigid.h printGromacsTopology.h globals.h