
# make requires to be run from sh (not cmd!) 
# Some targets need cp and mv commands:
# in case the user forgets to set up his own my_make and etc/ files.
# (cp and mv provided in winavr for winferior systems)
# perl is also needed for some targets (see activestate perl)

# WARNTEXT = $(SHLVL)
WARNTEXT ?= "runme from sh shell, win32 cmd is broken, dies with CreateProcess error"

#Target processor:
	MCU = atmega128

#Main target file
	TRG = megasquirt

#C sources
	SRC  = megasquirt.c helpers.c adc.c comm.c fuelcalc.c tables.c fuelcontrol.c storage.c actuators.c ve.c iac.c keyboard.c lcd.c heap.c eventqueue.c button.c benchmark.c lcd_display.c injconf.c ignconf.c timing.c kb.c menu.c ringbuf.c phasesignal.c ego.c wbo2.c pid.c log.c multitooth.c spi.c knock.c

#firmware.tgz goes here:
	DISTDIR = ..

### System sources
#	SRC += sys\time.c

#ASM sources
#	ASRC     = xram.S

#Assembler flags
	ASFLAGS = -Wa, -gstabs

#C Compiler flags -O2 fails sometimes (lcd.c and button.c), use -O0
	CPBASICFLAGS = -g -Wall -Wstrict-prototypes -Wa,-ahlms=$(<:.c=.lst)

# -DDEBUG
#	CPFLAGS = -g -O0 -Wall -Wstrict-prototypes -Wa,-ahlms=$(<:.c=.lst)  # Debug mode. Don't optimize
#	CPFLAGS = -g -O3 -Wall -Wstrict-prototypes -Wa,-ahlms=$(<:.c=.lst)  # Optimize for speed

#Linker flags
	LDFLAGS = -Wl,-Map=$(TRG).map,--cref
#	LDFLAGS = -Wl,-Map=$(TRG).map,--cref,--defsym=__heap_start=0x801000,--defsym=__heap_end=0x807fff
#   LDFLAGS = -Wl,-Map=$(TRG).map,--cref

# include personal configuration file
include my_make

	CPBASICFLAGS += $(MY_CONF)
	CPFLAGS = -O3 $(CPBASICFLAGS)
	CPSAFEFLAGS = -O3 $(CPBASICFLAGS)

include avr_make

# if the user forgets to provide his own files, this "fixes" it (with incorrect values, most likely)
$(TRG).pre: SH_NEEDED my_make etc/thermfactor.c etc/airdenfactor.c etc/barofac4115.c etc/barofac4250.c etc/kpafactor4115.c etc/kpafactor4250.c etc/matfactor.c

#$(SHLVL:-hihi)
# could it be tested with $SHELL or $SHLVL variable?
.PHONY: SH_NEEDED

SH_NEEDED:
	echo $(WARNTEXT)

my_make:
	cp -i doc/my_make .

etc/thermfactor.c:
	cp -i thermfactor.c etc

etc/matfactor.c:
	cp -i matfactor.c etc

etc/airdenfactor.c:
	cp -i airdenfactor.c etc

etc/barofac4115.c:
	cp -i barofac4115.c etc

etc/barofac4250.c:
	cp -i barofac4250.c etc

etc/kpafactor4115.c:
	cp -i kpafactor4115.c etc

etc/kpafactor4250.c:
	cp -i kpafactor4250.c etc


###### dependencies, add any dependencies you need here ###################
$(TRG).o	: $(TRG).c megasquirt.h global.h helpers.h adc.h storage.h comm.h actuators.h tables.h ve.h iac.h button.h compile_time.h
helpers.o	: helpers.c helpers.h global.h
adc.o		: adc.c adc.h global.h
comm.o		: comm.c comm.h global.h storage.h actuators.h ve.h varstr.h
fuelcalc.o	: fuelcalc.c fuelcalc.h global.h tables.h
fuelcontrol.o	: fuelcontrol.c fuelcontrol.h fuelcalc.h global.h adc.h actuators.h iac.h
storage.o	: storage.c storage.h global.h
tables.o	: tables.c tables.h etc/thermfactor.c etc/matfactor.c etc/airdenfactor.c etc/barofac4115.c etc/barofac4250.c etc/kpafactor4115.c etc/kpafactor4250.c
actuators.o	: actuators.c actuators.h global.h
ve.o		: ve.c global.h
iac.o		: iac.c global.h
heap.o		: heap.c
lcd.o		: lcd.c
ego.o		: ego.c
lcd_display.o: lcd_display.c lcd_display.h magicstr.h
	$(CC) -c $(CPSAFEFLAGS) -I$(INCDIR) $< -o $@

kb.o		: kb.c kbpindefs.h scancodes.h
comm.o		: comm.c
keyboard.o	: keyboard.c
button.o	: button.c
	$(CC) -c $(CPSAFEFLAGS) -I$(INCDIR) $< -o $@

wbo2.o		: wbo2.c
pid.o		: pid.c
log.o		: log.c

m.cof: megasquirt.elf
	objtool loadelf megasquirt.elf mapfile megasquirt.map writecof m.cof

# oops, this requires perl interpreter... (should we add it to cvs?? or sometimes upload to a webpage?)
# just in case, check 
# could be autofetched with wget (which you probably have if sh is installed from unxutils)
magicstr.h: global.h bin/stru.pl
	perl bin/get_name.pl global.h || (wget http://www.x-dsl.hu/genboard/magicstr.h && echo "magicstr might be OLD")

varstr.h: global.h bin/get_name.pl
	perl bin/get_name.pl global.h || (wget http://www.x-dsl.hu/genboard/varstr.h && echo "varstr might be OLD")

compile_time.h: bin/ctime.pl $(SRC)
	perl bin/ctime.pl || echo "unknown" > compile_time.h

mtt: etc/config.mtt etc/tables.mtt

etc/config.mtt: etc/config.txt global.h
	perl bin/make_conf.pl global.h etc/config.txt etc/config.mtt

etc/tables.mtt: etc/tables.txt
	perl bin/make_table.pl etc/tables.txt etc/tables.mtt

# |gzip could be added, but avoid tar czf
dist: megasquirt.hex boot/main.hex
	cp megasquirt.hex $(DISTDIR)
	cp boot/main.hex $(DISTDIR)
	make clean
	mv $(DISTDIR)/megasquirt.hex .
	mv $(DISTDIR)/main.hex boot
	tar cf - . | gzip >$(DISTDIR)/firmware.tgz

boot/main.hex:
	cd boot; make

