#
# make.rules  -  makefile rules for netboot
#
# Copyright (C) 2003-2007 Gero Kuhlmann   <gero@gkminix.han.de>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: make.rules,v 1.15 2007/01/06 18:30:44 gkminix Exp $
#


#
# Avoid the following rules to get executed when no target has been
# specified on the make command line
#
.PHONY:	all

all:



#
# Lets see if this is for the bootrom
#
BOOTROM = $(if $(findstring bootrom,$(MODNAME)),-D_BOOTROM_ $(if $(MODDEFS),-D$(MODDEFS)) -I$(ROMDIR))



#
# Define all object files for the target system. If more object files
# need to be added, use the += operator.
#
OBJS86 = $(SSRCS86:.S86=.o86) $(CSRCS86:.c86=.o86)



#
# Determine about how to make the libtool executable
#
$(LIBTOOL):
	@(cd $(TOPDIR) && $(MAKE) libtool) || exit 1



#
# Most host programs require the netboot library to be built
#
LIBNBA = $(LIBDIR)/libnb.la

ifneq ($(MODNAME),nblib)
$(LIBNBA):
	@(cd $(LIBDIR) && $(MAKE) libnb.la) || exit 1
endif



#
# Rules to compile the source files for the host programs
#
COMPILE = $(CC) $(CFLAGS) $(CMODFLAGS) $($(<:%.c=%)_FLAGS) -c

%.o:	%.c
	$(COMPILE) -o $@ $<

%.lo:	%.c
	@$(LIBTOOL) --mode=compile $(COMPILE) -o $@ $<



#
# Program to install network driver files
#
INSTDRV = $(MISCDIR)/instdrv

ifneq ($(MODNAME),misc)
$(INSTDRV):
	@(cd $(MISCDIR) && $(MAKE) instdrv) || exit 1
endif



#
# Program to generate an MD5 checksum
#
MD5PROG     = $(MISCDIR)/nbmd5
MD5PROGEXEC = $(LIBTOOL) --mode=execute $(MD5PROG)

ifneq ($(MODNAME),misc)
$(MD5PROG):
	@(cd $(MISCDIR) && $(MAKE) nbmd5) || exit 1
endif



#
# We need a program to convert a binary file into C source code
#
MAKEC = $(MISCDIR)/makec

ifneq ($(MODNAME),misc)
$(MAKEC):
	@(cd $(MISCDIR) && $(MAKE) makec) || exit 1
endif



#
# Program to convert GCC output into real-mode assembler code
#
GCCOPT = $(MISCDIR)/gccopt

ifneq ($(MODNAME),misc)
$(GCCOPT):
	@(cd $(MISCDIR) && $(MAKE) gccopt) || exit 1
endif



#
# Some x86 binaries need the i386 library
#
LIBI386A = $(I386DIR)/libi386.a

ifneq ($(MODNAME),i386)
$(LIBI386A):
	@(cd $(I386DIR) && $(MAKE) libi386.a) || exit 1
endif



#
# Rules to process target assembler files into object files
#
%.s86::	%.S86
	$(CPP86) $(CPP86INCS) $(CPP86SFLAGS) $($(<:%.S86=%)_FLAGS) -o $@ $<


%.o86:	%.s86
	$(GAS86) $(GAS86FLAGS) -o $@ $<


%-debug.o86:	%.s86
	$(GAS86) $(GAS86FLAGS) $(GAS86DEBUG) -o $@ $<



#
# Rules to process target C files into object files
#
DOGCC86 = $(GCC86) $(CPP86INCS) $(GCC86FLAGS) -S

%.t86::	%.c86
	$(DOGCC86) $($(<:%.c86=%)_FLAGS) -o $@ $<


%-debug.t86::	%.c86
	$(GCC86) $(CPP86INCS) $(GCC86FLAGS) $(GCC86DEBUG) \
				$($(<:%.c86=%)_FLAGS) -S -o $@ $<


%.s86:	%.t86 | $(GCCOPT)
	$(GCCOPT) -f$< -o$@ -h".code16"



#
# Rules to generate target binary files from object files
#
# Note that the boot sector and DOS program files (.com and .sys) dont
# get linked with the i386 library. This is because the library assumes
# different code and data segments, and those files get compiled using
# shared segments. Only .exe type DOS program files can be linked with
# the runtime library.
#
%.bin:	$(LIBI386A)
	$(GLD86) $(GLD86FLAGS) -T $(SCRIPTDIR)/ldscript.bin \
			-Map $(@:.bin=.map) -o $@ \
			$(filter-out $(LIBI386A),$^) $(GLD86LIBS)
	@chmod 644 $@


%.b86:	$(LIBI386A)
	$(GLD86) $(GLD86FLAGS) -T $(SCRIPTDIR)/ldscript.bin \
			-Map $(@:.b86=.map) -o $@ \
			$(filter-out $(LIBI386A),$^) $(GLD86LIBS)
	@chmod 644 $@


%.undi:	$(LIBI386A)
	$(GLD86) $(GLD86FLAGS) -r -T $(SCRIPTDIR)/ldscript.undi \
			-Map $(@:.undi=.map) -o $@ \
			$(filter-out $(LIBI386A),$^) $(GLD86LIBS)
	@chmod 644 $@


%.boot:
	$(GLD86) $(GLD86FLAGS) -T $(SCRIPTDIR)/ldscript.boot \
			-Map $(@:.boot=.map) -o $@ $^
	@chmod 644 $@


%.exe:	$(LIBI386A)
	$(GLD86) $(GLD86FLAGS) -T $(SCRIPTDIR)/ldscript.exe \
			-Map $(@:.exe=.map) -o $@ \
			$(filter-out $(LIBI386A),$^) $(GLD86LIBS)
	@chmod 644 $@


%.com:
	$(GLD86) $(GLD86FLAGS) -T $(SCRIPTDIR)/ldscript.com \
			-Map $(@:.com=.map) -o $@ $^
	@chmod 644 $@


%.sys:
	$(GLD86) $(GLD86FLAGS) -T $(SCRIPTDIR)/ldscript.sys \
			-Map $(@:.sys=.map) -o $@ $^
	@chmod 644 $@



#
# Rule to generate dependency file
#
.PHONY:	dep

dep:	dep-recursive .depend

.depend::	$(SRCS) $(CSRCS86) $(SSRCS86)
	@rm -f .depend
	@touch .depend
	@if test "x$(SRCS)" != "x"; then                                       \
	  echo $(CPP) $(CFLAGS) $(CMODFLAGS) -D_DEPEND_ -MM $(SRCS);           \
	  $(CPP) $(CFLAGS) $(CMODFLAGS) -D_DEPEND_ -MM $(SRCS) >>.depend;      \
	 fi
	@if test "x$(CSRCS86)" != "x"; then                                    \
	  echo $(CPP86) $(CPP86CFLAGS) $(CPP86INCS) -D_DEPEND_ -MM $(CSRCS86); \
	  $(CPP86) $(CPP86CFLAGS) $(CPP86INCS) -D_DEPEND_ -MM $(CSRCS86) |     \
		sed "s/\.o:/.o86:/" >>.depend;                                 \
	 fi
	@if test "x$(SSRCS86)" != "x"; then                                    \
	  echo $(CPP86) $(CPP86SFLAGS) $(CPP86INCS) -D_DEPEND_ -MM $(SSRCS86); \
	  $(CPP86) $(CPP86SFLAGS) $(CPP86INCS) -D_DEPEND_ -MM $(SSRCS86) |     \
		sed "s/\.o:/.o86:/" >>.depend;                                 \
	 fi


# Include dependencies if they exist
ifneq ($(wildcard .depend),)
include .depend
endif



#
# Define all phony rules
#
.PHONY:		distrib install clean distclean realclean

distrib:

install:

clean:

distclean:

realclean:


#
# Rule to do general cleaning
#
clean-general:
	rm -f *.o *.lo *.t86 *.o86 *.s86 *.lst *.map $(TARGET)
	rm -f .depend core stamp-*
	rm -rf .libs



#
# Rule to process subdirectories
#
%-recursive:
	@target=`echo $@ | sed 's/-recursive//'`;			\
	 for subdir in $(if $(SUBDIRS),$(SUBDIRS),$(wildcard *)); do	\
	 if test -d $$subdir -a -r $$subdir/Makefile; then		\
	    echo making $$target in $$subdir;				\
	    (cd $$subdir && $(MAKE) $$target) || exit 1;		\
	  fi;								\
	 done

