make/MakeHelpers.gmk

Thu, 12 Oct 2017 19:39:08 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:39:08 +0800
changeset 1288
7801df19942d
parent 1140
6d0ebf545f49
parent 1133
50aaf272884f
permissions
-rw-r--r--

merge

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

mercurial