# /usr/src/Makefile for MacMach

include /usr/src/COPYRIGHTS

# This is the primary Makefile for the MacMach system.

# `make all'        just compile everything (the default)
# `make install'    install binaries to DESTDIR (make "all" or "build" first)
# `make clean'      remove garabage
# `make compress'   compress all source files
# `make uncompress' uncompress all source files
# `make build'      rebuild everything

# Note that all, install, clean, compress and uncompress should be
# available (with the same semantics) from any Makefile.

# DESTDIR specifies the destination directory for "install" and "mkdir"
DESTDIR=

# library / compiler directories
LIBDIR=	lib usr.lib

# order is important, old and man must be #1 and #2
SRCDIR= old man bin usr.bin etc ucb games

# standard system directories for "make mkdir"
MKDIR=	/tmp /lib /bin /etc /dev /usr /usr/etc /usr/tmp /usr/include \
	/usr/lib /usr/bin /usr/ucb /usr/man /usr/man/info /usr/spool \
	/usr/adm /usr/old /usr/lib/tmac /usr/lib/learn /usr/games \
	/usr/games/lib /usr/games/hide /usr/bin/X11 /usr/lib/X11 \
	/usr/include/X11

#ALL=	${LIBDIR} ${SRCDIR} X11 vmunix tags
ALL=	${LIBDIR} ${SRCDIR} X11 tags
BUILD=	uncompress Makefiles clean includes depend compiler libraries

# default action -- compile everything but don't install anything
# Note that, for most things, this will rebuild ".depend" after a "clean".
all: startall ${ALL}
	@echo; echo -n "=== system compilation completed at: "; date
startall: FRC
	@echo; echo -n "=== beginning system compilation at: "; date

# build  everything, updates libc, cc, /usr/include and /usr/lib
build: startbuild ${BUILD} ${ALL}
	@echo; echo -n "=== system build completed at: "; date
startbuild: FRC
	@echo; echo -n "=== beginning system build at: "; date

# make and install /usr/include
# note that DESTDIR is not used here, works with local file system only
# also note that some of the X11 includes are not installed until the
# libraries are installed, see libraries below
includes: FRC
	@echo; echo -n "=== building/installing include files at: "; date
	mv /usr/include  /usr/include.rm
	mkdir /usr/include
	cd include; make ${MFLAGS}
	cd include; make ${MFLAGS} install
	cd X11; make ${MFLAGS} includes
	cd X11/include; make ${MFLAGS} install
	rm -rf /usr/include.rm

# make and install /lib/libc.a and the C compiler twice
# note that DESTDIR is not used here, installs to local file system only
compiler: FRC
	@echo; echo -n "=== building libc first time at: "; date
	cd lib/libc; make ${MFLAGS}
	cd lib/libc; make ${MFLAGS} install
	@echo; echo -n "=== building compiler first time at: "; date
	cd lib/gcc; make ${MFLAGS}
	cd lib/gcc; make ${MFLAGS} install
	cd lib; make ${MFLAGS} clean
	@echo; echo -n "=== building libc second time at: "; date
	cd lib/libc; make ${MFLAGS}
	cd lib/libc; make ${MFLAGS} install
	@echo; echo -n "=== building compiler second time at: "; date
	cd lib/gcc; make ${MFLAGS}
	cd lib/gcc; make ${MFLAGS} install

# make and install libraries in /usr/lib
# note that DESTDIR is not used here, works with local file system only
# when installing X11 libraries, a bunch of include files are installed too
libraries: FRC
	@echo; echo -n "=== building libraries at: "; date
	cd usr.lib; make ${MFLAGS}
	cd usr.lib; make ${MFLAGS} install
	cd X11/lib; make ${MFLAGS}
	cd X11/lib; make ${MFLAGS} install

# install everything to DESTDIR
install: startinstall mkdir
	-@for i in ${SRCDIR} X11 ${LIBDIR} include; do \
	  echo; echo -n "=== installing $$i at: "; date; \
	  (cd $$i; \
	  make ${MFLAGS} DESTDIR=${DESTDIR} install); \
	done
	@echo; echo -n "=== linking X to Xmacmach at: "; date
	rm -rf ${DESTDIR}/usr/bin/X11/X
	ln -s Xmacmach ${DESTDIR}/usr/bin/X11/X
	@echo; echo -n "=== installing X11 man pages at: "; date
	cd X11; make ${MFLAGS} DESTDIR=${DESTDIR} install.man
	-@if [ -f sys/vmunix ]; then \
	  echo; echo -n "=== installing /vmunix at: "; date; \
	  mv ${DESTDIR}/vmunix ${DESTDIR}/vmunix.old; \
	  install -c -m 444 -o root -g bin sys/vmunix ${DESTDIR}/vmunix; \
	fi
	@echo; echo -n "=== installing /usr/lib/tags at: "; date
	install -c -m 444 -o bin -g bin tags ${DESTDIR}/usr/lib/tags
	@echo; echo -n "=== installation complete at: "; date

startinstall: FRC
	@echo; echo -n "=== installing system to ${DESTDIR}/ at: "; date

# make all the standard directories on DESTDIR
mkdir:	FRC
	@echo; echo -n "=== creating directories on ${DESTDIR}/ at: "; date
	-for i in ${MKDIR}; do \
	  [ -d ${DESTDIR}$$i ] || mkdir ${DESTDIR}$$i; \
	  chown root.bin ${DESTDIR}$$i; \
	  chmod 755 ${DESTDIR}$$i; \
	done
	chmod 777 ${DESTDIR}/tmp ${DESTDIR}/usr/tmp
	chown games.bin ${DESTDIR}/usr/games/lib ${DESTDIR}/usr/games/hide

# make X11 Makefiles
Makefiles: FRC
	@echo; echo -n "=== rebuilding X11 Makefiles at: "; date
	cd X11/config; make imake
	cd X11; make Makefiles

# make X11 dependencies
depend: FRC
	cd X11; make depend

# build new kernel
vmunix:	FRC
	@echo; echo -n "=== building new kernel at: "; date
	cd sys; make vmunix

# clean up everything
clean:	FRC
	rm -f tags
	@for i in include ${LIBDIR} X11 ${SRCDIR}; do \
	  echo; echo -n "=== cleaning $$i at: "; date; \
	  (cd $$i; make -k ${MFLAGS} clean); \
	done
	@echo; echo -n "=== cleaning sys at: "; date
	rm -rf sys/MACMACH*/*

# make (not install) libraries and compiler
${LIBDIR}: FRC
	@echo; echo -n "=== compiling $@ at: "; date
	cd $@; make ${MFLAGS}

# how to make (not install) everything else but libraries and compiler
${SRCDIR}: FRC
	@echo; echo -n "=== compiling $@ at: "; date
	cd $@; make -k ${MFLAGS}

# how to make X11 -- must make includes first
X11:	FRC
	@echo; echo -n "=== compiling $@ at: "; date
	cd $@; make ${MFLAGS} includes; make -k ${MFLAGS}

# compress all source files
compress: clean
	find . -type f ! -name Makefile ! -name '*.Z' -exec compress {} \;

# uncompress all source files
uncompress: FRC
	find . -type f ! -name Makefile -name '*.Z' -exec uncompress {} \;

# create tags file
tags:
	@echo; echo -n "=== creating tags file at: "; date
	cp /dev/null tags
	@for i in include lib usr.lib; do \
	  echo; echo -n "=== making tags for $$i at: "; date; \
	  (cd $$i; make ${MFLAGS} TAGSFILE=../tags tags); \
	done
	sort -u +0 -1 -o tags tags

FRC:
