#
# Copyright (c) 2012, 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 MakeFileStart.gmk

################################################################################
# Init.gmk sits between PreInit.gmk and Main.gmk when bootstrapping the build.
# It is called from PreInit.gmk, and its main responsibility is to launch
# Main.gmk with the proper make and the proper make arguments.
# PreMain.gmk has provided us with a proper SPEC. This allows us to use the
# value of $(MAKE) for all further make calls.
################################################################################

# Our helper functions.
include $(TOPDIR)/make/InitSupport.gmk
include LogUtils.gmk

# Inclusion of this pseudo-target will cause make to execute this file
# serially, regardless of -j.
.NOTPARALLEL:

# Parse COMPARE_BUILD (for makefile development)
$(eval $(call ParseCompareBuild))

# Setup reproducible build environment
$(eval $(call SetupReproducibleBuild))

# If no LOG= was given on command line, but we have a non-standard default
# value, use that instead and re-parse log level.
ifeq ($(LOG), )
  ifneq ($(DEFAULT_LOG), )
    override LOG := $(DEFAULT_LOG)
    $(eval $(call ParseLogLevel))
  endif
endif

ifeq ($(LOG_NOFILE), true)
  # Disable build log if LOG=[level,]nofile was given
  override BUILD_LOG_PIPE :=
  override BUILD_LOG_PIPE_SIMPLE :=
endif

ifeq ($(filter dist-clean, $(SEQUENTIAL_TARGETS)), dist-clean)
  # We can't have a log file if we're about to remove it.
  override BUILD_LOG_PIPE :=
  override BUILD_LOG_PIPE_SIMPLE :=
endif

ifeq ($(OUTPUT_SYNC_SUPPORTED), true)
  OUTPUT_SYNC_FLAG := -O$(OUTPUT_SYNC)
endif

##############################################################################
# Init targets. These are handled fully, here and now.
##############################################################################

print-modules:
	( cd $(TOPDIR) && \
	    $(MAKE) $(MAKE_ARGS) -j 1 -f make/Main.gmk $(USER_MAKE_VARS) \
	    NO_RECIPES=true print-modules )

print-targets:
	( cd $(TOPDIR) && \
	    $(MAKE) $(MAKE_ARGS) -j 1 -f make/Main.gmk $(USER_MAKE_VARS) \
	    NO_RECIPES=true print-targets )

print-tests:
	( cd $(TOPDIR) && \
	    $(MAKE) $(MAKE_ARGS) -j 1 -f make/Main.gmk $(USER_MAKE_VARS) \
	    NO_RECIPES=true print-tests )

print-configuration:
	$(ECHO) $(CONFIGURE_COMMAND_LINE)

reconfigure:
        ifneq ($(REAL_CONFIGURE_COMMAND_EXEC_FULL), )
	  $(ECHO) "Re-running configure using original command line '$(REAL_CONFIGURE_COMMAND_EXEC_SHORT) $(REAL_CONFIGURE_COMMAND_LINE)'"
	  $(eval RECONFIGURE_COMMAND := $(REAL_CONFIGURE_COMMAND_EXEC_FULL) $(REAL_CONFIGURE_COMMAND_LINE))
        else ifneq ($(CONFIGURE_COMMAND_LINE), )
	  $(ECHO) "Re-running configure using arguments '$(CONFIGURE_COMMAND_LINE)'"
	  $(eval RECONFIGURE_COMMAND := $(BASH) $(TOPDIR)/configure $(CONFIGURE_COMMAND_LINE))
        else
	  $(ECHO) "Re-running configure using default settings"
	  $(eval RECONFIGURE_COMMAND := $(BASH) $(TOPDIR)/configure)
        endif
	( cd $(CONFIGURE_START_DIR) && PATH="$(ORIGINAL_PATH)" AUTOCONF="$(AUTOCONF)" \
	    CUSTOM_ROOT="$(CUSTOM_ROOT)" \
	    CUSTOM_CONFIG_DIR="$(CUSTOM_CONFIG_DIR)" \
	    $(RECONFIGURE_COMMAND) )

# Create files that are needed to run most targets in Main.gmk
create-make-helpers:
	( cd $(TOPDIR) && \
	    $(MAKE) $(MAKE_ARGS) -j 1 -f make/GenerateFindTests.gmk \
	    $(USER_MAKE_VARS) )
	( cd $(TOPDIR) && \
	    $(MAKE) $(MAKE_ARGS) -j 1 -f make/Main.gmk $(USER_MAKE_VARS) \
	    UPDATE_MODULE_DEPS=true NO_RECIPES=true \
	    create-main-targets-include )

.PHONY: print-modules print-targets print-tests print-configuration \
    reconfigure create-make-helpers

##############################################################################
# The main target. This will delegate all other targets into Main.gmk.
##############################################################################

MAIN_TARGETS := $(SEQUENTIAL_TARGETS) $(PARALLEL_TARGETS) $(COMPARE_BUILD_MAKE)
# If building the default target, add what they are to the description.
DESCRIPTION_TARGETS := $(strip $(MAIN_TARGETS))
ifeq ($(DESCRIPTION_TARGETS), default)
  DESCRIPTION_TARGETS += ($(DEFAULT_MAKE_TARGET))
endif
TARGET_DESCRIPTION := target$(if $(word 2, $(MAIN_TARGETS)),s) \
    '$(strip $(DESCRIPTION_TARGETS))' in configuration '$(CONF_NAME)'

# MAKEOVERRIDES is automatically set and propagated by Make to sub-Make calls.
# We need to clear it of the init-specific variables. The user-specified
# variables are explicitly propagated using $(USER_MAKE_VARS).
main: MAKEOVERRIDES :=

main: $(INIT_TARGETS) create-make-helpers
        ifneq ($(SEQUENTIAL_TARGETS)$(PARALLEL_TARGETS), )
	  $(call RotateLogFiles)
	  $(ECHO) "Building $(TARGET_DESCRIPTION)" $(BUILD_LOG_PIPE_SIMPLE)
          ifneq ($(SEQUENTIAL_TARGETS), )
            # Don't touch build output dir since we might be cleaning. That
            # means no log pipe.
	    ( cd $(TOPDIR) && \
	        $(MAKE) $(MAKE_ARGS) -j 1 -f make/Main.gmk $(USER_MAKE_VARS) \
	        $(SEQUENTIAL_TARGETS) )
            # We might have cleaned away essential files, recreate them.
	    ( cd $(TOPDIR) && \
	        $(MAKE) $(MAKE_ARGS) -j 1 -f make/Init.gmk create-make-helpers )
          endif
          ifneq ($(PARALLEL_TARGETS), )
	    $(call PrepareFailureLogs)
	    $(call StartGlobalTimer)
	    $(call PrepareJavacServer)
            # JOBS will only be empty for a bootcycle-images recursive call
            # or if specified via a make argument directly. In those cases
            # treat it as NOT using jobs at all.
	    ( cd $(TOPDIR) && \
	        $(NICE) $(MAKE) $(MAKE_ARGS) $(OUTPUT_SYNC_FLAG) \
	            $(if $(JOBS), -j $(JOBS)) \
	            -f make/Main.gmk $(USER_MAKE_VARS) \
	            $(PARALLEL_TARGETS) $(COMPARE_BUILD_MAKE) $(BUILD_LOG_PIPE) || \
	        ( exitcode=$$? && \
	        $(ECHO) "" $(BUILD_LOG_PIPE_SIMPLE) && \
	        $(ECHO) "ERROR: Build failed for $(TARGET_DESCRIPTION) (exit code $$exitcode)" \
	            $(BUILD_LOG_PIPE_SIMPLE) && \
	        cd $(TOPDIR) && $(MAKE) $(MAKE_ARGS) -j 1 -f make/Init.gmk \
	            on-failure ; \
	        exit $$exitcode ) )
	    $(call CleanupJavacServer)
	    $(call StopGlobalTimer)
	    $(call ReportBuildTimes)
          endif
	  if test -f $(MAKESUPPORT_OUTPUTDIR)/exit-with-error ; then \
	    exit 1 ; \
	  fi
	  $(ECHO) "Finished building $(TARGET_DESCRIPTION)" $(BUILD_LOG_PIPE_SIMPLE)
	  $(call ReportProfileTimes)
        endif

on-failure:
	$(call CleanupJavacServer)
	$(call StopGlobalTimer)
	$(call ReportBuildTimes)
	$(call PrintFailureReports)
	$(call PrintBuildLogFailures)
	$(call ReportProfileTimes)
	$(ECHO) "HELP: Run 'make doctor' to diagnose build problems."
	$(ECHO) ""
        ifneq ($(COMPARE_BUILD), )
	  $(call CleanupCompareBuild)
        endif

# Support targets for COMPARE_BUILD, used for makefile development
pre-compare-build:
	$(call WaitForJavacServerFinish)
	$(call PrepareCompareBuild)

post-compare-build:
	$(call WaitForJavacServerFinish)
	$(call CleanupCompareBuild)
	$(call CompareBuildDoComparison)

.PHONY: main on-failure pre-compare-build post-compare-build

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

include MakeFileEnd.gmk
