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