

SHELL	    := /bin/bash

CC	    := gcc

CFLAGS	    := -Wall
#CPPFLAGS    += -DMSTAMP=20080620

HOST := $(shell uname -i)
ifeq ("$(HOST)", "x86_64")
	MCMACHFLAGS += -m32
	CORELDFLAGS := -m elf_i386
	OCFLAGS := -O elf32-i386
endif

#CFLAGS	    += -DMARTIAN_CORE_DEBUG
#CFLAGS	    += -DDYNAMICRELOC_DEBUG
#CFLAGS	    += -DRELOCFIX_DEBUG		# to debug elf386tweakrelocs

# leave the relocations in
MLDFLAGS := $(MCMACHFLAGS) -lpthread -lrt
MCFLAGS += $(MCMACHFLAGS)

ifeq ("y","$(DEBUG)") 
	MLDFLAGS += -g
	CFLAGS += -g
endif

%.o: %.c
	@set -e; \
	echo -e "    CC\t$@"; \
	gcc -c $(CPPFLAGS) $(CFLAGS) $<

mmodem_source_objs := main.o \
	dumpers.o log.o \
	session.o mport.o \
	pty.o sysdep.o \
	isr.o smp.o \
	core_if.o coresubst.o \
	link.o tweakrelocsdynamic.o 

martian_modem_OBJS := $(mmodem_source_objs) marscore.o 
#$(mmodem_source_objs) coreadd.o: %.o: %.c
#	$(CC) $(CPPFLAGS) $(MCFLAGS) $(CFLAGS) -c $<

notify-obj: $(martian_modem_OBJS)
	@echo "Objects:"


$(mmodem_source_objs) coreadd.o: CFLAGS += $(MCMACHFLAGS)
				

core_if.o main.o session.o mport.o sysdep.o pty.o: sysdep.h 
main.o pty.o session.o mport.o: mport.h sysdep.h
main.o mport.o link.o dumpers.o isr.o coresubst.o: ../martian.h
session.o main.o smp.o: watch.h common.h
log.o main.o coresubst.o link.o mport.o pty.o: common.h
main.o pty.o mport.o link.o dumpers.o isr.o smp.o log.o session.o tweakrelocsdynamic.o sysdep.o: log.h common.h
core_if.o coresubst.o: overriden_core.h

main.o: CPPFLAGS += -DMSTAMP=20080620
	
overriden_core.h: overriden_core_functions
	@set -e; \
	> $@; \
	for sym in `<overriden_core_functions`; do \
		if grep -q -h " $$sym[[:blank:]]*(" core_if.c coresubst.c; then \
			grep -h " $$sym[[:blank:]]*(" core_if.c coresubst.c | sed -e "s/^/extern /" -e "s/$$sym/do_$$sym/" -e "s/).*/);/" >> $@; \
		fi \
	done

marscore.o: ltmdmobj.o coreadd.o elf386tweakrelocs overriden_core_functions
	@set -e; \
	echo -e "    LD\t$@"; \
	overriden_funcs=`<overriden_core_functions`; \
	ld -r \
		`for sym in $$overriden_funcs; do echo -n "--defsym do_$$sym=$$sym "; done` \
		$(CORELDFLAGS) -o marscore.o ltmdmobj.o coreadd.o
	@echo -e "    TWEAK\t$@"; \
	objcopy --weaken-symbols=overriden_core_functions \
		$(OCFLAGS) marscore.o; \
	./tweakcore.sh marscore.o 

#martian_modem: binaries := 1
#@if [ "$(binaries)" != "1" ] ; then echo "Binaries:"; fi 
martian_modem: MLDFLAGS := -Wl,-q $(MLDFLAGS)
martian_modem:	$(martian_modem_OBJS)
	@set -e; \
	echo -e "    LD\t$@"; \
	gcc -o $@ $(martian_modem_OBJS) $(MLDFLAGS) 

debug_script: debug_script.in martian_modem 
	@addr=`nm martian_modem | grep mcb_dump | awk '{print $$1}'`; \
	if [ "$${addr}" != "" ]; then sed "s/\&mcb_dump/0x$${addr}/" $< > $@; fi

ifeq ("y","$(DEBUG)")
all: debug_script
endif

elf386tweakrelocs: elf386tweakrelocs.c
	@set -e; \
	echo -e "    CC\t$@"; \
	gcc -o $@ $(CFLAGS) $(CPPFLAGS) $<

.PHONY: clean all install uninstall install-program install-debug debug-dir
clean:
	@echo -e "    RM\tOBJS"; rm -f $(martian_modem_OBJS) coreadd.o
	@echo -e "    RM\tBINS"; rm -f martian_modem{,.stripped,.debug}  
	@echo -e "    RM\tTOOLS"; rm -f relocs.sym elf386tweakrelocs
	@rm -f overriden_core.h relocs.sym
	@rm -f debug_script

relocs.sym: link.c
	@awk '/SYMBOL \(/ { sub (/^.*SYMBOL \(/,""); sub (/[,)].*/,""); print }\
	    /SYMBOL_TO_MEMBER \(/ { sub (/^.*SYMBOL_TO_MEMBER \(/,""); sub (/[,)].*/,""); print }' \
	    $< > $@

martian_modem.debug: $(martian_modem_OBJS) 
	@set -e; echo -e "    LD\t$@"; gcc -g -o $@ $(martian_modem_OBJS) $(MLDFLAGS) 
	@set -e; echo -e "    STRIP\t$@"; strip --only-keep-debug -o $@ $@ || rm $@
	
martian_modem.stripped: martian_modem martian_modem.debug relocs.sym
	@echo -e "    STRIP\t$@"; \
	objcopy --keep-symbols relocs.sym --add-gnu-debuglink=$<.debug --strip-all $< $@

all: martian_modem

install-program: target := /usr/sbin/martian_modem
install-program: martian_modem.stripped martian_modem.debug
	@echo -e "    INSTALL\t$(target)"
	@install $< $(target)

debug-dir:
	@[ -d /usr/lib/debug/usr/sbin ] || mkdir -p /usr/lib/debug/usr/sbin

install-debug: target := /usr/lib/debug/usr/sbin/martian_modem.debug
install-debug: martian_modem.debug debug-dir
	@echo -e "    INSTALL\t$(target)"
	@install $< $(target)


install: install-program install-debug
#//usr//sbin//martian_modem: martian_modem.stripped martian_modem.debug
/usr/sbin/martian_modem: martian_modem.stripped martian_modem.debug
	@echo -e "    INSTALL\t$@"
	@install $< $@

#install: /usb/sbin/martian_modem

uninstall: 
	rm /usr/sbin/martian_modem

test:
	echo $(DEBUG) $(CFLAGS)
