# 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

#
# This probably won't help at all...
#
ifndef CONFIG_CLUEFUL
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 arch/arch.o # net/net.o
FILESYSTEMS	 = fs/filesystems.a
DRIVERS 	 = kernel/blk_drv/blk_drv.a kernel/chr_drv/chr_drv.a \
		   kernel/blk_drv/scsi/scsi.a
MATH		 =
LIBS		 = lib/lib.a
SUBDIRS 	 = kernel lib mm fs ipc arch # net
TOOLS		 = tools/makeinit tools/filetodata

PROGTOWRITE	 = notsh

ifdef CONFIG_AMIGA
BOOTOBJS	 = tools/amiga/bootstrap.o tools/amiga/get_nlist.o
ARCHIVES	 += amiga/amiga.o
SUBDIRS 	 += amiga
endif

ifdef CONFIG_ATARI
ARCHIVES	 += atari/atari.o
SUBDIRS 	 += atari
endif

ifdef CONFIG_MAC
ARCHIVES	 += mac/mac.o
SUBDIRS 	 += mac
endif

Version: dummy
	rm -f init/version.h

config:
	sh Configure $(OPTS) < 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 cd $$i && $(MAKE) $(MFLAGS)) || exit; done

bootstrap: $(BOOTOBJS)
	$(HOSTCC) $(HOSTFLAGS) -o tmp $(BOOTOBJS)
ifdef CONFIG_AMIGA
	blink from tmp to bootstrap chip
	rm -f tmp
else
	mv tmp bootstrap
endif

tools : $(TOOLS)

$(TOOLS) : $(addsuffix .c,$(TOOLS))
	$(HOSTCC) $(HOSTFLAGS) $@.c -o $@

init/./version.h: init/version.h

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

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

# this target just creates an amigados format executable of
# the kernel so that you can run an amigados disassembler on it.
vmlinux.amigados : $(CONFIGURE) arch/head.o init/main.o init/config.o init/initdata.o init/version.o linuxsubdirs
	$(HOSTCC) -nostdlib arch/head.o init/main.o init/config.o init/initdata.o \
		init/version.o \
		$(ARCHIVES) \
		$(FILESYSTEMS) \
		$(DRIVERS) \
		$(MATH) \
		$(LIBS) \
		-o vmlinux.amigados

arch/head.o: arch/head.S arch/assyms.h

arch/assyms.h:
	cd arch && make assyms.h

arch/crt0.o: arch/crt0.S

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

init/$(PROGTOWRITE)data.o: tools/filetodata init/$(PROGTOWRITE)
	tools/filetodata init/$(PROGTOWRITE) prog init/$(PROGTOWRITE)data.o

init/notsh: init/notsh.c lib/lib.a arch/crt0.o
	gcc -b linux -nostdlib $(INCFLAGS) \
		-O2 -fomit-frame-pointer -o $@ arch/crt0.o \
		init/notsh.c lib/lib.a

ifdef WRITEPROG
init/init: init/init.c init/$(PROGTOWRITE)data.o init/version.o lib/lib.a
	gcc -b linux -fwritable-strings -nostdlib -Xlinker -N \
		-DPROGTOWRITE=\"/bin/$(PROGTOWRITE)\" $(INCFLAGS) -O2 \
		-o $@ init/init.c init/$(PROGTOWRITE)data.o init/version.o \
		lib/lib.a
else
init/init: init/init.c init/version.o lib/lib.a
	gcc -b linux -fwritable-strings -nostdlib -Xlinker -N \
		$(INCFLAGS) -O2 -o $@ init/init.c init/version.o lib/lib.a
endif

init/version.o: init/version.c init/version.h

$(BOOTOBJS): $(BOOTOBJS:.o=.c)
	$(HOSTCC) $(HOSTFLAGS) -c $*.c -o $@

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
	if [ -f /vmlinuz ]; then mv /vmlinuz /vmlinuz.old; fi
	cat zImage > /vmlinuz
	/etc/lilo/install


tools/zSystem:	arch/head.o init/main.o tools/version.o linuxsubdirs
	$(LD) $(LDFLAGS) -T 100000 -M arch/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

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

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

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


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

clobber: clean
	rm -f vmlinux bootstrap $(TOOLS)

mrproper: clean
	rm -f include/linux/autoconf.h init/version.h
	rm -f .version .config*

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

depend dep:
	touch init/version.h
	for i in init/*.c;do (echo -n init/;$(CPP) -M $(INCFLAGS) $$i); \
		done > .depend~
	for i in $(addsuffix .c,$(TOOLS)); do (echo -n tools/;$(HOSTCC) \
		$(HOSTINCFLAGS) -M $$i); done >> .depend~
	for i in $(BOOTOBJS:.o=.c); do (echo -n tools/amiga/;$(HOSTCC) \
		$(HOSTINCFLAGS) -M $$i); done >> .depend~
	for i in $(SUBDIRS); do (cd $$i && $(MAKE) $(MFLAGS) dep) || exit; done
	rm -f init/version.h
	mv .depend~ .depend

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
