MakeBase.gmk revision 2398:05ac48290793
1#
2# Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
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.
10#
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).
16#
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.
20#
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
23# questions.
24#
25
26################################################################
27#
28# Setup common utility functions.
29#
30################################################################
31
32ifndef _MAKEBASE_GMK
33_MAKEBASE_GMK := 1
34
35ifeq ($(wildcard $(SPEC)),)
36  $(error MakeBase.gmk needs SPEC set to a proper spec.gmk)
37endif
38
39# By defining this pseudo target, make will automatically remove targets
40# if their recipe fails so that a rebuild is automatically triggered on the
41# next make invocation.
42.DELETE_ON_ERROR:
43
44################################################################################
45# Definitions for special characters
46################################################################################
47
48# When calling macros, the spaces between arguments are
49# often semantically important! Sometimes we need to subst
50# spaces and commas, therefore we need the following macros.
51X:=
52SPACE:=$(X) $(X)
53COMMA:=,
54DOLLAR:=$$
55HASH:=\#
56LEFT_PAREN:=(
57RIGHT_PAREN:=)
58SQUOTE:='
59#'
60DQUOTE:="
61#"
62define NEWLINE
63
64
65endef
66
67# In GNU Make 4.0 and higher, there is a file function for writing to files.
68ifeq (4.0, $(firstword $(sort 4.0 $(MAKE_VERSION))))
69  HAS_FILE_FUNCTION := true
70endif
71
72##############################
73# Functions
74##############################
75
76### Debug functions
77
78# Prints the name and value of a variable
79PrintVar = \
80    $(info $(strip $1) >$($(strip $1))<)
81
82### Functions for timers
83
84# Store the build times in this directory.
85BUILDTIMESDIR=$(OUTPUT_ROOT)/make-support/build-times
86
87# Record starting time for build of a sub repository.
88define RecordStartTime
89	$(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) && \
90	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$(strip $1)_human_readable
91endef
92
93# Record ending time and calculate the difference and store it in a
94# easy to read format. Handles builds that cross midnight. Expects
95# that a build will never take 24 hours or more.
96define RecordEndTime
97	$(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)
98	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)_human_readable
99	$(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$(strip $1)` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$(strip $1)` $1 | \
100	    $(NAWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \
101	    M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \
102	    > $(BUILDTIMESDIR)/build_time_diff_$(strip $1)
103endef
104
105# Hook to be called when starting to execute a top-level target
106define TargetEnter
107	$(PRINTF) "## Starting $(patsubst %-only,%,$@)\n"
108	$(call RecordStartTime,$(patsubst %-only,%,$@))
109endef
110
111# Hook to be called when finish executing a top-level target
112define TargetExit
113	$(call RecordEndTime,$(patsubst %-only,%,$@))
114	$(PRINTF) "## Finished $(patsubst %-only,%,$@) (build time %s)\n\n" \
115	    "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_$(patsubst %-only,%,$@) | $(CUT) -f 1 -d ' '`"
116endef
117
118################################################################################
119# This macro translates $ into \$ to protect the $ from expansion in the shell.
120# To make this macro resilient against already escaped strings, first remove
121# any present escapes before escaping so that no double escapes are added.
122EscapeDollar = $(subst $$,\$$,$(subst \$$,$$,$(strip $1)))
123
124################################################################################
125# This macro works just like EscapeDollar above, but for #.
126EscapeHash = $(subst \#,\\\#,$(subst \\\#,\#,$(strip $1)))
127
128################################################################################
129# This macro translates $ into $$ to protect the string from make itself.
130DoubleDollar = $(subst $$,$$$$,$(strip $1))
131
132################################################################################
133# ListPathsSafely can be used to print command parameters to a file. This is
134# typically done if the command line lenght risk being too long for the
135# OS/shell. In later make versions, the file function can be used for this
136# purpose. For earlier versions, a more complex implementation is provided.
137#
138# The function ListPathsSafely can be called either directly or, more commonly
139# from a recipe line. If called from a recipe, it will be executed in the
140# evaluation phase of that recipe, which means that it will write to the file
141# before any other line in the recipe has been run.
142ifeq ($(HAS_FILE_FUNCTION), true)
143  # Param 1 - Name of variable containing paths/arguments to output
144  # Param 2 - File to print to
145  # Param 3 - Set to true to append to file instead of overwriting
146  define ListPathsSafely
147    $$(call MakeDir, $$(dir $$(strip $2)))
148    $$(file $$(if $$(filter true, $$(strip $3)),>>,>) \
149        $$(strip $2),$$(subst $$(SPACE),$$(NEWLINE),$$(strip $$($$(strip $1)))))
150  endef
151
152else # HAS_FILE_FUNCTION = false
153
154  $(eval compress_paths = \
155      $(strip $(shell $(CAT) $(SRC_ROOT)/make/common/support/ListPathsSafely-pre-compress.incl)))
156  compress_paths += \
157      $(subst $(SRC_ROOT),X97, \
158      $(subst $(OUTPUT_ROOT),X98, \
159      $(subst X,X00, \
160      $(subst $(SPACE),\n,$(strip $1)))))
161  $(eval compress_paths += \
162      $(strip $(shell $(CAT) $(SRC_ROOT)/make/common/support/ListPathsSafely-post-compress.incl)))
163
164  decompress_paths=$(SED) -f $(SRC_ROOT)/make/common/support/ListPathsSafely-uncompress.sed \
165      -e 's|X99|\\n|g' \
166      -e 's|X98|$(OUTPUT_ROOT)|g' -e 's|X97|$(SRC_ROOT)|g' \
167      -e 's|X00|X|g'
168
169  ListPathsSafely_IfPrintf = \
170      $(if $(word $3,$($(strip $1))), \
171          $(shell $(PRINTF) -- "$(strip $(call EscapeDollar, \
172              $(call compress_paths, $(wordlist $3,$4,$($(strip $1))))))\n" \
173              | $(decompress_paths) >> $2))
174
175  # Param 1 - Name of variable containing paths/arguments to output
176  # Param 2 - File to print to
177  # Param 3 - Set to true to append to file instead of overwriting
178  define ListPathsSafely
179    ifneq (,$$(word 30001,$$($$(strip $1))))
180      $$(error Cannot list safely more than 30000 paths. $1 has $$(words $$($$(strip $1))) paths!)
181    endif
182    $$(call MakeDir, $$(dir $2))
183    ifneq ($$(strip $3), true)
184      $$(shell $(RM) $$(strip $2))
185    endif
186
187    $$(call ListPathsSafely_IfPrintf,$1,$2,1,250)
188    $$(call ListPathsSafely_IfPrintf,$1,$2,251,500)
189    $$(call ListPathsSafely_IfPrintf,$1,$2,501,750)
190    $$(call ListPathsSafely_IfPrintf,$1,$2,751,1000)
191
192    $$(call ListPathsSafely_IfPrintf,$1,$2,1001,1250)
193    $$(call ListPathsSafely_IfPrintf,$1,$2,1251,1500)
194    $$(call ListPathsSafely_IfPrintf,$1,$2,1501,1750)
195    $$(call ListPathsSafely_IfPrintf,$1,$2,1751,2000)
196
197    $$(call ListPathsSafely_IfPrintf,$1,$2,2001,2250)
198    $$(call ListPathsSafely_IfPrintf,$1,$2,2251,2500)
199    $$(call ListPathsSafely_IfPrintf,$1,$2,2501,2750)
200    $$(call ListPathsSafely_IfPrintf,$1,$2,2751,3000)
201
202    $$(call ListPathsSafely_IfPrintf,$1,$2,3001,3250)
203    $$(call ListPathsSafely_IfPrintf,$1,$2,3251,3500)
204    $$(call ListPathsSafely_IfPrintf,$1,$2,3501,3750)
205    $$(call ListPathsSafely_IfPrintf,$1,$2,3751,4000)
206
207    $$(call ListPathsSafely_IfPrintf,$1,$2,4001,4250)
208    $$(call ListPathsSafely_IfPrintf,$1,$2,4251,4500)
209    $$(call ListPathsSafely_IfPrintf,$1,$2,4501,4750)
210    $$(call ListPathsSafely_IfPrintf,$1,$2,4751,5000)
211
212    $$(call ListPathsSafely_IfPrintf,$1,$2,5001,5250)
213    $$(call ListPathsSafely_IfPrintf,$1,$2,5251,5500)
214    $$(call ListPathsSafely_IfPrintf,$1,$2,5501,5750)
215    $$(call ListPathsSafely_IfPrintf,$1,$2,5751,6000)
216
217    $$(call ListPathsSafely_IfPrintf,$1,$2,6001,6250)
218    $$(call ListPathsSafely_IfPrintf,$1,$2,6251,6500)
219    $$(call ListPathsSafely_IfPrintf,$1,$2,6501,6750)
220    $$(call ListPathsSafely_IfPrintf,$1,$2,6751,7000)
221
222    $$(call ListPathsSafely_IfPrintf,$1,$2,7001,7250)
223    $$(call ListPathsSafely_IfPrintf,$1,$2,7251,7500)
224    $$(call ListPathsSafely_IfPrintf,$1,$2,7501,7750)
225    $$(call ListPathsSafely_IfPrintf,$1,$2,7751,8000)
226
227    $$(call ListPathsSafely_IfPrintf,$1,$2,8001,8250)
228    $$(call ListPathsSafely_IfPrintf,$1,$2,8251,8500)
229    $$(call ListPathsSafely_IfPrintf,$1,$2,8501,8750)
230    $$(call ListPathsSafely_IfPrintf,$1,$2,8751,9000)
231
232    $$(call ListPathsSafely_IfPrintf,$1,$2,9001,9250)
233    $$(call ListPathsSafely_IfPrintf,$1,$2,9251,9500)
234    $$(call ListPathsSafely_IfPrintf,$1,$2,9501,9750)
235    $$(call ListPathsSafely_IfPrintf,$1,$2,9751,10000)
236
237    $$(call ListPathsSafely_IfPrintf,$1,$2,10001,10250)
238    $$(call ListPathsSafely_IfPrintf,$1,$2,10251,10500)
239    $$(call ListPathsSafely_IfPrintf,$1,$2,10501,10750)
240    $$(call ListPathsSafely_IfPrintf,$1,$2,10751,11000)
241
242    $$(call ListPathsSafely_IfPrintf,$1,$2,11001,11250)
243    $$(call ListPathsSafely_IfPrintf,$1,$2,11251,11500)
244    $$(call ListPathsSafely_IfPrintf,$1,$2,11501,11750)
245    $$(call ListPathsSafely_IfPrintf,$1,$2,11751,12000)
246
247    $$(call ListPathsSafely_IfPrintf,$1,$2,12001,12250)
248    $$(call ListPathsSafely_IfPrintf,$1,$2,12251,12500)
249    $$(call ListPathsSafely_IfPrintf,$1,$2,12501,12750)
250    $$(call ListPathsSafely_IfPrintf,$1,$2,12751,13000)
251
252    $$(call ListPathsSafely_IfPrintf,$1,$2,13001,13250)
253    $$(call ListPathsSafely_IfPrintf,$1,$2,13251,13500)
254    $$(call ListPathsSafely_IfPrintf,$1,$2,13501,13750)
255    $$(call ListPathsSafely_IfPrintf,$1,$2,13751,14000)
256
257    $$(call ListPathsSafely_IfPrintf,$1,$2,14001,14250)
258    $$(call ListPathsSafely_IfPrintf,$1,$2,14251,14500)
259    $$(call ListPathsSafely_IfPrintf,$1,$2,14501,14750)
260    $$(call ListPathsSafely_IfPrintf,$1,$2,14751,15000)
261
262    $$(call ListPathsSafely_IfPrintf,$1,$2,15001,15250)
263    $$(call ListPathsSafely_IfPrintf,$1,$2,15251,15500)
264    $$(call ListPathsSafely_IfPrintf,$1,$2,15501,15750)
265    $$(call ListPathsSafely_IfPrintf,$1,$2,15751,16000)
266
267    $$(call ListPathsSafely_IfPrintf,$1,$2,16001,16250)
268    $$(call ListPathsSafely_IfPrintf,$1,$2,16251,16500)
269    $$(call ListPathsSafely_IfPrintf,$1,$2,16501,16750)
270    $$(call ListPathsSafely_IfPrintf,$1,$2,16751,17000)
271
272    $$(call ListPathsSafely_IfPrintf,$1,$2,17001,17250)
273    $$(call ListPathsSafely_IfPrintf,$1,$2,17251,17500)
274    $$(call ListPathsSafely_IfPrintf,$1,$2,17501,17750)
275    $$(call ListPathsSafely_IfPrintf,$1,$2,17751,18000)
276
277    $$(call ListPathsSafely_IfPrintf,$1,$2,18001,18250)
278    $$(call ListPathsSafely_IfPrintf,$1,$2,18251,18500)
279    $$(call ListPathsSafely_IfPrintf,$1,$2,18501,18750)
280    $$(call ListPathsSafely_IfPrintf,$1,$2,18751,19000)
281
282    $$(call ListPathsSafely_IfPrintf,$1,$2,19001,19250)
283    $$(call ListPathsSafely_IfPrintf,$1,$2,19251,19500)
284    $$(call ListPathsSafely_IfPrintf,$1,$2,19501,19750)
285    $$(call ListPathsSafely_IfPrintf,$1,$2,19751,20000)
286
287    $$(call ListPathsSafely_IfPrintf,$1,$2,20001,20250)
288    $$(call ListPathsSafely_IfPrintf,$1,$2,20251,20500)
289    $$(call ListPathsSafely_IfPrintf,$1,$2,20501,20750)
290    $$(call ListPathsSafely_IfPrintf,$1,$2,20751,21000)
291
292    $$(call ListPathsSafely_IfPrintf,$1,$2,21001,21250)
293    $$(call ListPathsSafely_IfPrintf,$1,$2,21251,21500)
294    $$(call ListPathsSafely_IfPrintf,$1,$2,21501,21750)
295    $$(call ListPathsSafely_IfPrintf,$1,$2,21751,22000)
296
297    $$(call ListPathsSafely_IfPrintf,$1,$2,22001,22250)
298    $$(call ListPathsSafely_IfPrintf,$1,$2,22251,22500)
299    $$(call ListPathsSafely_IfPrintf,$1,$2,22501,22750)
300    $$(call ListPathsSafely_IfPrintf,$1,$2,22751,23000)
301
302    $$(call ListPathsSafely_IfPrintf,$1,$2,23001,23250)
303    $$(call ListPathsSafely_IfPrintf,$1,$2,23251,23500)
304    $$(call ListPathsSafely_IfPrintf,$1,$2,23501,23750)
305    $$(call ListPathsSafely_IfPrintf,$1,$2,23751,24000)
306
307    $$(call ListPathsSafely_IfPrintf,$1,$2,24001,24250)
308    $$(call ListPathsSafely_IfPrintf,$1,$2,24251,24500)
309    $$(call ListPathsSafely_IfPrintf,$1,$2,24501,24750)
310    $$(call ListPathsSafely_IfPrintf,$1,$2,24751,25000)
311
312    $$(call ListPathsSafely_IfPrintf,$1,$2,25001,25250)
313    $$(call ListPathsSafely_IfPrintf,$1,$2,25251,25500)
314    $$(call ListPathsSafely_IfPrintf,$1,$2,25501,25750)
315    $$(call ListPathsSafely_IfPrintf,$1,$2,25751,26000)
316
317    $$(call ListPathsSafely_IfPrintf,$1,$2,26001,26250)
318    $$(call ListPathsSafely_IfPrintf,$1,$2,26251,26500)
319    $$(call ListPathsSafely_IfPrintf,$1,$2,26501,26750)
320    $$(call ListPathsSafely_IfPrintf,$1,$2,26751,27000)
321
322    $$(call ListPathsSafely_IfPrintf,$1,$2,27001,27250)
323    $$(call ListPathsSafely_IfPrintf,$1,$2,27251,27500)
324    $$(call ListPathsSafely_IfPrintf,$1,$2,27501,27750)
325    $$(call ListPathsSafely_IfPrintf,$1,$2,27751,28000)
326
327    $$(call ListPathsSafely_IfPrintf,$1,$2,28001,28250)
328    $$(call ListPathsSafely_IfPrintf,$1,$2,28251,28500)
329    $$(call ListPathsSafely_IfPrintf,$1,$2,28501,28750)
330    $$(call ListPathsSafely_IfPrintf,$1,$2,28751,29000)
331
332    $$(call ListPathsSafely_IfPrintf,$1,$2,29001,29250)
333    $$(call ListPathsSafely_IfPrintf,$1,$2,29251,29500)
334    $$(call ListPathsSafely_IfPrintf,$1,$2,29501,29750)
335    $$(call ListPathsSafely_IfPrintf,$1,$2,29751,30000)
336  endef
337endif # HAS_FILE_FUNCTION
338
339################################################################################
340
341# A file containing a way to uniquely identify the source code revision that
342# the build was created from
343SOURCE_REVISION_TRACKER := $(SUPPORT_OUTPUTDIR)/src-rev/source-revision-tracker
344
345# Locate all hg repositories included in the forest, as absolute paths
346FindAllReposAbs = \
347    $(strip $(sort $(dir $(filter-out $(SRC_ROOT)/build/%, $(wildcard \
348        $(addprefix $(SRC_ROOT)/, .hg */.hg */*/.hg */*/*/.hg) \
349    )))))
350
351# Locate all hg repositories included in the forest, as relative paths
352FindAllReposRel = \
353    $(strip $(subst $(SRC_ROOT)/,.,$(patsubst $(SRC_ROOT)/%/, %, $(FindAllReposAbs))))
354
355################################################################################
356
357define SetupLogging
358  ifeq ($$(LOG_LEVEL), trace)
359    # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
360    # For each target executed, will print
361    # Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer)
362    # but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much
363    # (and causing a crash on Cygwin).
364    # Default shell seems to always be /bin/sh. Must override with bash to get this to work on Solaris.
365    # Only use time if it's GNU time which supports format and output file.
366    WRAPPER_SHELL := $$(BASH) $$(SRC_ROOT)/common/bin/shell-tracer.sh $$(if $$(findstring yes,$$(IS_GNU_TIME)),$$(TIME),-) $$(OUTPUT_ROOT)/build-trace-time.log $$(SHELL)
367    SHELL = $$(warning $$(if $$@,Building $$@,Running shell command) $$(if $$<, (from $$<))$$(if $$?, ($$(wordlist 1, 20, $$?) $$(if $$(wordlist 21, 22, $$?), ... [in total $$(words $$?) files]) newer)))$$(WRAPPER_SHELL)
368  endif
369  # The warn level can never be turned off
370  LogWarn = $$(info $$(strip $$1))
371  LOG_WARN :=
372  ifneq ($$(findstring $$(LOG_LEVEL), info debug trace),)
373    LogInfo = $$(info $$(strip $$1))
374    LOG_INFO :=
375  else
376    LogInfo =
377    LOG_INFO := > /dev/null
378  endif
379  ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
380    LogDebug = $$(info $$(strip $$1))
381    LOG_DEBUG :=
382  else
383    LogDebug =
384    LOG_DEBUG := > /dev/null
385  endif
386  ifneq ($$(findstring $$(LOG_LEVEL), trace),)
387    LogTrace = $$(info $$(strip $$1))
388    LOG_TRACE :=
389  else
390    LogTrace =
391    LOG_TRACE := > /dev/null
392  endif
393endef
394
395# Make sure logging is setup for everyone that includes MakeBase.gmk.
396$(eval $(call SetupLogging))
397
398################################################################################
399# Creates a sequence of increasing numbers (inclusive).
400# Param 1 - starting number
401# Param 2 - ending number
402sequence = \
403    $(wordlist $1, $2, $(strip \
404        $(eval SEQUENCE_COUNT :=) \
405        $(call _sequence-do,$(strip $2))))
406
407_sequence-do = \
408    $(if $(word $1, $(SEQUENCE_COUNT)),, \
409      $(eval SEQUENCE_COUNT += .) \
410      $(words $(SEQUENCE_COUNT)) \
411      $(call _sequence-do,$1))
412
413################################################################################
414
415MAX_PARAMS := 35
416PARAM_SEQUENCE := $(call sequence, 2, $(MAX_PARAMS))
417
418# Template for creating a macro taking named parameters. To use it, assign the
419# template to a variable with the name you want for your macro, using '='
420# assignment. Then define a macro body with the suffix "Body". The Body macro
421# should take 1 parameter which should be a unique string for that invocation
422# of the macro.
423# Ex:
424# SetupFoo = $(NamedParamsMacroTemplate)
425# define SetupFooBody
426#   # do something
427#   # access parameters as $$($1_BAR)
428# endef
429# Call it like this
430# $(eval $(call SetupFoo, BUILD_SOMETHING, \
431#     BAR := some parameter value, \
432# ))
433define NamedParamsMacroTemplate
434  $(if $($(MAX_PARAMS)),$(error Internal makefile error: \
435      Too many named arguments to macro, please update MAX_PARAMS in MakeBase.gmk))
436  # Iterate over 2 3 4... and evaluate the named parameters with $1_ as prefix
437  $(foreach i,$(PARAM_SEQUENCE), $(if $(strip $($i)),\
438    $(strip $1)_$(strip $(call DoubleDollar, $($i))))$(NEWLINE))
439  # Debug print all named parameter names and values
440  $(if $(findstring $(LOG_LEVEL),debug trace), \
441    $(info $0 $(strip $1) $(foreach i,$(PARAM_SEQUENCE), \
442      $(if $(strip $($i)),$(NEWLINE) $(strip [$i] $(if $(filter $(LOG_LEVEL), trace), \
443        $($i), $(wordlist 1, 20, $($(i))) $(if $(word 21, $($(i))), ...)))))))
444
445  $(if $(DEBUG_$(strip $1)),
446    $(info -------- <<< Begin expansion of $(strip $1)) \
447    $(info $(call $(0)Body,$(strip $1))) \
448    $(info -------- >>> End expansion of $(strip $1)) \
449  )
450
451  $(call $(0)Body,$(strip $1))
452endef
453
454################################################################################
455# Make directory without forking mkdir if not needed
456# 1: List of directories to create
457MakeDir = \
458    $(strip \
459        $(eval MakeDir_dirs_to_make := $(strip $(foreach d, $1, $(if $(wildcard $d), , $d)))) \
460        $(if $(MakeDir_dirs_to_make), $(shell $(MKDIR) -p $(MakeDir_dirs_to_make))) \
461    )
462
463################################################################################
464# Assign a variable only if it is empty
465# Param 1 - Variable to assign
466# Param 2 - Value to assign
467SetIfEmpty = \
468    $(if $($(strip $1)),,$(eval $(strip $1) := $2))
469
470################################################################################
471
472ifeq ($(OPENJDK_TARGET_OS),solaris)
473  # On Solaris, if the target is a symlink and exists, cp won't overwrite.
474  # Cp has to operate in recursive mode to allow for -P flag, to preserve soft links. If the
475  # name of the target file differs from the source file, rename after copy.
476  # If the source and target parent directories are the same, recursive copy doesn't work
477  # so we fall back on regular copy, which isn't preserving symlinks.
478  define install-file
479	$(MKDIR) -p '$(@D)'
480	$(RM) '$@'
481	if [ "$(@D)" != "$(<D)" ]; then \
482	  $(CP) -f -r -P '$<' '$(@D)'; \
483	  if [ "$(@F)" != "$(<F)" ]; then \
484	    $(MV) '$(@D)/$(<F)' '$@'; \
485	  fi; \
486	else \
487	  if [ -L '$<' ]; then \
488	    $(ECHO) "Source file is a symlink and target is in the same directory: $< $@" ; \
489	    exit 1; \
490	  fi; \
491	  $(CP) -f '$<' '$@'; \
492	fi
493  endef
494else ifeq ($(OPENJDK_TARGET_OS),macosx)
495  # On mac, extended attributes sometimes creep into the source files, which may later
496  # cause the creation of ._* files which confuses testing. Clear these with xattr if
497  # set. Some files get their write permissions removed after being copied to the
498  # output dir. When these are copied again to images, xattr would fail. By only clearing
499  # attributes when they are present, failing on this is avoided.
500  #
501  # If copying a soft link to a directory, need to delete the target first to avoid
502  # weird errors.
503  define install-file
504	$(MKDIR) -p '$(@D)'
505	$(RM) '$@'
506	$(CP) -fRP '$<' '$@'
507	if [ -n "`$(XATTR) -l '$@'`" ]; then $(XATTR) -c '$@'; fi
508  endef
509else
510  define install-file
511	$(call MakeDir, $(@D))
512	$(CP) -fP '$<' '$@'
513  endef
514endif
515
516# Variant of install file that does not preserve symlinks
517define install-file-nolink
518	$(call MakeDir, $(@D))
519	$(CP) -f '$<' '$@'
520endef
521
522################################################################################
523# Take two paths and return the path of the last common directory.
524# Ex: /foo/bar/baz, /foo/bar/banan -> /foo/bar
525#     foo/bar/baz, /foo/bar -> <empty>
526#
527# The x prefix is used to preserve the presence of the initial slash
528#
529# $1 - Path to compare
530# $2 - Other path to compare
531FindCommonPathPrefix = \
532    $(patsubst x%,%,$(subst $(SPACE),/,$(strip \
533        $(call FindCommonPathPrefixHelper, \
534            $(subst /,$(SPACE),x$(strip $1)), $(subst /,$(SPACE),x$(strip $2))) \
535    )))
536
537FindCommonPathPrefixHelper = \
538    $(if $(call equals, $(firstword $1), $(firstword $2)), \
539      $(firstword $1) \
540      $(call FindCommonPathPrefixHelper, \
541          $(wordlist 2, $(words $1), $1), $(wordlist 2, $(words $2), $2) \
542      ) \
543    )
544
545# Convert a partial path into as many directory levels of ../, removing
546# leading and following /.
547# Ex: foo/bar/baz/ -> ../../..
548#     foo/bar -> ../..
549#     /foo -> ..
550DirToDotDot = \
551    $(subst $(SPACE),/,$(foreach d, $(subst /,$(SPACE),$1),..))
552
553# Computes the relative path from a directory to a file
554# $1 - File to compute the relative path to
555# $2 - Directory to compute the relative path from
556RelativePath = \
557    $(eval $1_prefix := $(call FindCommonPathPrefix, $1, $2)) \
558    $(eval $1_dotdots := $(call DirToDotDot, $(patsubst $($(strip $1)_prefix)/%, %, $2))) \
559    $(eval $1_suffix := $(patsubst $($(strip $1)_prefix)/%, %, $1)) \
560    $($(strip $1)_dotdots)/$($(strip $1)_suffix)
561
562################################################################################
563# link-file-* works similarly to install-file but creates a symlink instead.
564# There are two versions, either creating a relative or an absolute link. Be
565# careful when using this on Windows since the symlink created is only valid in
566# the unix emulation environment.
567define link-file-relative
568	$(call MakeDir, $(@D))
569	$(RM) $@
570	$(LN) -s $(call RelativePath, $<, $(@D)) $@
571endef
572
573define link-file-absolute
574	$(call MakeDir, $(@D))
575	$(RM) $@
576	$(LN) -s $< $@
577endef
578
579################################################################################
580# Filter out duplicate sub strings while preserving order. Keeps the first occurance.
581uniq = \
582    $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
583
584# Returns all whitespace-separated words in $2 where at least one of the
585# whitespace-separated words in $1 is a substring.
586containing = \
587    $(strip \
588        $(foreach v,$(strip $2),\
589          $(call uniq,$(foreach p,$(strip $1),$(if $(findstring $p,$v),$v)))))
590
591# Returns all whitespace-separated words in $2 where none of the
592# whitespace-separated words in $1 is a substring.
593not-containing = \
594    $(strip $(filter-out $(call containing,$1,$2),$2))
595
596# Return a list of all string elements that are duplicated in $1.
597dups = \
598    $(strip $(foreach v, $(sort $1), $(if $(filter-out 1, \
599        $(words $(filter $v, $1))), $v)))
600
601# String equals
602equals = \
603    $(and $(findstring $(strip $1),$(strip $2)),\
604        $(findstring $(strip $2),$(strip $1)))
605
606# Remove a whole list of prefixes
607# $1 - List of prefixes
608# $2 - List of elements to process
609remove-prefixes = \
610    $(strip $(if $1,$(patsubst $(firstword $1)%,%,\
611      $(call remove-prefixes,$(filter-out $(firstword $1),$1),$2)),$2))
612
613# Convert the string given to upper case, without any $(shell)
614# Inspired by http://lists.gnu.org/archive/html/help-make/2013-09/msg00009.html
615uppercase_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 \
616    p,P q,Q r,R s,S t,T u,U v,V w,W x,X y,Y z,Z
617
618uppercase_internal = \
619  $(if $(strip $1), $$(subst $(firstword $1), $(call uppercase_internal, \
620      $(wordlist 2, $(words $1), $1), $2)), $2)
621
622# Convert a string to upper case. Works only on a-z.
623# $1 - The string to convert
624uppercase = \
625  $(strip \
626    $(eval uppercase_result := $(call uppercase_internal, $(uppercase_table), $1)) \
627    $(uppercase_result) \
628  )
629
630################################################################################
631
632ifneq ($(DISABLE_CACHE_FIND), true)
633  # In Cygwin, finds are very costly, both because of expensive forks and because
634  # of bad file system caching. Find is used extensively in $(shell) commands to
635  # find source files. This makes rerunning make with no or few changes rather
636  # expensive. To speed this up, these two macros are used to cache the results
637  # of simple find commands for reuse.
638  #
639  # Runs a find and stores both the directories where it was run and the results.
640  # This macro can be called multiple times to add to the cache. Only finds files
641  # with no filters.
642  #
643  # Needs to be called with $(eval )
644  #
645  # Even if the performance benifit is negligible on other platforms, keep the
646  # functionality active unless explicitly disabled to exercise it more.
647  #
648  # Initialize FIND_CACHE_DIRS with := to make it a non recursively-expanded variable
649  FIND_CACHE_DIRS :=
650  # Param 1 - Dirs to find in
651  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
652  define FillCacheFind
653    # Filter out already cached dirs. The - is needed when FIND_CACHE_DIRS is empty
654    # since filter out will then return empty.
655    FIND_CACHE_NEW_DIRS := $$(filter-out $$(addsuffix /%,\
656        - $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS), $1)
657    ifneq ($$(FIND_CACHE_NEW_DIRS), )
658      # Remove any trailing slash from dirs in the cache dir list
659      FIND_CACHE_DIRS += $$(patsubst %/,%, $$(FIND_CACHE_NEW_DIRS))
660      FIND_CACHE := $$(sort $$(FIND_CACHE) $$(shell $(FIND) $$(FIND_CACHE_NEW_DIRS) \( -type f -o -type l \) $2))
661    endif
662  endef
663
664  # Mimics find by looking in the cache if all of the directories have been cached.
665  # Otherwise reverts to shell find. This is safe to call on all platforms, even if
666  # cache is deactivated.
667  #
668  # $1 can be either a directory or a file. If it's a directory, make
669  # sure we have exactly one trailing slash before the wildcard.
670  # The extra - is needed when FIND_CACHE_DIRS is empty but should be harmless.
671  #
672  # Param 1 - Dirs to find in
673  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
674  define CacheFind
675      $(if $(filter-out $(addsuffix /%,- $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS),$1), \
676    $(shell $(FIND) $1 \( -type f -o -type l \) $2), \
677        $(filter $(addsuffix /%,$(patsubst %/,%,$1)) $1,$(FIND_CACHE)))
678  endef
679
680else
681  # If CacheFind is disabled, just run the find command.
682  # Param 1 - Dirs to find in
683  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
684  define CacheFind
685    $(shell $(FIND) $1 \( -type f -o -type l \) $2)
686  endef
687endif
688
689################################################################################
690
691define AddFileToCopy
692  # Helper macro for SetupCopyFiles
693  # 1 : Source file
694  # 2 : Dest file
695  # 3 : Variable to add targets to
696  # 4 : Macro to call for copy operation
697  $2: $1
698	$$(call LogInfo, Copying $$(patsubst $(OUTPUT_ROOT)/%,%,$$@))
699	$$($$(strip $4))
700
701  $3 += $2
702endef
703
704# Returns the value of the first argument
705identity = \
706    $(strip $1)
707
708# Setup make rules for copying files, with an option to do more complex
709# processing instead of copying.
710#
711# Parameter 1 is the name of the rule. This name is used as variable prefix,
712# and the targets generated are listed in a variable by that name.
713#
714# Remaining parameters are named arguments. These include:
715#   SRC     : Source root dir (defaults to dir of first file)
716#   DEST    : Dest root dir
717#   FILES   : List of files to copy with absolute paths, or path relative to SRC.
718#             Must be in SRC.
719#   FLATTEN : Set to flatten the directory structure in the DEST dir.
720#   MACRO   : Optionally override the default macro used for making the copy.
721#             Default is 'install-file'
722#   NAME_MACRO : Optionally supply a macro that rewrites the target file name
723#                based on the source file name
724SetupCopyFiles = $(NamedParamsMacroTemplate)
725define SetupCopyFilesBody
726
727  ifeq ($$($1_MACRO), )
728    $1_MACRO := install-file
729  endif
730
731  # Default SRC to the dir of the first file.
732  ifeq ($$($1_SRC), )
733    $1_SRC := $$(dir $$(firstword $$($1_FILES)))
734  endif
735
736  ifeq ($$($1_NAME_MACRO), )
737    $1_NAME_MACRO := identity
738  endif
739
740  # Remove any trailing slash from SRC and DEST
741  $1_SRC := $$(patsubst %/,%,$$($1_SRC))
742  $1_DEST := $$(patsubst %/,%,$$($1_DEST))
743
744  $$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \
745      $$(eval $$(call AddFileToCopy, $$($1_SRC)/$$f, \
746      $$($1_DEST)/$$(call $$(strip $$($1_NAME_MACRO)),$$(if $$($1_FLATTEN),$$(notdir $$f),$$f)), \
747      $1, $$($1_MACRO))))
748
749endef
750
751################################################################################
752# ShellQuote
753#
754# Quotes a string with single quotes and replaces single quotes with '\'' so
755# that the contents survives being given to the shell.
756
757ShellQuote = \
758    $(SQUOTE)$(subst $(SQUOTE),$(SQUOTE)\$(SQUOTE)$(SQUOTE),$(strip $1))$(SQUOTE)
759
760################################################################################
761# Write to and read from file
762
763# Param 1 - File to read
764ReadFile = \
765    $(shell $(CAT) $1)
766
767# Param 1 - Text to write
768# Param 2 - File to write to
769ifeq ($(HAS_FILE_FUNCTION), true)
770  WriteFile = \
771      $(file >$2,$(strip $1))
772else
773  # Use printf to get consistent behavior on all platforms.
774  WriteFile = \
775      $(shell $(PRINTF) "%s" $(call ShellQuote, $1) > $2)
776endif
777
778################################################################################
779# DependOnVariable
780#
781# This macro takes a variable name and puts the value in a file only if the
782# value has changed since last. The name of the file is returned. This can be
783# used to create rule dependencies on make variable values. The following
784# example would get rebuilt if the value of SOME_VAR was changed:
785#
786# path/to/some-file: $(call DependOnVariable, SOME_VAR)
787#         echo $(SOME_VAR) > $@
788#
789# Note that leading and trailing white space in the value is ignored.
790#
791
792# Defines the sub directory structure to store variable value file in
793DependOnVariableDirName = \
794    $(strip $(addsuffix $(if $(MODULE),/$(MODULE)), \
795        $(subst $(SRC_ROOT)/,, $(if $(filter /%, $(firstword $(MAKEFILE_LIST))), \
796          $(firstword $(MAKEFILE_LIST)), \
797          $(CURDIR)/$(firstword $(MAKEFILE_LIST))))))
798
799# Defines the name of the file to store variable value in. Generates a name
800# unless parameter 2 is given.
801# Param 1 - Name of variable
802# Param 2 - (optional) name of file to store value in
803DependOnVariableFileName = \
804    $(strip $(if $(strip $2), $2, \
805      $(MAKESUPPORT_OUTPUTDIR)/vardeps/$(DependOnVariableDirName)/$(strip $1).vardeps))
806
807# Does the actual work with parameters stripped.
808# If the file exists AND the contents is the same as the variable, do nothing
809# else print a new file.
810# Always returns the name of the file where the value was printed.
811# Param 1 - Name of variable
812# Param 2 - (optional) name of file to store value in
813DependOnVariableHelper = \
814    $(strip \
815        $(eval -include $(call DependOnVariableFileName, $1, $2)) \
816        $(if $(call equals, $(strip $($1)), $(strip $($1_old))),,\
817          $(call MakeDir, $(dir $(call DependOnVariableFileName, $1, $2))) \
818          $(if $(findstring $(LOG_LEVEL), trace), \
819              $(info NewVariable $1: >$(strip $($1))<) \
820              $(info OldVariable $1: >$(strip $($1_old))<)) \
821          $(call WriteFile, $1_old:=$(call DoubleDollar,$(call EscapeHash,$($1))), \
822              $(call DependOnVariableFileName, $1, $2))) \
823        $(call DependOnVariableFileName, $1, $2) \
824    )
825
826# Main macro
827# Param 1 - Name of variable
828# Param 2 - (optional) name of file to store value in
829DependOnVariable = \
830    $(call DependOnVariableHelper,$(strip $1),$(strip $2))
831
832# LogCmdlines is only intended to be used by ExecuteWithLog
833ifeq ($(LOG_CMDLINES), true)
834  LogCmdlines = $(info $(strip $1))
835else
836  LogCmdlines =
837endif
838
839################################################################################
840# ExecuteWithLog will run a command and log the output appropriately. This is
841# meant to be used by commands that do "real" work, like a compilation.
842# The output is stored in a specified log file, which is displayed at the end
843# of the build in case of failure. The  command line itself is stored in a file,
844# and also logged to stdout if the LOG=cmdlines option has been given.
845#
846# NOTE: If the command redirects stdout, the caller needs to wrap it in a
847# subshell (by adding parentheses around it), otherwise the redirect to the
848# subshell tee process will create a race condition where the target file may
849# not be fully written when the make recipe is done.
850#
851# Param 1 - The path to base the name of the log file / command line file on
852# Param 2 - The command to run
853ExecuteWithLog = \
854  $(call LogCmdlines, Exececuting: [$(strip $2)]) \
855  $(call WriteFile, $2, $(strip $1).cmdline) \
856  ( $(strip $2) > >($(TEE) $(strip $1).log) 2> >($(TEE) $(strip $1).log >&2) || \
857      ( exitcode=$(DOLLAR)? && \
858      $(CP) $(strip $1).log $(MAKESUPPORT_OUTPUTDIR)/failure-logs/$(subst /,_,$(patsubst $(BUILD_OUTPUT)/%,%,$(strip $1))).log && \
859      exit $(DOLLAR)exitcode ) )
860
861################################################################################
862# Find lib dir for module
863# Param 1 - module name
864FindLibDirForModule = \
865    $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)
866
867################################################################################
868# Return a string suitable for use after a -classpath or --module-path option. It
869# will be correct and safe to use on all platforms. Arguments are given as space
870# separate classpath entries. Safe for multiple nested calls.
871# param 1 : A space separated list of classpath entries
872# The surrounding strip is needed to keep additional whitespace out
873PathList = \
874  "$(subst $(SPACE),$(PATH_SEP),$(strip $(subst $(DQUOTE),,$1)))"
875
876################################################################################
877# Check if a specified hotspot variant is being built, or at least one of a
878# list of variants. Will return 'true' or 'false'.
879# $1 - the variant to test for
880check-jvm-variant = \
881  $(strip \
882    $(if $(filter-out $(VALID_JVM_VARIANTS), $1), \
883      $(error Internal error: Invalid variant tested: $1)) \
884    $(if $(filter $1, $(JVM_VARIANTS)), true, false))
885
886################################################################################
887# Converts a space separated list to a comma separated list.
888#
889# Replacing double-comma with a single comma is to workaround the issue with
890# some version of make on windows that doesn't substitute spaces with one comma
891# properly.
892CommaList = \
893  $(strip \
894      $(subst $(COMMA)$(COMMA),$(COMMA),$(subst $(SPACE),$(COMMA),$(strip $1))) \
895  )
896
897################################################################################
898
899# Hook to include the corresponding custom file, if present.
900$(eval $(call IncludeCustomExtension, , common/MakeBase.gmk))
901
902endif # _MAKEBASE_GMK
903