##############################################################
#
# Makefile master for the Source Navigator / GNU Compiler Env.
#
# Copyright (C) 2000 K-TEAM S.A.
#
##############################################################


##############################################################
# Variables that user must no change without possible disrupts
##############################################################

BIOS = $(hdw_pack)lib/bios.bin
mdscript = $(hdw_pack)etc/module.ld
rmscript = $(hdw_pack)etc/rom.ld
dwscript = $(hdw_pack)etc/download.ld
dbscript = $(hdw_pack)etc/debug.ld

# In case the distribution is not the builtin one in gcc, tell
# the compiler where to find everything:

EXE_SYS = -b $(exeprefix) -B$(prefix)lib/gcc-lib/
LIB_SYS = -L$(hdw_pack)lib \
          -L$(prefix)lib/gcc-lib/$(exeprefix)/$(gccversion) \
          -L$(prefix)lib \
          -L$(sft_pack)lib \
          -L$(sft_pack)ktdebug/lib \
          -L$(sft_pack)glue/lib \
          -L$(sft_pack)kbus/lib
INC_SYS	= -I$(hdw_pack)include \
          -I$(prefix)lib/gcc-lib/$(exeprefix)/$(gccversion)/include \
          -I$(prefix)include/g++-3 \
 	  -I$(sft_pack)include \
          -I$(sft_pack)ktdebug/include \
          -I$(sft_pack)kbus/include

# Object Format Converter:
# Used cause PIC is supported only for elf output
# it's impossible to make direct srec conversion
CONVERT	= $(prefix)/bin/$(exeprefix)-objcopy

# Archiver Options:
#          -r: replace files in archive if already present
#          -s: generate file symbol table
ARFLAGS = rs
AR	= $(prefix)/bin/$(exeprefix)-ar

# Ranlib Options:
RANLIB	= $(prefix)/bin/$(exeprefix)-ranlib

# Listing Options:
#           -D: disassemble all sections of the files
#           -x: show all the headers
LIST	= $(prefix)/bin/$(exeprefix)-objdump --disassemble-zeroes -Dx


# Assembler Options:
# Play around needed to implement ASFLAGS -> CC_ASFLAGS and LDFLAGS
comma:= ,
empty:=
space:= $(empty) $(empty)
ifeq ($(strip $(ASFLAGS)),)
CC_ASFLAGS = -Wa,$(subst  $(space),$(comma),$(strip $(ASFLAGS)))
endif

# Linker Options:
ifeq ($(strip $(LDFLAGS)),)
MORELDFLAGS = ,$(subst  $(space),$(comma),$(strip $(LDFLAGS)))
endif
LDFLAGS_MOD  = $(LIB_SYS) -Wl,-s,-T$(mdscript),$(hdw_pack)lib/crt0.o$(MORELDFLAGS)
LDFLAGS_ROM  = $(LIB_SYS) -Wl,-s,-T$(rmscript),$(hdw_pack)lib/crt0.o$(MORELDFLAGS)
LDFLAGS_SREC = $(LIB_SYS) -Wl,-s,-T$(dwscript),$(hdw_pack)lib/crt0.o$(MORELDFLAGS)
LDFLAGS_DBG  = $(LIB_SYS) -Wl,-T$(dbscript),$(hdw_pack)lib/crt0.o$(MORELDFLAGS)

# Compiler Options:
CCDEBUG	   = -g -DKROSDBG
CCOPT	   = -msoft-float -m68332 -nostdlib -ffreestanding $(CCPIC)
CPPFLAGS   = -I$(includedir) 
ALL_CFLAGS =  $(CPPFLAGS) $(EXE_SYS) $(INC_SYS) $(CC_ASFLAGS) $(CCOPT) $(CCUSERFLAGS) $(CFLAGS)
CC	   = $(prefix)bin/$(exeprefix)-gcc
CXX	   = $(prefix)bin/$(exeprefix)-g++

# Administator Options:
DWNLD	= $(prefix)java/bin/java -cp $(prefix)lib/KheperaTerminal.jar KheperaTerminal.Main -port $(port) -speed $(speed) -upload
COPY	= cp
SPECIAL =

##################
# Implicit Rules #
##################

.SUFFIXES:

%.dwn: $(bindir)%.s37
	@echo Downloading $?
	@$(DWNLD) $?

$(bindir)%.s37: $(objdir)%.o
	@echo Linking $(filter %.o,$^) into $@ 
	@$(LINKER) $(SPECIAL) $(OPTS) $(ALL_CFLAGS) $(LDFLAGS_SREC) -o $@ $(filter %.o,$^) $(LOADLIBES)
	@$(CONVERT) -I elf32-m68k -O srec $@

$(bindir)%.dbg: $(objdir)%.o
	@echo Linking $(filter %.o,$^) into $@ 
	@$(LINKER) $(CCDEBUG) $(SPECIAL) $(OPTS) $(ALL_CFLAGS) $(LDFLAGS_DBG) -o $@ $(filter %.o,$^) $(LOADLIBES)

$(bindir)%.rom: $(objdir)%.o
	@echo Linking $(filter %.o,$^) into $@ 
	@$(LINKER) $(SPECIAL) $(OPTS) $(ALL_CFLAGS) $(LDFLAGS_ROM) -o $@ $(filter %.o,$^) $(LOADLIBES)
	@$(CONVERT) -I elf32-m68k -O binary $@
	@echo Building Rom using BIOS file $(firstword $(filter %.BIN,$^) $(filter %.bin,$^) $(BIOS))
	@cat  $(firstword $(filter %.BIN,$^) $(filter %.bin,$^) $(BIOS)) $@ > $(bindir)tmp
	@mv $(bindir)tmp $@

$(libdir)%.a: 
	@echo Building library $@ from $(filter %.o,$^) 
	@$(AR) $(ARFLAGS) $@ $(filter %.o,$^) 

$(asmdir)%.lst: $(bindir)%.dbg
	@echo Generating assembler listing for $< into $@ 
	@$(LIST) $< > $@ 

$(asmdir)%.s: $(srcdir)%.cc
	@echo Compiling w/o assembling $< into $@ 
	@$(CXX) $(SPECIAL) $(OPTS) $(ALL_CFLAGS) -S -o $@ $<

$(asmdir)%.s: $(srcdir)%.c
	@echo Compiling w/o assembling $< into $@ 
	@$(CC) $(SPECIAL) $(OPTS) $(ALL_CFLAGS) -S -o $@ $<

$(srcdir)%.d: $(srcdir)%.c
	@echo Finding dependencies into $<
	@$(CC) -MM $(SPECIAL) $(OPTS) $(ALL_CFLAGS) -S -o $@ $<
	
$(srcdir)%.d: $(srcdir)%.cc
	@echo Finding dependencies into $<
	@$(CC) -MM $(SPECIAL) $(OPTS) $(ALL_CFLAGS) -S -o $@ $<



