CURDIR1 = $(CURDIR)/..
CURDIR2 = $(CURDIR1)/..

CAMDIR  = $(CURDIR2)/pco.camera

ifndef PCORT
	PCORT = $(CURDIR2)/runtime
endif

ifeq "$(strip $(filter debug,$(MAKECMDGOALS)))" "debug"
    OUTDIR  = $(CURDIR2)/debug_lnx
    PREDEFS += -D_DEBUG
    PREDEFS += -O0 -g -fno-inline
    CXXFLAGS += -g
	DEB = debug
	CAMBIN  = $(CAMDIR)/debug_lnx
	CAM_O   = $(patsubst %.cpp,%.o,$(notdir $(wildcard $(CAMDIR)/*.cpp)))
	CAMERA  = $(addprefix $(CAMBIN)/,$(CAM_O))
else
    OUTDIR  = $(CURDIR2)/bin64
    PREDEFS += -O2
	DEB = 
	CAMBIN  = $(CAMDIR)/bin64
	CAM_O   = $(patsubst %.cpp,%.o,$(notdir $(wildcard $(CAMDIR)/*.cpp)))
	CAMERA  = $(addprefix $(CAMBIN)/,$(CAM_O))
endif


TARGET = SimpleExample

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

PCOINCDIR  = $(PCORT)/include
PCOLIBDIR  = $(PCORT)/bin64

INCLUDES = -I$(PCOINCDIR)

PCOLIB  = -lpco_sc2cam -lpco_recorder -lpco_convert
LIBADD = -lpthread -lstdc++fs

LFLAGS +=  -L$(PCOLIBDIR)
LIBADD_START = -Wl,--start-group 
LIBADD_END   = -Wl,--end-group
LINKRPATH = -Wl,-rpath,$(PCOLIBDIR)
LINKRPATH += -Wl,-rpath,'$$ORIGIN'
LINKRPATH += -Wl,-z,origin

all: dirs $(OUTDIR)/$(TARGET)

debug: dirs $(OUTDIR)/$(TARGET)

clean:
		$(RM) *.o *~ $(CURDIR2)/bin64/$(TARGET) $(CURDIR2)/bin64_debug/$(TARGET) $(CURDIR2)/bin64/*.log $(CURDIR2)/bin64_debug/*.log

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

# compile cam binaries if newer than target
$(CAMERA): $(CAMDIR)/Makefile
	@echo pco.$@:
	@cd $(CAMDIR) && $(MAKE)

$(OUTDIR)/$(TARGET): $(TARGET).cpp $(CAMERA)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(INCLUDES) $(LFLAGS) $(LINKRPATH) $(LIBADD_START) $(PCOLIB) $(LIBADD) $(LIBADD_END)
