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.
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.
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).
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.
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
26
################################################################
27
# MakeBase provides the core functionality needed and used by all makefiles. It
28
# should be included by all makefiles. MakeBase provides essential
29
# functionality for named parameter functions, variable dependency, tool
30
# execution, logging and fixpath functionality.
31
################################################################
36
ifeq ($(wildcard $(SPEC)),)
37
$(error MakeBase.gmk needs SPEC set to a proper spec.gmk)
40
# By defining this pseudo target, make will automatically remove targets
41
# if their recipe fails so that a rebuild is automatically triggered on the
42
# next make invocation.
45
################################################################################
46
# Definitions for special characters
47
################################################################################
49
# When calling macros, the spaces between arguments are
50
# often semantically important! Sometimes we need to subst
51
# spaces and commas, therefore we need the following macros.
68
# Certain features only work in newer version of GNU Make. The build will still
69
# function in 3.81, but will be less performant.
70
ifeq (4.0, $(firstword $(sort 4.0 $(MAKE_VERSION))))
71
HAS_FILE_FUNCTION := true
72
CORRECT_FUNCTION_IN_RECIPE_EVALUATION := true
73
RWILDCARD_WORKS := true
76
# For convenience, MakeBase.gmk continues to include these separate files, at
78
# Utils.gmk must be included before FileUtils.gmk, since it uses some of the
79
# basic utility functions there.
80
include $(TOPDIR)/make/common/Utils.gmk
81
include $(TOPDIR)/make/common/FileUtils.gmk
83
################################################################################
84
# Make sure we have a value (could be overridden on command line by caller)
85
CREATING_BUILDJDK ?= false
87
################################################################################
90
ifeq ($$(LOG_PROFILE_TIMES_FILE), true)
91
ifeq ($$(IS_GNU_TIME), yes)
92
SHELL := $$(BASH) $$(TOPDIR)/make/scripts/shell-profiler.sh \
94
$$(OUTPUTDIR)/build-profile.log $$(SHELL)
95
else ifneq ($$(FLOCK), )
96
SHELL := $$(BASH) $$(TOPDIR)/make/scripts/shell-profiler.sh \
98
$$(OUTPUTDIR)/build-profile.log $$(SHELL)
102
ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
103
SHELL := $$(SHELL) -x
106
ifeq ($$(LOG_LEVEL), trace)
107
SHELL_NO_RECURSE := $$(SHELL)
108
# Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
109
# For each target executed, will print
110
# Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer)
111
# but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much
112
# (and causing a crash on Cygwin).
113
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
116
# The LOG_PREFIX is set for sub recursive calls like buildjdk and bootcycle.
117
# The warn level can never be turned off
118
LogWarn = $$(info $(LOG_PREFIX)$$(strip $$1))
120
ifneq ($$(findstring $$(LOG_LEVEL), info debug trace),)
121
LogInfo = $$(info $(LOG_PREFIX)$$(strip $$1))
125
LOG_INFO := > /dev/null
127
ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
128
LogDebug = $$(info $(LOG_PREFIX)$$(strip $$1))
132
LOG_DEBUG := > /dev/null
134
ifneq ($$(findstring $$(LOG_LEVEL), trace),)
135
LogTrace = $$(info $(LOG_PREFIX)$$(strip $$1))
139
LOG_TRACE := > /dev/null
143
# Make sure logging is setup for everyone that includes MakeBase.gmk.
144
$(eval $(call SetupLogging))
146
################################################################################
149
PARAM_SEQUENCE := $(call sequence, 2, $(MAX_PARAMS))
151
# Template for creating a macro taking named parameters. To use it, assign the
152
# template to a variable with the name you want for your macro, using '='
153
# assignment. Then define a macro body with the suffix "Body". The Body macro
154
# should take 1 parameter which should be a unique string for that invocation
157
# SetupFoo = $(NamedParamsMacroTemplate)
160
# # access parameters as $$($1_BAR)
163
# $(eval $(call SetupFoo, BUILD_SOMETHING, \
164
# BAR := some parameter value, \
166
define NamedParamsMacroTemplate
167
$(if $($(MAX_PARAMS)),$(error Internal makefile error: \
168
Too many named arguments to macro, please update MAX_PARAMS in MakeBase.gmk))
169
# Iterate over 2 3 4... and evaluate the named parameters with $1_ as prefix
170
$(foreach i,$(PARAM_SEQUENCE), $(if $(strip $($i)),\
171
$(strip $1)_$(strip $(call EscapeHash, $(call DoubleDollar, $($i))))$(NEWLINE)))
172
# Debug print all named parameter names and values
173
$(if $(findstring $(LOG_LEVEL), trace), \
174
$(info $0 $(strip $1) $(foreach i,$(PARAM_SEQUENCE), \
175
$(if $(strip $($i)),$(NEWLINE) $(strip [$i] $(if $(filter $(LOG_LEVEL), trace), \
176
$($i), $(wordlist 1, 20, $($(i))) $(if $(word 21, $($(i))), ...)))))))
178
$(if $(DEBUG_$(strip $1)),
179
$(info -------- <<< Begin expansion of $(strip $1)) \
180
$(info $(call $(0)Body,$(strip $1))) \
181
$(info -------- >>> End expansion of $(strip $1)) \
184
$(call $(0)Body,$(strip $1))
187
################################################################################
190
# On Windows, converts a path from cygwin/unix style (e.g. /bin/foo) into
191
# "mixed mode" (e.g. c:/cygwin/bin/foo). On other platforms, return the path
193
# This also converts a colon-separated list of paths to a semicolon-separated
195
# This is normally not needed since we use the FIXPATH prefix for command lines,
196
# but might be needed in certain circumstances.
198
# FixPathFile is the file version of FixPath. It instead takes a file with paths in $1
199
# and outputs the 'fixed' paths into the file in $2. If the file in $2 already exists
201
# On non-Windows platforms this instead does a copy, so that $2 can still be used
202
# as a depenendency of a make rule, instead of having to conditionally depend on
203
# $1 instead, based on the target platform.
204
ifeq ($(call isTargetOs, windows), true)
206
$(strip $(subst \,\\, $(shell $(FIXPATH_BASE) print $(patsubst $(FIXPATH), , $1))))
208
$(shell $(FIXPATH_BASE) convert $1 $2)
216
################################################################################
219
# This macro takes a variable name and puts the value in a file only if the
220
# value has changed since last. The name of the file is returned. This can be
221
# used to create rule dependencies on make variable values. The following
222
# example would get rebuilt if the value of SOME_VAR was changed:
224
# path/to/some-file: $(call DependOnVariable, SOME_VAR)
225
# echo $(SOME_VAR) > $@
227
# Note that leading and trailing white space in the value is ignored.
230
# Defines the sub directory structure to store variable value file in
231
DependOnVariableDirName = \
232
$(strip $(addsuffix $(if $(MODULE),/$(MODULE)), \
233
$(subst $(WORKSPACE_ROOT)/,, $(if $(filter /%, $(firstword $(MAKEFILE_LIST))), \
234
$(firstword $(MAKEFILE_LIST)), \
235
$(CURDIR)/$(firstword $(MAKEFILE_LIST))))))
237
# Defines the name of the file to store variable value in. Generates a name
238
# unless parameter 2 is given.
239
# Param 1 - Name of variable
240
# Param 2 - (optional) name of file to store value in
241
DependOnVariableFileName = \
242
$(strip $(if $(strip $2), $2, \
243
$(MAKESUPPORT_OUTPUTDIR)/vardeps/$(DependOnVariableDirName)/$(strip $1).vardeps))
245
# Writes the vardeps file. Assumes $1_filename has been setup
246
# Param 1 - Name of variable
247
DependOnVariableWriteFile = \
248
$(call MakeDir, $(dir $($1_filename))) \
249
$(call WriteFile, $1_old:=$(call DoubleDollar,$(call EscapeHash,$($1))), \
252
# Does the actual work with parameters stripped.
253
# If the file exists AND the contents is the same as the variable, do nothing
254
# else print a new file.
255
# Always returns the name of the file where the value was printed.
256
# Param 1 - Name of variable
257
# Param 2 - (optional) name of file to store value in
258
DependOnVariableHelper = \
260
$(eval $1_filename := $(call DependOnVariableFileName, $1, $2)) \
261
$(if $(wildcard $($1_filename)), \
262
$(eval include $($1_filename)) \
263
$(if $(call equals, $(strip $($1)), $(strip $($1_old))),,\
264
$(if $(findstring $(LOG_LEVEL), trace), \
265
$(info NewVariable $1: >$(strip $($1))<) \
266
$(info OldVariable $1: >$(strip $($1_old))<) \
268
$(call DependOnVariableWriteFile,$1) \
271
$(call DependOnVariableWriteFile,$1) \
277
# Param 1 - Name of variable
278
# Param 2 - (optional) name of file to store value in
280
$(call DependOnVariableHelper,$(strip $1),$(strip $2))
282
# LogCmdlines is only intended to be used by ExecuteWithLog
283
ifeq ($(LOG_CMDLINES), true)
284
LogCmdlines = $(info $(strip $1))
289
################################################################################
290
# ExecuteWithLog will run a command and log the output appropriately. This is
291
# meant to be used by commands that do "real" work, like a compilation.
292
# The output is stored in a specified log file, which is displayed at the end
293
# of the build in case of failure. The command line itself is stored in a file,
294
# and also logged to stdout if the LOG=cmdlines option has been given.
296
# NOTE: If the command redirects stdout, the caller needs to wrap it in a
297
# subshell (by adding parentheses around it), otherwise the redirect to the
298
# subshell tee process will create a race condition where the target file may
299
# not be fully written when the make recipe is done.
301
# Param 1 - The path to base the name of the log file / command line file on
302
# Param 2 - The command to run
304
$(call LogCmdlines, Executing: [$(strip $2)]) \
305
$(call MakeDir, $(dir $(strip $1)) $(MAKESUPPORT_OUTPUTDIR)/failure-logs) \
306
$(call WriteFile, $2, $(strip $1).cmdline) \
307
( $(RM) $(strip $1).log && $(strip $2) > >($(TEE) -a $(strip $1).log) 2> >($(TEE) -a $(strip $1).log >&2) || \
308
( exitcode=$(DOLLAR)? && \
309
$(CP) $(strip $1).log $(MAKESUPPORT_OUTPUTDIR)/failure-logs/$(subst /,_,$(patsubst $(OUTPUTDIR)/%,%,$(strip $1))).log && \
310
$(CP) $(strip $1).cmdline $(MAKESUPPORT_OUTPUTDIR)/failure-logs/$(subst /,_,$(patsubst $(OUTPUTDIR)/%,%,$(strip $1))).cmdline && \
311
exit $(DOLLAR)exitcode ) )
313
################################################################################
315
# Hook to include the corresponding custom file, if present.
316
$(eval $(call IncludeCustomExtension, common/MakeBase.gmk))