xref: /haiku/Jamrules (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1# Vanilla Jam compatibility
2if ! $(INVOCATION_SUBDIR_SET) {
3
4	rule FIsPrefix
5	{
6		# FIsPrefix <a> : <b> ;
7		# Returns true, if list <a> is a prefix (a proper one or equal) of
8		# list <b>, an empty list otherwise.
9		local a = $(1) ;
10		local b = $(2) ;
11		while $(a) && $(a[1]) = $(b[1]) {
12			a = $(a[2-]) ;
13			b = $(b[2-]) ;
14		}
15
16		if $(a) {
17			return ;
18		} else {
19			return true ;
20		}
21	}
22
23	rule LocalClean { Clean $(1) : $(2) ; }
24
25	rule LocalDepends { Depends $(1) : $(2) ; }
26
27} # vanilla Jam compatibility
28
29# The directory for build system specific files
30OBOS_BUILD_DIR = [ FDirName $(OBOS_TOP) build ] ;
31
32# Cache files for header scanning and jamfile caching
33HCACHEFILE = header_cache ;
34JCACHEFILE = jamfile_cache ;
35LOCATE on $(HCACHEFILE) $(JCACHEFILE) = $(OBOS_BUILD_DIR) ;
36
37# Include BuildConfig
38{
39	local buildConfig = [ GLOB $(OBOS_BUILD_DIR) : BuildConfig ] ;
40	if ! $(buildConfig)
41	{
42		ECHO "No BuildConfig found in $(OBOS_BUILD_DIR)!" ;
43		EXIT "Run ./configure in the source tree's root directory first!" ;
44	}
45	LOCATE on BuildConfig = $(OBOS_BUILD_DIR) ;
46	include BuildConfig ;
47}
48
49# analyze GCC version
50if ! $(GCC_RAW_VERSION) {
51	ECHO "Variable GCC_RAW_VERSION not set. Please run ./configure or" ;
52	EXIT "specify it manually." ;
53}
54GCC_VERSION = ;
55{
56	# split the raw version string at `.' and `-' characters
57	local version = $(GCC_RAW_VERSION) ;
58	while $(version) {
59		local split = [ Match "([^.-]*)[.-](.*)" : $(version) ] ;
60		if $(split) {
61			GCC_VERSION += $(split[1]) ;
62			version = $(split[2]) ;
63		} else {
64			GCC_VERSION += $(version) ;
65			version = ;
66		}
67	}
68}
69
70# Save the platform default headers.
71PLATFORM_DEFAULT_HEADERS = $(HDRS) ;
72
73# We do not include any local BeOS system headers by default
74CCFLAGS += -nostdinc ;
75C++FLAGS += -nostdinc ;
76
77# Determine if we're building on PPC or x86
78# Determine mimetype of executable
79# Cross compiling can come later
80
81TARGET_CPU ?= $(OSPLAT:L) ;
82OBOS_VERSION ?= R1 ;
83
84switch $(TARGET_CPU) {
85	case ppc :
86	{
87		if $(METROWERKS) {
88			# at least parts of OpenBeOS still can be compiled with
89			# the Metrowerks compiler on BeOS/PPC
90			OBOS_TARGET_TYPE ?= "application/x-be-executable" ;
91		} else {
92			OBOS_TARGET_TYPE ?= "application/x-vnd.Be-elfexecutable" ;
93		}
94		DEFINES += __POWERPC__ ;
95		OBOS_BOOT_PLATFORM = openfirmware ;
96	}
97	case x86 :
98	{
99		# nothing special to do here...
100		OBOS_BOOT_PLATFORM = bios_ia32 ;
101	}
102	case * :
103		Exit "Currently unsupported build platform:" $(TARGET_CPU) ;
104}
105
106# set target specific variables
107{
108	#Echo "Building for" $(TARGET_CPU) ;
109
110	OBOS_TARGET ?= $(TARGET_CPU).$(OBOS_VERSION) ;
111	OBOS_TARGET_TYPE ?= "application/x-vnd.Be-elfexecutable" ;
112	OBOS_ARCH ?= $(TARGET_CPU) ;
113	OBOS_TARGET_DEFINE ?= "ARCH_"$(TARGET_CPU) ;
114}
115
116# Disable warnings only if WARNINGS is set to 0
117# Should be enabled by default later
118#
119WARNINGS ?= 1 ;
120if $(WARNINGS) = 1 {
121	# For an explanation of the different warning options, see:
122	# http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_2.html
123	# to get even more warnings, add:
124	# -Wwrite-strings  	(doesn't work well with some Be headers)
125	# -Wundef			(dito)
126	# -Wconversion		(gets you many warnings about implicit conversions)
127	# -W				(gets you even more warnigs)
128	CCFLAGS += -Wall -Wno-multichar -Wmissing-prototypes ;
129	CCFLAGS += -Wpointer-arith -Wcast-align -Wsign-compare ;
130	C++FLAGS += -Wall -Wno-multichar -Wmissing-prototypes -Wno-ctor-dtor-privacy -Woverloaded-virtual ;
131	C++FLAGS += -Wpointer-arith -Wcast-align -Wsign-compare ;
132} else {
133	CCFLAGS += -Wno-multichar ;
134	C++FLAGS += -Wno-multichar ;
135}
136
137# standard kernel C/C++ flags
138KERNEL_CCFLAGS ?= -Wall -Wno-multichar -Wmissing-prototypes -finline -nostdinc ;
139KERNEL_CCFLAGS += -fno-builtin -D$(OBOS_TARGET_DEFINE) ;
140KERNEL_CCFLAGS += -DBOCHS_DEBUG_HACK=$(BOCHS_DEBUG_HACK) ;
141KERNEL_C++FLAGS ?= -Wall -Wno-multichar -Wmissing-prototypes -finline -nostdinc ;
142KERNEL_C++FLAGS += -fno-builtin -fno-exceptions -fno-rtti -D$(OBOS_TARGET_DEFINE) ;
143KERNEL_C++FLAGS += -DBOCHS_DEBUG_HACK=$(BOCHS_DEBUG_HACK) ;
144if $(GCC_VERSION[1]) >= 3 {
145	KERNEL_C++FLAGS += -fno-use-cxa-atexit ;
146}
147
148# We might later want to introduce debug levels or handle the whole issue
149# differently. For now there's only on or off.
150#
151DEBUG ?= 0 ;
152if $(DEBUG) != 0 {
153	OPTIM ?= -O0 ;
154	CCFLAGS += -g [ FDefines DEBUG=$(DEBUG) ] ;
155	C++FLAGS += -g [ FDefines DEBUG=$(DEBUG) ] ;
156	KERNEL_CCFLAGS += -g [ FDefines DEBUG=$(DEBUG) ] ;
157	KERNEL_C++FLAGS += -g [ FDefines DEBUG=$(DEBUG) ] ;
158	LINKFLAGS += -g ;
159} else {
160	OPTIM ?= -O2 ;
161}
162#
163# To disable for the tests OPTIM and DEBUG are overridden, set the environment
164# variable NO_TEST_DEBUG.
165
166# Instructs the Library rule to not make its object files temporary.
167# This is needed as some objects are used in a static library and for an
168# executable.
169KEEPOBJS = true ;
170
171# under BeOS use copyattr instead of cp
172if $(OS) = BEOS
173{
174	CP = copyattr --data ;
175}
176
177# If no OBOS_OBJECT_TARGET is not defined yet, use our default directory and
178# include our "OBOS_TARGET" as subdirectory in there (to prevent different
179# builds mixing objects from different targets).
180if ! $(OBOS_OBJECT_TARGET) {
181	OBOS_OBJECT_TARGET ?= [ FDirName $(OBOS_TOP) objects $(OBOS_TARGET) ] ;
182}
183
184# If no OBOS_DISTRO_TARGET is not defined yet, use our default directory and
185# include our "OBOS_TARGET" as subdirectory in there (to prevent different
186# builds mixing executables from different targets).
187if ! $(OBOS_DISTRO_TARGET) {
188	OBOS_DISTRO_TARGET ?= [ FDirName $(OBOS_TOP) distro $(OBOS_TARGET) ] ;
189}
190
191# Set our version number if not already set and mark it as a developer build
192if ! $(OBOS_BUILD_VERSION) {
193	OBOS_BUILD_VERSION ?= "1 0 0 a 1" ;
194	OBOS_BUILD_DESCRIPTION ?= "Developer Build" ;
195}
196
197# If OBOS_BUILD_VERSION is set, but OBOS_BUILD_DESCRIPTION isn't, mark it as
198# an unknown build.
199if ! $(OBOS_BUILD_DESCRIPTION) {
200	OBOS_BUILD_DESCRIPTION ?= "Unknown Build" ;
201}
202
203# Relative subdirs for distro dir
204OBOS_ADDON_DIR  ?= [ FDirName $(OBOS_DISTRO_TARGET) beos system add-ons ] ;
205OBOS_APPS_DIR   ?= [ FDirName $(OBOS_DISTRO_TARGET) beos apps ] ;
206OBOS_BIN_DIR    ?= [ FDirName $(OBOS_DISTRO_TARGET) beos bin ] ;
207OBOS_ETC_DIR    ?= [ FDirName $(OBOS_DISTRO_TARGET) beos etc ] ;
208OBOS_FONTS_DIR  ?= [ FDirName $(OBOS_ETC_DIR) fonts ] ;
209OBOS_KERNEL_DIR ?= [ FDirName $(OBOS_DISTRO_TARGET) beos system ] ;
210OBOS_PREFS_DIR  ?= [ FDirName $(OBOS_DISTRO_TARGET) beos preferences ] ;
211OBOS_SERVER_DIR ?= [ FDirName $(OBOS_DISTRO_TARGET) beos system servers ] ;
212OBOS_SHLIB_DIR  ?= [ FDirName $(OBOS_DISTRO_TARGET) beos system lib ] ;
213OBOS_STLIB_DIR  ?= [ FDirName $(OBOS_DISTRO_TARGET) develop lib
214							  $(OBOS_ARCH) ] ;
215OBOS_TEST_DIR   ?= [ FDirName $(OBOS_TOP) tests ] ;
216
217OBOS_KERNEL_CONFIG = config.$(OBOS_ARCH).ini ;
218OBOS_KERNEL = kernel.$(OBOS_ARCH) ;
219OBOS_FLOPPY = floppy.$(OBOS_ARCH) ;
220
221rule SetupIncludes
222{
223	# XXX add "opengl" later
224	local os_includes = add-ons add-ons/file_system add-ons/graphics add-ons/input_server add-ons/screen_saver add-ons/tracker app device drivers game interface kernel media mail midi midi2 net storage support translation ;
225
226	# Overwrite any exiting content when changing HDRS. This rule may be invoked multiple times.
227
228	# Use headers directory, to allow to do things like include <posix/string.h>
229	HDRS = [ FDirName $(OBOS_TOP) headers ] ;
230
231	# Use posix headers directory
232	HDRS += [ FDirName $(OBOS_TOP) headers posix ] ;
233
234	# Use public OS header directories
235	HDRS += [ PublicHeaders $(os_includes) ] ;
236
237	# Used as a fallback, the R5 header directories (we should remove this as soon as possible)
238	HDRS += /boot/develop/headers/posix /boot/develop/headers/cpp ;
239
240	# The platform dependent headers.
241	HDRS += $(PLATFORM_HEADERS) ;
242}
243
244rule SetupR5Includes
245{
246	# Unsets HDRS, so that the OBOS headers do not `shadow' the system headers.
247	HDRS = ;
248}
249
250rule SetupDefaultIncludes
251{
252	# Resets HDRS to the default headers for the build platform.
253	HDRS = $(PLATFORM_DEFAULT_HEADERS) ;
254}
255
256#-------------------------------------------------------------------------------
257# Things Jam needs in order to work :)
258#-------------------------------------------------------------------------------
259
260rule Object
261{
262	# This is basically the original Jambase 2.4 Object rule stripped by
263	# comments. Only the final switch statement has been changed to allow
264	# intermediate C++ files for Yacc and Lex.
265
266	LocalClean clean : $(<) ;
267
268	MakeLocate $(<) : $(LOCATE_TARGET) ;
269	SEARCH on $(>) = $(SEARCH_SOURCE) ;
270
271	HDRS on $(<) = $(SEARCH_SOURCE) $(SUBDIRHDRS) $(HDRS) ;
272
273	HDRRULE on $(>) = HdrRule ;
274	HDRSCAN on $(>) = $(HDRPATTERN) ;
275	HDRSEARCH on $(>) =
276		$(SEARCH_SOURCE:E) $(SUBDIRHDRS) $(HDRS) $(STDHDRS) ;
277
278	HDRGRIST on $(>) = $(HDRGRIST) ;
279
280	DEFINES on $(<) += $(DEFINES) ;
281
282	# if source is not .c, generate .c with specific rule
283
284	switch $(>:S)
285	{
286	    case .asm : As $(<) : $(>) ;
287	    case .c :	Cc $(<) : $(>) ;
288	    case .C :	C++ $(<) : $(>) ;
289	    case .cc :	C++ $(<) : $(>) ;
290	    case .cpp : C++ $(<) : $(>) ;
291	    case .f :	Fortran $(<) : $(>) ;
292	    case .l :	if [ on $(2) return $(GENERATE_C++) ] {
293						C++ $(<) : $(<:S=.cpp) ;
294						LexC++ $(<:S=.cpp) : $(>) ;
295					} else {
296						Cc $(<) : $(<:S=.c) ;
297						Lex $(<:S=.c) : $(>) ;
298					}
299	    case .s :	As $(<) : $(>) ;
300	    case .y :	if [ on $(2) return $(GENERATE_C++) ] {
301						C++ $(<) : $(<:S=.cpp) ;
302						Bison $(<:S=.cpp) : $(>) ;
303					} else {
304						Cc $(<) : $(<:S=$(YACCGEN)) ;
305						Yacc $(<:S=$(YACCGEN)) : $(>) ;
306					}
307	    case * :	UserObject $(<) : $(>) ;
308	}
309}
310
311rule UserObject
312{
313	switch $(2)
314	{
315	case *.S    : assemble $(1) : $(2) ;
316	case *.o    : return ;
317	case *      : ECHO "unknown suffix on" $(2) ;
318	}
319}
320
321# Override the default to give "prettier" command lines.
322actions Cc
323{
324	$(CC) -c "$(2)" $(CCFLAGS) $(CCDEFS) $(CCHDRS) -o "$(1)" ;
325}
326
327actions C++
328{
329	$(C++) -c "$(2)" $(C++FLAGS) $(CCDEFS) $(CCHDRS) -o "$(1)" ;
330}
331
332
333#-------------------------------------------------------------------------------
334# General High-level OBOS target rules
335#-------------------------------------------------------------------------------
336
337rule App
338{
339	# App <name> : <sources> : <libraries> ;
340	SetupIncludes ;
341	SetupObjectsDir ;
342	Main $(1) : $(2) ;
343	MakeLocate $(1) : $(OBOS_APPS_DIR) ;
344	LinkSharedOSLibs $(1) : $(3) ;
345	LINKFLAGS on $(1) = [ on $(1) return $(LINKFLAGS) ]
346						-Xlinker -soname=_APP_ ;
347}
348
349rule BinCommand
350{
351	# BinCommand <name> : <sources> : <libraries> ;
352	SetupIncludes ;
353	SetupObjectsDir ;
354	Main $(1) : $(2) ;
355	MakeLocate $(1) : $(OBOS_BIN_DIR) ;
356	LinkSharedOSLibs $(1) : $(3) ;
357	LINKFLAGS on $(1) = [ on $(1) return $(LINKFLAGS) ]
358						-Xlinker -soname=_APP_ ;
359}
360
361rule StdBinCommands
362{
363	# StdBinCommands <sources> : <libs> ;
364	SetupIncludes ;
365	SetupObjectsDir ;
366	local libs = $(2) ;
367	local source ;
368	for source in $(1)
369	{
370		local target = $(source:S=) ;
371		target = [ FGristFiles $(target) ] ;
372
373		BinCommand $(target) : $(source) : $(libs) ;
374	}
375}
376
377rule Preference
378{
379	# Preference <name> : <sources> : <libraries> ;
380	SetupIncludes ;
381	SetupObjectsDir ;
382	Main $(1) : $(2) ;
383	MakeLocate $(1) : $(OBOS_PREFS_DIR) ;
384	LinkSharedOSLibs $(1) : $(3) ;
385	LINKFLAGS on $(1) = [ on $(1) return $(LINKFLAGS) ]
386						-Xlinker -soname=_APP_ ;
387}
388
389rule Server
390{
391	# Server <name> : <sources> : <libraries> ;
392
393	SetupIncludes ;
394	SetupObjectsDir ;
395	Main $(1) : $(2) ;
396	MakeLocate $(1) : $(OBOS_SERVER_DIR) ;
397	LinkSharedOSLibs $(1) : $(3) ;
398	LINKFLAGS on $(1) = [ on $(1) return $(LINKFLAGS) ]
399						-Xlinker -soname=_APP_ ;
400}
401
402# test pseudo targets
403NOTFILE obostests ;
404NOTFILE r5tests ;
405
406rule CommonTestLib
407{
408	# CommonTestLib <target> : <sources> : <obos libraries>
409	#	: <r5 libraries> : <test libraries> : <public headers>;
410	# Builds a unit test for both OBOS and R5 modules.
411	# <target> The name of the target.
412	# <sources> The list of sources.
413	# <obos libraries> A list of link libraries for the OBOS tests (as passed
414	# to LinkSharedOSLibs).
415	# <r5 libraries> A list of link libraries for the R5 tests (as passed
416	# to LinkSharedOSLibs).
417	# <test libraries> A list of link libraries for both OBOS tests and R5 tests
418	# that have a common name (i.e. specify libx.so and the OBOS tests will link
419	# to libx.so and the R5 tests will link to libx_r5.so).
420	# <public headers> A list of public header dirs (as passed to
421	# UsePublicHeaders).
422
423	TestLib $(1) : $(2) : [ FDirName $(OBOS_TEST_DIR) unittester lib ] : $(3) $(5) : $(6) ;
424	R5TestLib $(1) : $(2) : [ FDirName $(OBOS_TEST_DIR) unittester_r5 lib ] : $(4) [ R5SharedLibraryNames $(5) ] ;
425}
426
427rule TestLib
428{
429	# TestLib <target> : <sources> : <dest> : <libraries> : <public headers>
430	# Builds a unit test library for an OBOS module.
431	# <target> The name of the target.
432	# <sources> The list of sources.
433	# <dest> The directory for the target (as passed to FDirName).
434	# <libraries> A list of link libraries (as passed to LinkSharedOSLibs).
435	# <public headers> A list of public header dirs (as passed to
436	# UsePublicHeaders).
437
438	local target = $(1) ;
439	local sources = $(2) ;
440	local dest = $(3) ;
441	local libraries = $(4) ;
442	local headerDirs = $(5) ;
443	local objects = [ FGristFiles $(sources:S=$(SUFOBJ)) ] ;
444
445	# Our Main replacement.
446	MainFromObjects $(target) : $(objects) ;
447	TestObjects $(sources) : $(headerDirs) ;
448
449	MakeLocate $(target) : $(dest) ;
450	Depends $(target) : libcppunit.so ;
451	Depends obostests : $(target) ;
452	LinkSharedOSLibs $(target) : libcppunit.so $(libraries) ;
453	LINKFLAGS on $(target) = $(LINKFLAGS) -nostart -Xlinker -soname=\"$(target)\" ;
454}
455
456rule R5TestLib
457{
458	# R5TestLib <target> : <sources> : <dest> : <libraries>
459	# Builds a unit test for an R5 module. "_r5" is appended to the object
460	# and the target name.
461	# <target> The name of the target.
462	# <sources> The list of sources.
463	# <dest> The directory for the target (as passed to FDirName).
464	# <libraries> A list of link libraries (as passed to LinkSharedOSLibs).
465
466	local target = $(1:B)_r5$(1:S) ;
467	local sources = $(2) ;
468	local dest = $(3) ;
469	local libraries = $(4) ;
470	local objects = [ R5ObjectNames $(sources) ] ;
471
472	# Our Main replacement.
473	MainFromObjects $(target) : $(objects) ;
474	TestObjects $(sources) : : true ;
475
476	MakeLocate $(target) : $(dest) ;
477	Depends $(target) : libcppunit.so ;
478	Depends r5tests : $(target) ;
479	LinkSharedOSLibs $(target) : libcppunit.so $(libraries) ;
480	LINKFLAGS on $(target) = $(LINKFLAGS) -nostart -Xlinker -soname=\"$(target)\" ;
481}
482
483rule CommonUnitTest
484{
485	# CommonUnitTest <target> : <sources> : <dest> : <obos libraries>
486	#	: <r5 libraries> : <public headers>;
487	# Builds a unit test for both OBOS and R5 modules.
488	# <target> The name of the target.
489	# <sources> The list of sources.
490	# <dest> The directory for the target (as passed to FDirName).
491	# <obos libraries> A list of link libraries for the OBOS tests (as passed
492	# to LinkSharedOSLibs).
493	# <r5 libraries> A list of link libraries for the R5 tests (as passed
494	# to LinkSharedOSLibs).
495	# <public headers> A list of public header dirs (as passed to
496	# UsePublicHeaders).
497
498	UnitTest $(1) : $(2) : $(3) : $(4) : $(6) ;
499	R5UnitTest $(1) : $(2) : $(3) : $(5) ;
500}
501
502rule UnitTest
503{
504	# UnitTest <target> : <sources> : <dest> : <libraries> : <public headers>
505	# Builds a unit test for an OBOS module.
506	# <target> The name of the target.
507	# <sources> The list of sources.
508	# <dest> The directory for the target (as passed to FDirName).
509	# <libraries> A list of link libraries (as passed to LinkSharedOSLibs).
510	# <public headers> A list of public header dirs (as passed to
511	# UsePublicHeaders).
512
513	local target = $(1) ;
514	local sources = $(2) ;
515	local dest = $(3) ;
516	local libraries = $(4) ;
517	local headerDirs = $(5) ;
518	local objects = [ FGristFiles $(sources:S=$(SUFOBJ)) ] ;
519
520	# Our Main replacement.
521	MainFromObjects $(target) : $(objects) ;
522	TestObjects $(sources) : $(headerDirs) ;
523
524	MakeLocate $(target) : [ FDirName $(OBOS_TEST_DIR) $(dest) ] ;
525	Depends $(target) : libcppunit.so ;
526	Depends obostests : $(target) ;
527	LinkSharedOSLibs $(target) : libcppunit.so $(libraries) ;
528}
529
530rule R5UnitTest
531{
532	# R5UnitTest <target> : <sources> : <dest> : <libraries>
533	# Builds a unit test for an R5 module. "_r5" is appended to the object
534	# and the target name.
535	# <target> The name of the target.
536	# <sources> The list of sources.
537	# <dest> The directory for the target (as passed to FDirName).
538	# <libraries> A list of link libraries (as passed to LinkSharedOSLibs).
539
540	local target = $(1)_r5 ;
541	local sources = $(2) ;
542	local dest = $(3) ;
543	local libraries = $(4) ;
544	local objects = [ R5ObjectNames $(sources) ] ;
545
546	# Our Main replacement.
547	MainFromObjects $(target) : $(objects) ;
548	TestObjects $(sources) : : true ;
549
550	MakeLocate $(target) : [ FDirName $(OBOS_TEST_DIR) $(dest) ] ;
551	Depends $(target) : libcppunit.so ;
552	Depends r5tests : $(target) ;
553	LinkSharedOSLibs $(target) : libcppunit.so $(libraries) ;
554}
555
556rule R5ObjectNames
557{
558	# R5ObjectNames <sources> ;
559	# Returns a list of gristed object names given a list of source file names.
560	# Moreover each object names gets "_r5" inserted before the object suffix.
561	local objects = $(1:S=)_r5 ;
562	return [ FGristFiles $(objects:S=$(SUFOBJ)) ] ;
563}
564
565rule R5Objects
566{
567	# R5Objects <sources>
568	# Similar to Objects, but appends "_r5" to the object file names and
569	# removes `-nostdinc' from the CC and C++ flags to enable system headers.
570	# <sources> The source files.
571
572	# Remove `-nostdinc' from CCFLAGS and C++FLAGS.
573	local oldCCFLAGS = $(CCFLAGS) ;
574	local oldC++FLAGS = $(C++FLAGS) ;
575	CCFLAGS = [ Filter $(CCFLAGS) : -nostdinc ] ;
576	C++FLAGS = [ Filter $(C++FLAGS) : -nostdinc ] ;
577
578	local sources = $(1) ;
579	local source ;
580	for source in [ FGristFiles $(sources) ]
581	{
582		local object = [ R5ObjectNames $(source) ] ;
583		Object $(object) : $(source) ;
584		LocalDepends obj : $(object) ;
585	}
586
587	# Reset CCFLAGS and C++FLAGS to original values.
588	CCFLAGS = $(oldCCFLAGS) ;
589	C++FLAGS = $(oldC++FLAGS) ;
590}
591
592rule TestObjects
593{
594	# TestLib <sources> : <public headers> : <r5>
595	# Compiles objects for tests.
596	# <sources> The list of sources.
597	# <public headers> A list of public header dirs (as passed to
598	# UsePublicHeaders).
599	# <r5> If set, "_r5" is appended to the object file names and
600	# <public headers> is ignored. Furthermore the pre-processor macro
601	# TEST_R5 is defined, TEST_OBOS otherwise.
602
603	local sources = $(1) ;
604	local headerDirs = $(2) ;
605	local r5 = $(3) ;
606	local objects ;
607
608	# Turn optimization off.
609	if ! $(NO_TEST_DEBUG) {
610		local optim = $(OPTIM) ;
611		OPTIM = ;
612	}
613
614	SetupObjectsDir ;
615
616	# compile
617	if $(r5) {
618		SetupR5Includes ;
619		objects = [ R5ObjectNames $(sources) ] ;
620		R5Objects $(sources) ;
621	} else {
622		SetupIncludes ;
623		objects = [ FGristFiles $(sources:S=$(SUFOBJ)) ] ;
624		Objects $(sources) ;
625	}
626
627	# set headers/defines
628	UseCppUnitObjectHeaders $(sources) : $(objects) ;
629	if $(r5) {
630		ObjectsDefines $(objects) : TEST_R5 ;
631	} else {
632		UsePublicObjectHeaders $(sources) : $(headerDirs) : $(objects) ;
633		ObjectsDefines $(objects) : TEST_OBOS ;
634	}
635
636	if ! $(NO_TEST_DEBUG) {
637		# Turn debugging on. That is usually desired for test code.
638		ObjectCcFlags $(objects) : "-g" ;
639		ObjectC++Flags $(objects) : "-g" ;
640
641		# Turn optimization on again.
642		OPTIM = $(optim) ;
643	}
644}
645
646rule R5SharedLibraryNames
647{
648	# R5SharedLibraryNames <sources> ;
649	# Returns a list of shared library names given a list of file names. NO
650	# GRISTING IS PERFORMED :-) However, each library names gets "_r5" inserted
651	# before the shared lib suffix.
652	return $(1:S=)_r5.so ;
653}
654
655rule SimpleTest
656{
657	# UnitTest <target> : <sources> : <libraries>
658	# Builds a unit test for an OBOS module.
659	# <target> The name of the target.
660	# <sources> The list of sources.
661	# <dest> The directory for the target (as passed to FDirName).
662	# <libraries> A list of link libraries (as passed to LinkSharedOSLibs).
663	# <public headers> A list of public header dirs (as passed to
664	# UsePublicHeaders).
665
666	local target = $(1) ;
667	local sources = $(2) ;
668	local libraries = $(3) ;
669	local relPath = [ FRelPath src tests : $(SUBDIR_TOKENS) ] ;
670
671	# Turn optimization off.
672	if ! $(NO_TEST_DEBUG) {
673		local optim = $(OPTIM) ;
674		OPTIM = ;
675	}
676
677	SetupIncludes ;
678	SetupObjectsDir ;
679	MakeLocateObjects $(sources) ;
680	Main $(target) : $(sources) ;
681	MakeLocate $(target) : [ FDirName $(OBOS_TEST_DIR) $(relPath) ] ;
682	Depends obostests : $(target) ;
683	LinkSharedOSLibs $(target) : $(libraries) ;
684	ObjectsDefines $(sources) : TEST_OBOS ;
685	if ! $(NO_TEST_DEBUG) {
686		# Turn debugging on. That is usually desired for test code.
687		ObjectCcFlags $(sources) : "-g" ;
688		ObjectC++Flags $(sources) : "-g" ;
689
690		# Turn optimization on again.
691		OPTIM = $(optim) ;
692	}
693}
694
695rule Addon
696{
697	# Addon <name> : <relpath> : <sources> : <is executable> : <libraries> ;
698	# <name>: Name of the add-on.
699	# <relpath>: Path where the add-on shall live relative to the add-on dir.
700	# <sources>: Source files.
701	# <is executable>: true, if the target shall be executable as well.
702	# <libraries>: Libraries to be linked against.
703
704	local isExecutable = $(4) ;
705
706	SetupIncludes ;
707	SetupObjectsDir ;
708	Main $(1) : $(3) ;
709
710	# Create output dir path for addon
711	local targetdir;
712	targetdir = [ FDirName $(OBOS_ADDON_DIR) $(2) ] ;
713
714	MakeLocate $(1) : $(targetdir) ;
715
716	local linkFlags = -Xlinker -soname=\"$(1)\" ;
717	if $(isExecutable) != true {
718		linkFlags = -nostart $(linkFlags) ;
719	}
720	LINKFLAGS on $(1) = [ on $(1) return $(LINKFLAGS) ] $(linkFlags) ;
721	LinkSharedOSLibs $(1) : $(5) ;
722}
723
724rule R5KernelAddon
725{
726	# R5KernelAddon <name> : <relpath> : <sources> ;
727
728	local sources = $(3) ;
729
730	Addon $(1) : $(2) : $(3) ;
731	ObjectCcFlags $(sources) : -D_KERNEL_MODE=1 -no-fpic ;
732	ObjectC++Flags $(sources) : -D_KERNEL_MODE=1 -no-fpic
733								-fno-exceptions -fno-rtti ;
734	LINKFLAGS on $(1) = [ on $(1) return $(LINKFLAGS) ] -nostdlib ;
735	LinkSharedOSLibs $(1) : /boot/develop/lib/x86/_KERNEL_ ;
736}
737
738rule Translator
739{
740	# Translator <name> : <sources> : <libraries> ;
741	SetupIncludes ;
742	SetupObjectsDir ;
743	Main $(1) : $(2) ;
744	LinkSharedOSLibs $(1) : $(3) ;
745
746	# Create output dir path for translator
747	local targetdir;
748	targetdir = [ FDirName $(OBOS_ADDON_DIR) Translators ] ;
749	MakeLocate $(1) : $(targetdir) ;
750}
751
752rule MakeLocateObjects
753{
754	# MakeLocateObjects <sources_or_objects> ;
755
756	local _objs = [ FGristFiles $(1:S=$(SUFOBJ)) ] ;
757
758	for o in $(_objs)
759	{
760		local dir = $(o:D) ;
761		if $(dir) {
762			MakeLocate $(o) : [ FDirName $(LOCATE_TARGET) $(dir) ] ;
763		} else {
764			MakeLocate $(o) : $(LOCATE_TARGET) ;
765		}
766	}
767}
768
769rule StaticLibrary
770{
771	# StaticLibrary <name> : <sources> [ : <target dir> ] ;
772	# Creates a static library from sources.
773	# <name>: Basename of the library, without leading "lib" and trailing ".a".
774	# <source>: List of source files.
775	# <target dir>: Directory into which the library shall be placed. Defaults
776	#               to the standard directory for static libs.
777	#
778	local lib = lib$(1)$(SUFLIB) ;
779	SetupIncludes ;
780	SetupObjectsDir ;
781	MakeLocateObjects $(2) ;
782	Library $(lib) : $(2) ;
783	local targetDir = $(3) ;
784	if ! $(targetDir) {
785		targetDir = $(OBOS_STLIB_DIR) ;
786	}
787	MakeLocate $(lib) : $(targetDir) ;
788
789	# If KEEPOBJS is set, Library doesn't make the library depend on
790	# `lib'.
791	if $(KEEPOBJS) {
792		LocalDepends lib : $(lib) ;
793	}
794}
795
796rule R5KernelStaticLibrary
797{
798	# R5KernelStaticLibrary <name> : <sources> ;
799
800	local lib = lib$(1)$(SUFLIB) ;
801	local sources = $(2) ;
802
803	SetupIncludes ;
804	SetupObjectsDir ;
805	MakeLocateObjects $(sources) ;
806	Library $(lib) : $(sources) ;
807	ObjectCcFlags $(sources) : -D_KERNEL_MODE=1 -no-fpic ;
808	ObjectC++Flags $(sources) : -D_KERNEL_MODE=1 -no-fpic
809								-fno-exceptions -fno-rtti ;
810}
811
812rule MergeObjectFromObjects
813{
814	# MergeObjectFromObjects <name> : <objects> ;
815	local objects = [ FGristFiles $(2) ] ;
816	MakeLocate $(1) : $(LOCATE_TARGET) ;
817	Depends $(1) : $(objects) ;
818	LINK on $(1) = ld ;
819	MergeObjectFromObjects1 $(1) : $(objects) ;
820}
821
822actions MergeObjectFromObjects1
823{
824	$(LINK) -r $(2) -o $(1) ;
825}
826
827rule MergeObject
828{
829	# MergeObject <name> : <sources> ;
830	SetupIncludes ;
831	SetupObjectsDir ;
832	MakeLocateObjects $(2) ;
833	Objects $(2) ;
834	MergeObjectFromObjects $(1) : $(2:S=$(SUFOBJ)) ;
835}
836
837rule SharedLibraryFromObjects
838{
839	# SharedLibraryFromObjects <name> : <objects> : <libraries> ;
840	local _lib = lib$(1).so ;
841	MainFromObjects $(_lib) : $(2) ;
842	MakeLocate $(_lib) : $(OBOS_SHLIB_DIR) ;
843	LINKFLAGS on $(_lib) = [ on $(_lib) return $(LINKFLAGS) ]
844						   -nostart -Xlinker -soname=\"$(_lib)\" ;
845	LinkSharedOSLibs $(_lib) : $(3) ;
846}
847
848rule SharedLibrary
849{
850	# SharedLibrary <name> : <sources> : <libraries> ;
851	SetupIncludes ;
852	SetupObjectsDir ;
853	MakeLocateObjects $(2) ;
854	Objects $(2) ;
855	SharedLibraryFromObjects $(1) : $(2:S=$(SUFOBJ)) : $(3) ;
856}
857
858rule LinkSharedOSLibs
859{
860	# LinkSharedOSLibs <name> : <libs> ;
861	# Valid elements for <libs> are e.g. "be" or "libopenbeos.so" or
862	# "/boot/.../libfoo.so". If the basename starts with "lib" or the thingy
863	# has a dirname or grist, it is added to the NEEDLIBS variable (i.e. the
864	# file will be bound!), otherwise it is prefixed "-l" and added to
865	# LINKLIBS. If you want to specify a target that isn't a library and
866	# also has neither grist nor a dirname, you can prepend "<nogrist>" as
867	# grist; it will be stripped by this rule.
868
869	for i in $(>)
870	{
871		local isfile = ;
872		if $(i:D) || $(i:G) {
873			isfile = true ;
874			if $(i:G) = <nogrist> {
875				i = $(i:G=) ;
876			}
877		} else {
878			switch $(i:B)
879			{
880				# XXX: _APP_ and _KERNEL_ should not be needed for ELF.
881				case _APP_ : isfile = true ;
882				case _KERNEL_ : isfile = true ;
883				case lib*	: isfile = true ;
884				case *	: isfile = ;
885			}
886			if ! $(isfile) && ( $(i:S) = .so || $(i:S) = .a ) {
887				isfile = true ;
888			}
889		}
890		if $(isfile) {
891			NEEDLIBS on $(1) = [ on $(1) return $(NEEDLIBS) ] $(i) ;
892			Depends $(1) : $(i) ;
893		} else {
894			LINKLIBS on $(1) = [ on $(1) return $(LINKLIBS) ] -l$(i) ;
895		}
896	}
897}
898
899rule LinkStaticOSLibs
900{
901	# LinkStaticOSLibs <name> : <libs> ;
902
903	for i in $(>)
904	{
905		LINKLIBS on $(<) = $(LINKLIBS) -l $(i) ;
906	}
907}
908
909rule AddResources
910{
911	# AddResources <name> : <resourcefiles> ;
912
913	local resfiles = [ FGristFiles $(2) ] ;
914	SEARCH on $(resfiles) += $(SEARCH_SOURCE) ;
915
916	for file in $(resfiles) {
917		if $(file:S) = .rdef {
918			local rdef = $(file) ;
919			file = $(rdef:S=.rsrc) ;
920			ResComp $(file) : $(rdef) ;
921		}
922		RESFILES on $(1) += $(file) ;
923	}
924}
925
926rule ResComp
927{
928	# ResComp <resource file> : <rdef file> ;
929	#
930	# <resource file> and <rdef file> must be gristed.
931
932	SetupObjectsDir ;
933
934	SEARCH on $(2) += $(SEARCH_SOURCE) ;
935	MakeLocate $(1) : $(LOCATE_TARGET) ;
936	Depends $(1) : $(2) rc ;
937	LocalClean clean : $(1) ;
938	ResComp1 $(1) : rc $(2) ;
939}
940
941actions ResComp1
942{
943	$(2[1]) -o $(1) $(2[2-])
944}
945
946rule ObjectsDefines
947{
948	# Like ObjectDefines, but allows multiple files to be supplied
949	local file ;
950	for file in $(1) {
951		ObjectDefines $(file) : $(2) ;
952	}
953}
954
955rule SourceHdrs
956{
957	# SourceHdrs <sources> : <headers> [ : <gristed objects> ] ;
958	#
959	# Is a wrapper for ObjectHdrs, that passes <sources> and <headers> or,
960	# if supplied <objects> and <headers>, and also adjusts HDRSEARCH (not
961	# done by ObjectHdrs).
962
963	local sources = [ FGristFiles $(1) ] ;
964	local headers = $(2) ;
965	local objects = $(3) ;
966
967	local file ;
968	if $(objects) {
969		for file in $(objects) {
970			ObjectHdrs $(file) : $(headers) ;
971		}
972	} else {
973		for file in $(sources) {
974			ObjectHdrs $(file) : $(headers) ;
975		}
976	}
977
978	# Also add the header search dirs to HDRSEARCH. Note, that these dirs
979	# will be listed after the STDHDRS (if any), but that's better than not
980	# being listed at all.
981	HDRSEARCH on $(sources) += $(headers) ;
982}
983
984rule PublicHeaders
985{
986	# PublicHeaders <group list>
987	#
988	# Returns the directory names for the public header dirs identified by
989	# <group list>.
990
991	local list = $(1) ;
992	local dirs = [ FDirName $(OBOS_TOP) headers os ] ;
993
994	for i in $(list) {
995		dirs += [ FDirName $(OBOS_TOP) headers os $(i) ] ;
996	}
997	return $(dirs) ;
998}
999
1000rule PrivateHeaders
1001{
1002	# PrivateHeaders <group list>
1003	#
1004	# Returns the directory names for the private header dirs identified by
1005	# <group list>.
1006
1007	local list = $(1) ;
1008	local dirs ;
1009	for i in $(list) {
1010		dirs += [ FDirName $(OBOS_TOP) headers private $(i) ] ;
1011	}
1012	return $(dirs) ;
1013}
1014
1015rule LibraryHeaders
1016{
1017	# LibraryHeaders <group list>
1018	#
1019	# Returns the directory names for the library header dirs identified by
1020	# <group list>.
1021
1022	local list = $(1) ;
1023	local dirs ;
1024	for i in $(list) {
1025		dirs += [ FDirName $(OBOS_TOP) headers libs $(i) ] ;
1026	}
1027	return $(dirs) ;
1028}
1029
1030rule ArchHeaders
1031{
1032	# usage: ArchHeaders <arch> ;
1033	#
1034	# <arch> specifies the architecture (e.g. x86).
1035
1036	return [ FDirName $(OBOS_TOP) headers private kernel arch $(1) ] ;
1037}
1038
1039rule UsePublicHeaders
1040{
1041	# UsePublicHeaders <group list> ;
1042	#
1043	# Adds the public C header dirs given by <group list> to the header search
1044	# dirs of the subdirectory.
1045	# NOTE: This rule must be invoked *before* the rule that builds the
1046	# objects.
1047
1048	UseHeaders [ PublicHeaders $(1) ] ;
1049}
1050
1051rule UsePublicObjectHeaders
1052{
1053	# UsePublicObjectHeaders <sources> : <group list> [ : <objects> ] ;
1054	#
1055	# Adds the public C header dirs given by <group list> to the header search
1056	# dirs of either the object targets of <sources> or if supplied to
1057	# <objects>. Also adjusts HDRSEARCH of <sources>.
1058	# NOTE: This rule must be invoked *after* the rule that builds the objects.
1059
1060	SourceHdrs $(1) : [ PublicHeaders $(2) ] : $(3) ;
1061}
1062
1063rule UsePrivateHeaders
1064{
1065	# UsePrivateHeaders <group list> ;
1066	#
1067	# Adds the private C header dirs given by <group list> to the header search
1068	# dirs of the subdirectory.
1069	# NOTE: This rule must be invoked *before* the rule that builds the objects.
1070
1071	UseHeaders [ PrivateHeaders $(1) ] ;
1072}
1073
1074rule UsePrivateObjectHeaders
1075{
1076	# UsePrivateObjectHeaders <sources> : <group list> [ : <objects> ] ;
1077	#
1078	# Adds the private C header dirs given by <group list> to the header search
1079	# dirs of either the object targets of <sources> or if supplied to
1080	# <objects>. Also adjusts HDRSEARCH of <sources>.
1081	# NOTE: This rule must be invoked *after* the rule that builds the objects.
1082
1083	SourceHdrs $(1) : [ PrivateHeaders $(2) ] : $(3) ;
1084}
1085
1086rule UseHeaders
1087{
1088	# UseHeaders <headers> ;
1089	#
1090	# Adds the C header dirs <headers> to the header search
1091	# dirs of the subdirectory.
1092	# NOTE: This rule must be invoked *before* the rule that builds the objects.
1093
1094	local header ;
1095	for header in $(1) {
1096		SubDirHdrs $(header) ;
1097	}
1098}
1099
1100rule UseCppUnitHeaders
1101{
1102	SubDirHdrs [ FDirName $(OBOS_TOP) headers tools cppunit ] ;
1103}
1104
1105rule UseCppUnitObjectHeaders
1106{
1107	# UseCppUnitObjectHeaders <sources> [ : <objects> ] ;
1108	SourceHdrs $(1) : [ FDirName $(OBOS_TOP) headers tools cppunit ] : $(2) ;
1109}
1110
1111rule UseArchHeaders
1112{
1113	# usage: UseArchHeaders <arch> ;
1114	#
1115	# <arch> specifies the architecture (e.g. x86).
1116	# NOTE: This rule must be invoked *before* the rule that builds the objects.
1117
1118	local headers = [ ArchHeaders $(1) ] ;
1119	local opt = -D$(OBOS_TARGET_DEFINE) ;
1120
1121	SubDirCcFlags $(opt)  ;
1122	SubDirC++Flags $(opt)  ;
1123	HDRS += $(headers) ;
1124}
1125
1126rule UseArchObjectHeaders
1127{
1128	# usage: UseArchObjectHeaders <sources> : <arch> : [ <objects> ] ;
1129	#
1130	# <arch> specifies the architecture (e.g. x86).
1131	# <sources_or_objects> Source or object files.
1132	# NOTE: This rule must be invoked *after* the rule that builds the objects.
1133
1134	local sources = $(1) ;
1135	local headers = [ ArchHeaders $(2) ] ;
1136	local objects = $(3) ;
1137	local targets ;
1138	if $(objects) {
1139		targets = $(objects) ;
1140	} else {
1141		targets = $(sources) ;
1142	}
1143	local opt = -D$(OBOS_TARGET_DEFINE) ;
1144
1145	ObjectCcFlags $(targets) : $(opt)  ;
1146	ObjectC++Flags $(targets) : $(opt)  ;
1147	SourceHdrs $(sources) : $(headers) : $(objects) ;
1148}
1149
1150rule UsePosixHeaders
1151{
1152	# XXX changed to do nothing
1153}
1154
1155rule UsePosixObjectHeaders
1156{
1157	# UsePosixObjectHeaders <sources> [ : <objects> ] ;
1158	#
1159	# Adds the POSIX header dir to the header search
1160	# dirs of either the object targets of <sources> or if supplied to
1161	# <objects>. Also adjusts HDRSEARCH of <sources>.
1162	# NOTE: This rule must be invoked *after* the rule that builds the objects.
1163
1164	SourceHdrs $(1) : [ FDirName $(OBOS_TOP) headers posix ] : $(2) ;
1165}
1166
1167rule UseLibraryHeaders
1168{
1169	# UseLibraryHeaders <group list> ;
1170	#
1171	# Adds the library header dirs given by <group list> to the header search
1172	# dirs of the subdirectory.
1173	# NOTE: This rule must be invoked *before* the rule that builds the objects.
1174
1175	UseHeaders [ LibraryHeaders $(1) ] ;
1176}
1177
1178rule SplitPath
1179{
1180	# SplitPath <path> ;
1181	# Decomposes a path into its components.
1182	local path = $(1:G=) ;
1183	local components ;
1184	# $(path:D) for "/" is "/". Therefore the second condition.
1185	while $(path:D) && $(path:D) != $(path)
1186	{
1187		# Note: $(path:B) returns "." for "..", but $(path:D=) is fine.
1188		components = $(path:D=) $(components) ;
1189		path = $(path:D) ;
1190	}
1191	components = $(path) $(components) ;
1192	return $(components) ;
1193}
1194
1195rule PrependObjectHdrs
1196{
1197	# PrependObjectHdrs <objects_or_sources> : <dirs> ;
1198	# Prepends <dirs> to the list of header search dirs of the objects
1199	# specified by <objects_or_sources>. The HDRS variable will not be
1200	# changed, only CCHDRS.
1201	# Note: A subsequent ObjectHdrs invocation will therefore undo the
1202	# effect of this rule.
1203	# NOTE: This is a hack.
1204
1205	local objects = [ FGristFiles $(1:S=$(SUFOBJ)) ] ;
1206	local dirs = $(2) ;
1207	for object in $(objects) {
1208		# Don't change HDRS to avoid screwing up the header scanning.
1209		PREPENDED_HDRS on $(object)
1210			= $(dirs) [ on $(object) return $(PREPENDED_HDRS) ] ;
1211		CCHDRS on $(object)
1212			= [ FIncludes [ on $(object) return $(PREPENDED_HDRS) $(HDRS) ] ] ;
1213	}
1214}
1215
1216rule SymLink
1217{
1218	# SymLink <target> : <source> ;
1219	# Links <target> to <source>.
1220	# <source> is the exact link contents. No binding is done.
1221	LINKCONTENTS on $(1) = $(2) ;
1222	SymLink1 $(1) ;
1223	LocalDepends all : $(target) ;
1224}
1225
1226actions SymLink1
1227{
1228	$(RM) "$(1)" && $(LN) -s "$(LINKCONTENTS)" "$(1)"
1229}
1230
1231rule RelSymLink
1232{
1233	# RelSymLink <link> : <link target>
1234	# Creates a relative symbolic link from <link> to <link target>.
1235	# <link> and <link target> can be usual targets. They may have a grist
1236	# and don't need to have any dirname. Their LOCATE variables are used to
1237	# find their locations.
1238
1239	local target = $(1) ;
1240	local source = $(2) ;
1241	local targetDir = [ on $(target) FDirName $(LOCATE[1]) $(target:D) ] ;
1242	local sourceDir = [ on $(source) FDirName $(LOCATE[1]) $(source:D) ] ;
1243	local sourcePath = $(source:G=) ;
1244	sourcePath = $(sourcePath:D=$(sourceDir)) ;
1245	local targetDirComponents = [ SplitPath $(targetDir) ] ;
1246	local sourceComponents = [ SplitPath $(sourcePath) ] ;
1247
1248	SymLink $(target)
1249		: [ FRelPath $(targetDirComponents) : $(sourceComponents) ] ;
1250	NOUPDATE $(target) ;
1251	Depends $(target) : $(source) ;
1252	LocalClean clean : $(target) ;
1253}
1254
1255rule AbsSymLink
1256{
1257	# AbsSymLink <link> : <link target> : <link dir>
1258	# Creates an absolute symbolic link from <link> to <link target>.
1259	# <link> and <link target> must be usual targets. If <link dir> is
1260	# given, then it is set as LOCATE directory on <link>.
1261
1262	LocalDepends files : $(1) ;
1263	Depends $(1) : $(2) ;
1264	if $(3) {
1265		MakeLocate $(1) : $(3) ;
1266	}
1267	SEARCH on $(2) += $(SEARCH_SOURCE) ;
1268	LocalClean clean : $(1) ;
1269}
1270
1271actions AbsSymLink
1272{
1273	target="$(2)"
1274	case "$target" in
1275		/*) ;;
1276		*) target=`pwd`/"$target";;
1277	esac
1278	$(RM) "$(1)" && $(LN) -s "$target" "$(1)"
1279}
1280
1281rule OBOSInstall
1282{
1283	# Usage: OBOSInstall <[ install [ and uninstall ] pseudotarget ]>
1284	#					 : <directory> : <sources to install>
1285	#					 : [ <installgrist> ] : [ <install rule> ] ;
1286	local install = $(1[1]) ;
1287	install ?= install ;
1288	local uninstall = $(1[2]) ;
1289	uninstall ?= un$(install) ;
1290	local dir = $(2) ;
1291	local sources = $(3) ;
1292	local installgrist = $(4) ;
1293	installgrist ?= $(INSTALLGRIST) ;
1294	local installRule = $(5) ;
1295	installRule ?= Install ;
1296	local targets = $(sources:G=$(installgrist)) ;
1297
1298	Depends $(install) : $(targets) ;
1299	Clean $(uninstall) : $(targets) ;
1300
1301	SEARCH on $(sources) += $(SEARCH_SOURCE) ;
1302	MakeLocate $(targets) : $(dir) ;
1303
1304	local source ;
1305	for source in $(sources) {
1306		local target = $(source:G=$(installgrist)) ;
1307
1308		Depends $(target) : $(source) ;
1309		$(installRule) $(target) : $(source) ;
1310
1311		if [ on $(target) return $(MODE) ] {
1312			Chmod $(target) ;
1313		}
1314
1315		if $(OWNER) && $(CHOWN) {
1316			Chown $(target) ;
1317			OWNER on $(target) = $(OWNER) ;
1318		}
1319
1320		if $(GROUP) && $(CHGRP) {
1321			Chgrp $(target) ;
1322			GROUP on $(target) = $(GROUP) ;
1323		}
1324	}
1325}
1326
1327rule OBOSInstallAbsSymLink
1328{
1329	# Usage: OBOSInstallAbsSymLink <[ install [ and uninstall ] pseudotarget ]>
1330	#							   : <directory> : <sources to install>
1331	#							   : [ <installgrist> ] ;
1332	OBOSInstall $(1) : $(2) : $(3) : $(4) : AbsSymLink ;
1333}
1334
1335rule InstallRelSymLinkAdapter
1336{
1337	# InstallRelSymLinkAdapter <link> : <link target>
1338	if ! [ on $(2) return $(TARGET) ] {
1339		TARGET on $(2) = [ on $(2) return $(SEARCH) ] ;
1340	}
1341	RelSymLink $(1) : $(2) ;
1342}
1343
1344rule OBOSInstallRelSymLink
1345{
1346	# Usage: OBOSInstallRelSymLink <[ install [ and uninstall ] pseudotarget ]>
1347	#							   : <directory> : <sources to install>
1348	#							   : [ <installgrist> ] ;
1349	OBOSInstall $(1) : $(2) : $(3) : $(4) : InstallRelSymLinkAdapter ;
1350}
1351
1352
1353#-------------------------------------------------------------------------------
1354# Low-level OBOS utility rules
1355#-------------------------------------------------------------------------------
1356rule SetupObjectsDir
1357{
1358	local rel_objectsdir;
1359
1360	# Copy subdir tokens except the first, as that will be "sources", and we
1361	# do not want to include that :)
1362	rel_objectsdir = [ FDirName $(SUBDIR_TOKENS[2-]) ] ;
1363	LOCATE_TARGET = [ FDirName $(OBOS_OBJECT_TARGET) $(rel_objectsdir) ] ;
1364	LOCATE_SOURCE = $(LOCATE_TARGET) ;
1365	SEARCH_SOURCE = [ Filter $(SEARCH_SOURCE) : $(LOCATE_TARGET) ]
1366					$(LOCATE_TARGET) ;
1367}
1368
1369#-------------------------------------------------------------------------------
1370# Link rule/action are overwritten as they don't handle linking files who's name
1371# contain spaces very well. Also adds resources and version to executable.
1372#-------------------------------------------------------------------------------
1373rule Link
1374{
1375	# Note: RESFILES must be set before invocation.
1376	MODE on $(<) = $(EXEMODE) ;
1377	on $(1) XRes $(1) : $(RESFILES) ;
1378	Chmod $(<) ;
1379	SetType $(1) ;
1380	MimeSet $(1) ;
1381	SetVersion $(1) ;
1382}
1383
1384actions Link bind NEEDLIBS
1385{
1386	$(LINK) $(LINKFLAGS) -o "$(1)" $(UNDEFS) "$(2)" "$(NEEDLIBS)" $(LINKLIBS) ;
1387}
1388
1389rule LexC++
1390{
1391	Depends $(1) : $(2) ;
1392	MakeLocate $(1) : $(LOCATE_SOURCE) ;
1393	LocalClean clean : $(1) ;
1394}
1395
1396actions LexC++
1397{
1398	$(LEX) -o$(1) $(2)
1399}
1400
1401rule Bison
1402{
1403	local _h ;
1404
1405	_h = $(1).h ;
1406
1407	MakeLocate $(<) $(_h) : $(LOCATE_SOURCE) ;
1408
1409    Depends $(<) $(_h) : $(>) ;
1410    Bison1 $(<) $(_h) : $(>) ;
1411    LocalClean clean : $(<) $(_h) ;
1412
1413	# make sure someone includes $(_h) else it will be
1414	# a deadly independent target
1415
1416	Includes $(<) : $(_h) ;
1417}
1418
1419actions Bison1
1420{
1421	bison $(YACCFLAGS) -o $(1[1]) $(2)
1422}
1423
1424# BeOS specific rules
1425
1426rule XRes
1427{
1428	# XRes <target> : <resource files>
1429	if $(2)
1430	{
1431		Depends $(1) : $(2) ;
1432		XRes1 $(1) : $(2) ;
1433	}
1434}
1435
1436rule XRes1 { }
1437
1438rule SetVersion
1439{
1440	# SetVersion <target>
1441}
1442
1443rule SetType
1444{
1445	# SetType <target>
1446}
1447
1448rule MimeSet
1449{
1450	# SetType <target>
1451}
1452
1453
1454if $(OS) = BEOS
1455{
1456
1457actions XRes1
1458{
1459	xres -o "$(1)" "$(2)" ;
1460}
1461
1462actions SetVersion
1463{
1464	setversion "$(1)" -system $(OBOS_BUILD_VERSION) -short "$(OBOS_BUILD_DESCRIPTION)" ;
1465}
1466
1467actions SetType
1468{
1469	settype -t $(OBOS_TARGET_TYPE) "$(1)" ;
1470}
1471
1472actions MimeSet
1473{
1474	mimeset -f "$(1)" ;
1475}
1476
1477}	# if BEOS
1478
1479
1480rule assemble
1481{
1482	Depends $(<) : $(>) ;
1483	ASFLAGS on $(<) += $(ASFLAGS) $(SUBDIRASFLAGS) ;
1484	ASHDRS on $(<) = [ FIncludes $(SEARCH_SOURCE) $(SUBDIRHDRS) $(HDRS) ] ;
1485}
1486
1487actions assemble
1488{
1489	$(CC) -c "$(2)" -O2 $(ASFLAGS) -D_ASSEMBLER $(KERNEL_CCFLAGS) $(ASHDRS) -o "$(1)" ;
1490}
1491
1492# Overridden to allow spaces in file names.
1493actions Chmod1
1494{
1495	$(CHMOD) "$(MODE)" "$(1)"
1496}
1497
1498# Overridden to allow spaces in file names.
1499actions piecemeal together existing Clean
1500{
1501	$(RM) "$(>)"
1502}
1503
1504rule ObjectReference
1505{
1506	# ObjectReference <reference object> : <source object>
1507	# Makes <reference object> refer to the same file as <source object>.
1508	# The filenames must of course be identical.
1509	# <source object> must have already been LOCATEd.
1510
1511	local ref = $(1) ;
1512	local source = $(2) ;
1513	if $(ref) != $(source) {
1514		Depends $(ref) : $(source) ;
1515		LOCATE on $(ref) = [ on $(source) return $(LOCATE) ] ;
1516	}
1517}
1518
1519rule ObjectReferences
1520{
1521	# ObjectReferences <source objects>
1522	# Creates local references to <source objects>, i.e. identifiers with the
1523	# current grist referring to the same files. <source objects> must have
1524	# already been LOCATEd.
1525
1526	local source ;
1527	for source in $(1) {
1528		ObjectReference [ FGristFiles $(source) ] : $(source) ;
1529	}
1530}
1531
1532rule Filter
1533{
1534	# Filter <list> : <excludes> ;
1535	# Removes all occurrences of <excludes> in <list>.
1536
1537	local list = $(1) ;
1538	local excludes = $(2) ;
1539	local newList ;
1540	local item ;
1541	for item in $(list) {
1542		local skip ;
1543		local exclude ;
1544		for exclude in $(excludes) {
1545			if $(item) = $(exclude) {
1546				skip = true ;
1547			}
1548		}
1549		if ! $(skip) {
1550			newList += $(item) ;
1551		}
1552	}
1553	return $(newList) ;
1554}
1555
1556
1557## Kernel stuff!
1558
1559rule SetupKernel
1560{
1561	# Usage SetupKernel <sources_or_objects> : <extra_cc_flags>;
1562
1563	local _objs = [ FGristFiles $(1:S=$(SUFOBJ)) ] ;
1564
1565	#Setup Kernel header directories
1566	local public_kernel_includes = add-ons/file_system add-ons/graphics device drivers kernel storage support ;
1567	local private_kernel_includes = kernel libroot kernel/boot/platform/$(OBOS_BOOT_PLATFORM) ;
1568	# Use posix headers directory
1569	HDRS = [ FDirName $(OBOS_TOP) headers posix ] ;
1570	# Use public OS header directories
1571	HDRS += [ PublicHeaders $(public_kernel_includes) ] ;
1572	# Use private directories
1573	HDRS += [ PrivateHeaders $(private_kernel_includes) ] ;
1574	# The platform dependent headers.
1575	HDRS += $(PLATFORM_HEADERS) ;
1576
1577	UseArchHeaders $(OBOS_ARCH) ;
1578
1579	SetupObjectsDir ;
1580
1581	CCFLAGS on $(_objs) = $(KERNEL_CCFLAGS) $(2) ;
1582	C++FLAGS on $(_objs) = $(KERNEL_C++FLAGS) $(2) ;
1583}
1584
1585rule KernelObjects
1586{
1587	SetupKernel $(1) : $(2) ;
1588
1589	Objects $(1) ;
1590}
1591
1592rule KernelLd
1593{
1594	# KernelLd <name> : <objs> : <linkerscript> : <args> : <gcc_off> : <config_section> ;
1595
1596	SetupKernel $(2) ;
1597	LINK on $(1) = ld ;
1598
1599	LINKFLAGS on $(1) = $(4) ;
1600	if $(3) { LINKFLAGS on $(1) += --script=$(3) ; }
1601
1602	# Remove any preset LINKLIBS
1603	LINKLIBS on $(1) =  ;
1604
1605	# Show that we depend on the libraries we need
1606	LocalClean clean : $(1) ;
1607	LocalDepends all : $(1) ;
1608	Depends $(1) : $(2) ;
1609
1610	if $(6) {
1611		for i in $(6) {
1612			KernelConfigSection $(i) : elf32 : $(1) ;
1613		}
1614	}
1615
1616	MakeLocate $(1) : $(LOCATE_TARGET) ;
1617
1618	# Add the platform specific static libs (libgcc.a).
1619	if ! $(5) {
1620		LINKLIBS on $(1) += $(PLATFORM_LINKLIBS) ;
1621	}
1622}
1623
1624actions KernelLd
1625{
1626	$(LINK) $(LINKFLAGS) -o "$(1)" "$(2)" $(LINKLIBS) ;
1627}
1628
1629rule KernelMergeObject
1630{
1631	# KernelMergeObject <name> : <sources> : <extra CFLAGS> ;
1632
1633	SetupKernel $(2) : $(3) ;
1634
1635	MakeLocateObjects $(2) ;
1636	Objects $(2) ;
1637	MergeObjectFromObjects $(1) : $(2:S=$(SUFOBJ)) ;
1638}
1639
1640rule KernelStaticLibrary
1641{
1642	# Usage KernelStaticLibrary <name> : <sources> : <extra cc flags>  ;
1643	# This is designed to take a set of sources and libraries and create
1644	# a file called lib<name>.a
1645
1646	SetupKernel $(2) : $(3) ;
1647
1648	MakeLocateObjects $(2) ;
1649	Library $(1) : $(2) ;
1650}
1651
1652rule KernelStaticLibraryObjects
1653{
1654	# Usage KernelStaticLibrary <name> : <sources> ;
1655	# This is designed to take a set of sources and libraries and create
1656	# a file called <name>
1657
1658	SetupKernel $(2) ;
1659
1660	# Show that we depend on the libraries we need
1661	LocalClean clean : $(1) ;
1662	LocalDepends all : $(1) ;
1663	Depends $(1) : $(2) ;
1664
1665	MakeLocate $(1) : $(LOCATE_TARGET) ;
1666}
1667
1668actions KernelStaticLibraryObjects
1669{
1670	ar -r "$(1)" "$(2)" ;
1671}
1672
1673rule BuildPlatformMain
1674{
1675	# Usage BuildPlatformMain <target> : <sources> ;
1676	SetupObjectsDir ;
1677	SetupDefaultIncludes ;
1678
1679	# Remove `-nostdinc' from CCFLAGS and C++FLAGS.
1680	local oldCCFLAGS = $(CCFLAGS) ;
1681	local oldC++FLAGS = $(C++FLAGS) ;
1682	CCFLAGS = [ Filter $(CCFLAGS) : -nostdinc ] ;
1683	C++FLAGS = [ Filter $(C++FLAGS) : -nostdinc ] ;
1684
1685	Main $(1) : $(2) ;
1686
1687	# Reset CCFLAGS and C++FLAGS to original values.
1688	CCFLAGS = $(oldCCFLAGS) ;
1689	C++FLAGS = $(oldC++FLAGS) ;
1690}
1691
1692rule BuildPlatformTest
1693{
1694	# Usage BuildPlatformTest <target> : <sources> ;
1695
1696	local target = $(1) ;
1697	local sources = $(2) ;
1698
1699	BuildPlatformMain $(target) : $(sources) ;
1700	local relPath ;
1701	if [ FIsPrefix src tests : $(SUBDIR_TOKENS) ] {
1702		relPath = $(SUBDIR_TOKENS[3-]) ;
1703	} else {
1704		relPath = $(SUBDIR_TOKENS[2-]) ;
1705	}
1706	MakeLocate $(target) : [ FDirName $(OBOS_TEST_DIR) $(relPath) ] ;
1707}
1708
1709rule KernelConfigSection
1710{
1711	# KernelConfigSection <section> : <type> : <file>  ;
1712
1713	SECTION_NAMES on $(OBOS_KERNEL_CONFIG) += $(1) ;
1714	SECTION_TYPES on $(OBOS_KERNEL_CONFIG) += $(2) ;
1715	SECTION_FILES on $(OBOS_KERNEL_CONFIG) += $(3) ;
1716
1717	Depends $(OBOS_KERNEL_CONFIG) : $(3) ;
1718}
1719
1720rule WriteKernelConfig
1721{
1722	# usage: WriteKernelConfig <target> ;
1723
1724	LocalDepends files : $(1) ;
1725
1726	MakeLocate $(1) : $(OBOS_OBJECT_TARGET) ;
1727
1728	LocalClean clean : $(1) ;
1729}
1730
1731actions WriteKernelConfig bind SECTION_FILES
1732{
1733	target="$(1)"
1734	echo "# OpenBeOS Kernel Config File" > "$target"
1735	echo "# Automatically generated - do not edit!" >> "$target"
1736	count=0
1737	for section in "$(SECTION_NAMES)" ; do
1738		count=`expr $count + 1`
1739		eval section$count="$section"
1740	done
1741	i=1
1742	for type in "$(SECTION_TYPES)" ; do
1743		eval type$i="$type"
1744		i=`expr $i + 1`
1745	done
1746	i=1
1747	for file in "$(SECTION_FILES)" ; do
1748		eval file$i="$file"
1749		i=`expr $i + 1`
1750	done
1751	for i in `seq $count` ; do
1752		eval section="\$section$i"
1753		eval type="\$type$i"
1754		eval file="\$file$i"
1755		echo "" >> "$target"
1756		echo "["$section"]" >> "$target"
1757		echo "type="$type >> "$target"
1758		case "$file" in
1759			/*) ;;
1760			*) file=`pwd`/"$file";;
1761		esac
1762		echo "file="$file >> "$target"
1763	done
1764}
1765
1766rule BuildKernel
1767{
1768	# Usage BuildKernel <target> : <config_file> ;
1769	local kernel = $(1) ;
1770	local configFile = $(2) ;
1771	local bootmaker = bootmaker ;
1772
1773	LocalDepends all : $(kernel) ;
1774	Depends $(kernel) : $(configFile) $(bootmaker) ;
1775	LocalClean clean : $(kernel) ;
1776	MakeLocate $(kernel) : $(LOCATE_TARGET) ;
1777
1778	BOOT_MAKER on $(kernel) = $(bootmaker) ;
1779}
1780
1781actions BuildKernel bind BOOT_MAKER
1782{
1783	"$(BOOT_MAKER)" --strip-debug --strip-binary strip "$(2)" -o "$(1)" ;
1784	echo ""
1785	echo "Kernel linked!"
1786	echo ""
1787}
1788
1789rule KernelFloppyImage
1790{
1791	# Usage KernelFloppyImage <target> : <kernel> : <bootblock> ;
1792	local floppy = $(1) ;
1793	local kernel = $(2) ;
1794	local bootblock = $(3) ;
1795	local makeflop = makeflop ;
1796
1797	LocalDepends all : $(floppy) ;
1798	Depends $(floppy) : $(kernel) $(bootblock) $(makeflop) ;
1799	LocalClean clean : $(floppy) ;
1800	MakeLocate $(floppy) : $(OBOS_OBJECT_TARGET) ;
1801
1802	BOOT_BLOCK on $(floppy) = $(bootblock) ;
1803	MAKE_FLOP on $(floppy) = $(makeflop) ;
1804}
1805
1806# This may be a bit verbose, but I think it's useful to show what's
1807# going on, at least in this early stage of development.
1808actions KernelFloppyImage bind BOOT_BLOCK bind MAKE_FLOP
1809{
1810	"$(MAKE_FLOP)" "-p $(shell expr 18 \* 2 \* 512)" "$(BOOT_BLOCK)" "$(2)" "$(1)" ;
1811
1812	echo ""
1813	echo "*************************************************"
1814	echo "*         Kernel build completed!               *"
1815	echo "*    Boot image for a 1.44M floppy created      *"
1816	echo "*************************************************"
1817	echo ""
1818	echo "Floppy image is $(1)"
1819	echo "The following command will write it to a floppy on BeOS"
1820	echo "  dd if=$(1) of=/dev/disk/floppy/raw bs=18k"
1821	echo "Alternatively you can run"
1822	echo "  ./configure --floppy /dev/disk/floppy/raw"
1823	echo "once and build + write the image subsequently via"
1824	echo "  jam installfloppy"
1825	echo ""
1826}
1827
1828rule InstallFloppy
1829{
1830	# InstallFloppy <target> : <floppy>
1831	# "dd"s <floppy> to $(FLOPPY_PATH).
1832
1833	local target = $(1) ;
1834	local floppy = $(2) ;
1835
1836	NotFile $(target) ;
1837	Always $(target) ;
1838	Depends $(target) : $(floppy) ;
1839}
1840
1841actions InstallFloppy
1842{
1843	if [ -z $(FLOPPY_PATH) ] ; then
1844		echo "Can't install floppy: FLOPPY_PATH not set."
1845		echo "run: ./configure --floppy <floppy path>"
1846		echo
1847		exit 0
1848	fi
1849	dd if=$(2) of=$(FLOPPY_PATH) bs=18k
1850}
1851
1852#-------------------------------------------------------------------------------
1853# FreeType 2 specific rules and variables
1854#-------------------------------------------------------------------------------
1855
1856FT2_INCLUDE = [ FDirName $(OBOS_TOP) headers libs freetype2 ] ;
1857FT2_SRC     = [ FDirName $(OBOS_TOP) src libs freetype2 ] ;
1858
1859FT2_LIB     = freetype ;
1860
1861FT2_COMPONENTS ?= gzip       # support for gzip-compressed files.
1862                  autohint   # auto-hinter
1863                  base       # base component (public APIs)
1864                  bdf        # BDF font driver
1865                  cache      # cache sub-system
1866                  cff        # CFF/CEF font driver
1867                  cid        # Postscript CID-keyed font driver
1868                  pcf        # PCF font driver
1869                  pfr        # PFR/TrueDoc font driver
1870                  psaux      # Common Postscript routines module
1871                  pshinter   # Postscript hinter module
1872                  psnames    # Postscript names handling
1873                  raster     # Monochrome rasterizer
1874                  smooth     # Anti-aliased rasterizer
1875                  sfnt       # SFNT-based format support routines
1876                  truetype   # TrueType font driver
1877                  type1      # Postscript Type 1 font driver
1878                  type42     # Postscript Type 42 (embedded TrueType) driver
1879                  winfonts   # Windows FON/FNT font driver
1880                  ;
1881
1882rule UseFreeTypeHeaders
1883{
1884	SubDirHdrs $(FT2_INCLUDE) ;
1885}
1886
1887rule UseFreeTypeObjectHeaders
1888{
1889	# UseFreeTypeObjectHeaders <sources> [ : <objects> ] ;
1890	SourceHdrs $(1) : $(FT2_INCLUDE) : $(2) ;
1891}
1892
1893rule FT2_SubDir
1894{
1895	# FT2_SubDir <dir>
1896	# <dir>: Components of a directory in the original hierarchy.
1897	local dir = $(1) ;
1898	local topDir ;
1899	switch $(dir[1])
1900	{
1901		case "include"	: topDir = $(FT2_INCLUDE) ;
1902		case src		: topDir = $(FT2_SRC) ;
1903		case *			: ECHO "Unknown FreeType2 directory: " $(dir) ;
1904	}
1905	return [ FDirName $(topDir) $(dir[2-]) ] ;
1906}
1907
1908rule FT2_Library
1909{
1910	# FT2_Library <libname> : <sources>
1911	# Builds objects from sources and adds the objects to the list of objects
1912	# to be linked into the library.
1913	# <libname> The name of the library.
1914	# <sources> The sources.
1915
1916	local library = lib$(1).so ;
1917	local sources = $(2) ;
1918	SetupIncludes ;
1919	SetupObjectsDir ;
1920	MakeLocateObjects $(sources) ;
1921	Objects $(sources) ;
1922	LIBRARY_OBJECTS on $(library) += [ FGristFiles $(sources:S=$(SUFOBJ)) ] ;
1923}
1924
1925rule FT2_LinkLibrary
1926{
1927	# FT2_LinkLibrary <libname>
1928	# Links the library from the objects build with FT2_LIBRARY before.
1929
1930	local library = lib$(1).so ;
1931	local objects = [ on $(library) return $(LIBRARY_OBJECTS) ] ;
1932	ObjectReferences $(objects) ;
1933	objects = [ FGristFiles $(objects) ] ;
1934	SharedLibraryFromObjects $(1) : $(objects) ;
1935}
1936
1937