#
#
#

VPATH = ../obj:./
OBJ_DIR = ../obj
BIN_DIR = ../bin
PDEXEC = $(BIN_DIR)/pd



INSTALL_PREFIX = @prefix@
GFLAGS = -DINSTALL_PREFIX=\"$(INSTALL_PREFIX)\"

# ALSA compilation

SOUND_ALSA = @alsa@

# RME compilation

SOUND_RME = @rme@


CC = @CC@ 
INCLUDE = -I.
GLIB = @LIBS@

LDFLAGS = -Wl,-export-dynamic
LIB =  @PDLIB@

#select either the DBG and OPT compiler flags below:
OPT_CFLAGS = -funroll-loops -fomit-frame-pointer
WARN_CFLAGS = -Wall -W -Wstrict-prototypes \
    -Wno-unused -Wno-parentheses -Wno-switch
ARCH_CFLAGS = -DPD -DUNIX 

CFLAGS = @CFLAGS@ $(ARCH_CFLAGS) $(WARN_CFLAGS) $(OPT_CFLAGS)

# you might want ALSA linked in non-shared because
# many Linux machines don't have the ALSA shared library.  To link
# ALSA non-shared, move the # sign below.

ifeq (${SOUND_ALSA},yes)
CFLAGS += -DHAVE_ALSA
LIB += /usr/lib/libasound.a
#LIB += -lasound
endif

ifeq (${SOUND_RME},yes)
CFLAGS += -DHAVE_RME
endif


# Which system

SYSTEM = $(shell uname -m)

ifeq (${SYSTEM},alpha)
LIB += -lffm -lm
CFLAGS += -mieee -mcpu=ev56
endif




SYSSRC = s_linux.c s_linux_midiparse.c s_linux_midi_io.c 

SRC = g_canvas.c g_graph.c g_text.c g_rtext.c g_array.c g_template.c g_io.c \
    g_scalar.c g_traversal.c \
    m_pd.c m_class.c m_obj.c m_atom.c m_memory.c m_binbuf.c \
    m_conf.c m_glob.c m_sched.c \
    s_main.c s_inter.c s_unix.c s_file.c s_print.c \
    s_loader.c s_path.c s_entry.c \
    d_ugen.c d_ctl.c d_arithmetic.c d_osc.c d_filter.c d_dac.c d_misc.c \
    d_math.c d_fft.c d_mayer_fft.c d_fftroutine.c d_array.c d_global.c \
    d_delay.c x_arithmetic.c x_connective.c x_interface.c x_midi.c x_misc.c \
    x_time.c x_acoustics.c x_net.c x_qlist.c x_gui.c\
    $(SYSSRC)


OBJ = $(SRC:.c=.o) m_stamp.o

GSRC =  t_main.c t_tkcmd.c

GOBJ = $(GSRC:.c=.o)

.PHONY: pd gui externs

test:  $(PDEXEC) $(BIN_DIR)/pd-gui
all: m_stamp.c $(PDEXEC) $(BIN_DIR)/pd-gui externs

$(OBJ) : %.o : %.c
	$(CC) $(CFLAGS) $(GFLAGS) $(INCLUDE) -c -o $(OBJ_DIR)/$*.o $*.c 

$(GOBJ) : %.o : %.c
	$(CC) $(CFLAGS) $(GFLAGS) $(INCLUDE) -c -o $(OBJ_DIR)/$*.o $*.c 

pd: $(PDEXEC)

gui: $(BIN_DIR)/pd-gui

externs: 
	cd ../doc/6.externs;make pd_linux
	cd ../doc/4.fft.examples;make pd_linux

$(PDEXEC): $(OBJ) 
	cd ../obj;  $(CC) $(LDFLAGS) $(DBG_CFLAGS) -o $(PDEXEC) $(OBJ) \
	$(LIB)

$(BIN_DIR)/pd-gui: $(GOBJ) $(GSRC)
	cd ../obj; $(CC) $(INCLUDE) -o $(BIN_DIR)/pd-gui $(GOBJ) \
	$(GLIB)

m_stamp.c:
	sh m_stamp.sh > m_stamp.c

install: install-bin install-indep

INSTDIR = $(DESTDIR)/$(INSTALL_PREFIX)
install-bin: 
	install -d $(INSTDIR)/lib/pd/bin
	install $(BIN_DIR)/pd-gui $(INSTDIR)/lib/pd/bin/pd-gui
	cp $(PDEXEC) $(INSTDIR)/bin/pd

install-indep:
	install -d $(INSTDIR)/lib/pd/src
	install  -m644 *.tk $(INSTDIR)/lib/pd/src
	cp -r ../doc $(INSTDIR)/lib/pd/
	install -m644 m_pd.h $(INSTDIR)/include

clean:
	-rm ../obj/* $(BIN_DIR)/* m_stamp.c
	-(cd ../doc/6.externs;rm *.pd_linux)
	-(cd ../doc/4.fft.examples;rm *.pd_linux)
	rm makefile.dependencies
	touch makefile.dependencies
	chmod 666 makefile.dependencies

distclean: clean
	-rm config.cache config.log config.status makefile

tags: $(SRC) $(GSRC); ctags *.[ch]

depend: 
	$(CC) $(INCLUDE) $(CFLAGS) -M $(SRC) > makefile.dependencies


include makefile.dependencies







