#
# Makefile  -  makefile for mknbi program for DOS
#
# Copyright (C) 1996-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: Makefile,v 1.19 2007/02/01 12:09:24 gkminix Exp $
#


# Directory name of this module
MODNAME   = mknbi-dos


# Include makefile configuration
include ../make.config
include ../make.rules


# MANEXT is the manual section number
MANEXT    = 8


# Subdirectories
SUBDIRS   = first utils


# Module-specific compiler flags
CMODFLAGS = $(LZO_FLAGS)


# Source and object files for the host system
SRCS      = mknbi.c sysdb.c openrd.c hostfs.c fatfs.c misc.c
BINSRCS   = boot_c.c first_c.c firstd_c.c
OBJS      = $(SRCS:.c=.o) $(BINSRCS:.c=.o)
TARGET    = mknbi-dos


# Rules to make the target
all:		$(TARGET) tools

$(TARGET):	mknbi

mknbi:		$(LIBTOOL) $(OBJS) $(LIBNBA)
		@$(LIBTOOL) --mode=link \
		  $(CC) -o mknbi $(LDFLAGS) $(OBJS) $(LIBS) $(LZOLIB)


# Generate C source file from boot image loader binary source
first_c.c:	stamp-first-$(GNU16BIT)
		@touch $@

first/first.b86::
		@if !(cd first && $(MAKE) -q first.b86); then		\
		   echo making first.b86 in first;			\
		   (cd first && $(MAKE) first.b86) || exit 1;		\
		 fi

stamp-first-gnu:	first/first.b86 | $(MAKEC)
		@touch stamp-first-gnu
		$(MAKEC) first <first/first.b86 >first_c.c
		@$(CPATTR) first_c.c first_c.c-default

stamp-first-nognu:
		@touch stamp-first-nognu
		cp first_c.c-default first_c.c


firstd_c.c:	stamp-firstd-$(GNU16BIT)
		@touch $@

first/first-debug.b86::
		@if !(cd first && $(MAKE) -q first-debug.b86); then	\
		   echo making first-debug.b86 in first;		\
		   (cd first && $(MAKE) first-debug.b86) || exit 1;	\
		 fi

stamp-firstd-gnu:	first/first-debug.b86 | $(MAKEC)
		@touch stamp-firstd-gnu
		$(MAKEC) firstd <first/first-debug.b86 >firstd_c.c
		@$(CPATTR) firstd_c.c firstd_c.c-default

stamp-firstd-nognu:
		@touch stamp-firstd-nognu
		cp firstd_c.c-default firstd_c.c


# Generate C source file from boot sector binary source
boot_c.c:	stamp-boot-$(GNU16BIT)
		@touch $@

utils/boot.boot::
		@if !(cd utils && $(MAKE) -q boot.boot); then	\
		   echo making boot.boot in utils;		\
		   (cd utils && $(MAKE) boot.boot) || exit 1;	\
		 fi

stamp-boot-gnu:	utils/boot.boot | $(MAKEC)
		@touch stamp-boot-gnu
		$(MAKEC) boot <utils/boot.boot >boot_c.c
		@$(CPATTR) boot_c.c boot_c.c-default

stamp-boot-nognu:
		@touch stamp-boot-nognu
		cp boot_c.c-default boot_c.c


# Generate utilities
.PHONY:	tools

tools:
		@if !(cd utils && $(MAKE) -q tools); then		\
		   echo making tools in utils;				\
		   (cd utils && $(MAKE) tools) || exit 1;		\
		 fi


# distrib generates all files which are not removed by distclean
distrib:	stamp-first-gnu stamp-firstd-gnu stamp-boot-gnu tools


install:	$(LIBTOOL) all
		$(INSTALL) -d $(DESTDIR)$(bindir)
		$(INSTALL) -d $(DESTDIR)$(mandir)/man$(MANEXT)
		@$(LIBTOOL) --mode=install \
		  $(INSTALL_PROGRAM) mknbi $(DESTDIR)$(bindir)/$(TARGET)
		$(INSTALL_DATA) mknbi.man $(DESTDIR)$(mandir)/man$(MANEXT)/$(TARGET).$(MANEXT)
		@echo making install in utils
		@(cd utils && $(MAKE) install) || exit 1



clean:			clean-local clean-recursive

clean-local:		clean-general
			rm -f mknbi first_c.c firstd_c.c boot_c.c


distclean:		distclean-local distclean-recursive

distclean-local:	clean-local


realclean:		realclean-local realclean-recursive

realclean-local:	distclean-local
			rm -f first_c.c-default firstd_c.c-default boot_c.c-default


