#
# Copyright (c) 2001, 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.
#

################################################################
# SANITY
################################################################

sanity:: pre-sanity settings insane environment

ifeq ($(BUILD_HOTSPOT), true)
  sanity:: hotspot-sanity
endif

ifeq ($(BUILD_DEPLOY), true)
  sanity:: deploy-sanity
endif

ifeq ($(BUILD_JDK), true)
  sanity:: jdk-sanity 
endif

# Only need these sanity rules when not doing a debug build
ifeq ($(DEBUG_NAME),)

  ifeq ($(BUILD_INSTALL), true)
    sanity:: install-sanity
  endif

  ifeq ($(BUILD_SPONSORS), true)
    sanity:: sponsors-sanity
  endif

endif

sanity:: post-sanity

source-sanity : pre-sanity alt_bootdir bootdir post-sanity

$(ERROR_FILE) $(WARNING_FILE) $(MESSAGE_FILE):
	@$(prep-target)

purge-sanity: $(ERROR_FILE) $(WARNING_FILE) $(MESSAGE_FILE)
	-@$(RM) $(ERROR_FILE) $(WARNING_FILE) $(MESSAGE_FILE)

pre-sanity: purge-sanity 

# this should be the last rule in any target's sanity rule.
post-sanity post-sanity-hotspot post-sanity-jdk post-sanity-install post-sanity-deploy:
	@if [ -r $(MESSAGE_FILE) ]; then \
	  $(ECHO) "" ; \
	  $(CAT) $(MESSAGE_FILE) ; \
	fi
	@if [ -r $(WARNING_FILE) ]; then \
	  $(CAT) $(WARNING_FILE) ; \
	fi
	@if [ "x$(INSANE)" != x ]; then \
	  $(ECHO) "INSANE mode requested. \n" \
	    "Sanity will not force a build termination, even with errors.\n" \
	    "" >> $(ERROR_FILE); \
	fi
	@if [ -r $(ERROR_FILE) ]; then \
	  if [ "x$(INSANE)" = x ]; then \
	    $(ECHO) "Exiting because of the above error(s). \n" \
	      "">> $(ERROR_FILE); \
	  fi ; \
	  $(CAT) $(ERROR_FILE) ; \
	  if [ "x$(INSANE)" = x ]; then \
	    exit 1 ; \
	  fi ; \
	fi
ifdef PEDANTIC
	@if [ -r $(WARNING_FILE) ]; then \
	  $(ECHO) "PEDANTIC mode requested. \n" \
	    "Exiting because of the above warning(s). \n" \
	    "" >> $(ERROR_FILE); \
	  $(CAT) $(ERROR_FILE) ; \
	  exit 1 ; \
	fi
endif
	@if [ ! -r $(ERROR_FILE) ]; then \
	  $(ECHO) "Sanity check passed." ; \
	fi

insane:
ifdef INSANE
	@$(ECHO) "WARNING: You are building in 'INSANE' mode. You \n" \
	   "        should not use this mode, and in fact, \n" \
	   "        it may be removed at any time. If you \n" \
	   "        have build problems as a result of using \n" \
	   "        INSANE mode, then you should not expect \n" \
	   "        assistance from anyone with the problems \n" \
	   "        or consequences you experience. \n" \
	   "" >> $(WARNING_FILE)
endif

# Get list of environment variables set that we do not want set
ifdef ALT_HOTSPOT_SERVER_PATH
  DO_NOT_SET_LIST += ALT_HOTSPOT_SERVER_PATH
endif
ifdef ALT_HOTSPOT_CLIENT_PATH
  DO_NOT_SET_LIST += ALT_HOTSPOT_CLIENT_PATH
endif
ifdef ALT_HOTSPOT_LIB_PATH
  DO_NOT_SET_LIST += ALT_HOTSPOT_LIB_PATH
endif

# Error message for environment variable set that should not be
%.do_not_set:
	@$(ECHO) "ERROR: Your build environment has the variable\n" \
	   "        $* defined. Please unset it and restart your build. \n" \
	   "" >> $(ERROR_FILE)

# Check the environment variables	
environment: $(DO_NOT_SET_LIST:%=%.do_not_set)
ifeq ($(LANGTOOLS_SRC_AVAILABLE), true)
  ifneq ($(BUILD_LANGTOOLS), true)
	@$(ECHO) "WARNING: You are not building the LANGTOOLS sources.\n" \
           "        Lang tools (javac) will be obtained from \n" \
           "        the location set in ALT_JDK_IMPORT_PATH. \n" \
           "" >> $(WARNING_FILE)
  endif
endif
ifeq ($(JAXP_SRC_AVAILABLE), true)
  ifneq ($(BUILD_JAXP), true)
	@$(ECHO) "WARNING: You are not building the JAXP sources.\n" \
           "        The jaxp files will be obtained from \n" \
           "        the location set in ALT_JDK_IMPORT_PATH. \n" \
           "" >> $(WARNING_FILE)
  endif
endif
ifeq ($(JAXWS_SRC_AVAILABLE), true)
  ifneq ($(BUILD_JAXWS), true)
	@$(ECHO) "WARNING: You are not building the JAXWS sources.\n" \
           "        The jaxws files will be obtained from \n" \
           "        the location set in ALT_JDK_IMPORT_PATH. \n" \
           "" >> $(WARNING_FILE)
  endif
endif
ifeq ($(CORBA_SRC_AVAILABLE), true)
  ifneq ($(BUILD_CORBA), true)
	@$(ECHO) "WARNING: You are not building the CORBA sources.\n" \
           "        The corba files will be obtained from \n" \
           "        the location set in ALT_JDK_IMPORT_PATH. \n" \
           "" >> $(WARNING_FILE)
  endif
endif
ifeq ($(HOTSPOT_SRC_AVAILABLE), true)
  ifneq ($(BUILD_HOTSPOT), true)
	@$(ECHO) "WARNING: You are not building the HOTSPOT sources.\n" \
           "        Hotspot libs will be obtained from \n" \
           "        the location set in ALT_HOTSPOT_IMPORT_PATH. \n" \
           "" >> $(WARNING_FILE)
  endif
endif
ifeq ($(JDK_SRC_AVAILABLE), true)
  ifneq ($(BUILD_JDK), true)
	@$(ECHO) "WARNING: You are not building the JDK sources.\n" \
	   "        This will result in a development-only\n" \
	   "        build of the JDK , lacking the jdk binaries.\n" \
	   "" >> $(WARNING_FILE)
  endif
endif
ifeq ($(DEPLOY_SRC_AVAILABLE), true)
  ifneq ($(BUILD_DEPLOY), true)
	@$(ECHO) "WARNING: You are not building the DEPLOY sources.\n" \
	   "        This will result in a development-only\n" \
	   "        build of the JDK, lacking the plugin and javaws binaries.\n" \
	   "" >> $(WARNING_FILE)
  endif
endif
ifeq ($(INSTALL_SRC_AVAILABLE), true)
  ifneq ($(BUILD_INSTALL), true)
	@$(ECHO) "WARNING: You are not building the INSTALL sources.\n" \
	   "        This will result in a development-only\n" \
	   "        build of the JDK, lacking the installation bundles\n" \
	   "" >> $(WARNING_FILE)
  endif
endif
ifeq ($(SPONSORS_SRC_AVAILABLE), true)
  ifneq ($(BUILD_SPONSORS), true)
	@$(ECHO) "WARNING: You are not building the SPONSORS sources.\n" \
	   "" >> $(WARNING_FILE)
  endif
endif
ifeq ($(GENERATE_DOCS),false)
	@$(ECHO) "WARNING: This build does not include running javadoc.\n" \
	   "" >> $(WARNING_FILE)
endif
ifdef NO_IMAGES
	@$(ECHO) "WARNING: Your build environment has the variable NO_IMAGES\n" \
	   "        defined. This will result in a development-only\n" \
	   "        build of the JDK, lacking the install images\n" \
	   "        and bundles.\n" \
	   "" >> $(WARNING_FILE)
endif
ifdef DEV_ONLY
	@$(ECHO) "WARNING: Your build environment has the variable DEV_ONLY\n" \
	   "        defined. This will result in a development-only\n" \
	   "        build of the JDK, lacking the documentation\n" \
	   "        build and installation bundles.\n" \
	   "" >> $(WARNING_FILE)
endif

######################################################
# if specified, ALT_BOOTDIR must point to non-relative path if set
######################################################
alt_bootdir:
ifdef ALT_BOOTDIR
	@if [ `$(ECHO) $(subst \,/,$(ALT_BOOTDIR)) | $(EGREP) -c '^([A-Za-z]:)?/'` -ne 1 ]; then \
	  $(ECHO) "ERROR: ALT_BOOTDIR must be an Absolute Path Name, \n" \
	    "      not a Relative Path Name. \n" \
	    "      The current value of ALT_BOOTDIR is \n" \
	    "          $(ALT_BOOTDIR) \n" \
	    "      Please fix this and continue your build. \n" \
	    "" >> $(ERROR_FILE) ; \
	fi
endif

######################################################
# BOOTDIR must point to a valid JDK.
######################################################
JAVA_VERSION := $(shell $(BOOTDIR)/bin/java$(EXE_SUFFIX) -version 2>&1  | $(NAWK) -F'"' '{ print $$2 }')
JAVA_VERSION_OK := $(shell $(ECHO) $(JAVA_VERSION) | $(EGREP) -c '^1.[5-6].[0-9]')
bootdir:
	@if [ -x $(BOOTDIR)/bin/java$(EXE_SUFFIX) ]; then \
	  if [ $(JAVA_VERSION_OK) -ne 1 ]; then \
	    $(ECHO) "ERROR: Your BOOTDIR environment variable does not point \n" \
	      "      to a valid JDK for bootstrapping this build. \n" \
	      "      A JDK $(JDK_MKTG_VERSION) source generation process must be \n" \
	      "      bootstrapped using JDK $(PREVIOUS_JDK_VERSION) fcs (or later). \n" \
	      "      Apparently, your bootstrap JDK is version $(JAVA_VERSION) \n" \
	      "      Please update your ALT_BOOTDIR setting and start your build again. \n" \
	      "" >> $(ERROR_FILE) ; \
	  fi \
	else \
	  $(ECHO) "ERROR: BOOTDIR does not point to a valid Java 2 SDK \n" \
	    "      Check that you have access to \n" \
	    "          $(BOOTDIR)/bin/java$(EXE_SUFFIX) \n" \
	    "      and/or check your value of ALT_BOOTDIR. \n" \
	    "" >> $(ERROR_FILE) ; \
	fi

######################################################
# dump out the variable settings...
######################################################
ifneq ($(PLATFORM), windows)
  ifndef HOSTNAME
    HOSTNAME := $(shell hostname)
  endif
endif
settings:: 
	@$(ECHO) "Build Machine Information:" >> $(MESSAGE_FILE)
ifeq ($(PLATFORM), windows)
	@$(ECHO) "   build machine = $(COMPUTERNAME)"  >> $(MESSAGE_FILE)
else
	@$(ECHO) "   build machine = $(HOSTNAME)"  >> $(MESSAGE_FILE)
endif
	@$(ECHO) "" >> $(MESSAGE_FILE)
	@$(ECHO) "Build Directory Structure:" >> $(MESSAGE_FILE)
	@$(ECHO) "   CWD = `$(PWD)`" >> $(MESSAGE_FILE)
	@$(ECHO) "   TOPDIR = $(TOPDIR)"  >> $(MESSAGE_FILE)
ifeq ($(LANGTOOLS_SRC_AVAILABLE), true)
	@$(ECHO) "   LANGTOOLS_TOPDIR = $(LANGTOOLS_TOPDIR)"  >> $(MESSAGE_FILE)
endif
ifeq ($(JAXP_SRC_AVAILABLE), true)
	@$(ECHO) "   JAXP_TOPDIR = $(JAXP_TOPDIR)"  >> $(MESSAGE_FILE)
endif
ifeq ($(JAXWS_SRC_AVAILABLE), true)
	@$(ECHO) "   JAXWS_TOPDIR = $(JAXWS_TOPDIR)"  >> $(MESSAGE_FILE)
endif
ifeq ($(CORBA_SRC_AVAILABLE), true)
	@$(ECHO) "   CORBA_TOPDIR = $(CORBA_TOPDIR)"  >> $(MESSAGE_FILE)
endif
ifeq ($(HOTSPOT_SRC_AVAILABLE), true)
	@$(ECHO) "   HOTSPOT_TOPDIR = $(HOTSPOT_TOPDIR)"  >> $(MESSAGE_FILE)
endif
ifeq ($(JDK_SRC_AVAILABLE), true)
	@$(ECHO) "   JDK_TOPDIR = $(JDK_TOPDIR)"  >> $(MESSAGE_FILE)
endif
ifeq ($(DEPLOY_SRC_AVAILABLE), true)
	@$(ECHO) "   DEPLOY_TOPDIR = $(DEPLOY_TOPDIR)"  >> $(MESSAGE_FILE)
endif
ifeq ($(INSTALL_SRC_AVAILABLE), true)
	@$(ECHO) "   INSTALL_TOPDIR = $(INSTALL_TOPDIR)"  >> $(MESSAGE_FILE)
endif
ifeq ($(SPONSORS_SRC_AVAILABLE), true)
	@$(ECHO) "   SPONSORS_TOPDIR = $(SPONSORS_TOPDIR)"  >> $(MESSAGE_FILE)
endif
	@$(ECHO) "" >> $(MESSAGE_FILE)
ifneq ($(SKIP_COMPARE_IMAGES), true)
	@$(ECHO) "External File/Binary Locations:" >> $(MESSAGE_FILE)
	@$(ECHO) "   PREVIOUS_JRE_BUNDLE = $(PREVIOUS_JRE_BUNDLE) " >> $(MESSAGE_FILE)
	@$(ECHO) "   PREVIOUS_JDK_BUNDLE = $(PREVIOUS_JDK_BUNDLE) " >> $(MESSAGE_FILE)
	@$(ECHO) "" >> $(MESSAGE_FILE)
endif
	@$(ECHO) "Build Directives:" >> $(MESSAGE_FILE)
ifeq ($(LANGTOOLS_SRC_AVAILABLE), true)
	@$(ECHO) "   BUILD_LANGTOOLS = $(BUILD_LANGTOOLS) " >> $(MESSAGE_FILE)
endif
ifeq ($(JAXP_SRC_AVAILABLE), true)
	@$(ECHO) "   BUILD_JAXP = $(BUILD_JAXP) " >> $(MESSAGE_FILE)
endif
ifeq ($(JAXWS_SRC_AVAILABLE), true)
	@$(ECHO) "   BUILD_JAXWS = $(BUILD_JAXWS) " >> $(MESSAGE_FILE)
endif
ifeq ($(CORBA_SRC_AVAILABLE), true)
	@$(ECHO) "   BUILD_CORBA = $(BUILD_CORBA) " >> $(MESSAGE_FILE)
endif
ifeq ($(HOTSPOT_SRC_AVAILABLE), true)
	@$(ECHO) "   BUILD_HOTSPOT = $(BUILD_HOTSPOT) " >> $(MESSAGE_FILE)
endif
ifeq ($(JDK_SRC_AVAILABLE), true)
	@$(ECHO) "   BUILD_JDK    = $(BUILD_JDK) " >> $(MESSAGE_FILE)
endif
ifeq ($(DEPLOY_SRC_AVAILABLE), true)
	@$(ECHO) "   BUILD_DEPLOY  = $(BUILD_DEPLOY) " >> $(MESSAGE_FILE)
endif
ifeq ($(INSTALL_SRC_AVAILABLE), true)
	@$(ECHO) "   BUILD_INSTALL = $(BUILD_INSTALL) " >> $(MESSAGE_FILE)
endif
ifeq ($(SPONSORS_SRC_AVAILABLE), true)
	@$(ECHO) "   BUILD_SPONSORS = $(BUILD_SPONSORS) " >> $(MESSAGE_FILE)
endif
	@$(ECHO) "   DEBUG_CLASSFILES = $(DEBUG_CLASSFILES) " >> $(MESSAGE_FILE)
	@$(ECHO) "   DEBUG_BINARIES = $(DEBUG_BINARIES) " >> $(MESSAGE_FILE)
	@$(ECHO) "" >> $(MESSAGE_FILE)

.PHONY: sanity settings pre-sanity insane \
	post-sanity post-sanity-hotspot post-sanity-jdk \
	post-sanity-install post-sanity-deploy \
	alt_bootdir bootdir environment
