# Software License :
#
# Copyright (c) 2003-2015, The Open Effects Association Ltd. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright notice,
#       this list of conditions and the following disclaimer in the documentation
#       and/or other materials provided with the distribution.
#     * Neither the name The Open Effects Association Ltd, nor the names of its 
#       contributors may be used to endorse or promote products derived from this
#       software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

ifeq ($(CONFIG), debug)
  DEBUGFLAG = -g -DDEBUG -Wall -Wextra
  DEBUGNAME = debug
endif
ifeq ($(CONFIG), release)
  DEBUGFLAG = -O3 -DNDEBUG
  DEBUGNAME = release
endif

BITS := 32
ifeq ($(shell getconf LONG_BIT),64)
  BITS := 64
endif
OS := $(shell uname -s)
DEBUGFLAG ?= -g
DEBUGNAME ?= debug
ifeq ($(DEBUGFLAG),-O3)
  DEBUGNAME = release
endif

OBJECTPATH = $(OS)-$(BITS)-$(DEBUGNAME)

$(OBJECTPATH)/%.o : %.cpp
	mkdir -p $(OBJECTPATH)
	$(CXX) -c $(CXXFLAGS) $< -o $@

all: $(OBJECTPATH)/$(PLUGINNAME).ofx.bundle

  ifeq ($(OS),$(filter $(OS),MINGW64_NT-6.1 MINGW32_NT-6.1))
    LINKFLAGS = -shared -fvisibility=hidden -Xlinker --version-script=$(PATHTOROOT)/include/linuxSymbols -lopengl32
    ARCH = Win32
    BITSFLAG = -m32
    ifeq ($(BITS), 64)
      BITSFLAG = -m64
      ARCH = Win64
    endif
    LINKFLAGS := $(LINKFLAGS) $(BITSFLAG)
  endif
  ifeq ($(OS),Linux)
    # use $ORIGIN to link to bundled libraries first, see http://itee.uq.edu.au/~daniel/using_origin/
    LINKFLAGS = -shared -fvisibility=hidden -Xlinker --version-script=$(PATHTOROOT)/include/linuxSymbols -lGL -Wl,-rpath,'$$ORIGIN'/../../Libraries
    ARCH = Linux-x86
    BITSFLAG = -m32 -fPIC
    ifeq ($(BITS), 64)
      BITSFLAG = -m64 -fPIC
      ARCH = Linux-x86-64
    endif
    LINKFLAGS := $(LINKFLAGS) $(BITSFLAG)
  endif
  ifeq ($(OS),FreeBSD)
    LINKFLAGS = -L/usr/local/lib -shared -fvisibility=hidden -Xlinker --version-script=$(PATHTOROOT)/include/linuxSymbols -lGL -Wl,-rpath,'$$ORIGIN'/../../Libraries
    ARCH= FreeBSD-x86
    BITSFLAG = -m32 -fPIC
    ifeq ($(BITS), 64)
      BITSFLAG = -m64 -fPIC
      ARCH = FreeBSD-x86-64
    endif
    LINKFLAGS := $(LINKFLAGS) $(BITSFLAG)
  endif
  ifeq ($(OS),Darwin)
    ifeq ($(BITS), Universal)
      # Universal 32/64 is useful only on OSX 10.4 (i386/ppc), 10.5 (i386/ppc) and 10.6 (i386/x86_64).
      # OSX 10.6 (Snow Leopard) or later, build for i386/x86_64
      MACOSX := 10.6
      MACOSXSDK := 10.6
      ARCHFLAGS=-arch i386 -arch x86_64
      ifeq ($(shell uname -r | sed 's/^\(.*\)\..*\..*/\1/'), 8)
        # OSX 10.4 (Tiger)
        MACOSX := 10.4
        MACOSXSDK := 10.4u
        ARCHFLAGS=-arch i386 -arch ppc
      endif
      ifeq ($(shell uname -r | sed 's/^\(.*\)\..*\..*/\1/'), 9)
        # OSX 10.5 (Leopard)
        MACOSX := 10.5
        MACOSXSDK := 10.5
        ARCHFLAGS=-arch i386 -arch ppc
      endif
      BITSFLAG = -isysroot /Developer/SDKs/MacOSX$(MACOSXSDK).sdk $(ARCHFLAGS) -mmacosx-version-min=$(MACOSX) -fPIC
    endif
    LINKFLAGS = $(BITSFLAG) -bundle -fvisibility=hidden -exported_symbols_list $(PATHTOROOT)/include/osxSymbols -framework OpenGL -Wl,-rpath,@loader_path/../Frameworks -Wl,-rpath,@loader_path/../Libraries
    ARCH = MacOS
  endif

  CXXFLAGS := $(DEBUGFLAG)  -I$(PATHTOROOT)/../include -I$(PATHTOROOT)/include $(BITSFLAG) -fvisibility=hidden $(CXXFLAGS_ADD)

$(OBJECTPATH)/$(PLUGINNAME).ofx: $(addprefix $(OBJECTPATH)/,$(PLUGINOBJECTS))
	mkdir -p $(OBJECTPATH)/
	$(CXX) $^ $(LINKFLAGS) $(LDFLAGS_ADD) -o $@

$(OBJECTPATH)/$(PLUGINNAME).ofx.bundle: $(OBJECTPATH)/$(PLUGINNAME).ofx
	mkdir -p  $@/Contents/$(ARCH)
	cp  $^  $@/Contents/$(ARCH)
	cp  Info.plist  $@/Contents/
	if [ -n "$(RESOURCES)" ]; then mkdir -p $@/Contents/Resources; cp $(RESOURCES)  $@/Contents/Resources; fi
	if [ $(DEBUGNAME) = "release" -a $(ARCH) = "MacOS" ]; then bash $(PATHTOROOT)/include/osxDeploy.sh $@ $(PLUGINNAME).ofx; fi

clean:
	rm -rf $(OBJECTPATH)/ $(PATHTOROOT)/Library/$(OBJECTPATH)/ 

release:
	make DEBUGFLAG=-O3

