# Build runtime library for C backend

srcdir          = /home/bruce/Development/dylan/opendylan/sources/dfmc/c-run-time
top_builddir    = ../../..
GC_CFLAGS       = -DGC_USE_BOEHM -DGC_THREADS
GC_LIBRARY      = /usr/lib/libgc.so
GC_USE_STATIC_BOEHM = false

OPEN_DYLAN_PLATFORM_NAME = x86_64-linux

subdir          = sources/dfmc/c-run-time

VPATH           = $(srcdir)

LIBDEST         = $(OPEN_DYLAN_USER_INSTALL)/lib
BINDEST         = $(OPEN_DYLAN_USER_INSTALL)/bin
INCDEST         = $(OPEN_DYLAN_USER_INSTALL)/include
OBJDIR          = obj-$(OPEN_DYLAN_PLATFORM_NAME)

CC = gcc
AR = ar -rcs

ifeq ($(OPEN_DYLAN_PLATFORM_NAME),x86-darwin)
  PLATFORM_CFLAGS=-arch i386 -D_REENTRANT=1 \
    -DOPEN_DYLAN_PLATFORM_DARWIN -DOPEN_DYLAN_PLATFORM_UNIX
endif
ifeq ($(OPEN_DYLAN_PLATFORM_NAME),x86_64-darwin)
  PLATFORM_CFLAGS=-arch x86_64 -D_REENTRANT=1 \
    -DOPEN_DYLAN_PLATFORM_DARWIN -DOPEN_DYLAN_PLATFORM_UNIX
endif
ifeq ($(OPEN_DYLAN_PLATFORM_NAME),ppc-darwin)
  PLATFORM_CFLAGS=-arch ppc -D_REENTRANT=1 \
    -DOPEN_DYLAN_PLATFORM_DARWIN -DOPEN_DYLAN_PLATFORM_UNIX
endif
ifeq ($(OPEN_DYLAN_PLATFORM_NAME),x86_64-freebsd)
  PLATFORM_CFLAGS=-D_REENTRANT=1 \
    -DOPEN_DYLAN_PLATFORM_FREEBSD -DOPEN_DYLAN_PLATFORM_UNIX
endif
ifeq ($(OPEN_DYLAN_PLATFORM_NAME),x86_64-linux)
  PLATFORM_CFLAGS=-DGC_LINUX_THREADS=1 -D_REENTRANT=1 \
    -DOPEN_DYLAN_PLATFORM_LINUX -DOPEN_DYLAN_PLATFORM_UNIX
endif
ifeq ($(OPEN_DYLAN_PLATFORM_NAME),arm-linux)
  PLATFORM_CFLAGS=-DGC_LINUX_THREADS=1 -D_REENTRANT=1 \
    -DOPEN_DYLAN_PLATFORM_LINUX -DOPEN_DYLAN_PLATFORM_UNIX
endif

CFLAGS          = -W -Wall -O2 -g -fPIC -fno-strict-aliasing \
                  $(PLATFORM_CFLAGS) -I$(srcdir) \
                  $(GC_CFLAGS)

OBJS    = $(OBJDIR)/posix-threads.o \
          $(OBJDIR)/debug-print.o \
          $(OBJDIR)/run-time.o \
          $(OBJDIR)/trace.o

C_RUNTIME_LIBRARY = $(OBJDIR)/libdylan-c-runtime.a

RUNTIMELIBDEST  = $(LIBDEST)/runtime/c-$(OPEN_DYLAN_PLATFORM_NAME)

all: $(C_RUNTIME_LIBRARY)

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

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

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

$(RUNTIMELIBDEST):
	mkdir -p $(RUNTIMELIBDEST)

install-header: $(srcdir)/run-time.h $(INCDEST)
	cp -fp $(srcdir)/trace.h $(INCDEST)
	cp -fp $(srcdir)/run-time.h $(INCDEST)

install-runtime: $(C_RUNTIME_LIBRARY) $(RUNTIMELIBDEST)
	cp -fp $(C_RUNTIME_LIBRARY) $(RUNTIMELIBDEST)
	@ if [ "$(GC_USE_STATIC_BOEHM)" = "true" ]; then \
		cp -fp $(GC_LIBRARY) $(RUNTIMELIBDEST); \
	fi

install: install-runtime install-header

clean:
	rm -rf $(OBJDIR)

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

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