# 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: Version warn2key doslib

#
# Set up special OS definitions
#

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

INCDIR  = -I. -I$(TOPDIR)/include # -I$(LINUX_INCLUDE)
LIBDIR  = lib

ifdef X11LIBDIR
INCDIR := $(INCDIR) -I$(X11INCDIR)
endif

CFLAGS = -pipe -fstrict-prototypes -Wall $(INCDIR) -O2 # -funroll-loops
         # -fno-inline

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

ifdef X11LIBDIR
CFLAGS := $(CFLAGS) -DX_SUPPORT
endif

#
# Set up our list of targets
#

#
#ARCHIVES	=
#LIBS		=base/base.a
#SUBDIRS		=env ...
#
#ifdef CONFIG_SCSI
#DRIVERS := $(DRIVERS) drivers/scsi/scsi.a
#endif
#
#include arch/$(ARCH)/Makefile

CFILES  = init/emu.c init/dos.c init/dosstatic.c
SUBDIRS = 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

SUBDIRS := $(SUBDIRS) commands

ifdef CONFIG_DOC
SUBDIRS := $(SUBDIRS) doc
DOCS    = doc
endif

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

# The following sets up the X windows support for DOSEMU.
ifdef X11LIBDIR
X_SUPPORT  = 1
X2_SUPPORT = 1
#the -u forces the X11 shared library to be linked into ./dos
ifdef CONFIG_ELF
LIBS := $(LIBS) -Wl,-rpath,$(X11LIBDIR)/elf -lX11
else
LIBS := $(LIBS) -L$(X11LIBDIR) -lX11 -u _XOpenDisplay
endif
endif

ifdef X2_SUPPORT
CFILES  := $(CFILES) init/x2dos.c
X2CEXE   = x2dos xtermdos xinstallvgafont
X2DEFS   = -DX_SUPPORT
endif

#
# export the necessary variables
#

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

.EXPORT_ALL_VARIABLES:

doeverything: Version warn2delay doc installnew

most: Version warn2delay installnew

itall: Version warn2delay doc installnew

all:: Version warnconf tools commands dos $(X2CEXE)

ifndef CONFIG_ELF
all:: libdosemu
endif

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

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 )

config.in: ./arch/$(ARCH)/config.in
	cp $< $@

Version:
#	rm -f include/version.h

include/version.h: $(CONFIGURATION) Makefile
	@echo \#define VERSION $(VERSION) > include/version.h
	@echo \#define PATCHLEVEL $(PATCHLEVEL) >> include/version.h
	@echo \#define SUBLEVEL $(SUBLEVEL) >> include/version.h
	@echo \#define BIOS_STR \"Dosemu $(VERSION).$(PATCHLEVEL).$(SUBLEVEL) $(ARCH) $(EMULATOR)\" >> include/version.h
	@echo \#define VERSION_STR \"$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)\" >> 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

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

warnconf: dummy include/config.h

include/config.h: Makefile 
ifeq (include/config.h,$(wildcard include/config.h))
	@echo "WARNING: Your Makefile has changed since config.h was generated."
	@echo "         Consider doing a 'make config' to be safe."
else
endif

dos.o: include/config.h

x2dos.o: include/config.h x2dos.c
	$(CC) $(CFLAGS) -I/usr/openwin/include -c x2dos.c

DOSSTATICFILES = dosstatic.c emu.o

dosstatic:	$(DOSSTATICFILES) ${addsuffix .a,${addprefix lib/lib,$(LIBS)}}
	$(LD) $(LDFLAGS) -o $@ $(DOSSTATICFILES) \
		$(addprefix -L,$(LIBDIR)) -L. \
		$(ARCHIVES) $(EMULIBS) $(LIBS)

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

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

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


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

else

dos:	init/dos.o
	$(LD) $(LDFLAGS) -N -o $@ $^ $(addprefix -L,$(LIBDIR))

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

libdosemu:	dossubdirs $(LIBDOSEMU)

endif

doslib: all
	@echo ""
	@echo "---------------------------------DONE compiling-------------------------------"
	@echo ""
	@echo " Now you must install DOSEMU. Make sure you are root and:"
	@echo " make install"
	@echo ""
	@echo ""

installnew: doslib install

install:
	@install -d /var/lib/dosemu
	@install -d /var/run
	@if [ -f load_module.sh ]; then chmod 700 load_module.sh; fi
	@if [ -f unload_module.sh ]; then chmod 700 unload_module.sh; fi
ifdef CONFIG_ELF
	@nm dos | grep -v '\(compiled\)\|\(\.o$$\)\|\( a \)' | \
		sort > dosemu.map
else
	@nm $(LIBDOSEMU) | grep -v '\(compiled\)\|\(\.o$$\)\|\( a \)' | \
		sort > dosemu.map
endif
	@if [ -f /lib/libemu ]; then rm -f /lib/libemu ; fi
	@for i in $(SUBDIRS); do \
		(cd $$i && echo $$i && $(MAKE) install) || exit; \
	done
	@install -c -o root -m 04755 dos /usr/bin
ifndef CONFIG_ELF
	@install -m 0644 $(LIBDOSEMU) /usr/lib
	@(cd /usr/lib; ln -sf $(LIBDOSEMU) libdosemu)
endif
	@if [ -f /usr/bin/xdosemu ]; then \
		install -m 0700 /usr/bin/xdosemu /tmp; \
		rm -f /usr/bin/xdosemu; \
	fi
	@if [ -f $(BOOTDIR)/sillyint.o ]; then rm -f $(BOOTDIR)/sillyint.o ; fi
	@install -m 0755 -d $(BOOTDIR)
ifdef X_SUPPORT
	@ln -sf dos xdos
	@install -m 0755 xtermdos /usr/bin
	@if [ ! -e /usr/bin/xdos ]; then ln -s dos /usr/bin/xdos; fi
	@echo ""
	@echo "-> Main DOSEMU files installation done. Installing the Xwindows PC-8 font..."
	@if [ -w $(X11LIBDIR)/X11/fonts/misc ] && [ -d $(X11LIBDIR)/X11/fonts/misc ]; then \
		if [ ! -e $(X11LIBDIR)/X11/fonts/misc/vga.pcf* ]; then \
			install -m 0644 vga.pcf $(X11LIBDIR)/X11/fonts/misc; \
			cd $(X11LIBDIR)/X11/fonts/misc; \
			mkfontdir; \
		fi \
	fi
endif
	@echo ""; \
	 echo "---------------------------------DONE Installing-------------------------------"; \
	 echo ""; \
	 echo "  - You need to configure DOSEMU. Read 'config.dist' in the 'examples' dir."; \
	 echo "  - Update your /etc/dosemu.conf by editing a copy of './examples/config.dist'"; \
	 echo "  - Using your old DOSEMU 0.52 configuration file might not work."; \
	 echo "  - After configuring DOSEMU, you can type 'dos' to run DOSEMU."
ifdef X_SUPPORT
	@echo "  - Use 'xdos' instead of 'dos' to cause DOSEMU to open its own Xwindow."; \
	 echo "  - Type 'xset fp rehash' before running 'xdos' for the first time ever."; \
	 echo "  - To make your backspace and delete key work properly in 'xdos', type:"; \
	 echo "		xmodmap -e \"keycode 107 = 0xffff\""; \
	 echo "		xmodmap -e \"keycode 22 = 0xff08\""; \
	 echo "		xmodmap -e \"key 108 = 0xff0d\"  [Return = 0xff0d]"; \
	 echo ""
endif
	@echo "  - Try the ./commands/mouse.com if your INTERNAL mouse won't work"; \
	 echo "  - Try ./commands/unix.com to run a Unix command under DOSEMU"; \
	 echo ""
ifdef CONFIG_EMUMODULE
	@echo "  - DO NOT FORGET TO LOAD EMUMODULE"; \
	 echo "    use load_module.sh from this directory."; \
	 echo ""
endif

converthd: hdimage
	mv hdimage hdimage.preconvert
	periph/mkhdimage -h 4 -s 17 -c 40 | cat - hdimage.preconvert > hdimage
	@echo "Your hdimage is now converted and ready to use with 0.52 and above!"

newhd: periph/bootsect
	periph/mkhdimage -h 4 -s 17 -c 40 | cat - periph/bootsect > newhd
	@echo "You now have a hdimage file called 'newhd'"

checkin::
	-ci -l -M $(CFILES) $(HFILES) $(SFILES) $(OFILES)
	@for i in $(SUBDIRS); do (cd $$i && echo $$i && $(MAKE) checkin) || exit; done

checkout::
	-co -M -l $(CFILES) $(HFILES) $(SFILES) $(OFILES)
	@for i in $(SUBDIRS); do (cd $$i && echo $$i && $(MAKE) checkout) || exit; done

dist:: $(CFILES) $(HFILES) $(SFILES) $(OFILES) $(BFILES) include/config.h
	install -d $(DISTPATH)
	install -d $(DISTPATH)/lib
	cp -a dosemu.xpm libslang.a $(CFILES) $(HFILES) $(SFILES) $(OFILES) $(BFILES) $(DISTPATH)
	cp -a hdimages/hdimage.dist $(DISTPATH)/hdimage.dist
	cp -a hddummy $(DISTPATH)/hddummy
	@for i in $(REQUIRED) $(LIBS) $(SUBDIRS) $(DOCS) ipxutils $(OPTIONALSUBDIRS) ipxbridge; do \
	    (cd $$i && echo $$i && $(MAKE) dist) || exit; \
	done
	(cd $(DISTBASE); tar cf - $(DISTNAME) | gzip -9 >$(DISTFILE))
	rm -rf $(DISTPATH)
	@echo "FINAL .tgz FILE:"
	@ls -l $(DISTFILE)

clean:
	-rm -f *~ $(OBJS) $(X2CEXE) x2dos.o dos xdos libdosemu-* *.[so] core \
	  dosstatic *.tmp dosemu.map
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i clean; done

realclean: local_realclean local_clean
	-rm -f include/config.h include/kversion.h tools/tools86 .depend \
	  .config config.in config.out
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i realclean; done

pristine: realclean
	-rm -f lib/*

depend dep:
	touch include/version.h
	$(CPP) -MM $(CFLAGS) $(CFILES) > .tmpdepend
	for i in tools/*.c;do echo -n "tools/";$(CPP) -M $$i;done >> .tmpdepend
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i dep; done
	mv .tmpdepend .depend

echo::
	for i in $(SUBDIRS); do \
		$(MAKE) -C $$i $@  || exit 1; \
	done

#
# All of the warning information
#

warn2key: warning2 pausekey

warn2delay: warning2 pausedelay

warning2: dummy 
	@echo ""; \
	 echo "Starting DOSEMU "$(VERSION)"."$(SUBLEVEL)" compile..."; \
	 echo ""; \
	 echo "-> IMPORTANT!"; \
	 echo "    - Please read 'QuickStart' file before compiling DOSEMU!"; \
	 echo "    - Location and format of DOSEMU files have changed since 0.60!"; \
	 echo ""; \
	 echo "-> REQUIREMENTS for DOSEMU:"; \
	 echo "    - gcc 2.5.8"; \
	 echo "    - libc 4.5.21"; \
	 echo "    - Linux 1.1.45"; \
	 echo "    - 16 megabytes total memory+swap"; \
	 echo ""; \
	 echo "-> AUTOMATIC DETECTION:"; \
	 if [ "1" = "$(X_SUPPORT)" ]; then \
	 	 echo "    - Found X11 development files, X-libs in $(X11LIBDIR)."; \
	 	 echo "      DOSEMU will be compiled with X-window support."; \
	 else \
	 	 echo "    - Could not find the X11 development files"; \
	 	 echo "      DOSEMU will be compiled without X-window support."; \
	 fi; \
	 if [ "1" = "$(WIN31)" ]; then \
	 	 echo "    - Your kernel supports the experimental use of Windows 3.1."; \
	 else \
	 	 echo "    - Kernel is not patched for experimental use of Windows 3.1."; \
	 fi; \
	 echo "";

pausekey:
	@echo "====> Press Enter to continue, or hit Ctrl-C to abort <===="
	@read

pausedelay:
	@echo "====> HIT CTRL-C NOW to abort if you forgot something! <===="
	@sleep 3

.PHONY: help
help:
	@echo "'make dep' will make the dependency files"; \
	 echo "Each .c file has a corresponding .d file (the dependencies)"; \
	 echo ""; \
	 echo ""; \
	 echo "'make dossubdirs' will make the follow targets:"; \
	 echo "     $(SUBDIRS)"; \
	 echo ""; \
	 echo "To clean a directory, do make -C <dirname> clean|realclean"

#
# 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

