#  Copyright (c) 1997-2009
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.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: utils.make 9364 2009-10-18 19:12:59Z gawrilow $


PERL := perl
export PERL

_extract_var = $(patsubst $(1)='%',%,$(filter $(1)=%,$(2)))

ifeq "$(origin FINK)" "command line"
  _exports := FINK=${FINK}
endif
ifeq "$(origin Arch)" "command line"
  _exports += Arch=${Arch}
endif

ifdef _allow_lacking_BuildDir
  PerlArch := $(shell eval `${PERL} -V:archname -V:version`; echo $$archname $$version)
  PerlVersion := $(word 2,${PerlArch})
  PerlArch := $(word 1,${PerlArch})

  ifeq "$(origin Arch)" "command line"
    BuildDir := $(patsubst %/,%,$(dir $(wildcard ${ProjectTop}/build.${Arch}/conf.make)))
  else
    ConfigParams := $(shell ${_exports} ${PERL} -e '\
      do "${ProjectTop}/support/locate_build_dir"; \
      if ($$@) { print STDERR $$@; print "!" } else { print &export_vars } ')
    ifeq (${ConfigParams},!)
      $(error 1)
    endif
    BuildDir := $(call _extract_var,BuildDir,${ConfigParams})
  endif
else ifndef BuildDir
  BuildDir := $(shell ${_exports} ${PERL} -e '\
    $$InstallTop="${ProjectTop}"; \
    do "${ProjectTop}/support/locate_build_dir"; \
    if ($$@) { print STDERR $$@; print "!" } else { print $$BuildDir } ')
  ifeq (${BuildDir},!)
    $(error 1)
  endif
  ifndef BuildDir
    $(error BuildDir undefined)
  endif
endif

MV := mv -f

# recursively expand a variable which may contain names of other variables
_expand_var = $(foreach word,$(1),$(if $(${word}),$(call _expand_var,$(${word})), ${word}))

# convert a space-separated list into a colon-separated path
_empty:=
_space:=${_empty} ${_empty}
_list2path = $(subst ${_space},:,$(strip $(1)))

# suffixes of C++ source files (not headers!)
CXX_suffixes := cc cpp C

# list all C++ sources in a directory
_list_CXX_sources = $(patsubst $(1)/%,%,$(wildcard $(addprefix $(1)$(if $(2),/$(2))/*., ${CXX_suffixes})))

# list all subdirectories of the current directory
_list_dirs = $(shell $(if $(1),cd $(1) || exit 1;) for d in *; do [ -d $$d ] && echo $$d; done)

# convert a relative path to an absolute one
_abs_path = $(if $(filter /%,$(1)),$(1),$(shell ${PERL} -MCwd=abs_path -e 'print abs_path(q{$(1)})'))

override .svn := $(wildcard ${ProjectTop}/.svn)
ifdef .svn
  # list all files or subdirectories marked with an SVN property
  _get_no_export = $(shell svn propget $(if $(filter %/*,$(1)), noexport $(wildcard $(1)), -R noexport $(1)) | awk '$$3 ~ /$(2)/ { print $$1 }')
else
  _get_no_export :=
endif


# Local Variables:
# mode: Makefile
# End:
