# *****************************************************************
BIN_PATH	=   ../bin/

PROGRAM		=   $(BIN_PATH)sys.out

BSC		=   $(BIN_PATH)sys.bsc

PWD		:=  $(subst \,/,$(shell cd))

VPATH		=   ../src

bin_path	=   $(subst /,\,$(BIN_PATH))

src		=   $(notdir $(foreach dir, $(VPATH), $(wildcard $(dir)/*.c)))

obj		=   $(patsubst %.c,%.o, $(src))
OBJ		=   $(addprefix $(BIN_PATH), $(obj))

#------------------------------------------  Swithes --------------------------------------#
CC	      	=   $(WIND_BASE)\host\x86-win32\bin\cc386
CL		=   cl.exe
AS		=   as386
LD		=   ld386
RM		=   del
LIST		=   echo
BSCMAKE		=   bscmake

DEBUG		=   -g

INC		=   -I$(WIND_BASE)/target/h \
		    -I$(PWD)/../inc \

TARGET		=   -DCPU=I80486

CLFLAGS		=   -DRW_MULTI_THREAD -D_REENTRANT -DVXW_EXPLICIT_TEMPLATES \
		    -DWIN32 $(DEFINES)

CCFLAGS		=   -B$(WIND_BASE)/host/$(WIND_HOST_TYPE)/lib/gcc-lib/ \
		    -m486 -fvolatile -nostdlib -fno-builtin -Wall \
		    -nostdinc -DRW_MULTI_THREAD -D_REENTRANT \
		    -fno-implicit-templates -DVXW_EXPLICIT_TEMPLATES \
		    -fno-defer-pop $(INC) $(TARGET) $(DEBUG) $(DEFINES)

CCFLAGS2	=   -B$(WIND_BASE)/host/$(WIND_HOST_TYPE)/lib/gcc-lib/ \
		    -nostdinc -DRW_MULTI_THREAD -D_REENTRANT \
		    -fno-implicit-templates -DVXW_EXPLICIT_TEMPLATES \
		    -fno-defer-pop $(INC) $(TARGET) $(DEFINES) -MM

LNKFLAGS	=   -r

SEDCMD		=   sed -e "/:[0-9]*:/s/:\([0-9]*\):/(\1)  :/"

.PHONY		:   all clean rebuild

# -------------------------- RULES -----------------------------
# When there isn't any target given to make, make will automaticly execute the first
# rule it detects. In our case, the "all" rule.

all		:   $(PROGRAM)

clean		:
		    - $(RM) $(bin_path)*.out 
		    - $(RM) $(bin_path)*.o 
		    - $(RM) $(bin_path)*.d
		    - $(RM) $(bin_path)*.sbr
		    - $(RM) $(bin_path)*.bsc

rebuild		:
		    $(MAKE) clean
		    $(MAKE) all

$(PROGRAM)	:   $(OBJ)

#Now we can link all the object files to one out file
		    @echo "Linking and building " $@
		    @$(LD) -o $@ $(LNKFLAGS) $(BIN_PATH)$(OBJ)

#//-----Rules to make an object file out of an source file---------

$(BIN_PATH)%.o :    %.c
		    @echo compiling : $<

#First Rule : Make a SBR file. This file contains the static browse information used by the
#             Microsoft developer studio. The compiler of the developer studio is used to
#             create this file.

		    @$(CL) -nologo -c -Zs $(INC) $(TARGET) $(DEFINES) $(CLFLAGS) -W3 $(PWD)/$< -Fr$(patsubst %.o,%.sbr, $@)

#Second rule : Make a file which contains the dependencies of the source file.
#The name of the dependencie file is filename.d .

		    @$(CC) -c $(CCFLAGS2) $(PWD)/$< | sed "1s/^/$$(BIN_PATH)/" > $(bin_path)$(subst .c,.d,$(notdir $<))

#Third Rule : Make an object file from the current source file. This object file 
#              is created in the $(BIN_PATH) directory.

		    @$(CC) -c $(CCFLAGS) $(PWD)/$< -o $(BIN_PATH)$@ 2>&1 | $(SEDCMD)
		    @echo done.

#----------------------DEPENDECIES-----------------------------
DEPS := $(wildcard $(BIN_PATH)*.d)
ifneq ($(DEPS),)
include $(DEPS)
endif

# *****************************************************************
