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

include MakeIncludeStart.gmk
ifeq ($(INCLUDE), true)

################################################################################
# This file contains helper functions for Init.gmk.
################################################################################

# Define basic logging setup
BUILD_LOG := $(OUTPUTDIR)/build.log
BUILD_PROFILE_LOG := $(OUTPUTDIR)/build-profile.log

BUILD_LOG_PIPE := > >($(TEE) -a $(BUILD_LOG)) 2> >($(TEE) -a $(BUILD_LOG) >&2) && wait
# Use this for simple echo/printf commands that are never expected to print
# to stderr.
BUILD_LOG_PIPE_SIMPLE := | $(TEE) -a $(BUILD_LOG)

# Setup the build environment to match the requested specification on
# level of reproducible builds
define SetupReproducibleBuild
  ifeq ($$(SOURCE_DATE), updated)
    # For static values of SOURCE_DATE (not "updated"), these are set in spec.gmk
    export SOURCE_DATE_EPOCH := $$(shell $$(DATE) +"%s")
    export SOURCE_DATE_ISO_8601 := $$(call EpochToISO8601, $$(SOURCE_DATE_EPOCH))
  endif
endef

# Parse COMPARE_BUILD into COMPARE_BUILD_*
# Syntax: COMPARE_BUILD=CONF=<configure options>:PATCH=<patch file>:
#         MAKE=<make targets>:COMP_OPTS=<compare script options>:
#         COMP_DIR=<compare script base dir>|<default>:
#         FAIL=<bool>
# If neither CONF or PATCH is given, assume <default> means CONF if it
# begins with "--", otherwise assume it means PATCH.
# MAKE and COMP_OPTS can only be used with CONF and/or PATCH specified.
# If any value contains "+", it will be replaced by space.
# FAIL can be set to false to have the return value of compare be ignored.
define ParseCompareBuild
  ifneq ($$(COMPARE_BUILD), )
    COMPARE_BUILD_OUTPUTDIR := $(WORKSPACE_ROOT)/build/compare-build/$(CONF_NAME)
    COMPARE_BUILD_FAIL := true

    ifneq ($$(findstring :, $$(COMPARE_BUILD)), )
      $$(foreach part, $$(subst :, , $$(COMPARE_BUILD)), \
        $$(if $$(filter PATCH=%, $$(part)), \
          $$(eval COMPARE_BUILD_PATCH = $$(strip $$(patsubst PATCH=%, %, $$(part)))) \
        ) \
        $$(if $$(filter CONF=%, $$(part)), \
          $$(eval COMPARE_BUILD_CONF = $$(strip $$(subst +, , $$(patsubst CONF=%, %, $$(part))))) \
        ) \
        $$(if $$(filter MAKE=%, $$(part)), \
          $$(eval COMPARE_BUILD_MAKE = $$(strip $$(subst +, , $$(patsubst MAKE=%, %, $$(part))))) \
        ) \
        $$(if $$(filter COMP_OPTS=%, $$(part)), \
          $$(eval COMPARE_BUILD_COMP_OPTS = $$(strip $$(subst +, , $$(patsubst COMP_OPTS=%, %, $$(part))))) \
        ) \
        $$(if $$(filter COMP_DIR=%, $$(part)), \
          $$(eval COMPARE_BUILD_COMP_DIR = $$(strip $$(subst +, , $$(patsubst COMP_DIR=%, %, $$(part))))) \
        ) \
        $$(if $$(filter FAIL=%, $$(part)), \
          $$(eval COMPARE_BUILD_FAIL = $$(strip $$(subst +, , $$(patsubst FAIL=%, %, $$(part))))) \
        ) \
        $$(if $$(filter NODRYRUN=%, $$(part)), \
          $$(eval COMPARE_BUILD_NODRYRUN = $$(strip $$(subst +, , $$(patsubst NODRYRUN=%, %, $$(part))))) \
        ) \
      )
    else
      # Separate handling for single field case, to allow for spaces in values.
      ifneq ($$(filter PATCH=%, $$(COMPARE_BUILD)), )
        COMPARE_BUILD_PATCH = $$(strip $$(patsubst PATCH=%, %, $$(COMPARE_BUILD)))
      else ifneq ($$(filter CONF=%, $$(COMPARE_BUILD)), )
        COMPARE_BUILD_CONF = $$(strip $$(subst +, , $$(patsubst CONF=%, %, $$(COMPARE_BUILD))))
      else ifneq ($$(filter --%, $$(COMPARE_BUILD)), )
        # Assume CONF if value begins with --
        COMPARE_BUILD_CONF = $$(strip $$(subst +, , $$(COMPARE_BUILD)))
      else
        # Otherwise assume patch file
        COMPARE_BUILD_PATCH = $$(strip $$(COMPARE_BUILD))
      endif
    endif
    ifneq ($$(COMPARE_BUILD_PATCH), )
      ifneq ($$(wildcard $$(WORKSPACE_ROOT)/$$(COMPARE_BUILD_PATCH)), )
        # Assume relative path, if file exists
        COMPARE_BUILD_PATCH := $$(wildcard $$(WORKSPACE_ROOT)/$$(COMPARE_BUILD_PATCH))
      else ifeq ($$(wildcard $$(COMPARE_BUILD_PATCH)), )
        $$(error Patch file $$(COMPARE_BUILD_PATCH) does not exist)
      endif
      ifneq ($$(COMPARE_BUILD_NODRYRUN), true)
        PATCH_DRY_RUN := $$(shell cd $$(WORKSPACE_ROOT) && $$(PATCH) --dry-run -p1 < $$(COMPARE_BUILD_PATCH) > /dev/null 2>&1 || $$(ECHO) FAILED)
        ifeq ($$(PATCH_DRY_RUN), FAILED)
          $$(error Patch file $$(COMPARE_BUILD_PATCH) does not apply cleanly)
        endif
      endif
    endif
    ifneq ($$(COMPARE_BUILD_FAIL), true)
      COMPARE_BUILD_IGNORE_RESULT := || true
    endif
  endif
endef

# Prepare for a comparison rebuild
define PrepareCompareBuild
	$(ECHO) "Preparing for comparison rebuild"
        # Apply patch, if any
	$(if $(COMPARE_BUILD_PATCH), cd $(WORKSPACE_ROOT) && $(PATCH) -p1 < $(COMPARE_BUILD_PATCH))
        # Move the first build away temporarily
	$(RM) -r $(WORKSPACE_ROOT)/build/.compare-build-temp
	$(MKDIR) -p $(WORKSPACE_ROOT)/build/.compare-build-temp
	$(MV) $(OUTPUTDIR) $(WORKSPACE_ROOT)/build/.compare-build-temp
        # Restore an old compare-build, or create a new compare-build directory.
	if test -d $(COMPARE_BUILD_OUTPUTDIR); then \
	  $(MV) $(COMPARE_BUILD_OUTPUTDIR) $(OUTPUTDIR); \
	else \
	  $(MKDIR) -p $(OUTPUTDIR); \
	fi
        # Re-run configure with the same arguments (and possibly some additional),
        # must be done after patching.
	( cd $(CONFIGURE_START_DIR) && PATH="$(ORIGINAL_PATH)" \
	    $(BASH) $(WORKSPACE_ROOT)/configure $(CONFIGURE_COMMAND_LINE) $(COMPARE_BUILD_CONF))
endef

# Cleanup after a compare build
define CleanupCompareBuild
        # If running with a COMPARE_BUILD patch, reverse-apply it, but continue
        # even if that fails (can happen with removed files).
	$(if $(COMPARE_BUILD_PATCH), cd $(WORKSPACE_ROOT) && $(PATCH) -R -p1 < $(COMPARE_BUILD_PATCH) || true)
        # Move this build away and restore the original build
	$(MKDIR) -p $(WORKSPACE_ROOT)/build/compare-build
	$(MV) $(OUTPUTDIR) $(COMPARE_BUILD_OUTPUTDIR)
	$(MV) $(WORKSPACE_ROOT)/build/.compare-build-temp/$(CONF_NAME) $(OUTPUTDIR)
	$(RM) -r $(WORKSPACE_ROOT)/build/.compare-build-temp
endef

# Do the actual comparison of two builds
define CompareBuildDoComparison
        # Compare first and second build. Ignore any error code from compare.sh.
	$(ECHO) "Comparing between comparison rebuild (this/new) and baseline (other/old)"
	$(if $(COMPARE_BUILD_COMP_DIR), \
	  +(cd $(COMPARE_BUILD_OUTPUTDIR) && ./compare.sh --diffs $(COMPARE_BUILD_COMP_OPTS) \
	      -2dirs $(COMPARE_BUILD_OUTPUTDIR)/$(COMPARE_BUILD_COMP_DIR) \
	      $(OUTPUTDIR)/$(COMPARE_BUILD_COMP_DIR) $(COMPARE_BUILD_IGNORE_RESULT)), \
	  +(cd $(COMPARE_BUILD_OUTPUTDIR) && ./compare.sh --diffs $(COMPARE_BUILD_COMP_OPTS) \
	      -o $(OUTPUTDIR) $(COMPARE_BUILD_IGNORE_RESULT)) \
	)
endef

define PrintFailureReports
	$(if $(filter none, $(LOG_REPORT)), , \
	  $(RM) $(MAKESUPPORT_OUTPUTDIR)/failure-summary.log ; \
	  $(if $(wildcard $(MAKESUPPORT_OUTPUTDIR)/failure-logs/*.log), \
	    ( \
	      $(ECHO) "" ; \
	      $(ECHO) "=== Output from failing command(s) repeated here ===" ;  \
	      $(foreach logfile, $(sort $(wildcard $(MAKESUPPORT_OUTPUTDIR)/failure-logs/*.log)), \
	          $(ECHO) "* For target $(notdir $(basename $(logfile))):" ; \
	          $(if $(filter all, $(LOG_REPORT)), \
	            $(GREP) -v -e "^Note: including file:" <  $(logfile) || true ; \
	          , \
	            ($(GREP) -v -e "^Note: including file:" <  $(logfile) || true) | $(HEAD) -n 15 ; \
	            if test `$(WC) -l < $(logfile)` -gt 15; then \
	              $(ECHO) "   ... (rest of output omitted)" ; \
	            fi ; \
	          ) \
	      ) \
	      $(ECHO) "" ; \
	      $(ECHO) "* All command lines available in $(MAKESUPPORT_OUTPUTDIR)/failure-logs." ; \
	      $(ECHO) "=== End of repeated output ===" ; \
	    )  >> $(MAKESUPPORT_OUTPUTDIR)/failure-summary.log  \
	  ) \
	)
endef

define PrintBuildLogFailures
	$(if $(filter none, $(LOG_REPORT)), , \
	  if $(GREP) -q "recipe for target .* failed" $(BUILD_LOG) 2> /dev/null; then  \
	    $(ECHO) "" ; \
	    $(ECHO) "=== Make failed targets repeated here ===" ; \
	    $(GREP) "recipe for target .* failed" $(BUILD_LOG) ; \
	    $(ECHO) "=== End of repeated output ===" ; \
	    $(ECHO) "" ; \
	    $(ECHO) "HELP: Try searching the build log for the name of the first failed target." ; \
	  else \
	    $(ECHO) "" ; \
	    $(ECHO) "No indication of failed target found." ; \
	    $(ECHO) "HELP: Try searching the build log for '] Error'." ; \
	  fi >> $(MAKESUPPORT_OUTPUTDIR)/failure-summary.log ; \
	  $(CAT) $(MAKESUPPORT_OUTPUTDIR)/failure-summary.log \
	)
endef

define RotateLogFiles
	$(RM) $(BUILD_LOG).old 2> /dev/null && \
	$(MV) $(BUILD_LOG) $(BUILD_LOG).old 2> /dev/null || true
	$(if $(findstring true, $(LOG_PROFILE_TIMES_FILE)), \
	  $(RM) $(BUILD_PROFILE_LOG).old 2> /dev/null && \
	  $(MV) $(BUILD_PROFILE_LOG) $(BUILD_PROFILE_LOG).old 2> /dev/null || true \
	)
endef

# Failure logs are only supported for "parallel" main targets, not the
# (trivial) sequential make targets (such as clean and reconfigure),
# since the failure-logs directory creation will conflict with clean.
# We also make sure the javatmp directory exists, which is needed if a java
# process (like javac) is using java.io.tmpdir.
define PrepareFailureLogs
	$(RM) -r $(MAKESUPPORT_OUTPUTDIR)/failure-logs 2> /dev/null && \
	$(MKDIR) -p $(MAKESUPPORT_OUTPUTDIR)/failure-logs
	$(MKDIR) -p $(JAVA_TMP_DIR)
	$(RM) $(MAKESUPPORT_OUTPUTDIR)/exit-with-error 2> /dev/null
endef

# Remove any javac server logs and port files. This
# prevents a new make run to reuse the previous servers.
define PrepareJavacServer
	$(if $(JAVAC_SERVER_DIR), \
	  $(RM) -r $(JAVAC_SERVER_DIR) 2> /dev/null && \
	  $(MKDIR) -p $(JAVAC_SERVER_DIR) \
	)
endef

define CleanupJavacServer
	[ -f $(JAVAC_SERVER_DIR)/server.port ] && $(ECHO) Stopping javac server && \
	    $(TOUCH) $(JAVAC_SERVER_DIR)/server.port.stop; true
endef

ifeq ($(call isBuildOs, windows), true)
  # On windows we need to synchronize with the javac server to be able to
  # move or remove the build output directory. Since we have no proper
  # synchronization process, wait for a while and hope it helps. This is only
  # used by build comparisons.
    define WaitForJavacServerFinish
	$(if $(JAVAC_SERVER_DIR), \
	  sleep 5 \
	)
  endef
else
  define WaitForJavacServerFinish
  endef
endif

##############################################################################
# Functions for timers
##############################################################################

# Store the build times in this directory.
BUILDTIMESDIR := $(OUTPUTDIR)/make-support/build-times

# Record starting time for build of a sub repository.
define RecordStartTime
	$(DATE) '+%Y %m %d %H %M %S' | $(AWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_start_$(strip $1) && \
	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$(strip $1)_human_readable
endef

# Record ending time and calculate the difference and store it in a
# easy to read format. Handles builds that cross midnight. Expects
# that a build will never take 24 hours or more.
define RecordEndTime
	$(DATE) '+%Y %m %d %H %M %S' | $(AWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)
	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)_human_readable
	$(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$(strip $1)` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$(strip $1)` $1 | \
	    $(AWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \
	    M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \
	    > $(BUILDTIMESDIR)/build_time_diff_$(strip $1)
endef

define StartGlobalTimer
	$(RM) -r $(BUILDTIMESDIR) 2> /dev/null && \
	$(MKDIR) -p $(BUILDTIMESDIR) && \
	$(call RecordStartTime,TOTAL)
endef

define StopGlobalTimer
	$(call RecordEndTime,TOTAL)
endef

# Find all build_time_* files and print their contents in a list sorted
# on the name of the sub repository.
define ReportBuildTimes
	$(PRINTF) $(LOG_INFO) -- \
	    "----- Build times -------\nStart %s\nEnd   %s\n%s\n%s\n-------------------------\n" \
	    "`$(CAT) $(BUILDTIMESDIR)/build_time_start_TOTAL_human_readable`" \
	    "`$(CAT) $(BUILDTIMESDIR)/build_time_end_TOTAL_human_readable`" \
	    "`$(LS) $(BUILDTIMESDIR)/build_time_diff_* | $(GREP) -v _TOTAL | \
	    $(XARGS) $(CAT) | $(SORT) -k 2`" \
	    "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_TOTAL`" \
	    $(BUILD_LOG_PIPE_SIMPLE)
endef

define ReportProfileTimes
  $(if $(findstring true, $(LOG_PROFILE_TIMES_LOG)), \
    [ ! -f $(BUILD_PROFILE_LOG) ] || \
    { $(ECHO) Begin $(notdir $(BUILD_PROFILE_LOG)) && \
      $(CAT) $(BUILD_PROFILE_LOG) && \
      $(ECHO) End $(notdir $(BUILD_PROFILE_LOG)); \
    } \
    $(BUILD_LOG_PIPE_SIMPLE)
  )
endef

################################################################################

endif # include guard
include MakeIncludeEnd.gmk
