#
# Makefile for Cygwin.
#
CC = gcc
CXX = c++
FC = g77

DEBUG = -g #-O2
CXXFLAGS = $(DEBUG)
CFLAGS = $(DEBUG)
FFLAGS = $(DEBUG)
CPPFLAGS = -I.

exeext = .exe

#
# Various targets to build.
#
DLL_NAME = cygf77dll.dll
DLL_EXP_LIB = libf77dll.dll.a
DLL_EXP_DEF = libf77dll.def

TESTPROGS = usedll$(exeext)

all: $(DLL_NAME) $(TESTPROGS)

#
# sources, objects, etc. 
#
SRCS  = $(wildcard *.cc *.c *.f *.F)
OBJS  = $(SRCS:.cc=.o)
OBJS := $(OBJS:.c=.o)
OBJS := $(OBJS:.f=.o)
OBJS := $(OBJS:.F=.o)

#
# DLL related variables. These are used when building the DLL. See later.
#

# Must define BUILDING_DLL when building the DLL. Otherwise import/exports 
# will not work correctly. See dllclass.h for more info.
DLL_CFLAGS = -DBUILDING_DLL=1
# any special flags that you need to pass to the F77 compiler when building 
# the DLL. (lots of software need _DLL defined).
DLL_FFLAGS = 
# The default entry point defined by dllwrap; the default user callback
# is DllMain, and there is stub in dllinit.c. 
DLL_LDFLAGS = 
# any extra libraries that your DLL may depend on.
DLL_LDLIBS = 

#
# your sources etc that go into the DLL.
#
DLL_SRCS = f77dll.f
DLL_OBJS = f77dll.o

###
#
# Making DLL
#
###


$(DLL_NAME) $(DLL_EXP_DEF) $(DLL_EXP_LIB): $(DLL_OBJS)
	$(FC) -shared -Wl,--export-dynamic -Wl,--output-def=$(DLL_EXP_DEF) \
	-Wl,--exclude-symbols=_bss_end__ \
	-Wl,--exclude-symbols=_bss_start__ \
	-Wl,--exclude-symbols=_data_end__ \
	-Wl,--exclude-symbols=_data_start__  \
	-Wl,--out-implib=$(DLL_EXP_LIB) -Wl,--enable-auto-image-base \
	-o $(DLL_NAME) $(DLL_OBJS) $(DLL_LDFLAGS) $(DLL_LDLIBS)
#
# making exec
#
usedll$(exeext): usedll.o $(DLL_EXP_LIB)
	$(FC) -o $@ $(CXXFLAGS) $(LDFLAGS) usedll.o -L./ -lf77dll

#
# dependencies.
#

f77dll.o: f77dll.F
usedll.o: usedll.F

#
# default rules for building DLL objects. Note that client programs (ie.,
# the ones that *use* the DLL) have to be compiled without the DLL_CFLAGS
# flags.
#
.cc.o:
	$(CXX) -c $(DLL_CFLAGS) $(CPPFLAGS) $(CXXFLAGS) -o $@ $<
.c.o:
	$(CC) -c $(DLL_CFLAGS) $(CPPFLAGS) $(CFLAGS) -o $@ $<
.F.o:
	$(FC) -c $(DLL_FFLAGS) $(FFLAGS) $(CPPFLAGS) -o $@ $<
.f.o:
	$(FC) -c $(DLL_FFLAGS) $(FFLAGS) -o $@ $<

# Note that we omit the $(DLL_CFLAGS) for client programs.
usedll.o: %o: %F
	$(FC) -c -o $@ $<

clean:
	-rm -f $(OBJS) $(DLL_NAME) $(DLL_EXP_LIB) $(DLL_EXP_DEF) $(TESTPROGS)

