MakeBase.gmk revision 1552:c8b4333cb501
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) ; \
373        $(LS) -d $(HG_SEARCH:%/REPO=%/$(HG_DIRECTORY)) \
374            $(HG_SEARCH:%/REPO=%/$(HGTIP_FILENAME)) \
375        2> /dev/null)))))
376
377# Emit the repo:tip pairs to $@
378define GetSourceTips
379	$(CD) $(SRC_ROOT) ; \
380	for i in $(REPO_LIST) IGNORE ; do \
381	  if [ "$${i}" = "IGNORE" ] ; then \
382	    continue; \
383	  elif [ -d $${i}/$(HG_DIRECTORY) -a "$(HG_VERSION)" != "" ] ; then \
384	    $(PRINTF) " %s:%s" \
385	        "$${i}" `$(HG) tip --repository $${i} --template '{node|short}\n'` ; \
386	  elif [ -f $${i}/$(HGTIP_FILENAME) ] ; then \
387	    $(PRINTF) " %s:%s" \
388	        "$${i}" `$(CAT) $${i}/$(HGTIP_FILENAME)` ; \
389	  fi; \
390	done >> $@
391	$(PRINTF) "\n" >> $@
392endef
393
394# Create the HGTIP_FILENAME file. Called from jdk/make/closed/bundles.gmk
395define CreateHgTip
396	$(HG) tip --repository $1 --template '{node|short}\n' > $1/$(HGTIP_FILENAME); \
397	$(ECHO) $1/$(HGTIP_FILENAME)
398endef
399
400define SetupLogging
401  ifeq ($$(LOG_LEVEL), trace)
402    # Shell redefinition trick inspired by http://www.cmcrossroads.com/ask-mr-make/6535-tracing-rule-execution-in-gnu-make
403    # For each target executed, will print
404    # Building <TARGET> (from <FIRST PREREQUISITE>) (<ALL NEWER PREREQUISITES> newer)
405    # but with a limit of 20 on <ALL NEWER PREREQUISITES>, to avoid cluttering logs too much
406    # (and causing a crash on Cygwin).
407    # Default shell seems to always be /bin/sh. Must override with bash to get this to work on Solaris.
408    # Only use time if it's GNU time which supports format and output file.
409    WRAPPER_SHELL := $$(BASH) $$(SRC_ROOT)/common/bin/shell-tracer.sh $$(if $$(findstring yes,$$(IS_GNU_TIME)),$$(TIME),-) $$(OUTPUT_ROOT)/build-trace-time.log $$(SHELL)
410    SHELL := $$(warning $$(if $$@,Building $$@,Running shell command) $$(if $$<, (from $$<))$$(if $$?, ($$(wordlist 1, 20, $$?) $$(if $$(wordlist 21, 22, $$?), ... [in total $$(words $$?) files]) newer)))$$(WRAPPER_SHELL)
411  endif
412  # Never remove warning messages; this is just for completeness
413  LOG_WARN :=
414  ifneq ($$(findstring $$(LOG_LEVEL), info debug trace),)
415    LOG_INFO :=
416  else
417    LOG_INFO := > /dev/null
418  endif
419  ifneq ($$(findstring $$(LOG_LEVEL), debug trace),)
420    LOG_DEBUG :=
421  else
422    LOG_DEBUG := > /dev/null
423  endif
424  ifneq ($$(findstring $$(LOG_LEVEL), trace),)
425    LOG_TRACE :=
426  else
427    LOG_TRACE := > /dev/null
428  endif
429endef
430
431# Make sure logging is setup for everyone that includes MakeBase.gmk.
432$(eval $(call SetupLogging))
433
434################################################################################
435# Creates a sequence of increasing numbers (inclusive).
436# Param 1 - starting number
437# Param 2 - ending number
438sequence = \
439    $(wordlist $1, $2, $(strip \
440        $(eval SEQUENCE_COUNT :=) \
441        $(call _sequence-do,$(strip $2))))
442
443_sequence-do = \
444    $(if $(word $1, $(SEQUENCE_COUNT)),, \
445      $(eval SEQUENCE_COUNT += .) \
446      $(words $(SEQUENCE_COUNT)) \
447      $(call _sequence-do,$1))
448
449################################################################################
450
451MAX_PARAMS := 30
452PARAM_SEQUENCE := $(call sequence, 2, $(MAX_PARAMS))
453
454# Template for creating a macro taking named parameters. To use it, assign the
455# template to a variable with the name you want for your macro, using '='
456# assignment. Then define a macro body with the suffix "Body". The Body macro
457# should take 1 parameter which should be a unique string for that invocation
458# of the macro.
459# Ex:
460# SetupFoo = $(NamedParamsMacroTemplate)
461# define SetupFooBody
462#   # do something
463#   # access parameters as $$($1_BAR)
464# endef
465# Call it like this
466# $(eval $(call SetupFoo, BUILD_SOMETHING, \
467#     BAR := some parameter value, \
468# ))
469define NamedParamsMacroTemplate
470  $(if $($(MAX_PARAMS)),$(error Internal makefile error: \
471      Too many named arguments to macro, please update MAX_PARAMS in MakeBase.gmk))
472  # Iterate over 2 3 4... and evaluate the named parameters with $1_ as prefix
473  $(foreach i,$(PARAM_SEQUENCE), $(if $(strip $($i)),\
474    $(strip $1)_$(strip $($i)))$(NEWLINE))
475  # Debug print all named parameter names and values
476  $(if $(findstring $(LOG_LEVEL),debug trace), \
477    $(info $0 $(strip $1) $(foreach i,$(PARAM_SEQUENCE), \
478      $(if $(strip $($i)),$(NEWLINE) $(strip [$i] $(if $(filter $(LOG_LEVEL), trace), \
479        $($i), $(wordlist 1, 20, $($(i))) $(if $(word 21, $($(i))), ...)))))))
480
481  $(if $(DEBUG_$(strip $1)),
482    $(info -------- <<< Begin expansion of $(strip $1)) \
483    $(info $(call $(0)Body,$(strip $1))) \
484    $(info -------- >>> End expansion of $(strip $1)) \
485  )
486
487  $(call $(0)Body,$(strip $1))
488endef
489
490################################################################################
491# Make directory without forking mkdir if not needed
492MakeDir = \
493    $(strip $(if $(subst $(wildcard $1 $2 $3 $4 $5 $6 $7 $8 $9),,$(strip $1 $2 $3 $4 $5 $6 $7 $8 $9)),\
494      $(shell $(MKDIR) -p $1 $2 $3 $4 $5 $6 $7 $8 $9)))
495
496################################################################################
497# Assign a variable only if it is empty
498# Param 1 - Variable to assign
499# Param 2 - Value to assign
500SetIfEmpty = \
501    $(if $($(strip $1)),,$(eval $(strip $1) := $2))
502
503################################################################################
504
505ifeq ($(OPENJDK_TARGET_OS),solaris)
506  # On Solaris, if the target is a symlink and exists, cp won't overwrite.
507  # Cp has to operate in recursive mode to allow for -P flag, to preserve soft links. If the
508  # name of the target file differs from the source file, rename after copy.
509  # If the source and target parent directories are the same, recursive copy doesn't work
510  # so we fall back on regular copy, which isn't preserving symlinks.
511  define install-file
512	$(MKDIR) -p '$(@D)'
513	$(RM) '$@'
514	if [ "$(@D)" != "$(<D)" ]; then \
515	  $(CP) -f -r -P '$<' '$(@D)'; \
516	  if [ "$(@F)" != "$(<F)" ]; then \
517	    $(MV) '$(@D)/$(<F)' '$@'; \
518	  fi; \
519	else \
520	  if [ -L '$<' ]; then \
521	    $(ECHO) "Source file is a symlink and target is in the same directory: $< $@" ; \
522	    exit 1; \
523	  fi; \
524	  $(CP) -f '$<' '$@'; \
525	fi
526  endef
527else ifeq ($(OPENJDK_TARGET_OS),macosx)
528  # On mac, extended attributes sometimes creep into the source files, which may later
529  # cause the creation of ._* files which confuses testing. Clear these with xattr if
530  # set. Some files get their write permissions removed after being copied to the
531  # output dir. When these are copied again to images, xattr would fail. By only clearing
532  # attributes when they are present, failing on this is avoided.
533  define install-file
534	$(MKDIR) -p '$(@D)'
535	$(CP) -fRP '$<' '$@'
536	if [ -n "`$(XATTR) -l '$@'`" ]; then $(XATTR) -c '$@'; fi
537  endef
538else
539  # Running mkdir and cp in the same shell speeds up copy intensive tasks in Cygwin
540  # significantly.
541  define install-file
542	$(MKDIR) -p '$(@D)' && $(CP) -fP '$<' '$@'
543  endef
544endif
545
546################################################################################
547# Convenience functions for working around make's limitations with $(filter ).
548containing = \
549    $(strip $(foreach v,$(strip $2),$(if $(findstring $(strip $1),$v),$v)))
550not-containing = \
551    $(strip $(foreach v,$(strip $2),$(if $(findstring $(strip $1),$v),,$v)))
552
553# Filter out duplicate sub strings while preserving order. Keeps the first occurance.
554uniq = \
555    $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
556
557# Return a list of all string elements that are duplicated in $1.
558dups = \
559    $(strip $(foreach v, $(sort $1), $(if $(filter-out 1, \
560        $(words $(filter $v, $1))), $v)))
561
562# String equals
563equals = \
564    $(and $(findstring $(strip $1),$(strip $2)),\
565        $(findstring $(strip $2),$(strip $1)))
566
567# Remove a whole list of prefixes
568# $1 - List of prefixes
569# $2 - List of elements to process
570remove-prefixes = \
571    $(strip $(if $1,$(patsubst $(firstword $1)%,%,\
572      $(call remove-prefixes,$(filter-out $(firstword $1),$1),$2)),$2))
573
574################################################################################
575
576ifneq ($(DISABLE_CACHE_FIND), true)
577  # In Cygwin, finds are very costly, both because of expensive forks and because
578  # of bad file system caching. Find is used extensively in $(shell) commands to
579  # find source files. This makes rerunning make with no or few changes rather
580  # expensive. To speed this up, these two macros are used to cache the results
581  # of simple find commands for reuse.
582  #
583  # Runs a find and stores both the directories where it was run and the results.
584  # This macro can be called multiple times to add to the cache. Only finds files
585  # with no filters.
586  #
587  # Needs to be called with $(eval )
588  #
589  # Even if the performance benifit is negligible on other platforms, keep the
590  # functionality active unless explicitly disabled to exercise it more.
591  #
592  # Initialize FIND_CACHE_DIRS with := to make it a non recursively-expanded variable
593  FIND_CACHE_DIRS :=
594  # Param 1 - Dirs to find in
595  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
596  define FillCacheFind
597    # Filter out already cached dirs. The - is needed when FIND_CACHE_DIRS is empty
598    # since filter out will then return empty.
599    FIND_CACHE_NEW_DIRS := $$(filter-out $$(addsuffix /%,\
600        - $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS), $1)
601    ifneq ($$(FIND_CACHE_NEW_DIRS), )
602      # Remove any trailing slash from dirs in the cache dir list
603      FIND_CACHE_DIRS += $$(patsubst %/,%, $$(FIND_CACHE_NEW_DIRS))
604      FIND_CACHE := $$(sort $$(FIND_CACHE) $$(shell $(FIND) $$(FIND_CACHE_NEW_DIRS) \( -type f -o -type l \) $2))
605    endif
606  endef
607
608  # Mimics find by looking in the cache if all of the directories have been cached.
609  # Otherwise reverts to shell find. This is safe to call on all platforms, even if
610  # cache is deactivated.
611  #
612  # $1 can be either a directory or a file. If it's a directory, make
613  # sure we have exactly one trailing slash before the wildcard.
614  # The extra - is needed when FIND_CACHE_DIRS is empty but should be harmless.
615  #
616  # Param 1 - Dirs to find in
617  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
618  define CacheFind
619    $(if $(filter-out $(addsuffix /%,- $(FIND_CACHE_DIRS)) $(FIND_CACHE_DIRS),$1), \
620    $(shell $(FIND) $1 \( -type f -o -type l \) $2), \
621    $(filter $(addsuffix /%,$(patsubst %/,%,$1)) $1,$(FIND_CACHE)))
622  endef
623
624else
625  # If CacheFind is disabled, just run the find command.
626  # Param 1 - Dirs to find in
627  # Param 2 - (optional) specialization. Normally "-a \( ... \)" expression.
628  define CacheFind
629    $(shell $(FIND) $1 \( -type f -o -type l \) $2)
630  endef
631endif
632
633################################################################################
634
635define AddFileToCopy
636  # Helper macro for SetupCopyFiles
637  # 1 : Source file
638  # 2 : Dest file
639  # 3 : Variable to add targets to
640  # 4 : Macro to call for copy operation
641  $2: $1
642	$(ECHO) $(LOG_INFO) Copying $$(patsubst $(OUTPUT_ROOT)/%,%,$$@)
643	$$($$(strip $4))
644
645  $3 += $2
646endef
647
648# Setup make rules for copying files, with an option to do more complex
649# processing instead of copying.
650#
651# Parameter 1 is the name of the rule. This name is used as variable prefix,
652# and the targets generated are listed in a variable by that name.
653#
654# Remaining parameters are named arguments. These include:
655#   SRC     : Source root dir (defaults to dir of first file)
656#   DEST    : Dest root dir
657#   FILES   : List of files to copy with absolute paths, or path relative to SRC.
658#             Must be in SRC.
659#   FLATTEN : Set to flatten the directory structure in the DEST dir.
660#   MACRO   : Optionally override the default macro used for making the copy.
661#             Default is 'install-file'
662SetupCopyFiles = $(NamedParamsMacroTemplate)
663define SetupCopyFilesBody
664
665  ifeq ($$($1_MACRO), )
666    $1_MACRO := install-file
667  endif
668
669  # Default SRC to the dir of the first file.
670  ifeq ($$($1_SRC), )
671    $1_SRC := $$(dir $$(firstword $$($1_FILES)))
672  endif
673
674  # Remove any trailing slash from SRC
675  $1_SRC := $$(patsubst %/,%,$$($1_SRC))
676
677  $$(foreach f, $$(patsubst $$($1_SRC)/%,%,$$($1_FILES)), \
678      $$(eval $$(call AddFileToCopy, $$($1_SRC)/$$f, \
679      $$($1_DEST)/$$(if $$($1_FLATTEN),$$(notdir $$f),$$f), $1, $$($1_MACRO))))
680
681endef
682
683################################################################################
684# ShellQuote
685#
686# Quotes a string with single quotes and replaces single quotes with '\'' so
687# that the contents survives being given to the shell.
688
689ShellQuote = \
690    $(SQUOTE)$(subst $(SQUOTE),$(SQUOTE)\$(SQUOTE)$(SQUOTE),$(strip $1))$(SQUOTE)
691
692################################################################################
693# Write to and read from file
694
695# Param 1 - File to read
696ReadFile = \
697    $(shell $(CAT) $1)
698
699# Param 1 - Text to write
700# Param 2 - File to write to
701# Use printf to get consistent behavior on all platforms.
702WriteFile = \
703    $(shell $(PRINTF) "%s" $(call ShellQuote, $1) > $2)
704
705################################################################################
706# DependOnVariable
707#
708# This macro takes a variable name and puts the value in a file only if the
709# value has changed since last. The name of the file is returned. This can be
710# used to create rule dependencies on make variable values. The following
711# example would get rebuilt if the value of SOME_VAR was changed:
712#
713# path/to/some-file: $(call DependOnVariable, SOME_VAR)
714#         echo $(SOME_VAR) > $@
715#
716# Note that leading and trailing white space in the value is ignored.
717#
718
719# Defines the sub directory structure to store variable value file in
720DependOnVariableDirName = \
721    $(strip $(subst $(SRC_ROOT)/,,\
722        $(if $(filter /%, $(firstword $(MAKEFILE_LIST))), \
723          $(firstword $(MAKEFILE_LIST)), \
724          $(CURDIR)/$(firstword $(MAKEFILE_LIST)))))
725
726# Defines the name of the file to store variable value in. Generates a name
727# unless parameter 2 is given.
728# Param 1 - Name of variable
729# Param 2 - (optional) name of file to store value in
730DependOnVariableFileName = \
731    $(strip $(if $(strip $2), $2, \
732      $(MAKESUPPORT_OUTPUTDIR)/vardeps/$(DependOnVariableDirName)/$(strip $1).vardeps))
733
734# Does the actual work with parameters stripped.
735# If the file exists AND the contents is the same as the variable, do nothing
736# else print a new file.
737# Always returns the name of the file where the value was printed.
738# Param 1 - Name of variable
739# Param 2 - (optional) name of file to store value in
740DependOnVariableHelper = \
741    $(strip $(if $(and $(wildcard $(call DependOnVariableFileName, $1, $2)),\
742        $(call equals, $(strip $($1)), \
743            $(call ReadFile, $(call DependOnVariableFileName, $1, $2)))),,\
744      $(call MakeDir, $(dir $(call DependOnVariableFileName, $1, $2))) \
745      $(if $(findstring $(LOG_LEVEL), trace), \
746          $(info Variable $1: >$(strip $($1))<) \
747          $(info File: >$(call ReadFile, $(call DependOnVariableFileName, $1, $2))<)) \
748      $(call WriteFile, $($1), $(call DependOnVariableFileName, $1, $2))) \
749    $(call DependOnVariableFileName, $1, $2))
750
751# Main macro
752# Param 1 - Name of variable
753# Param 2 - (optional) name of file to store value in
754DependOnVariable = \
755    $(call DependOnVariableHelper,$(strip $1),$(strip $2))
756
757################################################################################
758# Find lib dir for module
759# Param 1 - module name
760ifeq ($(OPENJDK_TARGET_OS_TYPE), unix)
761  FindLibDirForModule = \
762      $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)$(OPENJDK_TARGET_CPU_LIBDIR)
763else
764  FindLibDirForModule = \
765      $(SUPPORT_OUTPUTDIR)/modules_libs/$(strip $1)
766endif
767
768################################################################################
769
770# Hook to include the corresponding custom file, if present.
771$(eval $(call IncludeCustomExtension, , common/MakeBase.gmk))
772
773endif # _MAKEBASE_GMK
774