# Makefile for Linux/68k main source directory
#
# Copyright 1993,1994 by Hamish Macdonald
#
# This file is subject to the terms and conditions of the GNU General Public
# License.  See the file "README.legal" in the main directory of this archive
# for more details.

VERSION = 0
PATCHLEVEL = 9
SUBLEVEL = 13

all:	Version vmlinux

.EXPORT_ALL_VARIABLES:

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

#
# 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
CONFIGURE = .config
endif

#
# ROOT_DEV specifies the default root-device when making the image.
# This can be either FLOPPY, CURRENT, /dev/xxxx or empty, in which case
# the default of FLOPPY is used by 'build'.
#

ROOT_DEV = CURRENT

# Special options.
#OPTS	= -pro

# Include the make variables (CC, etc...)
#
include MakeVars


ARCHIVES	 =kernel/kernel.o mm/mm.o fs/fs.o ipc/ipc.o m68k/arch.o net/net.o
FILESYSTEMS	 =fs/filesystems.a
DRIVERS 	 =drivers/block/block.a \
		  drivers/char/char.a \
		  drivers/net/net.a \
#		  ibcs/ibcs.o
LIBS		 =lib/lib.a
SUBDIRS 	 =kernel drivers mm fs ipc m68k lib net

ifdef CONFIG_SCSI
DRIVERS := $(DRIVERS) drivers/scsi/scsi.a
endif

ifdef CONFIG_SOUND
DRIVERS := $(DRIVERS) drivers/sound/sound.a
endif

ifdef CONFIG_MATH_EMULATION
DRIVERS := $(DRIVERS) drivers/FPU-emu/math.a
endif

ifdef CONFIG_AMIGA
AMIGA_BOOTOBJS := tools/amiga/bootstrap.o
ARCHIVES := $(ARCHIVES) amiga/amiga.o
SUBDIRS := $(SUBDIRS) amiga
endif

ifdef CONFIG_ATARI
ATARI_BOOTOBJS := tools/atari/bootstrap.o
ARCHIVES := $(ARCHIVES) atari/atari.o
SUBDIRS := $(SUBDIRS) atari
endif

ifdef CONFIG_MAC
ARCHIVES := $(ARCHIVES) mac/mac.o
SUBDIRS := $(SUBDIRS) mac
endif

ifdef CONFIG_FPSP_040
ARCHIVES := $(ARCHIVES) fpsp040/fpsp.o
SUBDIRS := $(SUBDIRS) fpsp040
endif

Version: dummy
	$(RM) $(RMFLAGS) include/linux/version.h

config:
	$(CONFIG_SHELL) Configure $(OPTS) < config.in
	@if grep -s '^CONFIG_SOUND' .tmpconfig ; then \
		$(MAKE) -C drivers/sound config; \
		else : ; fi
	mv .tmpconfig .config


linuxsubdirs: $(SUBDIRS)

ifdef CONFIG_ATARI
atari_bootstrap: $(ATARI_BOOTOBJS)
	$(HOSTCC) $(HOSTFLAGS) -o $@ $(ATARI_BOOTOBJS)
	rm -f bootstrap
	ln $@ bootstrap
endif

ifdef CONFIG_AMIGA
amiga_bootstrap: $(AMIGA_BOOTOBJS)
	$(HOSTCC) $(HOSTFLAGS) -o $@ $(AMIGA_BOOTOBJS)
	rm -f bootstrap
	ln $@ bootstrap
endif

include/linux/./version.h: include/linux/version.h

include/linux/version.h: $(CONFIGURE) Makefile
	@/bin/sh makever.sh
	@echo \#define UTS_RELEASE \"$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)\" > include/linux/version.h
	@if [ -f .name ]; then \
	   echo \#define UTS_VERSION \"\#`cat .version`-`cat .name` `date`\"; \
	 else \
	   echo \#define UTS_VERSION \"\#`cat .version` `date`\";  \
	 fi >> include/linux/version.h 
	@echo \#define LINUX_COMPILE_TIME \"`date +%T`\" >> include/linux/version.h
	@echo \#define LINUX_COMPILE_BY \"`whoami`\" >> include/linux/version.h
	@echo \#define LINUX_COMPILE_HOST \"`hostname`\" >> include/linux/version.h
	@if [ -x /bin/dnsdomainname ]; then \
	   echo \#define LINUX_COMPILE_DOMAIN \"`dnsdomainname`\"; \
	 else \
	   echo \#define LINUX_COMPILE_DOMAIN \"`domainname`\"; \
	 fi >> include/linux/version.h
	@echo \#define LINUX_COMPILER \"`$(CC) -v 2>&1 | tail -1`\" >> include/linux/version.h
	@echo \#define LINUX_VERSION_CODE `expr $(VERSION) \\* 65536 + $(PATCHLEVEL) \\* 256 + $(SUBLEVEL)` >> include/linux/version.h

tools/version.o: tools/version.c include/linux/version.h

init/main.o: $(CONFIGURE) init/main.c
	$(CC) $(CFLAGS) $(PROFILING) -c -o $*.o $<

init/config.o: $(CONFIGURE) init/config.c
	$(CC) $(CFLAGS) -c -o $*.o $<

vmlinux: m68k/head.o init/main.o init/config.o tools/version.o linuxsubdirs
	$(LD) $(LDFLAGS) -M m68k/head.o init/main.o init/config.o \
		tools/version.o \
		$(ARCHIVES) \
		$(FILESYSTEMS) \
		$(DRIVERS) \
		$(LIBS) \
		-o vmlinux > System.map

m68k/head.o: m68k/head.S

$(AMIGA_BOOTOBJS) $(ATARI_BOOTOBJS): %.o: %.c
	$(HOSTCC) $(HOSTFLAGS) -c $< -o $@

ifdef SUBDIRS
.PHONY: $(SUBDIRS)
$(SUBDIRS):
	$(MAKE) -C $@
endif

lilo:	vmlinux
	if [ -f $(INSTALL_PATH)/vmlinux ]; then mv -f $(INSTALL_PATH)/vmlinux $(INSTALL_PATH)/vmlinux.old; fi
	if [ -f $(INSTALL_PATH)/System.map ]; then mv -f $(INSTALL_PATH)/System.map $(INSTALL_PATH)/System.old; fi
	cat vmlinux > $(INSTALL_PATH)/vmlinux
	cp System.map $(INSTALL_PATH)/
	if [ -x /sbin/lilo ]; then /sbin/lilo; else /etc/lilo/install; fi

clean:
	$(RM) $(RMFLAGS) kernel/ksyms.lst
	find -name '*.[oa]' -o -name core | xargs $(RM) $(RMFLAGS)
	$(RM) $(RMFLAGS) System.map
	$(RM) $(RMFLAGS) .tmp* drivers/sound/configure

clobber: clean
	$(RM) $(RMFLAGS) vmlinux bootstrap m68k/gensyms
	$(RM) $(RMFLAGS) drivers/sound/local.h

mrproper: clobber
	$(RM) $(RMFLAGS) include/linux/autoconf.h include/linux/version.h
	$(RM) $(RMFLAGS) .version .config* config.old
	find -name .depend | xargs $(RM) $(RMFLAGS)

distclean: mrproper

backup: mrproper
	cd .. && tar cf - linux | gzip -9 > backup.gz
	sync

depend dep:
	-touch include/linux/version.h
	for i in init/*.c;do (echo -n init/;$(CPP) -M $(INCFLAGS) $$i); done > .tmpdepend
	for i in $(SUBDIRS); do $(MAKE) $(MFLAGS) -C $$i dep; done
	$(RM) $(RMFLAGS) include/linux/version.h
	mv .tmpdepend .depend

amigadep:
ifdef CONFIG_AMIGA
	for i in $(AMIGA_BOOTOBJS:.o=.c); do (echo -n tools/amiga/;$(HOSTCC) $(HOSTINCFLAGS) -M $$i); done >> .depend
endif

ataridep:
ifdef CONFIG_ATARI
	for i in $(ATARI_BOOTOBJS:.o=.c); do (echo -n tools/atari/;$(HOSTCC) $(HOSTINCFLAGS) -M $$i); done >> .depend
endif

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

dummy: ..$(CONFIGURATION)

else

dummy:

endif
