MakeBase.gmk revision 2024:cc16e2a24f94
1151497Sru#
275584Sru# Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
375584Sru# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
475584Sru#
575584Sru# This code is free software; you can redistribute it and/or modify it
675584Sru# under the terms of the GNU General Public License version 2 only, as
775584Sru# published by the Free Software Foundation.  Oracle designates this
875584Sru# particular file as subject to the "Classpath" exception as provided
975584Sru# by Oracle in the LICENSE file that accompanied this code.
10151497Sru#
11151497Sru# This code is distributed in the hope that it will be useful, but WITHOUT
12151497Sru# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13151497Sru# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14151497Sru# version 2 for more details (a copy is included in the LICENSE file that
15151497Sru# accompanied this code).
16151497Sru#
17151497Sru# You should have received a copy of the GNU General Public License version
18151497Sru# 2 along with this work; if not, write to the Free Software Foundation,
19151497Sru# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20151497Sru#
21151497Sru# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22151497Sru# or visit www.oracle.com if you need additional information or have any
23151497Sru# questions.
24151497Sru#
25151497Sru
26151497Sru################################################################
27151497Sru#
28151497Sru# Setup common utility functions.
29151497Sru#
30151497Sru################################################################
31151497Sru
32151497Sruifndef _MAKEBASE_GMK
33151497Sru_MAKEBASE_GMK := 1
34151497Sru
35151497Sruifeq ($(wildcard $(SPEC)),)
36151497Sru  $(error MakeBase.gmk needs SPEC set to a proper spec.gmk)
37151497Sruendif
38151497Sru
39151497Sru# By defining this pseudo target, make will automatically remove targets
40151497Sru# if their recipe fails so that a rebuild is automatically triggered on the
41151497Sru# next make invocation.
42151497Sru.DELETE_ON_ERROR:
43151497Sru
44151497Sru################################################################################
45151497Sru# Definitions for special characters
46151497Sru################################################################################
47151497Sru
48151497Sru# When calling macros, the spaces between arguments are
49151497Sru# often semantically important! Sometimes we need to subst
50151497Sru# spaces and commas, therefore we need the following macros.
51151497SruX:=
52151497SruSPACE:=$(X) $(X)
53151497SruCOMMA:=,
54151497SruDOLLAR:=$$
55151497SruHASH:=\#
56151497SruLEFT_PAREN:=(
57151497SruRIGHT_PAREN:=)
58151497SruSQUOTE:='
59151497Sru#'
60151497SruDQUOTE:="
61151497Sru#"
62151497Srudefine NEWLINE
63151497Sru
64151497Sru
65151497Sruendef
66151497Sru
67151497Sru# In GNU Make 4.0 and higher, there is a file function for writing to files.
68151497Sruifeq (4.0, $(firstword $(sort 4.0 $(MAKE_VERSION))))
69151497Sru  HAS_FILE_FUNCTION := true
70151497Sruendif
71151497Sru
72151497Sru##############################
73151497Sru# Functions
74151497Sru##############################
75151497Sru
76151497Sru### Debug functions
77151497Sru
78151497Sru# Prints the name and value of a variable
79151497SruPrintVar = \
80151497Sru    $(info $(strip $1) >$($(strip $1))<)
81151497Sru
82151497Sru### Functions for timers
83151497Sru
84151497Sru# Store the build times in this directory.
85151497SruBUILDTIMESDIR=$(OUTPUT_ROOT)/make-support/build-times
86151497Sru
87151497Sru# Record starting time for build of a sub repository.
88151497Srudefine RecordStartTime
89151497Sru	$(DATE) '+%Y %m %d %H %M %S' | $(NAWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_start_$(strip $1) && \
90151497Sru	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$(strip $1)_human_readable
91151497Sruendef
92151497Sru
93151497Sru# Record ending time and calculate the difference and store it in a
94151497Sru# easy to read format. Handles builds that cross midnight. Expects
95151497Sru# that a build will never take 24 hours or more.
96151497Srudefine RecordEndTime
97151497Sru	$(DATE) '+%Y %m %d %H %M %S' | $(NAWK) '{ print $$1,$$2,$$3,$$4,$$5,$$6,($$4*3600+$$5*60+$$6) }' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)
98151497Sru	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)_human_readable
99151497Sru	$(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$(strip $1)` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$(strip $1)` $1 | \
100151497Sru	    $(NAWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \
101151497Sru	    M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \
102151497Sru	    > $(BUILDTIMESDIR)/build_time_diff_$(strip $1)
103151497Sruendef
104151497Sru
105151497Sru# Hook to be called when starting to execute a top-level target
106151497Srudefine TargetEnter
107151497Sru	$(PRINTF) "## Starting $(patsubst %-only,%,$@)\n"
108151497Sru	$(call RecordStartTime,$(patsubst %-only,%,$@))
109151497Sruendef
110151497Sru
111151497Sru# Hook to be called when finish executing a top-level target
112151497Srudefine TargetExit
113151497Sru	$(call RecordEndTime,$(patsubst %-only,%,$@))
114151497Sru	$(PRINTF) "## Finished $(patsubst %-only,%,$@) (build time %s)\n\n" \
115151497Sru	    "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_$(patsubst %-only,%,$@) | $(CUT) -f 1 -d ' '`"
116151497Sruendef
117151497Sru
118151497Sru################################################################################
119151497Sru# This macro translates $ into \$ to protect the $ from expansion in the shell.
120151497Sru# To make this macro resilient against already escaped strings, first remove
121151497Sru# any present escapes before escaping so that no double escapes are added.
122151497SruEscapeDollar = $(subst $$,\$$,$(subst \$$,$$,$(strip $1)))
123151497Sru
124151497Sru################################################################################
125151497Sru# This macro translates $ into $$ to protect the string from make itself.
126151497SruDoubleDollar = $(subst $$,$$$$,$(strip $1))
127151497Sru
128151497Sru################################################################################
129151497Sru# ListPathsSafely can be used to print command parameters to a file. This is
130151497Sru# typically done if the command line lenght risk being too long for the
131151497Sru# OS/shell. In later make versions, the file function can be used for this
132151497Sru# purpose. For earlier versions, a more complex implementation is provided.
133151497Sru#
134151497Sru# The function ListPathsSafely can be called either directly or, more commonly
135151497Sru# from a recipe line. If called from a recipe, it will be executed in the
136151497Sru# evaluation phase of that recipe, which means that it will write to the file
137151497Sru# before any other line in the recipe has been run.
138151497Sruifeq ($(HAS_FILE_FUNCTION), true)
139151497Sru  # Param 1 - Name of variable containing paths/arguments to output
140151497Sru  # Param 2 - File to print to
141151497Sru  # Param 3 - Set to true to append to file instead of overwriting
142151497Sru  define ListPathsSafely
143151497Sru    $$(call MakeDir, $$(dir $$(strip $2)))
144151497Sru    $$(file $$(if $$(filter true, $$(strip $3)),>>,>) \
145151497Sru        $$(strip $2),$$(subst $$(SPACE),$$(NEWLINE),$$(strip $$($$(strip $1)))))
146151497Sru  endef
147151497Sru
148151497Sruelse # HAS_FILE_FUNCTION
149151497Sru
150151497Sru  $(eval compress_paths = \
151151497Sru      $(strip $(shell $(CAT) $(SRC_ROOT)/make/common/support/ListPathsSafely-pre-compress.incl)))
152151497Sru  compress_paths += \
153151497Sru      $(subst $(SRC_ROOT),X97, \
154151497Sru      $(subst $(OUTPUT_ROOT),X98, \
155151497Sru      $(subst X,X00, \
156151497Sru      $(subst $(SPACE),\n,$(strip $1)))))
157151497Sru  $(eval compress_paths += \
158151497Sru      $(strip $(shell $(CAT) $(SRC_ROOT)/make/common/support/ListPathsSafely-post-compress.incl)))
159151497Sru
160151497Sru  decompress_paths=$(SED) -f $(SRC_ROOT)/make/common/support/ListPathsSafely-uncompress.sed \
161151497Sru      -e 's|X99|\\n|g' \
162151497Sru      -e 's|X98|$(OUTPUT_ROOT)|g' -e 's|X97|$(SRC_ROOT)|g' \
163151497Sru      -e 's|X00|X|g'
164151497Sru
165151497Sru  ListPathsSafely_IfPrintf = \
166151497Sru      $(if $(word $3,$($(strip $1))), \
167151497Sru          $(shell $(PRINTF) -- "$(strip $(call EscapeDollar, \
168151497Sru              $(call compress_paths, $(wordlist $3,$4,$($(strip $1))))))\n" \
169151497Sru              | $(decompress_paths) >> $2))
170151497Sru
171151497Sru  # Param 1 - Name of variable containing paths/arguments to output
172151497Sru  # Param 2 - File to print to
173151497Sru  # Param 3 - Set to true to append to file instead of overwriting
174151497Sru  define ListPathsSafely
175151497Sru    ifneq (,$$(word 10001,$$($1)))
176151497Sru      $$(error Cannot list safely more than 10000 paths. $1 has $$(words $$($1)) paths!)
177151497Sru    endif
178151497Sru    $$(call MakeDir, $$(dir $2))
179151497Sru    ifneq ($$(strip $3), true)
180151497Sru      $$(shell $(RM) $$(strip $2))
181151497Sru    endif
182151497Sru
183151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,1,250)
184151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,251,500)
185151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,501,750)
186151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,751,1000)
187151497Sru
188151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,1001,1250)
189151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,1251,1500)
190151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,1501,1750)
191151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,1751,2000)
192151497Sru
193151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,2001,2250)
194151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,2251,2500)
195151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,2501,2750)
196151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,2751,3000)
197151497Sru
198151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,3001,3250)
199151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,3251,3500)
200151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,3501,3750)
201151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,3751,4000)
202151497Sru
203151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,4001,4250)
204151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,4251,4500)
205151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,4501,4750)
206151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,4751,5000)
207151497Sru
208151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,5001,5250)
209151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,5251,5500)
210151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,5501,5750)
211151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,5751,6000)
212151497Sru
213151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,6001,6250)
214151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,6251,6500)
215151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,6501,6750)
216151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,6751,7000)
217151497Sru
218151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,7001,7250)
219151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,7251,7500)
220151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,7501,7750)
221151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,7751,8000)
222151497Sru
223151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,8001,8250)
224151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,8251,8500)
225151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,8501,8750)
226151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,8751,9000)
227151497Sru
228151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,9001,9250)
229151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,9251,9500)
230151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,9501,9750)
231151497Sru    $$(call ListPathsSafely_IfPrintf,$1,$2,9751,10000)
232151497Sru  endef
233151497Sruendif # HAS_FILE_FUNCTION
234151497Sru
235151497Sru################################################################################
236151497Sru# The source tips can come from the Mercurial repository, or in the files
237151497Sru# $(HGTIP_FILENAME) which contains the tip but is also positioned in the same
238151497Sru# directory as the original $(HGDIR) directory.
239151497Sru# These should not be := assignments, only used from the root Makefile.
240151497SruHG_VERSION = $(shell $(HG) version 2> /dev/null)
241151497SruHG_DIRECTORY=.hg
242151497SruHGTIP_FILENAME=.hgtip
243151497SruHG_SEARCH = ./REPO ./*/REPO ./*/*/REPO ./*/*/*/REPO
244151497SruREPO_LIST = $(patsubst ./%,%,$(patsubst %/,%,$(sort $(dir \
245151497Sru    $(shell $(CD) $(SRC_ROOT) ; \
246151497Sru        $(LS) -d $(HG_SEARCH:%/REPO=%/$(HG_DIRECTORY)) \
247151497Sru            $(HG_SEARCH:%/REPO=%/$(HGTIP_FILENAME)) \
248151497Sru        2> /dev/null)))))
249151497Sru
250151497Sru# Emit the repo:tip pairs to $@, but only if they changed since last time
251151497Srudefine GetSourceTips
252151497Sru	$(CD) $(SRC_ROOT) ; \
253151497Sru	for i in $(REPO_LIST) IGNORE ; do \
254151497Sru	  if [ "$${i}" = "IGNORE" ] ; then \
255151497Sru	    continue; \
256151497Sru	  elif [ -d $${i}/$(HG_DIRECTORY) -a "$(HG_VERSION)" != "" ] ; then \
257151497Sru	    $(PRINTF) " %s:%s" \
258151497Sru	        "$${i}" `$(HG) tip --repository $${i} --template '{node|short}\n'` ; \
259151497Sru	  elif [ -f $${i}/$(HGTIP_FILENAME) ] ; then \
260151497Sru	    $(PRINTF) " %s:%s" \
261151497Sru	        "$${i}" `$(CAT) $${i}/$(HGTIP_FILENAME)` ; \
262151497Sru	  fi; \
263151497Sru	done > $@.tmp
264151497Sru	$(PRINTF) "\n" >> $@.tmp
265151497Sru	if [ ! -f $@ ] || [ "`$(CAT) $@`" != "`$(CAT) $@.tmp`" ]; then \
266151497Sru	  $(MV) $@.tmp $@ ; \
267151497Sru	else \
268151497Sru	  $(RM) $@.tmp ; \
269151497Sru	fi
270151497Sruendef
271151497Sru
272151497Sru# Create the HGTIP_FILENAME file. Called from closed/make/SourceBundles.gmk
273151497Srudefine CreateHgTip
274151497Sru	$(HG) tip --repository $1 --template '{node|short}\n' > $1/$(HGTIP_FILENAME); \
275151497Sru	$(ECHO) $1/$(HGTIP_FILENAME)
276151497Sruendef
277151497Sru
278151497Sru################################################################################
279151497Sru
280151497Srudefine SetupLogging
281151497Sru  ifeq ($$(LOG_LEVEL), trace)
282151497Sru    # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
283151497Sru    # For each target executed, will print
284151497Sru    # Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer)
285151497Sru    # but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much
286151497Sru    # (and causing a crash on Cygwin).
287151497Sru    # Default shell seems to always be /bin/sh. Must override with bash to get this to work on Solaris.
288151497Sru    # Only use time if it's GNU time which supports format and output file.
289151497Sru    WRAPPER_SHELL := $$(BASH) $$(SRC_ROOT)/common/bin/shell-tracer.sh $$(if $$(findstring yes,$$(IS_GNU_TIME)),$$(TIME),-) $$(OUTPUT_ROOT)/build-trace-time.log $$(SHELL)
290151497Sru    SHELL = $$(warning $$(if $$@,Building $$@,Running shell command) $$(if $$<, (from $$<))$$(if $$?, ($$(wordlist 1, 20, $$?) $$(if $$(wordlist 21, 22, $$?), ... [in total $$(words $$?) files]) newer)))$$(WRAPPER_SHELL)
291151497Sru  endif
292151497Sru  # The warn level can never be turned off
293151497Sru  LogWarn = $$(info $$(strip $$1))
294151497Sru  LOG_WARN :=
295151497Sru  ifneq ($$(findstring $$(LOG_LEVEL), info debug trace),)
296151497Sru    LogInfo = $$(info $$(strip $$1))
297151497Sru    LOG_INFO :=
298151497Sru  else
299151497Sru    LogInfo =
300151497Sru    LOG_INFO := > /dev/null
301151497Sru  endif
302151497Sru  ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
303151497Sru    LogDebug = $$(info $$(strip $$1))
304151497Sru    LOG_DEBUG :=
305151497Sru  else
306151497Sru    LogDebug =
307151497Sru    LOG_DEBUG := > /dev/null
308151497Sru  endif
309151497Sru  ifneq ($$(findstring $$(LOG_LEVEL), trace),)
310151497Sru    LogTrace = $$(info $$(strip $$1))
311151497Sru    LOG_TRACE :=
312151497Sru  else
313151497Sru    LogTrace =
314151497Sru    LOG_TRACE := > /dev/null
315151497Sru  endif
316151497Sruendef
317151497Sru
318151497Sru# Make sure logging is setup for everyone that includes MakeBase.gmk.
319151497Sru$(eval $(call SetupLogging))
320151497Sru
321151497Sru################################################################################
322151497Sru# Creates a sequence of increasing numbers (inclusive).
323151497Sru# Param 1 - starting number
324151497Sru# Param 2 - ending number
325151497Srusequence = \
326151497Sru    $(wordlist $1, $2, $(strip \
327151497Sru        $(eval SEQUENCE_COUNT :=) \
328151497Sru        $(call _sequence-do,$(strip $2))))
329151497Sru
330151497Sru_sequence-do = \
331151497Sru    $(if $(word $1, $(SEQUENCE_COUNT)),, \
332151497Sru      $(eval SEQUENCE_COUNT += .) \
333151497Sru      $(words $(SEQUENCE_COUNT)) \
334151497Sru      $(call _sequence-do,$1))
335151497Sru
336151497Sru################################################################################
337151497Sru
338151497SruMAX_PARAMS := 35
339151497SruPARAM_SEQUENCE := $(call sequence, 2, $(MAX_PARAMS))
340151497Sru
341151497Sru# Template for creating a macro taking named parameters. To use it, assign the
342151497Sru# template to a variable with the name you want for your macro, using '='
343151497Sru# assignment. Then define a macro body with the suffix "Body". The Body macro
344151497Sru# should take 1 parameter which should be a unique string for that invocation
345151497Sru# of the macro.
346151497Sru# Ex:
347151497Sru# SetupFoo = $(NamedParamsMacroTemplate)
348151497Sru# define SetupFooBody
349151497Sru#   # do something
350151497Sru#   # access parameters as $$($1_BAR)
351151497Sru# endef
352151497Sru# Call it like this
353151497Sru# $(eval $(call SetupFoo, BUILD_SOMETHING, \
354151497Sru#     BAR := some parameter value, \
355151497Sru# ))
356151497Srudefine NamedParamsMacroTemplate
357151497Sru  $(if $($(MAX_PARAMS)),$(error Internal makefile error: \
358151497Sru      Too many named arguments to macro, please update MAX_PARAMS in MakeBase.gmk))
359151497Sru  # Iterate over 2 3 4... and evaluate the named parameters with $1_ as prefix
360151497Sru  $(foreach i,$(PARAM_SEQUENCE), $(if $(strip $($i)),\
361151497Sru    $(strip $1)_$(strip $($i)))$(NEWLINE))
362151497Sru  # Debug print all named parameter names and values
363151497Sru  $(if $(findstring $(LOG_LEVEL),debug trace), \
364151497Sru    $(info $0 $(strip $1) $(foreach i,$(PARAM_SEQUENCE), \
365151497Sru      $(if $(strip $($i)),$(NEWLINE) $(strip [$i] $(if $(filter $(LOG_LEVEL), trace), \
366151497Sru        $($i), $(wordlist 1, 20, $($(i))) $(if $(word 21, $($(i))), ...)))))))
367151497Sru
368151497Sru  $(if $(DEBUG_$(strip $1)),
369151497Sru    $(info -------- <<< Begin expansion of $(strip $1)) \
370151497Sru    $(info $(call $(0)Body,$(strip $1))) \
371151497Sru    $(info -------- >>> End expansion of $(strip $1)) \
372151497Sru  )
373151497Sru
374151497Sru  $(call $(0)Body,$(strip $1))
375151497Sruendef
376151497Sru
377151497Sru################################################################################
378151497Sru# Make directory without forking mkdir if not needed
379151497Sru# 1: List of directories to create
380151497SruMakeDir = \
381151497Sru    $(strip \
382151497Sru        $(eval MakeDir_dirs_to_make := $(strip $(foreach d, $1, $(if $(wildcard $d), , $d)))) \
383151497Sru        $(if $(MakeDir_dirs_to_make), $(shell $(MKDIR) -p $(MakeDir_dirs_to_make))) \
384151497Sru    )
385151497Sru
386151497Sru################################################################################
387151497Sru# Assign a variable only if it is empty
388151497Sru# Param 1 - Variable to assign
389151497Sru# Param 2 - Value to assign
390151497SruSetIfEmpty = \
391151497Sru    $(if $($(strip $1)),,$(eval $(strip $1) := $2))
392151497Sru
393151497Sru################################################################################
394151497Sru
395151497Sruifeq ($(OPENJDK_TARGET_OS),solaris)
396151497Sru  # On Solaris, if the target is a symlink and exists, cp won't overwrite.
397151497Sru  # Cp has to operate in recursive mode to allow for -P flag, to preserve soft links. If the
398151497Sru  # name of the target file differs from the source file, rename after copy.
399151497Sru  # If the source and target parent directories are the same, recursive copy doesn't work
400151497Sru  # so we fall back on regular copy, which isn't preserving symlinks.
401151497Sru  define install-file
402151497Sru	$(MKDIR) -p '$(@D)'
403151497Sru	$(RM) '$@'
404151497Sru	if [ "$(@D)" != "$(<D)" ]; then \
405151497Sru	  $(CP) -f -r -P '$<' '$(@D)'; \
406151497Sru	  if [ "$(@F)" != "$(<F)" ]; then \
407151497Sru	    $(MV) '$(@D)/$(<F)' '$@'; \
408151497Sru	  fi; \
409151497Sru	else \
410151497Sru	  if [ -L '$<' ]; then \
411151497Sru	    $(ECHO) "Source file is a symlink and target is in the same directory: $< $@" ; \
412151497Sru	    exit 1; \
413151497Sru	  fi; \
414151497Sru	  $(CP) -f '$<' '$@'; \
415151497Sru	fi
416151497Sru  endef
417151497Sruelse ifeq ($(OPENJDK_TARGET_OS),macosx)
418151497Sru  # On mac, extended attributes sometimes creep into the source files, which may later
419151497Sru  # cause the creation of ._* files which confuses testing. Clear these with xattr if
420151497Sru  # set. Some files get their write permissions removed after being copied to the
421151497Sru  # output dir. When these are copied again to images, xattr would fail. By only clearing
422151497Sru  # attributes when they are present, failing on this is avoided.
423151497Sru  define install-file
424151497Sru	$(MKDIR) -p '$(@D)'
425151497Sru	$(CP) -fRP '$<' '$@'
426151497Sru	if [ -n "`$(XATTR) -l '$@'`" ]; then $(XATTR) -c '$@'; fi
427151497Sru  endef
428151497Sruelse
429151497Sru  # Running mkdir and cp in the same shell speeds up copy intensive tasks in Cygwin
430151497Sru  # significantly.
431151497Sru  define install-file
432151497Sru	$(call MakeDir, $(@D))
433151497Sru	$(CP) -fP '$<' '$@'
434151497Sru  endef
435151497Sruendif
436151497Sru
437151497Sru################################################################################
438151497Sru# Filter out duplicate sub strings while preserving order. Keeps the first occurance.
439151497Sruuniq = \
440151497Sru    $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
441151497Sru
442151497Sru# Returns all whitespace-separated words in $2 where at least one of the
443151497Sru# whitespace-separated words in $1 is a substring.
444151497Srucontaining = \
445151497Sru    $(strip \
446151497Sru        $(foreach v,$(strip $2),\
447151497Sru          $(call uniq,$(foreach p,$(strip $1),$(if $(findstring $p,$v),$v)))))
448151497Sru
449151497Sru# Returns all whitespace-separated words in $2 where none of the
450151497Sru# whitespace-separated words in $1 is a substring.
451151497Srunot-containing = \
452151497Sru    $(strip $(filter-out $(call containing,$1,$2),$2))
453151497Sru
454151497Sru# Return a list of all string elements that are duplicated in $1.
455151497Srudups = \
456151497Sru    $(strip $(foreach v, $(sort $1), $(if $(filter-out 1, \
457151497Sru        $(words $(filter $v, $1))), $v)))
458151497Sru
459151497Sru# String equals
460151497Sruequals = \
461151497Sru    $(and $(findstring $(strip $1),$(strip $2)),\
46275584Sru        $(findstring $(strip $2),$(strip $1)))
463151497Sru
464151497Sru# Remove a whole list of prefixes
46575584Sru# $1 - List of prefixes
466151497Sru# $2 - List of elements to process
46775584Sruremove-prefixes = \
468151497Sru    $(strip $(if $1,$(patsubst $(firstword $1)%,%,\
46975584Sru      $(call remove-prefixes,$(filter-out $(firstword $1),$1),$2)),$2))
470151497Sru
471151497Sru# Convert the string given to upper case, without any $(shell)
472151497Sru# Inspired by http://lists.gnu.org/archive/html/help-make/2013-09/msg00009.html
47375584Sruuppercase_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 \
474151497Sru    p,P q,Q r,R s,S t,T u,U v,V w,W x,X y,Y z,Z
475151497Sru
476151497Sruuppercase_internal = \
477151497Sru  $(if $(strip $1), $$(subst $(firstword $1), $(call uppercase_internal, \
478151497Sru      $(wordlist 2, $(words $1), $1), $2)), $2)
479151497Sru
48075584Sru# Convert a string to upper case. Works only on a-z.
481151497Sru# $1 - The string to convert
482151497Sruuppercase = \
48375584Sru  $(strip \
484151497Sru    $(eval uppercase_result := $(call uppercase_internal, $(uppercase_table), $1)) \
485151497Sru    $(uppercase_result) \
486151497Sru  )
487151497Sru
488151497Sru################################################################################
489151497Sru
490151497Sruifneq ($(DISABLE_CACHE_FIND), true)
491151497Sru  # In Cygwin, finds are very costly, both because of expensive forks and because
492151497Sru  # of bad file system caching. Find is used extensively in $(shell) commands to
493151497Sru  # find source files. This makes rerunning make with no or few changes rather
494151497Sru  # expensive. To speed this up, these two macros are used to cache the results
495151497Sru  # of simple find commands for reuse.
496151497Sru  #
497151497Sru  # Runs a find and stores both the directories where it was run and the results.
498151497Sru  # This macro can be called multiple times to add to the cache. Only finds files
499151497Sru  # with no filters.
500151497Sru  #
50175584Sru  # Needs to be called with $(eval )
502151497Sru  #
503151497Sru  # Even if the performance benifit is negligible on other platforms, keep the
504151497Sru  # functionality active unless explicitly disabled to exercise it more.
505151497Sru  #
506151497Sru  # Initialize FIND_CACHE_DIRS with := to make it a non recursively-expanded variable
507151497Sru  FIND_CACHE_DIRS :=
508151497Sru  # Param 1 - Dirs to find in
509151497Sru  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
510151497Sru  define FillCacheFind
511151497Sru    # Filter out already cached dirs. The - is needed when FIND_CACHE_DIRS is empty
512151497Sru    # since filter out will then return empty.
513151497Sru    FIND_CACHE_NEW_DIRS := $$(filter-out $$(addsuffix /%,\
514151497Sru        - $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS), $1)
515151497Sru    ifneq ($$(FIND_CACHE_NEW_DIRS), )
516151497Sru      # Remove any trailing slash from dirs in the cache dir list
517151497Sru      FIND_CACHE_DIRS += $$(patsubst %/,%, $$(FIND_CACHE_NEW_DIRS))
518151497Sru      FIND_CACHE := $$(sort $$(FIND_CACHE) $$(shell $(FIND) $$(FIND_CACHE_NEW_DIRS) \( -type f -o -type l \) $2))
519151497Sru    endif
520151497Sru  endef
521151497Sru
522151497Sru  # Mimics find by looking in the cache if all of the directories have been cached.
523151497Sru  # Otherwise reverts to shell find. This is safe to call on all platforms, even if
524151497Sru  # cache is deactivated.
525151497Sru  #
526151497Sru  # $1 can be either a directory or a file. If it's a directory, make
527151497Sru  # sure we have exactly one trailing slash before the wildcard.
528151497Sru  # The extra - is needed when FIND_CACHE_DIRS is empty but should be harmless.
52975584Sru  #
530151497Sru  # Param 1 - Dirs to find in
53175584Sru  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
532151497Sru  define CacheFind
53375584Sru      $(if $(filter-out $(addsuffix /%,- $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS),$1), \
534151497Sru    $(shell $(FIND) $1 \( -type f -o -type l \) $2), \
535151497Sru        $(filter $(addsuffix /%,$(patsubst %/,%,$1)) $1,$(FIND_CACHE)))
536151497Sru  endef
53775584Sru
538151497Sruelse
539151497Sru  # If CacheFind is disabled, just run the find command.
540151497Sru  # Param 1 - Dirs to find in
541151497Sru  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
542151497Sru  define CacheFind
543151497Sru    $(shell $(FIND) $1 \( -type f -o -type l \) $2)
544151497Sru  endef
545151497Sruendif
546151497Sru
547151497Sru################################################################################
548151497Sru
549151497Srudefine AddFileToCopy
550151497Sru  # Helper macro for SetupCopyFiles
551151497Sru  # 1 : Source file
552151497Sru  # 2 : Dest file
553151497Sru  # 3 : Variable to add targets to
554151497Sru  # 4 : Macro to call for copy operation
555151497Sru  $2: $1
556151497Sru	$$(call LogInfo, Copying $$(patsubst $(OUTPUT_ROOT)/%,%,$$@))
557151497Sru	$$($$(strip $4))
558151497Sru
559151497Sru  $3 += $2
560151497Sruendef
561151497Sru
562151497Sru# Returns the value of the first argument
563151497Sruidentity = \
564151497Sru    $(strip $1)
565151497Sru
566151497Sru# Setup make rules for copying files, with an option to do more complex
567151497Sru# processing instead of copying.
568151497Sru#
569151497Sru# Parameter 1 is the name of the rule. This name is used as variable prefix,
570151497Sru# and the targets generated are listed in a variable by that name.
571151497Sru#
572151497Sru# Remaining parameters are named arguments. These include:
573151497Sru#   SRC     : Source root dir (defaults to dir of first file)
574151497Sru#   DEST    : Dest root dir
575151497Sru#   FILES   : List of files to copy with absolute paths, or path relative to SRC.
576151497Sru#             Must be in SRC.
577151497Sru#   FLATTEN : Set to flatten the directory structure in the DEST dir.
578151497Sru#   MACRO   : Optionally override the default macro used for making the copy.
579151497Sru#             Default is 'install-file'
580151497Sru#   NAME_MACRO : Optionally supply a macro that rewrites the target file name
58175584Sru#                based on the source file name
582151497SruSetupCopyFiles = $(NamedParamsMacroTemplate)
583151497Srudefine SetupCopyFilesBody
584151497Sru
585151497Sru  ifeq ($$($1_MACRO), )
586151497Sru    $1_MACRO := install-file
587151497Sru  endif
588151497Sru
589151497Sru  # Default SRC to the dir of the first file.
590151497Sru  ifeq ($$($1_SRC), )
591151497Sru    $1_SRC := $$(dir $$(firstword $$($1_FILES)))
592151497Sru  endif
593151497Sru
594151497Sru  ifeq ($$($1_NAME_MACRO), )
595151497Sru    $1_NAME_MACRO := identity
596151497Sru  endif
597151497Sru
598151497Sru  # Remove any trailing slash from SRC and DEST
599151497Sru  $1_SRC := $$(patsubst %/,%,$$($1_SRC))
600151497Sru  $1_DEST := $$(patsubst %/,%,$$($1_DEST))
601151497Sru
602151497Sru  $$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \
603151497Sru      $$(eval $$(call AddFileToCopy, $$($1_SRC)/$$f, \
604151497Sru      $$($1_DEST)/$$(call $$(strip $$($1_NAME_MACRO)),$$(if $$($1_FLATTEN),$$(notdir $$f),$$f)), \
605151497Sru      $1, $$($1_MACRO))))
606151497Sru
607151497Sruendef
608151497Sru
609151497Sru################################################################################
610151497Sru# ShellQuote
611151497Sru#
612151497Sru# Quotes a string with single quotes and replaces single quotes with '\'' so
613151497Sru# that the contents survives being given to the shell.
614151497Sru
615151497SruShellQuote = \
616151497Sru    $(SQUOTE)$(subst $(SQUOTE),$(SQUOTE)\$(SQUOTE)$(SQUOTE),$(strip $1))$(SQUOTE)
617151497Sru
618151497Sru################################################################################
619151497Sru# Write to and read from file
620151497Sru
621151497Sru# Param 1 - File to read
622151497SruReadFile = \
623151497Sru    $(shell $(CAT) $1)
624151497Sru
625151497Sru# Param 1 - Text to write
626151497Sru# Param 2 - File to write to
627151497Sruifeq ($(HAS_FILE_FUNCTION), true)
628151497Sru  WriteFile = \
629151497Sru      $(file >$2,$(strip $1))
630151497Sruelse
631151497Sru  # Use printf to get consistent behavior on all platforms.
632151497Sru  WriteFile = \
633151497Sru      $(shell $(PRINTF) "%s" $(call ShellQuote, $1) > $2)
634151497Sruendif
635151497Sru
636151497Sru################################################################################
637151497Sru# DependOnVariable
638151497Sru#
639151497Sru# This macro takes a variable name and puts the value in a file only if the
640151497Sru# value has changed since last. The name of the file is returned. This can be
641151497Sru# used to create rule dependencies on make variable values. The following
642151497Sru# example would get rebuilt if the value of SOME_VAR was changed:
643151497Sru#
644151497Sru# path/to/some-file: $(call DependOnVariable, SOME_VAR)
645151497Sru#         echo $(SOME_VAR) > $@
646151497Sru#
647151497Sru# Note that leading and trailing white space in the value is ignored.
648151497Sru#
649151497Sru
650151497Sru# Defines the sub directory structure to store variable value file in
651151497SruDependOnVariableDirName = \
652151497Sru    $(strip $(addsuffix $(if $(MODULE),/$(MODULE)), \
653151497Sru        $(subst $(SRC_ROOT)/,, $(if $(filter /%, $(firstword $(MAKEFILE_LIST))), \
654151497Sru          $(firstword $(MAKEFILE_LIST)), \
655151497Sru          $(CURDIR)/$(firstword $(MAKEFILE_LIST))))))
656151497Sru
657151497Sru# Defines the name of the file to store variable value in. Generates a name
658151497Sru# unless parameter 2 is given.
659151497Sru# Param 1 - Name of variable
660151497Sru# Param 2 - (optional) name of file to store value in
661151497SruDependOnVariableFileName = \
662151497Sru    $(strip $(if $(strip $2), $2, \
663151497Sru      $(MAKESUPPORT_OUTPUTDIR)/vardeps/$(DependOnVariableDirName)/$(strip $1).vardeps))
664151497Sru
665151497Sru# Does the actual work with parameters stripped.
666151497Sru# If the file exists AND the contents is the same as the variable, do nothing
667151497Sru# else print a new file.
668151497Sru# Always returns the name of the file where the value was printed.
669151497Sru# Param 1 - Name of variable
670151497Sru# Param 2 - (optional) name of file to store value in
671151497SruDependOnVariableHelper = \
672151497Sru    $(strip \
673151497Sru        $(eval -include $(call DependOnVariableFileName, $1, $2)) \
674151497Sru        $(if $(call equals, $(strip $($1)), $(strip $($1_old))),,\
675151497Sru          $(call MakeDir, $(dir $(call DependOnVariableFileName, $1, $2))) \
676151497Sru          $(if $(findstring $(LOG_LEVEL), trace), \
677151497Sru              $(info NewVariable $1: >$(strip $($1))<) \
678151497Sru              $(info OldVariable $1: >$(strip $($1_old))<)) \
679151497Sru          $(call WriteFile, $1_old:=$(call DoubleDollar,$($1)), \
680151497Sru              $(call DependOnVariableFileName, $1, $2))) \
681151497Sru        $(call DependOnVariableFileName, $1, $2) \
682151497Sru    )
683151497Sru
684151497Sru# Main macro
685151497Sru# Param 1 - Name of variable
686151497Sru# Param 2 - (optional) name of file to store value in
687151497SruDependOnVariable = \
688151497Sru    $(call DependOnVariableHelper,$(strip $1),$(strip $2))
689151497Sru
690151497Sru# LogCmdlines is only intended to be used by ExecuteWithLog
691151497Sruifeq ($(LOG_CMDLINES), true)
692151497Sru  LogCmdlines = $(info $(strip $1))
693151497Sruelse
694151497Sru  LogCmdlines =
695151497Sruendif
696151497Sru
697151497Sru################################################################################
698151497Sru# ExecuteWithLog will run a command and log the output appropriately. This is
699151497Sru# meant to be used by commands that do "real" work, like a compilation.
700151497Sru# The output is stored in a specified log file, which is displayed at the end
701151497Sru# of the build in case of failure. The  command line itself is stored in a file,
702151497Sru# and also logged to stdout if the LOG=cmdlines option has been given.
703151497Sru#
704151497Sru# Param 1 - The path to base the name of the log file / command line file on
705151497Sru# Param 2 - The command to run
706151497SruExecuteWithLog = \
707151497Sru  $(call LogCmdlines, Exececuting: [$(strip $2)]) \
708151497Sru  $(call WriteFile, $2, $(strip $1).cmdline) \
709151497Sru  ( $(strip $2) > >($(TEE) $(strip $1).log) 2> >($(TEE) $(strip $1).log >&2) || \
710151497Sru      ( exitcode=$(DOLLAR)? && \
711151497Sru      $(CP) $(strip $1).log $(MAKESUPPORT_OUTPUTDIR)/failure-logs/$(subst /,_,$(patsubst $(BUILD_OUTPUT)/%,%,$(strip $1))).log && \
712151497Sru      exit $(DOLLAR)exitcode ) )
713151497Sru
714151497Sru################################################################################
715151497Sru# Find lib dir for module
716151497Sru# Param 1 - module name
717151497Sruifeq ($(OPENJDK_TARGET_OS_TYPE), unix)
718151497Sru  FindLibDirForModule = \
719151497Sru      $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)$(OPENJDK_TARGET_CPU_LIBDIR)
720151497Sruelse
721151497Sru  FindLibDirForModule = \
722151497Sru      $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)
723151497Sruendif
724151497Sru
725151497Sru################################################################################
726151497Sru# Return a string suitable for use after a -classpath option. It will correct and safe to use
727151497Sru# on all platforms. Arguments are given as space separate classpath entries.
728151497Sru# param 1 : A space separated list of classpath entries
729151497Sru# The surrounding strip is needed to keep additional whitespace out
730151497SruPathList = \
731151497Sru  "$(subst $(SPACE),$(PATH_SEP),$(strip $1))"
732151497Sru
733151497Sru################################################################################
734151497Sru# Check if a specified hotspot variant is being built, or at least one of a
735151497Sru# list of variants. Will return 'true' or 'false'.
736151497Sru# $1 - the variant to test for
737151497Srucheck-jvm-variant = \
738151497Sru  $(strip \
739151497Sru    $(if $(filter-out $(VALID_JVM_VARIANTS), $1), \
740151497Sru      $(error Internal error: Invalid variant tested: $1)) \
741151497Sru    $(if $(filter $1, $(JVM_VARIANTS)), true, false))
742151497Sru
743151497Sru################################################################################
744151497Sru
745151497Sru# Hook to include the corresponding custom file, if present.
746151497Sru$(eval $(call IncludeCustomExtension, , common/MakeBase.gmk))
747151497Sru
748151497Sruendif # _MAKEBASE_GMK
749151497Sru