# Shared make information between Build engine and games

MAKEFILE_SHARED:=$(lastword $(MAKEFILE_LIST))

ENGINELIB=libengine.a
EDITORLIB=libbuild.a


# SDK locations - adjust to match your setup
# Overrides must use absolute paths since this Makefile is included at different directory levels
#
DXROOT=$(abspath $(dir $(MAKEFILE_COMMON))../sdk/dx)
#DXROOT=/c/sdks/directx/dx8
SDLROOT=$(abspath $(dir $(MAKEFILE_COMMON))../sdk/SDL)

DXROOT_OVERRIDE ?=
SDLROOT_OVERRIDE ?=
ifneq ($(DXROOT_OVERRIDE),)
    DXROOT=$(DXROOT_OVERRIDE)
endif
ifneq ($(SDLROOT_OVERRIDE),)
    SDLROOT=$(SDLROOT_OVERRIDE)
endif

SDL_TARGET ?= 2
SDL_FRAMEWORK ?= 1

ifeq (1,$(strip $(shell expr $(GCC_MAJOR) \>= 4)))
    L_SSP := -lssp
endif

BUILDCOMMONFLAGS=$(ARCH)
BUILDLIBS=
BUILDLIBDIRS=

# NOTE: If your setup doesn't have libstdc++, you can try using libsupc++.
# Search for STDCPPLIB below and change it to -lsupc++.

ifeq ($(SUBPLATFORM),LINUX)
    RENDERTYPE=SDL
    MIXERTYPE=SDL
    BUILDCOMMONFLAGS+= -DHAVE_INTTYPES
    GTKCOMPAT32=0
    SDL_FRAMEWORK=0

    # On Linux, we don't need to specify libstdc++ manually, the linker will
    # presumably take care for us.
    STDCPPLIB:=

    ifeq ($(PLATFORM),GCW)
        override USE_OPENGL=0
        override NOASM=1
    endif
    ifeq ($(PLATFORM),DINGOO)
        override USE_OPENGL=0
        override NOASM=1
    endif

    ifeq ($(findstring x86_64,$(SYSARCH)),x86_64)
        ifeq (1,$(BUILD32_ON_64))
            # On my 64bit Gentoo these are the 32bit emulation libs
            BUILDLIBS+= -m32
            BUILDLIBDIRS+= -L/emul/linux/x86/usr/lib
            BUILDCOMMONFLAGS+= -m32
            # Override WITHOUT_GTK=0
            GTKCOMPAT32=1
        else
            override NOASM=1
        endif
    endif
endif
ifeq ($(PLATFORM),DARWIN)
    RENDERTYPE    = SDL
    MIXERTYPE     = SDL
    STDCPPLIB:=-lstdc++
    BUILDCOMMONFLAGS    += -DHAVE_INTTYPES
    GTKCOMPAT32    = 0

    ifeq (1,$(DARWIN9))
        BUILDCOMMONFLAGS += -DDARWIN9
    endif

    ifeq (1,$(BUILD32_ON_64))
        ARCH=-arch i386
    else
        # ASM won't work on PowerPC or x86_64
        override NOASM=1
    endif
endif
ifeq ($(PLATFORM),WINDOWS)
    # Windows types can be SDL or WIN
    RENDERTYPE?=SDL
    MIXERTYPE?=WIN
    ifneq ($(RENDERTYPE),SDL)
        ifeq ($(MIXERTYPE),SDL)
            MIXERTYPE:=WIN
        endif
    endif

    WITHOUT_GTK?=1
    SDLCONFIG=
    SDL_FRAMEWORK=1
    BUILDCOMMONFLAGS+= -DHAVE_INTTYPES
    EXESUFFIX=.exe
    BUILDLIBS+= -Wl,--enable-auto-import -lmingwex -lgdi32 -lcomctl32 -lwinmm $(L_SSP) -lwsock32 -lws2_32 -lshlwapi
    ifeq (0,$(CLANG))
        BUILDLIBS+= -mwindows
    endif
    #-lshfolder
    #BUILDLIBDIRS+= -L$(ENETROOT)
    STDCPPLIB:=-lstdc++
    ifeq ($(findstring x86_64,$(SYSARCH)),x86_64)
        ifneq (1,$(BUILD32_ON_64))
            override NOASM=1
        endif
    endif
endif
ifeq ($(PLATFORM),BSD)
    RENDERTYPE=SDL
    MIXERTYPE=SDL
    BUILDCOMMONFLAGS+= -DHAVE_INTTYPES
    ifneq ($(findstring i386,$(SYSARCH)),i386)
        override NOASM=1
    endif
    STDCPPLIB:=-lstdc++
endif
ifeq ($(PLATFORM),BEOS)
    RENDERTYPE=SDL
    MIXERTYPE=SDL
    STDCPPLIB:=-lstdc++
endif
ifeq ($(PLATFORM),SKYOS)
    RENDERTYPE=SDL
    MIXERTYPE=SDL
    EXESUFFIX=.app
    override NOASM=1
    BUILDCOMMONFLAGS+= -DUNDERSCORES -I/boot/programs/sdk/include/sdl
    SDLCONFIG=
    BUILDLIBS+= -lSDL -lnet
endif
ifeq ($(PLATFORM),WII)
    RENDERTYPE=SDL
    MIXERTYPE=SDL
    EXESUFFIX=.elf
    override USE_OPENGL=0
    override POLYMER=0
    override NOASM=1
    override WITHOUT_GTK=1
    # -msdata=eabi
    BUILDCOMMONFLAGS+= -g -mtune=750 -meabi -mhard-float \
        -DGEKKO -DHAVE_INTTYPES -D__POWERPC__ \
        -I$(LIBOGC_INC) -I$(PORTLIBS)/include
    SDLCONFIG=
    SDL_TARGET=1
    SDL_FRAMEWORK=1
    BUILDLIBS+= -lSDL_mixer -lSDL -laesnd_tueidj -lpng -lvorbisidec -lfat -lwiiuse -lbte -logc -lm -lwiikeyboard
    BUILDLIBDIRS += -L$(LIBOGC_LIB) -L$(PORTLIBS)/lib
endif
ifeq ($(PLATFORM),QNX)
    RENDERTYPE=SDL
    MIXERTYPE=SDL
    override USE_OPENGL=0
    override NOASM=1
    STDCPPLIB:=-lstdc++
    BUILDLIBS+= -lsocket
endif
ifeq ($(PLATFORM),SUNOS)
    RENDERTYPE=SDL
    MIXERTYPE=SDL
    override USE_OPENGL=0
    override NOASM=1
    STDCPPLIB:=-lstdc++
    BUILDLIBS+= -lsocket -lnsl
endif
ifeq ($(PLATFORM),SYLLABLE)
    RENDERTYPE=SDL
    MIXERTYPE=SDL
    override USE_OPENGL=0
    override NOASM=1
endif

ifneq (0,$(CLANG))
    ifneq (,$(STDCPPLIB))
        STDCPPLIB:=-Xlinker $(STDCPPLIB)
    endif
endif

ifeq ($(SDL_TARGET),2)
    SDLCONFIG ?= sdl2-config
    SDLNAME ?= SDL2
endif
ifeq ($(SDL_TARGET),1)
    SDLCONFIG ?= sdl-config
    SDLNAME ?= SDL
endif

ifneq ($(SDLCONFIG),)
    SDLVERSION:=$(strip $(shell $(SDLCONFIG) --version))
    ifneq ($(SDLVERSION),)
        SDLROOT:=$(strip $(shell $(SDLCONFIG) --prefix))
    endif
endif

SDL_STATIC?=1

ifeq ($(RENDERTYPE),SDL)
    BUILDCOMMONFLAGS += -DSDL_TARGET=$(SDL_TARGET)

    ifeq ($(SDL_FRAMEWORK),1)
        BUILDCOMMONFLAGS += -DSDL_FRAMEWORK
    endif

    ifneq ($(PLATFORM),WINDOWS)
        ifeq ($(SDL_FRAMEWORK),1)
            ifeq ($(PLATFORM),DARWIN)
                BUILDLIBS        += -Wl,-framework,SDL
                BUILDCOMMONFLAGS+= -I$(APPLE_FRAMEWORKS)/SDL.framework/Headers
            endif
        else
            ifneq ($(SDLCONFIG),)
                SDLCONFIG_COMMONFLAGS:=$(subst -Dmain=SDL_main,,$(strip $(shell $(SDLCONFIG) --cflags)))
                SDLCONFIG_LIBS:=$(strip $(shell $(SDLCONFIG) --libs))

                BUILDLIBS+= $(SDLCONFIG_LIBS)
                BUILDCOMMONFLAGS+= $(SDLCONFIG_COMMONFLAGS)
            endif
        endif
    else
        BUILDCOMMONFLAGS += -D_GNU_SOURCE=1
        ifneq ($(SDL_STATIC),0)
            ifneq ($(SDL_TARGET),1) # Since SDL2 is under the zlib license, link statically if possible.
                BUILDLIBS+= -static
            endif
        endif
        BUILDLIBS+= -l$(SDLNAME)main -l$(SDLNAME) -lmingw32 -lgdi32 -limm32 -lole32 -loleaut32 -lwinmm -lversion
    endif

    ifeq (1,$(WITHOUT_GTK))
        HAVE_GTK2=0
    else
        ifneq (No,$(shell pkg-config --exists gtk+-2.0 || echo No))
            HAVE_GTK2=1
            # On my 64bit Gentoo box I have Cairo enabled which means the libs list includes
            # -lpangocairo-1.0 and -lcairo, however the 32bit compatibility libraries don't
            # include cairo, so we need to filter out those -l switches in order to link
            ifneq ($(LINKED_GTK),0)
                ifeq ($(GTKCOMPAT32),1)
                    BUILDLIBS+= $(shell pkg-config --libs gtk+-2.0 | sed 's/\s-l\(pango\)\{0,1\}cairo\S*\s/ /g')
                else
                    BUILDLIBS+= $(shell pkg-config --libs gtk+-2.0)
                endif
            endif
        else
            HAVE_GTK2=0
        endif
    endif
endif
ifeq ($(RENDERTYPE),WIN)
    BUILDLIBS+= -ldxguid
endif

ifeq ($(PLATFORM),WINDOWS)
    ifeq ($(MIXERTYPE),WIN)
        ifeq ($(RENDERTYPE),SDL)
            BUILDLIBS+= -ldxguid_sdl
        endif
    endif
endif

BUILDCOMMONFLAGS+= -DRENDERTYPE$(RENDERTYPE)=1 -DMIXERTYPE$(MIXERTYPE)=1 $(W_STRICT_OVERFLOW)

ifneq (0,$(USE_OPENGL))
    BUILDCOMMONFLAGS+= -DUSE_OPENGL
endif
ifneq (0,$(NOASM))
    BUILDCOMMONFLAGS+= -DNOASM
endif
ifneq (0,$(USE_ASM64))
    BUILDCOMMONFLAGS+= -DUSE_ASM64
endif
ifneq (0,$(LINKED_GTK))
    BUILDCOMMONFLAGS+= -DLINKED_GTK
endif

ifneq (0,$(POLYMER))
 ifneq (0,$(USE_OPENGL))
  BUILDCOMMONFLAGS+= -DPOLYMER
 endif
endif

ifneq ($(EXESUFFIX_OVERRIDE),)
    EXESUFFIX=$(EXESUFFIX_OVERRIDE)
endif

STATICSTDCPP?=
ifeq (1,$(STATICSTDCPP))
    STATICSTDCPP= -static
endif
ifeq (0,$(STATICSTDCPP))
    STATICSTDCPP= -shared
endif


ifneq ($(PLATFORM),WINDOWS)
    ifneq ($(PLATFORM),WII)
        BUILDLIBS+= -ldl
    endif
    BUILDLIBS+= -pthread
endif

ifeq ($(PLATFORM),WINDOWS)
    ifneq ($(USE_LIBPNG),0)
        BUILDLIBS+= -lpng_mini -lz_mini
    endif
    BUILDLIBS+= -lcompat-from-mingw-w64
else
    ifeq ($(PLATFORM),DARWIN)
        ifneq ($(USE_LIBPNG),0)
            BUILDLIBS+= -lpng # -lz
        endif
    else
        ifneq ($(USE_LIBPNG),0)
            BUILDLIBS+= -lpng -lz
        endif
    endif
endif


ifeq ($(PRETTY_OUTPUT),1)
BUILD_SETTINGS_COMPILER = \033[1;36mcompiler: \033[0;36m\"$(COMPILER) $(OURCFLAGS)\"
BUILD_SETTINGS_ASSEMBLER = \033[1;36massembler: \033[0;36m\"$(AS) $(OURASFLAGS)\"
BUILD_SETTINGS_LINKER = \033[1;36mlinker: \033[0;36m\"$(PRINTLDFLAGS) $(OURLIBS) $(STATICSTDCPP) $(STDCPPLIB)\"
ifeq (0,$(NOASM))
    BUILD_SETTINGS = printf "$(BUILD_SETTINGS_COMPILER)\n$(BUILD_SETTINGS_ASSEMBLER)\n$(BUILD_SETTINGS_LINKER)\033[0m\n"
else
    BUILD_SETTINGS = printf "$(BUILD_SETTINGS_COMPILER)\n$(BUILD_SETTINGS_LINKER)\033[0m\n"
endif
BUILD_STARTED = printf "\033[K\033[1;36mBuild started using:\033[0m\n"; $(BUILD_SETTINGS)
BUILD_ECHOFLAGS = printf "\033[K\033[1;36mEnded compilation in this directory using:\n$(BUILD_SETTINGS_COMPILER)\033[0m\n"
BUILD_FINISHED = printf "\033[K\033[1;36mBuild successful:\033[0m\n"
COMPILE_STATUS = printf "\033[K\033[0mBuilding object \033[1m$@\033[0m...\033[0m\r"
ONESTEP_STATUS = printf "\033[K\033[0mBuilding executable \033[1m$@\033[0m...\033[0m\r"
COMPILE_OK = printf "\033[K\033[0;32mBuilt object \033[1;32m$@\033[0;32m \033[0m\n"
ONESTEP_OK = printf "\033[K\033[0;32mBuilt executable \033[1;32m$@\033[0;32m \033[0m\n"
COMPILE_FAILED = printf "\033[K\033[0;31mFailed building \033[1;31m$@\033[0;31m from\033[0m \033[1;31m$<\033[0;31m!\033[0m\n"; exit 1
ONESTEP_FAILED = printf "\033[K\033[0;31mFailed building executable \033[1;31m$@\033[0;31m!\033[0m\n"; exit 1
ARCHIVE_STATUS = printf "\033[K\033[0mCreating library archive \033[1m$@\033[0m...\033[0m\r"
ARCHIVE_OK = printf "\033[K\033[0;32mCreated library archive \033[1;32m$@\033[0;32m \033[0m\n"
ARCHIVE_FAILED = printf "\033[K\033[0;31mFailed creating library archive \033[1;31m$@\033[0;31m from\033[0m \033[1;31m$<\033[0;31m!\033[0m\n"; exit 1
LINK_STATUS = printf "\033[K\033[0;0mLinking executable \033[1m$@\033[0;0m...\033[0m\r"
LINK_OK = printf "\033[K\033[0;32mLinked executable \033[1;32m$@\033[0;32m \033[0m\n"
LINK_FAILED = printf "\033[K\033[0;31mFailed linking executable \033[1;31m$@\033[0;31m!\033[0m\n"; exit 1
else
BUILD_STARTED = 
BUILD_ECHOFLAGS =
BUILD_FINISHED = 
COMPILE_STATUS = 
ONESTEP_STATUS = 
COMPILE_OK = true
ONESTEP_OK = true
COMPILE_FAILED = false; exit 1
ONESTEP_FAILED = false; exit 1
ARCHIVE_STATUS = 
ARCHIVE_OK = true
ARCHIVE_FAILED = false; exit 1
LINK_STATUS = 
LINK_OK = true
LINK_FAILED = false; exit 1
endif
