# -*- makefile -*-
# ====================================================================
#
# libffi Windows Makefile
#
#
# ====================================================================
#
NAME	= ffi
TARGET	= @TARGET@
CC	= cl
!if "$(TARGET)" == "X86_WIN64"
AS	= ml64
!else
AS	= ml
!endif
AR	= link
DLEXT	= dll
OBJEXT	= obj
LIBEXT	= lib
TOPDIR	= @SRCDIR@
CPP	= $(CC) -EP
CFLAGS	= @CFLAGS@
ARFLAGS	= -lib
ASFLAGS	= -coff -W3 -Cx
INCLUDES= -I. -I./include -I./src/x86 \
	  -I$(TOPDIR)/include -I$(TOPDIR)/include/src/x86

SRCDIR	= $(TOPDIR)/src
WORKDIR	= ./.libs
BUILDDIR= ./src
LIBNAME	= lib$(NAME)
STATICLIB= $(WORKDIR)/$(LIBNAME)_convenience.$(LIBEXT)

HEADERS = \
	./fficonfig.h
FFI_HEADERS = \
	./include/ffi.h \
	./include/ffitarget.h

!if "$(TARGET)" == "X86_WIN32"
OSSRC	= win32
!else if "$(TARGET)" == "X86_WIN64"
OSSRC	= win64
!else
! error unknown target: $(TARGET)
!endif

OBJECTS = \
	$(BUILDDIR)/closures.$(OBJEXT) \
	$(BUILDDIR)/debug.$(OBJEXT) \
	$(BUILDDIR)/java_raw_api.$(OBJEXT) \
	$(BUILDDIR)/prep_cif.$(OBJEXT) \
	$(BUILDDIR)/raw_api.$(OBJEXT) \
	$(BUILDDIR)/types.$(OBJEXT) \
	$(BUILDDIR)/x86/ffi.$(OBJEXT) \
	$(BUILDDIR)/x86/$(OSSRC).$(OBJEXT)
ASMSRCS = \
	$(BUILDDIR)/x86/$(OSSRC).asm

.SUFFIXES : .S .asm

all: $(WORKDIR) $(STATICLIB)

{$(SRCDIR)}.c{$(BUILDDIR)}.$(OBJEXT):
	$(CC) -c $(CFLAGS) $(INCLUDES) -Fo$(@:\=/) -Fd$(WORKDIR)/$(NAME)-src $(<:\=/)

{$(SRCDIR)/x86}.c{$(BUILDDIR)/x86}.$(OBJEXT):
	$(CC) -c $(CFLAGS) $(INCLUDES) -Fo$(@:\=/) -Fd$(WORKDIR)/$(NAME)-src $(<:\=/)

{$(SRCDIR)/x86}.S{$(BUILDDIR)/x86}.asm:
	$(CPP) $(CFLAGS) $(INCLUDES) $(<:\=/) >$(@:\=/)

{$(BUILDDIR)/x86}.asm{$(BUILDDIR)/x86}.$(OBJEXT):
	cd $(@D) && $(AS) -c $(ASFLAGS) -Fo $(@F) $(<F)

$(BUILDDIR)/x86/$(OSSRC).asm: $(SRCDIR)/x86/$(OSSRC).S

$(OBJECTS): $(FFI_HEADERS) $(HEADERS)

$(WORKDIR):
	-@if not exist "$(WORKDIR:/=\)\$(NULL)" mkdir $(WORKDIR:/=\)

$(STATICLIB): $(WORKDIR) $(OBJECTS)
	$(AR) $(ARFLAGS) -out:$(STATICLIB) @<<
	$(OBJECTS)
<<

clean:
	-@del /Q $(OBJECTS:/=\) 2>NUL
	-@del /Q $(ASMSRCS:/=\) 2>NUL
	-@del /Q /S $(WORKDIR:/=\) 2>NUL

distclean: clean
	-@del /Q $(HEADERS:/=\) $(FFI_HEADERS:/=\) 2>NUL
	-@del /Q Makefile 2>NUL
