# Makefile for GRAB (data collected with REACH)
#
#

# name of the produced executable

BINARY := grab.2009

SAVE_DIRECTORY := _saves/`date +%F`

OBJECTS := align.o anova.o arm.o batch.o behav.o blink.o button.o calib.o \
	coord.o event.o graph.o head.o help.o histo.o \
	interval.o layout.o list.o macro.o main.o merge.o \
	output.o picture.o sac.o skip.o time.o title.o trial.o \
	ttest.o xyplot.o

USER_OBJECTS := config.o util.o input.o

G_MACS := anova.o bounds.o print.o util.o ttest.o printbehav.o interval.o \
	 saccades.o targets.o touch2.o
B_MACS := sac_endpt.o movement.o
C_MACS := success.o failure.o
CO_MACS := reference.o
F_MACS := flash.o frame.o
FR_MACS := vest_sensory.o vest_motor.o frame.o pursue.o write_sac.o roc.o\
	  trialinfo.o
A_MACS := latency.o
JF_MACS := intent.o exportdata.o direction.o test.o getbestclass.o split.o
N_MACS  := write_sac.o
ST_MACS := printbehav.o printstim.o getbestclass.o findsac.o movement.o \
	  interval.o 
MR_MACS := write_times.o sort.o events.o memsac2009.o memsac2011.o strabismus.o
MU_MACS := timing.o target.o sactime.o sacbehave.o errortype.o
RWD_MACS  := basic.o
LFP_MACS  := lfp.o #  correlate.o power.o
MEM_MACS  := extract.o times.o sort.o

GENERIC_MACS :=  $(G_MACS:%=_macros/generic/_objects/%)
BEHAV_MACS   :=  $(B_MACS:%=_macros/behavior/_objects/%)
COORD_MACS   :=  $(CO_MACS:%=_macros/coord/_objects/%)
CUE_MACS     :=  $(C_MACS:%=_macros/cue/_objects/%)
FIELD_MACS   :=  $(F_MACS:%=_macros/field/_objects/%)
FRAME_MACS   :=  $(FR_MACS:%=_macros/frame/_objects/%)
ARM_MACS     :=  $(A_MACS:%=_macros/arm/_objects/%)
JEFF_MACS    :=  $(JF_MACS:%=_macros/jeff/_objects/%)
NOVEL_MACS   :=  $(N_MACS:%=_macros/novelty/_objects/%)
STIM_MACS    :=  $(ST_MACS:%=_macros/stimulate/_objects/%)
MRI_MACS     :=  $(MR_MACS:%=_macros/mr/_objects/%)
MUSCIMOL_MACS:=  $(MU_MACS:%=_macros/muscimol/_objects/%)
REWARD_MACS  :=  $(RWD_MACS:%=_macros/reward/_objects/%)
LFP_FULL_MACS:=  $(LFP_MACS:%=_macros/lfp/_objects/%)
MEMORY_MACS  :=  $(MEM_MACS:%=_macros/memory/_objects/%)

FULL_OBJECTS := $(OBJECTS:%=_objects/%)
FULL_USER_OBJECTS := $(USER_OBJECTS:%=_objects/%)
FULL_MACS := $(GENERIC_MACS) $(BEHAV_MACS) $(FIELD_MACS) $(FRAME_MACS)	\
	    $(ARM_MACS) $(JEFF_MACS) $(NOVEL_MACS) $(STIM_MACS)		\
	    $(MRI_MACS) $(MUSCIMOL_MACS) $(CUE_MACS) $(COORD_MACS)	\
	    $(REWARD_MACS) $(LFP_FULL_MACS) $(MEMORY_MACS)

HOST := $(shell hostname)
WAS :=  $(shell cat .host)

SHELL = /bin/tcsh
BIN := $(shell ls -d --color=none /home/$(USER)/{cmds,bin} |& grep -v No)
ifeq ($(USER),stoet)
	BIN := /home2/stoet/bin/grab
endif

CC := gcc
#CC 	:= cc			# Compiler

ifeq ($(HOST),conception)
   CFLAGS := -O3 -Wall -Wextra -Wno-implicit-fallthrough -Wno-unused-result -Wno-unused-but-set-variable
else
   CFLAGS := -O3 -Wall -Wextra
endif
NO_WARN := -O3 
#  -O3   : high level of optimization
#  -Wall : show 'all' warnings (a pain, but like lint, can be a help!)
#  -Wextra : show even more warnings
#  -Wno-unused-result: don't complain about system() etc calls that
#         don't check their return value
#  NO_WARN : optional CFLAGS for buggy macros!
   
CFLAGS := $(CFLAGS) -DVERSION=2009

.KEEP_STATE:
.SILENT:
# .PHONY "dependencies" are targets that are not actual files that will be made.
# This list is probably not complete (cdh, 2020-04-03)
.PHONY: start_here all static clean save
################################################################################

start_here:
ifeq ($(HOST), $(WAS))
	rm -f _objects/{config,util,input}.o
else
	@make clean
endif
	@make --no-print-directory all

all:
	@make NEW_USER "USER=${USER}"
	@make ps
	@mv $(BINARY) $(BIN)
	@hostname >! .host

static:
	make clean
	make "CFLAGS=-static"
	make clean

clean:
	rm -f _objects/*.o _macros/*/_objects/*.o
	touch _objects/dummy.o


#  CHANGE #define in event.h to include old event definitions
1997:
	rm -f _objects/{config,util}.o
	@make NEW_USER "USER=TINA"
	echo 'Why is this set to user tina?'
	@make ps
	touch util.c config.c
	@mv $(BINARY) /home/larry/cmds/ograb

################################################################################
ps: 	$(FULL_OBJECTS) $(FULL_USER_OBJECTS) _objects/ps.o
	@cd _macros/generic; \
	   make -s "MACS = $(G_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/behavior; \
	   make -s "MACS=$(B_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/coord; \
	   make -s "MACS=$(CO_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/cue; \
	   make -s "MACS=$(C_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/field; \
	   make -s "MACS=$(F_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/frame; \
	   make -s "MACS=$(FR_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/arm; \
	   make -s "MACS=$(A_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/jeff; \
	   make -s "MACS=$(JF_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/novelty; \
	   make -s "MACS=$(N_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/stimulate; \
	   make -s "MACS=$(ST_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/mr; \
	   make -s "MACS=$(MR_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/muscimol; \
	   make -s "MACS=$(MU_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/reward; \
	   make -s "MACS=$(RWD_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/lfp; \
	   make -s "MACS=$(LFP_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@cd _macros/memory; \
	   make -s "MACS=$(MEM_MACS)" "CFLAGS=$(CFLAGS)" "CC=$(CC)"
	@$(CC) -o $(BINARY) $(FULL_OBJECTS) $(FULL_USER_OBJECTS) $(FULL_MACS) _objects/ps.o -lm -lrt
	@strip $(BINARY)
	@-chmod --silent -R gu+rw *[chi] _macros _objects
# -lposix4: for get_clocktime, used by Monte-carlo to seed rand.
################################################################################

swap:	swap.o
	@$(CC) -o swap swap.o
	@rm swap.o
	@strip swap
	@-chmod a+rwx swap
	@mv swap /home/larry/cmds

save:
	@mkdir $(SAVE_DIRECTORY)
	@cp -p *[chi] Makefile $(SAVE_DIRECTORY)
	@cp -pr _macros $(SAVE_DIRECTORY)
	@rm -fr $(SAVE_DIRECTORY)/_macros/lfp/{chronux_2_12,packages,will}
	@rm     $(SAVE_DIRECTORY)/_macros/lfp/[dm]-files
	@rm -fr $(SAVE_DIRECTORY)/_macros/*/_objects

SAVELFP_DIRECTORY := _saves/lfp/`date +%F`

savelfp:
	@mkdir $(SAVELFP_DIRECTORY)
	@cp -pr _macros/lfp $(SAVELFP_DIRECTORY)
################################################################################

_objects/ps.o : ps.c
	$(COMPILE.c) ps.c
	@mv ps.o _objects

NEW_USER: $(FULL_USER_OBJECTS)

_objects/config.o : config.c
	$(COMPILE.c) -D$(USER) config.c
	@mv config.o _objects

_objects/util.o : util.c
	$(COMPILE.c) -D$(USER) util.c
	@mv util.o _objects

_objects/input.o : input.c
	$(COMPILE.c) -D$(USER) input.c
	@mv input.o _objects

_objects/%.o : %.c
	$(COMPILE.c) $<
	@mv $(<:%.c=%.o) _objects
################################################################################
# DEPENDENCIES:
# from gcc -MM *.c

_objects/align.o: align.c config.h deffs.h anal.h function.h macro.h event.h
_objects/anova.o: anova.c deffs.h anal.h function.h macro.h
_objects/arm.o: arm.c config.h deffs.h anal.h function.h macro.h ad.h
_objects/batch.o: batch.c deffs.h anal.h function.h macro.h
_objects/behav.o: behav.c deffs.h anal.h function.h macro.h ad.h
_objects/blink.o: blink.c config.h deffs.h anal.h function.h macro.h
_objects/button.o: button.c config.h deffs.h anal.h function.h macro.h
_objects/calib.o: calib.c deffs.h anal.h function.h macro.h
_objects/config.o: config.c
_objects/coord.o: coord.c deffs.h anal.h function.h macro.h config.h
_objects/event.o: event.c deffs.h anal.h function.h macro.h event.h event.i
_objects/graph.o: graph.c config.h deffs.h anal.h function.h macro.h ad.h
_objects/head.o: head.c deffs.h anal.h function.h macro.h head.h
_objects/help.o: help.c deffs.h anal.h function.h macro.h config.h
_objects/histo.o: histo.c deffs.h anal.h function.h macro.h config.h
_objects/input.o: input.c filter.c deffs.h anal.h function.h macro.h ad.h
_objects/interval.o: interval.c deffs.h anal.h function.h macro.h config.h
_objects/layout.o: layout.c deffs.h anal.h function.h macro.h config.h
_objects/list.o: list.c deffs.h anal.h function.h macro.h
_objects/macro.o: macro.c deffs.h anal.h function.h macro.h config.h
_objects/main.o: main.c deffs.h anal.h function.h macro.h config.h
_objects/merge.o: merge.c
_objects/output.o: output.c deffs.h anal.h function.h macro.h
_objects/picture.o: picture.c deffs.h anal.h function.h macro.h event.h config.h
_objects/ps.o: ps.c
_objects/sac.o: sac.c deffs.h anal.h function.h macro.h ad.h
_objects/skip.o: skip.c deffs.h anal.h function.h macro.h
_objects/time.o: time.c config.h deffs.h anal.h function.h macro.h event.h
_objects/title.o: title.c deffs.h anal.h function.h macro.h config.h
_objects/trial.o: trial.c deffs.h anal.h function.h macro.h event.h trial.h
_objects/ttest.o: ttest.c
_objects/util.o: util.c deffs.h anal.h function.h macro.h
_objects/Version.o: Version.c
_objects/xplot.o: xplot.c
_objects/xyplot.o: xyplot.c deffs.h anal.h function.h macro.h ad.h config.h \
  event.h
