annotate make/common/MakeBase.gmk @ 1670:216f63a10515

8136385: Various build speed improvements for windows Reviewed-by: ihse
author erikj
date Mon, 28 Sep 2015 11:39:13 +0200
parents 382d23edebb6
children 1e64ec3d1c60
rev   line source
ohair@423 1 #
ihse@1349 2 # Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
ohair@423 3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@423 4 #
ohair@423 5 # This code is free software; you can redistribute it and/or modify it
ohair@423 6 # under the terms of the GNU General Public License version 2 only, as
ohair@423 7 # published by the Free Software Foundation. Oracle designates this
ohair@423 8 # particular file as subject to the "Classpath" exception as provided
ohair@423 9 # by Oracle in the LICENSE file that accompanied this code.
ohair@423 10 #
ohair@423 11 # This code is distributed in the hope that it will be useful, but WITHOUT
ohair@423 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@423 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@423 14 # version 2 for more details (a copy is included in the LICENSE file that
ohair@423 15 # accompanied this code).
ohair@423 16 #
ohair@423 17 # You should have received a copy of the GNU General Public License version
ohair@423 18 # 2 along with this work; if not, write to the Free Software Foundation,
ohair@423 19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@423 20 #
ohair@423 21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@423 22 # or visit www.oracle.com if you need additional information or have any
ohair@423 23 # questions.
ohair@423 24 #
ohair@423 25
ohair@423 26 ################################################################
ohair@423 27 #
ohair@423 28 # Setup common utility functions.
ohair@423 29 #
ohair@423 30 ################################################################
ohair@423 31
ohair@423 32 ifndef _MAKEBASE_GMK
ohair@423 33 _MAKEBASE_GMK := 1
ohair@423 34
ihse@1414 35 ifeq ($(wildcard $(SPEC)),)
ihse@1414 36 $(error MakeBase.gmk needs SPEC set to a proper spec.gmk)
ihse@1414 37 endif
ihse@1414 38
erikj@1453 39 # By defining this pseudo target, make will automatically remove targets
erikj@1453 40 # if their recipe fails so that a rebuild is automatically triggered on the
erikj@1453 41 # next make invocation.
erikj@1453 42 .DELETE_ON_ERROR:
ihse@1414 43
ihse@1637 44 ################################################################################
ihse@1637 45 # Definitions for special characters
ihse@1637 46 ################################################################################
ihse@1637 47
ihse@1637 48 # When calling macros, the spaces between arguments are
ihse@1637 49 # often semantically important! Sometimes we need to subst
ihse@1637 50 # spaces and commas, therefore we need the following macros.
ihse@1637 51 X:=
ihse@1637 52 SPACE:=$(X) $(X)
ihse@1637 53 COMMA:=,
ihse@1637 54 DOLLAR:=$$
ihse@1637 55 HASH:=\#
ihse@1637 56 LEFT_PAREN:=(
ihse@1637 57 RIGHT_PAREN:=)
ihse@1637 58 SQUOTE:='
ihse@1637 59 #'
ihse@1637 60 DQUOTE:="
ihse@1637 61 #"
ihse@1637 62 define NEWLINE
ihse@1637 63
ihse@1637 64
ihse@1637 65 endef
ihse@1637 66
erikj@1670 67 # In GNU Make 4.0 and higher, there is a file function for writing to files.
erikj@1670 68 ifeq (4.0, $(firstword $(sort 4.0 $(MAKE_VERSION))))
erikj@1670 69 HAS_FILE_FUNCTION := true
erikj@1670 70 endif
erikj@1670 71
ihse@1414 72 ##############################
ihse@1414 73 # Functions
ihse@1414 74 ##############################
ihse@1414 75
erikj@1486 76 ### Debug functions
erikj@1486 77
erikj@1486 78 # Prints the name and value of a variable
erikj@1486 79 PrintVar = \
erikj@1486 80 $(info $(strip $1) >$($(strip $1))<)
ihse@1414 81
ihse@1414 82 ### Functions for timers
ihse@1414 83
ihse@1414 84 # Store the build times in this directory.
ihse@1414 85 BUILDTIMESDIR=$(OUTPUT_ROOT)/make-support/build-times
ihse@1414 86
ihse@1414 87 # Record starting time for build of a sub repository.
ihse@1414 88 define RecordStartTime
ihse@1414 89 $(MKDIR) -p $(BUILDTIMESDIR)
ihse@1414 90 $(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)
ihse@1414 91 $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$(strip $1)_human_readable
ihse@1414 92 endef
ihse@1414 93
ihse@1414 94 # Record ending time and calculate the difference and store it in a
ihse@1414 95 # easy to read format. Handles builds that cross midnight. Expects
ihse@1414 96 # that a build will never take 24 hours or more.
ihse@1414 97 define RecordEndTime
ihse@1414 98 $(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)
ihse@1414 99 $(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)_human_readable
ihse@1414 100 $(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$(strip $1)` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$(strip $1)` $1 | \
ihse@1414 101 $(NAWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \
ihse@1414 102 M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \
ihse@1414 103 > $(BUILDTIMESDIR)/build_time_diff_$(strip $1)
ihse@1414 104 endef
ihse@1414 105
ihse@1414 106 # Hook to be called when starting to execute a top-level target
ihse@1414 107 define TargetEnter
ihse@1414 108 $(PRINTF) "## Starting $(patsubst %-only,%,$@)\n"
ihse@1414 109 $(call RecordStartTime,$(patsubst %-only,%,$@))
ihse@1414 110 endef
ihse@1414 111
ihse@1414 112 # Hook to be called when finish executing a top-level target
ihse@1414 113 define TargetExit
ihse@1414 114 $(call RecordEndTime,$(patsubst %-only,%,$@))
ihse@1414 115 $(PRINTF) "## Finished $(patsubst %-only,%,$@) (build time %s)\n\n" \
ihse@1414 116 "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_$(patsubst %-only,%,$@) | $(CUT) -f 1 -d ' '`"
ihse@1414 117 endef
ihse@1414 118
chegar@1120 119 ################################################################################
chegar@1120 120 # This macro translates $ into \$ to protect the $ from expansion in the shell.
chegar@1120 121 # To make this macro resilient against already escaped strings, first remove
chegar@1120 122 # any present escapes before escaping so that no double escapes are added.
chegar@1120 123 EscapeDollar = $(subst $$,\$$,$(subst \$$,$$,$(strip $1)))
chegar@1120 124
chegar@1120 125 ################################################################################
chegar@1120 126 # This macro translates $ into $$ to protect the string from make itself.
chegar@1120 127 DoubleDollar = $(subst $$,$$$$,$(strip $1))
chegar@1120 128
chegar@1120 129 ################################################################################
erikj@1670 130 # ListPathsSafely can be used to print command parameters to a file. This is
erikj@1670 131 # typically done if the command line lenght risk being too long for the
erikj@1670 132 # OS/shell. In later make versions, the file function can be used for this
erikj@1670 133 # purpose. For earlier versions, a more complex implementation is provided.
erikj@1670 134 #
erikj@1670 135 # The function ListPathsSafely can be called either directly or, more commonly
erikj@1670 136 # from a recipe line. If called from a recipe, it will be executed in the
erikj@1670 137 # evaluation phase of that recipe, which means that it will write to the file
erikj@1670 138 # before any other line in the recipe has been run.
erikj@1670 139 ifeq ($(HAS_FILE_FUNCTION), true)
erikj@1670 140 # Param 1 - Name of variable containing paths/arguments to output
erikj@1670 141 # Param 2 - File to print to
erikj@1670 142 # Param 3 - Set to true to append to file instead of overwriting
erikj@1670 143 define ListPathsSafely
erikj@1670 144 $$(call MakeDir, $$(dir $$(strip $2)))
erikj@1670 145 $$(file $$(if $$(filter true, $$(strip $3)),>>,>) \
erikj@1670 146 $$(strip $2),$$(subst $$(SPACE),$$(NEWLINE),$$(strip $$($$(strip $1)))))
erikj@1670 147 endef
chegar@1120 148
erikj@1670 149 else # HAS_FILE_FUNCTION
erikj@1670 150
erikj@1670 151 $(eval compress_paths = \
erikj@1670 152 $(strip $(shell $(CAT) $(SRC_ROOT)/make/common/support/ListPathsSafely-pre-compress.incl)))
erikj@1670 153 compress_paths += \
ohair@423 154 $(subst $(SRC_ROOT),X97,\
ohair@423 155 $(subst $(OUTPUT_ROOT),X98,\
ohair@423 156 $(subst X,X00,\
erikj@1670 157 $(subst $(SPACE),\n,$(strip $1)))))
erikj@1670 158 $(eval compress_paths += \
erikj@1670 159 $(strip $(shell $(CAT) $(SRC_ROOT)/make/common/support/ListPathsSafely-post-compress.incl)))
ohair@423 160
erikj@1670 161 decompress_paths=$(SED) -f $(SRC_ROOT)/make/common/support/ListPathsSafely-uncompress.sed \
erikj@1670 162 -e 's|X99|\\n|g' \
ihse@837 163 -e 's|X98|$(OUTPUT_ROOT)|g' -e 's|X97|$(SRC_ROOT)|g' \
erikj@1670 164 -e 's|X00|X|g'
ohair@423 165
erikj@1670 166 ListPathsSafely_IfPrintf = \
erikj@1670 167 $(if $(word $3,$($(strip $1))), \
erikj@1670 168 $(shell $(PRINTF) -- "$(strip $(call EscapeDollar, \
erikj@1670 169 $(call compress_paths, $(wordlist $3,$4,$($(strip $1))))))\n" \
erikj@1670 170 | $(decompress_paths) >> $2))
ohair@423 171
erikj@1670 172 # Param 1 - Name of variable containing paths/arguments to output
erikj@1670 173 # Param 2 - File to print to
erikj@1670 174 # Param 3 - Set to true to append to file instead of overwriting
ohair@423 175 define ListPathsSafely
ihse@837 176 ifneq (,$$(word 10001,$$($1)))
ihse@837 177 $$(error Cannot list safely more than 10000 paths. $1 has $$(words $$($1)) paths!)
ihse@837 178 endif
erikj@1670 179 $$(call MakeDir, $$(dir $2))
erikj@1670 180 ifneq ($$(strip $3), true)
erikj@1670 181 $$(shell $(RM) $$(strip $2))
erikj@1670 182 endif
ohair@423 183
erikj@1670 184 $$(call ListPathsSafely_IfPrintf,$1,$2,1,250)
erikj@1670 185 $$(call ListPathsSafely_IfPrintf,$1,$2,251,500)
erikj@1670 186 $$(call ListPathsSafely_IfPrintf,$1,$2,501,750)
erikj@1670 187 $$(call ListPathsSafely_IfPrintf,$1,$2,751,1000)
ohair@423 188
erikj@1670 189 $$(call ListPathsSafely_IfPrintf,$1,$2,1001,1250)
erikj@1670 190 $$(call ListPathsSafely_IfPrintf,$1,$2,1251,1500)
erikj@1670 191 $$(call ListPathsSafely_IfPrintf,$1,$2,1501,1750)
erikj@1670 192 $$(call ListPathsSafely_IfPrintf,$1,$2,1751,2000)
ohair@423 193
erikj@1670 194 $$(call ListPathsSafely_IfPrintf,$1,$2,2001,2250)
erikj@1670 195 $$(call ListPathsSafely_IfPrintf,$1,$2,2251,2500)
erikj@1670 196 $$(call ListPathsSafely_IfPrintf,$1,$2,2501,2750)
erikj@1670 197 $$(call ListPathsSafely_IfPrintf,$1,$2,2751,3000)
ohair@423 198
erikj@1670 199 $$(call ListPathsSafely_IfPrintf,$1,$2,3001,3250)
erikj@1670 200 $$(call ListPathsSafely_IfPrintf,$1,$2,3251,3500)
erikj@1670 201 $$(call ListPathsSafely_IfPrintf,$1,$2,3501,3750)
erikj@1670 202 $$(call ListPathsSafely_IfPrintf,$1,$2,3751,4000)
ohair@423 203
erikj@1670 204 $$(call ListPathsSafely_IfPrintf,$1,$2,4001,4250)
erikj@1670 205 $$(call ListPathsSafely_IfPrintf,$1,$2,4251,4500)
erikj@1670 206 $$(call ListPathsSafely_IfPrintf,$1,$2,4501,4750)
erikj@1670 207 $$(call ListPathsSafely_IfPrintf,$1,$2,4751,5000)
ohair@423 208
erikj@1670 209 $$(call ListPathsSafely_IfPrintf,$1,$2,5001,5250)
erikj@1670 210 $$(call ListPathsSafely_IfPrintf,$1,$2,5251,5500)
erikj@1670 211 $$(call ListPathsSafely_IfPrintf,$1,$2,5501,5750)
erikj@1670 212 $$(call ListPathsSafely_IfPrintf,$1,$2,5751,6000)
ohair@423 213
erikj@1670 214 $$(call ListPathsSafely_IfPrintf,$1,$2,6001,6250)
erikj@1670 215 $$(call ListPathsSafely_IfPrintf,$1,$2,6251,6500)
erikj@1670 216 $$(call ListPathsSafely_IfPrintf,$1,$2,6501,6750)
erikj@1670 217 $$(call ListPathsSafely_IfPrintf,$1,$2,6751,7000)
ohair@423 218
erikj@1670 219 $$(call ListPathsSafely_IfPrintf,$1,$2,7001,7250)
erikj@1670 220 $$(call ListPathsSafely_IfPrintf,$1,$2,7251,7500)
erikj@1670 221 $$(call ListPathsSafely_IfPrintf,$1,$2,7501,7750)
erikj@1670 222 $$(call ListPathsSafely_IfPrintf,$1,$2,7751,8000)
ohair@423 223
erikj@1670 224 $$(call ListPathsSafely_IfPrintf,$1,$2,8001,8250)
erikj@1670 225 $$(call ListPathsSafely_IfPrintf,$1,$2,8251,8500)
erikj@1670 226 $$(call ListPathsSafely_IfPrintf,$1,$2,8501,8750)
erikj@1670 227 $$(call ListPathsSafely_IfPrintf,$1,$2,8751,9000)
ohair@423 228
erikj@1670 229 $$(call ListPathsSafely_IfPrintf,$1,$2,9001,9250)
erikj@1670 230 $$(call ListPathsSafely_IfPrintf,$1,$2,9251,9500)
erikj@1670 231 $$(call ListPathsSafely_IfPrintf,$1,$2,9501,9750)
erikj@1670 232 $$(call ListPathsSafely_IfPrintf,$1,$2,9751,10000)
ohair@423 233 endef
erikj@1670 234 endif # HAS_FILE_FUNCTION
ohair@423 235
erikj@443 236 # The source tips can come from the Mercurial repository, or in the files
ihse@837 237 # $(HGTIP_FILENAME) which contains the tip but is also positioned in the same
ihse@837 238 # directory as the original $(HGDIR) directory.
ihse@837 239 # These should not be := assignments, only used from the root Makefile.
erikj@443 240 HG_VERSION = $(shell $(HG) version 2> /dev/null)
erikj@443 241 HG_DIRECTORY=.hg
erikj@443 242 HGTIP_FILENAME=.hgtip
erikj@443 243 HG_SEARCH = ./REPO ./*/REPO ./*/*/REPO ./*/*/*/REPO
erikj@443 244 REPO_LIST = $(patsubst ./%,%,$(patsubst %/,%,$(sort $(dir \
erikj@1547 245 $(shell $(CD) $(SRC_ROOT) ; \
erikj@1547 246 $(LS) -d $(HG_SEARCH:%/REPO=%/$(HG_DIRECTORY)) \
erikj@1547 247 $(HG_SEARCH:%/REPO=%/$(HGTIP_FILENAME)) \
ihse@837 248 2> /dev/null)))))
erikj@443 249
erikj@443 250 # Emit the repo:tip pairs to $@
erikj@443 251 define GetSourceTips
ihse@837 252 $(CD) $(SRC_ROOT) ; \
ihse@837 253 for i in $(REPO_LIST) IGNORE ; do \
ihse@837 254 if [ "$${i}" = "IGNORE" ] ; then \
ihse@837 255 continue; \
ihse@837 256 elif [ -d $${i}/$(HG_DIRECTORY) -a "$(HG_VERSION)" != "" ] ; then \
ihse@837 257 $(PRINTF) " %s:%s" \
ihse@837 258 "$${i}" `$(HG) tip --repository $${i} --template '{node|short}\n'` ; \
ihse@837 259 elif [ -f $${i}/$(HGTIP_FILENAME) ] ; then \
ihse@837 260 $(PRINTF) " %s:%s" \
ihse@837 261 "$${i}" `$(CAT) $${i}/$(HGTIP_FILENAME)` ; \
ihse@837 262 fi; \
ihse@837 263 done >> $@
ihse@837 264 $(PRINTF) "\n" >> $@
erikj@443 265 endef
erikj@443 266
erikj@443 267 # Create the HGTIP_FILENAME file. Called from jdk/make/closed/bundles.gmk
erikj@443 268 define CreateHgTip
ihse@837 269 $(HG) tip --repository $1 --template '{node|short}\n' > $1/$(HGTIP_FILENAME); \
ihse@837 270 $(ECHO) $1/$(HGTIP_FILENAME)
erikj@443 271 endef
erikj@443 272
erikj@443 273 define SetupLogging
ihse@1414 274 ifeq ($$(LOG_LEVEL), trace)
ihse@837 275 # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
ihse@837 276 # For each target executed, will print
ihse@837 277 # Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer)
ihse@837 278 # but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much
ihse@837 279 # (and causing a crash on Cygwin).
ihse@837 280 # Default shell seems to always be /bin/sh. Must override with bash to get this to work on Solaris.
ihse@837 281 # Only use time if it's GNU time which supports format and output file.
ihse@1414 282 WRAPPER_SHELL := $$(BASH) $$(SRC_ROOT)/common/bin/shell-tracer.sh $$(if $$(findstring yes,$$(IS_GNU_TIME)),$$(TIME),-) $$(OUTPUT_ROOT)/build-trace-time.log $$(SHELL)
ihse@1414 283 SHELL := $$(warning $$(if $$@,Building $$@,Running shell command) $$(if $$<, (from $$<))$$(if $$?, ($$(wordlist 1, 20, $$?) $$(if $$(wordlist 21, 22, $$?), ... [in total $$(words $$?) files]) newer)))$$(WRAPPER_SHELL)
ihse@837 284 endif
ihse@837 285 # Never remove warning messages; this is just for completeness
ihse@1414 286 LOG_WARN :=
ihse@1414 287 ifneq ($$(findstring $$(LOG_LEVEL), info debug trace),)
ihse@1414 288 LOG_INFO :=
ihse@837 289 else
ihse@1414 290 LOG_INFO := > /dev/null
ihse@837 291 endif
ihse@1414 292 ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
ihse@1414 293 LOG_DEBUG :=
ihse@837 294 else
ihse@1414 295 LOG_DEBUG := > /dev/null
ihse@837 296 endif
ihse@1414 297 ifneq ($$(findstring $$(LOG_LEVEL), trace),)
ihse@1414 298 LOG_TRACE :=
ihse@837 299 else
ihse@1414 300 LOG_TRACE := > /dev/null
ihse@837 301 endif
erikj@443 302 endef
erikj@443 303
erikj@443 304 # Make sure logging is setup for everyone that includes MakeBase.gmk.
erikj@443 305 $(eval $(call SetupLogging))
erikj@443 306
erikj@1383 307 ################################################################################
erikj@1383 308 # Creates a sequence of increasing numbers (inclusive).
erikj@1383 309 # Param 1 - starting number
erikj@1383 310 # Param 2 - ending number
erikj@1383 311 sequence = \
erikj@1383 312 $(wordlist $1, $2, $(strip \
erikj@1383 313 $(eval SEQUENCE_COUNT :=) \
erikj@1383 314 $(call _sequence-do,$(strip $2))))
erikj@1383 315
erikj@1383 316 _sequence-do = \
erikj@1383 317 $(if $(word $1, $(SEQUENCE_COUNT)),, \
erikj@1383 318 $(eval SEQUENCE_COUNT += .) \
erikj@1383 319 $(words $(SEQUENCE_COUNT)) \
erikj@1383 320 $(call _sequence-do,$1))
erikj@1383 321
erikj@1383 322 ################################################################################
erikj@1383 323
erikj@1383 324 MAX_PARAMS := 30
erikj@1383 325 PARAM_SEQUENCE := $(call sequence, 2, $(MAX_PARAMS))
erikj@1383 326
erikj@1383 327 # Template for creating a macro taking named parameters. To use it, assign the
erikj@1383 328 # template to a variable with the name you want for your macro, using '='
erikj@1383 329 # assignment. Then define a macro body with the suffix "Body". The Body macro
erikj@1383 330 # should take 1 parameter which should be a unique string for that invocation
erikj@1383 331 # of the macro.
erikj@1383 332 # Ex:
erikj@1383 333 # SetupFoo = $(NamedParamsMacroTemplate)
erikj@1383 334 # define SetupFooBody
erikj@1383 335 # # do something
erikj@1383 336 # # access parameters as $$($1_BAR)
erikj@1383 337 # endef
erikj@1383 338 # Call it like this
erikj@1383 339 # $(eval $(call SetupFoo, BUILD_SOMETHING, \
erikj@1383 340 # BAR := some parameter value, \
erikj@1383 341 # ))
erikj@1383 342 define NamedParamsMacroTemplate
erikj@1383 343 $(if $($(MAX_PARAMS)),$(error Internal makefile error: \
erikj@1383 344 Too many named arguments to macro, please update MAX_PARAMS in MakeBase.gmk))
erikj@1383 345 # Iterate over 2 3 4... and evaluate the named parameters with $1_ as prefix
erikj@1383 346 $(foreach i,$(PARAM_SEQUENCE), $(if $(strip $($i)),\
erikj@1383 347 $(strip $1)_$(strip $($i)))$(NEWLINE))
erikj@1383 348 # Debug print all named parameter names and values
erikj@1383 349 $(if $(findstring $(LOG_LEVEL),debug trace), \
erikj@1383 350 $(info $0 $(strip $1) $(foreach i,$(PARAM_SEQUENCE), \
erikj@1383 351 $(if $(strip $($i)),$(NEWLINE) $(strip [$i] $(if $(filter $(LOG_LEVEL), trace), \
erikj@1383 352 $($i), $(wordlist 1, 20, $($(i))) $(if $(word 21, $($(i))), ...)))))))
erikj@1383 353
erikj@1383 354 $(if $(DEBUG_$(strip $1)),
erikj@1383 355 $(info -------- <<< Begin expansion of $(strip $1)) \
erikj@1383 356 $(info $(call $(0)Body,$(strip $1))) \
erikj@1383 357 $(info -------- >>> End expansion of $(strip $1)) \
erikj@1383 358 )
erikj@1383 359
erikj@1383 360 $(call $(0)Body,$(strip $1))
ohair@492 361 endef
ohair@492 362
erikj@1383 363 ################################################################################
ohair@492 364 # Make directory without forking mkdir if not needed
erikj@1303 365 MakeDir = \
erikj@1303 366 $(strip $(if $(subst $(wildcard $1 $2 $3 $4 $5 $6 $7 $8 $9),,$(strip $1 $2 $3 $4 $5 $6 $7 $8 $9)),\
erikj@1303 367 $(shell $(MKDIR) -p $1 $2 $3 $4 $5 $6 $7 $8 $9)))
ohair@492 368
erikj@1383 369 ################################################################################
erikj@1458 370 # Assign a variable only if it is empty
erikj@1458 371 # Param 1 - Variable to assign
erikj@1458 372 # Param 2 - Value to assign
erikj@1458 373 SetIfEmpty = \
erikj@1458 374 $(if $($(strip $1)),,$(eval $(strip $1) := $2))
erikj@1458 375
erikj@1458 376 ################################################################################
erikj@1383 377
ohair@492 378 ifeq ($(OPENJDK_TARGET_OS),solaris)
ihse@837 379 # On Solaris, if the target is a symlink and exists, cp won't overwrite.
ihse@837 380 # Cp has to operate in recursive mode to allow for -P flag, to preserve soft links. If the
ihse@837 381 # name of the target file differs from the source file, rename after copy.
erikj@995 382 # If the source and target parent directories are the same, recursive copy doesn't work
erikj@995 383 # so we fall back on regular copy, which isn't preserving symlinks.
ihse@837 384 define install-file
ihse@1240 385 $(MKDIR) -p '$(@D)'
ohair@492 386 $(RM) '$@'
erikj@995 387 if [ "$(@D)" != "$(<D)" ]; then \
erikj@995 388 $(CP) -f -r -P '$<' '$(@D)'; \
erikj@995 389 if [ "$(@F)" != "$(<F)" ]; then \
erikj@995 390 $(MV) '$(@D)/$(<F)' '$@'; \
erikj@995 391 fi; \
erikj@995 392 else \
erikj@995 393 if [ -L '$<' ]; then \
erikj@995 394 $(ECHO) "Source file is a symlink and target is in the same directory: $< $@" ; \
erikj@995 395 exit 1; \
erikj@995 396 fi; \
erikj@995 397 $(CP) -f '$<' '$@'; \
erikj@995 398 fi
ihse@837 399 endef
ohair@492 400 else ifeq ($(OPENJDK_TARGET_OS),macosx)
ihse@837 401 # On mac, extended attributes sometimes creep into the source files, which may later
ihse@837 402 # cause the creation of ._* files which confuses testing. Clear these with xattr if
ihse@837 403 # set. Some files get their write permissions removed after being copied to the
ihse@837 404 # output dir. When these are copied again to images, xattr would fail. By only clearing
ihse@837 405 # attributes when they are present, failing on this is avoided.
ihse@837 406 define install-file
ihse@1240 407 $(MKDIR) -p '$(@D)'
erikj@668 408 $(CP) -fRP '$<' '$@'
erikj@668 409 if [ -n "`$(XATTR) -l '$@'`" ]; then $(XATTR) -c '$@'; fi
ihse@837 410 endef
ohair@492 411 else
chegar@1120 412 # Running mkdir and cp in the same shell speeds up copy intensive tasks in Cygwin
chegar@1120 413 # significantly.
ihse@837 414 define install-file
ihse@1240 415 $(MKDIR) -p '$(@D)' && $(CP) -fP '$<' '$@'
ihse@837 416 endef
ohair@492 417 endif
ohair@492 418
erikj@1383 419 ################################################################################
chegar@1120 420 # Filter out duplicate sub strings while preserving order. Keeps the first occurance.
ihse@1349 421 uniq = \
ihse@1349 422 $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
ihse@1349 423
erikj@1642 424 # Returns all whitespace-separated words in $2 where at least one of the
erikj@1642 425 # whitespace-separated words in $1 is a substring.
erikj@1642 426 containing = \
erikj@1642 427 $(strip \
erikj@1642 428 $(foreach v,$(strip $2),\
erikj@1642 429 $(call uniq,$(foreach p,$(strip $1),$(if $(findstring $p,$v),$v)))))
erikj@1642 430
erikj@1642 431 # Returns all whitespace-separated words in $2 where none of the
erikj@1642 432 # whitespace-separated words in $1 is a substring.
erikj@1642 433 not-containing = \
erikj@1642 434 $(strip $(filter-out $(call containing,$1,$2),$2))
erikj@1642 435
ihse@1349 436 # Return a list of all string elements that are duplicated in $1.
ihse@1349 437 dups = \
ihse@1349 438 $(strip $(foreach v, $(sort $1), $(if $(filter-out 1, \
ihse@1349 439 $(words $(filter $v, $1))), $v)))
erikj@555 440
erikj@1303 441 # String equals
erikj@1310 442 equals = \
erikj@1310 443 $(and $(findstring $(strip $1),$(strip $2)),\
erikj@1310 444 $(findstring $(strip $2),$(strip $1)))
erikj@1303 445
erikj@1486 446 # Remove a whole list of prefixes
erikj@1486 447 # $1 - List of prefixes
erikj@1486 448 # $2 - List of elements to process
erikj@1486 449 remove-prefixes = \
erikj@1486 450 $(strip $(if $1,$(patsubst $(firstword $1)%,%,\
erikj@1486 451 $(call remove-prefixes,$(filter-out $(firstword $1),$1),$2)),$2))
erikj@1486 452
erikj@1383 453 ################################################################################
erikj@1383 454
erikj@997 455 ifneq ($(DISABLE_CACHE_FIND), true)
mduigou@1029 456 # In Cygwin, finds are very costly, both because of expensive forks and because
mduigou@1029 457 # of bad file system caching. Find is used extensively in $(shell) commands to
mduigou@1029 458 # find source files. This makes rerunning make with no or few changes rather
mduigou@1029 459 # expensive. To speed this up, these two macros are used to cache the results
mduigou@1029 460 # of simple find commands for reuse.
mduigou@1029 461 #
mduigou@1029 462 # Runs a find and stores both the directories where it was run and the results.
mduigou@1029 463 # This macro can be called multiple times to add to the cache. Only finds files
mduigou@1029 464 # with no filters.
mduigou@1029 465 #
mduigou@1029 466 # Needs to be called with $(eval )
mduigou@1029 467 #
erikj@997 468 # Even if the performance benifit is negligible on other platforms, keep the
erikj@997 469 # functionality active unless explicitly disabled to exercise it more.
erikj@997 470 #
erikj@997 471 # Initialize FIND_CACHE_DIRS with := to make it a non recursively-expanded variable
erikj@997 472 FIND_CACHE_DIRS :=
mduigou@1029 473 # Param 1 - Dirs to find in
mduigou@1029 474 # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
ihse@837 475 define FillCacheFind
mduigou@1029 476 # Filter out already cached dirs. The - is needed when FIND_CACHE_DIRS is empty
erikj@997 477 # since filter out will then return empty.
erikj@997 478 FIND_CACHE_NEW_DIRS := $$(filter-out $$(addsuffix /%,\
erikj@997 479 - $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS), $1)
erikj@997 480 ifneq ($$(FIND_CACHE_NEW_DIRS), )
erikj@997 481 # Remove any trailing slash from dirs in the cache dir list
erikj@997 482 FIND_CACHE_DIRS += $$(patsubst %/,%, $$(FIND_CACHE_NEW_DIRS))
mduigou@1029 483 FIND_CACHE := $$(sort $$(FIND_CACHE) $$(shell $(FIND) $$(FIND_CACHE_NEW_DIRS) \( -type f -o -type l \) $2))
erikj@997 484 endif
ihse@837 485 endef
erikj@555 486
mduigou@1029 487 # Mimics find by looking in the cache if all of the directories have been cached.
mduigou@1029 488 # Otherwise reverts to shell find. This is safe to call on all platforms, even if
mduigou@1029 489 # cache is deactivated.
mduigou@1029 490 #
chegar@1120 491 # $1 can be either a directory or a file. If it's a directory, make
chegar@1120 492 # sure we have exactly one trailing slash before the wildcard.
mduigou@1029 493 # The extra - is needed when FIND_CACHE_DIRS is empty but should be harmless.
mduigou@1029 494 #
mduigou@1029 495 # Param 1 - Dirs to find in
mduigou@1029 496 # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
mduigou@1029 497 define CacheFind
erikj@997 498 $(if $(filter-out $(addsuffix /%,- $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS),$1), \
mduigou@1029 499 $(shell $(FIND) $1 \( -type f -o -type l \) $2), \
chegar@1120 500 $(filter $(addsuffix /%,$(patsubst %/,%,$1)) $1,$(FIND_CACHE)))
mduigou@1029 501 endef
chegar@1120 502
erikj@997 503 else
erikj@997 504 # If CacheFind is disabled, just run the find command.
mduigou@1029 505 # Param 1 - Dirs to find in
mduigou@1029 506 # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
erikj@997 507 define CacheFind
mduigou@1029 508 $(shell $(FIND) $1 \( -type f -o -type l \) $2)
erikj@997 509 endef
erikj@997 510 endif
erikj@555 511
erikj@555 512 ################################################################################
erikj@555 513
chegar@1120 514 define AddFileToCopy
chegar@1120 515 # Helper macro for SetupCopyFiles
chegar@1120 516 # 1 : Source file
chegar@1120 517 # 2 : Dest file
chegar@1120 518 # 3 : Variable to add targets to
chegar@1120 519 # 4 : Macro to call for copy operation
chegar@1120 520 $2: $1
chegar@1120 521 $(ECHO) $(LOG_INFO) Copying $$(patsubst $(OUTPUT_ROOT)/%,%,$$@)
chegar@1120 522 $$($$(strip $4))
chegar@1120 523
chegar@1120 524 $3 += $2
chegar@1120 525 endef
chegar@1120 526
ihse@1331 527 # Setup make rules for copying files, with an option to do more complex
ihse@1331 528 # processing instead of copying.
ihse@1331 529 #
ihse@1331 530 # Parameter 1 is the name of the rule. This name is used as variable prefix,
ihse@1331 531 # and the targets generated are listed in a variable by that name.
ihse@1331 532 #
ihse@1331 533 # Remaining parameters are named arguments. These include:
ihse@1331 534 # SRC : Source root dir (defaults to dir of first file)
ihse@1331 535 # DEST : Dest root dir
ihse@1331 536 # FILES : List of files to copy with absolute paths, or path relative to SRC.
ihse@1331 537 # Must be in SRC.
ihse@1331 538 # FLATTEN : Set to flatten the directory structure in the DEST dir.
ihse@1331 539 # MACRO : Optionally override the default macro used for making the copy.
ihse@1331 540 # Default is 'install-file'
erikj@1383 541 SetupCopyFiles = $(NamedParamsMacroTemplate)
erikj@1383 542 define SetupCopyFilesBody
chegar@1120 543
chegar@1120 544 ifeq ($$($1_MACRO), )
chegar@1120 545 $1_MACRO := install-file
chegar@1120 546 endif
chegar@1120 547
erikj@1330 548 # Default SRC to the dir of the first file.
erikj@1330 549 ifeq ($$($1_SRC), )
erikj@1330 550 $1_SRC := $$(dir $$(firstword $$($1_FILES)))
erikj@1330 551 endif
erikj@1330 552
erikj@1330 553 # Remove any trailing slash from SRC
erikj@1330 554 $1_SRC := $$(patsubst %/,%,$$($1_SRC))
erikj@1330 555
chegar@1120 556 $$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \
chegar@1120 557 $$(eval $$(call AddFileToCopy, $$($1_SRC)/$$f, \
chegar@1120 558 $$($1_DEST)/$$(if $$($1_FLATTEN),$$(notdir $$f),$$f), $1, $$($1_MACRO))))
chegar@1120 559
chegar@1120 560 endef
chegar@1120 561
chegar@1120 562 ################################################################################
erikj@1303 563 # ShellQuote
erikj@1303 564 #
ihse@1349 565 # Quotes a string with single quotes and replaces single quotes with '\'' so
erikj@1303 566 # that the contents survives being given to the shell.
erikj@1303 567
erikj@1303 568 ShellQuote = \
erikj@1303 569 $(SQUOTE)$(subst $(SQUOTE),$(SQUOTE)\$(SQUOTE)$(SQUOTE),$(strip $1))$(SQUOTE)
erikj@1303 570
erikj@1303 571 ################################################################################
erikj@1303 572 # Write to and read from file
erikj@1303 573
erikj@1303 574 # Param 1 - File to read
erikj@1303 575 ReadFile = \
erikj@1303 576 $(shell $(CAT) $1)
erikj@1303 577
erikj@1303 578 # Param 1 - Text to write
erikj@1303 579 # Param 2 - File to write to
erikj@1670 580 ifeq ($(HAS_FILE_FUNCTION), true)
erikj@1670 581 WriteFile = \
erikj@1670 582 $(file >$2,$(strip $1))
erikj@1670 583 else
erikj@1303 584 # Use printf to get consistent behavior on all platforms.
erikj@1303 585 WriteFile = \
erikj@1303 586 $(shell $(PRINTF) "%s" $(call ShellQuote, $1) > $2)
erikj@1670 587 endif
erikj@1303 588
erikj@1303 589 ################################################################################
erikj@1303 590 # DependOnVariable
erikj@1303 591 #
ihse@1349 592 # This macro takes a variable name and puts the value in a file only if the
ihse@1349 593 # value has changed since last. The name of the file is returned. This can be
erikj@1303 594 # used to create rule dependencies on make variable values. The following
erikj@1303 595 # example would get rebuilt if the value of SOME_VAR was changed:
erikj@1303 596 #
erikj@1303 597 # path/to/some-file: $(call DependOnVariable, SOME_VAR)
erikj@1303 598 # echo $(SOME_VAR) > $@
erikj@1303 599 #
erikj@1303 600 # Note that leading and trailing white space in the value is ignored.
erikj@1303 601 #
erikj@1303 602
erikj@1303 603 # Defines the sub directory structure to store variable value file in
erikj@1303 604 DependOnVariableDirName = \
erikj@1303 605 $(strip $(subst $(SRC_ROOT)/,,\
erikj@1303 606 $(if $(filter /%, $(firstword $(MAKEFILE_LIST))), \
erikj@1303 607 $(firstword $(MAKEFILE_LIST)), \
erikj@1303 608 $(CURDIR)/$(firstword $(MAKEFILE_LIST)))))
erikj@1303 609
erikj@1303 610 # Defines the name of the file to store variable value in. Generates a name
erikj@1303 611 # unless parameter 2 is given.
erikj@1303 612 # Param 1 - Name of variable
erikj@1303 613 # Param 2 - (optional) name of file to store value in
erikj@1303 614 DependOnVariableFileName = \
erikj@1303 615 $(strip $(if $(strip $2), $2, \
erikj@1303 616 $(MAKESUPPORT_OUTPUTDIR)/vardeps/$(DependOnVariableDirName)/$(strip $1).vardeps))
erikj@1303 617
erikj@1303 618 # Does the actual work with parameters stripped.
erikj@1303 619 # If the file exists AND the contents is the same as the variable, do nothing
erikj@1303 620 # else print a new file.
erikj@1303 621 # Always returns the name of the file where the value was printed.
erikj@1303 622 # Param 1 - Name of variable
erikj@1303 623 # Param 2 - (optional) name of file to store value in
erikj@1303 624 DependOnVariableHelper = \
erikj@1670 625 $(strip \
erikj@1670 626 $(eval -include $(call DependOnVariableFileName, $1, $2)) \
erikj@1670 627 $(if $(call equals, $(strip $($1)), $(strip $($1_old))),,\
erikj@1670 628 $(call MakeDir, $(dir $(call DependOnVariableFileName, $1, $2))) \
erikj@1670 629 $(if $(findstring $(LOG_LEVEL), trace), \
erikj@1670 630 $(info NewVariable $1: >$(strip $($1))<) \
erikj@1670 631 $(info OldVariable $1: >$(strip $($1_old))<)) \
erikj@1670 632 $(call WriteFile, $1_old:=$($1), $(call DependOnVariableFileName, $1, $2))) \
erikj@1670 633 $(call DependOnVariableFileName, $1, $2) \
erikj@1670 634 )
erikj@1303 635
erikj@1303 636 # Main macro
erikj@1303 637 # Param 1 - Name of variable
erikj@1303 638 # Param 2 - (optional) name of file to store value in
erikj@1303 639 DependOnVariable = \
erikj@1303 640 $(call DependOnVariableHelper,$(strip $1),$(strip $2))
erikj@1303 641
erikj@1303 642 ################################################################################
ihse@1633 643 # Failure logging support macros. These are supposed to be used by the Setup*
ihse@1633 644 # compilation macros.
ihse@1633 645 #
ihse@1633 646 # LogFailures will run a command and store a copy of output in a specified file.
ihse@1633 647 # If the command succeeds, the file is deleted, otherwise it is moved to the
ihse@1633 648 # failure-logs directory.
ihse@1633 649 # Param 1 - The log file of the failed command
ihse@1633 650 # Param 2 - A compact but representative name to describe this command
ihse@1633 651 # Param 3 - Command to run
ihse@1633 652 LogFailures = \
ihse@1633 653 ( ($(BASH) $(SRC_ROOT)/common/bin/logger.sh $1 $3 && $(RM) $1) || \
ihse@1633 654 (exitcode=$(DOLLAR)$(DOLLAR)? && $(MV) $1 $(MAKESUPPORT_OUTPUTDIR)/failure-logs/$(strip $2).log && exit $(DOLLAR)$(DOLLAR)exitcode) )
ihse@1633 655
ihse@1633 656 ################################################################################
erikj@1560 657 # Find lib dir for module
erikj@1560 658 # Param 1 - module name
erikj@1560 659 ifeq ($(OPENJDK_TARGET_OS_TYPE), unix)
erikj@1560 660 FindLibDirForModule = \
erikj@1560 661 $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)$(OPENJDK_TARGET_CPU_LIBDIR)
erikj@1560 662 else
erikj@1560 663 FindLibDirForModule = \
erikj@1560 664 $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)
erikj@1560 665 endif
erikj@1560 666
erikj@1560 667 ################################################################################
ihse@1637 668 # Return a string suitable for use after a -classpath option. It will correct and safe to use
ihse@1637 669 # on all platforms. Arguments are given as space separate classpath entries.
ihse@1637 670 # param 1 : A space separated list of classpath entries
ihse@1637 671 # The surrounding strip is needed to keep additional whitespace out
ihse@1637 672 PathList = \
ihse@1637 673 "$(subst $(SPACE),$(PATH_SEP),$(strip $1))"
ihse@1637 674
ihse@1637 675 ################################################################################
chegar@1120 676
chegar@1120 677 # Hook to include the corresponding custom file, if present.
chegar@1120 678 $(eval $(call IncludeCustomExtension, , common/MakeBase.gmk))
chegar@1120 679
ohair@423 680 endif # _MAKEBASE_GMK