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