InitSupport.gmk revision 1789:11b31df300ae
1#
2# Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# This code is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 2 only, as
7# published by the Free Software Foundation.  Oracle designates this
8# particular file as subject to the "Classpath" exception as provided
9# by Oracle in the LICENSE file that accompanied this code.
10#
11# This code is distributed in the hope that it will be useful, but WITHOUT
12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14# version 2 for more details (a copy is included in the LICENSE file that
15# accompanied this code).
16#
17# You should have received a copy of the GNU General Public License version
18# 2 along with this work; if not, write to the Free Software Foundation,
19# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22# or visit www.oracle.com if you need additional information or have any
23# questions.
24#
25
26################################################################################
27# This file contains helper functions for Init.gmk.
28# It is divided in two parts, depending on if a SPEC is present or not
29# (HAS_SPEC is true or not).
30################################################################################
31
32ifndef _INITSUPPORT_GMK
33_INITSUPPORT_GMK := 1
34
35ifeq ($(HAS_SPEC),)
36  ##############################################################################
37  # Helper functions for the initial part of Init.gmk, before the spec file is
38  # loaded. Most of these functions provide parsing and setting up make options
39  # from the command-line.
40  ##############################################################################
41
42  # Make control variables, handled by Init.gmk
43  INIT_CONTROL_VARIABLES := LOG CONF SPEC JOBS CONF_CHECK COMPARE_BUILD
44
45  # All known make control variables
46  MAKE_CONTROL_VARIABLES := $(INIT_CONTROL_VARIABLES) TEST JDK_FILTER
47
48  # Define a simple reverse function.
49  # Should maybe move to MakeBase.gmk, but we can't include that file now.
50  reverse = \
51      $(if $(strip $(1)), $(call reverse, $(wordlist 2, $(words $(1)), $(1)))) \
52          $(firstword $(1))
53
54  # The variable MAKEOVERRIDES contains variable assignments from the command
55  # line, but in reverse order to what the user entered.
56  # The '\#' <=> '\ 'dance is needed to keep values with space in them connected.
57  COMMAND_LINE_VARIABLES := $(subst \#,\ , $(call reverse, $(subst \ ,\#,$(MAKEOVERRIDES))))
58
59  # A list like FOO="val1" BAR="val2" containing all user-supplied make
60  # variables that we should propagate.
61  # The '\#' <=> '\ 'dance is needed to keep values with space in them connected.
62  USER_MAKE_VARS := $(subst \#,\ , $(filter-out $(addsuffix =%, $(INIT_CONTROL_VARIABLES)), \
63      $(subst \ ,\#,$(MAKEOVERRIDES))))
64
65  # Setup information about available configurations, if any.
66  build_dir=$(topdir)/build
67  all_spec_files=$(wildcard $(build_dir)/*/spec.gmk)
68  # Extract the configuration names from the path
69  all_confs=$(patsubst %/spec.gmk, %, $(patsubst $(build_dir)/%, %, $(all_spec_files)))
70
71  # Check for unknown command-line variables
72  define CheckControlVariables
73    command_line_variables := $$(strip $$(foreach var, \
74        $$(subst \ ,_,$$(MAKEOVERRIDES)), \
75        $$(firstword $$(subst =, , $$(var)))))
76    unknown_command_line_variables := $$(strip \
77        $$(filter-out $$(MAKE_CONTROL_VARIABLES), $$(command_line_variables)))
78    ifneq ($$(unknown_command_line_variables), )
79      $$(info Note: Command line contains non-control variables:)
80      $$(foreach var, $$(unknown_command_line_variables), $$(info * $$(var)=$$($$(var))))
81      $$(info Make sure it is not mistyped, and that you intend to override this variable.)
82      $$(info 'make help' will list known control variables.)
83      $$(info )
84    endif
85  endef
86
87  # Check for deprecated ALT_ variables
88  define CheckDeprecatedEnvironment
89    defined_alt_variables := $$(filter ALT_%, $$(.VARIABLES))
90    ifneq ($$(defined_alt_variables), )
91      $$(info Warning: You have the following ALT_ variables set:)
92      $$(foreach var, $$(defined_alt_variables), $$(info * $$(var)=$$($$(var))))
93      $$(info ALT_ variables are deprecated, and may result in a failed build.)
94      $$(info Please clean your environment.)
95      $$(info )
96    endif
97  endef
98
99  # Check for invalid make flags like -j
100  define CheckInvalidMakeFlags
101    # This is a trick to get this rule to execute before any other rules
102    # MAKEFLAGS only indicate -j if read in a recipe (!)
103    $$(topdir)/make/Init.gmk: .FORCE
104	$$(if $$(findstring --jobserver, $$(MAKEFLAGS)), \
105	    $$(info Error: 'make -jN' is not supported, use 'make JOBS=N') \
106	    $$(error Cannot continue) \
107	)
108    .FORCE:
109    .PHONY: .FORCE
110  endef
111
112  # Check that the CONF_CHECK option is valid and set up handling
113  define ParseConfCheckOption
114    ifeq ($$(CONF_CHECK), )
115      # Default behavior is fail
116      CONF_CHECK := fail
117    else ifneq ($$(filter-out auto fail ignore, $$(CONF_CHECK)),)
118      $$(info Error: CONF_CHECK must be one of: auto, fail or ignore.)
119      $$(error Cannot continue)
120    endif
121  endef
122
123  define ParseLogLevel
124    # Catch old-style VERBOSE= command lines.
125    ifneq ($$(origin VERBOSE), undefined)
126      $$(info Error: VERBOSE is deprecated. Use LOG=<warn|info|debug|trace> instead.)
127      $$(error Cannot continue)
128    endif
129
130    # Setup logging according to LOG
131
132    # If the "nofile" argument is given, act on it and strip it away
133    ifneq ($$(findstring nofile, $$(LOG)),)
134      LOG_NOFILE := true
135      # COMMA is defined in spec.gmk, but that is not included yet
136      COMMA := ,
137      # First try to remove ",nofile" if it exists, otherwise just remove "nofile"
138      LOG_STRIPPED := $$(subst nofile,, $$(subst $$(COMMA)nofile,, $$(LOG)))
139      # We might have ended up with a leading comma. Remove it
140      LOG_LEVEL := $$(strip $$(patsubst $$(COMMA)%, %, $$(LOG_STRIPPED)))
141    else
142      LOG_LEVEL := $$(LOG)
143    endif
144
145    ifeq ($$(LOG_LEVEL),)
146      # Set LOG to "warn" as default if not set
147      LOG_LEVEL := warn
148    endif
149
150    ifeq ($$(LOG_LEVEL), warn)
151      MAKE_LOG_FLAGS := -s
152    else ifeq ($$(LOG_LEVEL), info)
153      MAKE_LOG_FLAGS := -s
154    else ifeq ($$(LOG_LEVEL), debug)
155      MAKE_LOG_FLAGS :=
156    else ifeq ($$(LOG_LEVEL), trace)
157      MAKE_LOG_FLAGS := -d
158    else
159      $$(info Error: LOG must be one of: warn, info, debug or trace.)
160      $$(error Cannot continue)
161    endif
162  endef
163
164  define ParseConfAndSpec
165    ifneq ($$(origin SPEC), undefined)
166      # We have been given a SPEC, check that it works out properly
167      ifneq ($$(origin CONF), undefined)
168        # We also have a CONF argument. We can't have both.
169        $$(info Error: Cannot use CONF=$$(CONF) and SPEC=$$(SPEC) at the same time. Choose one.)
170        $$(error Cannot continue)
171      endif
172      ifeq ($$(wildcard $$(SPEC)),)
173        $$(info Error: Cannot locate spec.gmk, given by SPEC=$$(SPEC).)
174        $$(error Cannot continue)
175      endif
176      ifeq ($$(filter /%, $$(SPEC)),)
177        # If given with relative path, make it absolute
178        SPECS := $$(CURDIR)/$$(strip $$(SPEC))
179      else
180        SPECS := $$(SPEC)
181      endif
182
183      # For now, unset this SPEC variable.
184      override SPEC :=
185    else
186      # Use spec.gmk files in the build output directory
187      ifeq ($$(all_spec_files),)
188        $$(info Error: No configurations found for $$(topdir).)
189        $$(info Please run 'bash configure' to create a configuration.)
190        $$(info )
191        $$(error Cannot continue)
192      endif
193
194      ifneq ($$(origin CONF), undefined)
195        # User have given a CONF= argument.
196        ifeq ($$(CONF),)
197          # If given CONF=, match all configurations
198          matching_confs := $$(strip $$(all_confs))
199        else
200          # Otherwise select those that contain the given CONF string
201          matching_confs := $$(strip $$(foreach var, $$(all_confs), \
202              $$(if $$(findstring $$(CONF), $$(var)), $$(var))))
203        endif
204        ifeq ($$(matching_confs),)
205          $$(info Error: No configurations found matching CONF=$$(CONF).)
206          $$(info Available configurations in $$(build_dir):)
207          $$(foreach var, $$(all_confs), $$(info * $$(var)))
208          $$(error Cannot continue)
209        else
210          ifeq ($$(words $$(matching_confs)), 1)
211            $$(info Building configuration '$$(matching_confs)' (matching CONF=$$(CONF)))
212          else
213            $$(info Building these configurations (matching CONF=$$(CONF)):)
214            $$(foreach var, $$(matching_confs), $$(info * $$(var)))
215          endif
216        endif
217
218        # Create a SPEC definition. This will contain the path to one or more spec.gmk files.
219        SPECS := $$(addsuffix /spec.gmk, $$(addprefix $$(build_dir)/, $$(matching_confs)))
220      else
221        # No CONF or SPEC given, check the available configurations
222        ifneq ($$(words $$(all_spec_files)), 1)
223          $$(info Error: No CONF given, but more than one configuration found.)
224          $$(info Available configurations in $$(build_dir):)
225          $$(foreach var, $$(all_confs), $$(info * $$(var)))
226          $$(info Please retry building with CONF=<config pattern> (or SPEC=<spec file>).)
227          $$(info )
228          $$(error Cannot continue)
229        endif
230
231        # We found exactly one configuration, use it
232        SPECS := $$(strip $$(all_spec_files))
233      endif
234    endif
235  endef
236
237  # Extract main targets from Main.gmk using the spec provided in $2.
238  #
239  # Param 1: FORCE = force generation of main-targets.gmk or LAZY = do not force.
240  # Param 2: The SPEC file to use.
241  define DefineMainTargets
242
243    # We will start by making sure the main-targets.gmk file is removed, if
244    # make has not been restarted. By the -include, we will trigger the
245    # rule for generating the file (which is never there since we removed it),
246    # thus generating it fresh, and make will restart, incrementing the restart
247    # count.
248    main_targets_file := $$(dir $(strip $2))make-support/main-targets.gmk
249
250    ifeq ($$(MAKE_RESTARTS),)
251      # Only do this if make has not been restarted, and if we do not force it.
252      ifeq ($(strip $1), FORCE)
253        $$(shell rm -f $$(main_targets_file))
254      endif
255    endif
256
257    $$(main_targets_file):
258	@( cd $$(topdir) && \
259	    $$(MAKE) $$(MAKE_LOG_FLAGS) -r -R -f $$(topdir)/make/Main.gmk \
260	    -I $$(topdir)/make/common SPEC=$(strip $2) NO_RECIPES=true \
261	    LOG_LEVEL=$$(LOG_LEVEL) \
262	    create-main-targets-include )
263
264    # Now include main-targets.gmk. This will define ALL_MAIN_TARGETS.
265    -include $$(main_targets_file)
266  endef
267
268  define PrintConfCheckFailed
269	@echo ' '
270	@echo "Please rerun configure! Easiest way to do this is by running"
271	@echo "'make reconfigure'."
272	@echo "This behavior may also be changed using CONF_CHECK=<ignore|auto>."
273	@echo ' '
274  endef
275
276else # $(HAS_SPEC)=true
277  ##############################################################################
278  # Helper functions for the 'main' target. These functions assume a single,
279  # proper and existing SPEC is included.
280  ##############################################################################
281
282  include $(SRC_ROOT)/make/common/MakeBase.gmk
283
284  # Define basic logging setup
285  BUILD_LOG := $(OUTPUT_ROOT)/build.log
286  BUILD_TRACE_LOG := $(OUTPUT_ROOT)/build-trace-time.log
287
288  BUILD_LOG_WRAPPER := $(BASH) $(SRC_ROOT)/common/bin/logger.sh $(BUILD_LOG)
289
290  # Sanity check the spec file, so it matches this source code
291  define CheckSpecSanity
292    ifneq ($$(ACTUAL_TOPDIR), $$(TOPDIR))
293      ifneq ($$(ACTUAL_TOPDIR), $$(ORIGINAL_TOPDIR))
294        ifneq ($$(ACTUAL_TOPDIR), $$(CANONICAL_TOPDIR))
295          $$(info Error: SPEC mismatch! Current working directory)
296          $$(info $$(ACTUAL_TOPDIR))
297          $$(info does not match either TOPDIR, ORIGINAL_TOPDIR or CANONICAL_TOPDIR)
298          $$(info $$(TOPDIR))
299          $$(info $$(ORIGINAL_TOPDIR))
300          $$(info $$(CANONICAL_TOPDIR))
301          $$(error Cannot continue)
302        endif
303      endif
304    endif
305  endef
306
307  # Parse COMPARE_BUILD into COMPARE_BUILD_*
308  # Syntax: COMPARE_BUILD=CONF=<configure options>:PATCH=<patch file>:
309  #         MAKE=<make targets>:COMP_OPTS=<compare script options>:
310  #         COMP_DIR=<compare script base dir>|<default>
311  # If neither CONF or PATCH is given, assume <default> means CONF if it
312  # begins with "--", otherwise assume it means PATCH.
313  # MAKE and COMP_OPTS can only be used with CONF and/or PATCH specified.
314  # If any value contains "+", it will be replaced by space.
315  define ParseCompareBuild
316    ifneq ($$(COMPARE_BUILD), )
317      COMPARE_BUILD_OUTPUT_ROOT := $(TOPDIR)/build/compare-build/$(CONF_NAME)
318
319      ifneq ($$(findstring :, $$(COMPARE_BUILD)), )
320        $$(foreach part, $$(subst :, , $$(COMPARE_BUILD)), \
321          $$(if $$(filter PATCH=%, $$(part)), \
322            $$(eval COMPARE_BUILD_PATCH=$$(strip $$(patsubst PATCH=%, %, $$(part)))) \
323          ) \
324          $$(if $$(filter CONF=%, $$(part)), \
325            $$(eval COMPARE_BUILD_CONF=$$(strip $$(subst +, , $$(patsubst CONF=%, %, $$(part))))) \
326          ) \
327          $$(if $$(filter MAKE=%, $$(part)), \
328            $$(eval COMPARE_BUILD_MAKE=$$(strip $$(subst +, , $$(patsubst MAKE=%, %, $$(part))))) \
329          ) \
330          $$(if $$(filter COMP_OPTS=%, $$(part)), \
331            $$(eval COMPARE_BUILD_COMP_OPTS=$$(strip $$(subst +, , $$(patsubst COMP_OPTS=%, %, $$(part))))) \
332          ) \
333          $$(if $$(filter COMP_DIR=%, $$(part)), \
334            $$(eval COMPARE_BUILD_COMP_DIR=$$(strip $$(subst +, , $$(patsubst COMP_DIR=%, %, $$(part))))) \
335          ) \
336        )
337      else
338        # Separate handling for single field case, to allow for spaces in values.
339        ifneq ($$(filter PATCH=%, $$(COMPARE_BUILD)), )
340          COMPARE_BUILD_PATCH=$$(strip $$(patsubst PATCH=%, %, $$(COMPARE_BUILD)))
341        else ifneq ($$(filter CONF=%, $$(COMPARE_BUILD)), )
342          COMPARE_BUILD_CONF=$$(strip $$(subst +, , $$(patsubst CONF=%, %, $$(COMPARE_BUILD))))
343        else ifneq ($$(filter --%, $$(COMPARE_BUILD)), )
344          # Assume CONF if value begins with --
345          COMPARE_BUILD_CONF=$$(strip $$(subst +, , $$(COMPARE_BUILD)))
346        else
347          # Otherwise assume patch file
348          COMPARE_BUILD_PATCH=$$(strip $$(COMPARE_BUILD))
349        endif
350      endif
351      ifneq ($$(COMPARE_BUILD_PATCH), )
352        ifneq ($$(wildcard $$(TOPDIR)/$$(COMPARE_BUILD_PATCH)), )
353          # Assume relative path, if file exists
354          COMPARE_BUILD_PATCH := $$(wildcard $$(TOPDIR)/$$(COMPARE_BUILD_PATCH))
355        else ifeq ($$(wildcard $$(COMPARE_BUILD_PATCH)), )
356          $$(error Patch file $$(COMPARE_BUILD_PATCH) does not exist)
357        endif
358      endif
359    endif
360  endef
361
362  # Prepare for a comparison rebuild
363  define PrepareCompareBuild
364	$(ECHO) "Preparing for comparison rebuild"
365        # Apply patch, if any
366	$(if $(COMPARE_BUILD_PATCH), $(PATCH) -p1 < $(COMPARE_BUILD_PATCH))
367        # Move the first build away temporarily
368	$(RM) -r $(TOPDIR)/build/.compare-build-temp
369	$(MKDIR) -p $(TOPDIR)/build/.compare-build-temp
370	$(MV) $(OUTPUT_ROOT) $(TOPDIR)/build/.compare-build-temp
371        # Restore an old compare-build, or create a new compare-build directory.
372	if test -d $(COMPARE_BUILD_OUTPUT_ROOT); then \
373	  $(MV) $(COMPARE_BUILD_OUTPUT_ROOT) $(OUTPUT_ROOT); \
374	else \
375	  $(MKDIR) -p $(OUTPUT_ROOT); \
376	fi
377        # Re-run configure with the same arguments (and possibly some additional),
378        # must be done after patching.
379	( cd $(OUTPUT_ROOT) && PATH="$(ORIGINAL_PATH)" \
380	    $(BASH) $(TOPDIR)/configure $(CONFIGURE_COMMAND_LINE) $(COMPARE_BUILD_CONF))
381  endef
382
383  # Cleanup after a compare build
384  define CleanupCompareBuild
385        # If running with a COMPARE_BUILD patch, reverse-apply it
386	$(if $(COMPARE_BUILD_PATCH), $(PATCH) -R -p1 < $(COMPARE_BUILD_PATCH))
387        # Move this build away and restore the original build
388	$(MKDIR) -p $(TOPDIR)/build/compare-build
389	$(MV) $(OUTPUT_ROOT) $(COMPARE_BUILD_OUTPUT_ROOT)
390	$(MV) $(TOPDIR)/build/.compare-build-temp/$(CONF_NAME) $(OUTPUT_ROOT)
391	$(RM) -r $(TOPDIR)/build/.compare-build-temp
392  endef
393
394  # Do the actual comparison of two builds
395  define CompareBuildDoComparison
396        # Compare first and second build. Ignore any error code from compare.sh.
397	$(ECHO) "Comparing between comparison rebuild (this/new) and baseline (other/old)"
398	$(if $(COMPARE_BUILD_COMP_DIR), \
399	  +(cd $(COMPARE_BUILD_OUTPUT_ROOT) && ./compare.sh $(COMPARE_BUILD_COMP_OPTS) \
400	      -2dirs $(COMPARE_BUILD_OUTPUT_ROOT)/$(COMPARE_BUILD_COMP_DIR) $(OUTPUT_ROOT)/$(COMPARE_BUILD_COMP_DIR) || true), \
401	  +(cd $(COMPARE_BUILD_OUTPUT_ROOT) && ./compare.sh $(COMPARE_BUILD_COMP_OPTS) \
402	      -o $(OUTPUT_ROOT) || true) \
403	)
404  endef
405
406  define PrintFailureReports
407	$(if $(wildcard $(MAKESUPPORT_OUTPUTDIR)/failure-logs/*), \
408	  $(PRINTF) "=== Output from failing command(s) repeated here ===\n" $(NEWLINE) \
409	  $(foreach logfile, $(sort $(wildcard $(MAKESUPPORT_OUTPUTDIR)/failure-logs/*)), \
410	      $(PRINTF) "* For target $(notdir $(basename $(logfile))):\n" $(NEWLINE) \
411	      $(CAT) $(logfile) | $(GREP) -v -e "^Note: including file:" $(NEWLINE) \
412	  ) \
413	  $(PRINTF) "=== End of repeated output ===\n" \
414	)
415  endef
416
417  define PrintBuildLogFailures
418	if $(GREP) -q "recipe for target .* failed" $(BUILD_LOG) 2> /dev/null; then  \
419	  $(PRINTF) "=== Make failure sequence repeated here ===\n" ; \
420	  $(GREP) "recipe for target .* failed" $(BUILD_LOG) ; \
421	  $(PRINTF) "=== End of repeated output ===\n" ; \
422	  $(PRINTF) "Hint: Try searching the build log for the name of the first failed target.\n" ; \
423	else \
424	  $(PRINTF) "No indication of failed target found.\n" ; \
425	  $(PRINTF) "Hint: Try searching the build log for '] Error'.\n" ; \
426	fi
427  endef
428
429  define RotateLogFiles
430	$(RM) $(BUILD_LOG).old 2> /dev/null
431	$(MV) $(BUILD_LOG) $(BUILD_LOG).old 2> /dev/null || true
432	$(if $(findstring trace, $(LOG_LEVEL)), \
433	  $(RM) $(BUILD_TRACE_LOG).old 2> /dev/null && \
434	  $(MV) $(BUILD_TRACE_LOG) $(BUILD_TRACE_LOG).old 2> /dev/null || true \
435	)
436  endef
437
438  define PrepareFailureLogs
439	$(RM) -r $(MAKESUPPORT_OUTPUTDIR)/failure-logs 2> /dev/null
440	$(MKDIR) -p $(MAKESUPPORT_OUTPUTDIR)/failure-logs
441  endef
442
443  # Remove any javac server logs and port files. This
444  # prevents a new make run to reuse the previous servers.
445  define PrepareSmartJavac
446	$(if $(SJAVAC_SERVER_DIR), \
447	  $(RM) -r $(SJAVAC_SERVER_DIR) 2> /dev/null && \
448	  $(MKDIR) -p $(SJAVAC_SERVER_DIR) \
449	)
450  endef
451
452  define CleanupSmartJavac
453	[ -f $(SJAVAC_SERVER_DIR)/server.port ] && $(ECHO) Stopping sjavac server && \
454	    $(TOUCH) $(SJAVAC_SERVER_DIR)/server.port.stop; true
455  endef
456
457  define StartGlobalTimer
458	$(RM) -r $(BUILDTIMESDIR) 2> /dev/null
459	$(MKDIR) -p $(BUILDTIMESDIR)
460	$(call RecordStartTime,TOTAL)
461  endef
462
463  define StopGlobalTimer
464	$(call RecordEndTime,TOTAL)
465  endef
466
467  # Find all build_time_* files and print their contents in a list sorted
468  # on the name of the sub repository.
469  define ReportBuildTimes
470	$(BUILD_LOG_WRAPPER) $(PRINTF) $(LOG_INFO) -- \
471	    "----- Build times -------\nStart %s\nEnd   %s\n%s\n%s\n-------------------------\n" \
472	    "`$(CAT) $(BUILDTIMESDIR)/build_time_start_TOTAL_human_readable`" \
473	    "`$(CAT) $(BUILDTIMESDIR)/build_time_end_TOTAL_human_readable`" \
474	    "`$(LS) $(BUILDTIMESDIR)/build_time_diff_* | $(GREP) -v _TOTAL | \
475	    $(XARGS) $(CAT) | $(SORT) -k 2`" \
476	    "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_TOTAL`"
477  endef
478
479endif # HAS_SPEC
480
481endif # _INITSUPPORT_GMK
482