make/MakeHelpers.gmk

Mon, 14 Sep 2020 16:42:03 +0100

author
andrew
date
Mon, 14 Sep 2020 16:42:03 +0100
changeset 2554
7f60c2d9823e
parent 1140
6d0ebf545f49
child 1288
7801df19942d
permissions
-rw-r--r--

Added tag jdk8u272-b08 for changeset 34c6baf21464

erikj@459 1 #
erikj@459 2 # Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
erikj@459 3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
erikj@459 4 #
erikj@459 5 # This code is free software; you can redistribute it and/or modify it
erikj@459 6 # under the terms of the GNU General Public License version 2 only, as
erikj@459 7 # published by the Free Software Foundation. Oracle designates this
erikj@459 8 # particular file as subject to the "Classpath" exception as provided
erikj@459 9 # by Oracle in the LICENSE file that accompanied this code.
erikj@459 10 #
erikj@459 11 # This code is distributed in the hope that it will be useful, but WITHOUT
erikj@459 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
erikj@459 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
erikj@459 14 # version 2 for more details (a copy is included in the LICENSE file that
erikj@459 15 # accompanied this code).
erikj@459 16 #
erikj@459 17 # You should have received a copy of the GNU General Public License version
erikj@459 18 # 2 along with this work; if not, write to the Free Software Foundation,
erikj@459 19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
erikj@459 20 #
erikj@459 21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
erikj@459 22 # or visit www.oracle.com if you need additional information or have any
erikj@459 23 # questions.
erikj@459 24 #
erikj@459 25
erikj@459 26 ################################################################
erikj@459 27 #
erikj@459 28 # This file contains helper functions for the top-level Makefile that does
ihse@839 29 # not depend on the spec.gmk file having been read. (The purpose of this
erikj@459 30 # file is ju to avoid cluttering the top-level Makefile.)
erikj@459 31 #
erikj@459 32 ################################################################
erikj@459 33
erikj@459 34 ifndef _MAKEHELPERS_GMK
erikj@459 35 _MAKEHELPERS_GMK := 1
erikj@459 36
erikj@459 37 ##############################
erikj@459 38 # Stuff to run at include time
erikj@459 39 ##############################
erikj@459 40
erikj@459 41 # Find out which variables were passed explicitely on the make command line. These
erikj@459 42 # will be passed on to sub-makes, overriding spec.gmk settings.
erikj@459 43 MAKE_ARGS=$(foreach var,$(subst =command,,$(filter %=command,$(foreach var,$(.VARIABLES),$(var)=$(firstword $(origin $(var)))))),$(var)=$($(var)))
erikj@459 44
erikj@459 45 list_alt_overrides_with_origins=$(filter ALT_%=environment ALT_%=command,$(foreach var,$(.VARIABLES),$(var)=$(firstword $(origin $(var)))))
erikj@459 46 list_alt_overrides=$(subst =command,,$(subst =environment,,$(list_alt_overrides_with_origins)))
erikj@459 47
ohair@494 48 # Store the build times in this directory.
ohair@494 49 BUILDTIMESDIR=$(OUTPUT_ROOT)/tmp/buildtimes
ohair@494 50
ohair@494 51 # Global targets are possible to run either with or without a SPEC. The prototypical
ihse@839 52 # global target is "help".
mikael@1140 53 global_targets=help
ohair@494 54
erikj@459 55 ##############################
erikj@459 56 # Functions
erikj@459 57 ##############################
erikj@459 58
ohair@494 59 define CheckEnvironment
ihse@839 60 # Find all environment or command line variables that begin with ALT.
ihse@839 61 $(if $(list_alt_overrides),
ihse@839 62 @$(PRINTF) "\nWARNING: You have the following ALT_ variables set:\n"
ihse@839 63 @$(PRINTF) "$(foreach var,$(list_alt_overrides),$(var)=$$$(var))\n"
ihse@839 64 @$(PRINTF) "ALT_ variables are deprecated and will be ignored. Please clean your environment.\n\n"
ihse@839 65 )
ohair@494 66 endef
ohair@494 67
ohair@494 68 ### Functions for timers
ohair@494 69
ohair@494 70 # Record starting time for build of a sub repository.
ohair@494 71 define RecordStartTime
ihse@839 72 $(MKDIR) -p $(BUILDTIMESDIR)
ihse@839 73 $(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 74 $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$1_human_readable
ohair@494 75 endef
ohair@494 76
ohair@494 77 # Record ending time and calculate the difference and store it in a
ohair@494 78 # easy to read format. Handles builds that cross midnight. Expects
ihse@839 79 # that a build will never take 24 hours or more.
ohair@494 80 define RecordEndTime
ihse@839 81 $(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 82 $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$1_human_readable
ihse@839 83 $(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$1` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$1` $1 | \
ihse@839 84 $(NAWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \
ihse@839 85 M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \
ihse@839 86 > $(BUILDTIMESDIR)/build_time_diff_$1
ohair@494 87 endef
ohair@494 88
ohair@494 89 # Find all build_time_* files and print their contents in a list sorted
ohair@494 90 # on the name of the sub repository.
ohair@494 91 define ReportBuildTimes
ihse@839 92 $(BUILD_LOG_WRAPPER) $(PRINTF) -- "----- Build times -------\nStart %s\nEnd %s\n%s\n%s\n-------------------------\n" \
ihse@839 93 "`$(CAT) $(BUILDTIMESDIR)/build_time_start_TOTAL_human_readable`" \
ihse@839 94 "`$(CAT) $(BUILDTIMESDIR)/build_time_end_TOTAL_human_readable`" \
ihse@839 95 "`$(LS) $(BUILDTIMESDIR)/build_time_diff_* | $(GREP) -v _TOTAL | $(XARGS) $(CAT) | $(SORT) -k 2`" \
ihse@839 96 "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_TOTAL`"
ohair@494 97 endef
ohair@494 98
ohair@494 99 define ResetAllTimers
ihse@839 100 $$(shell $(MKDIR) -p $(BUILDTIMESDIR) && $(RM) $(BUILDTIMESDIR)/build_time_*)
ohair@494 101 endef
ohair@494 102
ohair@494 103 define StartGlobalTimer
ihse@839 104 $(call RecordStartTime,TOTAL)
ohair@494 105 endef
ohair@494 106
ohair@494 107 define StopGlobalTimer
ihse@839 108 $(call RecordEndTime,TOTAL)
ohair@494 109 endef
ohair@494 110
ohair@494 111 ### Functions for managing makefile structure (start/end of makefile and individual targets)
ohair@494 112
ohair@494 113 # Do not indent this function, this will add whitespace at the start which the caller won't handle
ohair@494 114 define GetRealTarget
erikj@559 115 $(strip $(if $(MAKECMDGOALS),$(MAKECMDGOALS),default))
ohair@494 116 endef
ohair@494 117
ohair@494 118 # Do not indent this function, this will add whitespace at the start which the caller won't handle
ohair@494 119 define LastGoal
ohair@494 120 $(strip $(lastword $(call GetRealTarget)))
ohair@494 121 endef
ohair@494 122
ohair@494 123 # Check if the current target is the final target, as specified by
ohair@494 124 # the user on the command line. If so, call AtRootMakeEnd.
ohair@494 125 define CheckIfMakeAtEnd
ihse@839 126 # Check if the current target is the last goal
ihse@839 127 $(if $(filter $@,$(call LastGoal)),$(call AtMakeEnd))
ihse@839 128 # If the target is 'foo-only', check if our goal was stated as 'foo'
ihse@839 129 $(if $(filter $@,$(call LastGoal)-only),$(call AtMakeEnd))
ohair@494 130 endef
ohair@494 131
ohair@494 132 # Hook to be called when starting to execute a top-level target
ohair@494 133 define TargetEnter
ihse@839 134 $(BUILD_LOG_WRAPPER) $(PRINTF) "## Starting $(patsubst %-only,%,$@)\n"
ihse@839 135 $(call RecordStartTime,$(patsubst %-only,%,$@))
ohair@494 136 endef
ohair@494 137
ohair@494 138 # Hook to be called when finish executing a top-level target
ohair@494 139 define TargetExit
ihse@839 140 $(call RecordEndTime,$(patsubst %-only,%,$@))
ihse@839 141 $(BUILD_LOG_WRAPPER) $(PRINTF) "## Finished $(patsubst %-only,%,$@) (build time %s)\n\n" \
ihse@839 142 "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_$(patsubst %-only,%,$@) | $(CUT) -f 1 -d ' '`"
ihse@839 143 $(call CheckIfMakeAtEnd)
ohair@494 144 endef
ohair@494 145
ohair@494 146 # Hook to be called as the very first thing when running a normal build
ohair@494 147 define AtMakeStart
ihse@839 148 $(if $(findstring --jobserver,$(MAKEFLAGS)),$(error make -j is not supported, use make JOBS=n))
ihse@839 149 $(call CheckEnvironment)
ihse@839 150 @$(PRINTF) $(LOG_INFO) "Running make as '$(MAKE) $(MFLAGS) $(MAKE_ARGS)'\n"
ihse@839 151 @$(PRINTF) "Building $(PRODUCT_NAME) for target '$(call GetRealTarget)' in configuration '$(CONF_NAME)'\n\n"
ihse@839 152 $(call StartGlobalTimer)
ohair@494 153 endef
ohair@494 154
ohair@494 155 # Hook to be called as the very last thing for targets that are "top level" targets
ohair@494 156 define AtMakeEnd
ihse@839 157 [ -f $(SJAVAC_SERVER_DIR)/server.port ] && echo Stopping sjavac server && $(TOUCH) $(SJAVAC_SERVER_DIR)/server.port.stop; true
ihse@839 158 $(call StopGlobalTimer)
ihse@839 159 $(call ReportBuildTimes)
ihse@839 160 @$(PRINTF) "Finished building $(PRODUCT_NAME) for target '$(call GetRealTarget)'\n"
ihse@839 161 $(call CheckEnvironment)
ohair@494 162 endef
ohair@494 163
ohair@494 164 ### Functions for parsing and setting up make options from command-line
ohair@494 165
ohair@494 166 define FatalError
ihse@839 167 # If the user specificed a "global" target (e.g. 'help'), do not exit but continue running
ihse@839 168 $$(if $$(filter-out $(global_targets),$$(call GetRealTarget)),$$(error Cannot continue))
erikj@459 169 endef
erikj@459 170
erikj@459 171 define ParseLogLevel
ihse@839 172 ifeq ($$(origin VERBOSE),undefined)
ihse@839 173 # Setup logging according to LOG (but only if VERBOSE is not given)
tbell@507 174
ihse@839 175 # If the "nofile" argument is given, act on it and strip it away
ihse@839 176 ifneq ($$(findstring nofile,$$(LOG)),)
ihse@839 177 # Reset the build log wrapper, regardless of other values
ihse@839 178 override BUILD_LOG_WRAPPER=
ihse@839 179 # COMMA is defined in spec.gmk, but that is not included yet
ihse@839 180 COMMA=,
ihse@839 181 # First try to remove ",nofile" if it exists
ihse@839 182 LOG_STRIPPED1=$$(subst $$(COMMA)nofile,,$$(LOG))
ihse@839 183 # Otherwise just remove "nofile"
ihse@839 184 LOG_STRIPPED2=$$(subst nofile,,$$(LOG_STRIPPED1))
ihse@839 185 # We might have ended up with a leading comma. Remove it
ihse@839 186 LOG_STRIPPED3=$$(strip $$(patsubst $$(COMMA)%,%,$$(LOG_STRIPPED2)))
ihse@839 187 LOG_LEVEL:=$$(LOG_STRIPPED3)
ihse@839 188 else
ihse@839 189 LOG_LEVEL:=$$(LOG)
ihse@839 190 endif
ohair@478 191
ihse@839 192 ifeq ($$(LOG_LEVEL),)
ihse@839 193 # Set LOG to "warn" as default if not set (and no VERBOSE given)
ihse@839 194 override LOG_LEVEL=warn
ihse@839 195 endif
ihse@839 196 ifeq ($$(LOG_LEVEL),warn)
ihse@839 197 VERBOSE=-s
ihse@839 198 else ifeq ($$(LOG_LEVEL),info)
ihse@839 199 VERBOSE=-s
ihse@839 200 else ifeq ($$(LOG_LEVEL),debug)
ihse@839 201 VERBOSE=
ihse@839 202 else ifeq ($$(LOG_LEVEL),trace)
ihse@839 203 VERBOSE=
erikj@459 204 else
ihse@839 205 $$(info Error: LOG must be one of: warn, info, debug or trace.)
ihse@839 206 $$(eval $$(call FatalError))
erikj@459 207 endif
ihse@839 208 else
ihse@839 209 # Provide resonable interpretations of LOG_LEVEL if VERBOSE is given.
ihse@839 210 ifeq ($(VERBOSE),)
ihse@839 211 LOG_LEVEL:=debug
ihse@839 212 else
ihse@839 213 LOG_LEVEL:=warn
ihse@839 214 endif
ihse@839 215 ifneq ($$(LOG),)
ihse@839 216 # We have both a VERBOSE and a LOG argument. This is OK only if this is a repeated call by ourselves,
ihse@839 217 # but complain if this is the top-level make call.
ihse@839 218 ifeq ($$(MAKELEVEL),0)
ihse@839 219 $$(info Cannot use LOG=$$(LOG) and VERBOSE=$$(VERBOSE) at the same time. Choose one.)
ihse@839 220 $$(eval $$(call FatalError))
ihse@839 221 endif
ihse@839 222 endif
ihse@839 223 endif
erikj@459 224 endef
erikj@459 225
erikj@459 226 define ParseConfAndSpec
ihse@839 227 ifneq ($$(filter-out $(global_targets),$$(call GetRealTarget)),)
ihse@839 228 # If we only have global targets, no need to bother with SPEC or CONF
ihse@839 229 ifneq ($$(origin SPEC),undefined)
ihse@839 230 # We have been given a SPEC, check that it works out properly
ihse@839 231 ifeq ($$(wildcard $$(SPEC)),)
ihse@839 232 $$(info Cannot locate spec.gmk, given by SPEC=$$(SPEC))
ihse@839 233 $$(eval $$(call FatalError))
ihse@839 234 endif
ihse@839 235 ifneq ($$(origin CONF),undefined)
ihse@839 236 # We also have a CONF argument. This is OK only if this is a repeated call by ourselves,
ihse@839 237 # but complain if this is the top-level make call.
ihse@839 238 ifeq ($$(MAKELEVEL),0)
ihse@839 239 $$(info Cannot use CONF=$$(CONF) and SPEC=$$(SPEC) at the same time. Choose one.)
ihse@839 240 $$(eval $$(call FatalError))
ihse@839 241 endif
ihse@839 242 endif
ihse@839 243 # ... OK, we're satisfied, we'll use this SPEC later on
ihse@839 244 else
ihse@839 245 # Find all spec.gmk files in the build output directory
ihse@839 246 output_dir=$$(root_dir)/build
ihse@839 247 all_spec_files=$$(wildcard $$(output_dir)/*/spec.gmk)
ihse@839 248 ifeq ($$(all_spec_files),)
ihse@839 249 $$(info No configurations found for $$(root_dir)! Please run configure to create a configuration.)
ihse@839 250 $$(eval $$(call FatalError))
ihse@839 251 endif
ihse@839 252 # Extract the configuration names from the path
ihse@839 253 all_confs=$$(patsubst %/spec.gmk,%,$$(patsubst $$(output_dir)/%,%,$$(all_spec_files)))
ihse@839 254
ihse@839 255 ifneq ($$(origin CONF),undefined)
ihse@839 256 # User have given a CONF= argument.
ihse@839 257 ifeq ($$(CONF),)
ihse@839 258 # If given CONF=, match all configurations
ihse@839 259 matching_confs=$$(strip $$(all_confs))
ohair@494 260 else
ihse@839 261 # Otherwise select those that contain the given CONF string
ihse@839 262 matching_confs=$$(strip $$(foreach var,$$(all_confs),$$(if $$(findstring $$(CONF),$$(var)),$$(var))))
ihse@839 263 endif
ihse@839 264 ifeq ($$(matching_confs),)
ihse@839 265 $$(info No configurations found matching CONF=$$(CONF))
ihse@839 266 $$(info Available configurations:)
ihse@839 267 $$(foreach var,$$(all_confs),$$(info * $$(var)))
ihse@839 268 $$(eval $$(call FatalError))
ihse@839 269 else
ihse@839 270 ifeq ($$(words $$(matching_confs)),1)
ihse@839 271 $$(info Building '$$(matching_confs)' (matching CONF=$$(CONF)))
ihse@839 272 else
ihse@839 273 $$(info Building target '$(call GetRealTarget)' in the following configurations (matching CONF=$$(CONF)):)
ihse@839 274 $$(foreach var,$$(matching_confs),$$(info * $$(var)))
ihse@839 275 endif
ihse@839 276 endif
erikj@459 277
ihse@839 278 # Create a SPEC definition. This will contain the path to one or more spec.gmk files.
ihse@839 279 SPEC=$$(addsuffix /spec.gmk,$$(addprefix $$(output_dir)/,$$(matching_confs)))
ihse@839 280 else
ihse@839 281 # No CONF or SPEC given, check the available configurations
ihse@839 282 ifneq ($$(words $$(all_spec_files)),1)
ihse@839 283 $$(info No CONF given, but more than one configuration found in $$(output_dir).)
ihse@839 284 $$(info Available configurations:)
ihse@839 285 $$(foreach var,$$(all_confs),$$(info * $$(var)))
ihse@839 286 $$(info Please retry building with CONF=<config pattern> (or SPEC=<specfile>))
ihse@839 287 $$(eval $$(call FatalError))
ihse@839 288 endif
ohair@494 289
ihse@839 290 # We found exactly one configuration, use it
ihse@839 291 SPEC=$$(strip $$(all_spec_files))
ihse@839 292 endif
erikj@459 293 endif
ihse@839 294 endif
erikj@459 295 endef
erikj@459 296
ohair@494 297 ### Convenience functions from Main.gmk
erikj@459 298
ohair@478 299 # Cleans the component given as $1
ohair@478 300 define CleanComponent
ihse@839 301 @$(PRINTF) "Cleaning $1 build artifacts ..."
ihse@839 302 @($(CD) $(OUTPUT_ROOT) && $(RM) -r $1)
ihse@839 303 @$(PRINTF) " done\n"
ohair@478 304 endef
ohair@478 305
erikj@459 306 endif # _MAKEHELPERS_GMK

mercurial