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
ifeq (,$(_MAKEBASE_GMK))
27
$(error You must include MakeBase.gmk prior to including Utils.gmk)
30
################################################################################
32
# Basic utility functions available to MakeBase.gmk itself
34
################################################################################
38
$(if $(strip $1)$(strip $2),$(strip \
39
$(and $(findstring $(strip $1),$(strip $2)),\
40
$(findstring $(strip $2),$(strip $1)))), \
44
# Convert the string given to upper case, without any $(shell)
45
# Inspired by http://lists.gnu.org/archive/html/help-make/2013-09/msg00009.html
46
uppercase_table := a,A b,B c,C d,D e,E f,F g,G h,H i,I j,J k,K l,L m,M n,N o,O \
47
p,P q,Q r,R s,S t,T u,U v,V w,W x,X y,Y z,Z
50
$(if $(strip $1), $$(subst $(firstword $1), $(call uppercase_internal, \
51
$(wordlist 2, $(words $1), $1), $2)), $2)
53
# Convert a string to upper case. Works only on a-z.
54
# $1 - The string to convert
57
$(eval uppercase_result := $(call uppercase_internal, $(uppercase_table), $1)) \
61
################################################################################
62
# Creates a sequence of increasing numbers (inclusive).
63
# Param 1 - starting number
64
# Param 2 - ending number
66
$(wordlist $1, $2, $(strip \
67
$(eval SEQUENCE_COUNT :=) \
68
$(call _sequence-do,$(strip $2))))
71
$(if $(word $1, $(SEQUENCE_COUNT)),, \
72
$(eval SEQUENCE_COUNT += .) \
73
$(words $(SEQUENCE_COUNT)) \
74
$(call _sequence-do,$1))
76
################################################################################
77
# This macro translates $ into \$ to protect the $ from expansion in the shell.
78
# To make this macro resilient against already escaped strings, first remove
79
# any present escapes before escaping so that no double escapes are added.
80
EscapeDollar = $(subst $$,\$$,$(subst \$$,$$,$(strip $1)))
82
################################################################################
83
# This macro works just like EscapeDollar above, but for #.
84
EscapeHash = $(subst \#,\\\#,$(subst \\\#,\#,$(strip $1)))
86
################################################################################
87
# This macro translates $ into $$ to protect the string from make itself.
88
DoubleDollar = $(subst $$,$$$$,$(strip $1))
90
################################################################################
93
# Quotes a string with single quotes and replaces single quotes with '\'' so
94
# that the contents survives being given to the shell.
96
$(SQUOTE)$(subst $(SQUOTE),$(SQUOTE)\$(SQUOTE)$(SQUOTE),$(strip $1))$(SQUOTE)
98
################################################################################
99
# Write to and read from file
101
# Param 1 - File to read
105
# Param 1 - Text to write
106
# Param 2 - File to write to
107
ifeq ($(HAS_FILE_FUNCTION), true)
109
$(file >$2,$(strip $1))
111
# Use printf to get consistent behavior on all platforms.
113
$(shell $(PRINTF) "%s\n" $(strip $(call ShellQuote, $1)) > $2)
116
# Param 1 - Text to write
117
# Param 2 - File to write to
118
ifeq ($(HAS_FILE_FUNCTION), true)
120
$(file >>$2,$(strip $1))
122
# Use printf to get consistent behavior on all platforms.
124
$(shell $(PRINTF) "%s\n" $(strip $(call ShellQuote, $1)) >> $2)
127
################################################################################
128
# Make directory without forking mkdir if not needed.
130
# If a directory with an encoded space is provided, the wildcard function
131
# sometimes returns false answers (typically if the dir existed when the
132
# makefile was parsed, but was deleted by a previous rule). In that case, always
133
# call mkdir regardless of what wildcard says.
135
# 1: List of directories to create
138
$(eval MakeDir_dirs_to_make := $(strip $(foreach d, $1, \
139
$(if $(findstring ?, $d), '$(call DecodeSpace, $d)', \
140
$(if $(wildcard $d), , $d) \
143
$(if $(MakeDir_dirs_to_make), $(shell $(MKDIR) -p $(MakeDir_dirs_to_make))) \
146
################################################################################
147
# Check if our build or target conforms to certain restrictions. This set of
148
# functions all work in similar ways, testing the property that the name
149
# implies, so e.g. isTargetCpu test the CPU of the target system.
151
# $1 - A property, or a space separated list of properties to test for.
153
# Returns true if the actual property matches one of the properties in the list,
154
# and false otherwise.
156
# Examples: $(call isTargetOs, linux windows) will return true when executed
157
# on either linux or windows, and false otherwise.
158
# $(call isBuildCpuArch, x86) will return true iff the build CPU Arch is x86.
161
$(strip $(if $(filter $(OPENJDK_TARGET_OS), $1), true, false))
164
$(strip $(if $(filter $(OPENJDK_TARGET_OS_TYPE), $1), true, false))
167
$(strip $(if $(filter $(OPENJDK_TARGET_CPU), $1), true, false))
170
$(strip $(if $(filter $(OPENJDK_TARGET_CPU_ARCH), $1), true, false))
173
$(strip $(if $(filter $(OPENJDK_TARGET_CPU_BITS), $1), true, false))
176
$(strip $(if $(filter $(OPENJDK_BUILD_OS), $1), true, false))
179
$(strip $(if $(filter $(OPENJDK_BUILD_OS_TYPE), $1), true, false))
182
$(strip $(if $(filter $(OPENJDK_BUILD_OS_ENV), $1), true, false))
185
$(strip $(if $(filter $(OPENJDK_BUILD_CPU), $1), true, false))
188
$(strip $(if $(filter $(OPENJDK_BUILD_CPU_ARCH), $1), true, false))
191
$(strip $(if $(filter $(TOOLCHAIN_TYPE), $1), true, false))
193
################################################################################
195
# Common utility functions
197
################################################################################
201
# Prints the name and value of a variable
203
$(info $(strip $1) >$($(strip $1))<)
206
################################################################################
207
# Strip both arguments. Append the first argument to the second argument. If the
208
# first argument is empty, return the empty string.
210
$(if $(strip $1),$(strip $1)$(strip $2),)
212
################################################################################
213
# Assign a variable only if it is empty
214
# Param 1 - Variable to assign
215
# Param 2 - Value to assign
217
$(if $($(strip $1)),,$(eval $(strip $1) := $2))
219
################################################################################
220
# Filter out duplicate sub strings while preserving order. Keeps the first occurrence.
222
$(strip $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1))))
224
# Returns all whitespace-separated words in $2 where at least one of the
225
# whitespace-separated words in $1 is a substring.
228
$(foreach v,$(strip $2),\
229
$(call uniq,$(foreach p,$(strip $1),$(if $(findstring $p,$v),$v)))))
231
# Returns all whitespace-separated words in $2 where none of the
232
# whitespace-separated words in $1 is a substring.
234
$(strip $(filter-out $(call containing,$1,$2),$2))
236
# Return a list of all string elements that are duplicated in $1.
238
$(strip $(foreach v, $(sort $1), $(if $(filter-out 1, \
239
$(words $(filter $v, $1))), $v)))
241
# Remove a whole list of prefixes
242
# $1 - List of prefixes
243
# $2 - List of elements to process
245
$(strip $(if $1,$(patsubst $(firstword $1)%,%,\
246
$(call remove-prefixes,$(filter-out $(firstword $1),$1),$2)),$2))
248
################################################################################
251
# Return the word "true" if all the boolean words given as argument is "true",
252
# and returns "false" otherwise. Boolean words must be "true" or "false". It is
253
# an error to supply a non-boolean word. An empty string is considered "true".
255
$(strip $(if $(filter-out true false, $1), $(error Non-boolean values: $1)) \
256
$(if $(strip $(filter-out true, $1)), false, true))
258
# Return the word "false" if all the boolean words given as argument is "false",
259
# and returns "true" otherwise. Boolean words must be "true" or "false". It is
260
# an error to supply a non-boolean word. An empty string is considered "false".
262
$(strip $(if $(filter-out true false, $1), $(error Non-boolean values: $1)) \
263
$(if $(strip $(filter-out false, $1)), true, false))
266
################################################################################
267
# Convert an UNIX epoch based timestamp (as an integer) to an ISO 8601 date
270
ifeq ($(IS_GNU_DATE), yes)
272
$(shell $(DATE) --utc --date="@$(strip $1)" \
273
+"$(ISO_8601_FORMAT_STRING)" 2> /dev/null)
276
$(shell $(DATE) -u -j -f "%s" "$(strip $1)" \
277
+"$(ISO_8601_FORMAT_STRING)" 2> /dev/null)
280
################################################################################
281
# Parse a multiple-keyword variable, like FOO="KEYWORD1=val1;KEYWORD2=val2;..."
282
# These will be converted into a series of variables like FOO_KEYWORD1=val1,
283
# FOO_KEYWORD2=val2, etc. Unknown keywords will cause an error.
285
# Parameter 1 is the name of the rule, and is also the name of the variable.
287
# Remaining parameters are named arguments. These include:
288
# SINGLE_KEYWORDS A list of valid keywords with single string values
289
# STRING_KEYWORDS A list of valid keywords, processed as string. This means
290
# that '%20' will be replaced by ' ' to allow for multi-word strings.
292
ParseKeywordVariable = $(NamedParamsMacroTemplate)
293
define ParseKeywordVariableBody
295
# To preserve spaces, substitute them with a hopefully unique pattern
296
# before splitting and then re-substitute spaces back.
297
$1_MANGLED := $$(subst $$(SPACE),||||,$$($1))
298
$$(foreach mangled_part, $$(subst ;, , $$($1_MANGLED)), \
299
$$(eval mangled_part_eval := $$(call DoubleDollar, $$(mangled_part))) \
300
$$(eval part := $$$$(subst ||||,$$$$(SPACE),$$$$(mangled_part_eval))) \
301
$$(eval $1_NO_MATCH := true) \
302
$$(if $$(filter help, $$(part)), \
303
$$(info Valid keywords for $1:) \
304
$$(info $$($1_SINGLE_KEYWORDS) $$($1_STRING_KEYWORDS).) \
305
$$(error Re-run without 'help' to continue)) \
306
$$(foreach keyword, $$($1_SINGLE_KEYWORDS), \
307
$$(eval keyword_eval := $$(call DoubleDollar, $$(keyword))) \
308
$$(if $$(filter $$(keyword)=%, $$(part)), \
309
$$(eval $(strip $1)_$$$$(keyword_eval) := $$$$(strip $$$$(patsubst $$$$(keyword_eval)=%, %, $$$$(part)))) \
310
$$(eval $1_NO_MATCH := ) \
313
$$(foreach keyword, $$($1_STRING_KEYWORDS), \
314
$$(eval keyword_eval := $$(call DoubleDollar, $$(keyword))) \
315
$$(if $$(filter $$(keyword)=%, $$(part)), \
316
$$(eval $(strip $1)_$$$$(keyword_eval) := $$$$(strip $$$$(subst %20, , $$$$(patsubst $$$$(keyword_eval)=%, %, $$$$(part))))) \
317
$$(eval $1_NO_MATCH := ) \
320
$$(if $$($1_NO_MATCH), \
321
$$(if $$(filter $$(part), $$($1_SINGLE_KEYWORDS) $$($1_STRING_KEYWORDS)), \
322
$$(info Keyword $$(part) for $1 needs to be assigned a value.) \
324
$$(info $$(part) is not a valid keyword for $1.) \
325
$$(info Valid keywords: $$($1_SINGLE_KEYWORDS) $$($1_STRING_KEYWORDS).) \
327
$$(error Cannot continue) \
333
################################################################################
334
# Find lib dir for module
335
# Param 1 - module name
336
FindLibDirForModule = \
337
$(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)
339
################################################################################
340
# Find executable dir for module
341
# Param 1 - module name
342
FindExecutableDirForModule = \
343
$(SUPPORT_OUTPUTDIR)/modules_cmds/$(strip $1)
345
################################################################################
346
# Return a string suitable for use after a -classpath or --module-path option. It
347
# will be correct and safe to use on all platforms. Arguments are given as space
348
# separate classpath entries. Safe for multiple nested calls.
349
# param 1 : A space separated list of classpath entries
350
# The surrounding strip is needed to keep additional whitespace out
352
"$(subst $(SPACE),:,$(strip $(subst $(DQUOTE),,$1)))"
354
################################################################################
355
# Check if a specified hotspot variant is being built, or at least one of a
356
# list of variants. Will return 'true' or 'false'.
357
# $1 - the variant to test for
360
$(if $(filter-out $(VALID_JVM_VARIANTS), $1), \
361
$(error Internal error: Invalid variant tested: $1)) \
362
$(if $(filter $1, $(JVM_VARIANTS)), true, false))
364
################################################################################
365
# Converts a space separated list to a comma separated list.
367
# Replacing double-comma with a single comma is to workaround the issue with
368
# some version of make on windows that doesn't substitute spaces with one comma
372
$(subst $(COMMA)$(COMMA),$(COMMA),$(subst $(SPACE),$(COMMA),$(strip $1))) \
375
################################################################################
376
# Converts a space separated list to a colon separated list.
378
# Replacing double-colon with a single colon is to workaround the issue with
379
# some version of make on windows that doesn't substitute spaces with one colon
383
$(subst ::,:,$(subst $(SPACE),:,$(strip $1))) \
386
################################################################################
387
# Given a list of files, filters out locale specific files for translations
388
# that should be excluded from this build.
389
# $1 - The list of files to filter
390
# $2 - The suffix of the files that should be considered (.java or .properties)
391
FilterExcludedTranslations = \
392
$(strip $(if $(EXCLUDE_TRANSLATIONS), \
394
$(foreach suffix, $2, \
395
$(addprefix %_, $(addsuffix $(suffix), $(EXCLUDE_TRANSLATIONS))) \