#
# Makefile  -  makefile for mknbi program for MGL compiler
#
# Copyright (C) 1997-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.20 2007/02/01 12:09:24 gkminix Exp $
#


# Directory name of this module
MODNAME   = mknbi-mgl


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


# MANEXT is the manual section number
MANEXT    = 8


# Subdirectories
SUBDIRS   = first runtime test


# Module-specific compiler flags
# Define PARANOID to compile various checks
CMODFLAGS = -DPARANOID


# Source and object files for the host system
#
# Notes:
#
#	YSRCS      - YACC source files
#	LSRCS      - LEX source files
#	DISTSRCS   - files which are only necessary for distribution
#	MGLCSRCS   - generic C files for MGL compiler, only used to generate
#	             a list of interror() calls
#	MGLSRCS    - all source files required to build an MGL compiler
#	MKNBISRCS  - all source files required to build mknbi program part
#	             (without the actual compiler sources)
#	BINSRCS    - all C-converted binary sources
#	SRCS       - only used for generating dependencies in make.rules
#	OBJS       - all object files to build the final mknbi program
#
YSRCS     = mglparse.y
LSRCS     = mgllex.l
DISTSRCS  = $(YSRCS:.y=.c) $(YSRCS:.y=.h) $(LSRCS:.l=.c)
MGLCSRCS  = gencode.c gencmd.c genexpr.c expr.c symbols.c utils.c
MGLSRCS   = $(MGLCSRCS) $(YROOT).c $(LROOT).c runtime_c.c
MKNBISRCS = mknbi.c sysdb.c compile.c
BINSRCS   = first-dos_c.c first-nb_c.c first-pxe_c.c
SRCS      = $(MKNBISRCS) $(MGLCSRCS)
OBJS      = $(MKNBISRCS:.c=.o) $(MGLSRCS:.c=.o) $(BINSRCS:.c=.o)
TARGET    = mknbi-mgl


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

$(TARGET):	mknbi

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


# Rules to generate the parser
$(YROOT).o:	$(YROOT).c mknbi.h mgl.h gencode.h

$(YROOT).c $(YROOT).h:	$(YSRCS)
		$(YACC) $(YFLAGS) $(YSRCS)

$(YSRCS:.y=.c):	$(YROOT).c
		@cp $(YROOT).c $(YSRCS:.y=.c)

$(YSRCS:.y=.h):	$(YROOT).h
		@cp $(YROOT).h $(YSRCS:.y=.h)


# Rules to generate the lexer
$(LROOT).o:	$(LROOT).c mknbi.h mgl.h gencode.h headers/runtime.h

$(LROOT).c:	$(LSRCS)
		$(LEX) $(LFLAGS) $(LSRCS)

$(LSRCS:.l=.c):	$(LROOT).c
		@cp $(LROOT).c $(LSRCS:.l=.c)


# Rules to generate a test program
testcompile:	$(LIBTOOL) testcompile.o $(MGLSRCS:.c=.o)
		@$(LIBTOOL) --mode=link \
		  $(CC) -o testcompile -static $(LDFLAGS) $^ $(LIBS) $(LEXLIB)

testcompile.o:	compile.c
		$(CC) $(CFLAGS) $(CMODFLAGS) -DTEST -c -o $@ $<


# Generate C source file for all different boot image loaders
first-dos_c.c:	stamp-first-dos-$(GNU16BIT)
		@touch $@

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

stamp-first-dos-gnu:	first/first-dos.exe | $(MAKEC)
		@touch stamp-first-dos-gnu
		$(MAKEC) first_dos <first/first-dos.exe >first-dos_c.c
		@$(CPATTR) first-dos_c.c first-dos_c.c-default

stamp-first-dos-nognu:
		@touch stamp-first-dos-nognu
		cp first-dos_c.c-default first-dos_c.c


first-nb_c.c:	stamp-first-nb-$(GNU16BIT)
		@touch $@

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

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

stamp-first-nb-nognu:
		@touch stamp-first-nb-nognu
		cp first-nb_c.c-default first-nb_c.c


first-pxe_c.c:	stamp-first-pxe-$(GNU16BIT)
		@touch $@

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

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

stamp-first-pxe-nognu:
		@touch stamp-first-pxe-nognu
		cp first-pxe_c.c-default first-pxe_c.c


# Generate C source file from runtime code
runtime_c.c:	stamp-runtime-$(GNU16BIT)
		@touch $@

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

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

stamp-runtime-nognu:
		@touch stamp-runtime-nognu
		cp runtime_c.c-default runtime_c.c


# distrib generates all files which are not removed by distclean
distrib:	stamp-first-dos-gnu stamp-first-nb-gnu \
		stamp-first-pxe-gnu stamp-runtime-gnu \
		$(DISTSRCS)

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)
		$(INSTALL_DATA) misc/mgl.vim $(DESTDIR)$(nbmiscdir)/mgl.vim
		$(INSTALL_DATA) test/menu.mgl $(DESTDIR)$(nbmiscdir)/menu.mgl


test:		mknbi
		$(MAKE) -C test all


interrors:
		cat $(MGLCSRCS) $(YSRCS) $(LSRCS) | grep "interror[(]" | \
				tr -d "\t" | sort >interrors


# Cleanup
clean:			clean-local clean-recursive

clean-local:		clean-general
			rm -f mknbi testcompile interrors
			rm -f $(YROOT).c $(YROOT).h $(LROOT).c
			rm -f first-dos_c.c first-nb_c.c first-pxe_c.c
			rm -f runtime_c.c


distclean:		distclean-local distclean-recursive

distclean-local:	clean-local


realclean:		realclean-local realclean-recursive

realclean-local:	distclean-local
			rm -f first-dos_c.c-default
			rm -f first-nb_c.c-default
			rm -f first-pxe_c.c-default
			rm -f runtime_c.c-default
			rm -f $(YSRCS:.y=.c) $(YSRCS:.y=.h) $(LSRCS:.l=.c)

