MakeBase.gmk revision 1443:e101d3d68578
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# 28# Setup common utility functions. 29# 30################################################################ 31 32ifndef _MAKEBASE_GMK 33_MAKEBASE_GMK := 1 34 35ifeq ($(wildcard $(SPEC)),) 36 $(error MakeBase.gmk needs SPEC set to a proper spec.gmk) 37endif 38 39# By defining this pseudo target, make will automatically remove targets 40# if their recipe fails so that a rebuild is automatically triggered on the 41# next make invocation. 42.DELETE_ON_ERROR: 43 44############################## 45# Functions 46############################## 47 48 49### Functions for timers 50 51# Store the build times in this directory. 52BUILDTIMESDIR=$(OUTPUT_ROOT)/make-support/build-times 53 54# Record starting time for build of a sub repository. 55define RecordStartTime 56 $(MKDIR) -p $(BUILDTIMESDIR) 57 $(DATE) '+%Y %m %d %H %M %S' | $(NAWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_start_$(strip $1) 58 $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$(strip $1)_human_readable 59endef 60 61# Record ending time and calculate the difference and store it in a 62# easy to read format. Handles builds that cross midnight. Expects 63# that a build will never take 24 hours or more. 64define RecordEndTime 65 $(DATE) '+%Y %m %d %H %M %S' | $(NAWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_end_$(strip $1) 66 $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)_human_readable 67 $(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$(strip $1)` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$(strip $1)` $1 | \ 68 $(NAWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \ 69 M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \ 70 > $(BUILDTIMESDIR)/build_time_diff_$(strip $1) 71endef 72 73# Hook to be called when starting to execute a top-level target 74define TargetEnter 75 $(PRINTF) "## Starting $(patsubst %-only,%,$@)\n" 76 $(call RecordStartTime,$(patsubst %-only,%,$@)) 77endef 78 79# Hook to be called when finish executing a top-level target 80define TargetExit 81 $(call RecordEndTime,$(patsubst %-only,%,$@)) 82 $(PRINTF) "## Finished $(patsubst %-only,%,$@) (build time %s)\n\n" \ 83 "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_$(patsubst %-only,%,$@) | $(CUT) -f 1 -d ' '`" 84endef 85 86################################################################################ 87# This macro translates $ into \$ to protect the $ from expansion in the shell. 88# To make this macro resilient against already escaped strings, first remove 89# any present escapes before escaping so that no double escapes are added. 90EscapeDollar = $(subst $$,\$$,$(subst \$$,$$,$(strip $1))) 91 92################################################################################ 93# This macro translates $ into $$ to protect the string from make itself. 94DoubleDollar = $(subst $$,$$$$,$(strip $1)) 95 96################################################################################ 97 98# If the variable that you want to send to stdout for piping into a file or otherwise, 99# is potentially long, for example the a list of file paths, eg a list of all package directories. 100# Then you need to use ListPathsSafely, which optimistically splits the output into several shell 101# calls as well as use compression on recurrent file paths segments, to get around the potential 102# command line length problem that exists in cygwin and other shells. 103compress_pre:=$(strip $(shell $(CAT) $(SRC_ROOT)/make/common/support/ListPathsSafely-pre-compress.incl)) 104compress_post:=$(strip $(shell $(CAT) $(SRC_ROOT)/make/common/support/ListPathsSafely-post-compress.incl)) 105compress_paths=$(compress_pre)\ 106$(subst $(SRC_ROOT),X97,\ 107$(subst $(OUTPUT_ROOT),X98,\ 108$(subst X,X00,\ 109$(subst $(SPACE),\n,$(strip $1)))))\ 110$(compress_post) 111 112decompress_paths=$(SED) -f $(SRC_ROOT)/make/common/support/ListPathsSafely-uncompress.sed -e 's|X99|\\n|g' \ 113 -e 's|X98|$(OUTPUT_ROOT)|g' -e 's|X97|$(SRC_ROOT)|g' \ 114 -e 's|X00|X|g' | tr '\n' '$2' 115 116define ListPathsSafely_If 117 $(if $(word $3,$($1)),$(eval $1_LPS$3:=$(call compress_paths,$(wordlist $3,$4,$($1))))) 118endef 119 120define ListPathsSafely_Printf 121 $(if $(strip $($1_LPS$4)),$(if $(findstring $(LOG_LEVEL),trace),,@)printf \ 122 -- "$(strip $(call EscapeDollar, $($1_LPS$4)))\n" | $(decompress_paths) $3) 123endef 124 125# Receipt example: 126# rm -f thepaths 127# $(call ListPathsSafely,THEPATHS,\n, >> thepaths) 128# The \n argument means translate spaces into \n 129# if instead , , (a space) is supplied, then spaces remain spaces. 130define ListPathsSafely 131 $(if $(word 16001,$($1)),$(error Cannot list safely more than 16000 paths. $1 has $(words $($1)) paths!)) 132 $(ECHO) $(LOG_DEBUG) Writing $(words $($1)) paths to '$3' 133 $(call ListPathsSafely_If,$1,$2,1,250) 134 $(call ListPathsSafely_If,$1,$2,251,500) 135 $(call ListPathsSafely_If,$1,$2,501,750) 136 $(call ListPathsSafely_If,$1,$2,751,1000) 137 138 $(call ListPathsSafely_If,$1,$2,1001,1250) 139 $(call ListPathsSafely_If,$1,$2,1251,1500) 140 $(call ListPathsSafely_If,$1,$2,1501,1750) 141 $(call ListPathsSafely_If,$1,$2,1751,2000) 142 143 $(call ListPathsSafely_If,$1,$2,2001,2250) 144 $(call ListPathsSafely_If,$1,$2,2251,2500) 145 $(call ListPathsSafely_If,$1,$2,2501,2750) 146 $(call ListPathsSafely_If,$1,$2,2751,3000) 147 148 $(call ListPathsSafely_If,$1,$2,3001,3250) 149 $(call ListPathsSafely_If,$1,$2,3251,3500) 150 $(call ListPathsSafely_If,$1,$2,3501,3750) 151 $(call ListPathsSafely_If,$1,$2,3751,4000) 152 153 $(call ListPathsSafely_If,$1,$2,4001,4250) 154 $(call ListPathsSafely_If,$1,$2,4251,4500) 155 $(call ListPathsSafely_If,$1,$2,4501,4750) 156 $(call ListPathsSafely_If,$1,$2,4751,5000) 157 158 $(call ListPathsSafely_If,$1,$2,5001,5250) 159 $(call ListPathsSafely_If,$1,$2,5251,5500) 160 $(call ListPathsSafely_If,$1,$2,5501,5750) 161 $(call ListPathsSafely_If,$1,$2,5751,6000) 162 163 $(call ListPathsSafely_If,$1,$2,6001,6250) 164 $(call ListPathsSafely_If,$1,$2,6251,6500) 165 $(call ListPathsSafely_If,$1,$2,6501,6750) 166 $(call ListPathsSafely_If,$1,$2,6751,7000) 167 168 $(call ListPathsSafely_If,$1,$2,7001,7250) 169 $(call ListPathsSafely_If,$1,$2,7251,7500) 170 $(call ListPathsSafely_If,$1,$2,7501,7750) 171 $(call ListPathsSafely_If,$1,$2,7751,8000) 172 173 $(call ListPathsSafely_If,$1,$2,8001,8250) 174 $(call ListPathsSafely_If,$1,$2,8251,8500) 175 $(call ListPathsSafely_If,$1,$2,8501,8750) 176 $(call ListPathsSafely_If,$1,$2,8751,9000) 177 178 $(call ListPathsSafely_If,$1,$2,9001,9250) 179 $(call ListPathsSafely_If,$1,$2,9251,9500) 180 $(call ListPathsSafely_If,$1,$2,9501,9750) 181 $(call ListPathsSafely_If,$1,$2,9751,10000) 182 183 $(call ListPathsSafely_If,$1,$2,10001,10250) 184 $(call ListPathsSafely_If,$1,$2,10251,10500) 185 $(call ListPathsSafely_If,$1,$2,10501,10750) 186 $(call ListPathsSafely_If,$1,$2,10751,11000) 187 188 $(call ListPathsSafely_If,$1,$2,11001,11250) 189 $(call ListPathsSafely_If,$1,$2,11251,11500) 190 $(call ListPathsSafely_If,$1,$2,11501,11750) 191 $(call ListPathsSafely_If,$1,$2,11751,12000) 192 193 $(call ListPathsSafely_If,$1,$2,12001,12250) 194 $(call ListPathsSafely_If,$1,$2,12251,12500) 195 $(call ListPathsSafely_If,$1,$2,12501,12750) 196 $(call ListPathsSafely_If,$1,$2,12751,13000) 197 198 $(call ListPathsSafely_If,$1,$2,13001,13250) 199 $(call ListPathsSafely_If,$1,$2,13251,13500) 200 $(call ListPathsSafely_If,$1,$2,13501,13750) 201 $(call ListPathsSafely_If,$1,$2,13751,14000) 202 203 $(call ListPathsSafely_If,$1,$2,14001,14250) 204 $(call ListPathsSafely_If,$1,$2,14251,14500) 205 $(call ListPathsSafely_If,$1,$2,14501,14750) 206 $(call ListPathsSafely_If,$1,$2,14751,15000) 207 208 $(call ListPathsSafely_If,$1,$2,15001,15250) 209 $(call ListPathsSafely_If,$1,$2,15251,15500) 210 $(call ListPathsSafely_If,$1,$2,15501,15750) 211 $(call ListPathsSafely_If,$1,$2,15751,16000) 212 213 $(call ListPathsSafely_Printf,$1,$2,$3,1) 214 $(call ListPathsSafely_Printf,$1,$2,$3,251) 215 $(call ListPathsSafely_Printf,$1,$2,$3,501) 216 $(call ListPathsSafely_Printf,$1,$2,$3,751) 217 218 $(call ListPathsSafely_Printf,$1,$2,$3,1001) 219 $(call ListPathsSafely_Printf,$1,$2,$3,1251) 220 $(call ListPathsSafely_Printf,$1,$2,$3,1501) 221 $(call ListPathsSafely_Printf,$1,$2,$3,1751) 222 223 $(call ListPathsSafely_Printf,$1,$2,$3,2001) 224 $(call ListPathsSafely_Printf,$1,$2,$3,2251) 225 $(call ListPathsSafely_Printf,$1,$2,$3,2501) 226 $(call ListPathsSafely_Printf,$1,$2,$3,2751) 227 228 $(call ListPathsSafely_Printf,$1,$2,$3,3001) 229 $(call ListPathsSafely_Printf,$1,$2,$3,3251) 230 $(call ListPathsSafely_Printf,$1,$2,$3,3501) 231 $(call ListPathsSafely_Printf,$1,$2,$3,3751) 232 233 $(call ListPathsSafely_Printf,$1,$2,$3,4001) 234 $(call ListPathsSafely_Printf,$1,$2,$3,4251) 235 $(call ListPathsSafely_Printf,$1,$2,$3,4501) 236 $(call ListPathsSafely_Printf,$1,$2,$3,4751) 237 238 $(call ListPathsSafely_Printf,$1,$2,$3,5001) 239 $(call ListPathsSafely_Printf,$1,$2,$3,5251) 240 $(call ListPathsSafely_Printf,$1,$2,$3,5501) 241 $(call ListPathsSafely_Printf,$1,$2,$3,5751) 242 243 $(call ListPathsSafely_Printf,$1,$2,$3,6001) 244 $(call ListPathsSafely_Printf,$1,$2,$3,6251) 245 $(call ListPathsSafely_Printf,$1,$2,$3,6501) 246 $(call ListPathsSafely_Printf,$1,$2,$3,6751) 247 248 $(call ListPathsSafely_Printf,$1,$2,$3,7001) 249 $(call ListPathsSafely_Printf,$1,$2,$3,7251) 250 $(call ListPathsSafely_Printf,$1,$2,$3,7501) 251 $(call ListPathsSafely_Printf,$1,$2,$3,7751) 252 253 $(call ListPathsSafely_Printf,$1,$2,$3,8001) 254 $(call ListPathsSafely_Printf,$1,$2,$3,8251) 255 $(call ListPathsSafely_Printf,$1,$2,$3,8501) 256 $(call ListPathsSafely_Printf,$1,$2,$3,8751) 257 258 $(call ListPathsSafely_Printf,$1,$2,$3,9001) 259 $(call ListPathsSafely_Printf,$1,$2,$3,9251) 260 $(call ListPathsSafely_Printf,$1,$2,$3,9501) 261 $(call ListPathsSafely_Printf,$1,$2,$3,9751) 262 263 $(call ListPathsSafely_Printf,$1,$2,$3,10001) 264 $(call ListPathsSafely_Printf,$1,$2,$3,10251) 265 $(call ListPathsSafely_Printf,$1,$2,$3,10501) 266 $(call ListPathsSafely_Printf,$1,$2,$3,10751) 267 268 $(call ListPathsSafely_Printf,$1,$2,$3,11001) 269 $(call ListPathsSafely_Printf,$1,$2,$3,11251) 270 $(call ListPathsSafely_Printf,$1,$2,$3,11501) 271 $(call ListPathsSafely_Printf,$1,$2,$3,11751) 272 273 $(call ListPathsSafely_Printf,$1,$2,$3,12001) 274 $(call ListPathsSafely_Printf,$1,$2,$3,12251) 275 $(call ListPathsSafely_Printf,$1,$2,$3,12501) 276 $(call ListPathsSafely_Printf,$1,$2,$3,12751) 277 278 $(call ListPathsSafely_Printf,$1,$2,$3,13001) 279 $(call ListPathsSafely_Printf,$1,$2,$3,13251) 280 $(call ListPathsSafely_Printf,$1,$2,$3,13501) 281 $(call ListPathsSafely_Printf,$1,$2,$3,13751) 282 283 $(call ListPathsSafely_Printf,$1,$2,$3,14001) 284 $(call ListPathsSafely_Printf,$1,$2,$3,14251) 285 $(call ListPathsSafely_Printf,$1,$2,$3,14501) 286 $(call ListPathsSafely_Printf,$1,$2,$3,14751) 287 288 $(call ListPathsSafely_Printf,$1,$2,$3,15001) 289 $(call ListPathsSafely_Printf,$1,$2,$3,15251) 290 $(call ListPathsSafely_Printf,$1,$2,$3,15501) 291 $(call ListPathsSafely_Printf,$1,$2,$3,15751) 292endef 293 294define ListPathsSafelyNow_IfPrintf 295 ifneq (,$$(word $4,$$($1))) 296 $$(eval $1_LPS$4:=$$(call compress_paths,$$(wordlist $4,$5,$$($1)))) 297 $$(shell printf -- "$$(strip $$($1_LPS$4))\n" | $(decompress_paths) $3) 298 endif 299endef 300 301# And an non-receipt version: 302define ListPathsSafelyNow 303 ifneq (,$$(word 10001,$$($1))) 304 $$(error Cannot list safely more than 10000 paths. $1 has $$(words $$($1)) paths!) 305 endif 306 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,1,250) 307 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,251,500) 308 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,501,750) 309 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,751,1000) 310 311 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,1001,1250) 312 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,1251,1500) 313 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,1501,1750) 314 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,1751,2000) 315 316 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,2001,2250) 317 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,2251,2500) 318 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,2501,2750) 319 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,2751,3000) 320 321 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,3001,3250) 322 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,3251,3500) 323 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,3501,3750) 324 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,3751,4000) 325 326 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,4001,4250) 327 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,4251,4500) 328 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,4501,4750) 329 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,4751,5000) 330 331 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,5001,5250) 332 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,5251,5500) 333 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,5501,5750) 334 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,5751,6000) 335 336 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,6001,6250) 337 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,6251,6500) 338 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,6501,6750) 339 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,6751,7000) 340 341 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,7001,7250) 342 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,7251,7500) 343 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,7501,7750) 344 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,7751,8000) 345 346 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,8001,8250) 347 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,8251,8500) 348 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,8501,8750) 349 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,8751,9000) 350 351 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,9001,9250) 352 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,9251,9500) 353 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,9501,9750) 354 $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,9751,10000) 355 356endef 357 358# The source tips can come from the Mercurial repository, or in the files 359# $(HGTIP_FILENAME) which contains the tip but is also positioned in the same 360# directory as the original $(HGDIR) directory. 361# These should not be := assignments, only used from the root Makefile. 362HG_VERSION = $(shell $(HG) version 2> /dev/null) 363HG_DIRECTORY=.hg 364HGTIP_FILENAME=.hgtip 365HG_SEARCH = ./REPO ./*/REPO ./*/*/REPO ./*/*/*/REPO 366REPO_LIST = $(patsubst ./%,%,$(patsubst %/,%,$(sort $(dir \ 367 $(shell $(CD) $(SRC_ROOT) ; ( $(LS) -d $(HG_SEARCH:%/REPO=%/$(HG_DIRECTORY)) ; \ 368 $(LS) $(HG_SEARCH:%/REPO=%/$(HGTIP_FILENAME)) ) \ 369 2> /dev/null))))) 370 371# Emit the repo:tip pairs to $@ 372define GetSourceTips 373 $(CD) $(SRC_ROOT) ; \ 374 for i in $(REPO_LIST) IGNORE ; do \ 375 if [ "$${i}" = "IGNORE" ] ; then \ 376 continue; \ 377 elif [ -d $${i}/$(HG_DIRECTORY) -a "$(HG_VERSION)" != "" ] ; then \ 378 $(PRINTF) " %s:%s" \ 379 "$${i}" `$(HG) tip --repository $${i} --template '{node|short}\n'` ; \ 380 elif [ -f $${i}/$(HGTIP_FILENAME) ] ; then \ 381 $(PRINTF) " %s:%s" \ 382 "$${i}" `$(CAT) $${i}/$(HGTIP_FILENAME)` ; \ 383 fi; \ 384 done >> $@ 385 $(PRINTF) "\n" >> $@ 386endef 387 388# Create the HGTIP_FILENAME file. Called from jdk/make/closed/bundles.gmk 389define CreateHgTip 390 $(HG) tip --repository $1 --template '{node|short}\n' > $1/$(HGTIP_FILENAME); \ 391 $(ECHO) $1/$(HGTIP_FILENAME) 392endef 393 394define SetupLogging 395 ifeq ($$(LOG_LEVEL), trace) 396 # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make 397 # For each target executed, will print 398 # Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer) 399 # but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much 400 # (and causing a crash on Cygwin). 401 # Default shell seems to always be /bin/sh. Must override with bash to get this to work on Solaris. 402 # Only use time if it's GNU time which supports format and output file. 403 WRAPPER_SHELL := $$(BASH) $$(SRC_ROOT)/common/bin/shell-tracer.sh $$(if $$(findstring yes,$$(IS_GNU_TIME)),$$(TIME),-) $$(OUTPUT_ROOT)/build-trace-time.log $$(SHELL) 404 SHELL := $$(warning $$(if $$@,Building $$@,Running shell command) $$(if $$<, (from $$<))$$(if $$?, ($$(wordlist 1, 20, $$?) $$(if $$(wordlist 21, 22, $$?), ... [in total $$(words $$?) files]) newer)))$$(WRAPPER_SHELL) 405 endif 406 # Never remove warning messages; this is just for completeness 407 LOG_WARN := 408 ifneq ($$(findstring $$(LOG_LEVEL), info debug trace),) 409 LOG_INFO := 410 else 411 LOG_INFO := > /dev/null 412 endif 413 ifneq ($$(findstring $$(LOG_LEVEL), debug trace),) 414 LOG_DEBUG := 415 else 416 LOG_DEBUG := > /dev/null 417 endif 418 ifneq ($$(findstring $$(LOG_LEVEL), trace),) 419 LOG_TRACE := 420 else 421 LOG_TRACE := > /dev/null 422 endif 423endef 424 425# Make sure logging is setup for everyone that includes MakeBase.gmk. 426$(eval $(call SetupLogging)) 427 428################################################################################ 429# Creates a sequence of increasing numbers (inclusive). 430# Param 1 - starting number 431# Param 2 - ending number 432sequence = \ 433 $(wordlist $1, $2, $(strip \ 434 $(eval SEQUENCE_COUNT :=) \ 435 $(call _sequence-do,$(strip $2)))) 436 437_sequence-do = \ 438 $(if $(word $1, $(SEQUENCE_COUNT)),, \ 439 $(eval SEQUENCE_COUNT += .) \ 440 $(words $(SEQUENCE_COUNT)) \ 441 $(call _sequence-do,$1)) 442 443################################################################################ 444 445MAX_PARAMS := 30 446PARAM_SEQUENCE := $(call sequence, 2, $(MAX_PARAMS)) 447 448# Template for creating a macro taking named parameters. To use it, assign the 449# template to a variable with the name you want for your macro, using '=' 450# assignment. Then define a macro body with the suffix "Body". The Body macro 451# should take 1 parameter which should be a unique string for that invocation 452# of the macro. 453# Ex: 454# SetupFoo = $(NamedParamsMacroTemplate) 455# define SetupFooBody 456# # do something 457# # access parameters as $$($1_BAR) 458# endef 459# Call it like this 460# $(eval $(call SetupFoo, BUILD_SOMETHING, \ 461# BAR := some parameter value, \ 462# )) 463define NamedParamsMacroTemplate 464 $(if $($(MAX_PARAMS)),$(error Internal makefile error: \ 465 Too many named arguments to macro, please update MAX_PARAMS in MakeBase.gmk)) 466 # Iterate over 2 3 4... and evaluate the named parameters with $1_ as prefix 467 $(foreach i,$(PARAM_SEQUENCE), $(if $(strip $($i)),\ 468 $(strip $1)_$(strip $($i)))$(NEWLINE)) 469 # Debug print all named parameter names and values 470 $(if $(findstring $(LOG_LEVEL),debug trace), \ 471 $(info $0 $(strip $1) $(foreach i,$(PARAM_SEQUENCE), \ 472 $(if $(strip $($i)),$(NEWLINE) $(strip [$i] $(if $(filter $(LOG_LEVEL), trace), \ 473 $($i), $(wordlist 1, 20, $($(i))) $(if $(word 21, $($(i))), ...))))))) 474 475 $(if $(DEBUG_$(strip $1)), 476 $(info -------- <<< Begin expansion of $(strip $1)) \ 477 $(info $(call $(0)Body,$(strip $1))) \ 478 $(info -------- >>> End expansion of $(strip $1)) \ 479 ) 480 481 $(call $(0)Body,$(strip $1)) 482endef 483 484################################################################################ 485# Make directory without forking mkdir if not needed 486MakeDir = \ 487 $(strip $(if $(subst $(wildcard $1 $2 $3 $4 $5 $6 $7 $8 $9),,$(strip $1 $2 $3 $4 $5 $6 $7 $8 $9)),\ 488 $(shell $(MKDIR) -p $1 $2 $3 $4 $5 $6 $7 $8 $9))) 489 490################################################################################ 491 492ifeq ($(OPENJDK_TARGET_OS),solaris) 493 # On Solaris, if the target is a symlink and exists, cp won't overwrite. 494 # Cp has to operate in recursive mode to allow for -P flag, to preserve soft links. If the 495 # name of the target file differs from the source file, rename after copy. 496 # If the source and target parent directories are the same, recursive copy doesn't work 497 # so we fall back on regular copy, which isn't preserving symlinks. 498 define install-file 499 $(MKDIR) -p '$(@D)' 500 $(RM) '$@' 501 if [ "$(@D)" != "$(<D)" ]; then \ 502 $(CP) -f -r -P '$<' '$(@D)'; \ 503 if [ "$(@F)" != "$(<F)" ]; then \ 504 $(MV) '$(@D)/$(<F)' '$@'; \ 505 fi; \ 506 else \ 507 if [ -L '$<' ]; then \ 508 $(ECHO) "Source file is a symlink and target is in the same directory: $< $@" ; \ 509 exit 1; \ 510 fi; \ 511 $(CP) -f '$<' '$@'; \ 512 fi 513 endef 514else ifeq ($(OPENJDK_TARGET_OS),macosx) 515 # On mac, extended attributes sometimes creep into the source files, which may later 516 # cause the creation of ._* files which confuses testing. Clear these with xattr if 517 # set. Some files get their write permissions removed after being copied to the 518 # output dir. When these are copied again to images, xattr would fail. By only clearing 519 # attributes when they are present, failing on this is avoided. 520 define install-file 521 $(MKDIR) -p '$(@D)' 522 $(CP) -fRP '$<' '$@' 523 if [ -n "`$(XATTR) -l '$@'`" ]; then $(XATTR) -c '$@'; fi 524 endef 525else 526 # Running mkdir and cp in the same shell speeds up copy intensive tasks in Cygwin 527 # significantly. 528 define install-file 529 $(MKDIR) -p '$(@D)' && $(CP) -fP '$<' '$@' 530 endef 531endif 532 533################################################################################ 534# Convenience functions for working around make's limitations with $(filter ). 535containing = \ 536 $(strip $(foreach v,$(strip $2),$(if $(findstring $(strip $1),$v),$v))) 537not-containing = \ 538 $(strip $(foreach v,$(strip $2),$(if $(findstring $(strip $1),$v),,$v))) 539 540# Filter out duplicate sub strings while preserving order. Keeps the first occurance. 541uniq = \ 542 $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1))) 543 544# Return a list of all string elements that are duplicated in $1. 545dups = \ 546 $(strip $(foreach v, $(sort $1), $(if $(filter-out 1, \ 547 $(words $(filter $v, $1))), $v))) 548 549# String equals 550equals = \ 551 $(and $(findstring $(strip $1),$(strip $2)),\ 552 $(findstring $(strip $2),$(strip $1))) 553 554################################################################################ 555 556ifneq ($(DISABLE_CACHE_FIND), true) 557 # In Cygwin, finds are very costly, both because of expensive forks and because 558 # of bad file system caching. Find is used extensively in $(shell) commands to 559 # find source files. This makes rerunning make with no or few changes rather 560 # expensive. To speed this up, these two macros are used to cache the results 561 # of simple find commands for reuse. 562 # 563 # Runs a find and stores both the directories where it was run and the results. 564 # This macro can be called multiple times to add to the cache. Only finds files 565 # with no filters. 566 # 567 # Needs to be called with $(eval ) 568 # 569 # Even if the performance benifit is negligible on other platforms, keep the 570 # functionality active unless explicitly disabled to exercise it more. 571 # 572 # Initialize FIND_CACHE_DIRS with := to make it a non recursively-expanded variable 573 FIND_CACHE_DIRS := 574 # Param 1 - Dirs to find in 575 # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression. 576 define FillCacheFind 577 # Filter out already cached dirs. The - is needed when FIND_CACHE_DIRS is empty 578 # since filter out will then return empty. 579 FIND_CACHE_NEW_DIRS := $$(filter-out $$(addsuffix /%,\ 580 - $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS), $1) 581 ifneq ($$(FIND_CACHE_NEW_DIRS), ) 582 # Remove any trailing slash from dirs in the cache dir list 583 FIND_CACHE_DIRS += $$(patsubst %/,%, $$(FIND_CACHE_NEW_DIRS)) 584 FIND_CACHE := $$(sort $$(FIND_CACHE) $$(shell $(FIND) $$(FIND_CACHE_NEW_DIRS) \( -type f -o -type l \) $2)) 585 endif 586 endef 587 588 # Mimics find by looking in the cache if all of the directories have been cached. 589 # Otherwise reverts to shell find. This is safe to call on all platforms, even if 590 # cache is deactivated. 591 # 592 # $1 can be either a directory or a file. If it's a directory, make 593 # sure we have exactly one trailing slash before the wildcard. 594 # The extra - is needed when FIND_CACHE_DIRS is empty but should be harmless. 595 # 596 # Param 1 - Dirs to find in 597 # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression. 598 define CacheFind 599 $(if $(filter-out $(addsuffix /%,- $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS),$1), \ 600 $(shell $(FIND) $1 \( -type f -o -type l \) $2), \ 601 $(filter $(addsuffix /%,$(patsubst %/,%,$1)) $1,$(FIND_CACHE))) 602 endef 603 604else 605 # If CacheFind is disabled, just run the find command. 606 # Param 1 - Dirs to find in 607 # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression. 608 define CacheFind 609 $(shell $(FIND) $1 \( -type f -o -type l \) $2) 610 endef 611endif 612 613################################################################################ 614 615define AddFileToCopy 616 # Helper macro for SetupCopyFiles 617 # 1 : Source file 618 # 2 : Dest file 619 # 3 : Variable to add targets to 620 # 4 : Macro to call for copy operation 621 $2: $1 622 $(ECHO) $(LOG_INFO) Copying $$(patsubst $(OUTPUT_ROOT)/%,%,$$@) 623 $$($$(strip $4)) 624 625 $3 += $2 626endef 627 628# Setup make rules for copying files, with an option to do more complex 629# processing instead of copying. 630# 631# Parameter 1 is the name of the rule. This name is used as variable prefix, 632# and the targets generated are listed in a variable by that name. 633# 634# Remaining parameters are named arguments. These include: 635# SRC : Source root dir (defaults to dir of first file) 636# DEST : Dest root dir 637# FILES : List of files to copy with absolute paths, or path relative to SRC. 638# Must be in SRC. 639# FLATTEN : Set to flatten the directory structure in the DEST dir. 640# MACRO : Optionally override the default macro used for making the copy. 641# Default is 'install-file' 642SetupCopyFiles = $(NamedParamsMacroTemplate) 643define SetupCopyFilesBody 644 645 ifeq ($$($1_MACRO), ) 646 $1_MACRO := install-file 647 endif 648 649 # Default SRC to the dir of the first file. 650 ifeq ($$($1_SRC), ) 651 $1_SRC := $$(dir $$(firstword $$($1_FILES))) 652 endif 653 654 # Remove any trailing slash from SRC 655 $1_SRC := $$(patsubst %/,%,$$($1_SRC)) 656 657 $$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \ 658 $$(eval $$(call AddFileToCopy, $$($1_SRC)/$$f, \ 659 $$($1_DEST)/$$(if $$($1_FLATTEN),$$(notdir $$f),$$f), $1, $$($1_MACRO)))) 660 661endef 662 663################################################################################ 664# ShellQuote 665# 666# Quotes a string with single quotes and replaces single quotes with '\'' so 667# that the contents survives being given to the shell. 668 669ShellQuote = \ 670 $(SQUOTE)$(subst $(SQUOTE),$(SQUOTE)\$(SQUOTE)$(SQUOTE),$(strip $1))$(SQUOTE) 671 672################################################################################ 673# Write to and read from file 674 675# Param 1 - File to read 676ReadFile = \ 677 $(shell $(CAT) $1) 678 679# Param 1 - Text to write 680# Param 2 - File to write to 681# Use printf to get consistent behavior on all platforms. 682WriteFile = \ 683 $(shell $(PRINTF) "%s" $(call ShellQuote, $1) > $2) 684 685################################################################################ 686# DependOnVariable 687# 688# This macro takes a variable name and puts the value in a file only if the 689# value has changed since last. The name of the file is returned. This can be 690# used to create rule dependencies on make variable values. The following 691# example would get rebuilt if the value of SOME_VAR was changed: 692# 693# path/to/some-file: $(call DependOnVariable, SOME_VAR) 694# echo $(SOME_VAR) > $@ 695# 696# Note that leading and trailing white space in the value is ignored. 697# 698 699# Defines the sub directory structure to store variable value file in 700DependOnVariableDirName = \ 701 $(strip $(subst $(SRC_ROOT)/,,\ 702 $(if $(filter /%, $(firstword $(MAKEFILE_LIST))), \ 703 $(firstword $(MAKEFILE_LIST)), \ 704 $(CURDIR)/$(firstword $(MAKEFILE_LIST))))) 705 706# Defines the name of the file to store variable value in. Generates a name 707# unless parameter 2 is given. 708# Param 1 - Name of variable 709# Param 2 - (optional) name of file to store value in 710DependOnVariableFileName = \ 711 $(strip $(if $(strip $2), $2, \ 712 $(MAKESUPPORT_OUTPUTDIR)/vardeps/$(DependOnVariableDirName)/$(strip $1).vardeps)) 713 714# Does the actual work with parameters stripped. 715# If the file exists AND the contents is the same as the variable, do nothing 716# else print a new file. 717# Always returns the name of the file where the value was printed. 718# Param 1 - Name of variable 719# Param 2 - (optional) name of file to store value in 720DependOnVariableHelper = \ 721 $(strip $(if $(and $(wildcard $(call DependOnVariableFileName, $1, $2)),\ 722 $(call equals, $(strip $($1)), \ 723 $(call ReadFile, $(call DependOnVariableFileName, $1, $2)))),,\ 724 $(call MakeDir, $(dir $(call DependOnVariableFileName, $1, $2))) \ 725 $(if $(findstring $(LOG_LEVEL), trace), \ 726 $(info Variable $1: >$(strip $($1))<) \ 727 $(info File: >$(call ReadFile, $(call DependOnVariableFileName, $1, $2))<)) \ 728 $(call WriteFile, $($1), $(call DependOnVariableFileName, $1, $2))) \ 729 $(call DependOnVariableFileName, $1, $2)) 730 731# Main macro 732# Param 1 - Name of variable 733# Param 2 - (optional) name of file to store value in 734DependOnVariable = \ 735 $(call DependOnVariableHelper,$(strip $1),$(strip $2)) 736 737################################################################################ 738 739# Hook to include the corresponding custom file, if present. 740$(eval $(call IncludeCustomExtension, , common/MakeBase.gmk)) 741 742endif # _MAKEBASE_GMK 743