#  Copyright (c) 1997-2004
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: Makefile,v 1.108.2.4 2004/12/22 14:10:34 gawrilow Exp $

###########################################################
#
#  Building and installation
#
.PHONY: all all-java configure clean clean-java distclean TAGS RTAGS perlx \
        install install-devel install-bin install-shared install-doc install-devel-bin install-devel-shared

PERL := perl
export PERL
PerlArch := $(shell eval `$(PERL) -V:archname`; echo $$archname)

ifndef BuildDir
  BuildDir := $(wildcard build*)
  ifdef BuildDir
    ifneq ($(BuildDir),build)
      Arch := $(shell uname -m)
      ifeq ($(Arch),Power Macintosh)
        Arch := darwin
      endif
      d := $(filter build.$(Arch), $(BuildDir))
      ifndef d
        Arch := $(PerlArch)
        d := $(filter build.$(Arch), $(BuildDir))
      endif
      ifndef d
        Arch :=
      endif
      BuildDir := $d
    endif
    ifeq ($(wildcard $(BuildDir)/conf.make),)
      BuildDir :=
    endif
  endif
endif

ifndef BuildDir

all install% config-% :
	@$(if $(Arch), Arch=$(Arch)) TOP=$(CURDIR) $(PERL) support/configure.pl -quick
	@$(MAKE) --no-print-directory $@

configure :
	@$(if $(Arch), Arch=$(Arch)) TOP=$(CURDIR) $(PERL) support/configure.pl

else  # BuildDir exists

### load configuration
include $(BuildDir)/conf.make

override _has_export := $(wildcard .export)

ifdef _has_export
  include .export
endif

Apps := $(filter-out $(NoInstall) CVS, $(notdir $(wildcard apps/*)))
Modules := $(filter-out $(NoInstall) CVS, $(notdir $(wildcard modules/*)))
DefaultAppName := $(firstword $(filter polytope, $(Apps)) $(Apps))

ifneq ($(Apps),)

define _MakeGMP
	$(if $(filter-out -,$(GMPbuildDir)), $(MAKE) -C $(GMPbuildDir) $(1))

endef
define _MakeAppBin
	$(MAKE) -C $(BuildDir)/apps/$(1) AppName=$(1) $(2)

endef

ifdef JavaBuildDir
  define _MakeAppJava
	[ ! -d $(JavaBuildDir)/$(PackageBase)/$(1) ] || $(MAKE) -C $(JavaBuildDir)/$(PackageBase)/$(1) $(2)

  endef
else 
  _MakeAppJava :=
endif

define _MakeAppsBin
	$(foreach a,$(Apps),$(call _MakeAppBin,$a,$(1)))
endef

Jars := $(wildcard $(foreach a, $(Apps) $(Modules), java_build/$a.jar))

define _MakeAppsJava
	$(if $(JavaBuildDir), \
	  $(foreach a, $(Apps) $(Modules),$(call _MakeAppJava,$a,$(1))), \
	  $(if $(filter install,$(1)), \
	    $(if $(Jars), \
	      $(call _CreateDir,-U $(InstallTop)/jars) \
	      $(PERL) support/install.pl -m $(DirMask) $(Jars) $(InstallTop)/jars)))

endef

all:
	@+$(call _MakeGMP, all)
	@+$(call _MakeAppsBin, all)
	@+$(call _MakeAppsJava, all)

all-java:
	@+$(call _MakeAppsJava, all)

clean-java:
	@+$(call _MakeAppsJava, clean)

else  # !Apps

all:
	@$(MAKE) -C $(BuildDir)/lib all-lib AppName=lib PTLonly=yes
endif

### installation

PerlxDir := $(BuildDir)/perlx-$(PerlArch)

perlx :
	[ -d $(PerlxDir) ] || $(PERL) $(INSTALL_PL) -d -m 755 $(PerlxDir)
	cd $(PerlxDir); TOP=$(CURDIR) $(PERL) $(CURDIR)/perl/ext/Makefile.PL
	@$(MAKE) -C $(PerlxDir) all pure_install

define _InstallSubdir
$(PERL) support/install.pl -m $(DirMask) -U -X CVS $(2) $(1) $(InstallTop)/$(1)

endef
define _CreateDir
$(PERL) support/install.pl -d -m $(DirMask) $(1)

endef

ifneq ($(Apps),)

define _InstallDirLinks
$(PERL) support/install_links.pl -m 111 $(1) $(InstallLinks) $(InstallTop) $(DESTDIR)

endef
define _InstallLinks
	$(call _InstallDirLinks, $(InstallArch)/$(1)/bin)
	$(if $(wildcard $(1)/scripts), $(call _InstallDirLinks, $(InstallTop)/$(1)/scripts))
endef
define _InstallShared
	$(if $(wildcard $(1)/scripts), $(call _InstallSubdir,$(1)/scripts,-e InstallTop=$(FinalInstallTop) $(if $(PerlExe),-P $(PerlExe))))
	$(foreach d, perllib rules, $(if $(wildcard $(1)/$d), $(call _InstallSubdir,$(1)/$d)))
endef

install: install-shared install-bin install-doc
install-devel: install-devel-shared install-devel-bin

install-bin: all
	$(call _CreateDir,$(InstallArch))
	@+$(call _MakeGMP, install-libLTLIBRARIES)
	@+$(call _MakeAppsBin, install)
ifdef InstallLinks
	$(call _CreateDir,$(InstallLinks))
	$(call _InstallDirLinks, $(InstallTop)/perl)
	$(foreach m, $(Modules), $(call _InstallLinks,modules/$m))
	$(foreach a, $(Apps), $(call _InstallLinks,apps/$a))
endif
ifndef RPM_BUILD_ROOT
	@$(MAKE) -C $(PerlxDir) all pure_install InstallTop=$(InstallTop)
endif

install-devel-bin:
	@+$(call _MakeAppsBin, install-lib)
	@rm -f $(InstallArch)/Makefile
	@{ sed -e '/Install.*=/ s/^/override /' \
	       -e '/INSTALL_PL=/ s:=.*:=$$(InstallTop)/support/install.pl:' \
	       -e '/GMPbuildDir=/ s:=.*:=-:' \
	       -e '/DESTDIR=/ { s/^/override /; q; }' \
	       $(BuildDir)/conf.make; \
	   echo 'override ProjectTop=$$(InstallTop)'; \
	   echo 'ExtendApp ?= $(DefaultAppName)'; \
	   echo 'include $$(ProjectTop)/support/build.make'; \
	 } >$(InstallArch)/Makefile
	@chmod 444 $(InstallArch)/Makefile

install-shared:
	$(call _CreateDir,$(InstallTop))
	[ -z "$(AutoconfFile)" -o ! -f "$(AutoconfFile)" ] || $(PERL) support/install.pl -m 644 $(AutoconfFile) $(InstallTop)
	$(call _InstallSubdir,perl,-e DefaultAppName=$(DefaultAppName) -e InstallTop=$(FinalInstallTop) \
				   -X testsuite -X ext $(if $(PerlExe),-P $(PerlExe)))
	$(foreach m, $(Modules), $(call _InstallShared,modules/$m))
	$(foreach a, $(Apps), $(call _InstallShared,apps/$a))
	$(call _MakeAppsJava,install)
	$(call _InstallSubdir,povray)
	$(call _InstallSubdir,scripts)

install-devel-shared:
	$(call _InstallSubdir,support)
	@+$(call _MakeAppsBin, install-include)

install-doc:
ifneq ($(wildcard doc),)
	$(MAKE) -C doc install
else
	$(PERL) support/install.pl -m $(DirMask) -X '.rpm_docs*' -U doc_build $(InstallDoc)
endif

else  # !Apps

install: install-shared install-bin

install-shared:
	$(call _CreateDir,$(InstallTop))
	@$(MAKE) -C $(BuildDir)/lib install-include AppName=lib PTLonly=yes

install-bin:
	$(call _CreateDir,$(InstallArch))
	@$(MAKE) -C $(BuildDir)/lib install-lib AppName=lib PTLonly=yes

endif

### manual re-configuration
configure :
	@TOP=$(CURDIR) $(PERL) support/configure.pl $(BuildDir)/conf.make

config-% :
	@echo $($*)

ifdef _has_export
### maintenance
tagsFLAGS = -R -e -f $@ --exclude=CVS --exclude='.\#*' --exclude='\#*' --exclude='*~'

TAGS:
	ctags $(tagsFLAGS) --language-force=c++ lib {apps,modules}/*/{src,include}
RTAGS:
	ctags $(tagsFLAGS) --language-force=perl --exclude=testsuite --exclude=ext perl \
			   {apps,modules}/*/{perllib,rules} scripts apps/*/scripts support/*.pl
	ctags -e -f $@ --append --language-force=c perl/ext/*.{xs,h}

include support/export.make

endif  # _has_export

endif  # BuildDir exists

### cleanup

clean:
	@+$(call _MakeGMP, clean)
ifdef BuildDir
	@+$(call _MakeAppsBin, clean)
	@+$(call _MakeAppsJava, clean)
	rm -rf $(PerlxDir)
endif
ifneq ($(wildcard doc),)
	@$(MAKE) -C doc clean
endif

distclean : clean
	[ ! -d lib/PTL ] || rm -rf build*


# Local Variables:
# mode: Makefile
# End:
