##############################################################################
# Makefile for Xlib with Hugs on Unix
# 
# This makefile is for GNU Make ver. 3.76 or later.
##############################################################################

################################################################
# Configuration parameters.  These may need slight modification.
# 
# Rather than edit this file, you can override the settings when you
# invoke Make.  For example:
#
#   make system=SunOS X_dir=/usr/local/X11R6
#
################################################################

# What OS are we using?  Affects choice of linker flags.
# Possible values: Linux, FreeBSD, NetBSD, SunOS and Unixlike
system=Linux

# Which directory contains lib/libX11.a and include/X11/X.h?
X_dir=/usr/X11R6

# Which directory is Hugs installed in?
hugs_install=/usr/share/hugs
# hugs_install=$(HOME)/local/share/hugs

# Should we rerun GreenCard if GreenCard generated files are out of date?
# (This will usually be set to "no" in distributions and "yes" in 
# development copies.)
rerun_GC=no

################################################################
# Tools and how to run them.  Nothing below this line should
# need modified.
################################################################

SHELL = /bin/sh
RM = rm -f

# At least Sun's Workshop C Compiler 5.0 98/12/15 C 5.0 has trouble with
# some of the generated C code.  Insist on gcc for now.
CC = gcc

INCLUDES = -I$(X_dir)/include -I$(hugs_install)/include

LD = ld
LDFLAGS	+= -L$(X_dir)/lib -lX11

# The following choices have been found to be appropriate for building
# shared object files on various systems.
# Please let us know if these don't work or if you know the appropriate
# choice for other systems.
LDFLAGS_MKSO.Linux    = -shared
LDFLAGS_MKSO.FreeBSD  = -shared
LDFLAGS_MKSO.NetBSD   = -shared
LDFLAGS_MKSO.SunOS    = -G
LDFLAGS_MKSO.Unixlike = -shared

LDFLAGS_MKSO = $(LDFLAGS_MKSO.$(system))

GC = green-card
GC_OPTS = --target=hugs

################################################################
# Implicit rules for GreenCard and shared objects
################################################################

ifeq ($(rerun_GC),yes)
%.c %.hs: %.gc
	$(GC) $(GC_OPTS) $<
endif

%.o: %.c
	$(CC) -c $(CPPFLAGS) $(INCLUDES) $(CFLAGS) $< -o $@

%.so: %.o
	$(LD) $(LDFLAGS_MKSO) $(LDFLAGS) $^ -o $@

################################################################
# Source and object files
################################################################

gc_sources = $(wildcard *.gc)
c_sources  = $(wildcard cbits/*.c)

hs_gen_sources = $(gc_sources:.gc=.hs)

all_hs_sources = $(hs_sources) $(hs_gen_sources)

c_gen_sources = $(gc_sources:.gc=.c)

all_c_sources = $(c_sources) $(c_gen_sources)

all_c_objects = $(all_c_sources:.c=.o)

shared_objects = $(gc_sources:.gc=.so)

################################################################
# Main targets
################################################################

.PHONY: all

all: $(hs_gen_sources) $(shared_objects)

ifeq ($(rerun_GC),yes)
clean::
	$(RM) $(hs_gen_sources)
	$(RM) $(c_gen_sources)
endif

clean::
	$(RM) $(all_c_objects)
	$(RM) $(shared_objects)

################################################################
# Auxiliary targets
################################################################

.PRECIOUS: $(hs_gen_sources) $(shared_objects)

.INTERMEDIATE: cbits/auxiliaries.o

# Additional dependences.
Xlib.so    : cbits/auxiliaries.o
Xlib.o X.o : cbits/HsXlib.h

################################################################
# End
################################################################

