#  C->Haskell Compiler: makefile for the marshaling library
#
#  Author : Manuel M. T. Chakravarty
#  Derived: 19 March 1999
#
#  Version $Revision: 1.13 $ from $Date: 2001/04/29 13:13:52 $
#
#  Copyright (c) [1999..2001] Manuel M. T. Chakravarty
#
#  This file 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 file 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.
#
#  = DOCU =====================================================================
#
#  * Originally, derived from `c2hs/gen/Makefile'.
#
#  * The files in this part are not linked into `c2hs', but instead contain
#    routines used at runtime by the code generated by `c2hs'.
#

#  ***************************************
#  !!! This makefile requires GNU make !!!
#  ***************************************

# info for this part: its package, name, sources and objects files (must be 
# before the include due to conditionals etc in `common.mk')
#
PACKAGE = c2hs
PART    = lib
SRCS	= C2HS.hs C2HSBase.hs C2HSDeprecated.hs C2HSMarsh.hs\
	  NewStorable.hs NewStablePtr.hs
OBJS	= $(patsubst %.hs,%.o,$(filter %.hs,$(SRCS)))

# C->Haskell specific configuration
#
# This must be before the legacy sources
#
-include ../mk/config.mk

# Legacy sources
#
# The following additional library sources are used when building C2HS for 
# Haskell systems using an old version of the Haskell FFI (this includes,
# for example, the GHC 4.08.x series).
#
ifeq ($(LEGACY_FFI),yes)
SRCS   += CError.hs CForeign.hs CString.hs ForeignPtr.hs MarshalAlloc.hs\
	  MarshalArray.hs MarshalError.hs MarshalUtils.hs Ptr.hs
endif

# This must be after legacy sources, as some rules depend on the setting
# of $(SRCS) and $(OBJS)
#
include  ../../mk/common.mk
-include $(DEPEND)

# The code in this part is independent from the compiler code; so forget about
# all the other directories
#
VPATH =
HIDIRS=.

# We have our own dependcy computations here, as the library modules are 
# independent from the c2hs compiler modules.
#
.PHONY: depend
depend:
	$(MKDEPENDHS) -optdep-f -optdep$(DEPEND) $(HCFLAGS)\
	  $(EXTRAMKDEPENDOPTS) $(SRCS)
ifeq ($(LEGACY_FFI),no)
	grep -v -e CError -e CForeign -e CString\
		-e ForeignPtr -e MarshalAlloc -e MarshalArray\
		-e MarshalError -e MarshalUtils -e "\bPtr" .depend\
		>.depend.new
	mv .depend.new .depend
# mkdependHS is a bit stupid here and does include files if present even if
# they are part of a syslib; moreover, the -x option never works for me 
# anyway, so let's do it the hard way
	mkdir -p ghc411hack_dir
	-mv CError.hs CForeign.hs CString.hs ForeignPtr.hs MarshalAlloc.hs\
	  MarshalArray.hs MarshalError.hs MarshalUtils.hs Ptr.hs ghc411hack_dir
# we have to move these files out of the way, because they confuse 4.11's
# compilation manager
endif

# Extra flags for the Haskell compiler
#
# * The following enables the use of the FFI and sucks in the prototypes needed
#   by external calls 
#
EXTRAHCFLAGS    = -fglasgow-exts $(LIB_HCFLAGS)

# make all object files
#
objs: $(OBJS)

# target for building the archive of the marshaling library
#
libc2hs: libc2hs.a
libc2hs.a: $(OBJS)
	-$(RM) $@
	$(AR) -crs $@ $(OBJS)

# make all
#
all: libc2hs
default: all

# misc targets
#
.PHONY: clean cleanhi

clean:
	-$(RM) *.o libc2hs.a
cleanhi:
	-$(RM) *.hi
