#
# ====================================================================
#
# libffi Windows Makefile
#
#
# ====================================================================
#

NAME=ffi-3
VERSION=3.0.10
TARGET=X86_WIN32
CC=cl.exe
AS=ml.exe
RC=rc.exe
MT=mt.exe
AR="link.exe"
SO=.dll
OBJ=.obj
EXE=.exe
LA=.lib
LINK="link.exe"
PREFIX=
TOPDIR=.
CPP=cl.exe -EP
CFLAGS=-nologo -Zi -D_MD -W3 -DWIN32 -DWINNT -D_WIN32 -D_WINDOWS -D_WINNT -D_WIN32_WINNT=0x0501 -D_WIN32_IE=0x0600 -D_X86_=1 -DNDEBUG -MD -O2 -Ob2 -Oy-
LDFLAGS=/NOLOGO /MACHINE:X86
SHFLAGS=/DLL /INCREMENTAL:NO /DEBUG /SUBSYSTEM:WINDOWS /OPT:REF
ARFLAGS=/LIB
ASFLAGS=-coff -W3 -Cx -Zm -Di386 -DQUIET -D?QUIET
RCFLAGS=/l 0x409 -DNDEBUG
INCLUDES=-I . -I .\include -I .\src\x86 
LIBS=kernel32.lib advapi32.lib shell32.lib user32.lib gdi32.lib

VERSION_MAJOR=3
VERSION_MINOR=1
VERSION_PATCH=10rc

DESTDIR=$(PREFIX)
DESTBIN=$(PREFIX)\bin
DESTLIB=$(PREFIX)\lib
DESTINC=$(PREFIX)\include
SRCDIR=$(TOPDIR)\src

WORKDIR=$(TOPDIR)\.libs
SOLIBNAME=lib$(NAME)
LALIBNAME=libffi_convenience
STATICLIB=$(WORKDIR)\$(LALIBNAME)$(LA)

BUILDPDB = $(WORKDIR)\$(SOLIBNAME).pdb

HEADERS = \
	$(TOPDIR)\fficonfig.h

FFI_HEADERS = \
	$(TOPDIR)\include\ffi.h
	
OBJECTS = \
	$(SRCDIR)\closures.obj \
	$(SRCDIR)\debug.obj \
	$(SRCDIR)\java_raw_api.obj \
	$(SRCDIR)\prep_cif.obj \
	$(SRCDIR)\raw_api.obj \
	$(SRCDIR)\types.obj \
	$(SRCDIR)\x86\ffi.obj

!IF "$(TARGET)" == "X86_WIN32"
OBJECTS = $(OBJECTS) \
	$(SRCDIR)\x86\win32.obj
ASMSRCS = \
    $(SRCDIR)\x86\win32.asm
    
!ENDIF
!IF "$(TARGET)" == "X86_WIN64"
OBJECTS = $(OBJECTS) \
	$(SRCDIR)\x86\win64.obj
ASMSRCS = \
    $(SRCDIR)\x86\win64.asm

!ENDIF

.SUFFIXES : .S .asm

all: $(WORKDIR) $(STATICLIB)

.c$(OBJ):
	$(CC) -c $(CFLAGS) $(INCLUDES) -Fo$@ -Fd$(WORKDIR)\$(NAME)-src $<

.asm$(OBJ):
	$(AS) -c $(ASFLAGS) /Fo $@ $<

.S.asm:
	$(CPP) $(CFLAGS) $(INCLUDES) $< >$@

.rc.res:
	$(RC) $(RCFLAGS) /fo $@ $<

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

$(HEADERS) :
	-@if not exist $(HEADERS) copy $(HEADERS)w $(HEADERS)

$(FFI_HEADERS) :
	-@if not exist $(FFI_HEADERS) copy $(FFI_HEADERS).vc $(FFI_HEADERS)
	
$(STATICLIB): $(WORKDIR) $(FFI_HEADERS) $(HEADERS) $(ASMSRCS) $(OBJECTS)
	$(AR) $(ARFLAGS) $(LDFLAGS) /out:$(STATICLIB) @<<
	$(OBJECTS)
<<


!IF !DEFINED(PREFIX) || "$(PREFIX)" == ""

install:
	@echo ERROR: You must define installation PREFIX=destination

!ELSE

install: all
	-@xcopy "$(TOPDIR)\include\*.h" "$(DESTINC)\" /Y /Q
	-@xcopy "$(SRCDIR)\x86\ffitarget.h" "$(DESTINC)\" /Y /Q
	-@xcopy "$(WORKDIR)\$(LALIBNAME)$(LA)" "$(DESTLIB)\" /Y /Q
	-@xcopy "$(WORKDIR)\$(SOLIBNAME)$(LA)" "$(DESTLIB)\" /Y /Q
	-@xcopy "$(WORKDIR)\$(SOLIBNAME)$(SO)" "$(DESTBIN)\" /Y /Q
	-@xcopy "$(WORKDIR)\$(SOLIBNAME).pdb" "$(DESTLIB)\" /Y /Q

!ENDIF

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

distclean: clean
	-@del /Q $(HEADERS) 2>NUL
	-@del /Q $(TOPDIR)\include\ffi.h 2>NUL
	-@del /Q $(TOPDIR)\fficonfig.h 2>NUL
	-@del /Q $(TOPDIR)\Makefile.vc 2>NUL

