# 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
# command ´ld´ comes with wrong (Linux) standard library search
# paths).
LD = g++

#Define the ´dlltool´ command.
DLLTOOL = dlltool

#Define the resource compiler.
RC = windres

PERL=perl

# Define the ´copy´ command.
CP     = $(PERL) -MExtUtils::Command -e cp --

# Define the mkpath´ command.
MKPATH = $(PERL) -MExtUtils::Command -e mkpath --

# Define the ´remove´ command.
RM_F   = $(PERL) -MExtUtils::Command -e rm_f --

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

# Define some additional symboles needed for WIN32 based builds.
WIN32_CFLAGS = -DWINVER=0x0500 $(LIB_TYPE_FLAGS) -DOPJ_STATIC
WIN32_CXXFLAGS = $(WIN32_CFLAGS) -DLIBRAW_NODLL

# Workaround for LibRawLite, which does not include C++ header
# file stdexcept, which is casually included with MSVC but not
# with MinGW. This can be removed after LibRawLite got control
# over its includes again.
WIN32_CXXFLAGS += -include stdexcept 

# Define DLL image header information flags for the linker.
WIN32_LDFLAGS = -Wl,--subsystem,windows:5.0,--major-os-version,5

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 -DDISABLE_PERF_MEASUREMENT $(WIN32_CFLAGS)
CFLAGS += $(INCLUDE)
CXXFLAGS ?= -O3 -fexceptions -Wno-ctor-dtor-privacy -DNDEBUG $(WIN32_CXXFLAGS)
CXXFLAGS += $(INCLUDE)
RCFLAGS ?= -DNDEBUG
LDFLAGS ?= -s -shared -static -Wl,-soname,$(SOLIBNAME) $(WIN32_LDFLAGS)
DLLTOOLFLAGS ?= --add-stdcall-underscore

TARGET = freeimage
STATICLIB = lib$(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):
	$(MKPATH) $(DISTDIR)

$(TARGETDIST): $(DISTDIR)

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

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