# Makefile for Amiga Linux main source directory
#
# Copyright 1993 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.


all:	Version vmlinux

.EXPORT_ALL_VARIABLES:

#
# 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 = dummy
endif

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

ROOT_DEV = /dev/hdb1

#
# uncomment the correct keyboard:
#
# The value of KBDFLAGS should be or'ed together from the following
# bits, depending on which features you want enabled.
#
# The least significant bits control if the following keys are "dead".
# The key is dead by default if the bit is on.
# 0x01 - backquote (`)
# 0x02 - accent acute
# 0x04 - circumflex (^)
# 0x08 - tilde (~)
# 0x10 - dieresis (umlaut)

KEYBOARD = -DKBD_FINNISH -DKBDFLAGS=0
# KEYBOARD = -DKBD_FINNISH_LATIN1 -DKBDFLAGS=0x9F
# KEYBOARD = -DKBD_US -DKBDFLAGS=0
# KEYBOARD = -DKBD_GR -DKBDFLAGS=0
# KEYBOARD = -DKBD_GR_LATIN1 -DKBDFLAGS=0x9F
# KEYBOARD = -DKBD_FR -DKBDFLAGS=0
# KEYBOARD = -DKBD_FR_LATIN1 -DKBDFLAGS=0x9F
# KEYBOARD = -DKBD_UK -DKBDFLAGS=0
# KEYBOARD = -DKBD_DK -DKBDFLAGS=0
# KEYBOARD = -DKBD_DK_LATIN1 -DKBDFLAGS=0x9F
# KEYBOARD = -DKBD_DVORAK -DKBDFLAGS=0
# KEYBOARD = -DKBD_SG -DKBDFLAGS=0
# KEYBOARD = -DKBD_SG_LATIN1 -DKBDFLAGS=0x9F
# KEYBOARD = -DKBD_SF -DKBDFLAGS=0
# KEYBOARD = -DKBD_SF_LATIN1 -DKBDFLAGS=0x9F
# KEYBOARD = -DKBD_NO -DKBDFLAGS=0

KERNADDR	 = 0xC0000000
HOSTCC		 = gcc
CC		 = gcc -DKERNEL -b linux
LD		 = /gcc/compilers/linux/2.2.2/ld
KERNELHDRS	 = include
INCFLAGS	 = -nostdinc -I$(KERNELHDRS)
CFLAGS		 = -Wall $(INCFLAGS) -O2 -mc68020
HOSTFLAGS	 = -O2 -fomit-frame-pointer -mc68020 -Wall -Dlinux \
		      -I/gcc/include -I/gcc/os-include -I$(KERNELHDRS)

LDFLAGS 	 = -Ttext $(KERNADDR)

BOOTOBJS	 = tools/bootstrap.o tools/get_nlist.o tools/bootmisc.o

ARCHIVES	 = kernel/kernel.o mm/mm.o fs/fs.o # net/net.o
FILESYSTEMS	 = fs/filesystems.a
DRIVERS 	 = kernel/blk_drv/blk_drv.a kernel/chr_drv/chr_drv.a
MATH		 =
LIBS		 = lib/lib.a
SUBDIRS 	 = kernel lib mm fs # net

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

Version: dummy
	rm -f tools/version.h

config:
	sh Configure < config.in
	mv .config~ .config
ifdef CONFIGURATION
	@echo
	@echo "Configure successful. Try re-making (ignore the error that follows)"
	@echo
	exit 1
endif

linuxsubdirs: dummy
	@for i in $(SUBDIRS); do (cd $$i && echo $$i && $(MAKE) $(MFLAGS)) || exit; done

bootstrap: $(BOOTOBJS)
	$(HOSTCC) $(HOSTFLAGS) -o tmp $(BOOTOBJS)
	blink from tmp to bootstrap
	rm -f tmp

tools/makeinit: tools/makeinit.c
	$(HOSTCC) $(HOSTFLAGS) -o $@ $<

tools/filetodata: tools/filetodata.c
	$(HOSTCC) $(HOSTFLAGS) -o $@ $<

tools/./version.h: tools/version.h

tools/version.h: $(CONFIGURE) Makefile
	@/bin/sh makever.sh
	@echo "#define UTS_RELEASE \"0.03.pl0-`cat .version`\"" > tools/version.h
	@echo "#define UTS_VERSION \"`date +%D`\"" >> tools/version.h
	@echo "#define LINUX_COMPILE_TIME \"`date +%T`\"" >> tools/version.h
	@echo "#define LINUX_COMPILE_BY \"`whoami`\"" >> tools/version.h
	@echo "#define LINUX_COMPILE_HOST \"`hostname`\"" >> tools/version.h

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

vmlinux.amigados : $(CONFIGURE) init/head.o init/main.o init/initdata.o tools/version.o linuxsubdirs
	$(HOSTCC) -nostdlib init/head.o init/main.o init/initdata.o \
		tools/version.o \
		$(ARCHIVES) \
		$(FILESYSTEMS) \
		$(DRIVERS) \
		$(MATH) \
		$(LIBS) \
		-o vmlinux.amigados

init/head.o: init/head.s

init/initdata.o: tools/makeinit init/init
	tools/makeinit init/init init/initdata.o

init/progdata.o: tools/filetodata init/prog
	tools/filetodata init/prog prog init/progdata.o

init/prog: init/prog.c lib/lib.a
	gcc -b linux -fwritable-strings -nostdlib \
		-Iinclude -O2 -fomit-frame-pointer -o $@ init/prog.c lib/lib.a

init/init: init/init.c init/progdata.o lib/lib.a
	gcc -b linux -fwritable-strings -nostdlib -Xlinker -N \
		-Iinclude -O2 -fomit-frame-pointer -o $@ \
		init/init.c init/progdata.o lib/lib.a

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

tools/bootstrap.o: tools/bootstrap.c
	$(HOSTCC) $(HOSTFLAGS) -o $@ -c $<

tools/bootmisc.o: tools/bootmisc.c
	$(HOSTCC) $(HOSTFLAGS) -o $@ -c $<

tools/get_nlist.o: tools/get_nlist.c
	$(HOSTCC) $(HOSTFLAGS) -o $@ -c $<

zBoot/zSystem: zBoot/*.c zBoot/*.S tools/zSystem
	cd zBoot;$(MAKE)

zImage: $(CONFIGURE) boot/bootsect boot/setup zBoot/zSystem tools/build
	tools/build boot/bootsect boot/setup zBoot/zSystem $(ROOT_DEV) > zImage
	sync

zdisk: zImage
	dd bs=8192 if=zImage of=/dev/fd0

zlilo: $(CONFIGURE) zImage
	cat zImage > /vmlinuz
	/etc/lilo/install


tools/zSystem:	boot/head.o init/main.o tools/version.o linuxsubdirs
	$(LD) $(LDFLAGS) -T 100000 -M boot/head.o init/main.o tools/version.o \
		$(ARCHIVES) \
		$(FILESYSTEMS) \
		$(DRIVERS) \
		$(MATH) \
		$(LIBS) \
		-o tools/zSystem > zSystem.map

fs: dummy
	$(MAKE) $(MFLAGS) linuxsubdirs SUBDIRS=fs

mm: dummy
	$(MAKE) $(MFLAGS) linuxsubdirs SUBDIRS=mm

kernel: dummy
	$(MAKE) $(MFLAGS) linuxsubdirs SUBDIRS=kernel

clean:
	rm -f System.map
	rm -f init/*.o init/init tools/*.o
	for i in $(SUBDIRS); do (cd $$i && $(MAKE) $(MFLAGS) clean); done

clobber: clean
	rm -f vmlinux bootstrap tools/makeinit

mrproper: clean
	rm -f include/linux/autoconf.h tools/version.h
	rm -f .version .config*
	rm -f `find . -name .depend -print`

backup: clean
	cd .. && tar cf - linux | gzip -9 > backup.z

depend dep:
	touch tools/version.h
	for i in init/*.c;do (echo -n init/;$(CPP) -M -Iinclude $$i); \
		done > .depend~
	for i in tools/*.c;do (echo -n tools/;$(HOSTCC) $(HOSTFLAGS) \
		-E -M $$i); done >> .depend~
	for i in $(SUBDIRS); do (cd $$i && $(MAKE) $(MFLAGS) dep) || exit; done
	rm -f tools/version.h
	mv .depend~ .depend

ifdef CONFIGURATION
.$(CONFIGURATION):
	@echo ""
	@echo "You have no" .$(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
