xref: /haiku/build/jam/ArchitectureRules (revision 8c039e20f24296695d8cb1abfb3700c42d3693af)
1rule ArchitectureSetup architecture
2{
3	# ArchitectureSetup <architecture> ;
4	#
5	# Initializes all global packaging architecture dependent variables for the
6	# given packaging architecture. Also sets HAIKU_ARCH (to the primary
7	# architecture), if this is the first invocation of the rule, and adds
8	# the architecture to HAIKU_ARCHS, if not yet contained.
9
10	# enable GCC -pipe option, if requested
11	local ccBaseFlags ;
12	if $(HAIKU_USE_GCC_PIPE) = 1 {
13		ccBaseFlags = -pipe ;
14	}
15
16	if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) != 1 {
17		# disable strict aliasing on anything newer than gcc 2 as it may lead to
18		# unexpected results.
19		# TODO: remove the -fno-strict-aliasing option when all code has been
20		#		analyzed/fixed with regard to aliasing.
21		ccBaseFlags += -fno-strict-aliasing ;
22
23		# Without this flag, GCC deletes many null-pointer checks that are
24		# technically undefined behavior (e.g. passing NULL to strdup, among
25		# others), which breaks both the kernel and various applications. See:
26		#  - https://freelists.org/post/haiku-development/hrev45320-Yet-another-nonobvious-effect-of-ftreevrp-optimization
27		#  - https://dev.haiku-os.org/ticket/13285#comment:8 (& subsequent comments)
28		#  - https://dev.haiku-os.org/ticket/10803#comment:4 (& subsequent comments)
29		# Note that the Linux also does the same:
30		#  - https://github.com/torvalds/linux/commit/a3ca86aea507904
31		ccBaseFlags += -fno-delete-null-pointer-checks ;
32
33		# disable some builtins that are incompatible with our definitions
34		ccBaseFlags += -fno-builtin-fork -fno-builtin-vfork ;
35	}
36
37	# default architecture tuning
38	local cpu = $(HAIKU_CPU_$(architecture)) ;
39	local archFlags ;
40	switch $(cpu) {
41		case ppc : archFlags += -mcpu=440fp ;
42		case arm : archFlags += -march=armv7-a -mfloat-abi=hard ;
43		case x86 : archFlags += -march=pentium ;
44		case riscv64 : archFlags += -march=rv64gc ;
45	}
46	if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
47		# TODO: These should be included in Clang's compiler specs.
48		ccBaseFlags += -fPIC ;
49		HAIKU_LINKFLAGS_$(architecture) += -shared ;
50	}
51	ccBaseFlags += $(archFlags) ;
52
53	# activating graphite optimizations
54	if $(HAIKU_USE_GCC_GRAPHITE_$(architecture)) = 1 {
55		ccBaseFlags += -floop-nest-optimize -fgraphite-identity ;
56	}
57
58	# initial state for flags etc.
59	HAIKU_C++_$(architecture) ?= $(HAIKU_CC_$(architecture)) ;
60	HAIKU_LINK_$(architecture) ?= $(HAIKU_CC_$(architecture)) ;
61
62	HAIKU_CCFLAGS_$(architecture) += $(ccBaseFlags) -nostdinc ;
63	HAIKU_C++FLAGS_$(architecture) += $(ccBaseFlags) -nostdinc ;
64	HAIKU_LINKFLAGS_$(architecture) += $(ccBaseFlags) ;
65	HAIKU_ASFLAGS_$(architecture) += $(archFlags) -nostdinc ;
66
67	# strip is required
68	if ! $(HAIKU_STRIP_$(architecture)) {
69		Exit "HAIKU_STRIP_$(architecture) not set. Please re-run configure." ;
70	}
71
72	HAIKU_ARCH_$(architecture) = $(cpu) ;
73	HAIKU_ARCH ?= $(cpu) ;
74		# Set only, if not set yet. This way HAIKU_ARCH is set to the primary
75		# architecture.
76	if ! $(cpu) in $(HAIKU_ARCHS) {
77		HAIKU_ARCHS += $(cpu) ;
78	}
79	HAIKU_DEFINES_$(architecture) += ARCH_$(cpu) ;
80
81	# directories
82	HAIKU_ARCH_OBJECT_DIR_$(architecture)
83		= [ FDirName $(HAIKU_OBJECT_BASE_DIR) $(architecture) ] ;
84	HAIKU_COMMON_DEBUG_OBJECT_DIR_$(architecture)
85		= [ FDirName $(HAIKU_ARCH_OBJECT_DIR_$(architecture)) common ] ;
86	HAIKU_DEBUG_0_OBJECT_DIR_$(architecture)
87		= [ FDirName $(HAIKU_ARCH_OBJECT_DIR_$(architecture)) release ] ;
88
89	local level ;
90	for level in $(HAIKU_DEBUG_LEVELS[2-]) {
91		HAIKU_DEBUG_$(level)_OBJECT_DIR_$(architecture)
92			= [ FDirName $(HAIKU_ARCH_OBJECT_DIR_$(architecture))
93				debug_$(level) ] ;
94	}
95
96	# set variables for gcc header options
97	SetIncludePropertiesVariables HAIKU : _$(architecture) ;
98
99	# warning flags
100	HAIKU_WARNING_CCFLAGS_$(architecture) = -Wall
101		-Wno-multichar
102		-Wpointer-arith -Wsign-compare
103		-Wmissing-prototypes ;
104	HAIKU_WARNING_C++FLAGS_$(architecture) = -Wall
105		-Wno-multichar
106		-Wpointer-arith -Wsign-compare
107		-Wno-ctor-dtor-privacy -Woverloaded-virtual ;
108
109	# disable some Clang warnings that are not very useful
110	if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
111		HAIKU_WARNING_CCFLAGS_$(architecture) += -Wno-address-of-packed-member
112			-Wno-unused-private-field -Wno-cast-align -Wno-gnu-designator
113			-Wno-builtin-requires-header ;
114		HAIKU_WARNING_C++FLAGS_$(architecture) += -Wno-address-of-packed-member
115			-Wno-unused-private-field -Wno-cast-align -Wno-gnu-designator
116			-Wno-builtin-requires-header ;
117	}
118
119	HAIKU_WERROR_FLAGS_$(architecture) = ;
120
121	if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) != 1 {
122		# TODO: Remove all these.
123		HAIKU_WERROR_FLAGS_$(architecture) += -Wno-error=unused-but-set-variable
124			-Wno-error=deprecated -Wno-error=deprecated-declarations
125			-Wno-error=cpp -Wno-error=trigraphs ;
126		# But these can stay.
127		HAIKU_WERROR_FLAGS_$(architecture) += -Wno-error=cast-align
128			-Wno-error=format-truncation ;
129	}
130
131	# debug flags
132	local debugFlags = -ggdb ;
133
134	# debug 0: suppress asserts
135	HAIKU_DEBUG_0_CCFLAGS_$(architecture) = [ FDefines NDEBUG=$(NDEBUG) ] ;
136	HAIKU_DEBUG_0_C++FLAGS_$(architecture) = [ FDefines NDEBUG=$(NDEBUG) ] ;
137
138	local level ;
139	for level in $(HAIKU_DEBUG_LEVELS[2-]) {
140		local flags = $(debugFlags) [ FDefines DEBUG=$(level) ] ;
141		HAIKU_DEBUG_$(level)_CCFLAGS_$(architecture) = $(flags) ;
142		HAIKU_DEBUG_$(level)_C++FLAGS_$(architecture) = $(flags) ;
143	}
144
145	# TODO: Temporary work-around. Should be defined in the compiler specs
146	HAIKU_LINKFLAGS_$(architecture) += -Xlinker --no-undefined ;
147
148	if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) = 1 {
149		HAIKU_DEFINES_$(architecture) += _BEOS_R5_COMPATIBLE_ ;
150	}
151
152	# private shared kernel/libroot headers
153	HAIKU_PRIVATE_SYSTEM_HEADERS_$(architecture)
154		= [ PrivateHeaders $(DOT) system system/arch/$(cpu) ] ;
155
156	# library and executable glue code
157	local commonGlueCode =
158		<src!system!glue!$(architecture)>init_term_dyn.o
159		<src!system!glue!arch!$(cpu)!$(architecture)>crti.o
160		<src!system!glue!arch!$(cpu)!$(architecture)>crtn.o
161		;
162	HAIKU_LIBRARY_BEGIN_GLUE_CODE_$(architecture) =
163		<src!system!glue!arch!$(cpu)!$(architecture)>crti.o
164		<$(architecture)>crtbeginS.o
165		<src!system!glue!$(architecture)>init_term_dyn.o
166		;
167	HAIKU_LIBRARY_END_GLUE_CODE_$(architecture) =
168		<$(architecture)>crtendS.o
169		<src!system!glue!arch!$(cpu)!$(architecture)>crtn.o
170		;
171	HAIKU_EXECUTABLE_BEGIN_GLUE_CODE_$(architecture) =
172		<src!system!glue!arch!$(cpu)!$(architecture)>crti.o
173		<$(architecture)>crtbeginS.o
174		<src!system!glue!$(architecture)>start_dyn.o
175		<src!system!glue!$(architecture)>init_term_dyn.o
176		;
177	HAIKU_EXECUTABLE_END_GLUE_CODE_$(architecture)
178		= $(HAIKU_LIBRARY_END_GLUE_CODE_$(architecture)) ;
179
180	SEARCH on <$(architecture)>crtbeginS.o <$(architecture)>crtendS.o
181		= $(HAIKU_GCC_LIB_DIR_$(architecture)) ;
182
183	# init library name map
184	local libraryGrist = "" ;
185	if $(architecture) != $(HAIKU_PACKAGING_ARCHS[1]) {
186		libraryGrist = $(architecture) ;
187	}
188	local i ;
189	for i in be bnetapi debug device game locale mail media midi midi2
190			network package root screensaver textencoding tracker
191			translation z {
192		local library = lib$(i).so ;
193		HAIKU_LIBRARY_NAME_MAP_$(architecture)_$(i)
194			= $(library:G=$(libraryGrist)) ;
195	}
196	HAIKU_LIBRARY_NAME_MAP_$(architecture)_localestub
197		= <$(architecture)>liblocalestub.a ;
198	HAIKU_LIBRARY_NAME_MAP_$(architecture)_shared
199		= <$(architecture)>libshared.a ;
200	if $(architecture) = $(HAIKU_PACKAGING_ARCHS[1]) {
201		HAIKU_LIBRARY_NAME_MAP_$(architecture)_input_server
202			= <nogrist>input_server ;
203	} else {
204		HAIKU_LIBRARY_NAME_MAP_$(architecture)_input_server
205			= <$(architecture)>input_server ;
206	}
207}
208
209
210rule KernelArchitectureSetup architecture
211{
212	# KernelArchitectureSetup <architecture> ;
213	#
214	# Initializes the global kernel and boot loader related variables. Those
215	# don't have a packaging architecture suffix, since they are only set for
216	# the primary packaging architecture. <architecture> is the primary
217	# packaging architecture (supplied for convenience).
218
219	HAIKU_KERNEL_ARCH = $(HAIKU_ARCH) ;
220
221	local cpu = $(HAIKU_CPU_$(architecture)) ;
222
223	switch $(cpu) {
224		case ppc :
225			HAIKU_KERNEL_PLATFORM ?= openfirmware ;
226			HAIKU_BOOT_TARGETS += openfirmware ;
227
228			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ; # in kB
229			# offset in floppy image (>= sizeof(haiku_loader))
230			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 384 ; # in kB
231
232		case sparc :
233			HAIKU_KERNEL_PLATFORM ?= openfirmware ;
234			HAIKU_BOOT_TARGETS += openfirmware ;
235
236			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ; # in kB
237			# offset in floppy image (>= sizeof(haiku_loader))
238			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 384 ; # in kB
239
240		case arm :
241			HAIKU_KERNEL_PLATFORM ?= efi ;
242			HAIKU_BOOT_TARGETS += efi ;
243
244			# SOC's like allwinner need an offset to skip the hardcoded initial loader
245			HAIKU_BOOT_SDIMAGE_BEGIN = 20475 ; # in KiB
246
247			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ;
248			# offset in floppy image (>= sizeof(haiku_loader))
249			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 192 ; # in kB - unused yet
250			HAIKU_BOOT_LOADER_BASE ?= 0x1000000 ;
251
252		case arm64 :
253			HAIKU_KERNEL_PLATFORM ?= efi ;
254			HAIKU_BOOT_TARGETS += efi ;
255
256			HAIKU_BOOT_SDIMAGE_BEGIN = 2 ; # in KiB
257
258			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ;
259			# offset in floppy image (>= sizeof(haiku_loader))
260			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 192 ; # in kB - unused yet
261			HAIKU_BOOT_LOADER_BASE ?= 0x1000000 ;
262
263		case x86 :
264			HAIKU_KERNEL_PLATFORM ?= bios_ia32 ;
265			HAIKU_BOOT_TARGETS += bios_ia32 pxe_ia32 ;
266			HAIKU_ANYBOOT_LEGACY = 1 ;
267
268			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 2880 ; # in kB
269			# offset in floppy image (>= sizeof(haiku_loader))
270			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 320 ; # in kB
271
272			# nasm is required for target arch x86
273			if ! $(HAIKU_NASM) {
274				Exit "HAIKU_NASM not set. Please re-run configure." ;
275			}
276
277		case riscv64 :
278			HAIKU_KERNEL_PLATFORM ?= efi ;
279			HAIKU_BOOT_TARGETS += efi riscv ;
280
281			HAIKU_BOOT_SDIMAGE_BEGIN = 2 ; # KiB
282
283			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ;
284			# offset in floppy image (>= sizeof(haiku_loader))
285			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 192 ; # in kB - unused yet
286			HAIKU_BOOT_LOADER_BASE ?= 0x1000000 ;
287
288		case x86_64 :
289			# x86_64 completely shares the x86 bootloader for MBR.
290			HAIKU_KERNEL_PLATFORM ?= bios_ia32 ;
291			HAIKU_BOOT_TARGETS += bios_ia32 efi pxe_ia32 ;
292
293			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 2880 ; # in kB
294			# offset in floppy image (>= sizeof(haiku_loader))
295			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 320 ; # in kB
296
297			# x86_64 kernel source is under arch/x86.
298			HAIKU_KERNEL_ARCH = x86 ;
299
300			# nasm is required for target arch x86_64
301			if ! $(HAIKU_NASM) {
302				Exit "HAIKU_NASM not set. Please re-run configure." ;
303			}
304
305		case m68k :
306			HAIKU_KERNEL_PLATFORM ?= atari_m68k ;
307			HAIKU_BOOT_TARGETS += amiga_m68k atari_m68k next_m68k ;
308			switch $(HAIKU_KERNEL_PLATFORM) {
309				case atari_m68k :
310				{
311					HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ; # in kB
312				}
313				case amiga_m68k :
314				{
315					# for now we have trouble reading from double-sided images
316					HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 880 ; # in kB
317				}
318				case next_m68k :
319				{
320					HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ; # in kB
321				}
322			}
323			# offset in floppy image (>= sizeof(haiku_loader))
324			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 260 ; # in kB
325			HAIKU_CONTAINER_STRIP_EXECUTABLES on
326				$(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME) = 1 ;
327
328		case * :
329			Exit "Currently unsupported target CPU:" $(cpu) ;
330	}
331
332	# private kernel headers to be used when compiling kernel code
333	HAIKU_PRIVATE_KERNEL_HEADERS =
334		[ PrivateHeaders $(DOT) kernel libroot shared
335			kernel/boot/platform/$(HAIKU_KERNEL_PLATFORM) ]
336		[ ArchHeaders $(HAIKU_KERNEL_ARCH) ]
337		[ FDirName $(HAIKU_COMMON_DEBUG_OBJECT_DIR_$(architecture)) system
338			kernel ]
339		$(HAIKU_PRIVATE_SYSTEM_HEADERS_$(architecture))
340		;
341
342	# C/C++ flags
343	local ccBaseFlags = -finline -fno-builtin ;
344
345	if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) != 1 {
346		if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
347			# Clang does not yet understand this flag.
348			ccBaseFlags += -fno-semantic-interposition ;
349		}
350
351		ccBaseFlags += -ffreestanding ;
352	}
353
354	local c++BaseFlags = $(ccBaseFlags) -fno-exceptions ;
355
356	if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) != 1
357			&& $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
358		c++BaseFlags += -fno-use-cxa-atexit ;
359	}
360
361	HAIKU_KERNEL_CCFLAGS = $(HAIKU_CCFLAGS_$(architecture)) $(ccBaseFlags) ;
362	HAIKU_KERNEL_C++FLAGS = $(HAIKU_C++FLAGS_$(architecture)) $(c++BaseFlags) ;
363	HAIKU_KERNEL_PIC_CCFLAGS = ;
364	HAIKU_KERNEL_PIC_LINKFLAGS = ;
365	HAIKU_KERNEL_ADDON_LINKFLAGS = ;
366
367	# Common boot-related cflags which apply to all loaders
368	HAIKU_BOOT_CCFLAGS = $(HAIKU_CCFLAGS_$(architecture)) $(ccBaseFlags) ;
369	HAIKU_BOOT_C++FLAGS = $(HAIKU_C++FLAGS_$(architecture)) $(c++BaseFlags) ;
370	HAIKU_BOOT_LINKFLAGS = ;
371	HAIKU_BOOT_LDFLAGS = -Bstatic ;
372
373	# Remove -fPIC and other unwanted options from the BOOT flags (they are sometimes
374	# added to force PIC in general.)
375	local fixedBootCCFlags ;
376	local fixedBootC++Flags ;
377	for flag in $(HAIKU_BOOT_CCFLAGS) {
378		if $(flag) = "-fpic" || $(flag) = "-fPIC" {
379			continue ;
380		}
381		fixedBootCCFlags += $(flag) ;
382	}
383	for flag in $(HAIKU_BOOT_C++FLAGS) {
384		if $(flag) = "-fpic" || $(flag) = "-fPIC" {
385			continue ;
386		}
387		fixedBootC++Flags += $(flag) ;
388	}
389	HAIKU_BOOT_CCFLAGS = $(fixedBootCCFlags) ;
390	HAIKU_BOOT_C++FLAGS = $(fixedBootC++Flags) ;
391
392	# Any special kernel base addresses
393	if $(HAIKU_BOOT_LOADER_BASE) {
394		HAIKU_BOOT_LDFLAGS +=
395			--defsym BOOT_LOADER_BASE=$(HAIKU_BOOT_LOADER_BASE) ;
396	}
397
398	switch $(cpu) {
399		case arm :
400			HAIKU_KERNEL_PIC_LINKFLAGS += -z max-page-size=0x1000 ;
401			HAIKU_KERNEL_ADDON_LINKFLAGS += -z max-page-size=0x1000 ;
402			HAIKU_KERNEL_PIC_CCFLAGS = -fno-pic ;
403
404		case arm64 :
405			HAIKU_KERNEL_PIC_LINKFLAGS += -z max-page-size=0x1000 ;
406			HAIKU_KERNEL_ADDON_LINKFLAGS += -z max-page-size=0x1000 ;
407			HAIKU_KERNEL_PIC_CCFLAGS = -fno-pic ;
408
409		case m68k :
410			# We don't want to have to handle emulating missing FPU opcodes for
411			# 040 and 060 in the kernel.
412			HAIKU_KERNEL_CCFLAGS += -m68020-60 ;
413			HAIKU_KERNEL_C++FLAGS += -m68020-60 ;
414
415		case ppc :
416			# Build a position independent PPC kernel. We need to be able to
417			# relocate the kernel, since the virtual address space layout at
418			# boot time is not fixed.
419			HAIKU_KERNEL_PIC_CCFLAGS = -fPIE ;
420			HAIKU_KERNEL_PIC_LINKFLAGS = -shared -fPIE ;
421
422		case riscv64 :
423			# Kernel lives within any single 2 GiB address space.
424			# Default is medlow (-2GiB / +2GiB)
425			HAIKU_KERNEL_CCFLAGS += -mcmodel=medany ;
426			HAIKU_KERNEL_C++FLAGS += -mcmodel=medany ;
427
428		case sparc :
429			# The medlow code model is enough (64-bit addresses, programs must
430			# be linked in the low 32 bits of memory. Programs can be
431			# statically or dynamically linked.)
432			HAIKU_KERNEL_CCFLAGS += -mcmodel=medlow ;
433			HAIKU_KERNEL_C++FLAGS += -mcmodel=medlow ;
434
435			# Unfortunately it's not easy to make the kernel be
436			# position-independant, on sparc, that requires relocation support
437			# in the ELF loader to fill in the plt section.
438			HAIKU_KERNEL_PIC_CCFLAGS = -fPIE ;
439			HAIKU_KERNEL_PIC_LINKFLAGS = -shared -fPIE ;
440
441		case x86 :
442			HAIKU_KERNEL_PIC_CCFLAGS = -fno-pic ;
443			HAIKU_KERNEL_CCFLAGS += -march=pentium ;
444			HAIKU_KERNEL_C++FLAGS += -march=pentium ;
445
446		case x86_64 :
447			# Kernel lives in the top 2GB of the address space, use kernel code
448			# model.
449			HAIKU_KERNEL_PIC_CCFLAGS = -fno-pic -mcmodel=kernel ;
450
451			# Disable the red zone, which cannot be used in kernel code due to
452			# interrupts, and always enable the frame pointer so stack traces
453			# are correct.
454			HAIKU_KERNEL_CCFLAGS += -mno-red-zone -fno-omit-frame-pointer ;
455			HAIKU_KERNEL_C++FLAGS += -mno-red-zone -fno-omit-frame-pointer ;
456			HAIKU_KERNEL_PIC_LINKFLAGS += -z max-page-size=0x1000 ;
457			HAIKU_KERNEL_ADDON_LINKFLAGS += -z max-page-size=0x1000 ;
458
459			if x86 in $(HAIKU_ARCHS[2-]) || x86_gcc2 in $(HAIKU_ARCHS[2-]) {
460				Echo "Enable kernel ia32 compatibility" ;
461				HAIKU_KERNEL_DEFINES += _COMPAT_MODE ;
462				HAIKU_KERNEL_COMPAT_MODE = 1 ;
463			}
464	}
465
466	# bootloader-centric flags
467	local bootTarget ;
468	for bootTarget in $(HAIKU_BOOT_TARGETS) {
469		switch $(bootTarget) {
470			case efi :
471				# efi bootloader is PIC
472				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fpic -fno-stack-protector
473					-fPIC -fshort-wchar -Wno-error=unused-variable -Wno-error=main ;
474				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fpic -fno-stack-protector
475					-fPIC -fshort-wchar -Wno-error=unused-variable -Wno-error=main ;
476				switch $(cpu) {
477					case x86 :
478						if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
479							HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -maccumulate-outgoing-args ;
480							HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -maccumulate-outgoing-args ;
481						}
482					case x86_64 :
483						HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -mno-red-zone ;
484						HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -mno-red-zone ;
485						if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
486							HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -maccumulate-outgoing-args ;
487							HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -maccumulate-outgoing-args ;
488						}
489				}
490				HAIKU_BOOT_$(bootTarget:U)_LDFLAGS = -Bstatic -Bsymbolic
491					-nostdlib -znocombreloc -no-undefined ;
492			case bios_ia32 :
493				# bios_ia32 is non-PIC
494				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fno-pic -march=pentium ;
495				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fno-pic -march=pentium ;
496				if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
497					HAIKU_BOOT_$(bootTarget:U)_LDFLAGS += -m elf_i386 ;
498				} else {
499					HAIKU_BOOT_$(bootTarget:U)_LDFLAGS += -m elf_i386_haiku ;
500				}
501				if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) != 1 {
502					HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -Wno-error=main -m32 ;
503					HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -Wno-error=main -m32 ;
504				}
505			case pxe_ia32 :
506				# pxe_ia32 is non-PIC
507				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fno-pic -march=pentium ;
508				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fno-pic -march=pentium ;
509				if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
510					HAIKU_BOOT_$(bootTarget:U)_LDFLAGS += -m elf_i386 ;
511				} else {
512					HAIKU_BOOT_$(bootTarget:U)_LDFLAGS += -m elf_i386_haiku ;
513				}
514				if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) != 1 {
515					HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -Wno-error=main -m32 ;
516					HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -Wno-error=main -m32 ;
517				}
518			case *_m68k :
519				# TODO: make sure all m68k bootloaders are non-PIC
520				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fno-pic -Wno-error=main ;
521				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fno-pic -Wno-error=main ;
522				switch $(cpu) {
523					case m68k :
524						# use only common instructions by default
525						HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -m68020-60 ;
526						HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -m68020-60 ;
527					# TODO: coldfire (FireBee)
528				}
529			case riscv :
530				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -mcmodel=medany -fno-omit-frame-pointer -fno-plt -fno-pic -fno-semantic-interposition ;
531				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -mcmodel=medany -fno-omit-frame-pointer -fno-plt -fno-pic -fno-semantic-interposition ;
532			case * :
533				# all other bootloaders are non-PIC
534				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fno-pic -Wno-error=main ;
535				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fno-pic -Wno-error=main ;
536		}
537	}
538
539	# warning flags
540	HAIKU_KERNEL_WARNING_CCFLAGS = $(HAIKU_WARNING_CCFLAGS_$(architecture)) ;
541	HAIKU_KERNEL_WARNING_C++FLAGS = $(HAIKU_WARNING_C++FLAGS_$(architecture)) ;
542
543	# debug flags
544	local level ;
545	for level in $(HAIKU_DEBUG_LEVELS) {
546		local flags = $(HAIKU_DEBUG_FLAGS) [ FDefines DEBUG=$(level) ] ;
547		HAIKU_KERNEL_DEBUG_$(level)_CCFLAGS
548			= $(HAIKU_DEBUG_$(level)_CCFLAGS_$(architecture)) ;
549		HAIKU_KERNEL_DEBUG_$(level)_C++FLAGS
550			= $(HAIKU_DEBUG_$(level)_C++FLAGS_$(architecture)) ;
551	}
552
553	# defines
554	HAIKU_KERNEL_DEFINES += _KERNEL_MODE ;
555
556	HAIKU_DEFINES_$(architecture)
557		+= BOOT_ARCHIVE_IMAGE_OFFSET=$(HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET) ;
558		# TODO: That doesn't need to be a general define. It's just needed for
559		# compiling (part of) the boot loader.
560
561	# kernel add-on glue code
562	HAIKU_KERNEL_ADDON_BEGIN_GLUE_CODE = <$(architecture)>crtbeginS.o
563		<src!system!glue!$(architecture)>haiku_version_glue.o ;
564	HAIKU_KERNEL_ADDON_END_GLUE_CODE = <$(architecture)>crtendS.o ;
565}
566
567
568rule ArchitectureSetupWarnings architecture
569{
570	# ArchitectureSetupWarnings <architecture> ;
571	#
572	# Sets up compiler warnings and error flags for various subdirectories for
573	# the given packaging architecture.
574
575	if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
576		AppendToConfigVar CCFLAGS :
577			HAIKU_TOP src system libroot posix glibc :
578			-fgnu89-inline -fheinous-gnu-extensions : global ;
579	}
580
581	local cpu = $(HAIKU_CPU_$(architecture)) ;
582	switch $(cpu) {
583		case arm :
584			return ;
585				# we use #warning as placeholders for things to write...
586		case m68k :
587			return ;
588				# we use #warning as placeholders for things to write...
589		case ppc :
590			return ;
591				# we use #warning as placeholders for things to write...
592	}
593
594	# enable -Werror for certain parts of the source tree
595	HAIKU_WERROR_ARCH = $(architecture) ;
596
597	rule EnableWerror dirTokens : scope {
598		# Clang gives way more warnings than GCC, so that code won't compile
599		# with -Werror when using Clang.
600		if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
601			SetConfigVar WARNINGS : HAIKU_TOP $(dirTokens) : treatAsErrors
602				: $(scope) ;
603		}
604	}
605
606	rule EnableStackProtector dirTokens : scope {
607		# enable stack protector, if requested
608		if $(HAIKU_USE_STACK_PROTECTOR) = 1 {
609			AppendToConfigVar CCFLAGS : HAIKU_TOP $(dirTokens) :
610				-fstack-protector : $(scope) ;
611			AppendToConfigVar C++FLAGS : HAIKU_TOP $(dirTokens) :
612				-fstack-protector : $(scope) ;
613		}
614	}
615
616	# Work-around for GCC 2 problem -- despite -Wno-multichar it reports
617	# multichar warnings in headers/private/kernel/debugger_keymaps.h included
618	# by src/system/kernel/arch/x86/arch_debug_console.cpp.
619	if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) = 1 {
620		local file = <src!system!kernel!arch!x86>arch_debug_console.o ;
621		WARNINGS on $(file) = $(WARNINGS) ;
622	}
623
624	EnableWerror src add-ons accelerants ;
625	EnableWerror src add-ons bluetooth ;
626	EnableWerror src add-ons decorators ;
627	EnableWerror src add-ons disk_systems ;
628	EnableWerror src add-ons input_server devices ;
629	EnableWerror src add-ons input_server filters ;
630#	EnableWerror src add-ons input_server methods pen ;
631	EnableWerror src add-ons input_server methods t9 ;
632	EnableWerror src add-ons kernel bluetooth ;
633	EnableWerror src add-ons kernel bus_managers acpi ;
634	EnableWerror src add-ons kernel bus_managers agp_gart ;
635	EnableWerror src add-ons kernel bus_managers ata ;
636	EnableWerror src add-ons kernel bus_managers config_manager ;
637#	EnableWerror src add-ons kernel bus_managers firewire ;
638	EnableWerror src add-ons kernel bus_managers ide ;
639	EnableWerror src add-ons kernel bus_managers isa ;
640	EnableWerror src add-ons kernel bus_managers pci ;
641	EnableWerror src add-ons kernel bus_managers ps2 ;
642	EnableWerror src add-ons kernel bus_managers random ;
643	EnableWerror src add-ons kernel bus_managers scsi ;
644	EnableWerror src add-ons kernel bus_managers tty ;
645	EnableWerror src add-ons kernel bus_managers usb ;
646	EnableWerror src add-ons kernel bus_managers virtio ;
647	EnableWerror src add-ons kernel busses agp_gart ;
648	EnableWerror src add-ons kernel busses ata ;
649	EnableWerror src add-ons kernel busses scsi ;
650	EnableWerror src add-ons kernel busses usb ;
651	EnableWerror src add-ons kernel console ;
652	EnableWerror src add-ons kernel cpu ;
653#	EnableWerror src add-ons kernel debugger ; # gcc2
654#	EnableWerror src add-ons kernel drivers audio ;
655	EnableWerror src add-ons kernel drivers bluetooth ;
656#	EnableWerror src add-ons kernel drivers bus ;
657	EnableWerror src add-ons kernel drivers common ;
658#	EnableWerror src add-ons kernel drivers disk ;
659	EnableWerror src add-ons kernel drivers dvb ;
660#	EnableWerror src add-ons kernel drivers graphics ;
661	EnableWerror src add-ons kernel drivers graphics intel_extreme ;
662#	EnableWerror src add-ons kernel drivers input ;
663	EnableWerror src add-ons kernel drivers joystick ;
664	EnableWerror src add-ons kernel drivers midi ;
665	EnableWerror src add-ons kernel drivers misc ;
666#	EnableWerror src add-ons kernel drivers network ;
667	EnableWerror src add-ons kernel drivers ports ;
668#	EnableWerror src add-ons kernel drivers power ;
669	EnableWerror src add-ons kernel drivers printer ;
670	EnableWerror src add-ons kernel drivers random ;
671	EnableWerror src add-ons kernel drivers tty ;
672	EnableWerror src add-ons kernel drivers video ;
673	EnableWerror src add-ons kernel file_systems bfs ;
674	EnableWerror src add-ons kernel file_systems cdda ;
675#	EnableWerror src add-ons kernel file_systems ext2 ;
676#	EnableWerror src add-ons kernel file_systems fat ;
677#	EnableWerror src add-ons kernel file_systems googlefs ;
678	EnableWerror src add-ons kernel file_systems iso9660 ;
679	EnableWerror src add-ons kernel file_systems layers ;
680#	EnableWerror src add-ons kernel file_systems netfs ;
681#	EnableWerror src add-ons kernel file_systems nfs ;
682	EnableWerror src add-ons kernel file_systems nfs4 ;
683#	EnableWerror src add-ons kernel file_systems ntfs ;
684	EnableWerror src add-ons kernel file_systems packagefs ;
685#	EnableWerror src add-ons kernel file_systems ramfs ;
686#	EnableWerror src add-ons kernel file_systems reiserfs ;
687	EnableWerror src add-ons kernel file_systems udf ;
688	EnableWerror src add-ons kernel file_systems userlandfs ;
689	EnableWerror src add-ons kernel generic ;
690#	EnableWerror src add-ons kernel network datalink_protocols ;
691	EnableWerror src add-ons kernel network devices ;
692	EnableWerror src add-ons kernel network dns_resolver ;
693	EnableWerror src add-ons kernel network notifications ;
694	EnableWerror src add-ons kernel network ppp ;
695	EnableWerror src add-ons kernel network protocols ;
696#	EnableWerror src add-ons kernel network stack ;
697	EnableWerror src add-ons kernel partitioning_systems ;
698	EnableWerror src add-ons kernel power ;
699	EnableWerror src add-ons locale ;
700	EnableWerror src add-ons mail_daemon ;
701	EnableWerror src add-ons media media-add-ons demultiplexer ;
702	EnableWerror src add-ons media media-add-ons dvb ;
703	EnableWerror src add-ons media media-add-ons esound_sink ;
704	EnableWerror src add-ons media media-add-ons finepix_webcam ;
705	EnableWerror src add-ons media media-add-ons firewire_dv ;
706	EnableWerror src add-ons media media-add-ons legacy ;
707	EnableWerror src add-ons media media-add-ons mixer ;
708	EnableWerror src add-ons media media-add-ons multi_audio ;
709	EnableWerror src add-ons media media-add-ons opensound ;
710	EnableWerror src add-ons media media-add-ons radeon ;
711	EnableWerror src add-ons media media-add-ons reader ;
712	EnableWerror src add-ons media media-add-ons tone_producer_demo ;
713	EnableWerror src add-ons media media-add-ons usb_vision ;
714#	EnableWerror src add-ons media media-add-ons usb_webcam ;
715	EnableWerror src add-ons media media-add-ons video_mixer ;
716#	EnableWerror src add-ons media media-add-ons video_producer_demo ;
717	EnableWerror src add-ons media media-add-ons videowindow ;
718	EnableWerror src add-ons media media-add-ons writer ;
719	EnableWerror src add-ons media plugins ape_reader ;
720	EnableWerror src add-ons media plugins au_reader ;
721#	EnableWerror src add-ons media plugins ffmpeg ;
722#	EnableWerror src add-ons media plugins raw_decoder ;
723	EnableWerror src add-ons print ;
724	EnableWerror src add-ons screen_savers ;
725	EnableWerror src add-ons tracker ;
726	EnableWerror src add-ons translators bmp ;
727	EnableWerror src add-ons translators exr ;
728	EnableWerror src add-ons translators gif ;
729	EnableWerror src add-ons translators hvif ;
730	EnableWerror src add-ons translators ico ;
731	EnableWerror src add-ons translators jpeg ;
732#	EnableWerror src add-ons translators jpeg2000 ;
733	EnableWerror src add-ons translators pcx ;
734	EnableWerror src add-ons translators png ;
735	EnableWerror src add-ons translators ppm ;
736	EnableWerror src add-ons translators raw ;
737	EnableWerror src add-ons translators rtf ;
738	EnableWerror src add-ons translators sgi ;
739	EnableWerror src add-ons translators shared ;
740	EnableWerror src add-ons translators stxt ;
741	EnableWerror src add-ons translators tga ;
742	EnableWerror src add-ons translators tiff ;
743	EnableWerror src add-ons translators wonderbrush ;
744	EnableWerror src add-ons print ;
745	EnableWerror src bin desklink ;
746	EnableWerror src bin multiuser ;
747	EnableWerror src bin package ;
748	EnableWerror src bin package_repo ;
749	EnableWerror src bin pkgman ;
750	EnableWerror src libs bsd ;
751	EnableWerror src apps ;
752	EnableWerror src kits ;
753	EnableWerror src preferences ;
754	EnableWerror src servers ;
755	EnableWerror src system boot ;
756	EnableWerror src system kernel ;
757	EnableWerror src system libroot add-ons ;
758	EnableWerror src system libroot os ;
759	EnableWerror src system libroot posix locale ;
760	EnableWerror src system libroot posix wchar ;
761	EnableWerror src system runtime_loader ;
762
763	EnableStackProtector src apps ;
764	EnableStackProtector src kits ;
765	EnableStackProtector src preferences ;
766	EnableStackProtector src servers ;
767	EnableStackProtector src system kernel ;
768}
769
770
771rule MultiArchIfPrimary ifValue : elseValue : architecture
772{
773	# MultiArchIfPrimary <ifValue> : <elseValue>
774	#	[ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
775	#
776	# Returns one of the two given values depending on whether
777	# <architecture> is the primary packaging architecture.
778
779	architecture ?= $(TARGET_PACKAGING_ARCH) ;
780
781	if $(architecture) = $(TARGET_PACKAGING_ARCHS[1]) {
782		return $(ifValue) ;
783	}
784	return $(elseValue) ;
785}
786
787
788rule MultiArchConditionalGristFiles files : primaryGrist : secondaryGrist
789	: architecture
790{
791	# MultiArchConditionalGristFiles <files> : <primaryGrist>
792	#	: <secondaryGrist> [ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
793	#
794	# Returns <files> with their grist set to either <primaryGrist> or
795	# <secondaryGrist> depending on whether <architecture> is the primary
796	# packaging architecture.
797
798	architecture ?= $(TARGET_PACKAGING_ARCH) ;
799
800	local grist = [ MultiArchIfPrimary $(primaryGrist) : $(secondaryGrist)
801		: $(architecture) ] ;
802	return $(files:G=$(grist:E=)) ;
803}
804
805
806rule MultiArchDefaultGristFiles files : gristPrefix : architecture
807{
808	# MultiArchDefaultGristFiles <files> : <gristPrefix>
809	#	[ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
810	#
811	# Convenient shorthand for MultiArchConditionalGristFiles for the common
812	# case that for a secondary packaging architecture the packaging
813	# architecture name shall be appended to the grist while it shall be omitted
814	# for the primary packaging architecture. IOW, if architecture is the
815	# primary packaging architecture, <files> are returned with their grist set
816	# to <gristPrefix>, otherwise <files> are returned with their grist set to
817	# <gristPrefix>!<architecture> respectively <architecture> (if <gristPrefix>
818	# is empty).
819
820	architecture ?= $(TARGET_PACKAGING_ARCH) ;
821
822	local secondaryGrist = $(gristPrefix)!$(architecture) ;
823	secondaryGrist ?= $(architecture) ;
824
825	return [ MultiArchConditionalGristFiles $(files) : $(gristPrefix) :
826		$(secondaryGrist) : $(architecture) ] ;
827}
828
829
830rule MultiArchSubDirSetup architectures
831{
832	# MultiArchSubDirSetup <architectures> ;
833	#
834	# For each of the given packaging architectures <architectures> that are
835	# in the packaging architectures configured for the build (or all configured
836	# packaging architectures, if <architectures> is empty) an object is
837	# prepared that can be used for an "on ... { ... }" block to set up subdir
838	# variables for the respective packaging architecture. Most notably
839	# TARGET_PACKAGING_ARCH, TARGET_ARCH are set to the values for the
840	# respective packaging architecture. The per-subdir variables SOURCE_GRIST,
841	# LOCATE_TARGET, LOCATE_SOURCE, SEARCH_SOURCE, *_LOCATE_TARGET, are reset.
842	# All SUBDIR* and config variables are set to the values they had when this
843	# rule was invoked.
844
845	local result ;
846	architectures ?= $(TARGET_PACKAGING_ARCHS) ;
847	local architecture ;
848	for architecture in $(architectures) {
849		if ! $(architecture) in $(TARGET_PACKAGING_ARCHS) {
850			continue ;
851		}
852
853		local architectureObject = $(architecture:G=<arch-object>) ;
854		result += $(architectureObject) ;
855
856		# Set the variables that default to the values of the respective
857		# variables for the primary architecture.
858		TARGET_PACKAGING_ARCH on $(architectureObject) = $(architecture) ;
859
860		local var ;
861		for var in TARGET_ARCH {
862			$(var) on $(architectureObject) = $($(var)_$(architecture)) ;
863		}
864
865		# Clone the current config variable values and the variables SubDir
866		# resets.
867		for var in $(AUTO_SET_UP_CONFIG_VARIABLES) SUBDIR$(SUBDIRRESET) {
868			$(var) on $(architectureObject) = $($(var)) ;
869		}
870
871		# adjust SOURCE_GRIST and HDRGRIST
872		SOURCE_GRIST on $(architectureObject)
873			= $(SOURCE_GRIST:E=)!$(architecture) ;
874
875		HDRGRIST on $(architectureObject)
876			= $(HDRGRIST:E=)!$(architecture) ;
877
878		# Adjust the subdir's object dirs that are architecture dependent. To
879		# avoid duplicating the code from SetupObjectsDir, we call it. Since it
880		# sets global variables, we set these variables on our object, call
881		# SetupObjectsDir in an "on" block, and grab the new variable values.
882		local hostTarget = HOST TARGET ;
883		local objectDirVars =
884			COMMON_ARCH COMMON_DEBUG DEBUG_$(HAIKU_DEBUG_LEVELS)
885			;
886		objectDirVars =
887			COMMON_PLATFORM_LOCATE_TARGET
888			$(hostTarget)_$(objectDirVars)_LOCATE_TARGET
889			LOCATE_TARGET
890			LOCATE_SOURCE
891			SEARCH_SOURCE
892			;
893
894		for var in $(objectDirVars) {
895			$(var) on $(architectureObject) = ;
896		}
897
898		on $(architectureObject) {
899			SetupObjectsDir ;
900
901			for var in $(objectDirVars) {
902				$(var) on $(architectureObject) = $($(var)) ;
903			}
904		}
905	}
906
907	return $(result) ;
908}
909