11#
2- # Copyright (c) 2011, 2025 , Oracle and/or its affiliates. All rights reserved.
2+ # Copyright (c) 2011, 2026 , Oracle and/or its affiliates. All rights reserved.
33# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44#
55# This code is free software; you can redistribute it and/or modify it
@@ -141,6 +141,66 @@ endef
141141# Make sure logging is setup for everyone that includes MakeBase.gmk.
142142$(eval $(call SetupLogging))
143143
144+ ################################################################################
145+ # Make does not have support for VARARGS, you can send variable amount
146+ # of arguments, but you can for example not append a list at the end.
147+ # It is therefore not easy to send the elements of a list of unknown
148+ # length as argument to a function. This can somewhat be worked around
149+ # by sending a list as an argument, and then interpreting each element
150+ # of the list as an argument to the function. However, Make is
151+ # limited, and using this method you can not easily send spaces.
152+ #
153+ # We need to quote strings for two reasons when sending them as
154+ # "variable append packs":
155+ #
156+ # 1) variable appends can include spaces, and those must be preserved
157+ # 2) variable appends can include assignment strings ":=", and those
158+ # must be quoted to a form so that we can recognise the "append pack".
159+ # We recognise an "append pack" by its lack of strict assignment ":="
160+
161+ Q := $(HASH)
162+ SpaceQ := $(Q)s
163+ AppendQ := $(Q)+
164+ AssignQ := $(Q)a
165+ QQ := $(Q)$(Q)
166+
167+ # $(call Quote,echo "#trala:=") -> echo#s"##trala#a"
168+ Quote = $(subst :=,$(AssignQ),$(subst $(SPACE),$(SpaceQ),$(subst $(Q),$(QQ),$1)))
169+
170+ # $(call Unquote,echo#s"##trala#a") -> echo "#trala:="
171+ Unquote = $(subst $(QQ),$(Q),$(subst $(SpaceQ),$(SPACE),$(subst $(AssignQ),:=,$1)))
172+
173+ # $(call QuoteAppend,name,some value) -> name#+some#svalue
174+ # $(call QuoteAppend,bad+=name,some value) -> error
175+ QuoteAppend = $(if $(findstring +=,$1),$(error you can not have += in a variable name: "$1"),$(call Quote,$1)$(AppendQ)$(call Quote,$2))
176+
177+ # $(call UnquoteAppendIndex,name#+some#svalue,1) -> name
178+ # $(call UnquoteAppendIndex,name#+some#svalue,2) -> some value
179+ UnquoteAppendIndex = $(call Unquote,$(word $2,$(subst $(AppendQ),$(SPACE),$1)))
180+
181+ # $(call FilterFiles,dir,%.cpp) -> file1.cpp file2.cpp (without path)
182+ FilterFiles = $(filter $2,$(notdir $(call FindFiles,$1)))
183+
184+ # $(call Unpack module_,file1.cpp_CXXFLAGS#+-Wconversion file2.cpp_CXXFLAGS#+-Wconversion) -> module_file1.cpp_CXXFLAGS += -Wconversion
185+ # module_file2.cpp_CXXFLAGS += -Wconversion
186+ Unpack = $(foreach pair,$2,$1$(call UnquoteAppendIndex,$(pair),1) += $(call UnquoteAppendIndex,$(pair),2)$(NEWLINE))
187+
188+ # This macro takes four arguments:
189+ # $1: directory where to find files (striped), example: $(TOPDIR)/src/hotspot/share/gc/g1
190+ # $2: filter to match what to keep (striped), example: g1Concurrent%.cpp
191+ # $3: what flags to override (striped), example: _CXXFLAGS
192+ # $4: what value to append to the flag (striped), example: $(CFLAGS_CONVERSION_WARNINGS)
193+ #
194+ # The result will be a quoted string that can be unpacked to a list of
195+ # variable appendings (see macro Unpack above). You do not need to take
196+ # care of unpacking, it is done in NamedParamsMacroTemplate.
197+ #
198+ # This feature should only be used for warnings that we want to
199+ # incrementally add to the rest of the code base.
200+ #
201+ # $(call ExtendFlags,dir,%.cpp,_CXXFLAGS,-Wconversion) -> file1.cpp_CXXFLAGS#+-Wconversion file2.cpp_CXXFLAGS#+-Wconversion
202+ ExtendFlags = $(foreach file,$(call FilterFiles,$(strip $1),$(strip $2)),$(call QuoteAppend,$(file)$(strip $3),$(strip $4)))
203+
144204################################################################################
145205
146206MAX_PARAMS := 96
@@ -166,7 +226,10 @@ define NamedParamsMacroTemplate
166226 Too many named arguments to macro, please update MAX_PARAMS in MakeBase.gmk))
167227 # Iterate over 2 3 4... and evaluate the named parameters with $1_ as prefix
168228 $(foreach i, $(PARAM_SEQUENCE), $(if $(strip $($i)), \
169- $(strip $1)_$(strip $(call EscapeHash, $(call DoubleDollar, $($i))))$(NEWLINE)))
229+ $(if $(findstring :=,$($i)), \
230+ $(strip $1)_$(strip $(call EscapeHash, $(call DoubleDollar, $($i))))$(NEWLINE), \
231+ $(call Unpack,$(strip $1)_,$($i)))))
232+
170233 # Debug print all named parameter names and values
171234 $(if $(findstring $(LOG_LEVEL), trace), \
172235 $(info $0 $(strip $1) $(foreach i, $(PARAM_SEQUENCE), \
0 commit comments