DLL = .pd_linux
shared = -shared

# Try to guess the host system type and figure out platform specifics.
host = $(shell ../../config.guess)
ifneq "$(findstring -mingw,$(host))" ""
# Windows
DLL = .dll
EXTRA_CFLAGS += -I/usr/local/include
ifneq "$(wildcard /c/Program*/Pd/bin/pd.dll)" ""
PDLIB = "$(wildcard /c/Program*/Pd/bin/pd.dll)"
else
PDLIB = -Wl,--enable-auto-import -lpd
endif
endif
ifneq "$(findstring -darwin,$(host))" ""
# OSX
DLL = .pd_darwin
shared = -dynamiclib -Wl,-undefined -Wl,dynamic_lookup
endif
ifneq "$(findstring x86_64-,$(host))" ""
# 64 bit, needs -fPIC flag
EXTRA_CFLAGS += -fPIC
endif
ifneq "$(findstring x86,$(host))" ""
# architecture-specific options for x86 and x86_64
EXTRA_CFLAGS += -msse -ffast-math
endif

EXTRA_CFLAGS += -I ../../pd

dspsrc  := $(wildcard *.dsp)
cppsrc  := $(dspsrc:.dsp=.cpp)
mods	:= $(dspsrc:%.dsp=%~$(DLL))
svg 	:= $(dspsrc:.dsp=.svg)
xml 	:= $(dspsrc:.dsp=.dsp.xml)
pd 	:= $(dspsrc:.dsp=.pd)
libs    := $(wildcard *.lib)

ARCH     = puredata.cpp
#FAUST2PD = faust2pd
FAUST2PD = ../../faust2pd
# faust2pd options (the -s forces additional number controls for each slider)
# a number of other options are supported, run faust2pd -h for help
F2PDFLAGS = -s

all: $(mods) $(pd)

cpp: $(cppsrc)

svg: $(svg)

xml: $(xml)

%~$(DLL): %.cpp
	$(CXX) $(shared) $(EXTRA_CFLAGS) $(CFLAGS) -Dmydsp=$(@:%~$(DLL)=%) $< -o $@ $(PDLIB)

%.cpp: %.dsp
	faust $(VEC) -a $(ARCH) $< -o $@

%.svg: %.dsp
	faust -svg $< -o /dev/null >/dev/null

%.dsp.xml: %.dsp
	faust -xml $< -o /dev/null

# synths

NVOICES = 2

organ.pd: organ.dsp.xml
	$(FAUST2PD) $(F2PDFLAGS) -n $(NVOICES) $<

subtractive.pd: subtractive.dsp.xml
	$(FAUST2PD) $(F2PDFLAGS) -n $(NVOICES) $<

karplusplus.pd: karplusplus.dsp.xml
	$(FAUST2PD) $(F2PDFLAGS) -n $(NVOICES) $<

# other dsps (effect units)

%.pd: %.dsp.xml
	$(FAUST2PD) $(F2PDFLAGS) $<

clean:
	rm -f $(mods) *~ *.a *.o

distclean:
	rm -f $(mods) $(cppsrc) *~ *.a *.o

clean-cpp:
	rm -f $(cppsrc)

clean-xml:
	rm -f $(xml)

clean-pd:
	rm -f $(pd)

clean-svg:
	rm -rf *-svg

realclean:
	rm -f $(mods) $(cppsrc) $(xml) $(pd) *~ *.a *.o
	rm -rf *-svg
