################################################################
# Makefile for X11 library (distributed as part of Graphics library)
#
# You may need to modify XDIR to suit your machine.
# You may also have to modify LDFLAGS to suit your machine/linker.
#
# You DO NOT need GreenCard UNLESS you have modified the *.gc files.
# If you used CVS to obtain this copy of the library, you may need to
# 
#   touch StdDIS.c X.c Xlib.c StdDIS.hs X.hs Xlib.hs
# 
# to 'correct' the modification dates before running make.
# 
# If you do modify the *.gc files, you need to obtain a copy of
# greencard-2.0 from 
#
#   http://www.dcs.gla.ac.uk/fp/software/green-card/
# 
# and either change GC to point to your GreenCard binary or change
# GCDIR to point to the directory containing GreenCard.
################################################################

# This goes first to make it the default
all		::

################################################################
# Configuration (needs to be customised)
################################################################

XDIR		= /usr/X11R6

# Either use the next line (if you have a GreenCard binary installed)
#GC	    	= greencard
# or use the next three lines (if you only have GreenCard sources installed)
RUNHUGS		= runhugs
GCDIR		= $(HOME)/local/greencard-2.0
GC	    	= $(RUNHUGS) -c100 -h2m $(GCDIR)/src/GreenCard.lhs

GHC		= ghc
GHCFLAGS	+= -fglasgow-exts -cpp -syslib concurrent
# This flag is required on FreeBSD to make sure that fd_set is defined
# by <sys/types.h>
GHCFLAGS	+= -optc-DNON_POSIX_SOURCE
CC		= cc
LDFLAGS		+= -shared -nostdlib

################################################################
# Files (should not need to be customised)
################################################################

# These specify the input files and some of the intermediate files
# In the GHC case, there is a lot of overlap between HS_SRCS and HS.
# Not sure what to do about that.
#
# Also, greencard doesn't always generate a stub file so any attempt to
# write automatic dependencies is hosed... Blech!

GC_SRCS		= $(wildcard *.gc)
HS_SRCS		= $(wildcard *.hs)
HS_OBJS		= $(addsuffix .o,  $(basename $(HS_SRCS)))
HS		= $(addsuffix .hs, $(basename $(GC_SRCS)))
# STUBS		= $(addsuffix _stub.o, $(basename $(GC_SRCS)))
OBJS		= $(addsuffix .o,  $(basename $(GC_SRCS)))
SOS		= $(addsuffix .so, $(basename $(GC_SRCS)))
C_FILES		= $(addsuffix .c,  $(basename $(GC_SRCS)))

.PRECIOUS: $(C_FILES) $(HS)

clean		::
		$(RM) $(HS) $(OBJS) $(SOS) $(C_FILES) 

################################################################
# Flags (should not need to be customised)
################################################################

INCLUDES	= -I$(XDIR)/include
LIBS		= -L $(XDIR)/lib -lX11 

################################################################
# GreenCard suffix rules
################################################################

ifdef FOR_GHC # rules for building GHC version

GC_FLAGS  	= --target GHC

.SUFFIXES	: .gc .hs .o .a

%.hs		: %.gc
		$(GC) $(GC_FLAGS) -I . $<
%.o		: %.hs
		$(GHC) $(GHCFLAGS) $(INCLUDES) $< -c
%_stub.o	: %_stub.c
		$(CC) $(INCLUDES) $< -c
libgraphics.a	: $(HS_OBJS) $(OBJS) $(STUBS)
		ar csq $@ $^

all		:: libgraphics.a

else # rules for building Hugs version

GC_FLAGS  	= --target Hugs 

.SUFFIXES	: .gc .hs .c .o .so

%.c %.hs	: %.gc
		$(GC) $(GC_FLAGS) -I . $<
%.o		: %.c
		$(CC) $(CFLAGS) $(INCLUDES) $< -c
%.so		: %.o
		$(CC) $(LDFLAGS) $(LIBS) -o $@ $< 

all		:: $(SOS)

endif

################################################################
# End of Makefile
################################################################
