#==================================================
# Copyright IBM 2000
#==================================================

# makefile for stand-alone build of Linux interface for GPFS.
# $Id: makefile.toship,v 1.27 2006/02/28 17:25:21 yuri Exp $

# Build instructions
#  0) export SHARKCLONEROOT=/usr/lpp/mmfs/src
#  1) Create the build configuration file. This may be done manually or
#     by autoconfig.
#     - Manual Method:
#        cd config
#        cp site.mcr.proto site.mcr
#        (edit site.mcr file as desired, eg, architecture and compiler flags)
#        cd ..
#     - Autoconfig Method:
#        make Autoconfig
#  2) make World
#
# If you want to recreate the site.mcr configuration file (e.g. build for new
# kernel), you may either edit manually or remove it prior to invoking auto-
# configuration via the 'make Autoconfig' rule. The site.mcr file may be
# manually removed from $SHARKCLONEROOT/shark/config or by invoking the
# 'make CleanConfig' rule.
#


LPPBIN  = /usr/lpp/mmfs/bin
DESTDIR = /usr/lpp/mmfs/src
TOOLDIR = /usr/X11R6/bin

IMAKE = $(TOOLDIR)/imake
IRULESRC = $(DESTDIR)/shark/config

MAKE = /usr/bin/make
MAKEDASH = -

DOTDOTIMAKE_CMD = $(IMAKE) -Timake.tmpl -I. -I$(IRULESRC) -s makefile

all: World

World::
	@if [ "$(SHARKCLONEROOT)" = "" ]; then \
	echo "Error: SHARKCLONEROOT is not set!"; exit 1; fi
World:: VerifyBuildTools
World:: config1
World:: Makefiles
World:: Clean
World:: config2
World:: Headers
World:: Depend
World:: Modules
World:: InstallLibs
World:: Install
World:: finish

InstLibs:: InstallLibs

###################################################

SUBDIRS = ibm-kxi ibm-linux gpl-linux misc
# old directories from previous releases (used for clean up)
OLDSUBDIRS = patches

Clean::
	@if [ "$(SHARKCLONEROOT)" = "" ]; then \
	echo "Error: SHARKCLONEROOT is not set!"; exit 1; fi
Clean:: clean
UninstallClean:: clean
clean::
	rm -rf $(DESTDIR)/include $(DESTDIR)/bin $(DESTDIR)/lib
	mkdir $(DESTDIR)/include $(DESTDIR)/bin $(DESTDIR)/lib
	rm -f config/install.ti

VerifyBuildTools::
	@echo "Verifying that tools to build the portability layer exist....";
	@which $(TOOLDIR)/imake > /dev/null 2>&1; if [ $$? -eq 0 ]; then \
	echo "imake present"; \
	else \
	echo "imake missing! Verify that it is installed."; \
        echo "imake is normally installed with X11 development packages."; \
        exit 1; \
	fi;
	@which cpp > /dev/null 2>&1; if [ $$? -eq 0 ]; then \
	echo "cpp present"; \
	else \
	echo "cpp missing! Verify that it is installed."; exit 1; \
	fi
	@which gcc > /dev/null 2>&1; if [ $$? -eq 0 ]; then \
	echo "gcc present"; \
	else \
	echo "gcc missing! Verify that it is installed."; exit 1; \
	fi
	@which g++ > /dev/null 2>&1; if [ $$? -eq 0 ]; then \
	echo "g++ present"; \
	else \
	echo "g++ missing! Verify that it is installed."; exit 1; \
	fi
	@which ld > /dev/null 2>&1; if [ $$? -eq 0 ]; then \
	echo "ld present"; \
	else \
	echo "ld missing! Verify that it is installed."; exit 1; \
	fi

Autoconfig: config/site.mcr

config/site.mcr: config/site.mcr.proto
	cd config; ./configure; exit $$? || exit 1;

config1::
	cd config; ./bootstrap; $(MAKE) clean; $(MAKE) install

config2::
	cd config; $(MAKE) config2;
	cd misc; $(DOTDOTIMAKE_CMD)
	cd misc; $(MAKE) $(MAKEDASH)$(MAKEFLAGS) $(MAKEEXTRA) stubs
	cd misc; $(MAKE) $(MAKEDASH)$(MAKEFLAGS) $(MAKEEXTRA) install

finish::

###################################################

Install::
	@(case '$(MAKEFLAGS)' in *[ik]*) set +e;; esac; \
	for i in $(SUBDIRS); do \
	(cd $$i; echo  "installing" "(`pwd`)"; \
	$(MAKE) $(MAKEDASH)$(MAKEFLAGS) $(MAKEEXTRA) DESTDIR=$(DESTDIR)  Install; \
	exit $$?) || exit 1; \
	done)

Depend::
	@(case '$(MAKEFLAGS)' in *[ik]*) set +e;; esac; \
	for i in $(SUBDIRS); do \
	(cd $$i; echo  "Depending" "(`pwd`)"; \
	$(MAKE) $(MAKEDASH)$(MAKEFLAGS) $(MAKEEXTRA) DESTDIR=$(DESTDIR)  Depend; \
	exit $$?) || exit 1; \
	done)

Clean::
	@(case '$(MAKEFLAGS)' in *[ik]*) set +e;; esac; \
	for i in $(SUBDIRS) $(EXTRASUBDIRS); do \
	(cd $$i; echo  "cleaning" "(`pwd`)"; \
	$(MAKE) $(MAKEDASH)$(MAKEFLAGS) $(MAKEEXTRA) DESTDIR=$(DESTDIR)  Clean; \
	exit $$?) || exit 1; \
	done)

CleanConfig::
	rm -f shark/config/site.mcr config/site.mcr; exit $$? || exit 1;

# Ensures old directories (from previous release) are cleaned out
UninstallClean::
	@(case '$(MAKEFLAGS)' in *[ik]*) set +e;; esac; \
	for i in $(SUBDIRS) $(EXTRASUBDIRS) $(OLDSUBDIRS); do \
	if [ -f $$i/makefile ]; then \
	(cd $$i; echo  "cleaning" "(`pwd`)"; \
	$(MAKE) $(MAKEDASH)$(MAKEFLAGS) $(MAKEEXTRA) DESTDIR=$(DESTDIR)  Clean; \
	exit $$?) || exit 1; \
	fi; \
	done)

Makefiles::
	@(case '$(MAKEFLAGS)' in *[ik]*) set +e;; esac; \
	for i in $(SUBDIRS) $(EXTRASUBDIRS); do \
	echo "Making Makefiles (`pwd`/$$i)"; \
	$(MAKE) subdirMakefiles \
	MAKE_SUBDIR=$$i; \
	done)

subdirMakefiles:
	cd $(MAKE_SUBDIR); \
	$(DOTDOTIMAKE_CMD); \
	touch .depends; \
	cat .depends >>makefile; \
	$(MAKE) $(MAKEDASH)$(MAKEFLAGS) $(MAKEEXTRA) Makefiles

Headers::
	@(case '$(MAKEFLAGS)' in *[ik]*) set +e;; esac; \
	for i in $(SUBDIRS) $(EXTRASUBDIRS); do \
	(cd $$i; echo  "installing header files" "(`pwd`)"; \
	$(MAKE) $(MAKEDASH)$(MAKEFLAGS) $(MAKEEXTRA) DESTDIR=$(DESTDIR)  Headers; \
	exit $$?) || exit 1; \
	done)

Libs::
	@(case '$(MAKEFLAGS)' in *[ik]*) set +e;; esac; \
	for i in $(SUBDIRS) $(EXTRASUBDIRS); do \
	(cd $$i; echo  "Make-Libraries-ing" "(`pwd`)"; \
	$(MAKE) $(MAKEDASH)$(MAKEFLAGS) $(MAKEEXTRA) DESTDIR=$(DESTDIR)  Libs; \
	exit $$?) || exit 1; \
	done)

# Set up symlink for binding machine dependent module code 
# with machine independent mmfs object
Modules::
	ln -fs $(LPPBIN)/mmfs $(DESTDIR)/bin ; \
	if [ -e $(LPPBIN)/mmfs-i386r ]; then ln -fs $(LPPBIN)/mmfs-i386r $(DESTDIR)/bin; fi ; \
	if [ -e $(LPPBIN)/mmfs-ppc64-rh4 ]; then ln -fs $(LPPBIN)/mmfs-ppc64-rh4 $(DESTDIR)/bin; fi

InstallLibs::
	@(case '$(MAKEFLAGS)' in *[ik]*) set +e;; esac; \
	for i in $(SUBDIRS) $(EXTRASUBDIRS); do \
	(cd $$i; echo  "Make-Install-Libraries-ing" "(`pwd`)"; \
	$(MAKE) $(MAKEDASH)$(MAKEFLAGS) $(MAKEEXTRA) DESTDIR=$(DESTDIR)  InstallLibs; \
	exit $$?) || exit 1; \
	done)

# RPM source build option to install images for gpl kernel modules
InstallImages::
	cd gpl-linux; $(MAKE) InstallImages;

# Use screwy method to clean up residual stuff left over
# from a gpl source build (if neccessary) and avoid
# various gripes from rpm uninstall
SrcBuildClean::
	@if [ -f gpl-linux/makefile ]; then make UninstallClean; fi; \
	rm -f config/site.mcr; \
	rm -f shark/config/site.mcr; \
	rm -f shark/config/imake.tmpl; \
	rm -f /usr/lpp/mmfs/bin/mmfslinux; \
	rm -f /usr/lpp/mmfs/bin/tracedev; \
	rm -f /usr/lpp/mmfs/bin/lxtrace; \
	rm -f /usr/lpp/mmfs/bin/dumpconv; \
	rm -f /usr/lpp/mmfs/bin/tracedev.BAK; \
	if [ -d shark/config ]; then rmdir shark/config; fi; \
	if [ -d shark ]; then rmdir shark; fi; \
	if [ -d lib ]; then rmdir lib; fi; \
	if [ -d bin ]; then rmdir bin; fi; \
	if [ -d include ]; then rmdir include; fi; \
	sh -c 'flist=$$(find ./ -name .depends); rm -f $$flist'; \
	sh -c 'flist=$$(find ./ -name install.he); rm -f $$flist'; \
	sh -c 'flist=$$(find ./ -name makefile.BAK); rm -f $$flist'; \
	for i in $(OLDSUBDIRS); do \
	  if [ -d $$i ]; then rm -f $$i/makefile; fi; \
	done; \
	rm -f config/makefile gpl-linux/makefile gpl-linux/i386/makefile \
	      ibm-kxi/makefile ibm-linux/makefile misc/makefile \
	      gpl-linux/ia64/makefile gpl-linux/ppc64/makefile \
	      gpl-linux/x86_64/makefile;

##### DEPENDENCY LINE - DO NOT DELETE #####

