# MinGW makefile for FreeImage

# This file can be generated by ./gensrclist.sh
include Makefile.srcs

# General configuration variables:
DESTDIR ?= $(SystemRoot)
INSTALLDIR ?= $(DESTDIR)/system32
DISTDIR ?= Dist
SRCDIR ?= Source
HEADER = FreeImage.h
RCFILE = FreeImage.rc

# Uncomment this variable to make a static library. This may
# also be specified as an environment variable and can hold
# any of STATIC and SHARED and must be in uppercase letters.
# Default: SHARED
#FREEIMAGE_LIBRARY_TYPE = STATIC

# Redefine the compiler (CC defaults to ´cc´ for MinGW's make, however
# there's only ´gcc´ available with MinGW)
CC = gcc

# Redefine the linker (we use ´g++´ for linking, since MinGW's ´ld´ comes with
# wrong (Linux) standard library search paths)
LD = g++

#Define the dlltool
DLLTOOL = dlltool

#Define the resource compiler
RC = windres

# Define the copy command
CP = cp

# Define the mkdir command
MD = mkdir

# Define additional libraries needed:
# libstdc++ is included by default with MinGW, however for
# WIN32 based builds, LibRawLite needs the winsock libraries.
LIBRARIES = -lwsock32 -lws2_32

# Define some additional symboles only needed for WIN32 based builds:
WIN32_CFLAGS = $(LIB_TYPE_FLAGS) -DOPJ_STATIC
WIN32_CXXFLAGS = $(WIN32_CFLAGS) -DLIBRAW_NODLL -DLIBRAW_LIBRARY_BUILD

WIN32_STATIC_FLAGS = -DFREEIMAGE_LIB
WIN32_SHARED_FLAGS = -DFREEIMAGE_EXPORTS

MODULES = $(SRCS:.c=.o)
MODULES := $(MODULES:.cpp=.o)
RESOURCE = $(RCFILE:.rc=.coff)
CFLAGS ?= -O3 -fexceptions -DNDEBUG $(WIN32_CFLAGS)
CFLAGS += $(INCLUDE)
CXXFLAGS ?= -O3 -fexceptions -Wno-ctor-dtor-privacy -DNDEBUG $(WIN32_CXXFLAGS) -DNO_LCMS
CXXFLAGS += $(INCLUDE)
RCFLAGS ?= -DNDEBUG
LDFLAGS = -s -shared -static -Wl,-soname,$(SOLIBNAME)
DLLTOOLFLAGS = --add-stdcall-underscore

TARGET = FreeImage
STATICLIB = $(TARGET).a
SHAREDLIB = $(TARGET).dll
IMPORTLIB = $(TARGET).lib
EXPORTLIB = $(TARGET).exp
SOLIBNAME = $(SHAREDLIB).$(VER_MAJOR)

DISTSHARED = $(addprefix $(DISTDIR)/, $(SHAREDLIB) $(IMPORTLIB) $(HEADER))
DISTSTATIC = $(addprefix $(DISTDIR)/, $(STATICLIB) $(HEADER))

# The FreeImage library type defaults to SHARED
FREEIMAGE_LIBRARY_TYPE ?= SHARED

TARGETLIB = $($(FREEIMAGE_LIBRARY_TYPE)LIB)
TARGETDIST = $(DIST$(FREEIMAGE_LIBRARY_TYPE))
LIB_TYPE_FLAGS = $(WIN32_$(FREEIMAGE_LIBRARY_TYPE)_FLAGS)

default: all

all: mkdist

rebuild: clean all

mkdist: FreeImage $(TARGETDIST)

FreeImage: $(TARGETLIB)

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

%.coff: %.rc
	$(RC) $(RCFLAGS) -o $@ $<

$(DISTDIR)/%: %
	$(CP) $< $@

$(DISTDIR)/%: $(SRCDIR)/%
	$(CP) $< $@

$(STATICLIB): $(MODULES)
	$(AR) rs $@ $(MODULES)

$(IMPORTLIB) $(EXPORTLIB): $(MODULES) 
	$(DLLTOOL) -e $(EXPORTLIB) -l $(IMPORTLIB) -D $(SHAREDLIB) $(DLLTOOLFLAGS) $(MODULES)

$(SHAREDLIB): $(EXPORTLIB) $(RESOURCE)
	$(LD) $(LDFLAGS) -o $@ $(EXPORTLIB) $(MODULES) $(RESOURCE) $(LIBRARIES)

$(DISTDIR):
	$(MD) $(DISTDIR)

$(TARGETDIST): $(DISTDIR)

install:
	$(CP) $(SHAREDLIB) $(INSTALLDIR)

clean:
	$(RM) core $(DISTDIR)/*.* $(MODULES) $(RESOURCE) $(STATICLIB) $(SHAREDLIB) $(IMPORTLIB) $(EXPORTLIB)
