xref: /haiku/build/jam/MainBuildRules (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1
2rule AddSharedObjectGlueCode
3{
4	# AddSharedObjectGlueCode <target> : <isExecutable> ;
5
6	# we link with -nostdlib and add the required libs manually, when building
7	# for Haiku
8	local platform = [ on $(1) return $(PLATFORM) ] ;
9	if $(platform) = haiku {
10		local stdLibs = libroot.so ;
11		local beginGlue ;
12		local endGlue ;
13		if $(2) = true {
14			beginGlue += $(HAIKU_EXECUTABLE_BEGIN_GLUE_CODE) ;
15			endGlue += $(HAIKU_EXECUTABLE_END_GLUE_CODE) ;
16		} else {
17			beginGlue += $(HAIKU_LIBRARY_BEGIN_GLUE_CODE) ;
18			endGlue += $(HAIKU_LIBRARY_END_GLUE_CODE) ;
19
20			# special case for libroot: don't link it against itself
21			if $(1) = libroot.so {
22				stdLibs = ;
23			}
24		}
25
26		LINK_BEGIN_GLUE on $(1) = $(beginGlue) ;
27		LINK_END_GLUE on $(1) = $(endGlue) ;
28
29		NEEDLIBS on $(1) = $(stdLibs) [ on $(1) return $(NEEDLIBS) ] ;
30		Depends $(1) : $(stdLibs) $(beginGlue) $(endGlue) ;
31		LINKFLAGS on $(1) = [ on $(1) return $(LINKFLAGS) ] -nostdlib ;
32	}
33
34	# link against the compatibility libraries needed for the target
35	if $(platform) != host && $(TARGET_HAIKU_COMPATIBILITY_LIBS) {
36		LinkAgainst $(1) : $(TARGET_HAIKU_COMPATIBILITY_LIBS) ;
37	}
38}
39
40rule Executable
41{
42	# Executable <name> : <sources> : <libraries> : <res> ;
43	#
44	if ! [ IsPlatformSupportedForTarget $(1) ] {
45		return ;
46	}
47
48	AddResources $(1) : $(4) ;
49	Main $(1) : $(2) ;
50	LinkAgainst $(1) : $(3) ;
51	LINKFLAGS on $(1) = [ on $(1) return $(LINKFLAGS) ]
52		-Xlinker -soname=_APP_ ;
53
54	# we link with -nostdlib and add the required libs manually, when building
55	# for Haiku
56	AddSharedObjectGlueCode $(1) : true ;
57}
58
59rule Application
60{
61	# Application <name> : <sources> : <libraries> : <res> ;
62	Executable $(1) : $(2) : $(3) : $(4) ;
63}
64
65rule BinCommand
66{
67	# BinCommand <name> : <sources> : <libraries> : <res> ;
68	Executable $(1) : $(2) : $(3) : $(4) ;
69}
70
71rule StdBinCommands
72{
73	# StdBinCommands <sources> : <libs> : <res> ;
74	local libs = $(2) ;
75	local ress = $(3) ;
76	local source ;
77	for source in $(1)
78	{
79		local target = $(source:S=) ;
80
81		BinCommand $(target) : $(source) : $(libs) : $(ress) ;
82	}
83}
84
85rule Preference
86{
87	# Preference <name> : <sources> : <libraries> : <res> ;
88	Executable $(1) : $(2) : $(3) : $(4) ;
89}
90
91rule Server
92{
93	# Server <name> : <sources> : <libraries> : <res> ;
94
95	Executable $(1) : $(2) : $(3) : $(4) ;
96}
97
98rule Addon
99{
100	# Addon <name> : <relpath> : <sources> : <is executable> : <libraries> ;
101	# <name>: Name of the add-on.
102	# <relpath>: Path where the add-on shall live relative to the add-on dir.
103	# <sources>: Source files.
104	# <is executable>: true, if the target shall be executable as well.
105	# <libraries>: Libraries to be linked against.
106# TODO: <relpath> is no longer needed
107
108	if ! [ IsPlatformSupportedForTarget $(1) ] {
109		return ;
110	}
111
112	local isExecutable = $(4) ;
113
114	Main $(1) : $(3) ;
115
116	local linkFlags = -Xlinker -soname=\"$(1)\" ;
117	if $(isExecutable) != true {
118		linkFlags = -nostart $(linkFlags) ;
119	}
120	LINKFLAGS on $(1) = [ on $(1) return $(LINKFLAGS) ] $(linkFlags) ;
121	LinkAgainst $(1) : $(5) ;
122
123	AddSharedObjectGlueCode $(1) : $(isExecutable) ;
124}
125
126rule Translator
127{
128	# Translator <name> : <sources> : <libraries> ;
129	# TODO: Although they currently all are, translators don't need to be
130	# executable. Introduce a flag as for Addon.
131	Addon $(1) : Translators : $(2) : true : $(3) ;
132}
133
134rule ScreenSaver
135{
136	# ScreenSaver <name> : <sources> : <libraries> ;
137	Addon $(1) : screen_savers : $(2) : false : $(3) ;
138}
139
140rule StaticLibrary
141{
142	# StaticLibrary <lib> : <sources> ;
143	# Creates a static library from sources.
144	# <lib>: The static library to be built.
145	# <source>: List of source files.
146	#
147	local lib = $(1) ;
148	local sources = [ FGristFiles $(2) ] ;
149	local objects = $(sources:S=$(SUFOBJ)) ;
150
151	if ! [ IsPlatformSupportedForTarget $(1) ] {
152		return ;
153	}
154
155	InheritPlatform $(objects) : $(lib) ;
156
157	StaticLibraryFromObjects $(lib) : $(objects) ;
158	Objects $(2) ;
159}
160
161rule StaticLibraryFromObjects
162{
163	if ! [ IsPlatformSupportedForTarget $(1) ] {
164		return ;
165	}
166
167	LibraryFromObjects $(1) : $(2) ;
168}
169
170rule MergeObjectFromObjects
171{
172	# MergeObjectFromObjects <name> : <objects> : <other objects> ;
173	# Merges object files to an object file.
174	# <name>: Name of the object file to create. No grist will be added.
175	# <objects>: Object files to be merged. Grist will be added.
176	# <other objects>: Object files or static libraries to be merged. No grist
177	#                  will be added.
178	#
179	local objects = [ FGristFiles $(2) ] ;
180
181	on $(1) {
182		if ! $(PLATFORM) in $(SUPPORTED_PLATFORMS) {
183			return ;
184		}
185
186		if $(PLATFORM) = host {
187			LINK on $(1) = $(HOST_LD) ;
188		} else {
189			LINK on $(1) = $(TARGET_LD) ;
190		}
191	}
192
193	MakeLocateDebug $(1) ;
194	Depends $(1) : $(objects) ;
195	Depends $(1) : $(3) ;
196	LocalDepends obj : $(1) ;
197	MergeObjectFromObjects1 $(1) : $(objects) $(3) ;
198}
199
200actions MergeObjectFromObjects1
201{
202	$(LINK) -r $(2) -o $(1) ;
203}
204
205rule MergeObject
206{
207	# MergeObject <name> : <sources> : <other objects> ;
208	# Compiles source files and merges the object files to an object file.
209	# <name>: Name of the object file to create. No grist will be added.
210	# <sources>: Sources to be compiled. Grist will be added.
211	# <other objects>: Object files or static libraries to be merged. No grist
212	#                  will be added.
213	#
214	local target = $(1) ;
215	local sources = [ FGristFiles $(2) ] ;
216	local otherObjects = $(3) ;
217	local objects = $(sources:S=$(SUFOBJ)) ;
218
219	if ! [ IsPlatformSupportedForTarget $(1) ] {
220		return ;
221	}
222
223	InheritPlatform $(objects) : $(target) ;
224	Objects $(sources) ;
225	MergeObjectFromObjects $(target) : $(objects) : $(otherObjects) ;
226}
227
228rule SharedLibraryFromObjects
229{
230	# SharedLibraryFromObjects <lib> : <objects> : <libraries> ;
231	#
232	local _lib = $(1) ;
233
234	if ! [ IsPlatformSupportedForTarget $(1) ] {
235		return ;
236	}
237
238	MainFromObjects $(_lib) : $(2) ;
239	LINKFLAGS on $(_lib) = [ on $(_lib) return $(LINKFLAGS) ]
240						   -nostart -Xlinker -soname=\"$(_lib)\" ;
241	LinkAgainst $(_lib) : $(3) ;
242
243	AddSharedObjectGlueCode $(_lib) : false ;
244}
245
246rule SharedLibrary
247{
248	# SharedLibrary <lib> : <sources> : <libraries> ;
249	local lib = $(1) ;
250	local sources = [ FGristFiles $(2) ] ;
251	local objects = $(sources:S=$(SUFOBJ)) ;
252	local libs = $(3) ;
253
254	if ! [ IsPlatformSupportedForTarget $(1) ] {
255		return ;
256	}
257
258	InheritPlatform $(objects) : $(lib) ;
259	Objects $(sources) ;
260	SharedLibraryFromObjects $(lib) : $(objects) : $(libs) ;
261}
262
263rule LinkAgainst
264{
265	# LinkAgainst <name> : <libs> [ : <mapLibs> ] ;
266	# Valid elements for <libs> are e.g. "be" or "libtranslation.so" or
267	# "/boot/.../libfoo.so". If the basename starts with "lib" or the thingy
268	# has a dirname or grist, it is added to the NEEDLIBS variable (i.e. the
269	# file will be bound!), otherwise it is prefixed "-l" and added to
270	# LINKLIBS. If you want to specify a target that isn't a library and
271	# also has neither grist nor a dirname, you can prepend "<nogrist>" as
272	# grist; it will be stripped by this rule.
273	# <mapLibs> specifies whether the to translate library names (e.g. "be"
274	# to "libbe.so" in case of target platform "haiku"). Defaults to "true".
275	#
276	local target = $(1) ;
277	local libs = $(2) ;
278	local mapLibs = $(3:E=true) ;
279
280	on $(target) {
281		# map libraries, if desired and target platform is Haiku
282		if $(PLATFORM) != host && $(mapLibs) = true
283				&& $(TARGET_LIBRARY_NAME_MAP) {
284			local mappedLibs ;
285
286			for i in $(libs) {
287				local mapped = $($(TARGET_LIBRARY_NAME_MAP)_$(i)) ;
288				mapped ?= $(i) ;
289				mappedLibs += $(mapped) ;
290			}
291
292			libs = $(mappedLibs) ;
293		}
294
295		local linkLibs ;
296		local needLibs ;
297
298		for i in $(libs)
299		{
300			local isfile = ;
301			if $(i:D) || $(i:G) {
302				isfile = true ;
303				if $(i:G) = <nogrist> {
304					i = $(i:G=) ;
305				}
306			} else {
307				switch $(i:B)
308				{
309					# XXX: _APP_ and _KERNEL_ should not be needed for ELF.
310					case _APP_ : isfile = true ;
311					case _KERNEL_ : isfile = true ;
312					case lib*	: isfile = true ;
313					case *	: isfile = ;
314				}
315				if ! $(isfile) && ( $(i:S) = .so || $(i:S) = .a ) {
316					isfile = true ;
317				}
318			}
319
320			if $(isfile) {
321				needLibs += $(i) ;
322			} else {
323				linkLibs += $(i) ;
324			}
325		}
326
327		NEEDLIBS on $(1) = $(NEEDLIBS) $(needLibs) ;
328		LINKLIBS on $(1) = $(LINKLIBS) -l$(linkLibs) ;
329
330		if $(needLibs) && ! $(NO_LIBRARY_DEPENDENCIES) {
331			Depends $(1) : $(needLibs) ;
332		}
333	}
334}
335
336rule AddResources
337{
338	# AddResources <name> : <resourcefiles> ;
339
340	# add grist to the resource files which don't have any yet
341	local resfiles ;
342	local file ;
343	for file in $(2) {
344		if ! $(file:G) {
345			file = [ FGristFiles $(file) ] ;
346		}
347		resfiles += $(file) ;
348	}
349
350	SEARCH on $(resfiles) += $(SEARCH_SOURCE) ;
351
352	for file in $(resfiles) {
353		if $(file:S) = .rdef {
354			local rdef = $(file) ;
355			file = $(rdef:S=.rsrc) ;
356			ResComp $(file) : $(rdef) ;
357		}
358		RESFILES on $(1) += $(file) ;
359	}
360}
361
362rule BuildPlatformObjects
363{
364	# Usage BuildPlatformObjects <sources> ;
365	# <sources> The sources.
366	#
367	local sources = [ FGristFiles $(1) ] ;
368	local objects = [ FGristFiles $(sources:S=$(SUFOBJ)) ] ;
369
370	PLATFORM on $(objects) = host ;
371	SUPPORTED_PLATFORMS on $(objects) = host ;
372
373	Objects $(sources) ;
374}
375
376rule BuildPlatformMain
377{
378	# Usage BuildPlatformMain <target> : <sources> : <libraries> ;
379	# <target> The executable/library.
380	# <sources> The sources.
381	# <libraries> Libraries to link against.
382	#
383	local target = $(1) ;
384	local sources = $(2) ;
385	local libs = $(3) ;
386	local objects = [ FGristFiles $(sources:S=$(SUFOBJ)) ] ;
387
388	PLATFORM on $(target) = host ;
389	SUPPORTED_PLATFORMS on $(target) = host ;
390	DONT_USE_BEOS_RULES on $(target) = true ;
391
392	local usesBeAPI = [ on $(target) return $(USES_BE_API) ] ;
393	if $(usesBeAPI) {
394		# propagate the flag to the objects
395		USES_BE_API on $(objects) = $(usesBeAPI) ;
396
397		# add the build libroot
398		if ! $(HOST_PLATFORM_BEOS_COMPATIBLE) {
399			Depends $(target) : $(HOST_LIBROOT) ;
400			NEEDLIBS on $(target) += $(HOST_LIBROOT) ;
401		}
402	}
403
404	Main $(target) : $(sources) ;
405	LinkAgainst $(target) : $(libs) ;
406}
407
408rule BuildPlatformSharedLibrary
409{
410	# Usage BuildPlatformSharedLibrary <target> : <sources> : <libraries> ;
411	# <target> The library.
412	# <sources> The sources.
413	# <libraries> Libraries to link against.
414	#
415	local target = $(1) ;
416	local sources = $(2) ;
417	local libs = $(3) ;
418
419	BuildPlatformMain $(target) : $(sources) : $(libs) ;
420	LINKFLAGS on $(target) = [ on $(target) return $(LINKFLAGS) ]
421		-shared -Xlinker -soname=\"$(target)\" ;
422}
423
424rule BuildPlatformMergeObject
425{
426	# BuildPlatformMergeObject <name> : <sources> : <other objects> ;
427	# Compiles source files and merges the object files to an object file.
428	# <name>: Name of the object file to create. No grist will be added.
429	# <sources>: Sources to be compiled. Grist will be added.
430	# <other objects>: Object files or static libraries to be merged. No grist
431	#                  will be added.
432	#
433	local target = $(1) ;
434	local sources = $(2) ;
435	local otherObjects = $(3) ;
436	local objects = [ FGristFiles $(sources:S=$(SUFOBJ)) ] ;
437
438	PLATFORM on $(target) = host ;
439	SUPPORTED_PLATFORMS on $(target) = host ;
440
441	local usesBeAPI = [ on $(target[1]) return $(USES_BE_API) ] ;
442	if $(usesBeAPI) {
443		# propagate the flag to the objects
444		USES_BE_API on $(objects) = $(usesBeAPI) ;
445	}
446
447	MergeObject $(target) : $(sources) : $(otherObjects) ;
448}
449
450rule BuildPlatformStaticLibrary
451{
452	# BuildPlatformStaticLibrary <lib> : <sources> ;
453	# Creates a static library from sources.
454	# <lib>: The library.
455	# <sources>: List of source files.
456
457	local lib = $(1) ;
458	local sources = $(2) ;
459	local objects = [ FGristFiles $(sources:S=$(SUFOBJ)) ] ;
460
461	PLATFORM on $(lib) = host ;
462	SUPPORTED_PLATFORMS on $(lib) = host ;
463
464	local usesBeAPI = [ on $(lib) return $(USES_BE_API) ] ;
465	if $(usesBeAPI) {
466		# propagate the flag to the objects
467		USES_BE_API on $(objects) = $(usesBeAPI) ;
468	}
469
470	StaticLibrary $(lib) : $(sources) ;
471}
472
473