## Define the version number of this release.  Do not change this.

VERSION = 5.0.1

## Define where the final targets are installed. If you wish to put the final
## pieces in different places, you MUST change these macros before you create
## the configuration file and before you build the package because these
## variables are used to hard-code addresses in some of the package modules.
## These variables can be changed directly in this file, or from the command
## line of the build, as in:
##
##    % gmake INSTALLPREFIX=/usr linux-i386
##    % gmake install
##
## or by setting an environment variable, as in (csh):
##
##    % setenv INSTALLPREFIX=/usr
##    % gmake linux-i386
##    % gmake install
##
## The previous commands build uC++ and then install the package at location
## "/usr". The default location for installation is the current directory.
##
## For packaging it may be necessary to install the files into a temporary
## location.  In this case, specify the final install paths when building, and
## the temporary paths when installing, e.g.:
##
##    % gmake INSTALLPREFIX=/usr linux-i386
##    % gmake
##    % gmake INSTALLPREFIX=debian/tmp install
##

INSTALLPREFIX ?= $(shell pwd)
INSTALLINCDIR ?= $(INSTALLPREFIX)/inc
INSTALLLIBDIR ?= $(INSTALLPREFIX)/lib
INSTALLBINDIR ?= $(INSTALLPREFIX)/bin
INSTALLMANDIR ?= $(INSTALLPREFIX)/man
INSTALLDOCDIR ?= $(INSTALLPREFIX)/doc

## Define where temporary files are created during compilation and execution.

TMPDIR ?= /tmp

## Define the compiler used to build the uC++ commands (u++/cpp/u++-cpp), and
## the compiler used to build the uC++ runtime system and for compiling
## applications. Note, the CCAPP value is hard-coded into the "u++" command,
## but can be dynamically overridden using the "-compiler" flag on the "u++"
## command. While currently only GNU g++ can be used to compile uC++, there are
## several versions of GNU g++.

CCBUILD ?= g++
CCAPP ?= $(CCBUILD)


########################### END OF THINGS TO CHANGE ###########################


## Define the directory that contains the package as the current directory.

PACKAGE = $(shell pwd)

## Define where the configuration file lives.

CONFIG = $(PACKAGE)/CONFIG

## Ensure the correct shell is used

SHELL := /bin/sh

## Define the major recipes.

.SILENT : all depend clean install

all \
depend \
clean \
install :
	## If the configuration file is not yet built, instruct
	## the user how to create one.

	if [ ! -f $(CONFIG) ] ; then \
		echo ""; \
		echo "The file $(CONFIG) was not found."; \
		echo "Create file $(CONFIG) with one of the following commands..."; \
		echo ""; \
		echo "	$(MAKE) solaris-sparc"; \
		echo "	$(MAKE) irix-mips"; \
		echo "	$(MAKE) linux-i386"; \
		echo "	$(MAKE) linux-ia64"; \
		echo ""; \
		exit 1; \
	fi

#		echo "	$(MAKE) dec-alpha"; \
#		echo "	$(MAKE) ibm-rs6000"; \
#		echo "	$(MAKE) hp-pa"; \
#		echo "	$(MAKE) sequent-i386-ptx"; \
#		echo "	$(MAKE) sun-sparc-sunos"; \
#		echo "	$(MAKE) sun-m68k-sunos"; \
#		echo "	$(MAKE) sun-i386-svr4"; \
#		echo "	$(MAKE) dec-ultrix-mips"; \
#		echo "	$(MAKE) sgi-mips-r3000"; \
#		echo "	$(MAKE) gizmo"; \

	## Build each of the modules in the system. It is important that the modules be
	## built in this order. Each successive module is built using previous modules.

	for module in doc/man src/translator src/collection src/kernel src/scheduler src/library src/profiler src/debugger ; do \
		( set -x; cd $(PACKAGE)/$${module}; $(MAKE) $(MAKECMDGOALS) ) \
	done

scratch :
	rm -rf $(INSTALLMANDIR)/* $(INSTALLBINDIR)/* $(INSTALLLIBDIR)/* $(INSTALLINCDIR)/*

# These recipe recursively invoke this file again with all configuration variables set.

#sequent-i386-ptx : ; @$(MAKE) configure VENDOR=sequent OS=svr4 CPU=i386 TVENDOR=sequent TOS=svr4 TCPU=i386 MULTI=TRUE

solaris-sparc : ; $(MAKE) configure VENDOR=sun OS=solaris CPU=sparc TVENDOR=sun TOS=solaris TCPU=sparc MULTI=TRUE

#sun-sparc-sunos : ; @$(MAKE) configure VENDOR=sun OS=sunos CPU=sparc TVENDOR=sun TOS=sunos TCPU=sparc MULTI=TRUE

#sun-m68k-sunos : ; @$(MAKE) configure VENDOR=sun OS=sunos CPU=m68k TVENDOR=sun TOS=sunos TCPU=m68k MULTI=TRUE

#sun-i386-svr4 : ; @$(MAKE) configure VENDOR=sun OS=svr4 CPU=i386 TVENDOR=sun TOS=svr4 TCPU=i386 MULTI=TRUE

#dec-alpha : ; @$(MAKE) configure VENDOR=dec OS=svr4 CPU=alpha TVENDOR=dec TOS=svr4 TCPU=alpha MULTI=TRUE

#dec-ultrix-mips : ; @$(MAKE) configure VENDOR=dec OS=ultrix CPU=mips TVENDOR=dec TOS=ultrix TCPU=mips MULTI=FALSE

irix-mips : ; @$(MAKE) configure VENDOR=sgi OS=irix CPU=mips TVENDOR=sgi TOS=irix TCPU=mips MULTI=TRUE

#sgi-mips-r3000 : ; @$(MAKE) configure VENDOR=sgi OS=svr4 CPU=mips TVENDOR=sgi TOS=svr4 TCPU=mips MULTI=FALSE

#ibm-rs6000 : ; @$(MAKE) configure VENDOR=ibm OS=svr4 CPU=rs6000 TVENDOR=ibm TOS=svr4 TCPU=rs6000 MULTI=FALSE

#hp-pa : ; @$(MAKE) configure VENDOR=hp OS=hpux CPU=hppa TVENDOR=hp TOS=hpux TCPU=hppa MULTI=FALSE

linux-i386 : ; @$(MAKE) configure VENDOR=pc OS=linux CPU=i386 TVENDOR=pc TOS=linux TCPU=i386 MULTI=TRUE

linux-ia64 : ; @$(MAKE) configure VENDOR=pc OS=linux CPU=ia64 TVENDOR=pc TOS=linux TCPU=ia64 MULTI=TRUE

#gizmo : ; @$(MAKE) configure VENDOR=sun OS=svr4 CPU=sparc TVENDOR=gizmo TOS=bsd TCPU=m68k MULTI=FALSE

.SILENT: configure

configure :
	echo "Configuring u++-$(VERSION) for $(VENDOR) host running $(OS) on a $(CPU) for $(TVENDOR) target running $(TOS) on a $(TCPU)."

	echo "##" > $(CONFIG)
	echo "## DO NOT EDIT THIS FILE DIRECTLY. DELETE THIS FILE, EDIT THE" >> $(CONFIG)
	echo "## MAKEFILE, AND THEN RUN $(MAKE) TO CREATE A NEW CONFIG FILE." >> $(CONFIG)
	echo "##" >> $(CONFIG)
	echo "VERSION = $(VERSION)" >> $(CONFIG)
	echo "VENDOR = $(VENDOR)" >> $(CONFIG)
	echo "OS = $(OS)" >> $(CONFIG)
	echo "CPU = $(CPU)" >> $(CONFIG)
	echo "TVENDOR = $(TVENDOR)" >> $(CONFIG)
	echo "TOS = $(TOS)" >> $(CONFIG)
	echo "TCPU = $(TCPU)" >> $(CONFIG)
	echo "INSTALLINCDIR = $(INSTALLINCDIR)" >> $(CONFIG)
	echo "INSTALLLIBDIR = $(INSTALLLIBDIR)" >> $(CONFIG)
	echo "INSTALLBINDIR = $(INSTALLBINDIR)" >> $(CONFIG)
	echo "INSTALLMANDIR = $(INSTALLMANDIR)" >> $(CONFIG)
	echo "INSTALLDOCDIR = $(INSTALLDOCDIR)" >> $(CONFIG)
	echo "TMPDIR = $(TMPDIR)" >> $(CONFIG)
	echo "CCBUILD = $(CCBUILD)" >> $(CONFIG)
	echo "CCAPP = $(CCAPP)" >> $(CONFIG)
	echo "MULTI = $(MULTI)" >> $(CONFIG)
	echo "SHELL := /bin/sh" >> $(CONFIG)

	for file in `find doc src -type f -name Makefile -print` ; do \
		sed 's@^PACKAGE = .*@PACKAGE = '$(PACKAGE)'@' $$file > /tmp/t$$$$; \
		if cmp -s $$file /tmp/t$$$$ ; then \
			rm /tmp/t$$$$; \
		else \
			echo "Setting hard coded variables in file $$file"; \
			mv /tmp/t$$$$ $$file; \
		fi; \
	done

	# TEMPORARY, printf adds "0x" for %p
	if [ $(TOS) = linux ] ; then \
		echo "Adjusting for printf format difference"; \
		for file in `find . -type f \( -name '*.cc' -o -name '*.h' \) -print` ; do \
			sed 's/0x%p/%p/g' $$file > /tmp/t$$$$; \
			if cmp -s $$file /tmp/t$$$$ ; then \
				rm /tmp/t$$$$; \
			else \
				mv /tmp/t$$$$ $$file; \
			fi; \
		done; \
	fi

	if [ $(TOS) = linux ] ; then \
		printf "Checking for TLS support in libc..."; \
		echo 'extern "C" void *_dl_allocate_tls( void *mem ); int main() { void *x; _dl_allocate_tls( x ); }' > /tmp/t$$$$.cc; \
		if $(CCAPP) /tmp/t$$$$.cc >/dev/null 2>&1; then \
			echo yes; \
			echo "TLS := TRUE" >> $(CONFIG); \
			rm a.out; \
		else \
			echo no; \
			echo "TLS := FALSE" >> $(CONFIG); \
		fi; \
		rm /tmp/t$$$$.cc; \
	else \
		echo "TLS := FALSE" >> $(CONFIG); \
	fi

	echo "Finished configuration, just type '$(MAKE) install'."

## Local Variables: ##
## compile-command: "gmake install" ##
## End: ##
