make/Init.gmk
author prr
Mon, 14 Sep 2015 10:56:55 -0700
changeset 32568 fc5205c29b31
parent 32461 e1cfbafc4e9c
child 32715 4d558a41a1ec
permissions -rw-r--r--
8136397: Build should recognise .cc file extension Reviewed-by: ihse

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

################################################################################
# This is the bootstrapping part of the build. This file is included from the
# top level Makefile, and is responsible for launching the Main.gmk file with
# the proper make and the proper make arguments.
################################################################################

# This must be the first rule
default:
.PHONY: default

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

ifeq ($(HAS_SPEC),)
  ##############################################################################
  # This is the default mode. We have not been recursively called with a SPEC.
  ##############################################################################

  # Include our helper functions.
  include $(topdir)/make/InitSupport.gmk

  # Here are "global" targets, i.e. targets that can be executed without having
  # a configuration. This will define ALL_GLOBAL_TARGETS.
  include $(topdir)/make/Help.gmk

  # Targets provided by Init.gmk.
  ALL_INIT_TARGETS := print-modules print-targets print-configuration reconfigure

  # CALLED_TARGETS is the list of targets that the user provided,
  # or "default" if unspecified.
  CALLED_TARGETS := $(if $(MAKECMDGOALS), $(MAKECMDGOALS), default)

  # Extract non-global targets that require a spec file.
  CALLED_SPEC_TARGETS := $(filter-out $(ALL_GLOBAL_TARGETS), $(CALLED_TARGETS))

  # If we have only global targets, or if we are called with -qp (assuming an
  # external part, e.g. bash completion, is trying to understand our targets),
  # we will skip SPEC location and the sanity checks.
  ifeq ($(CALLED_SPEC_TARGETS), )
    ONLY_GLOBAL_TARGETS := true
  endif
  ifneq ($(findstring qp, $(MAKEFLAGS)),)
    ONLY_GLOBAL_TARGETS := true
  endif

  ifeq ($(ONLY_GLOBAL_TARGETS), true)
    ############################################################################
    # We have only global targets, or are called with -pq.
    ############################################################################

    ifeq ($(wildcard $(SPEC)), )
      # If we have no SPEC provided, we will just make a "best effort" target list.
      # First try to grab any available pre-existing main-targets.gmk.
      main_targets_file := $(firstword $(wildcard $(build_dir)/*/make-support/main-targets.gmk))
      ifneq ($(main_targets_file), )
        # Extract the SPEC that corresponds to this main-targets.gmk file.
        SPEC := $(patsubst %/make-support/main-targets.gmk, %/spec.gmk, $(main_targets_file))
      else
        # None found, pick an arbitrary SPEC for which to generate a file
        SPEC := $(firstword $(all_spec_files))
      endif
    endif

    ifneq ($(wildcard $(SPEC)), )
      $(eval $(call DefineMainTargets, LAZY, $(SPEC)))
    else
      # If we have no configurations we can not provide any main targets.
      ALL_MAIN_TARGETS :=
    endif

    ALL_TARGETS := $(sort $(ALL_GLOBAL_TARGETS) $(ALL_MAIN_TARGETS) $(ALL_INIT_TARGETS))

    # Just list all our targets.
    $(ALL_TARGETS):

    .PHONY: $(ALL_TARGETS)

  else
    ############################################################################
    # This is the normal case, we have been called from the command line by the
    # user and we need to call ourself back with a proper SPEC.
    # We have at least one non-global target, so we need to find a spec file.
    ############################################################################

    # Basic checks on environment and command line.
    $(eval $(call CheckControlVariables))
    $(eval $(call CheckDeprecatedEnvironment))
    $(eval $(call CheckInvalidMakeFlags))

    # Check that CONF_CHECK is valid.
    $(eval $(call ParseConfCheckOption))

    # Check that the LOG given is valid, and set LOG_LEVEL, LOG_NOFILE and MAKE_LOG_FLAGS.
    $(eval $(call ParseLogLevel))

    # After this SPECS contain 1..N spec files (otherwise ParseConfAndSpec fails).
    $(eval $(call ParseConfAndSpec))

    # Extract main targets from Main.gmk using the spec(s) provided. In theory,
    # with multiple specs, we should find the intersection of targets provided
    # by all specs, but we approximate this by an arbitrary spec from the list.
    # This will setup ALL_MAIN_TARGETS.
    $(eval $(call DefineMainTargets, FORCE, $(firstword $(SPECS))))

    # Separate called targets depending on type.
    INIT_TARGETS := $(filter $(ALL_INIT_TARGETS), $(CALLED_SPEC_TARGETS))
    MAIN_TARGETS := $(filter $(ALL_MAIN_TARGETS), $(CALLED_SPEC_TARGETS))
    SEQUENTIAL_TARGETS := $(filter dist-clean clean%, $(MAIN_TARGETS))
    PARALLEL_TARGETS := $(filter-out $(SEQUENTIAL_TARGETS), $(MAIN_TARGETS))

    # The spec files depend on the autoconf source code. This check makes sure
    # the configuration is up to date after changes to configure.
    $(SPECS): $(wildcard $(topdir)/common/autoconf/*)
        ifeq ($(CONF_CHECK), fail)
	  @echo "Error: The configuration is not up to date for '$(lastword $(subst /, , $(dir $@)))'."
	  $(call PrintConfCheckFailed)
	  @exit 2
        else ifeq ($(CONF_CHECK), auto)
	  @echo "Note: The configuration is not up to date for '$(lastword $(subst /, , $(dir $@)))'."
	  @( cd $(topdir) && \
	      $(MAKE) $(MFLAGS) $(MAKE_LOG_FLAGS) -r -R -f $(topdir)/make/Init.gmk \
	      SPEC=$@ HAS_SPEC=true ACTUAL_TOPDIR=$(topdir) \
	      reconfigure )
        else ifeq ($(CONF_CHECK), ignore)
          # Do nothing
        endif

    # Unless reconfigure is explicitely called, let all main targets depend on
    # the spec files to be up to date.
    ifeq ($(findstring reconfigure, $(INIT_TARGETS)), )
      $(MAIN_TARGETS): $(SPECS)
    endif

    make-info:
        ifneq ($(findstring $(LOG_LEVEL),info debug trace),)
	  $(info Running make as '$(strip $(MAKE) $(MFLAGS) \
	      $(COMMAND_LINE_VARIABLES) $(MAKECMDGOALS))')
        endif

    # Now the init and main targets will be called, once for each SPEC. The
    # recipe will be run once for every target specified, but we only want to
    # execute the recipe a single time, hence the TARGET_DONE with a dummy
    # command if true.
    $(ALL_INIT_TARGETS) $(ALL_MAIN_TARGETS): make-info
	@$(if $(TARGET_DONE), \
	  true \
	, \
	  $(foreach spec, $(SPECS), \
	    ( cd $(topdir) && \
	    $(MAKE) $(MFLAGS) $(MAKE_LOG_FLAGS) -r -R -j 1 -f $(topdir)/make/Init.gmk \
	        SPEC=$(spec) HAS_SPEC=true ACTUAL_TOPDIR=$(topdir) \
	        USER_MAKE_VARS="$(USER_MAKE_VARS)" MAKE_LOG_FLAGS=$(MAKE_LOG_FLAGS) \
	        LOG_LEVEL=$(LOG_LEVEL) LOG_NOFILE=$(LOG_NOFILE) \
	        INIT_TARGETS="$(INIT_TARGETS)" \
	        SEQUENTIAL_TARGETS="$(SEQUENTIAL_TARGETS)" \
	        PARALLEL_TARGETS="$(PARALLEL_TARGETS)"  \
	        main ) && \
	  ) true \
	  $(eval TARGET_DONE=true) \
	)

    .PHONY: $(ALL_MAIN_TARGETS) $(ALL_INIT_TARGETS)

  endif # $(ONLY_GLOBAL_TARGETS)!=true

else # HAS_SPEC=true

  ##############################################################################
  # Now we have a spec. This part provides the "main" target that acts as a
  # trampoline to call the Main.gmk with the value of $(MAKE) found in the spec
  # file.
  ##############################################################################

  include $(SPEC)

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

  # Verify that the spec file we included seems okay.
  $(eval $(call CheckSpecSanity))

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

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

  ##############################################################################
  # Init targets
  ##############################################################################

  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-configuration:
	  $(ECHO) $(CONFIGURE_COMMAND_LINE)

  reconfigure:
        ifneq ($(CONFIGURE_COMMAND_LINE), )
	  $(ECHO) "Re-running configure using arguments '$(CONFIGURE_COMMAND_LINE)'"
        else
	  $(ECHO) "Re-running configure using default settings"
        endif
	( cd $(OUTPUT_ROOT) && PATH="$(ORIGINAL_PATH)" \
	    $(BASH) $(TOPDIR)/configure $(CONFIGURE_COMMAND_LINE) )

  ##############################################################################
  # The main target, for delegating into Main.gmk
  ##############################################################################

  MAIN_TARGETS := $(SEQUENTIAL_TARGETS) $(PARALLEL_TARGETS)
  TARGET_DESCRIPTION := target$(if $(word 2, $(MAIN_TARGETS)),s) \
      '$(strip $(MAIN_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 explicitely propagated using $(USER_MAKE_VARS).
  main: MAKEOVERRIDES :=

  main: $(INIT_TARGETS)
        ifneq ($(SEQUENTIAL_TARGETS)$(PARALLEL_TARGETS), )
	  $(call RotateLogFiles)
	  $(call PrepareFailureLogs)
	  $(BUILD_LOG_WRAPPER) $(PRINTF) "Building $(TARGET_DESCRIPTION)\n"
          ifneq ($(SEQUENTIAL_TARGETS), )
            # Don't touch build output dir since we might be cleaning. That
            # means no log wrapper.
	    ( cd $(TOPDIR) && \
	        $(MAKE) $(MAKE_ARGS) -j 1 -f make/Main.gmk $(USER_MAKE_VARS) \
	        $(SEQUENTIAL_TARGETS) )
          endif
          ifneq ($(PARALLEL_TARGETS), )
	    $(call StartGlobalTimer)
	    $(call PrepareSmartJavac)
	    ( cd $(TOPDIR) && \
	        $(BUILD_LOG_WRAPPER) $(MAKE) $(MAKE_ARGS) $(OUTPUT_SYNC_FLAG) \
	            -j $(JOBS) -f make/Main.gmk $(USER_MAKE_VARS) \
	            $(PARALLEL_TARGETS) || \
	        ( exitcode=$$? && $(BUILD_LOG_WRAPPER) \
	        $(PRINTF) "\nERROR: Build failed for $(TARGET_DESCRIPTION) (exit code $$exitcode) \n" && \
	        cd $(TOPDIR) && $(MAKE) $(MAKE_ARGS) -j 1 -f make/Init.gmk \
	            HAS_SPEC=true on-failure ; \
	        exit $$exitcode ) )
	    $(call CleanupSmartJavac)
	    $(call StopGlobalTimer)
	    $(call ReportBuildTimes)
          endif
	  $(BUILD_LOG_WRAPPER) $(PRINTF) "Finished building $(TARGET_DESCRIPTION)\n"
        endif

    on-failure:
        ifneq ($(wildcard $(MAKESUPPORT_OUTPUTDIR)/failure-logs/*), )
	  $(PRINTF) "=== Output from failing command(s) repeated here ===\n"
	  $(foreach logfile, $(sort $(wildcard $(MAKESUPPORT_OUTPUTDIR)/failure-logs/*)), \
	      $(PRINTF) "* For target $(notdir $(basename $(logfile))):\n" $(NEWLINE) \
	      $(CAT) $(logfile) | $(GREP) -v -e "^Note: including file:" $(NEWLINE) \
	  )
	  $(PRINTF) "=== End of repeated output ===\n"
        endif
	if $(GREP) -q "recipe for target .* failed" $(BUILD_LOG) 2> /dev/null; then  \
	  $(PRINTF) "=== Make failure sequence repeated here ===\n" ; \
	  $(GREP) "recipe for target .* failed" $(BUILD_LOG) ; \
	  $(PRINTF) "=== End of repeated output ===\n" ; \
	  $(PRINTF) "Hint: Try searching the build log for the name of the first failed target.\n" ; \
	else \
	  $(PRINTF) "No indication of failed target found.\n" ; \
	  $(PRINTF) "Hint: Try searching the build log for '] Error'.\n" ; \
	fi
	$(PRINTF) "Hint: If caused by a warning, try configure --disable-warnings-as-errors.\n\n"

  .PHONY: print-targets print-modules reconfigure main on-failure
endif