1# Rules without side effects.
2
3# Vanilla Jam compatibility
4if ! $(INVOCATION_SUBDIR_SET) {
5	rule FIsPrefix
6	{
7		# FIsPrefix <a> : <b> ;
8		# Returns true, if list <a> is a prefix (a proper one or equal) of
9		# list <b>, an empty list otherwise.
10		local a = $(1) ;
11		local b = $(2) ;
12		while $(a) && $(a[1]) = $(b[1]) {
13			a = $(a[2-]) ;
14			b = $(b[2-]) ;
15		}
16
17		if $(a) {
18			return ;
19		} else {
20			return true ;
21		}
22	}
23
24	rule LocalClean { Clean $(1) : $(2) ; }
25
26	rule LocalDepends { Depends $(1) : $(2) ; }
27
28} # vanilla Jam compatibility
29
30rule FFilter
31{
32	# FFilter <list> : <excludes> ;
33	# Removes all occurrences of <excludes> in <list>.
34
35	local list = $(1) ;
36	local excludes = $(2) ;
37	local newList ;
38	local item ;
39	for item in $(list) {
40		local skip ;
41		local exclude ;
42		for exclude in $(excludes) {
43			if $(item) = $(exclude) {
44				skip = true ;
45			}
46		}
47		if ! $(skip) {
48			newList += $(item) ;
49		}
50	}
51	return $(newList) ;
52}
53
54rule FGetGrist
55{
56	# FGetGrist <target> ;
57	#
58	# Returns the grist of a target, not including leading "<" and trailing ">".
59
60	local grist = $(1[1]:G) ;
61	if ! $(grist) {
62		return ;
63	}
64
65	return [ Match <(.*)> : $(grist) ] ;
66}
67
68rule FSplitString string : delimiterChar
69{
70	local result ;
71
72	while $(string) {
73		local split = [ Match $(delimiterChar)*([^$(delimiterChar)]+)(.*)
74			: $(string) ] ;
75		result += $(split[1]) ;
76		string = $(split[2-]) ;
77	}
78
79	return $(result) ;
80}
81
82rule FSplitPath
83{
84	# SplitPath <path> ;
85	# Decomposes a path into its components.
86	local path = $(1:G=) ;
87	local components ;
88	# $(path:D) for "/" is "/". Therefore the second condition.
89	while $(path:D) && $(path:D) != $(path)
90	{
91		# Note: $(path:B) returns "." for "..", but $(path:D=) is fine.
92		components = $(path:D=) $(components) ;
93		path = $(path:D) ;
94	}
95	components = $(path) $(components) ;
96	return $(components) ;
97}
98
99
100rule FConditionsHold conditions : predicate
101{
102	# FConditionsHold <conditions> : <predicate> ;
103	# Checks whether the conditions <conditions> are satisfied by the predicate
104	# rule <predicate> and returns a respective result (if so: "1", if not:
105	# empty list). The conditions are satisfied when <conditions> is not empty
106	# and
107	# * none of the negative conditions it contains hold and
108	# * if <conditions> contains any positive conditions, at least one of those
109	#   holds.
110	# A positive condition is an element not starting with a "!". It holds when
111	# the predicate rule <predicate> returns true for the element.
112	# A negative condition is an element that starts with a "!". It holds when
113	# the predicate rule <predicate> returns true for the string resulting from
114	# removing the leading "!".
115	#
116	# <conditions> - The list of conditions.
117	# <predicate> - The predicate rule invoked to test the elements.
118	#
119	# Examples:
120	# For a predicate that holds for the elements of the set { a b c } the
121	# following conditions hold:
122	# { a }, { a d }, { !d }, { !d !e }, { a !d }, { b !e !f }
123	# The following conditions don't hold:
124	# { }, { d }, { d e }, { !a }, { !a b }, { !d e } { a b !c !d }
125
126	local hasPositive ;
127	local hasNegative ;
128	local positiveMatch ;
129	local condition ;
130	for condition in $(conditions) {
131		switch $(condition) {
132			case !* :
133			{
134				hasNegative = 1 ;
135				condition = [ Match "!(.*)" : $(condition) ] ;
136				if [ $(predicate) $(condition) ] {
137					return ;
138				}
139			}
140			case * :
141			{
142				hasPositive = 1 ;
143				if [ $(predicate) $(condition) ] {
144					positiveMatch = 1 ;
145				}
146			}
147		}
148	}
149
150	if $(hasPositive) {
151		return $(positiveMatch) ;
152	}
153	return $(hasNegative) ;
154}
155
156
157rule SetPlatformCompatibilityFlagVariables
158{
159	# SetPlatformCompatibilityFlagVariables <platform var> : <var prefix>
160	#	: <platform kind> [ : other platforms ] ;
161
162	local platformVar = $(1) ;
163	local platform = $($(platformVar)) ;
164	local varPrefix = $(2) ;
165	local platformKind = $(3) ;
166	local otherPlatforms = $(4) ;
167
168	if ! $(platform) {
169		ECHO "Variable $(platformVar) not set. Please run ./configure or" ;
170		EXIT "specify it manually." ;
171	}
172
173	# special case: Haiku libbe.so built for testing under Haiku
174	if $(platform) = libbe_test {
175		platform = $(HOST_PLATFORM) ;
176	}
177
178	# unset variables first
179	$(varPrefix)_PLATFORM_HAIKU_COMPATIBLE = ;
180
181	switch $(platform)
182	{
183		case haiku_host :
184		{
185			$(varPrefix)_PLATFORM_HAIKU_COMPATIBLE = true ;
186		}
187
188		case haiku :
189		{
190			$(varPrefix)_PLATFORM_HAIKU_COMPATIBLE = true ;
191		}
192
193		case host :
194			# not compatible to anything
195
196		case * :
197		{
198			if ! ( $(platform) in $(otherPlatforms) ) {
199				Exit Unsupported $(platformKind) platform: $(platform) ;
200			}
201		}
202	}
203
204	# set the machine friendly flags
205	$(varPrefix)_PLATFORM_(haiku)_COMPATIBLE
206		?= $($(varPrefix)_PLATFORM_HAIKU_COMPATIBLE) ;
207	$(varPrefix)_PLATFORM_(haiku_host)_COMPATIBLE
208		?= $($(varPrefix)_PLATFORM_HAIKU_COMPATIBLE) ;
209}
210
211rule SetIncludePropertiesVariables prefix : suffix
212{
213	# SetIncludePropertiesVariables <prefix> : <suffix> ;
214	#
215	suffix = $(suffix:E=) ;
216	if $($(prefix)_CC_IS_LEGACY_GCC$(suffix)) = 1 {
217		$(prefix)_INCLUDES_SEPARATOR$(suffix) = -I- ;
218		$(prefix)_LOCAL_INCLUDES_OPTION$(suffix) = -I ;
219		$(prefix)_SYSTEM_INCLUDES_OPTION$(suffix) = -I ;
220	} else {
221		$(prefix)_INCLUDES_SEPARATOR$(suffix) = ;
222		$(prefix)_LOCAL_INCLUDES_OPTION$(suffix) = "-iquote " ;
223		$(prefix)_SYSTEM_INCLUDES_OPTION$(suffix) = "-I " ;
224	}
225}
226
227
228#pragma mark -
229
230rule SetPlatformForTarget
231{
232	# SetPlatformForTarget <target> : <platform> ;
233
234	PLATFORM on $(1) = $(2) ;
235}
236
237rule SetSubDirPlatform
238{
239	# SetSubDirPlatform <platform> ;
240
241	PLATFORM = $(1) ;
242}
243
244rule SetSupportedPlatformsForTarget
245{
246	# SetSupportedPlatformsForTarget <target> : <platforms> ;
247
248	SUPPORTED_PLATFORMS on $(1) = $(2) ;
249}
250
251rule SetSubDirSupportedPlatforms
252{
253	# SetSubDirSupportedPlatforms <platforms> ;
254
255	SUPPORTED_PLATFORMS = $(1) ;
256}
257
258rule AddSubDirSupportedPlatforms
259{
260	# AddSubDirSupportedPlatforms <platforms> ;
261
262	SUPPORTED_PLATFORMS += $(1) ;
263}
264
265rule IsPlatformSupportedForTarget
266{
267	# IsPlatformSupportedForTarget <target> [ : <platform> ]
268	#
269
270	on $(1) {
271		if $(PLATFORM) in $(SUPPORTED_PLATFORMS) {
272			return true ;
273		} else {
274			return ;
275		}
276	}
277}
278
279rule InheritPlatform
280{
281	# InheritPlatform <children> : <parent> ;
282	# PLATFORM and SUPPORTED_PLATFORMS are set on <children> to their value
283	# on <parent>.
284	#
285	local children = $(1) ;
286	local parent = $(2) ;
287
288	on $(parent) {
289		PLATFORM on $(children) = $(PLATFORM) ;
290		SUPPORTED_PLATFORMS on $(children) = $(SUPPORTED_PLATFORMS) ;
291	}
292}
293
294rule SubDirAsFlags
295{
296	SUBDIRASFLAGS += $(<) ;
297}
298