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