# Makefile for Linux DOSEMU
#
# $Date: 1995/04/08 22:29:17 $
# $Source: /home/src/dosemu0.60/RCS/Makefile,v $
# $Revision: 2.40 $
# $State: Exp $
#
# You should do a "make" to compile and a "make install" as root to
# install DOSEMU.

# please ensure that sublevel and patchlevel have two digits for the BIOS
VERSION    = 0
SUBLEVEL   = 61
PATCHLEVEL = 00

ARCH       = Linux
EMULATOR   = i386

CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
	  else if [ -x /bin/bash ]; then echo /bin/bash; \
	  else echo sh; fi ; fi)
TOPDIR := $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)

#
# Version information
#
LIBDOSEMU  = libdosemu-$(VERSION).$(SUBLEVEL).$(PATCHLEVEL)

#
# The assumption is that most new users will only type 'make'
# if they have not read the documentation, so therefore a 
# keypress pause is a good idea here and is included here.
#

default: warning all install

#
# Save those who haven't followed the README.
# Make "config" the default target if there is no configuration file or
# "depend" the target if there is no top-level dependency information.
#

ifeq (.config,$(wildcard .config))
include .config
ifeq (.depend,$(wildcard .depend))
include .depend
else
CONFIGURATION = depend
endif
else
CONFIGURATION = config
endif

ifdef CONFIGURATION
..$(CONFIGURATION):
	@echo
	@echo "You have a bad or nonexistent" .$(CONFIGURATION) ": running 'make" $(CONFIGURATION)"'"
	@echo
	$(MAKE) $(CONFIGURATION)
	@echo
	@echo "Successful. Try re-making (ignore the error that follows)"
	@echo
	exit 1

dummy: ..$(CONFIGURATION)

else

dummy:

endif

#
# Make rules
#

.c.o:
	$(CC) $(CFLAGS) -c -o $*.o $<

#
# Set up special OS definitions
#

CFLAGS  = -pipe -fstrict-prototypes -Wall $(INCDIR) -O2 # -funroll-loops \
	# -fno-inline
INCDIR  = -I. -I$(TOPDIR)/include # -I$(LINUX_INCLUDE)

LIBDIR  = $(TOPDIR)
LIBS    =

CFILES  = init/emu.c init/dos.c init/dosstatic.c
TARGETS = dos

SUBDIRS = tools commands arch/$(ARCH) emulator/$(EMULATOR) base dosext env
EMULIBS = base/base.a dosext/dosext.a env/env.a \
	  arch/$(ARCH)/arch.a emulator/$(EMULATOR)/emulator.a

include $(TOPDIR)/arch/$(ARCH)/Makefile.include
include $(TOPDIR)/emulator/$(EMULATOR)/Makefile.include

ifdef CONFIG_BOCHS
CFLAGS := $(CFLAGS) -DX86_EMULATOR -I/home/kpl/bochs-hacks -I/home/kpl/bochs
endif

ifdef CONFIG_DO_DEBUG
CFLAGS := $(CFLAGS) -g
endif

ifndef CONFIG_ELF
TARGETS := $(TARGETS) libdosemu
endif

ifdef CONFIG_ELF
# LIBS := $(LIBS) -lslang-elf
LIBS := $(LIBS) -lslang -lm
else
LIBS := $(LIBS) -lslang
endif

ifdef CONFIG_DOC
SUBDIRS := $(SUBDIRS) doc
endif

#
# X windows support goes here
#

ifdef CONFIG_ENV_X
CFILES    := $(CFILES) init/x2dos.c
LIBDIR    := $(LIBDIR) $(X11ROOTDIR)/lib
INCDIR    := $(INCDIR) -I$(X11ROOTDIR)/include
X11LIBDIR := $(X11ROOTDIR)/lib
TARGETS   := $(TARGETS) x2dos xtermdos xinstallvgafont
ifdef CONFIG_ELF
LIBS      := $(LIBS) -Wl,-rpath,$(X11LIBDIR)/elf -lX11
else
LIBS      := $(LIBS) -L$(X11LIBDIR) -lX11 -u _XOpenDisplay
endif
endif

#
# export the necessary variables
#

.EXPORT_ALL_VARIABLES:

all: Version $(TARGETS)

dossubdirs: dummy
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i; done

arch arch/$(ARCH): dummy
	$(MAKE) dossubdirs SUBDIRS=arch/$(ARCH)

base: dummy
	$(MAKE) dossubdirs SUBDIRS=base

commands: dummy
	$(MAKE) dossubdirs SUBDIRS=commands

doc: dummy
	$(MAKE) dossubdirs SUBDIRS=doc

dosext: dummy
	$(MAKE) dossubdirs SUBDIRS=dosext

emulator emulator/$(EMULATOR): dummy
	$(MAKE) dossubdirs SUBDIRS=emulator/$(EMULATOR)

env: dummy
	$(MAKE) dossubdirs SUBDIRS=env

tools: dummy
	$(MAKE) dossubdirs SUBDIRS=tools

#
# Configuration/version targets
#

symlinks: include/arch include/emulator

include/arch:
	( cd include ; ln -sf ../arch/$(ARCH)/include arch )

include/emulator:
	( cd include ; ln -sf ../emulator/$(EMULATOR)/include emulator )

Version: dummy include/version.h

config:: symlinks include/version.h
	$(CONFIG_SHELL) LibConfig ./arch/$(ARCH)/config.in

include/version.h: Makefile
	rm -f include/version.h
	@echo \#define VERSION $(VERSION) > include/version.h
	@echo \#define SUBLEVEL $(SUBLEVEL) >> include/version.h
	@echo \#define PATCHLEVEL $(PATCHLEVEL) >> include/version.h
	@echo \#define BIOS_STR \"Dosemu $(VERSION).$(SUBLEVEL).$(PATCHLEVEL) $(ARCH) $(EMULATOR)\" >> include/version.h
	@echo \#define VERSION_STR \"$(VERSION).$(SUBLEVEL).$(PATCHLEVEL)\" >> include/version.h
	@echo \#define ARCH_STR \"$(ARCH)\" >> include/version.h
	@echo \#define EMULATOR_STR \"$(EMULATOR)\" >> include/version.h
	@echo \#define CONFIG_HOST \"`uname -n`\, `uname -s -r -v`\" >> include/version.h
	@echo \#define CONFIG_TIME \"`date`\" >> include/version.h

#
# Library/executable targets
#

ifdef CONFIG_ELF
dos: 	init/emu.o dossubdirs
	$(LD) $(LDFLAGS) -o $@ init/emu.o \
	    $(addprefix -L,$(LIBDIR)) -L. $(EMULIBS) $(LIBS) -lc 

else

dos:	init/dos.o
	$(LD) $(LDFLAGS) -N -o $@ $^

libdosemu: dossubdirs $(LIBDOSEMU)

$(LIBDOSEMU): init/emu.o $(EMULIBS)
	$(LD) $(LDFLAGS) $(MAGIC) -Ttext $(LIBSTART) -nostdlib -o $@ $^ \
	  $(addprefix -L,$(LIBDIR)) -L. $(EMULIBS) $(LIBS) -lc

dosstatic: dossubdirs init/dosstatic.o init/emu.o
	$(LD) $(LDFLAGS) -o $@ init/dosstatic.o init/emu.o
	  $(addprefix -L,$(LIBDIR)) -L. $(EMULIBS) $(LIBS)

endif


#
# X-Windows files
#

x2dos: init/x2dos.o
	@echo "Including x2dos.o "
	$(CC) $(LDFLAGS) -o $@ $< -L$(X11LIBDIR) -lXaw -lXt -lX11

init/x2dos.o: init/x2dos.c
	$(CC) $(CFLAGS) -I/usr/openwin/include -c -o $@ $<

xtermdos:	init/xtermdos.sh
	@echo "#!/bin/sh" > $@
	@echo >> $@
	@echo X11ROOTDIR=$(X11ROOTDIR) >> $@
	@echo >> $@
	@cat $< >> $@

xinstallvgafont:	init/xinstallvgafont.sh
	@echo "#!/bin/sh" > $@
	@echo >> $@
	@echo X11ROOTDIR=$(X11ROOTDIR) >> $@
	@echo >> $@
	@cat $< >> $@

#
# The cleaning & dependency targets
#

clean: arch_clean emulator_clean
	rm -f $(TARGETS) xdos libdosemu* core dosstatic *.tmp dosemu.map
	rm -f `find . -name "*.[ois12]"` `find . -name "*~"`
	rm -f `find . -name "*.y" | sed 's/.y$$/.[ch]/'`
	rm -f `find . -name "*.l" | sed 's/.l$$/.c/'`
	rm -f `find . -name "*.a" -mindepth 2`
	$(MAKE) -C commands clean

realclean: clean arch_realclean emulator_realclean
	rm -f include/config.h include/version.h
	rm -f tools/tools86
	rm -f .config* `find . -name ".depend"` `find . -name "*.d" -print`
	rm -f include/arch include/emulator
	$(MAKE) -C commands realclean
  
depend dep:
	touch include/version.h
	$(CPP) -MM $(CFLAGS) $(CFILES) > .tmpdepend
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i dep; done
	mv .tmpdepend .depend

#
# All of the warning information
#

warning:
	@echo ""
	@echo "If you have not read the documentation, especially the README file, now would"
	@echo "be a really good time to do so."
	@echo ""
	@echo "====> Press Enter to continue, or hit Ctrl-C to abort <===="
	@read



