srcdir		= @srcdir@
top_srcdir	= @top_srcdir@
top_builddir	= @top_builddir@

subdir		= sources/lib/run-time

VPATH		= $(srcdir)

LIBDEST         = $(OPEN_DYLAN_USER_INSTALL)/lib
INCDEST         = $(OPEN_DYLAN_USER_INSTALL)/include

OPEN_DYLAN_TARGET_PLATFORM = @OPEN_DYLAN_TARGET_PLATFORM@
OBJDIR          = obj-$(OPEN_DYLAN_TARGET_PLATFORM)

HARP_RUNTIME_LIBRARY  = $(OBJDIR)/libdylan-harp-runtime.a
LLVM_RUNTIME_LIBRARY  = $(OBJDIR)/libdylan-llvm-runtime.a
C_RUNTIME_LIBRARY     = $(OBJDIR)/libdylan-c-runtime.a

# For all platforms, we want to build an LLVM and a C runtime.
# but for now, we'll modify this below to only build HARP on
# some platforms where we need HARP because we don't actually
# support a working C or LLVM runtime yet and when we use
# this directory on x86-linux and x86-freebsd, we only want to
# build HARP for now.
PLATFORM_RUNTIME_LIBRARIES=$(LLVM_RUNTIME_LIBRARY) $(C_RUNTIME_LIBRARY)
PLATFORM_INSTALL_RUNTIMES=install-llvm-runtime install-c-runtime

ifeq ($(OPEN_DYLAN_TARGET_PLATFORM),x86-linux)
  PLATFORM_CFLAGS=-m32 -D_REENTRANT=1 -D_GNU_SOURCE \
    -DOPEN_DYLAN_PLATFORM_LINUX -DOPEN_DYLAN_PLATFORM_UNIX \
    -DOPEN_DYLAN_ARCH_X86
  # Later, this = will become +=
  PLATFORM_RUNTIME_LIBRARIES = $(HARP_RUNTIME_LIBRARY)
  PLATFORM_INSTALL_RUNTIMES = install-harp-runtime
endif
ifeq ($(OPEN_DYLAN_TARGET_PLATFORM),x86_64-linux)
  PLATFORM_CFLAGS=-m64 -fPIC -D_REENTRANT=1 -D_GNU_SOURCE \
    -DOPEN_DYLAN_PLATFORM_LINUX -DOPEN_DYLAN_PLATFORM_UNIX \
    -DOPEN_DYLAN_ARCH_X86_64
endif
ifeq ($(OPEN_DYLAN_TARGET_PLATFORM),arm-linux)
  PLATFORM_CFLAGS=-fPIC -D_REENTRANT=1 -D_GNU_SOURCE \
    -DOPEN_DYLAN_PLATFORM_LINUX -DOPEN_DYLAN_PLATFORM_UNIX \
    -DOPEN_DYLAN_ARCH_ARM
endif
ifeq ($(OPEN_DYLAN_TARGET_PLATFORM),x86-freebsd)
  PLATFORM_CFLAGS=-m32 -D_REENTRANT=1 -DOPEN_DYLAN_PLATFORM_FREEBSD -DOPEN_DYLAN_PLATFORM_UNIX \
    -DOPEN_DYLAN_ARCH_X86
  # Later, this = will become +=
  PLATFORM_RUNTIME_LIBRARIES = $(HARP_RUNTIME_LIBRARY)
  PLATFORM_INSTALL_RUNTIMES = install-harp-runtime
endif
ifeq ($(OPEN_DYLAN_TARGET_PLATFORM),x86_64-freebsd)
  PLATFORM_CFLAGS=-m64 -fPIC -D_REENTRANT=1 -DOPEN_DYLAN_PLATFORM_FREEBSD -DOPEN_DYLAN_PLATFORM_UNIX \
    -DOPEN_DYLAN_ARCH_X86_64
endif
ifeq ($(OPEN_DYLAN_TARGET_PLATFORM),x86-darwin)
  PLATFORM_CFLAGS=-arch i386 -D_REENTRANT=1 -DOPEN_DYLAN_PLATFORM_DARWIN -DOPEN_DYLAN_PLATFORM_UNIX \
    -DOPEN_DYLAN_ARCH_X86
endif
ifeq ($(OPEN_DYLAN_TARGET_PLATFORM),x86_64-darwin)
  PLATFORM_CFLAGS=-arch x86_64 -D_REENTRANT=1 -DOPEN_DYLAN_PLATFORM_DARWIN -DOPEN_DYLAN_PLATFORM_UNIX \
    -DOPEN_DYLAN_ARCH_X86_64
endif

GC_CFLAGS       = @GC_CFLAGS@
GC_CHOICE       = @GC_CHOICE@
GC_LIBRARY      = @GC_LIBRARY@
GC_USE_STATIC_BOEHM = @GC_USE_STATIC_BOEHM@


CC = @CC@
AR = ar -rcs

CFLAGS		= $(PLATFORM_CFLAGS) -Wall -O -g $(GC_CFLAGS) -I$(srcdir)

OBJS		= $(OBJDIR)/break.o \
		  $(OBJDIR)/collector.o \
		  $(OBJDIR)/debug-print.o \
		  $(OBJDIR)/trace.o \
		  $(OBJDIR)/unix-spy-interfaces.o \
		  $(OBJDIR)/unix-support.o

HARP_SUPPORT_DIR = $(srcdir)/harp-support/$(OPEN_DYLAN_TARGET_PLATFORM)
HARP_OBJS        = $(HARP_SUPPORT_DIR)/runtime.o \
                   $(HARP_SUPPORT_DIR)/dylan-support.o

HARP_RUNTIME_OBJS = $(OBJDIR)/unix-threads-primitives.o

LLVM_RUNTIME_OBJS =

C_RUNTIME_OBJS    = $(OBJDIR)/c-run-time.o \
                    $(OBJDIR)/c-support.o \
                    $(OBJDIR)/c-primitives-debug.o \
                    $(OBJDIR)/c-primitives-math.o \
                    $(OBJDIR)/posix-threads.o

ifeq ($(GC_CHOICE),mps)
  OBJS += $(OBJDIR)/mps-dylan.o \
	  $(OBJDIR)/heap-display.o \
	  $(OBJDIR)/heap-utils.o \
	  $(OBJDIR)/heap-trail.o \
	  $(OBJDIR)/heap-order1.o \
	  $(OBJDIR)/heap-order2.o \
	  $(OBJDIR)/heap-table.o
endif

HARP_RUNTIME_LIBDEST  = $(LIBDEST)/runtime/harp-$(OPEN_DYLAN_TARGET_PLATFORM)
LLVM_RUNTIME_LIBDEST  = $(LIBDEST)/runtime/llvm-$(OPEN_DYLAN_TARGET_PLATFORM)
C_RUNTIME_LIBDEST     = $(LIBDEST)/runtime/c-$(OPEN_DYLAN_TARGET_PLATFORM)

all: $(OBJS) $(PLATFORM_RUNTIME_LIBRARIES)

$(HARP_RUNTIME_LIBDEST):
	mkdir -p $(HARP_RUNTIME_LIBDEST)

$(LLVM_RUNTIME_LIBDEST):
	mkdir -p $(LLVM_RUNTIME_LIBDEST)

$(C_RUNTIME_LIBDEST):
	mkdir -p $(C_RUNTIME_LIBDEST)

$(OBJDIR)/%.o: $(srcdir)/%.c
	mkdir -p $(OBJDIR)
	$(CC) $(CFLAGS) -MMD -c -o $@ $<

$(HARP_RUNTIME_LIBRARY): $(OBJS) $(HARP_RUNTIME_OBJS) $(HARP_OBJS)
	$(AR) $(HARP_RUNTIME_LIBRARY) $(OBJS) $(HARP_RUNTIME_OBJS) $(HARP_SUPPORT_DIR)/runtime.o

install-harp-runtime: $(HARP_RUNTIME_LIBRARY) $(HARP_RUNTIME_LIBDEST) $(HARP_OBJS)
	cp -fp $(HARP_RUNTIME_LIBRARY) $(HARP_RUNTIME_LIBDEST)
	cp -fp $(HARP_OBJS) $(HARP_RUNTIME_LIBDEST)
	cp -fp $(HARP_SUPPORT_DIR)/dylan-elf-dll.script $(LIBDEST)
	cp -fp $(HARP_SUPPORT_DIR)/dylan-elf-exe.script $(LIBDEST)

$(LLVM_RUNTIME_LIBRARY): $(OBJS) $(LLVM_RUNTIME_OBJS)
	$(AR) $(LLVM_RUNTIME_LIBRARY) $(OBJS) $(LLVM_RUNTIME_OBJS)

install-llvm-runtime: $(LLVM_RUNTIME_LIBRARY) $(LLVM_RUNTIME_LIBDEST)
	cp -fp $(LLVM_RUNTIME_LIBRARY) $(LLVM_RUNTIME_LIBDEST)

$(C_RUNTIME_LIBRARY): $(OBJS) $(C_RUNTIME_OBJS)
	$(AR) $(C_RUNTIME_LIBRARY) $(OBJS) $(C_RUNTIME_OBJS)

$(INCDEST):
	mkdir -p $(INCDEST)

install-c-runtime: $(C_RUNTIME_LIBRARY) $(C_RUNTIME_LIBDEST) $(INCDEST)
	cp -fp $(C_RUNTIME_LIBRARY) $(C_RUNTIME_LIBDEST)
	cp -fp $(srcdir)/trace.h $(INCDEST)
	cp -fp $(srcdir)/run-time.h $(INCDEST)
	@ if [ "$(GC_USE_STATIC_BOEHM)" = "true" ]; then \
	    cp -fp $(GC_LIBRARY) $(C_RUNTIME_LIBDEST); \
	fi


install: $(PLATFORM_INSTALL_RUNTIMES)

clean:
	rm -rf $(OBJDIR)

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	cd $(top_builddir) && ./config.status $(subdir)/Makefile

-include $(OBJS:%.o=%.d)
-include $(HARP_RUNTIME_OBJS:%.o=%.d)
-include $(LLVM_RUNTIME_OBJS:%.o=%.d)
-include $(C_RUNTIME_OBJS:%.o=%.d)
