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