#
# Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.  Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code 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
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

#
# Defnitions for all platforms.
#
# Normally the convention is that these alternate definitions of
#   primary make variables are never defined inside the Makefiles anywhere
#   but are defined via environment variables or set on the make command
#   line. So you should never see an ALT_* variable defined in any
#   makefiles, just used. This is the convention and there are some
#   exceptions, either mistakes or unusual circumstances.
#
# The naming convention for the default value of one of these variables
#   that has an ALT_* override capability is to name the default value with a
#   leading underscore (_). So for XXX you would have:
#      _XXX      default value
#      ALT_XXX   any override the user is providing if any
#      XXX       the final value, either the default _XXX or the ALT_XXX value.
#

# On Directory names. In very rare cases should the Windows directory
#    names use the backslash, please use the C:/ style of windows paths.
#    Avoid duplicating the // characters in paths, this has known to cause
#    strange problems with jar and other utilities, e.g. /a//b/ != /a/b/.
#    Some of these variables have an explicit trailing / character, but in
#    general, they should NOT have the trailing / character.

# Get shared system utilities macros defined
include $(BUILDDIR)/common/shared/Defs-utils.gmk

# Assumes ARCH, PLATFORM, etc. have been defined.

# Simple pwd path
define PwdPath
$(shell cd $1 2> $(DEV_NULL) && pwd)
endef

# Checks an ALT value for spaces (should be one word), 
#       warns and returns Check_ALT_$1 if spaces
define AltCheckSpaces
$(if $(word 2,$($1)),$(warning "WARNING: Value of $1 contains a space: '$($1)', check or set ALT_$1")Check_ALT_$1,$($1))
endef

# Checks an ALT value for empty, warns and returns Check_ALT_$1 if empty
define AltCheckValue
$(if $($1),$($1),$(warning "WARNING: Value of $1 cannot be empty, check or set ALT_$1")Check_ALT_$1)
endef

# Checks any value for empty, warns and returns $2 if empty
define CheckValue
$(if $($1),$($1),$(warning "WARNING: Value of $1 cannot be empty, will use '$2'")$2)
endef

# Prefix for a utility prefix path, if empty leave alone, otherwise end with a /
define PrefixPath
$(if $1,$(subst //,/,$1/),)
endef

# Select a directory if it exists, or the alternate 2 or the alternate 3
define DirExists
$(shell \
  if [ -d "$1" ]; then  \
    echo "$1"; \
  elif [ -d "$2" ]; then \
    echo "$2"; \
  else \
    echo "$3"; \
  fi)
endef

# Select a writable directory if it exists and is writable, or the alternate
define WriteDirExists
$(shell \
  if [ -d "$1" -a -w "$1" ]; then  \
    echo "$1"; \
  else \
    echo "$2"; \
  fi)
endef

# Select a file if it exists, or the alternate 1, or the alternate 2
define FileExists
$(shell \
  if [ -r "$1" ]; then \
    echo "$1"; \
  elif [ -r "$2" ]; then \
    echo "$2"; \
  else \
    echo "NO_FILE_EXISTS"; \
  fi)
endef

# Given a line of text, get the major.minor version number from it
define GetVersion
$(shell echo $1 | sed -e 's@[^1-9]*\([1-9][0-9]*\.[0-9][0-9]*\).*@\1@' )
endef

# Given a major.minor.micro version, return the major, minor, or micro number
define MajorVersion
$(if $(word 1, $(subst ., ,$1)),$(word 1, $(subst ., ,$1)),0)
endef
define MinorVersion
$(if $(word 2, $(subst ., ,$1)),$(word 2, $(subst ., ,$1)),0)
endef
define MicroVersion
$(if $(word 3, $(subst ., ,$1)),$(word 3, $(subst ., ,$1)),0)
endef

# Macro that returns missing, same, newer, or older $1=version $2=required
#  (currently does not check the micro number)
define CheckVersions
$(shell \
  if [ "$1" = "" -o "$2" = "" ]; then \
    echo missing; \
  else \
    if [ "$1" = "$2" ]; then \
      echo same; \
    else \
      if [ $(call MajorVersion,$1) -lt $(call MajorVersion,$2) ] ; then \
        echo older; \
      else \
	if [ $(call MajorVersion,$1) -eq $(call MajorVersion,$2) -a \
	     $(call MinorVersion,$1) -lt $(call MinorVersion,$2) ]; then \
          echo older; \
        else \
          echo newer; \
        fi; \
      fi; \
    fi; \
  fi)
endef

# Make sure certain variables are non-empty at this point
_check_values:=\
$(call CheckValue,ARCH,),\
$(call CheckValue,ARCH_DATA_MODEL,),\
$(call CheckValue,VARIANT,),\
$(call CheckValue,PLATFORM,)

# Misc common settings for all workspaces
#   This determines the version of the product, and the previous version or boot
ifndef JDK_MAJOR_VERSION
  JDK_MAJOR_VERSION      = 1
  PREVIOUS_MAJOR_VERSION = 1
endif

ifndef JDK_MINOR_VERSION
  JDK_MINOR_VERSION      = 7
  PREVIOUS_MINOR_VERSION = 6
endif

ifndef JDK_MICRO_VERSION
  JDK_MICRO_VERSION      = 0
  PREVIOUS_MICRO_VERSION = 0
endif

ifndef MILESTONE
  MILESTONE = internal
endif

ifndef BUILD_NUMBER
  JDK_BUILD_NUMBER = b00
else
  ifndef JDK_BUILD_NUMBER
    JDK_BUILD_NUMBER = $(BUILD_NUMBER)
  endif
endif

# Default variant is the optimized version of everything
#    can be OPT or DBG,  default is OPT
#    Determine the extra pattern to add to the release name for debug/fastdebug.
#    Determine the JDK_IMPORT_VARIANT, so we get the right VM files copied over.
#
ifeq ($(VARIANT), DBG)
  BUILD_VARIANT_RELEASE=-debug
else
  BUILD_VARIANT_RELEASE=
endif
ifeq ($(FASTDEBUG), true)
  VARIANT=DBG
  BUILD_VARIANT_RELEASE=-fastdebug
  _JDK_IMPORT_VARIANT=/fastdebug
endif

# Depending on the flavor of the build, add a -debug or -fastdebug to the name
ifdef DEBUG_NAME
  BUILD_VARIANT_RELEASE=-$(DEBUG_NAME)
endif

JDK_VERSION  = $(JDK_MAJOR_VERSION).$(JDK_MINOR_VERSION).$(JDK_MICRO_VERSION)
JDK_UNDERSCORE_VERSION =  $(subst .,_,$(JDK_VERSION))

# RELEASE is JDK_VERSION and -MILESTONE if MILESTONE is set
ifneq ($(MILESTONE),fcs)
  RELEASE      = $(JDK_VERSION)-$(MILESTONE)$(BUILD_VARIANT_RELEASE)
else
  RELEASE      = $(JDK_VERSION)$(BUILD_VARIANT_RELEASE)
endif

# FULL_VERSION is RELEASE and -BUILD_NUMBER if BUILD_NUMBER is set
ifndef FULL_VERSION
  ifdef BUILD_NUMBER
    FULL_VERSION = $(RELEASE)-$(BUILD_NUMBER)
  else
    BUILD_NUMBER = b00
    USER_RELEASE_SUFFIX := $(shell echo $(USER)_`date '+%d_%b_%Y_%H_%M' | tr "A-Z" "a-z"`)
    FULL_VERSION = $(RELEASE)-$(USER_RELEASE_SUFFIX)-$(BUILD_NUMBER)
  endif
  export FULL_VERSION
endif

# Promoted build location
PROMOTED_RE_AREA = $(SLASH_JAVA)/re/jdk/$(JDK_VERSION)/promoted
PROMOTED_BUILD_LATEST = latest
PROMOTED_BUILD_BASEDIR = $(PROMOTED_RE_AREA)/$(PROMOTED_BUILD_LATEST)
PROMOTED_BUILD_BINARIES = $(PROMOTED_BUILD_BASEDIR)/binaries

# OPT: Changes what the optimizations settings (in _OPT)
POPT = $(_OPT$(ALT_OPT))$(ALT_OPT)

# PARALLEL_COMPILE_JOBS: is the number of compiles done in parallel.
#  If the user sets ALT_PARALLEL_COMPILE_JOBS, then COMPILE_APPROACH is set
#  to parallel.
#
#  Recommended setting: 2 seems to be ideal for single cpu machines,
#                       2 times the number of CPU's is a basic formula, 
#                       but probably not more than 4 if the machine is 
#                       being shared by others, or the machine is limited 
#                       in RAM or swap.
#
ifdef ALT_PARALLEL_COMPILE_JOBS
  PARALLEL_COMPILE_JOBS=$(ALT_PARALLEL_COMPILE_JOBS)
else
  PARALLEL_COMPILE_JOBS=2
endif

# Previous JDK release (version of BOOTDIR version)
ifdef ALT_PREVIOUS_JDK_VERSION
  PREVIOUS_JDK_VERSION = $(ALT_PREVIOUS_JDK_VERSION)
else
  PREVIOUS_JDK_VERSION  = $(PREVIOUS_MAJOR_VERSION).$(PREVIOUS_MINOR_VERSION).$(PREVIOUS_MICRO_VERSION)
endif
export PREVIOUS_JDK_VERSION
PREVIOUS_JDK_VERSION:=$(call AltCheckSpaces,PREVIOUS_JDK_VERSION)
PREVIOUS_JDK_VERSION:=$(call AltCheckValue,PREVIOUS_JDK_VERSION)

# Version with _ instead of . in number
ifeq ($(PREVIOUS_MINOR_VERSION),5)
  PREVIOUS_JDK_UNDERSCORE_VERSION =  $(subst .,_,$(PREVIOUS_JDK_VERSION))
else
  PREVIOUS_JDK_UNDERSCORE_VERSION = $(PREVIOUS_MINOR_VERSION)
endif

# Get platform specific settings
include $(BUILDDIR)/common/shared/Defs-$(PLATFORM).gmk

# Components
ifdef ALT_LANGTOOLS_DIST
  LANGTOOLS_DIST :=$(call FullPath,$(ALT_LANGTOOLS_DIST))
else
  LANGTOOLS_DIST =
endif

# These are the same on all platforms but require the above platform include 1st

# BOOTDIR: Bootstrap JDK, previous released JDK.
#   _BOOTDIR1 and _BOOTDIR2 picked by platform
ifdef ALT_BOOTDIR
  BOOTDIR =$(ALT_BOOTDIR)
else
  BOOTDIR  :=$(call DirExists,$(_BOOTDIR1),$(_BOOTDIR2),/NO_BOOTDIR)
endif
export BOOTDIR
BOOTDIR:=$(call AltCheckSpaces,BOOTDIR)
BOOTDIR:=$(call AltCheckValue,BOOTDIR)

# OUTPUTDIR: Location of all output for the build
_BACKUP_OUTPUTDIR = $(TEMP_DISK)/$(USER)/jdk-outputdir
ifdef ALT_OUTPUTDIR
  _POSSIBLE_OUTPUTDIR =$(subst \,/,$(ALT_OUTPUTDIR))
else
  ifndef _OUTPUTDIR
    _OUTPUTDIR = $(_BACKUP_OUTPUTDIR)
  endif
  _POSSIBLE_OUTPUTDIR =$(_OUTPUTDIR)
endif
_create_outputdir1:=$(shell mkdir -p $(_POSSIBLE_OUTPUTDIR) > $(DEV_NULL) 2>&1)
OUTPUTDIR:=$(call WriteDirExists,$(_POSSIBLE_OUTPUTDIR),$(_BACKUP_OUTPUTDIR))
_create_outputdir2:=$(shell mkdir -p $(OUTPUTDIR) > $(DEV_NULL) 2>&1)
ifeq "$(OUTPUTDIR)" "$(_BACKUP_OUTPUTDIR)"
  _outputdir_warning:=$(warning "WARNING: OUTPUTDIR '$(_POSSIBLE_OUTPUTDIR)' not writable, will use '$(_BACKUP_OUTPUTDIR)'")
endif
OUTPUTDIR:=$(call AltCheckSpaces,OUTPUTDIR)
OUTPUTDIR:=$(call AltCheckValue,OUTPUTDIR)

# Bin directory
#   NOTE: ISA_DIR is usually empty, on Solaris it might be /sparcv9 or /amd64
BINDIR      = $(OUTPUTDIR)/bin$(ISA_DIR)
  
# Absolute path to output directory
ABS_OUTPUTDIR:=$(call FullPath,$(OUTPUTDIR))


