1 #
  2 # Copyright (c) 2011, 2022, 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 
 32 ifndef _MAKEBASE_GMK
 33 _MAKEBASE_GMK := 1
 34 
 35 ifeq ($(wildcard $(SPEC)),)
 36   $(error MakeBase.gmk needs SPEC set to a proper spec.gmk)
 37 endif
 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 # Definitions for special characters
 46 ################################################################################
 47 
 48 # When calling macros, the spaces between arguments are
 49 # often semantically important! Sometimes we need to subst
 50 # spaces and commas, therefore we need the following macros.
 51 X:=
 52 SPACE:=$(X) $(X)
 53 COMMA:=,
 54 DOLLAR:=$$
 55 HASH:=\#
 56 LEFT_PAREN:=(
 57 RIGHT_PAREN:=)
 58 SQUOTE:='
 59 #'
 60 DQUOTE:="
 61 #"
 62 define NEWLINE
 63 
 64 
 65 endef
 66 
 67 # Make sure we have a value (could be overridden on command line by caller)
 68 CREATING_BUILDJDK ?= false
 69 
 70 # Certain features only work in newer version of GNU Make. The build will still
 71 # function in 3.81, but will be less performant.
 72 ifeq (4.0, $(firstword $(sort 4.0 $(MAKE_VERSION))))
 73   HAS_FILE_FUNCTION := true
 74   CORRECT_FUNCTION_IN_RECIPE_EVALUATION := true
 75   RWILDCARD_WORKS := true
 76 endif
 77 
 78 
 79 # For convenience, MakeBase.gmk continues to include these separate files, at
 80 # least for now.
 81 
 82 include $(TOPDIR)/make/common/Utils.gmk
 83 include $(TOPDIR)/make/common/MakeIO.gmk
 84 include $(TOPDIR)/make/common/CopyFiles.gmk
 85 
 86 ################################################################################
 87 # Functions for timers
 88 ################################################################################
 89 
 90 # Store the build times in this directory.
 91 BUILDTIMESDIR=$(OUTPUTDIR)/make-support/build-times
 92 
 93 # Record starting time for build of a sub repository.
 94 define RecordStartTime
 95 	$(DATE) '+%Y %m %d %H %M %S' | $(AWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_start_$(strip $1) && \
 96 	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$(strip $1)_human_readable
 97 endef
 98 
 99 # Record ending time and calculate the difference and store it in a
100 # easy to read format. Handles builds that cross midnight. Expects
101 # that a build will never take 24 hours or more.
102 define RecordEndTime
103 	$(DATE) '+%Y %m %d %H %M %S' | $(AWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)
104 	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)_human_readable
105 	$(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$(strip $1)` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$(strip $1)` $1 | \
106 	    $(AWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \
107 	    M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \
108 	    > $(BUILDTIMESDIR)/build_time_diff_$(strip $1)
109 endef
110 
111 # Hook to be called when starting to execute a top-level target
112 define TargetEnter
113 	$(PRINTF) "## Starting $(patsubst %-only,%,$@)\n"
114 	$(call RecordStartTime,$(patsubst %-only,%,$@))
115 endef
116 
117 # Hook to be called when finish executing a top-level target
118 define TargetExit
119 	$(call RecordEndTime,$(patsubst %-only,%,$@))
120 	$(PRINTF) "## Finished $(patsubst %-only,%,$@) (build time %s)\n\n" \
121 	    "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_$(patsubst %-only,%,$@) | $(CUT) -f 1 -d ' '`"
122 endef
123 
124 ################################################################################
125 
126 # A file containing a way to uniquely identify the source code revision that
127 # the build was created from
128 SOURCE_REVISION_TRACKER := $(SUPPORT_OUTPUTDIR)/src-rev/source-revision-tracker
129 
130 # Locate all sourcecode repositories included in the forest, as absolute paths
131 FindAllReposAbs = \
132     $(strip $(sort $(dir $(filter-out $(TOPDIR)/build/%, $(wildcard \
133         $(addprefix $(TOPDIR)/, .git */.git */*/.git */*/*/.git */*/*/*/.git) \
134     )))))
135 
136 # Locate all sourcecode repositories included in the forest, as relative paths
137 FindAllReposRel = \
138     $(strip $(subst $(TOPDIR)/,.,$(patsubst $(TOPDIR)/%/, %, $(FindAllReposAbs))))
139 
140 ################################################################################
141 
142 define SetupLogging
143   ifeq ($$(LOG_PROFILE_TIMES_FILE), true)
144     ifeq ($$(IS_GNU_TIME), yes)
145       SHELL :=  $$(BASH) $$(TOPDIR)/make/scripts/shell-profiler.sh \
146                 gnutime $$(TIME) \
147                 $$(OUTPUTDIR)/build-profile.log $$(SHELL)
148     else ifneq ($$(FLOCK), )
149       SHELL :=  $$(BASH) $$(TOPDIR)/make/scripts/shell-profiler.sh \
150                 flock $$(FLOCK) \
151                 $$(OUTPUTDIR)/build-profile.log $$(SHELL)
152     endif
153   endif
154 
155   ifeq ($$(LOG_LEVEL), trace)
156     SHELL_NO_RECURSE := $$(SHELL)
157     # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
158     # For each target executed, will print
159     # Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer)
160     # but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much
161     # (and causing a crash on Cygwin).
162     SHELL = $$(warning $$(if $$@,Building $$@,Running shell command) $$(if $$<, (from $$<))$$(if $$?, ($$(wordlist 1, 20, $$?) $$(if $$(wordlist 21, 22, $$?), ... [in total $$(words $$?) files]) newer)))$$(SHELL_NO_RECURSE) -x
163   endif
164 
165   # The LOG_PREFIX is set for sub recursive calls like buildjdk and bootcycle.
166   # The warn level can never be turned off
167   LogWarn = $$(info $(LOG_PREFIX)$$(strip $$1))
168   LOG_WARN :=
169   ifneq ($$(findstring $$(LOG_LEVEL), info debug trace),)
170     LogInfo = $$(info $(LOG_PREFIX)$$(strip $$1))
171     LOG_INFO :=
172   else
173     LogInfo =
174     LOG_INFO := > /dev/null
175   endif
176   ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
177     LogDebug = $$(info $(LOG_PREFIX)$$(strip $$1))
178     LOG_DEBUG :=
179   else
180     LogDebug =
181     LOG_DEBUG := > /dev/null
182   endif
183   ifneq ($$(findstring $$(LOG_LEVEL), trace),)
184     LogTrace = $$(info $(LOG_PREFIX)$$(strip $$1))
185     LOG_TRACE :=
186   else
187     LogTrace =
188     LOG_TRACE := > /dev/null
189   endif
190 endef
191 
192 # Make sure logging is setup for everyone that includes MakeBase.gmk.
193 $(eval $(call SetupLogging))
194 
195 ################################################################################
196 
197 MAX_PARAMS := 96
198 PARAM_SEQUENCE := $(call sequence, 2, $(MAX_PARAMS))
199 
200 # Template for creating a macro taking named parameters. To use it, assign the
201 # template to a variable with the name you want for your macro, using '='
202 # assignment. Then define a macro body with the suffix "Body". The Body macro
203 # should take 1 parameter which should be a unique string for that invocation
204 # of the macro.
205 # Ex:
206 # SetupFoo = $(NamedParamsMacroTemplate)
207 # define SetupFooBody
208 #   # do something
209 #   # access parameters as $$($1_BAR)
210 # endef
211 # Call it like this
212 # $(eval $(call SetupFoo, BUILD_SOMETHING, \
213 #     BAR := some parameter value, \
214 # ))
215 define NamedParamsMacroTemplate
216   $(if $($(MAX_PARAMS)),$(error Internal makefile error: \
217       Too many named arguments to macro, please update MAX_PARAMS in MakeBase.gmk))
218   # Iterate over 2 3 4... and evaluate the named parameters with $1_ as prefix
219   $(foreach i,$(PARAM_SEQUENCE), $(if $(strip $($i)),\
220     $(strip $1)_$(strip $(call EscapeHash, $(call DoubleDollar, $($i))))$(NEWLINE)))
221   # Debug print all named parameter names and values
222   $(if $(findstring $(LOG_LEVEL), trace), \
223     $(info $0 $(strip $1) $(foreach i,$(PARAM_SEQUENCE), \
224       $(if $(strip $($i)),$(NEWLINE) $(strip [$i] $(if $(filter $(LOG_LEVEL), trace), \
225         $($i), $(wordlist 1, 20, $($(i))) $(if $(word 21, $($(i))), ...)))))))
226 
227   $(if $(DEBUG_$(strip $1)),
228     $(info -------- <<< Begin expansion of $(strip $1)) \
229     $(info $(call $(0)Body,$(strip $1))) \
230     $(info -------- >>> End expansion of $(strip $1)) \
231   )
232 
233   $(call $(0)Body,$(strip $1))
234 endef
235 
236 ################################################################################
237 # Make directory without forking mkdir if not needed.
238 #
239 # If a directory with an encoded space is provided, the wildcard function
240 # sometimes returns false answers (typically if the dir existed when the
241 # makefile was parsed, but was deleted by a previous rule). In that case, always
242 # call mkdir regardless of what wildcard says.
243 #
244 # 1: List of directories to create
245 MakeDir = \
246     $(strip \
247         $(eval MakeDir_dirs_to_make := $(strip $(foreach d, $1, \
248           $(if $(findstring ?, $d), '$(call DecodeSpace, $d)', \
249             $(if $(wildcard $d), , $d) \
250           ) \
251         ))) \
252         $(if $(MakeDir_dirs_to_make), $(shell $(MKDIR) -p $(MakeDir_dirs_to_make))) \
253     )
254 
255 # Make directory for target file. Should handle spaces in filenames. Just
256 # calling $(call MakeDir $(@D)) will not work if the directory contains a space
257 # and the target file already exists. In that case, the target file will have
258 # its wildcard ? resolved and the $(@D) will evaluate each space separated dir
259 # part on its own.
260 MakeTargetDir = \
261     $(call MakeDir, $(dir $(call EncodeSpace, $@)))
262 
263 ################################################################################
264 # All install-file and related macros automatically call DecodeSpace when needed.
265 
266 ifeq ($(call isTargetOs, macosx), true)
267   # On mac, extended attributes sometimes creep into the source files, which may later
268   # cause the creation of ._* files which confuses testing. Clear these with xattr if
269   # set. Some files get their write permissions removed after being copied to the
270   # output dir. When these are copied again to images, xattr would fail. By only clearing
271   # attributes when they are present, failing on this is avoided.
272   #
273   # If copying a soft link to a directory, need to delete the target first to avoid
274   # weird errors.
275   define install-file
276 	$(call MakeTargetDir)
277 	$(RM) '$(call DecodeSpace, $@)'
278         # Work around a weirdness with cp on Macosx. When copying a symlink, if
279         # the target of the link is write protected (e.g. 444), cp will add
280         # write permission for the user on the target file (644). Avoid this by
281         # using ln to create a new link instead.
282 	if [ -h '$(call DecodeSpace, $<)' ]; then \
283 	  $(LN) -s "`$(READLINK) '$(call DecodeSpace, $<)'`" '$(call DecodeSpace, $@)'; \
284 	else \
285 	  $(CP) -fRP '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)'; \
286 	fi
287 	if [ -n "`$(XATTR) -ls '$(call DecodeSpace, $@)'`" ]; then \
288 	  $(XATTR) -cs '$(call DecodeSpace, $@)'; \
289 	fi
290   endef
291 else
292   define install-file
293 	$(call MakeTargetDir)
294 	$(CP) -fP '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)'
295   endef
296 endif
297 
298 # Variant of install file that does not preserve symlinks
299 define install-file-nolink
300 	$(call MakeTargetDir)
301 	$(CP) -f '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)'
302 endef
303 
304 ################################################################################
305 # link-file-* works similarly to install-file but creates a symlink instead.
306 # There are two versions, either creating a relative or an absolute link. Be
307 # careful when using this on Windows since the symlink created is only valid in
308 # the unix emulation environment.
309 # In msys2 we use mklink /J because its ln would perform a deep copy of the target.
310 # This inhibits performance and can lead to issues with long paths. With mklink /J
311 # relative linking does not work, so we handle the link as absolute path.
312 ifeq ($(OPENJDK_BUILD_OS_ENV), windows.msys2)
313   define link-file-relative
314 	$(call MakeTargetDir)
315 	$(RM) '$(call DecodeSpace, $@)'
316 	cmd //c "mklink /J $(call FixPath, $(call DecodeSpace, $@)) $(call FixPath, $(call DecodeSpace, $<))"
317   endef
318 else
319   define link-file-relative
320 	$(call MakeTargetDir)
321 	$(RM) '$(call DecodeSpace, $@)'
322 	$(LN) -s '$(call DecodeSpace, $(call RelativePath, $<, $(@D)))' '$(call DecodeSpace, $@)'
323   endef
324 endif
325 
326 ifeq ($(OPENJDK_BUILD_OS_ENV), windows.msys2)
327   define link-file-absolute
328 	$(call MakeTargetDir)
329 	$(RM) '$(call DecodeSpace, $@)'
330 	cmd //c "mklink /J $(call FixPath, $(call DecodeSpace, $@)) $(call FixPath, $(call DecodeSpace, $<))"
331   endef
332 else
333   define link-file-absolute
334 	$(call MakeTargetDir)
335 	$(RM) '$(call DecodeSpace, $@)'
336 	$(LN) -s '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)'
337   endef
338 endif
339 
340 ################################################################################
341 
342 # Recursive wildcard function. Walks down directories recursively and matches
343 # files with the search patterns. Patterns use standard file wildcards (* and
344 # ?).
345 #
346 # $1 - Directories to start search in
347 # $2 - Search patterns
348 rwildcard = \
349     $(strip \
350         $(foreach d, \
351           $(patsubst %/,%,$(sort $(dir $(wildcard $(addsuffix /*/*, $(strip $1)))))), \
352           $(call rwildcard,$d,$2) \
353         ) \
354         $(call DoubleDollar, $(wildcard $(foreach p, $2, $(addsuffix /$(strip $p), $(strip $1))))) \
355     )
356 
357 # Find non directories using recursive wildcard function. This function may
358 # be used directly when a small amount of directories is expected to be
359 # searched and caching is not expected to be of use.
360 #
361 # $1 - Directory to start search in
362 # $2 - Optional search patterns, defaults to '*'.
363 WildcardFindFiles = \
364     $(sort $(strip \
365         $(eval WildcardFindFiles_result := $(call rwildcard,$(patsubst %/,%,$1),$(if $(strip $2),$2,*))) \
366         $(filter-out $(patsubst %/,%,$(sort $(dir $(WildcardFindFiles_result)))), \
367             $(WildcardFindFiles_result) \
368         ) \
369     ))
370 
371 # Find non directories using the find utility in the shell. Safe to call for
372 # non existing directories, or directories containing wildcards.
373 #
374 # Files containing space will get spaces replaced with ? because GNU Make
375 # cannot handle lists of files with space in them. By using ?, make will match
376 # the wildcard to space in many situations so we don't need to replace back
377 # to space on every use. While not a complete solution it does allow some uses
378 # of FindFiles to function with spaces in file names, including for
379 # SetupCopyFiles. Unfortunately this does not work for WildcardFindFiles so
380 # if files with spaces are anticipated, use ShellFindFiles directly.
381 #
382 # $1 - Directories to start search in.
383 # $2 - Optional search patterns, empty means find everything. Patterns use
384 #      standard file wildcards (* and ?) and should not be quoted.
385 # $3 - Optional options to find.
386 ShellFindFiles = \
387     $(if $(wildcard $1), \
388       $(sort \
389           $(shell $(FIND) $3 $(patsubst %/,%,$(wildcard $1)) \( -type f -o -type l \) \
390               $(if $(strip $2), -a \( -name "$(firstword $2)" \
391               $(foreach p, $(filter-out $(firstword $2), $2), -o -name "$(p)") \)) \
392               | $(TR) ' ' '?' \
393           ) \
394       ) \
395     )
396 
397 # Find non directories using the method most likely to work best for the
398 # current build host
399 #
400 # $1 - Directory to start search in
401 # $2 - Optional search patterns, defaults to '*'.
402 ifeq ($(OPENJDK_BUILD_OS)-$(RWILDCARD_WORKS), windows-true)
403   DirectFindFiles = $(WildcardFindFiles)
404 else
405   DirectFindFiles = $(ShellFindFiles)
406 endif
407 
408 # Finds files using a cache that is populated by FillFindCache below. If any of
409 # the directories given have not been cached, DirectFindFiles is used for
410 # everything. Caching is especially useful in Cygwin, where file finds are very
411 # costly.
412 #
413 # $1 - Directories to start search in.
414 # $2 - Optional search patterns. If used, no caching is done.
415 CacheFindFiles_CACHED_DIRS :=
416 CacheFindFiles_CACHED_FILES :=
417 CacheFindFiles = \
418     $(if $2, \
419       $(call DirectFindFiles, $1, $2) \
420     , \
421       $(if $(filter-out $(addsuffix /%, $(CacheFindFiles_CACHED_DIRS)) \
422           $(CacheFindFiles_CACHED_DIRS), $1), \
423         $(call DirectFindFiles, $1) \
424       , \
425         $(filter $(addsuffix /%,$(patsubst %/,%,$1)) $1,$(CacheFindFiles_CACHED_FILES)) \
426       ) \
427     )
428 
429 # Explicitly adds files to the find cache used by CacheFindFiles.
430 #
431 # $1 - Directories to start search in
432 FillFindCache = \
433     $(eval CacheFindFiles_NEW_DIRS := $$(filter-out $$(addsuffix /%,\
434         $$(CacheFindFiles_CACHED_DIRS)) $$(CacheFindFiles_CACHED_DIRS), $1)) \
435     $(if $(CacheFindFiles_NEW_DIRS), \
436       $(eval CacheFindFiles_CACHED_DIRS += $$(patsubst %/,%,$$(CacheFindFiles_NEW_DIRS))) \
437       $(eval CacheFindFiles_CACHED_FILES := $$(sort $$(CacheFindFiles_CACHED_FILES) \
438           $$(call DirectFindFiles, $$(CacheFindFiles_NEW_DIRS)))) \
439     )
440 
441 # Findfiles is the default macro that should be used to find files in the file
442 # system. This function does not always support files with spaces in the names.
443 # If files with spaces are anticipated, use ShellFindFiles directly.
444 #
445 # $1 - Directories to start search in.
446 # $2 - Optional search patterns, empty means find everything. Patterns use
447 #      standard file wildcards (* and ?) and should not be quoted.
448 ifeq ($(DISABLE_CACHE_FIND), true)
449   FindFiles = $(DirectFindFiles)
450 else
451   FindFiles = $(CacheFindFiles)
452 endif
453 
454 ################################################################################
455 # FixPath
456 #
457 # On Windows, converts a path from cygwin/unix style (e.g. /bin/foo) into
458 # "mixed mode" (e.g. c:/cygwin/bin/foo). On other platforms, return the path
459 # unchanged.
460 # This also converts a colon-separated list of paths to a semicolon-separated
461 # list.
462 # This is normally not needed since we use the FIXPATH prefix for command lines,
463 # but might be needed in certain circumstances.
464 #
465 # FixPathFile is the file version of FixPath. It instead takes a file with paths in $1
466 # and outputs the 'fixed' paths into the file in $2. If the file in $2 already exists
467 # it is overwritten.
468 # On non-Windows platforms this instead does a copy, so that $2 can still be used
469 # as a depenendency of a make rule, instead of having to conditionally depend on
470 # $1 instead, based on the target platform.
471 ifeq ($(call isTargetOs, windows), true)
472   FixPath = \
473     $(strip $(subst \,\\, $(shell $(FIXPATH_BASE) print $(patsubst $(FIXPATH), , $1))))
474   FixPathFile = \
475     $(shell $(FIXPATH_BASE) convert $1 $2)
476 else
477   FixPath = \
478       $1
479   FixPathFile = \
480       $(shell $(CP) $1 $2)
481 endif
482 
483 ################################################################################
484 # DependOnVariable
485 #
486 # This macro takes a variable name and puts the value in a file only if the
487 # value has changed since last. The name of the file is returned. This can be
488 # used to create rule dependencies on make variable values. The following
489 # example would get rebuilt if the value of SOME_VAR was changed:
490 #
491 # path/to/some-file: $(call DependOnVariable, SOME_VAR)
492 #         echo $(SOME_VAR) > $@
493 #
494 # Note that leading and trailing white space in the value is ignored.
495 #
496 
497 # Defines the sub directory structure to store variable value file in
498 DependOnVariableDirName = \
499     $(strip $(addsuffix $(if $(MODULE),/$(MODULE)), \
500         $(subst $(WORKSPACE_ROOT)/,, $(if $(filter /%, $(firstword $(MAKEFILE_LIST))), \
501           $(firstword $(MAKEFILE_LIST)), \
502           $(CURDIR)/$(firstword $(MAKEFILE_LIST))))))
503 
504 # Defines the name of the file to store variable value in. Generates a name
505 # unless parameter 2 is given.
506 # Param 1 - Name of variable
507 # Param 2 - (optional) name of file to store value in
508 DependOnVariableFileName = \
509     $(strip $(if $(strip $2), $2, \
510       $(MAKESUPPORT_OUTPUTDIR)/vardeps/$(DependOnVariableDirName)/$(strip $1).vardeps))
511 
512 # Writes the vardeps file. Assumes $1_filename has been setup
513 # Param 1 - Name of variable
514 DependOnVariableWriteFile = \
515     $(call MakeDir, $(dir $($1_filename))) \
516     $(call WriteFile, $1_old:=$(call DoubleDollar,$(call EscapeHash,$($1))), \
517          $($1_filename)) \
518 
519 # Does the actual work with parameters stripped.
520 # If the file exists AND the contents is the same as the variable, do nothing
521 # else print a new file.
522 # Always returns the name of the file where the value was printed.
523 # Param 1 - Name of variable
524 # Param 2 - (optional) name of file to store value in
525 DependOnVariableHelper = \
526     $(strip \
527         $(eval $1_filename := $(call DependOnVariableFileName, $1, $2)) \
528         $(if $(wildcard $($1_filename)), \
529           $(eval include $($1_filename)) \
530           $(if $(call equals, $(strip $($1)), $(strip $($1_old))),,\
531             $(if $(findstring $(LOG_LEVEL), trace), \
532                 $(info NewVariable $1: >$(strip $($1))<) \
533                 $(info OldVariable $1: >$(strip $($1_old))<) \
534             ) \
535             $(call DependOnVariableWriteFile,$1) \
536           ) \
537         , \
538           $(call DependOnVariableWriteFile,$1) \
539         ) \
540         $($1_filename) \
541     )
542 
543 # Main macro
544 # Param 1 - Name of variable
545 # Param 2 - (optional) name of file to store value in
546 DependOnVariable = \
547     $(call DependOnVariableHelper,$(strip $1),$(strip $2))
548 
549 # LogCmdlines is only intended to be used by ExecuteWithLog
550 ifeq ($(LOG_CMDLINES), true)
551   LogCmdlines = $(info $(strip $1))
552 else
553   LogCmdlines =
554 endif
555 
556 ################################################################################
557 # ExecuteWithLog will run a command and log the output appropriately. This is
558 # meant to be used by commands that do "real" work, like a compilation.
559 # The output is stored in a specified log file, which is displayed at the end
560 # of the build in case of failure. The  command line itself is stored in a file,
561 # and also logged to stdout if the LOG=cmdlines option has been given.
562 #
563 # NOTE: If the command redirects stdout, the caller needs to wrap it in a
564 # subshell (by adding parentheses around it), otherwise the redirect to the
565 # subshell tee process will create a race condition where the target file may
566 # not be fully written when the make recipe is done.
567 #
568 # Param 1 - The path to base the name of the log file / command line file on
569 # Param 2 - The command to run
570 ExecuteWithLog = \
571   $(call LogCmdlines, Executing: [$(strip $2)]) \
572   $(call MakeDir, $(dir $(strip $1)) $(MAKESUPPORT_OUTPUTDIR)/failure-logs) \
573   $(call WriteFile, $2, $(strip $1).cmdline) \
574   ( $(RM) $(strip $1).log && $(strip $2) > >($(TEE) -a $(strip $1).log) 2> >($(TEE) -a $(strip $1).log >&2) || \
575       ( exitcode=$(DOLLAR)? && \
576       $(CP) $(strip $1).log $(MAKESUPPORT_OUTPUTDIR)/failure-logs/$(subst /,_,$(patsubst $(OUTPUTDIR)/%,%,$(strip $1))).log && \
577       $(CP) $(strip $1).cmdline $(MAKESUPPORT_OUTPUTDIR)/failure-logs/$(subst /,_,$(patsubst $(OUTPUTDIR)/%,%,$(strip $1))).cmdline && \
578       exit $(DOLLAR)exitcode ) )
579 
580 ################################################################################
581 
582 # Hook to include the corresponding custom file, if present.
583 $(eval $(call IncludeCustomExtension, common/MakeBase.gmk))
584 
585 endif # _MAKEBASE_GMK