# Makefile for programs in this directory
CURDIR1 = $(CURDIR)/..

ifndef PCOSDK
 PCOSDK = /opt/pco/pco.sdk
endif

ifeq "$(strip $(filter debug,$(MAKECMDGOALS)))" "debug"
  PREDEFS := -g -O0 -fno-inline -D_DEBUG
  OUTDIR   = $(CURDIR1)/bin64_debug
else
  PREDEFS := -O2
  OUTDIR   = $(CURDIR1)/bin64
endif

OUTDIRD  = $(CURDIR1)/bin64_debug

PREDEFS += -DPCO_LINUX
PCOINCDIR  = $(PCOSDK)/include
PCOLIBDIR  = $(PCOSDK)/bin64


INCLUDES = -I$(PCOINCDIR)

CXXFLAGS += $(PREDEFS) -std=c++17 -Wall 

HEADERS = $(PCOINCDIR)/sc2_camexport.h $(PCOINCDIR)/pco_camexport.h
PCOLIB  = -lpco_sc2cam
LIBADD = -lpthread 

#add filefunctions using libtiff
ifdef FILEFUNC
 PREDEFS += -DFILEFUNCTION
 INCLUDES += -I$(CURDIR1)/libtiff_header
 LFLAGS += -L$(CURDIR1)/libtiff_header
 LIBADD += -lstdc++fs -ltiff
endif

LFLAGS +=  -L$(PCOLIBDIR)
LIBADD_START = -Wl,--start-group
LIBADD_END   = -Wl,--end-group

LINKRPATH += -Wl,-rpath,'$$ORIGIN'
LINKRPATH += -Wl,-z,origin
LINKRPATH += -Wl,-rpath,$(PCOLIBDIR)

TARGET  = Grab_AddBufferEx
TARGET += Grab_AddBufferExtern
TARGET += Grab_GetImageEx
TARGETS = $(addprefix $(OUTDIR)/,$(TARGET))


all: dirs $(TARGETS)

debug: predebug dirs $(TARGETS)


predebug:
		@echo build debug

clean:
		$(RM) *.o *~ $(TARGETS) $(addprefix $(OUTDIRD)/,$(TARGET))

dirs:
		@if [ ! -d $(OUTDIR) ]; then \
		echo create directory $(OUTDIR); \
		mkdir $(OUTDIR); \
		fi

$(OUTDIR)/%:	$(CURDIR)/%.cpp $(HEADERS) Makefile
		$(CXX) $(CXXFLAGS) $< -o $@ $(INCLUDES) $(LFLAGS) $(LINKRPATH) $(LIBADD_START) $(PCOLIB) $(LIBADD) $(LIBADD_END)
