Merge
authorlana
Thu, 09 Apr 2015 17:35:56 -0700
changeset 29789 bfb0c4f25f0f
parent 29787 1b6c0070f4ff (current diff)
parent 29788 b581a3d9db41 (diff)
child 29790 79641c886929
Merge
--- a/common/autoconf/spec.gmk.in	Thu Apr 09 06:40:11 2015 -0700
+++ b/common/autoconf/spec.gmk.in	Thu Apr 09 17:35:56 2015 -0700
@@ -59,7 +59,7 @@
 MAKE := @MAKE@
 
 # The default make arguments
-MAKE_ARGS = $(MAKE_LOG_FLAGS) -R -I $(TOPDIR)/make/common SPEC=$(SPEC) \
+MAKE_ARGS = $(MAKE_LOG_FLAGS) -r -R -I $(TOPDIR)/make/common SPEC=$(SPEC) \
     MAKE_LOG_FLAGS="$(MAKE_LOG_FLAGS)" LOG_LEVEL=$(LOG_LEVEL)
 
 OUTPUT_SYNC_SUPPORTED:=@OUTPUT_SYNC_SUPPORTED@
@@ -231,7 +231,6 @@
 # Colon left out to be able to override IMAGES_OUTPUTDIR for bootcycle-images
 SUPPORT_OUTPUTDIR=$(BUILD_OUTPUT)/support
 BUILDTOOLS_OUTPUTDIR=$(BUILD_OUTPUT)/buildtools
-MAKESUPPORT_OUTPUTDIR=$(BUILD_OUTPUT)/makesupport
 
 HOTSPOT_OUTPUTDIR=$(BUILD_OUTPUT)/hotspot
 JDK_OUTPUTDIR=$(BUILD_OUTPUT)/jdk
--- a/make/Help.gmk	Thu Apr 09 06:40:11 2015 -0700
+++ b/make/Help.gmk	Thu Apr 09 17:35:56 2015 -0700
@@ -88,28 +88,15 @@
 	$(info $(_)                        # make test TEST="jdk_lang jdk_net")
 	$(info )
 	$(if $(all_confs), $(info Available configurations in $(build_dir):) $(foreach var,$(all_confs),$(info * $(var))),\
-	    $(info No configurations were found in $(build_dir).) $(info Run 'bash configure' to create a configuration))
+	    $(info No configurations were found in $(build_dir).) $(info Run 'bash configure' to create a configuration.))
         # We need a dummy rule otherwise make will complain
 	@true
 
-print-targets:
-	$(if $(any_spec_file), ,\
-	  @echo "Note: More targets will be available when at least one configuration exists." 1>&2\
-	)
-	@echo $(ALL_TARGETS)
-
-print-modules:
-	$(if $(any_spec_file), \
-	  @$(MAKE) -s -f $(topdir)/make/Main.gmk -I $(topdir)/make/common SPEC=$(any_spec_file) print-modules \
-	, \
-	  @echo print-modules can currently only be run when at least one configuration exists \
-	)
-
 print-configurations:
 	$(foreach var, $(all_confs), $(info $(var)))
         # We need a dummy rule otherwise make will complain
 	@true
 
-ALL_GLOBAL_TARGETS := help print-modules print-targets print-configurations
+ALL_GLOBAL_TARGETS := help print-configurations
 
 .PHONY: $(ALL_GLOBAL_TARGETS)
--- a/make/Init.gmk	Thu Apr 09 06:40:11 2015 -0700
+++ b/make/Init.gmk	Thu Apr 09 17:35:56 2015 -0700
@@ -37,84 +37,102 @@
 # serially, regardless of -j.
 .NOTPARALLEL:
 
-# If included from the top-level Makefile then topdir is set, but not when
-# recursively calling ourself with a spec.
-ifeq ($(topdir),)
-  topdir := $(strip $(patsubst %/make/, %, $(dir $(lastword $(MAKEFILE_LIST)))))
-endif
-
-# Our helper functions. Will include $(SPEC) if $(HAS_SPEC) is true.
-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
-
-# Extract main targets from Main.gmk.
-ifneq ($(any_spec_file), )
-  ifeq ($(wildcard $(dir $(any_spec_file))/make-support/module-deps.gmk),)
-    # If make-support does not exist, we need to build the genmodules java tool first.
-    $(info Creating data for first make execution in new configuration...)
-    ignore_output := $(shell $(MAKE) -r -R -f $(topdir)/make/Main.gmk \
-        -I $(topdir)/make/common SPEC=$(any_spec_file) NO_RECIPES=true FRC)
-    $(info Done)
-  endif
-  ALL_MAIN_TARGETS := $(shell $(MAKE) -r -R -f $(topdir)/make/Main.gmk \
-      -I $(topdir)/make/common SPEC=$(any_spec_file) NO_RECIPES=true print-targets)
-else
-  # Without at least a single valid configuration, we cannot extract any real
-  # targets. To provide a helpful error message about the missing configuration
-  # later on, accept whatever targets the user has provided for now.
-  ALL_MAIN_TARGETS := $(if $(MAKECMDGOALS), $(MAKECMDGOALS), default)
-endif
-
-# Targets provided by this file.
-ALL_INIT_TARGETS := reconfigure
-
-ALL_TARGETS := $(sort $(ALL_GLOBAL_TARGETS) $(ALL_MAIN_TARGETS) $(ALL_INIT_TARGETS))
-
-ifneq ($(findstring qp, $(MAKEFLAGS)),)
+ifeq ($(HAS_SPEC),)
   ##############################################################################
-  # When called with -qp, assume an external part (e.g. bash completion) is trying
-  # to understand our targets. Just list our targets and do no more checking.
+  # This is the default mode. We have not been recursively called with a SPEC.
   ##############################################################################
 
-  $(ALL_TARGETS):
-
-  .PHONY: $(ALL_TARGETS)
-
-else ifeq ($(HAS_SPEC),)
-
-  ##############################################################################
-  # 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.
-  ##############################################################################
+  # Include our helper functions.
+  include $(topdir)/make/InitSupport.gmk
 
-  $(eval $(call CheckControlVariables))
-  $(eval $(call CheckDeprecatedEnvironment))
-  $(eval $(call CheckInvalidMakeFlags))
-
-  $(eval $(call ParseConfCheckOption))
+  # 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
 
-  # Check that the LOG given is valid, and set LOG_LEVEL, LOG_NOFILE and MAKE_LOG_FLAGS.
-  $(eval $(call ParseLogLevel))
-
-  ifneq ($(findstring $(LOG_LEVEL),info debug trace),)
-    $(info Running make as '$(strip $(MAKE) $(MFLAGS) $(COMMAND_LINE_VARIABLES) $(MAKECMDGOALS))')
-  endif
+  # Targets provided by Init.gmk.
+  ALL_INIT_TARGETS := print-modules print-targets reconfigure
 
   # CALLED_TARGETS is the list of targets that the user provided,
   # or "default" if unspecified.
   CALLED_TARGETS := $(if $(MAKECMDGOALS), $(MAKECMDGOALS), default)
-  CALLED_SPEC_TARGETS := $(filter $(ALL_MAIN_TARGETS) $(ALL_INIT_TARGETS), $(CALLED_TARGETS))
-  ifneq ($(CALLED_SPEC_TARGETS),)
-    # We have at least one non-global target, which need a SPEC
+
+  # 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))
-    # Now SPECS contain 1..N spec files (otherwise ParseConfAndSpec fails)
 
+    # 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))
-    SEQUENTIAL_TARGETS := $(filter dist-clean clean%, $(CALLED_SPEC_TARGETS))
-    PARALLEL_TARGETS := $(filter-out $(INIT_TARGETS) $(SEQUENTIAL_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.
@@ -126,30 +144,41 @@
         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) -f $(topdir)/make/Init.gmk SPEC=$@ HAS_SPEC=true \
+	      $(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 targets depend on the spec files to be up to date.
-    ifeq ($(findstring reconfigure, $(CALLED_SPEC_TARGETS)), )
-      $(CALLED_SPEC_TARGETS): $(SPECS)
+    # 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
 
-    # 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_MAIN_TARGETS) $(ALL_INIT_TARGETS):
+    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) -j 1 -f $(topdir)/make/Init.gmk \
-	        SPEC=$(spec) HAS_SPEC=true \
+	    $(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)" \
+	        INIT_TARGETS="$(INIT_TARGETS)" \
+	        SEQUENTIAL_TARGETS="$(SEQUENTIAL_TARGETS)" \
 	        PARALLEL_TARGETS="$(PARALLEL_TARGETS)"  \
 	        main ) && \
 	  ) true \
@@ -158,7 +187,7 @@
 
     .PHONY: $(ALL_MAIN_TARGETS) $(ALL_INIT_TARGETS)
 
-  endif # has $(CALLED_SPEC_TARGETS)
+  endif # $(ONLY_GLOBAL_TARGETS)!=true
 
 else # HAS_SPEC=true
 
@@ -168,6 +197,14 @@
   # 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 :=
@@ -177,7 +214,19 @@
     OUTPUT_SYNC_FLAG := -O$(OUTPUT_SYNC)
   endif
 
-  $(eval $(call CheckSpecSanity))
+  ##############################################################################
+  # 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 )
 
   reconfigure:
         ifneq ($(CONFIGURE_COMMAND_LINE), )
@@ -188,42 +237,43 @@
 	( cd $(OUTPUT_ROOT) && PATH="$(ORIGINAL_PATH)" \
 	    $(BASH) $(TOPDIR)/configure $(CONFIGURE_COMMAND_LINE) )
 
-  main-init:
-	$(call RotateLogFiles)
-	$(BUILD_LOG_WRAPPER) $(PRINTF) "Building target(s) '$(strip \
-	    $(INIT_TARGETS) $(SEQUENTIAL_TARGETS) $(PARALLEL_TARGETS) \
-	    )' in configuration '$(CONF_NAME)'\n"
+  ##############################################################################
+  # 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) main-init
-        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) \
-	  )
+  main: $(INIT_TARGETS)
+        ifneq ($(SEQUENTIAL_TARGETS)$(PARALLEL_TARGETS), )
+	  $(call RotateLogFiles)
+	  $(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) )
+	    $(call CleanupSmartJavac)
+	    $(call StopGlobalTimer)
+	    $(call ReportBuildTimes)
+          endif
+	  $(BUILD_LOG_WRAPPER) $(PRINTF) "Finished building $(TARGET_DESCRIPTION)\n"
         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) \
-	  )
-	  $(call CleanupSmartJavac)
-	  $(call StopGlobalTimer)
-	  $(call ReportBuildTimes)
-        endif
-	$(BUILD_LOG_WRAPPER) $(PRINTF) "Finished building target(s) '$(strip \
-	    $(INIT_TARGETS) $(SEQUENTIAL_TARGETS) $(PARALLEL_TARGETS) \
-	    )' in configuration '$(CONF_NAME)'\n"
 
-  .PHONY: reconfigure main-init main
+  .PHONY: print-targets print-modules reconfigure main
 endif
--- a/make/InitSupport.gmk	Thu Apr 09 06:40:11 2015 -0700
+++ b/make/InitSupport.gmk	Thu Apr 09 17:35:56 2015 -0700
@@ -32,13 +32,6 @@
 ifndef _INITSUPPORT_GMK
 _INITSUPPORT_GMK := 1
 
-# Setup information about available configurations, if any.
-build_dir=$(topdir)/build
-all_spec_files=$(wildcard $(build_dir)/*/spec.gmk)
-# Extract the configuration names from the path
-all_confs=$(patsubst %/spec.gmk, %, $(patsubst $(build_dir)/%, %, $(all_spec_files)))
-any_spec_file=$(firstword $(all_spec_files))
-
 ifeq ($(HAS_SPEC),)
   ##############################################################################
   # Helper functions for the initial part of Init.gmk, before the spec file is
@@ -62,17 +55,24 @@
   # line, but in reverse order to what the user entered.
   COMMAND_LINE_VARIABLES := $(subst \#,\ , $(call reverse, $(subst \ ,\#,$(MAKEOVERRIDES))))
 
-  # A list like FOO="val1" BAR="val2" containing all user-supplied make variables
-  # that we should propagate.
+  # A list like FOO="val1" BAR="val2" containing all user-supplied make
+  # variables that we should propagate.
   USER_MAKE_VARS := $(filter-out $(addsuffix =%, $(INIT_CONTROL_VARIABLES)), \
       $(MAKEOVERRIDES))
 
+  # Setup information about available configurations, if any.
+  build_dir=$(topdir)/build
+  all_spec_files=$(wildcard $(build_dir)/*/spec.gmk)
+  # Extract the configuration names from the path
+  all_confs=$(patsubst %/spec.gmk, %, $(patsubst $(build_dir)/%, %, $(all_spec_files)))
+
   # Check for unknown command-line variables
   define CheckControlVariables
     command_line_variables := $$(strip $$(foreach var, \
         $$(subst \ ,_,$$(MAKEOVERRIDES)), \
         $$(firstword $$(subst =, , $$(var)))))
-    unknown_command_line_variables := $$(strip $$(filter-out $$(MAKE_CONTROL_VARIABLES), $$(command_line_variables)))
+    unknown_command_line_variables := $$(strip \
+        $$(filter-out $$(MAKE_CONTROL_VARIABLES), $$(command_line_variables)))
     ifneq ($$(unknown_command_line_variables), )
       $$(info Note: Command line contains non-control variables:)
       $$(foreach var, $$(unknown_command_line_variables), $$(info * $$(var)=$$($$(var))))
@@ -182,7 +182,7 @@
       override SPEC :=
     else
       # Use spec.gmk files in the build output directory
-      ifeq ($$(any_spec_file),)
+      ifeq ($$(all_spec_files),)
         $$(info Error: No configurations found for $$(topdir).)
         $$(info Please run 'bash configure' to create a configuration.)
         $$(info )
@@ -196,7 +196,8 @@
           matching_confs := $$(strip $$(all_confs))
         else
           # Otherwise select those that contain the given CONF string
-          matching_confs := $$(strip $$(foreach var, $$(all_confs), $$(if $$(findstring $$(CONF), $$(var)), $$(var))))
+          matching_confs := $$(strip $$(foreach var, $$(all_confs), \
+              $$(if $$(findstring $$(CONF), $$(var)), $$(var))))
         endif
         ifeq ($$(matching_confs),)
           $$(info Error: No configurations found matching CONF=$$(CONF).)
@@ -231,6 +232,37 @@
     endif
   endef
 
+  # Extract main targets from Main.gmk using the spec provided in $2.
+  #
+  # Param 1: FORCE = force generation of main-targets.gmk or LAZY = do not force.
+  # Param 2: The SPEC file to use.
+  define DefineMainTargets
+
+    # We will start by making sure the main-targets.gmk file is removed, if
+    # make has not been restarted. By the -include, we will trigger the
+    # rule for generating the file (which is never there since we removed it),
+    # thus generating it fresh, and make will restart, incrementing the restart
+    # count.
+    main_targets_file := $$(dir $(strip $2))make-support/main-targets.gmk
+
+    ifeq ($$(MAKE_RESTARTS),)
+      # Only do this if make has not been restarted, and if we do not force it.
+      ifeq ($(strip $1), FORCE)
+        $$(shell rm -f $$(main_targets_file))
+      endif
+    endif
+
+    $$(main_targets_file):
+	@( cd $$(topdir) && \
+	    $$(MAKE) $$(MAKE_LOG_FLAGS) -r -R -f $$(topdir)/make/Main.gmk \
+	    -I $$(topdir)/make/common SPEC=$(strip $2) NO_RECIPES=true \
+	    LOG_LEVEL=$$(LOG_LEVEL) \
+	    create-main-targets-include )
+
+    # Now include main-targets.gmk. This will define ALL_MAIN_TARGETS.
+    -include $$(main_targets_file)
+  endef
+
   define PrintConfCheckFailed
 	@echo ' '
 	@echo "Please rerun configure! Easiest way to do this is by running"
@@ -242,10 +274,9 @@
 else # $(HAS_SPEC)=true
   ##############################################################################
   # Helper functions for the 'main' target. These functions assume a single,
-  # proper and existing SPEC is provided, and will load it.
+  # proper and existing SPEC is included.
   ##############################################################################
 
-  include $(SPEC)
   include $(SRC_ROOT)/make/common/MakeBase.gmk
 
   # Define basic logging setup
@@ -263,11 +294,11 @@
 
   # Sanity check the spec file, so it matches this source code
   define CheckSpecSanity
-    ifneq ($$(topdir), $$(TOPDIR))
-      ifneq ($$(topdir), $$(ORIGINAL_TOPDIR))
-        ifneq ($$(topdir), $$(CANONICAL_TOPDIR))
+    ifneq ($$(ACTUAL_TOPDIR), $$(TOPDIR))
+      ifneq ($$(ACTUAL_TOPDIR), $$(ORIGINAL_TOPDIR))
+        ifneq ($$(ACTUAL_TOPDIR), $$(CANONICAL_TOPDIR))
           $$(info Error: SPEC mismatch! Current working directory)
-          $$(info $$(topdir))
+          $$(info $$(ACTUAL_TOPDIR))
           $$(info does not match either TOPDIR, ORIGINAL_TOPDIR or CANONICAL_TOPDIR)
           $$(info $$(TOPDIR))
           $$(info $$(ORIGINAL_TOPDIR))
--- a/make/Main.gmk	Thu Apr 09 06:40:11 2015 -0700
+++ b/make/Main.gmk	Thu Apr 09 17:35:56 2015 -0700
@@ -607,14 +607,19 @@
 
 ################################################################################
 
+# The following targets are intentionally not added to ALL_TARGETS since they
+# are internal only, to support Init.gmk.
+
 print-targets:
 	  @$(ECHO) $(sort $(ALL_TARGETS))
 
 print-modules:
 	  @$(ECHO) $(sort $(ALL_MODULES))
 
-# print-* targets intentionally not added to ALL_TARGETS since they are internal only.
-# The corresponding external targets are in Help.gmk
+create-main-targets-include:
+	  @$(ECHO) $(LOG_INFO) Generating main target list
+	  @$(ECHO) ALL_MAIN_TARGETS := $(sort $(ALL_TARGETS)) > \
+	      $(MAKESUPPORT_OUTPUTDIR)/main-targets.gmk
 
 ################################################################################