# flext - C++ layer for Max/MSP and pd (pure data) externals
# Copyright (c) 2001-2003 Thomas Grill (xovo@gmx.net)
#
# Makefile for gcc @ linux
#
# usage:
# to build run "make -f makefile.pd-linux"
# to install (as root), do "make -f makefile.pd-linux install"
#

CONFIG=config-pd-linux.txt

include ${CONFIG}

# compiler+linker stuff 
INCLUDES=${PDPATH}

# general compiler flags
FLAGS=-DFLEXT_SYS=2 ${UFLAGS}

# compiler flags for optimized build
CFLAGS=-O2 

# compiler flags for debug build
CFLAGS_D=-g -DFLEXT_DEBUG      

# additional flags for threading
CFLAGS_T=-DFLEXT_THREADS  

# additional flags for shared library
CFLAGS_S=-DFLEXT_SHARED -DFLEXT_EXPORTS

# libraries
# it seems like libstdc++ must be defined for icc....
LIBS=stdc++

# ----------------------------------------------
# the rest can stay untouched
# ----------------------------------------------

NAME=flext
SRCDIR=./source

# all the source files from the package
include make-files.txt

ifdef SNDOBJ
INCLUDES+=${SNDOBJ}
SRCS+=${SRCS_SNDOBJ}
HDRS+=${HDRS_SNDOBJ}
LIBS+=sndobj
endif

ifdef STK
INCLUDES+=${STK}
SRCS+=${SRCS_STK}
HDRS+=${HDRS_STK}
LIBS+=stk
endif

MAKEFILE=makefile.pd-linux

# invoke dynamic linking
ifeq ($(CXX),icc)
DYNAMIC=-i_dynamic
else
DYNAMIC=-Wl,-Bdynamic
endif


TARGET=${TARGDIR}/lib${NAME}.a 
TARGET_D=${TARGDIR}/lib${NAME}_d.a
TARGET_T=${TARGDIR}/lib${NAME}_t.a
TARGET_TD=${TARGDIR}/lib${NAME}_td.a
TARGET_S=${TARGDIR}/lib${NAME}.so
TARGET_SD=${TARGDIR}/lib${NAME}_d.so


all: $(TARGDIR) $(TARGET) $(TARGET_D) $(TARGET_T) $(TARGET_TD) $(TARGET_S) $(TARGET_SD)

$(TARGDIR):
	mkdir $(TARGDIR)

$(patsubst %,$(SRCDIR)/%,$(SRCS)): $(patsubst %,$(SRCDIR)/%,$(HDRS)) $(patsubst %,$(SRCDIR)/%,$(IHDRS)) $(MAKEFILE) $(CONFIG)
	touch $@

$(TARGDIR)/%.ro : $(SRCDIR)/%.cpp
	$(CXX) -c $(CFLAGS) $(FLAGS) $(patsubst %,-I%,$(INCLUDES) $(SRCDIR)) $< -o $@

$(TARGDIR)/%.do : $(SRCDIR)/%.cpp
	$(CXX) -c $(CFLAGS_D) $(FLAGS) $(patsubst %,-I%,$(INCLUDES) $(SRCDIR)) $< -o $@

$(TARGDIR)/%.tro : $(SRCDIR)/%.cpp
	$(CXX) -c $(CFLAGS) $(CFLAGS_T) $(FLAGS) $(patsubst %,-I%,$(INCLUDES) $(SRCDIR)) $< -o $@

$(TARGDIR)/%.tdo : $(SRCDIR)/%.cpp
	$(CXX) -c $(CFLAGS_D) $(CFLAGS_T) $(FLAGS) $(patsubst %,-I%,$(INCLUDES) $(SRCDIR)) $< -o $@

$(TARGDIR)/%.sro : $(SRCDIR)/%.cpp
	$(CXX) -shared -c $(CFLAGS) $(CFLAGS_S) $(FLAGS) $(patsubst %,-I%,$(INCLUDES) $(SRCDIR)) $< -o $@

$(TARGDIR)/%.sdo : $(SRCDIR)/%.cpp
	$(CXX) -shared -c $(CFLAGS_D) $(CFLAGS_S) $(FLAGS) $(patsubst %,-I%,$(INCLUDES) $(SRCDIR)) $< -o $@




$(TARGET) : $(patsubst %.cpp,$(TARGDIR)/%.ro,$(SRCS)) 
	ar rc $@ $^
	chmod 644 $@

$(TARGET_D) : $(patsubst %.cpp,$(TARGDIR)/%.do,$(SRCS)) 
	ar rc $@ $^ 
	chmod 644 $@

$(TARGET_T) : $(patsubst %.cpp,$(TARGDIR)/%.tro,$(SRCS))
	ar rc $@ $^
	chmod 644 $@

$(TARGET_TD) : $(patsubst %.cpp,$(TARGDIR)/%.tdo,$(SRCS))
	ar rc $@ $^
	chmod 644 $@

$(TARGET_S) : $(patsubst %.cpp,$(TARGDIR)/%.sro,$(SRCS))
	$(CXX) -shared $(DYNAMIC) -Wl,--strip-debug $(patsubst %,-L%,$(LIBPATH)) -o $@ $^ $(patsubst %,-l%,$(LIBS)) 
	chmod 755 $@

$(TARGET_SD) : $(patsubst %.cpp,$(TARGDIR)/%.sdo,$(SRCS))
	$(CXX) -shared $(DYNAMIC) $(patsubst %,-L%,$(LIBPATH)) -o $@ $^ $(patsubst %,-l%,$(LIBS)) 
	chmod 755 $@


.PHONY: clean install

clean:
	rm -f $(TARGDIR)/*.{ro,do,tro,tdo,sro,sdo} $(TARGET) $(TARGET_D) $(TARGET_T) $(TARGET_TD) $(TARGET_S) $(TARGET_SD)
	
ifdef INSTDIR
$(INSTDIR):
	-mkdir $(INSTDIR)

install:: $(INSTDIR)
endif

install:: $(TARGET) $(TARGET_D) $(TARGET_T) $(TARGET_TD) $(TARGET_S) $(TARGET_SD)  $(patsubst %,$(SRCDIR)/%,$(HDRS))
	cp $^ $(INSTDIR)

	#make compatibility links for old-style naming
	for i in $(foreach f,$(notdir $(filter %.a,$^)),$(patsubst lib%.a,%,$(f))); do rm -f $(INSTDIR)/$$i.a && ln -sf lib$$i.a $(INSTDIR)/$$i.a; done 

	chmod a+r $(patsubst %,$(INSTDIR)/%,$(notdir $^))
	chown root.users $(patsubst %,$(INSTDIR)/%,$(notdir $^))