1# -*- makefile -*-
2# vim:set ts=8 sw=8 sts=8 noet:
3#
4# This Source Code Form is subject to the terms of the Mozilla Public
5# License, v. 2.0. If a copy of the MPL was not distributed with this file,
6# You can obtain one at http://mozilla.org/MPL/2.0/.
7
8## Identify function argument types
9istype =$(if $(value ${1}),list,scalar)
10isval  =$(if $(filter-out list,$(call istype,${1})),true)
11isvar  =$(if $(filter-out scalar,$(call istype,${1})),true)
12
13# Access up to 9 arguments passed, option needed to emulate $*
14# Inline for function expansion, do not use $(call )
15argv  =$(strip
16argv +=$(if $(1), $(1))$(if $(2), $(2))$(if $(3), $(3))$(if $(4), $(4))
17argv +=$(if $(5), $(5))$(if $(6), $(6))$(if $(7), $(7))$(if $(8), $(8))
18argv +=$(if $(9), $(9))
19argv +=$(if $(10), $(error makeutils.mk::argv can only handle 9 arguments))
20argv +=)
21
22###########################################################################
23## Access function args as a simple list, inline within user functions.
24## Usage: $(info ** $(call banner,$(getargv)))
25##    $(call banner,scalar)
26##    $(call banner,list0 list1 list2)
27##    $(call banner,ref) ; ref=foo bar tans
28## getarglist() would be a more accurate name but is longer to type
29getargv = $(if $(call isvar,$(1)),$($(1)),$(argv))
30
31###########################################################################
32# Strip [n] leading options from an argument list.  This will allow passing
33# extra args to user functions that will not propogate to sub-$(call )'s
34# Usage: $(call subargv,2)
35subargv =$(wordlist $(1),$(words $(getargv)),$(getargv))
36
37###########################################################################
38# Intent: Display a distinct banner heading in the output stream
39# Usage: $(call banner,BUILDING: foo bar tans)
40# Debug:
41#   target-preqs = \
42#     $(call banner,target-preqs-BEGIN) \
43#     foo bar tans \
44#     $(call banner,target-preqs-END) \
45#     $(NULL)
46#   target: $(target-preqs)
47
48banner = \
49$(info ) \
50$(info ***************************************************************************) \
51$(info ** $(getargv)) \
52$(info ***************************************************************************) \
53$(NULL)
54
55#####################################################################
56# Intent: Determine if a string or pattern is contained in a list
57# Usage: strcmp  - $(call if_XinY,clean,$(MAKECMDGOALS))
58#      : pattern - $(call if_XinY,clean%,$(MAKECMDGOALS))
59is_XinY =$(filter $(1),$(call subargv,3,$(getargv)))
60
61#####################################################################
62# Provide an alternate var to support testing
63ifdef MAKEUTILS_UNIT_TEST
64  mcg_goals=TEST_MAKECMDGOALS
65else
66  mcg_goals=MAKECMDGOALS
67endif
68
69# Intent: Conditionals for detecting common/tier target use
70isTargetStem       = $(sort \
71  $(foreach var,$(getargv),\
72    $(foreach pat,$(var)% %$(var),\
73      $(call is_XinY,$(pat),${$(mcg_goals)})\
74  )))
75isTargetStemClean  = $(call isTargetStem,clean)
76isTargetStemExport = $(call isTargetStem,export)
77isTargetStemLibs   = $(call isTargetStem,libs)
78isTargetStemTools  = $(call isTargetStem,tools)
79
80##################################################
81# Intent: Validation functions / unit test helpers
82
83errorifneq =$(if $(subst $(strip $(1)),$(NULL),$(strip $(2))),$(error expected [$(1)] but found [$(2)]))
84
85# Intent: verify function declaration exists
86requiredfunction =$(foreach func,$(1) $(2) $(3) $(4) $(5) $(6) $(7) $(8) $(9),$(if $(value $(func)),$(NULL),$(error required function [$(func)] is unavailable)))
87
88
89
90## http://www.gnu.org/software/make/manual/make.html#Call-Function
91## Usage: o = $(call map,origin,o map $(MAKE))
92map = $(foreach val,$(2),$(call $(1),$(val)))
93
94
95## Disable checking for clean targets
96ifeq (,$(filter %clean clean%,$(MAKECMDGOALS))) #{
97
98# Usage: $(call checkIfEmpty,[error|warning] foo NULL bar)
99checkIfEmpty =$(foreach var,$(wordlist 2,100,$(argv)),$(if $(strip $($(var))),$(NOP),$(call $(1),Variable $(var) does not contain a value)))
100
101# Usage: $(call errorIfEmpty,foo NULL bar)
102errorIfEmpty =$(call checkIfEmpty,error $(argv))
103warnIfEmpty  =$(call checkIfEmpty,warning $(argv))
104
105endif #}
106
107###########################################################################
108## Common makefile library loader
109###########################################################################
110ifdef MOZILLA_DIR
111topORerr = $(MOZILLA_DIR)
112else
113topORerr = $(if $(topsrcdir),$(topsrcdir),$(error topsrcdir is not defined))
114endif
115
116ifdef USE_AUTOTARGETS_MK # mkdir_deps
117  include $(topORerr)/config/makefiles/autotargets.mk
118endif
119
120## copy(src, dst): recursive copy
121copy_dir = (cd $(1)/. && $(TAR) $(TAR_CREATE_FLAGS) - .) | (cd $(2)/. && tar -xf -)
122