# You will probably need to edit this file to get everything to compile.
# You may want to review the SharedLink action in Jamshared as well.
#

BUILD ?= RELEASE ;
USESDL ?= yes ;
USEBABEL ?= yes ;
MAC_USEHOMEBREW ?= no ;
MACOS_MIN_VER ?= 10.7 ;
BUNDLEFONTS ?= yes ;
USETTS ?= no ;

# jam -sGUILIB=EFL
GUILIB ?= gtk+ ;

# jam -sGARGLKINI=/usr/local/etc/garglk.ini
GARGLKINI ?= /etc/garglk.ini ;

# jam -sGARGLKPRE=gargoyle-
GARGLKPRE ?= "" ;

CC  ?= gcc ;
CXX ?= g++ ;

if $(CROSS)
{
    MINGWARCH ?= i386-mingw32 ;
    CROSS   = "(cross-compiling)" ;
    OS      = MINGW ;
    SUFEXE  = .exe ;
    CC      = $(MINGWARCH)-gcc ;
    C++     = $(MINGWARCH)-g++ -std=c++0x ;
    CXX     = $(C++) ;
    AR      = $(MINGWARCH)-ar cru ;
    RANLIB  = $(MINGWARCH)-ranlib ;
    LINK    = $(CC) ;
}

#cross compiling for Open Inkpot
#jam -sIPLINUXARCH=mipsel-ip-linux-gnu
if $(IPLINUXARCH)
{
    OS      = IPLINUX ;
    GUILIB  = EFL ;
    USESDL  = no ;
    CC      = $(IPLINUXARCH)-gcc ;
    C++     = $(IPLINUXARCH)-g++ ;
    CXX     = $(IPLINUXARCH)-g++ ;
    AR      = $(IPLINUXARCH)-ar cru ;
    RANLIB  = $(IPLINUXARCH)-ranlib ;
    LINK    = $(CC) ;
}

switch $(OS)
{
    case LINUX :
        PLATFORM = UNIX ;
    case NETBSD :
        PLATFORM = UNIX ;
    case FREEBSD :
        PLATFORM = UNIX ;
        CC = clang ;
        C++ = clang++ ;
        CXX = clang++ ;
    case OPENBSD :
        PLATFORM = UNIX ;
        CC = clang ;
        C++ = clang++ ;
        CXX = clang++ ;
    case * :
        PLATFORM = $(OS) ;
}

switch $(PLATFORM)
{
    case MINGW :
        Echo "OS is MINGW (win32) $(CROSS)" ;
        SUFDLL  = .dll ;
        LINKLIBS = -lcomdlg32 -lgdi32 -lwinmm -lm ;
        SHRLINKLIBS = -lcomdlg32 -lgdi32 -lwinmm -lm ;
        SHRLINKLIBS += $(TOP)/support/freetype/libfreetype-6.dll ;
        SHRLINKLIBS += $(TOP)/support/libjpeg/libjpeg-8.dll ;
        SHRLINKLIBS += $(TOP)/support/libpng/libpng16-16.dll ;
        SHRLINKLIBS += $(TOP)/support/zlib/zlib1.dll ;

        if $(BUILD) = RELEASE || $(BUILD) = TURBO
        {
            SHRLINKLIBS += -mwindows ;
            LINKLIBS += -mwindows ;
        }

        if $(USESDL) = yes
        {
            SHRLINKLIBS += $(TOP)/support/sdl/SDL_mixer.dll ;
            SHRLINKLIBS += $(TOP)/support/sdl/SDL.dll ;
        }

        if $(USETTS) = yes
        {
            SHRLINKLIBS += -lsapi -lole32 ;
            LINKLIBS += -lsapi -lole32 ;
        }


    case UNIX :
        Echo "OS is $(OS) ($(GUILIB))" ;
        if $(GUILIB) = EFL {
            PKGCONFIG = "pkg-config freetype2 evas ecore ecore-evas elementary fontconfig" ;
        } else {
            PKGCONFIG = "pkg-config freetype2 gtk+-x11-2.0 gdk-x11-2.0 gobject-2.0 glib-2.0 fontconfig" ;
        }

        if $(USETTS) = yes {
           PKGCONFIG = "$(PKGCONFIG) speech-dispatcher" ;
        }

        PKGCONFIG = "$(PKGCONFIG) libpng zlib" ;

        if $(USESDL) = yes
        {
            PKGCONFIG = "$(PKGCONFIG) sdl SDL_mixer" ;
        }

        # Setting C++ standard version to use for current g++ compilers.
        C++FLAGS = -std=c++11 ;
        GARGLKCCFLAGS = "`$(PKGCONFIG) --cflags`" -fPIC -std=gnu99 ;

        # Not all supported platforms include a pkg-config file for libjpeg.
        GARGLKLIBS = "`$(PKGCONFIG) --libs`" -ljpeg ;
        LINKLIBS = -lm "`$(PKGCONFIG) --libs`" ;

        # SDL_sound does not include a pkg-config file.
        if $(USESDL) = yes
        {
            GARGLKLIBS += -lSDL_sound ;
            LINKLIBS += -lSDL_sound ;
        }

        if $(STATIC) { LINKLIBS += $(GARGLKLIBS) ; }
        else      { SHRLINKLIBS += $(GARGLKLIBS) ; }


    case IPLINUX :
        Echo "OS is IPLINUX (EFL)" ;
        PKGCONFIG = "PKG_CONFIG_PATH=/usr/$(IPLINUXARCH)/lib/pkgconfig pkg-config freetype2 fontconfig libkeys libeoi eina-0 evas ecore ecore-x ecore-file ecore-evas edje" ;
        GARGLKCCFLAGS = "`$(PKGCONFIG) --cflags`" -fPIC ;
        GARGLKLIBS = "`$(PKGCONFIG) --libs`" -ljpeg -lpng -lm -lrt ;
        LINKLIBS = -lz -lm "`$(PKGCONFIG) --libs`" ;

        if $(USESDL) = yes
        {
            GARGLKCCFLAGS += -I/usr/include/SDL ;
            GARGLKLIBS += -lSDL_mixer -lSDL_sound -lSDL ;
        }

        if $(STATIC) { LINKLIBS += $(GARGLKLIBS) ; }
        else      { SHRLINKLIBS += $(GARGLKLIBS) ; }


    case SOLARIS :
        Echo "OS is SOLARIS (gtk+)" ;
        PKGCONFIG = "pkg-config freetype2 gtk+-x11-2.0 gdk-x11-2.0 gobject-2.0 glib-2.0 fontconfig" ;
        GARGLKCCFLAGS = "`$(PKGCONFIG) --cflags`" -fPIC ;
        GARGLKLIBS = "`$(PKGCONFIG) --libs`" -ljpeg -lpng -lz ;
        LINKLIBS = -lz -lm "`$(PKGCONFIG) --libs`" ;

        if $(USESDL) = yes
        {
            GARGLKCCFLAGS += -I/usr/include/SDL ;
            GARGLKLIBS += -lSDL_mixer -lSDL -lsmpeg -lvorbisfile ;
        }

        if $(STATIC) { LINKLIBS += $(GARGLKLIBS) ; }
        else      { SHRLINKLIBS += $(GARGLKLIBS) ; }


    case MACOSX :
        Echo "OS is MACOSX (cocoa)" ;
        SUFDLL = .dylib ;
        MAINARCH = -arch x86_64 -mmacosx-version-min=$(MACOS_MIN_VER) ;
        AR = "libtool -static -o" ;
        RM = "touch -c" ;
        CCFLAGS = -Wno-pointer-sign $(MAINARCH) ;
        PKGCONFIG = "pkg-config freetype2 libpng" ;

        GARGLKCCFLAGS = "`$(PKGCONFIG) --cflags`" ;
        SHRLINKLIBS += "`$(PKGCONFIG) --libs`" -lz ;
        # Homebrew's JPEG library package is missing some configuration files.
        if $(MAC_USEHOMEBREW) = yes
        {
            GARGLKCCFLAGS += "-I`brew --prefix jpeg`/include" ;
            SHRLINKLIBS += "-L`brew --prefix jpeg`/lib" ;
        }
        SHRLINKLIBS += -ljpeg ;

        LINKLIBS = "`$(PKGCONFIG) --libs`" -lz ;
        FRAMEWORKS = -framework Cocoa -framework OpenGL ;

        if $(USESDL) = yes
        {
            PKGCONFIG = "pkg-config SDL_mixer sdl vorbisfile" ;
            GARGLKCCFLAGS += "`$(PKGCONFIG) --cflags`" ;
            SHRLINKLIBS += "`$(PKGCONFIG) --libs`" -lsmpeg ;
        }

        LINK = $(CC) -headerpad_max_install_names $(FRAMEWORKS) $(MAINARCH) ;


    case * :
        Exit "Unknown target. Please edit 'Jamrules'." ;
}

switch $(BUILD)
{
    case DEBUG :
        Echo "BUILD is DEBUG" ;
        OPTIM = -g ;

    case RELEASE :
        Echo "BUILD is RELEASE" ;
        OPTIM = -O2 $(CFLAGS) ;
        LINKFLAGS = $(LDFLAGS) ;
        SHRLINKFLAGS = $(LDFLAGS) ;

    case PROFILE :
        Echo "BUILD is PROFILE" ;
        OPTIM = -g -pg ;
        LINKFLAGS = -pg ;

    case * :
        Exit "Unknown build:" $(BUILD) ;
}

#
# To compile windows resource files
#

rule UserObject
{
    switch $(>)
    {
    case *.rc : WindRes $(<) : $(>) ;
    case *.m : Cc $(<) : $(>) ;
    case * : Echo "unknown suffix on" $(>) ;
    }
}

rule WindRes
{
    Depends $(<) : $(>) ;
    Clean clean : $(<) ;
}

actions WindRes { $(MINGWARCH)-windres -i $(>) -o $(<) --include-dir=$(>:D) }

#
# You shouldn't need to touch anything from here on.
#

HDRS = [ FDirName $(TOP) garglk ] ;
BTOP = [ FDirName $(TOP) build $(OS:L).$(BUILD:L) ] ;

rule ObjDir
{
    LOCATE_TARGET = [ FDirName $(BTOP) $(1) ] ;
}

include [ FDirName $(TOP) Jamshared ] ;

if $(OS) = MINGW
{
    LINKLIBS += $(BTOP)/garglk/icons.o ;
}

rule FPrependRoot
{
    # FPrependRoot prefix : path ;
    #
    # Safely prepend a prefix to a path
    # Use to e.g. install files into a build root

    # FDirName does not allow adding a prefix to a root path
    # ( e.g. [ FDirName /some/path /usr/bin ] -> /usr/bin )
    #
    # Care must be taken not to allow doubled separators in the path:
    # this causes MkDir to fail to traverse up the directory tree
    # and try to create the same directory twice

    local _prefix = [ FDirName $(<) ] ;
    local _path = [ FDirName $(>) ] ;
    local _full ;

    if $(_path) = $(_path:R=$(SLASH))
    {
        # _path is rooted
        _full = $(_prefix) $(_path) ;
    }
    else
    {
        # _path is relative
        if $(_path) = $(DOT)
        {
            _full = $(_prefix) ;
        }
        else
        {
            _full = $(_prefix) $(SLASH)$(_path) ;
    }
    }

    return $(_full:J=) ;

}
