MakeBase.gmk revision 1664:c800f2919a1f
1#
2# Copyright (c) 2011, 2015, 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##############################
68# Functions
69##############################
70
71### Debug functions
72
73# Prints the name and value of a variable
74PrintVar = \
75    $(info $(strip $1) >$($(strip $1))<)
76
77### Functions for timers
78
79# Store the build times in this directory.
80BUILDTIMESDIR=$(OUTPUT_ROOT)/make-support/build-times
81
82# Record starting time for build of a sub repository.
83define RecordStartTime
84	$(MKDIR) -p $(BUILDTIMESDIR)
85	$(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)
86	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_start_$(strip $1)_human_readable
87endef
88
89# Record ending time and calculate the difference and store it in a
90# easy to read format. Handles builds that cross midnight. Expects
91# that a build will never take 24 hours or more.
92define RecordEndTime
93	$(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)
94	$(DATE) '+%Y-%m-%d %H:%M:%S' > $(BUILDTIMESDIR)/build_time_end_$(strip $1)_human_readable
95	$(ECHO) `$(CAT) $(BUILDTIMESDIR)/build_time_start_$(strip $1)` `$(CAT) $(BUILDTIMESDIR)/build_time_end_$(strip $1)` $1 | \
96	    $(NAWK) '{ F=$$7; T=$$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); \
97	    M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$$15); }' \
98	    > $(BUILDTIMESDIR)/build_time_diff_$(strip $1)
99endef
100
101# Hook to be called when starting to execute a top-level target
102define TargetEnter
103	$(PRINTF) "## Starting $(patsubst %-only,%,$@)\n"
104	$(call RecordStartTime,$(patsubst %-only,%,$@))
105endef
106
107# Hook to be called when finish executing a top-level target
108define TargetExit
109	$(call RecordEndTime,$(patsubst %-only,%,$@))
110	$(PRINTF) "## Finished $(patsubst %-only,%,$@) (build time %s)\n\n" \
111	    "`$(CAT) $(BUILDTIMESDIR)/build_time_diff_$(patsubst %-only,%,$@) | $(CUT) -f 1 -d ' '`"
112endef
113
114################################################################################
115# This macro translates $ into \$ to protect the $ from expansion in the shell.
116# To make this macro resilient against already escaped strings, first remove
117# any present escapes before escaping so that no double escapes are added.
118EscapeDollar = $(subst $$,\$$,$(subst \$$,$$,$(strip $1)))
119
120################################################################################
121# This macro translates $ into $$ to protect the string from make itself.
122DoubleDollar = $(subst $$,$$$$,$(strip $1))
123
124################################################################################
125
126# If the variable that you want to send to stdout for piping into a file or otherwise,
127# is potentially long, for example the a list of file paths, eg a list of all package directories.
128# Then you need to use ListPathsSafely, which optimistically splits the output into several shell
129# calls as well as use compression on recurrent file paths segments, to get around the potential
130# command line length problem that exists in cygwin and other shells.
131compress_pre:=$(strip $(shell $(CAT) $(SRC_ROOT)/make/common/support/ListPathsSafely-pre-compress.incl))
132compress_post:=$(strip $(shell $(CAT) $(SRC_ROOT)/make/common/support/ListPathsSafely-post-compress.incl))
133compress_paths=$(compress_pre)\
134$(subst $(SRC_ROOT),X97,\
135$(subst $(OUTPUT_ROOT),X98,\
136$(subst X,X00,\
137$(subst $(SPACE),\n,$(strip $1)))))\
138$(compress_post)
139
140decompress_paths=$(SED) -f $(SRC_ROOT)/make/common/support/ListPathsSafely-uncompress.sed -e 's|X99|\\n|g' \
141    -e 's|X98|$(OUTPUT_ROOT)|g' -e 's|X97|$(SRC_ROOT)|g' \
142    -e 's|X00|X|g' | tr '\n' '$2'
143
144define ListPathsSafely_If
145	$(if $(word $3,$($1)),$(eval $1_LPS$3:=$(call compress_paths,$(wordlist $3,$4,$($1)))))
146endef
147
148define ListPathsSafely_Printf
149	$(if $(strip $($1_LPS$4)),$(if $(findstring $(LOG_LEVEL),trace),,@)printf \
150	    -- "$(strip $(call EscapeDollar, $($1_LPS$4)))\n" | $(decompress_paths) $3)
151endef
152
153# Receipt example:
154#   rm -f thepaths
155#   $(call ListPathsSafely,THEPATHS,\n, >> thepaths)
156# The \n argument means translate spaces into \n
157# if instead , , (a space) is supplied, then spaces remain spaces.
158define ListPathsSafely
159	$(if $(word 16001,$($1)),$(error Cannot list safely more than 16000 paths. $1 has $(words $($1)) paths!))
160	$(ECHO) $(LOG_DEBUG) Writing $(words $($1)) paths to '$3'
161	$(call ListPathsSafely_If,$1,$2,1,250)
162	$(call ListPathsSafely_If,$1,$2,251,500)
163	$(call ListPathsSafely_If,$1,$2,501,750)
164	$(call ListPathsSafely_If,$1,$2,751,1000)
165
166	$(call ListPathsSafely_If,$1,$2,1001,1250)
167	$(call ListPathsSafely_If,$1,$2,1251,1500)
168	$(call ListPathsSafely_If,$1,$2,1501,1750)
169	$(call ListPathsSafely_If,$1,$2,1751,2000)
170
171	$(call ListPathsSafely_If,$1,$2,2001,2250)
172	$(call ListPathsSafely_If,$1,$2,2251,2500)
173	$(call ListPathsSafely_If,$1,$2,2501,2750)
174	$(call ListPathsSafely_If,$1,$2,2751,3000)
175
176	$(call ListPathsSafely_If,$1,$2,3001,3250)
177	$(call ListPathsSafely_If,$1,$2,3251,3500)
178	$(call ListPathsSafely_If,$1,$2,3501,3750)
179	$(call ListPathsSafely_If,$1,$2,3751,4000)
180
181	$(call ListPathsSafely_If,$1,$2,4001,4250)
182	$(call ListPathsSafely_If,$1,$2,4251,4500)
183	$(call ListPathsSafely_If,$1,$2,4501,4750)
184	$(call ListPathsSafely_If,$1,$2,4751,5000)
185
186	$(call ListPathsSafely_If,$1,$2,5001,5250)
187	$(call ListPathsSafely_If,$1,$2,5251,5500)
188	$(call ListPathsSafely_If,$1,$2,5501,5750)
189	$(call ListPathsSafely_If,$1,$2,5751,6000)
190
191	$(call ListPathsSafely_If,$1,$2,6001,6250)
192	$(call ListPathsSafely_If,$1,$2,6251,6500)
193	$(call ListPathsSafely_If,$1,$2,6501,6750)
194	$(call ListPathsSafely_If,$1,$2,6751,7000)
195
196	$(call ListPathsSafely_If,$1,$2,7001,7250)
197	$(call ListPathsSafely_If,$1,$2,7251,7500)
198	$(call ListPathsSafely_If,$1,$2,7501,7750)
199	$(call ListPathsSafely_If,$1,$2,7751,8000)
200
201	$(call ListPathsSafely_If,$1,$2,8001,8250)
202	$(call ListPathsSafely_If,$1,$2,8251,8500)
203	$(call ListPathsSafely_If,$1,$2,8501,8750)
204	$(call ListPathsSafely_If,$1,$2,8751,9000)
205
206	$(call ListPathsSafely_If,$1,$2,9001,9250)
207	$(call ListPathsSafely_If,$1,$2,9251,9500)
208	$(call ListPathsSafely_If,$1,$2,9501,9750)
209	$(call ListPathsSafely_If,$1,$2,9751,10000)
210
211	$(call ListPathsSafely_If,$1,$2,10001,10250)
212	$(call ListPathsSafely_If,$1,$2,10251,10500)
213	$(call ListPathsSafely_If,$1,$2,10501,10750)
214	$(call ListPathsSafely_If,$1,$2,10751,11000)
215
216	$(call ListPathsSafely_If,$1,$2,11001,11250)
217	$(call ListPathsSafely_If,$1,$2,11251,11500)
218	$(call ListPathsSafely_If,$1,$2,11501,11750)
219	$(call ListPathsSafely_If,$1,$2,11751,12000)
220
221	$(call ListPathsSafely_If,$1,$2,12001,12250)
222	$(call ListPathsSafely_If,$1,$2,12251,12500)
223	$(call ListPathsSafely_If,$1,$2,12501,12750)
224	$(call ListPathsSafely_If,$1,$2,12751,13000)
225
226	$(call ListPathsSafely_If,$1,$2,13001,13250)
227	$(call ListPathsSafely_If,$1,$2,13251,13500)
228	$(call ListPathsSafely_If,$1,$2,13501,13750)
229	$(call ListPathsSafely_If,$1,$2,13751,14000)
230
231	$(call ListPathsSafely_If,$1,$2,14001,14250)
232	$(call ListPathsSafely_If,$1,$2,14251,14500)
233	$(call ListPathsSafely_If,$1,$2,14501,14750)
234	$(call ListPathsSafely_If,$1,$2,14751,15000)
235
236	$(call ListPathsSafely_If,$1,$2,15001,15250)
237	$(call ListPathsSafely_If,$1,$2,15251,15500)
238	$(call ListPathsSafely_If,$1,$2,15501,15750)
239	$(call ListPathsSafely_If,$1,$2,15751,16000)
240
241	$(call ListPathsSafely_Printf,$1,$2,$3,1)
242	$(call ListPathsSafely_Printf,$1,$2,$3,251)
243	$(call ListPathsSafely_Printf,$1,$2,$3,501)
244	$(call ListPathsSafely_Printf,$1,$2,$3,751)
245
246	$(call ListPathsSafely_Printf,$1,$2,$3,1001)
247	$(call ListPathsSafely_Printf,$1,$2,$3,1251)
248	$(call ListPathsSafely_Printf,$1,$2,$3,1501)
249	$(call ListPathsSafely_Printf,$1,$2,$3,1751)
250
251	$(call ListPathsSafely_Printf,$1,$2,$3,2001)
252	$(call ListPathsSafely_Printf,$1,$2,$3,2251)
253	$(call ListPathsSafely_Printf,$1,$2,$3,2501)
254	$(call ListPathsSafely_Printf,$1,$2,$3,2751)
255
256	$(call ListPathsSafely_Printf,$1,$2,$3,3001)
257	$(call ListPathsSafely_Printf,$1,$2,$3,3251)
258	$(call ListPathsSafely_Printf,$1,$2,$3,3501)
259	$(call ListPathsSafely_Printf,$1,$2,$3,3751)
260
261	$(call ListPathsSafely_Printf,$1,$2,$3,4001)
262	$(call ListPathsSafely_Printf,$1,$2,$3,4251)
263	$(call ListPathsSafely_Printf,$1,$2,$3,4501)
264	$(call ListPathsSafely_Printf,$1,$2,$3,4751)
265
266	$(call ListPathsSafely_Printf,$1,$2,$3,5001)
267	$(call ListPathsSafely_Printf,$1,$2,$3,5251)
268	$(call ListPathsSafely_Printf,$1,$2,$3,5501)
269	$(call ListPathsSafely_Printf,$1,$2,$3,5751)
270
271	$(call ListPathsSafely_Printf,$1,$2,$3,6001)
272	$(call ListPathsSafely_Printf,$1,$2,$3,6251)
273	$(call ListPathsSafely_Printf,$1,$2,$3,6501)
274	$(call ListPathsSafely_Printf,$1,$2,$3,6751)
275
276	$(call ListPathsSafely_Printf,$1,$2,$3,7001)
277	$(call ListPathsSafely_Printf,$1,$2,$3,7251)
278	$(call ListPathsSafely_Printf,$1,$2,$3,7501)
279	$(call ListPathsSafely_Printf,$1,$2,$3,7751)
280
281	$(call ListPathsSafely_Printf,$1,$2,$3,8001)
282	$(call ListPathsSafely_Printf,$1,$2,$3,8251)
283	$(call ListPathsSafely_Printf,$1,$2,$3,8501)
284	$(call ListPathsSafely_Printf,$1,$2,$3,8751)
285
286	$(call ListPathsSafely_Printf,$1,$2,$3,9001)
287	$(call ListPathsSafely_Printf,$1,$2,$3,9251)
288	$(call ListPathsSafely_Printf,$1,$2,$3,9501)
289	$(call ListPathsSafely_Printf,$1,$2,$3,9751)
290
291	$(call ListPathsSafely_Printf,$1,$2,$3,10001)
292	$(call ListPathsSafely_Printf,$1,$2,$3,10251)
293	$(call ListPathsSafely_Printf,$1,$2,$3,10501)
294	$(call ListPathsSafely_Printf,$1,$2,$3,10751)
295
296	$(call ListPathsSafely_Printf,$1,$2,$3,11001)
297	$(call ListPathsSafely_Printf,$1,$2,$3,11251)
298	$(call ListPathsSafely_Printf,$1,$2,$3,11501)
299	$(call ListPathsSafely_Printf,$1,$2,$3,11751)
300
301	$(call ListPathsSafely_Printf,$1,$2,$3,12001)
302	$(call ListPathsSafely_Printf,$1,$2,$3,12251)
303	$(call ListPathsSafely_Printf,$1,$2,$3,12501)
304	$(call ListPathsSafely_Printf,$1,$2,$3,12751)
305
306	$(call ListPathsSafely_Printf,$1,$2,$3,13001)
307	$(call ListPathsSafely_Printf,$1,$2,$3,13251)
308	$(call ListPathsSafely_Printf,$1,$2,$3,13501)
309	$(call ListPathsSafely_Printf,$1,$2,$3,13751)
310
311	$(call ListPathsSafely_Printf,$1,$2,$3,14001)
312	$(call ListPathsSafely_Printf,$1,$2,$3,14251)
313	$(call ListPathsSafely_Printf,$1,$2,$3,14501)
314	$(call ListPathsSafely_Printf,$1,$2,$3,14751)
315
316	$(call ListPathsSafely_Printf,$1,$2,$3,15001)
317	$(call ListPathsSafely_Printf,$1,$2,$3,15251)
318	$(call ListPathsSafely_Printf,$1,$2,$3,15501)
319	$(call ListPathsSafely_Printf,$1,$2,$3,15751)
320endef
321
322define ListPathsSafelyNow_IfPrintf
323  ifneq (,$$(word $4,$$($1)))
324    $$(eval $1_LPS$4:=$$(call compress_paths,$$(wordlist $4,$5,$$($1))))
325    $$(shell printf -- "$$(strip $$($1_LPS$4))\n" | $(decompress_paths) $3)
326  endif
327endef
328
329# And an non-receipt version:
330define ListPathsSafelyNow
331  ifneq (,$$(word 10001,$$($1)))
332    $$(error Cannot list safely more than 10000 paths. $1 has $$(words $$($1)) paths!)
333  endif
334  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,1,250)
335  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,251,500)
336  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,501,750)
337  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,751,1000)
338
339  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,1001,1250)
340  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,1251,1500)
341  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,1501,1750)
342  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,1751,2000)
343
344  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,2001,2250)
345  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,2251,2500)
346  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,2501,2750)
347  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,2751,3000)
348
349  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,3001,3250)
350  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,3251,3500)
351  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,3501,3750)
352  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,3751,4000)
353
354  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,4001,4250)
355  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,4251,4500)
356  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,4501,4750)
357  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,4751,5000)
358
359  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,5001,5250)
360  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,5251,5500)
361  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,5501,5750)
362  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,5751,6000)
363
364  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,6001,6250)
365  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,6251,6500)
366  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,6501,6750)
367  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,6751,7000)
368
369  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,7001,7250)
370  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,7251,7500)
371  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,7501,7750)
372  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,7751,8000)
373
374  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,8001,8250)
375  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,8251,8500)
376  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,8501,8750)
377  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,8751,9000)
378
379  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,9001,9250)
380  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,9251,9500)
381  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,9501,9750)
382  $(call ListPathsSafelyNow_IfPrintf,$1,$2,$3,9751,10000)
383
384endef
385
386# The source tips can come from the Mercurial repository, or in the files
387# $(HGTIP_FILENAME) which contains the tip but is also positioned in the same
388# directory as the original $(HGDIR) directory.
389# These should not be := assignments, only used from the root Makefile.
390HG_VERSION = $(shell $(HG) version 2> /dev/null)
391HG_DIRECTORY=.hg
392HGTIP_FILENAME=.hgtip
393HG_SEARCH = ./REPO ./*/REPO ./*/*/REPO ./*/*/*/REPO
394REPO_LIST = $(patsubst ./%,%,$(patsubst %/,%,$(sort $(dir \
395    $(shell $(CD) $(SRC_ROOT) ; \
396        $(LS) -d $(HG_SEARCH:%/REPO=%/$(HG_DIRECTORY)) \
397            $(HG_SEARCH:%/REPO=%/$(HGTIP_FILENAME)) \
398        2> /dev/null)))))
399
400# Emit the repo:tip pairs to $@
401define GetSourceTips
402	$(CD) $(SRC_ROOT) ; \
403	for i in $(REPO_LIST) IGNORE ; do \
404	  if [ "$${i}" = "IGNORE" ] ; then \
405	    continue; \
406	  elif [ -d $${i}/$(HG_DIRECTORY) -a "$(HG_VERSION)" != "" ] ; then \
407	    $(PRINTF) " %s:%s" \
408	        "$${i}" `$(HG) tip --repository $${i} --template '{node|short}\n'` ; \
409	  elif [ -f $${i}/$(HGTIP_FILENAME) ] ; then \
410	    $(PRINTF) " %s:%s" \
411	        "$${i}" `$(CAT) $${i}/$(HGTIP_FILENAME)` ; \
412	  fi; \
413	done >> $@
414	$(PRINTF) "\n" >> $@
415endef
416
417# Create the HGTIP_FILENAME file. Called from jdk/make/closed/bundles.gmk
418define CreateHgTip
419	$(HG) tip --repository $1 --template '{node|short}\n' > $1/$(HGTIP_FILENAME); \
420	$(ECHO) $1/$(HGTIP_FILENAME)
421endef
422
423define SetupLogging
424  ifeq ($$(LOG_LEVEL), trace)
425    # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
426    # For each target executed, will print
427    # Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer)
428    # but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much
429    # (and causing a crash on Cygwin).
430    # Default shell seems to always be /bin/sh. Must override with bash to get this to work on Solaris.
431    # Only use time if it's GNU time which supports format and output file.
432    WRAPPER_SHELL := $$(BASH) $$(SRC_ROOT)/common/bin/shell-tracer.sh $$(if $$(findstring yes,$$(IS_GNU_TIME)),$$(TIME),-) $$(OUTPUT_ROOT)/build-trace-time.log $$(SHELL)
433    SHELL := $$(warning $$(if $$@,Building $$@,Running shell command) $$(if $$<, (from $$<))$$(if $$?, ($$(wordlist 1, 20, $$?) $$(if $$(wordlist 21, 22, $$?), ... [in total $$(words $$?) files]) newer)))$$(WRAPPER_SHELL)
434  endif
435  # Never remove warning messages; this is just for completeness
436  LOG_WARN :=
437  ifneq ($$(findstring $$(LOG_LEVEL), info debug trace),)
438    LOG_INFO :=
439  else
440    LOG_INFO := > /dev/null
441  endif
442  ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
443    LOG_DEBUG :=
444  else
445    LOG_DEBUG := > /dev/null
446  endif
447  ifneq ($$(findstring $$(LOG_LEVEL), trace),)
448    LOG_TRACE :=
449  else
450    LOG_TRACE := > /dev/null
451  endif
452endef
453
454# Make sure logging is setup for everyone that includes MakeBase.gmk.
455$(eval $(call SetupLogging))
456
457################################################################################
458# Creates a sequence of increasing numbers (inclusive).
459# Param 1 - starting number
460# Param 2 - ending number
461sequence = \
462    $(wordlist $1, $2, $(strip \
463        $(eval SEQUENCE_COUNT :=) \
464        $(call _sequence-do,$(strip $2))))
465
466_sequence-do = \
467    $(if $(word $1, $(SEQUENCE_COUNT)),, \
468      $(eval SEQUENCE_COUNT += .) \
469      $(words $(SEQUENCE_COUNT)) \
470      $(call _sequence-do,$1))
471
472################################################################################
473
474MAX_PARAMS := 30
475PARAM_SEQUENCE := $(call sequence, 2, $(MAX_PARAMS))
476
477# Template for creating a macro taking named parameters. To use it, assign the
478# template to a variable with the name you want for your macro, using '='
479# assignment. Then define a macro body with the suffix "Body". The Body macro
480# should take 1 parameter which should be a unique string for that invocation
481# of the macro.
482# Ex:
483# SetupFoo = $(NamedParamsMacroTemplate)
484# define SetupFooBody
485#   # do something
486#   # access parameters as $$($1_BAR)
487# endef
488# Call it like this
489# $(eval $(call SetupFoo, BUILD_SOMETHING, \
490#     BAR := some parameter value, \
491# ))
492define NamedParamsMacroTemplate
493  $(if $($(MAX_PARAMS)),$(error Internal makefile error: \
494      Too many named arguments to macro, please update MAX_PARAMS in MakeBase.gmk))
495  # Iterate over 2 3 4... and evaluate the named parameters with $1_ as prefix
496  $(foreach i,$(PARAM_SEQUENCE), $(if $(strip $($i)),\
497    $(strip $1)_$(strip $($i)))$(NEWLINE))
498  # Debug print all named parameter names and values
499  $(if $(findstring $(LOG_LEVEL),debug trace), \
500    $(info $0 $(strip $1) $(foreach i,$(PARAM_SEQUENCE), \
501      $(if $(strip $($i)),$(NEWLINE) $(strip [$i] $(if $(filter $(LOG_LEVEL), trace), \
502        $($i), $(wordlist 1, 20, $($(i))) $(if $(word 21, $($(i))), ...)))))))
503
504  $(if $(DEBUG_$(strip $1)),
505    $(info -------- <<< Begin expansion of $(strip $1)) \
506    $(info $(call $(0)Body,$(strip $1))) \
507    $(info -------- >>> End expansion of $(strip $1)) \
508  )
509
510  $(call $(0)Body,$(strip $1))
511endef
512
513################################################################################
514# Make directory without forking mkdir if not needed
515MakeDir = \
516    $(strip $(if $(subst $(wildcard $1 $2 $3 $4 $5 $6 $7 $8 $9),,$(strip $1 $2 $3 $4 $5 $6 $7 $8 $9)),\
517      $(shell $(MKDIR) -p $1 $2 $3 $4 $5 $6 $7 $8 $9)))
518
519################################################################################
520# Assign a variable only if it is empty
521# Param 1 - Variable to assign
522# Param 2 - Value to assign
523SetIfEmpty = \
524    $(if $($(strip $1)),,$(eval $(strip $1) := $2))
525
526################################################################################
527
528ifeq ($(OPENJDK_TARGET_OS),solaris)
529  # On Solaris, if the target is a symlink and exists, cp won't overwrite.
530  # Cp has to operate in recursive mode to allow for -P flag, to preserve soft links. If the
531  # name of the target file differs from the source file, rename after copy.
532  # If the source and target parent directories are the same, recursive copy doesn't work
533  # so we fall back on regular copy, which isn't preserving symlinks.
534  define install-file
535	$(MKDIR) -p '$(@D)'
536	$(RM) '$@'
537	if [ "$(@D)" != "$(<D)" ]; then \
538	  $(CP) -f -r -P '$<' '$(@D)'; \
539	  if [ "$(@F)" != "$(<F)" ]; then \
540	    $(MV) '$(@D)/$(<F)' '$@'; \
541	  fi; \
542	else \
543	  if [ -L '$<' ]; then \
544	    $(ECHO) "Source file is a symlink and target is in the same directory: $< $@" ; \
545	    exit 1; \
546	  fi; \
547	  $(CP) -f '$<' '$@'; \
548	fi
549  endef
550else ifeq ($(OPENJDK_TARGET_OS),macosx)
551  # On mac, extended attributes sometimes creep into the source files, which may later
552  # cause the creation of ._* files which confuses testing. Clear these with xattr if
553  # set. Some files get their write permissions removed after being copied to the
554  # output dir. When these are copied again to images, xattr would fail. By only clearing
555  # attributes when they are present, failing on this is avoided.
556  define install-file
557	$(MKDIR) -p '$(@D)'
558	$(CP) -fRP '$<' '$@'
559	if [ -n "`$(XATTR) -l '$@'`" ]; then $(XATTR) -c '$@'; fi
560  endef
561else
562  # Running mkdir and cp in the same shell speeds up copy intensive tasks in Cygwin
563  # significantly.
564  define install-file
565	$(MKDIR) -p '$(@D)' && $(CP) -fP '$<' '$@'
566  endef
567endif
568
569################################################################################
570# Filter out duplicate sub strings while preserving order. Keeps the first occurance.
571uniq = \
572    $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
573
574# Returns all whitespace-separated words in $2 where at least one of the
575# whitespace-separated words in $1 is a substring.
576containing = \
577    $(strip \
578        $(foreach v,$(strip $2),\
579          $(call uniq,$(foreach p,$(strip $1),$(if $(findstring $p,$v),$v)))))
580
581# Returns all whitespace-separated words in $2 where none of the
582# whitespace-separated words in $1 is a substring.
583not-containing = \
584    $(strip $(filter-out $(call containing,$1,$2),$2))
585
586# Return a list of all string elements that are duplicated in $1.
587dups = \
588    $(strip $(foreach v, $(sort $1), $(if $(filter-out 1, \
589        $(words $(filter $v, $1))), $v)))
590
591# String equals
592equals = \
593    $(and $(findstring $(strip $1),$(strip $2)),\
594        $(findstring $(strip $2),$(strip $1)))
595
596# Remove a whole list of prefixes
597# $1 - List of prefixes
598# $2 - List of elements to process
599remove-prefixes = \
600    $(strip $(if $1,$(patsubst $(firstword $1)%,%,\
601      $(call remove-prefixes,$(filter-out $(firstword $1),$1),$2)),$2))
602
603################################################################################
604
605ifneq ($(DISABLE_CACHE_FIND), true)
606  # In Cygwin, finds are very costly, both because of expensive forks and because
607  # of bad file system caching. Find is used extensively in $(shell) commands to
608  # find source files. This makes rerunning make with no or few changes rather
609  # expensive. To speed this up, these two macros are used to cache the results
610  # of simple find commands for reuse.
611  #
612  # Runs a find and stores both the directories where it was run and the results.
613  # This macro can be called multiple times to add to the cache. Only finds files
614  # with no filters.
615  #
616  # Needs to be called with $(eval )
617  #
618  # Even if the performance benifit is negligible on other platforms, keep the
619  # functionality active unless explicitly disabled to exercise it more.
620  #
621  # Initialize FIND_CACHE_DIRS with := to make it a non recursively-expanded variable
622  FIND_CACHE_DIRS :=
623  # Param 1 - Dirs to find in
624  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
625  define FillCacheFind
626    # Filter out already cached dirs. The - is needed when FIND_CACHE_DIRS is empty
627    # since filter out will then return empty.
628    FIND_CACHE_NEW_DIRS := $$(filter-out $$(addsuffix /%,\
629        - $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS), $1)
630    ifneq ($$(FIND_CACHE_NEW_DIRS), )
631      # Remove any trailing slash from dirs in the cache dir list
632      FIND_CACHE_DIRS += $$(patsubst %/,%, $$(FIND_CACHE_NEW_DIRS))
633      FIND_CACHE := $$(sort $$(FIND_CACHE) $$(shell $(FIND) $$(FIND_CACHE_NEW_DIRS) \( -type f -o -type l \) $2))
634    endif
635  endef
636
637  # Mimics find by looking in the cache if all of the directories have been cached.
638  # Otherwise reverts to shell find. This is safe to call on all platforms, even if
639  # cache is deactivated.
640  #
641  # $1 can be either a directory or a file. If it's a directory, make
642  # sure we have exactly one trailing slash before the wildcard.
643  # The extra - is needed when FIND_CACHE_DIRS is empty but should be harmless.
644  #
645  # Param 1 - Dirs to find in
646  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
647  define CacheFind
648    $(if $(filter-out $(addsuffix /%,- $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS),$1), \
649    $(shell $(FIND) $1 \( -type f -o -type l \) $2), \
650    $(filter $(addsuffix /%,$(patsubst %/,%,$1)) $1,$(FIND_CACHE)))
651  endef
652
653else
654  # If CacheFind is disabled, just run the find command.
655  # Param 1 - Dirs to find in
656  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
657  define CacheFind
658    $(shell $(FIND) $1 \( -type f -o -type l \) $2)
659  endef
660endif
661
662################################################################################
663
664define AddFileToCopy
665  # Helper macro for SetupCopyFiles
666  # 1 : Source file
667  # 2 : Dest file
668  # 3 : Variable to add targets to
669  # 4 : Macro to call for copy operation
670  $2: $1
671	$(ECHO) $(LOG_INFO) Copying $$(patsubst $(OUTPUT_ROOT)/%,%,$$@)
672	$$($$(strip $4))
673
674  $3 += $2
675endef
676
677# Returns the value of the first argument
678identity = \
679    $(strip $1)
680
681# Setup make rules for copying files, with an option to do more complex
682# processing instead of copying.
683#
684# Parameter 1 is the name of the rule. This name is used as variable prefix,
685# and the targets generated are listed in a variable by that name.
686#
687# Remaining parameters are named arguments. These include:
688#   SRC     : Source root dir (defaults to dir of first file)
689#   DEST    : Dest root dir
690#   FILES   : List of files to copy with absolute paths, or path relative to SRC.
691#             Must be in SRC.
692#   FLATTEN : Set to flatten the directory structure in the DEST dir.
693#   MACRO   : Optionally override the default macro used for making the copy.
694#             Default is 'install-file'
695#   NAME_MACRO : Optionally supply a macro that rewrites the target file name
696#                based on the source file name
697SetupCopyFiles = $(NamedParamsMacroTemplate)
698define SetupCopyFilesBody
699
700  ifeq ($$($1_MACRO), )
701    $1_MACRO := install-file
702  endif
703
704  # Default SRC to the dir of the first file.
705  ifeq ($$($1_SRC), )
706    $1_SRC := $$(dir $$(firstword $$($1_FILES)))
707  endif
708
709  ifeq ($$($1_NAME_MACRO), )
710    $1_NAME_MACRO := identity
711  endif
712
713  # Remove any trailing slash from SRC
714  $1_SRC := $$(patsubst %/,%,$$($1_SRC))
715
716  $$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \
717      $$(eval $$(call AddFileToCopy, $$($1_SRC)/$$f, \
718      $$($1_DEST)/$$(call $$(strip $$($1_NAME_MACRO)),$$(if $$($1_FLATTEN),$$(notdir $$f),$$f)), \
719      $1, $$($1_MACRO))))
720
721endef
722
723################################################################################
724# ShellQuote
725#
726# Quotes a string with single quotes and replaces single quotes with '\'' so
727# that the contents survives being given to the shell.
728
729ShellQuote = \
730    $(SQUOTE)$(subst $(SQUOTE),$(SQUOTE)\$(SQUOTE)$(SQUOTE),$(strip $1))$(SQUOTE)
731
732################################################################################
733# Write to and read from file
734
735# Param 1 - File to read
736ReadFile = \
737    $(shell $(CAT) $1)
738
739# Param 1 - Text to write
740# Param 2 - File to write to
741# Use printf to get consistent behavior on all platforms.
742WriteFile = \
743    $(shell $(PRINTF) "%s" $(call ShellQuote, $1) > $2)
744
745################################################################################
746# DependOnVariable
747#
748# This macro takes a variable name and puts the value in a file only if the
749# value has changed since last. The name of the file is returned. This can be
750# used to create rule dependencies on make variable values. The following
751# example would get rebuilt if the value of SOME_VAR was changed:
752#
753# path/to/some-file: $(call DependOnVariable, SOME_VAR)
754#         echo $(SOME_VAR) > $@
755#
756# Note that leading and trailing white space in the value is ignored.
757#
758
759# Defines the sub directory structure to store variable value file in
760DependOnVariableDirName = \
761    $(strip $(subst $(SRC_ROOT)/,,\
762        $(if $(filter /%, $(firstword $(MAKEFILE_LIST))), \
763          $(firstword $(MAKEFILE_LIST)), \
764          $(CURDIR)/$(firstword $(MAKEFILE_LIST)))))
765
766# Defines the name of the file to store variable value in. Generates a name
767# unless parameter 2 is given.
768# Param 1 - Name of variable
769# Param 2 - (optional) name of file to store value in
770DependOnVariableFileName = \
771    $(strip $(if $(strip $2), $2, \
772      $(MAKESUPPORT_OUTPUTDIR)/vardeps/$(DependOnVariableDirName)/$(strip $1).vardeps))
773
774# Does the actual work with parameters stripped.
775# If the file exists AND the contents is the same as the variable, do nothing
776# else print a new file.
777# Always returns the name of the file where the value was printed.
778# Param 1 - Name of variable
779# Param 2 - (optional) name of file to store value in
780DependOnVariableHelper = \
781    $(strip $(if $(and $(wildcard $(call DependOnVariableFileName, $1, $2)),\
782        $(call equals, $(strip $($1)), \
783            $(call ReadFile, $(call DependOnVariableFileName, $1, $2)))),,\
784      $(call MakeDir, $(dir $(call DependOnVariableFileName, $1, $2))) \
785      $(if $(findstring $(LOG_LEVEL), trace), \
786          $(info Variable $1: >$(strip $($1))<) \
787          $(info File: >$(call ReadFile, $(call DependOnVariableFileName, $1, $2))<)) \
788      $(call WriteFile, $($1), $(call DependOnVariableFileName, $1, $2))) \
789    $(call DependOnVariableFileName, $1, $2))
790
791# Main macro
792# Param 1 - Name of variable
793# Param 2 - (optional) name of file to store value in
794DependOnVariable = \
795    $(call DependOnVariableHelper,$(strip $1),$(strip $2))
796
797################################################################################
798# Failure logging support macros. These are supposed to be used by the Setup*
799# compilation macros.
800#
801# LogFailures will run a command and store a copy of output in a specified file.
802# If the command succeeds, the file is deleted, otherwise it is moved to the
803# failure-logs directory.
804# Param 1 - The log file of the failed command
805# Param 2 - A compact but representative name to describe this command
806# Param 3 - Command to run
807LogFailures = \
808  ( ($(BASH) $(SRC_ROOT)/common/bin/logger.sh $1 $3 && $(RM) $1) || \
809  (exitcode=$(DOLLAR)$(DOLLAR)? && $(MV) $1 $(MAKESUPPORT_OUTPUTDIR)/failure-logs/$(strip $2).log && exit $(DOLLAR)$(DOLLAR)exitcode) )
810
811################################################################################
812# Find lib dir for module
813# Param 1 - module name
814ifeq ($(OPENJDK_TARGET_OS_TYPE), unix)
815  FindLibDirForModule = \
816      $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)$(OPENJDK_TARGET_CPU_LIBDIR)
817else
818  FindLibDirForModule = \
819      $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)
820endif
821
822################################################################################
823# Return a string suitable for use after a -classpath option. It will correct and safe to use
824# on all platforms. Arguments are given as space separate classpath entries.
825# param 1 : A space separated list of classpath entries
826# The surrounding strip is needed to keep additional whitespace out
827PathList = \
828  "$(subst $(SPACE),$(PATH_SEP),$(strip $1))"
829
830################################################################################
831
832# Hook to include the corresponding custom file, if present.
833$(eval $(call IncludeCustomExtension, , common/MakeBase.gmk))
834
835endif # _MAKEBASE_GMK
836