erikj@459: # erikj@459: # Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. erikj@459: # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. erikj@459: # erikj@459: # This code is free software; you can redistribute it and/or modify it erikj@459: # under the terms of the GNU General Public License version 2 only, as erikj@459: # published by the Free Software Foundation. Oracle designates this erikj@459: # particular file as subject to the "Classpath" exception as provided erikj@459: # by Oracle in the LICENSE file that accompanied this code. erikj@459: # erikj@459: # This code is distributed in the hope that it will be useful, but WITHOUT erikj@459: # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or erikj@459: # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License erikj@459: # version 2 for more details (a copy is included in the LICENSE file that erikj@459: # accompanied this code). erikj@459: # erikj@459: # You should have received a copy of the GNU General Public License version erikj@459: # 2 along with this work; if not, write to the Free Software Foundation, erikj@459: # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. erikj@459: # erikj@459: # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA erikj@459: # or visit www.oracle.com if you need additional information or have any erikj@459: # questions. erikj@459: # erikj@459: erikj@459: ################################################################ erikj@459: # erikj@459: # This file contains helper functions for the top-level Makefile that does ihse@839: # not depend on the spec.gmk file having been read. (The purpose of this erikj@459: # file is ju to avoid cluttering the top-level Makefile.) erikj@459: # erikj@459: ################################################################ erikj@459: erikj@459: ifndef _MAKEHELPERS_GMK erikj@459: _MAKEHELPERS_GMK := 1 erikj@459: erikj@459: ############################## erikj@459: # Stuff to run at include time erikj@459: ############################## erikj@459: erikj@459: # Find out which variables were passed explicitely on the make command line. These erikj@459: # will be passed on to sub-makes, overriding spec.gmk settings. erikj@459: MAKE_ARGS=$(foreach var,$(subst =command,,$(filter %=command,$(foreach var,$(.VARIABLES),$(var)=$(firstword $(origin $(var)))))),$(var)=$($(var))) erikj@459: erikj@459: list_alt_overrides_with_origins=$(filter ALT_%=environment ALT_%=command,$(foreach var,$(.VARIABLES),$(var)=$(firstword $(origin $(var))))) erikj@459: list_alt_overrides=$(subst =command,,$(subst =environment,,$(list_alt_overrides_with_origins))) erikj@459: ohair@494: # Store the build times in this directory. ohair@494: BUILDTIMESDIR=$(OUTPUT_ROOT)/tmp/buildtimes ohair@494: ohair@494: # Global targets are possible to run either with or without a SPEC. The prototypical ihse@839: # global target is "help". erikj@559: global_targets=help jprt% bridgeBuild ohair@494: erikj@459: ############################## erikj@459: # Functions erikj@459: ############################## erikj@459: ohair@494: define CheckEnvironment ihse@839: # Find all environment or command line variables that begin with ALT. ihse@839: $(if $(list_alt_overrides), ihse@839: @$(PRINTF) "\nWARNING: You have the following ALT_ variables set:\n" ihse@839: @$(PRINTF) "$(foreach var,$(list_alt_overrides),$(var)=$$$(var))\n" ihse@839: @$(PRINTF) "ALT_ variables are deprecated and will be ignored. Please clean your environment.\n\n" ihse@839: ) ohair@494: endef ohair@494: ohair@494: ### Functions for timers ohair@494: ohair@494: # Record starting time for build of a sub repository. ohair@494: define RecordStartTime ihse@839: $(MKDIR) -p $(BUILDTIMESDIR) ihse@839: $(DATE) '+%Y %m %d %H %M %S' | $(NAWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_start_$1 ihse@839: $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$1_human_readable ohair@494: endef ohair@494: ohair@494: # Record ending time and calculate the difference and store it in a ohair@494: # easy to read format. Handles builds that cross midnight. Expects ihse@839: # that a build will never take 24 hours or more. ohair@494: define RecordEndTime ihse@839: $(DATE) '+%Y %m %d %H %M %S' | $(NAWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_end_$1 ihse@839: $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$1_human_readable ihse@839: $(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$1` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$1` $1 | \ ihse@839: $(NAWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \ ihse@839: M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \ ihse@839: > $(BUILDTIMESDIR)/build_time_diff_$1 ohair@494: endef ohair@494: ohair@494: # Find all build_time_* files and print their contents in a list sorted ohair@494: # on the name of the sub repository. ohair@494: define ReportBuildTimes ihse@839: $(BUILD_LOG_WRAPPER) $(PRINTF) -- "----- Build times -------\nStart %s\nEnd %s\n%s\n%s\n-------------------------\n" \ ihse@839: "`$(CAT) $(BUILDTIMESDIR)/build_time_start_TOTAL_human_readable`" \ ihse@839: "`$(CAT) $(BUILDTIMESDIR)/build_time_end_TOTAL_human_readable`" \ ihse@839: "`$(LS) $(BUILDTIMESDIR)/build_time_diff_* | $(GREP) -v _TOTAL | $(XARGS) $(CAT) | $(SORT) -k 2`" \ ihse@839: "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_TOTAL`" ohair@494: endef ohair@494: ohair@494: define ResetAllTimers ihse@839: $$(shell $(MKDIR) -p $(BUILDTIMESDIR) && $(RM) $(BUILDTIMESDIR)/build_time_*) ohair@494: endef ohair@494: ohair@494: define StartGlobalTimer ihse@839: $(call RecordStartTime,TOTAL) ohair@494: endef ohair@494: ohair@494: define StopGlobalTimer ihse@839: $(call RecordEndTime,TOTAL) ohair@494: endef ohair@494: ohair@494: ### Functions for managing makefile structure (start/end of makefile and individual targets) ohair@494: ohair@494: # Do not indent this function, this will add whitespace at the start which the caller won't handle ohair@494: define GetRealTarget erikj@559: $(strip $(if $(MAKECMDGOALS),$(MAKECMDGOALS),default)) ohair@494: endef ohair@494: ohair@494: # Do not indent this function, this will add whitespace at the start which the caller won't handle ohair@494: define LastGoal ohair@494: $(strip $(lastword $(call GetRealTarget))) ohair@494: endef ohair@494: ohair@494: # Check if the current target is the final target, as specified by ohair@494: # the user on the command line. If so, call AtRootMakeEnd. ohair@494: define CheckIfMakeAtEnd ihse@839: # Check if the current target is the last goal ihse@839: $(if $(filter $@,$(call LastGoal)),$(call AtMakeEnd)) ihse@839: # If the target is 'foo-only', check if our goal was stated as 'foo' ihse@839: $(if $(filter $@,$(call LastGoal)-only),$(call AtMakeEnd)) ohair@494: endef ohair@494: ohair@494: # Hook to be called when starting to execute a top-level target ohair@494: define TargetEnter ihse@839: $(BUILD_LOG_WRAPPER) $(PRINTF) "## Starting $(patsubst %-only,%,$@)\n" ihse@839: $(call RecordStartTime,$(patsubst %-only,%,$@)) ohair@494: endef ohair@494: ohair@494: # Hook to be called when finish executing a top-level target ohair@494: define TargetExit ihse@839: $(call RecordEndTime,$(patsubst %-only,%,$@)) ihse@839: $(BUILD_LOG_WRAPPER) $(PRINTF) "## Finished $(patsubst %-only,%,$@) (build time %s)\n\n" \ ihse@839: "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_$(patsubst %-only,%,$@) | $(CUT) -f 1 -d ' '`" ihse@839: $(call CheckIfMakeAtEnd) ohair@494: endef ohair@494: ohair@494: # Hook to be called as the very first thing when running a normal build ohair@494: define AtMakeStart ihse@839: $(if $(findstring --jobserver,$(MAKEFLAGS)),$(error make -j is not supported, use make JOBS=n)) ihse@839: $(call CheckEnvironment) ihse@839: @$(PRINTF) $(LOG_INFO) "Running make as '$(MAKE) $(MFLAGS) $(MAKE_ARGS)'\n" ihse@839: @$(PRINTF) "Building $(PRODUCT_NAME) for target '$(call GetRealTarget)' in configuration '$(CONF_NAME)'\n\n" ihse@839: $(call StartGlobalTimer) ohair@494: endef ohair@494: ohair@494: # Hook to be called as the very last thing for targets that are "top level" targets ohair@494: define AtMakeEnd ihse@839: [ -f $(SJAVAC_SERVER_DIR)/server.port ] && echo Stopping sjavac server && $(TOUCH) $(SJAVAC_SERVER_DIR)/server.port.stop; true ihse@839: $(call StopGlobalTimer) ihse@839: $(call ReportBuildTimes) ihse@839: @$(PRINTF) "Finished building $(PRODUCT_NAME) for target '$(call GetRealTarget)'\n" ihse@839: $(call CheckEnvironment) ohair@494: endef ohair@494: ohair@494: ### Functions for parsing and setting up make options from command-line ohair@494: ohair@494: define FatalError ihse@839: # If the user specificed a "global" target (e.g. 'help'), do not exit but continue running ihse@839: $$(if $$(filter-out $(global_targets),$$(call GetRealTarget)),$$(error Cannot continue)) erikj@459: endef erikj@459: erikj@459: define ParseLogLevel ihse@839: ifeq ($$(origin VERBOSE),undefined) ihse@839: # Setup logging according to LOG (but only if VERBOSE is not given) tbell@507: ihse@839: # If the "nofile" argument is given, act on it and strip it away ihse@839: ifneq ($$(findstring nofile,$$(LOG)),) ihse@839: # Reset the build log wrapper, regardless of other values ihse@839: override BUILD_LOG_WRAPPER= ihse@839: # COMMA is defined in spec.gmk, but that is not included yet ihse@839: COMMA=, ihse@839: # First try to remove ",nofile" if it exists ihse@839: LOG_STRIPPED1=$$(subst $$(COMMA)nofile,,$$(LOG)) ihse@839: # Otherwise just remove "nofile" ihse@839: LOG_STRIPPED2=$$(subst nofile,,$$(LOG_STRIPPED1)) ihse@839: # We might have ended up with a leading comma. Remove it ihse@839: LOG_STRIPPED3=$$(strip $$(patsubst $$(COMMA)%,%,$$(LOG_STRIPPED2))) ihse@839: LOG_LEVEL:=$$(LOG_STRIPPED3) ihse@839: else ihse@839: LOG_LEVEL:=$$(LOG) ihse@839: endif ohair@478: ihse@839: ifeq ($$(LOG_LEVEL),) ihse@839: # Set LOG to "warn" as default if not set (and no VERBOSE given) ihse@839: override LOG_LEVEL=warn ihse@839: endif ihse@839: ifeq ($$(LOG_LEVEL),warn) ihse@839: VERBOSE=-s ihse@839: else ifeq ($$(LOG_LEVEL),info) ihse@839: VERBOSE=-s ihse@839: else ifeq ($$(LOG_LEVEL),debug) ihse@839: VERBOSE= ihse@839: else ifeq ($$(LOG_LEVEL),trace) ihse@839: VERBOSE= erikj@459: else ihse@839: $$(info Error: LOG must be one of: warn, info, debug or trace.) ihse@839: $$(eval $$(call FatalError)) erikj@459: endif ihse@839: else ihse@839: # Provide resonable interpretations of LOG_LEVEL if VERBOSE is given. ihse@839: ifeq ($(VERBOSE),) ihse@839: LOG_LEVEL:=debug ihse@839: else ihse@839: LOG_LEVEL:=warn ihse@839: endif ihse@839: ifneq ($$(LOG),) ihse@839: # We have both a VERBOSE and a LOG argument. This is OK only if this is a repeated call by ourselves, ihse@839: # but complain if this is the top-level make call. ihse@839: ifeq ($$(MAKELEVEL),0) ihse@839: $$(info Cannot use LOG=$$(LOG) and VERBOSE=$$(VERBOSE) at the same time. Choose one.) ihse@839: $$(eval $$(call FatalError)) ihse@839: endif ihse@839: endif ihse@839: endif erikj@459: endef erikj@459: erikj@459: define ParseConfAndSpec ihse@839: ifneq ($$(filter-out $(global_targets),$$(call GetRealTarget)),) ihse@839: # If we only have global targets, no need to bother with SPEC or CONF ihse@839: ifneq ($$(origin SPEC),undefined) ihse@839: # We have been given a SPEC, check that it works out properly ihse@839: ifeq ($$(wildcard $$(SPEC)),) ihse@839: $$(info Cannot locate spec.gmk, given by SPEC=$$(SPEC)) ihse@839: $$(eval $$(call FatalError)) ihse@839: endif ihse@839: ifneq ($$(origin CONF),undefined) ihse@839: # We also have a CONF argument. This is OK only if this is a repeated call by ourselves, ihse@839: # but complain if this is the top-level make call. ihse@839: ifeq ($$(MAKELEVEL),0) ihse@839: $$(info Cannot use CONF=$$(CONF) and SPEC=$$(SPEC) at the same time. Choose one.) ihse@839: $$(eval $$(call FatalError)) ihse@839: endif ihse@839: endif ihse@839: # ... OK, we're satisfied, we'll use this SPEC later on ihse@839: else ihse@839: # Find all spec.gmk files in the build output directory ihse@839: output_dir=$$(root_dir)/build ihse@839: all_spec_files=$$(wildcard $$(output_dir)/*/spec.gmk) ihse@839: ifeq ($$(all_spec_files),) ihse@839: $$(info No configurations found for $$(root_dir)! Please run configure to create a configuration.) ihse@839: $$(eval $$(call FatalError)) ihse@839: endif ihse@839: # Extract the configuration names from the path ihse@839: all_confs=$$(patsubst %/spec.gmk,%,$$(patsubst $$(output_dir)/%,%,$$(all_spec_files))) ihse@839: ihse@839: ifneq ($$(origin CONF),undefined) ihse@839: # User have given a CONF= argument. ihse@839: ifeq ($$(CONF),) ihse@839: # If given CONF=, match all configurations ihse@839: matching_confs=$$(strip $$(all_confs)) ohair@494: else ihse@839: # Otherwise select those that contain the given CONF string ihse@839: matching_confs=$$(strip $$(foreach var,$$(all_confs),$$(if $$(findstring $$(CONF),$$(var)),$$(var)))) ihse@839: endif ihse@839: ifeq ($$(matching_confs),) ihse@839: $$(info No configurations found matching CONF=$$(CONF)) ihse@839: $$(info Available configurations:) ihse@839: $$(foreach var,$$(all_confs),$$(info * $$(var))) ihse@839: $$(eval $$(call FatalError)) ihse@839: else ihse@839: ifeq ($$(words $$(matching_confs)),1) ihse@839: $$(info Building '$$(matching_confs)' (matching CONF=$$(CONF))) ihse@839: else ihse@839: $$(info Building target '$(call GetRealTarget)' in the following configurations (matching CONF=$$(CONF)):) ihse@839: $$(foreach var,$$(matching_confs),$$(info * $$(var))) ihse@839: endif ihse@839: endif erikj@459: ihse@839: # Create a SPEC definition. This will contain the path to one or more spec.gmk files. ihse@839: SPEC=$$(addsuffix /spec.gmk,$$(addprefix $$(output_dir)/,$$(matching_confs))) ihse@839: else ihse@839: # No CONF or SPEC given, check the available configurations ihse@839: ifneq ($$(words $$(all_spec_files)),1) ihse@839: $$(info No CONF given, but more than one configuration found in $$(output_dir).) ihse@839: $$(info Available configurations:) ihse@839: $$(foreach var,$$(all_confs),$$(info * $$(var))) ihse@839: $$(info Please retry building with CONF= (or SPEC=)) ihse@839: $$(eval $$(call FatalError)) ihse@839: endif ohair@494: ihse@839: # We found exactly one configuration, use it ihse@839: SPEC=$$(strip $$(all_spec_files)) ihse@839: endif erikj@459: endif ihse@839: endif erikj@459: endef erikj@459: ohair@494: ### Convenience functions from Main.gmk erikj@459: ohair@478: # Cleans the component given as $1 ohair@478: define CleanComponent ihse@839: @$(PRINTF) "Cleaning $1 build artifacts ..." ihse@839: @($(CD) $(OUTPUT_ROOT) && $(RM) -r $1) ihse@839: @$(PRINTF) " done\n" ohair@478: endef ohair@478: erikj@459: endif # _MAKEHELPERS_GMK