Thu, 24 Jan 2013 09:17:26 +0100
Merge
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 |
erikj@459 | 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 |
ohair@494 | 52 | # global target is "help". |
erikj@559 | 53 | global_targets=help jprt% bridgeBuild |
ohair@494 | 54 | |
erikj@459 | 55 | ############################## |
erikj@459 | 56 | # Functions |
erikj@459 | 57 | ############################## |
erikj@459 | 58 | |
ohair@494 | 59 | define CheckEnvironment |
ohair@494 | 60 | # Find all environment or command line variables that begin with ALT. |
ohair@494 | 61 | $(if $(list_alt_overrides), |
ohair@494 | 62 | @$(PRINTF) "\nWARNING: You have the following ALT_ variables set:\n" |
ohair@494 | 63 | @$(PRINTF) "$(foreach var,$(list_alt_overrides),$(var)=$$$(var))\n" |
ohair@494 | 64 | @$(PRINTF) "ALT_ variables are deprecated and will be ignored. Please clean your environment.\n\n" |
ohair@494 | 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 |
ohair@494 | 72 | $(MKDIR) -p $(BUILDTIMESDIR) |
ohair@494 | 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 |
ohair@494 | 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 |
ohair@494 | 79 | # that a build will never take 24 hours or more. |
ohair@494 | 80 | define RecordEndTime |
ohair@494 | 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 |
ohair@494 | 82 | $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$1_human_readable |
ohair@494 | 83 | $(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$1` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$1` $1 | \ |
ohair@494 | 84 | $(NAWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \ |
ohair@494 | 85 | M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \ |
ohair@494 | 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 |
ohair@494 | 92 | $(BUILD_LOG_WRAPPER) $(PRINTF) -- "----- Build times -------\nStart %s\nEnd %s\n%s\n%s\n-------------------------\n" \ |
ohair@494 | 93 | "`$(CAT) $(BUILDTIMESDIR)/build_time_start_TOTAL_human_readable`" \ |
ohair@494 | 94 | "`$(CAT) $(BUILDTIMESDIR)/build_time_end_TOTAL_human_readable`" \ |
ohair@494 | 95 | "`$(LS) $(BUILDTIMESDIR)/build_time_diff_* | $(GREP) -v _TOTAL | $(XARGS) $(CAT) | $(SORT) -k 2`" \ |
ohair@494 | 96 | "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_TOTAL`" |
ohair@494 | 97 | endef |
ohair@494 | 98 | |
ohair@494 | 99 | define ResetAllTimers |
ohair@494 | 100 | $$(shell $(MKDIR) -p $(BUILDTIMESDIR) && $(RM) $(BUILDTIMESDIR)/build_time_*) |
ohair@494 | 101 | endef |
ohair@494 | 102 | |
ohair@494 | 103 | define StartGlobalTimer |
ohair@494 | 104 | $(call RecordStartTime,TOTAL) |
ohair@494 | 105 | endef |
ohair@494 | 106 | |
ohair@494 | 107 | define StopGlobalTimer |
ohair@494 | 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 |
ohair@494 | 126 | # Check if the current target is the last goal |
ohair@494 | 127 | $(if $(filter $@,$(call LastGoal)),$(call AtMakeEnd)) |
ohair@494 | 128 | # If the target is 'foo-only', check if our goal was stated as 'foo' |
erikj@559 | 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 |
ohair@494 | 134 | $(BUILD_LOG_WRAPPER) $(PRINTF) "## Starting $(patsubst %-only,%,$@)\n" |
ohair@494 | 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 |
ohair@494 | 140 | $(call RecordEndTime,$(patsubst %-only,%,$@)) |
ohair@494 | 141 | $(BUILD_LOG_WRAPPER) $(PRINTF) "## Finished $(patsubst %-only,%,$@) (build time %s)\n\n" \ |
tbell@514 | 142 | "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_$(patsubst %-only,%,$@) | $(CUT) -f 1 -d ' '`" |
ohair@494 | 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 |
ohair@494 | 148 | $(if $(findstring --jobserver,$(MAKEFLAGS)),$(error make -j is not supported, use make JOBS=n)) |
ohair@494 | 149 | $(call CheckEnvironment) |
ohair@494 | 150 | @$(PRINTF) $(LOG_INFO) "Running make as '$(MAKE) $(MFLAGS) $(MAKE_ARGS)'\n" |
ohair@494 | 151 | @$(PRINTF) "Building $(PRODUCT_NAME) for target '$(call GetRealTarget)' in configuration '$(CONF_NAME)'\n\n" |
ohair@494 | 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 |
tbell@507 | 157 | [ -f $(SJAVAC_SERVER_DIR)/server.port ] && echo Stopping sjavac server && $(TOUCH) $(SJAVAC_SERVER_DIR)/server.port.stop; true |
ohair@494 | 158 | $(call StopGlobalTimer) |
ohair@494 | 159 | $(call ReportBuildTimes) |
ohair@494 | 160 | @$(PRINTF) "Finished building $(PRODUCT_NAME) for target '$(call GetRealTarget)'\n" |
ohair@494 | 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 |
erikj@459 | 167 | # If the user specificed a "global" target (e.g. 'help'), do not exit but continue running |
ohair@494 | 168 | $$(if $$(filter-out $(global_targets),$$(call GetRealTarget)),$$(error Cannot continue)) |
erikj@459 | 169 | endef |
erikj@459 | 170 | |
erikj@459 | 171 | define ParseLogLevel |
erikj@459 | 172 | ifeq ($$(origin VERBOSE),undefined) |
erikj@459 | 173 | # Setup logging according to LOG (but only if VERBOSE is not given) |
tbell@507 | 174 | |
tbell@507 | 175 | # If the "nofile" argument is given, act on it and strip it away |
ohair@478 | 176 | ifneq ($$(findstring nofile,$$(LOG)),) |
ohair@478 | 177 | # Reset the build log wrapper, regardless of other values |
ohair@478 | 178 | override BUILD_LOG_WRAPPER= |
ohair@478 | 179 | # COMMA is defined in spec.gmk, but that is not included yet |
ohair@478 | 180 | COMMA=, |
ohair@478 | 181 | # First try to remove ",nofile" if it exists |
ohair@478 | 182 | LOG_STRIPPED1=$$(subst $$(COMMA)nofile,,$$(LOG)) |
ohair@478 | 183 | # Otherwise just remove "nofile" |
ohair@478 | 184 | LOG_STRIPPED2=$$(subst nofile,,$$(LOG_STRIPPED1)) |
ohair@478 | 185 | # We might have ended up with a leading comma. Remove it |
ohair@478 | 186 | LOG_STRIPPED3=$$(strip $$(patsubst $$(COMMA)%,%,$$(LOG_STRIPPED2))) |
ohair@478 | 187 | override LOG:=$$(LOG_STRIPPED3) |
ohair@478 | 188 | endif |
ohair@478 | 189 | |
erikj@459 | 190 | ifeq ($$(LOG),) |
erikj@459 | 191 | # Set LOG to "warn" as default if not set (and no VERBOSE given) |
ohair@478 | 192 | override LOG=warn |
erikj@459 | 193 | endif |
erikj@459 | 194 | ifeq ($$(LOG),warn) |
erikj@459 | 195 | VERBOSE=-s |
erikj@459 | 196 | else ifeq ($$(LOG),info) |
ohair@494 | 197 | VERBOSE=-s |
erikj@459 | 198 | else ifeq ($$(LOG),debug) |
erikj@459 | 199 | VERBOSE= |
erikj@459 | 200 | else ifeq ($$(LOG),trace) |
ohair@494 | 201 | VERBOSE= |
erikj@459 | 202 | else |
erikj@459 | 203 | $$(info Error: LOG must be one of: warn, info, debug or trace.) |
ohair@494 | 204 | $$(eval $$(call FatalError)) |
erikj@459 | 205 | endif |
erikj@459 | 206 | else |
erikj@459 | 207 | ifneq ($$(LOG),) |
erikj@459 | 208 | # We have both a VERBOSE and a LOG argument. This is OK only if this is a repeated call by ourselves, |
erikj@459 | 209 | # but complain if this is the top-level make call. |
erikj@459 | 210 | ifeq ($$(MAKELEVEL),0) |
erikj@459 | 211 | $$(info Cannot use LOG=$$(LOG) and VERBOSE=$$(VERBOSE) at the same time. Choose one.) |
ohair@494 | 212 | $$(eval $$(call FatalError)) |
erikj@459 | 213 | endif |
erikj@459 | 214 | endif |
erikj@459 | 215 | endif |
erikj@459 | 216 | endef |
erikj@459 | 217 | |
erikj@459 | 218 | define ParseConfAndSpec |
ohair@494 | 219 | ifneq ($$(filter-out $(global_targets),$$(call GetRealTarget)),) |
ohair@494 | 220 | # If we only have global targets, no need to bother with SPEC or CONF |
ohair@494 | 221 | ifneq ($$(origin SPEC),undefined) |
ohair@494 | 222 | # We have been given a SPEC, check that it works out properly |
ohair@494 | 223 | ifeq ($$(wildcard $$(SPEC)),) |
ohair@494 | 224 | $$(info Cannot locate spec.gmk, given by SPEC=$$(SPEC)) |
ohair@494 | 225 | $$(eval $$(call FatalError)) |
erikj@459 | 226 | endif |
ohair@494 | 227 | ifneq ($$(origin CONF),undefined) |
ohair@494 | 228 | # We also have a CONF argument. This is OK only if this is a repeated call by ourselves, |
ohair@494 | 229 | # but complain if this is the top-level make call. |
ohair@494 | 230 | ifeq ($$(MAKELEVEL),0) |
ohair@494 | 231 | $$(info Cannot use CONF=$$(CONF) and SPEC=$$(SPEC) at the same time. Choose one.) |
ohair@494 | 232 | $$(eval $$(call FatalError)) |
erikj@459 | 233 | endif |
erikj@459 | 234 | endif |
ohair@494 | 235 | # ... OK, we're satisfied, we'll use this SPEC later on |
ohair@494 | 236 | else |
ohair@494 | 237 | # Find all spec.gmk files in the build output directory |
ohair@494 | 238 | output_dir=$$(root_dir)/build |
ohair@494 | 239 | all_spec_files=$$(wildcard $$(output_dir)/*/spec.gmk) |
ohair@494 | 240 | ifeq ($$(all_spec_files),) |
ohair@494 | 241 | $$(info No configurations found for $$(root_dir)! Please run configure to create a configuration.) |
ohair@494 | 242 | $$(eval $$(call FatalError)) |
ohair@494 | 243 | endif |
ohair@494 | 244 | # Extract the configuration names from the path |
ohair@494 | 245 | all_confs=$$(patsubst %/spec.gmk,%,$$(patsubst $$(output_dir)/%,%,$$(all_spec_files))) |
erikj@459 | 246 | |
ohair@494 | 247 | ifneq ($$(origin CONF),undefined) |
ohair@494 | 248 | # User have given a CONF= argument. |
ohair@494 | 249 | ifeq ($$(CONF),) |
ohair@494 | 250 | # If given CONF=, match all configurations |
ohair@494 | 251 | matching_confs=$$(strip $$(all_confs)) |
ohair@494 | 252 | else |
ohair@494 | 253 | # Otherwise select those that contain the given CONF string |
ohair@494 | 254 | matching_confs=$$(strip $$(foreach var,$$(all_confs),$$(if $$(findstring $$(CONF),$$(var)),$$(var)))) |
ohair@494 | 255 | endif |
ohair@494 | 256 | ifeq ($$(matching_confs),) |
ohair@494 | 257 | $$(info No configurations found matching CONF=$$(CONF)) |
ohair@494 | 258 | $$(info Available configurations:) |
ohair@494 | 259 | $$(foreach var,$$(all_confs),$$(info * $$(var))) |
ohair@494 | 260 | $$(eval $$(call FatalError)) |
ohair@494 | 261 | else |
ohair@494 | 262 | ifeq ($$(words $$(matching_confs)),1) |
ohair@494 | 263 | $$(info Building '$$(matching_confs)' (matching CONF=$$(CONF))) |
ohair@494 | 264 | else |
ohair@494 | 265 | $$(info Building target '$(call GetRealTarget)' in the following configurations (matching CONF=$$(CONF)):) |
ohair@494 | 266 | $$(foreach var,$$(matching_confs),$$(info * $$(var))) |
ohair@494 | 267 | endif |
ohair@494 | 268 | endif |
ohair@494 | 269 | |
ohair@494 | 270 | # Create a SPEC definition. This will contain the path to one or more spec.gmk files. |
ohair@494 | 271 | SPEC=$$(addsuffix /spec.gmk,$$(addprefix $$(output_dir)/,$$(matching_confs))) |
ohair@494 | 272 | else |
ohair@494 | 273 | # No CONF or SPEC given, check the available configurations |
ohair@494 | 274 | ifneq ($$(words $$(all_spec_files)),1) |
ohair@494 | 275 | $$(info No CONF given, but more than one configuration found in $$(output_dir).) |
ohair@494 | 276 | $$(info Available configurations:) |
ohair@494 | 277 | $$(foreach var,$$(all_confs),$$(info * $$(var))) |
ohair@494 | 278 | $$(info Please retry building with CONF=<config pattern> (or SPEC=<specfile>)) |
ohair@494 | 279 | $$(eval $$(call FatalError)) |
ohair@494 | 280 | endif |
ohair@494 | 281 | |
ohair@494 | 282 | # We found exactly one configuration, use it |
ohair@494 | 283 | SPEC=$$(strip $$(all_spec_files)) |
erikj@459 | 284 | endif |
erikj@459 | 285 | endif |
erikj@459 | 286 | endif |
erikj@459 | 287 | endef |
erikj@459 | 288 | |
ohair@494 | 289 | ### Convenience functions from Main.gmk |
erikj@459 | 290 | |
ohair@478 | 291 | # Cleans the component given as $1 |
ohair@478 | 292 | define CleanComponent |
ohair@478 | 293 | @$(PRINTF) "Cleaning $1 build artifacts ..." |
ohair@478 | 294 | @($(CD) $(OUTPUT_ROOT) && $(RM) -r $1) |
ohair@478 | 295 | @$(PRINTF) " done\n" |
ohair@478 | 296 | endef |
ohair@478 | 297 | |
erikj@459 | 298 | endif # _MAKEHELPERS_GMK |