# # Copyright (c) 2011, 2026, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License version 2 only, as # published by the Free Software Foundation. Oracle designates this # particular file as subject to the "Classpath" exception as provided # by Oracle in the LICENSE file that accompanied this code. # # This code is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # version 2 for more details (a copy is included in the LICENSE file that # accompanied this code). # # You should have received a copy of the GNU General Public License version # 2 along with this work; if not, write to the Free Software Foundation, # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. # # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA # or visit www.oracle.com if you need additional information or have any # questions. # MAKEBASE_INCLUDED := true include MakeIncludeStart.gmk ifeq ($(INCLUDE), true) ################################################################################ # MakeBase provides the core functionality needed and used by all makefiles. It # should be included by all makefiles. MakeBase provides essential # functionality for named parameter functions, variable dependency, tool # execution, logging and fixpath functionality. ################################################################################ # By defining this pseudo target, make will automatically remove targets # if their recipe fails so that a rebuild is automatically triggered on the # next make invocation. .DELETE_ON_ERROR: ################################################################################ # Definitions for special characters ################################################################################ # When calling macros, the spaces between arguments are # often semantically important! Sometimes we need to subst # spaces and commas, therefore we need the following macros. X := SPACE := $(X) $(X) COMMA := , DOLLAR := $$ HASH := \# LEFT_PAREN := ( RIGHT_PAREN := ) SQUOTE := ' #' DQUOTE := " #" define NEWLINE endef # Certain features only work in newer version of GNU Make. The build will still # function in 3.81, but will be less performant. ifeq (4.0, $(firstword $(sort 4.0 $(MAKE_VERSION)))) HAS_FILE_FUNCTION := true CORRECT_FUNCTION_IN_RECIPE_EVALUATION := true RWILDCARD_WORKS := true endif # For convenience, MakeBase.gmk continues to include these separate files, at # least for now. # Utils.gmk must be included before FileUtils.gmk, since it uses some of the # basic utility functions there. include $(TOPDIR)/make/common/Utils.gmk include $(TOPDIR)/make/common/FileUtils.gmk ################################################################################ # Make sure we have a value (could be overridden on command line by caller) CREATING_BUILDJDK ?= false ################################################################################ define SetupLogging ifeq ($$(LOG_PROFILE_TIMES_FILE), true) ifeq ($$(IS_GNU_TIME), yes) SHELL := $$(BASH) $$(TOPDIR)/make/scripts/shell-profiler.sh \ gnutime $$(TIME) \ $$(OUTPUTDIR)/build-profile.log $$(SHELL) else ifneq ($$(FLOCK), ) SHELL := $$(BASH) $$(TOPDIR)/make/scripts/shell-profiler.sh \ flock $$(FLOCK) \ $$(OUTPUTDIR)/build-profile.log $$(SHELL) endif endif ifneq ($$(findstring $$(LOG_LEVEL), debug trace), ) SHELL := $$(SHELL) -x endif ifeq ($$(LOG_LEVEL), trace) SHELL_NO_RECURSE := $$(SHELL) # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make # For each target executed, will print # Building (from ) ( newer) # but with a limit of 20 on , to avoid cluttering logs too much # (and causing a crash on Cygwin). 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 endif # The LOG_PREFIX is set for sub recursive calls like buildjdk and bootcycle. # The warn level can never be turned off LogWarn = $$(info $(LOG_PREFIX)$$(strip $$1)) LOG_WARN := ifneq ($$(findstring $$(LOG_LEVEL), info debug trace), ) LogInfo = $$(info $(LOG_PREFIX)$$(strip $$1)) LOG_INFO := else LogInfo = LOG_INFO := > /dev/null endif ifneq ($$(findstring $$(LOG_LEVEL), debug trace), ) LogDebug = $$(info $(LOG_PREFIX)$$(strip $$1)) LOG_DEBUG := else LogDebug = LOG_DEBUG := > /dev/null endif ifneq ($$(findstring $$(LOG_LEVEL), trace), ) LogTrace = $$(info $(LOG_PREFIX)$$(strip $$1)) LOG_TRACE := else LogTrace = LOG_TRACE := > /dev/null endif endef # Make sure logging is setup for everyone that includes MakeBase.gmk. $(eval $(call SetupLogging)) ################################################################################ # Make does not have support for VARARGS, you can send variable amount # of arguments, but you can for example not append a list at the end. # It is therefore not easy to send the elements of a list of unknown # length as argument to a function. This can somewhat be worked around # by sending a list as an argument, and then interpreting each element # of the list as an argument to the function. However, Make is # limited, and using this method you can not easily send spaces. # # We need to quote strings for two reasons when sending them as # "variable append packs": # # 1) variable appends can include spaces, and those must be preserved # 2) variable appends can include assignment strings ":=", and those # must be quoted to a form so that we can recognise the "append pack". # We recognise an "append pack" by its lack of strict assignment ":=" Q := $(HASH) SpaceQ := $(Q)s AppendQ := $(Q)+ AssignQ := $(Q)a QQ := $(Q)$(Q) # $(call Quote,echo "#trala:=") -> echo#s"##trala#a" Quote = $(subst :=,$(AssignQ),$(subst $(SPACE),$(SpaceQ),$(subst $(Q),$(QQ),$1))) # $(call Unquote,echo#s"##trala#a") -> echo "#trala:=" Unquote = $(subst $(QQ),$(Q),$(subst $(SpaceQ),$(SPACE),$(subst $(AssignQ),:=,$1))) # $(call QuoteAppend,name,some value) -> name#+some#svalue # $(call QuoteAppend,bad+=name,some value) -> error QuoteAppend = $(if $(findstring +=,$1),$(error you can not have += in a variable name: "$1"),$(call Quote,$1)$(AppendQ)$(call Quote,$2)) # $(call UnquoteAppendIndex,name#+some#svalue,1) -> name # $(call UnquoteAppendIndex,name#+some#svalue,2) -> some value UnquoteAppendIndex = $(call Unquote,$(word $2,$(subst $(AppendQ),$(SPACE),$1))) # $(call FilterFiles,dir,%.cpp) -> file1.cpp file2.cpp (without path) FilterFiles = $(filter $2,$(notdir $(call FindFiles,$1))) # $(call Unpack module_,file1.cpp_CXXFLAGS#+-Wconversion file2.cpp_CXXFLAGS#+-Wconversion) -> module_file1.cpp_CXXFLAGS += -Wconversion # module_file2.cpp_CXXFLAGS += -Wconversion Unpack = $(foreach pair,$2,$1$(call UnquoteAppendIndex,$(pair),1) += $(call UnquoteAppendIndex,$(pair),2)$(NEWLINE)) # This macro takes four arguments: # $1: directory where to find files (striped), example: $(TOPDIR)/src/hotspot/share/gc/g1 # $2: filter to match what to keep (striped), example: g1Concurrent%.cpp # $3: what flags to override (striped), example: _CXXFLAGS # $4: what value to append to the flag (striped), example: $(CFLAGS_CONVERSION_WARNINGS) # # The result will be a quoted string that can be unpacked to a list of # variable appendings (see macro Unpack above). You do not need to take # care of unpacking, it is done in NamedParamsMacroTemplate. # # This feature should only be used for warnings that we want to # incrementally add to the rest of the code base. # # $(call ExtendFlags,dir,%.cpp,_CXXFLAGS,-Wconversion) -> file1.cpp_CXXFLAGS#+-Wconversion file2.cpp_CXXFLAGS#+-Wconversion ExtendFlags = $(foreach file,$(call FilterFiles,$(strip $1),$(strip $2)),$(call QuoteAppend,$(file)$(strip $3),$(strip $4))) ################################################################################ MAX_PARAMS := 96 PARAM_SEQUENCE := $(call sequence, 2, $(MAX_PARAMS)) # Template for creating a macro taking named parameters. To use it, assign the # template to a variable with the name you want for your macro, using '=' # assignment. Then define a macro body with the suffix "Body". The Body macro # should take 1 parameter which should be a unique string for that invocation # of the macro. # Ex: # SetupFoo = $(NamedParamsMacroTemplate) # define SetupFooBody # # do something # # access parameters as $$($1_BAR) # endef # Call it like this # $(eval $(call SetupFoo, BUILD_SOMETHING, \ # BAR := some parameter value, \ # )) define NamedParamsMacroTemplate $(if $($(MAX_PARAMS)), $(error Internal makefile error: \ Too many named arguments to macro, please update MAX_PARAMS in MakeBase.gmk)) # Iterate over 2 3 4... and evaluate the named parameters with $1_ as prefix $(foreach i, $(PARAM_SEQUENCE), $(if $(strip $($i)), \ $(if $(findstring :=,$($i)), \ $(strip $1)_$(strip $(call EscapeHash, $(call DoubleDollar, $($i))))$(NEWLINE), \ $(call Unpack,$(strip $1)_,$($i))))) # Debug print all named parameter names and values $(if $(findstring $(LOG_LEVEL), trace), \ $(info $0 $(strip $1) $(foreach i, $(PARAM_SEQUENCE), \ $(if $(strip $($i)),$(NEWLINE) $(strip [$i] $(if $(filter $(LOG_LEVEL), trace), \ $($i), $(wordlist 1, 20, $($(i))) $(if $(word 21, $($(i))), ...))))))) $(if $(DEBUG_$(strip $1)), $(info -------- <<< Begin expansion of $(strip $1)) \ $(info $(call $(0)Body,$(strip $1))) \ $(info -------- >>> End expansion of $(strip $1)) \ ) $(call $(0)Body,$(strip $1)) endef ################################################################################ # FixPath # # On Windows, converts a path from cygwin/unix style (e.g. /bin/foo) into # "mixed mode" (e.g. c:/cygwin/bin/foo). On other platforms, return the path # unchanged. # This also converts a colon-separated list of paths to a semicolon-separated # list. # This is normally not needed since we use the FIXPATH prefix for command lines, # but might be needed in certain circumstances. # # FixPathFile is the file version of FixPath. It instead takes a file with paths in $1 # and outputs the 'fixed' paths into the file in $2. If the file in $2 already exists # it is overwritten. # On non-Windows platforms this instead does a copy, so that $2 can still be used # as a depenendency of a make rule, instead of having to conditionally depend on # $1 instead, based on the target platform. ifeq ($(call isTargetOs, windows), true) FixPath = \ $(strip $(subst \,\\, $(shell $(FIXPATH_BASE) print $(patsubst $(FIXPATH), , $1)))) FixPathFile = \ $(shell $(FIXPATH_BASE) convert $1 $2) else FixPath = \ $1 FixPathFile = \ $(shell $(CP) $1 $2) endif ################################################################################ # DependOnVariable # # This macro takes a variable name and puts the value in a file only if the # value has changed since last. The name of the file is returned. This can be # used to create rule dependencies on make variable values. The following # example would get rebuilt if the value of SOME_VAR was changed: # # path/to/some-file: $(call DependOnVariable, SOME_VAR) # echo $(SOME_VAR) > $@ # # Note that leading and trailing white space in the value is ignored. # # Defines the sub directory structure to store variable value file in DependOnVariableDirName = \ $(strip $(addsuffix $(if $(MODULE),/$(MODULE)), \ $(subst $(WORKSPACE_ROOT)/,, $(if $(filter /%, $(firstword $(MAKEFILE_LIST))), \ $(firstword $(MAKEFILE_LIST)), \ $(CURDIR)/$(firstword $(MAKEFILE_LIST)))))) # Defines the name of the file to store variable value in. Generates a name # unless parameter 2 is given. # Param 1 - Name of variable # Param 2 - (optional) name of file to store value in DependOnVariableFileName = \ $(strip $(if $(strip $2), $2, \ $(MAKESUPPORT_OUTPUTDIR)/vardeps/$(DependOnVariableDirName)/$(strip $1).vardeps)) # Writes the vardeps file. Assumes $1_filename has been setup # Param 1 - Name of variable DependOnVariableWriteFile = \ $(call MakeDir, $(dir $($1_filename))) \ $(call WriteFile, $1_old := $(call DoubleDollar,$(call EscapeHash,$($1))), \ $($1_filename)) \ # Does the actual work with parameters stripped. # If the file exists AND the contents is the same as the variable, do nothing # else print a new file. # Always returns the name of the file where the value was printed. # Param 1 - Name of variable # Param 2 - (optional) name of file to store value in DependOnVariableHelper = \ $(strip \ $(eval $1_filename := $(call DependOnVariableFileName, $1, $2)) \ $(if $(wildcard $($1_filename)), \ $(eval include $($1_filename)) \ $(if $(call equals, $(strip $($1)), $(strip $($1_old))),, \ $(if $(findstring $(LOG_LEVEL), trace), \ $(info NewVariable $1: >$(strip $($1))<) \ $(info OldVariable $1: >$(strip $($1_old))<) \ ) \ $(call DependOnVariableWriteFile,$1) \ ) \ , \ $(call DependOnVariableWriteFile,$1) \ ) \ $($1_filename) \ ) # Main macro # Param 1 - Name of variable # Param 2 - (optional) name of file to store value in DependOnVariable = \ $(call DependOnVariableHelper,$(strip $1),$(strip $2)) # LogCmdlines is only intended to be used by ExecuteWithLog ifeq ($(LOG_CMDLINES), true) LogCmdlines = $(info $(strip $1)) else LogCmdlines = endif # Check if the command line contains redirection, that is <, > or >>, # and if so, return a value that is interpreted as true in a make $(if) # construct. is_redirect = \ $(if $(filter < > >>, $1), true) ################################################################################ # ExecuteWithLog will run a command and log the output appropriately. This is # meant to be used by commands that do "real" work, like a compilation. # The output is stored in a specified log file, which is displayed at the end # of the build in case of failure. The command line itself is stored in a file, # and also logged to stdout if the LOG=cmdlines option has been given. # # Param 1 - The path to base the name of the log file / command line file on # Param 2 - The command to run ExecuteWithLog = \ $(call LogCmdlines, Executing: \ [$(if $(call is_redirect, $2),$(LEFT_PAREN) )$(strip $2)$(if $(call \ is_redirect, $2), $(RIGHT_PAREN))]) \ $(call MakeDir, $(dir $(strip $1)) $(MAKESUPPORT_OUTPUTDIR)/failure-logs) \ $(call WriteFile, $2, $(strip $1).cmdline) \ ( $(RM) $(strip $1).log && \ $(if $(call is_redirect, $2),$(LEFT_PAREN) )$(strip $2)$(if $(call \ is_redirect, $2), $(RIGHT_PAREN)) \ > >($(TEE) -a $(strip $1).log) 2> >($(TEE) -a $(strip $1).log >&2) || \ ( exitcode=$(DOLLAR)? && \ $(CP) $(strip $1).log $(MAKESUPPORT_OUTPUTDIR)/failure-logs/$(subst \ /,_,$(patsubst $(OUTPUTDIR)/%,%,$(strip $1))).log && \ $(CP) $(strip $1).cmdline $(MAKESUPPORT_OUTPUTDIR)/failure-logs/$(subst \ /,_,$(patsubst $(OUTPUTDIR)/%,%,$(strip $1))).cmdline && \ exit $(DOLLAR)exitcode ) ) ################################################################################ include MakeIncludeEnd.gmk endif # include guard