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

VERSION = 0.08
PATCHLEVEL = 3
ALPHA =

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
BOOTOBJS := tools/amiga/bootstrap.o tools/amiga/get_nlist.o
ARCHIVES := $(ARCHIVES) amiga/amiga.o
SUBDIRS := $(SUBDIRS) amiga
endif

ifdef CONFIG_ATARI
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) tools/version.h

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


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

bootstrap: $(BOOTOBJS)
	$(HOSTCC) $(HOSTFLAGS) -o $@ $(BOOTOBJS)

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

tools/version.h: $(CONFIGURE) Makefile
	@/bin/sh makever.sh
	@echo \#define UTS_RELEASE \"$(VERSION).$(PATCHLEVEL)$(ALPHA)\" > tools/version.h
	@echo \#define UTS_VERSION \"\#`cat .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
	@echo \#define LINUX_COMPILE_DOMAIN \"`domainname`\" >> tools/version.h

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

ifdef CONFIG_ATARI

# The "-fwritable-strings" is needed for a strange reason on the
# Atari: The first 2 KByte of physical memory (0x0 .. 0x7ff) are made
# supervisor-only by hardware. In most cases this doesn't matter,
# because at this location is the kernel that runs in super mode.
# But...: When executing the execve()s in init(), the argv and envp
# fields are accessed from user space (with get_fs_byte()) to be
# copied to the new process. And since main.o is linked as second
# file, its code lies within the magic first 2 KByte. And here it
# happens... a spurious bus error the memory manager can't handle. The
# solution is to move the offending strings into the kernel data, far
# behind 0x7ff. -fwritable-strings does exactly this.

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

else

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

endif

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


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

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

# This target makes vmlinux and then transfers it from the PC to the Atari. This
# is just for my personal cross-development environment.
remote-vmlinux: all
	rh_put -f -q /home/roman/src/linux.atari/vmlinux 'd:\linux\vmlinux'

m68k/head.o: m68k/head.S #m68k/assyms.h

m68k/assyms.h: dummy
	make -C m68k assyms.h

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

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

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

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

drivers: dummy
	$(MAKE) linuxsubdirs SUBDIRS=drivers

net: dummy
	$(MAKE) linuxsubdirs SUBDIRS=net

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

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

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


clean:
	$(RM) $(RMFLAGS) core `find . -name '*.[oa]' -print`
	$(RM) $(RMFLAGS) core `find . -name 'core' -print`
	$(RM) $(RMFLAGS) System.map
	$(RM) $(RMFLAGS) 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 tools/version.h
	$(RM) $(RMFLAGS) .version .config* config.old
	$(RM) $(RMFLAGS) .depend `find . -name '.depend' -print`

distclean: mrproper

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

depend dep:
	-touch tools/version.h
	for i in init/*.c;do (echo -n init/;$(CPP) -M $(INCFLAGS) $$i); \
		done > .depend~
	for i in $(BOOTOBJS:.o=.c); do (echo -n tools/atari/;$(HOSTCC) \
		$(HOSTINCFLAGS) -M $$i); done >> .depend~
	set -e ; for i in $(SUBDIRS); do $(MAKE) $(MFLAGS) -C $$i dep; done
	$(RM) $(RMFLAGS) tools/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
