xref: /haiku/build/jam/ArchitectureRules (revision 3d4afef9cba2f328e238089d4609d00d4b1524f3)
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 = -fpic ;
403			HAIKU_KERNEL_PIC_LINKFLAGS = -shared ;
404
405		case arm64 :
406			HAIKU_KERNEL_PIC_LINKFLAGS += -z max-page-size=0x1000 ;
407			HAIKU_KERNEL_ADDON_LINKFLAGS += -z max-page-size=0x1000 ;
408			HAIKU_KERNEL_PIC_CCFLAGS = -fpic ;
409			HAIKU_KERNEL_PIC_LINKFLAGS = -shared ;
410
411		case m68k :
412			# We don't want to have to handle emulating missing FPU opcodes for
413			# 040 and 060 in the kernel.
414			HAIKU_KERNEL_CCFLAGS += -m68020-60 ;
415			HAIKU_KERNEL_C++FLAGS += -m68020-60 ;
416
417		case ppc :
418			# Build a position independent PPC kernel. We need to be able to
419			# relocate the kernel, since the virtual address space layout at
420			# boot time is not fixed.
421			HAIKU_KERNEL_PIC_CCFLAGS = -fPIE ;
422			HAIKU_KERNEL_PIC_LINKFLAGS = -shared -fPIE ;
423
424		case riscv64 :
425			# Kernel lives within any single 2 GiB address space.
426			# Default is medlow (-2GiB / +2GiB)
427			HAIKU_KERNEL_CCFLAGS += -mcmodel=medany -fpic ;
428			HAIKU_KERNEL_C++FLAGS += -mcmodel=medany -fpic ;
429			HAIKU_KERNEL_PIC_LINKFLAGS = -shared ;
430
431		case sparc :
432			# The medlow code model is enough (64-bit addresses, programs must
433			# be linked in the low 32 bits of memory. Programs can be
434			# statically or dynamically linked.)
435			HAIKU_KERNEL_CCFLAGS += -mcmodel=medlow ;
436			HAIKU_KERNEL_C++FLAGS += -mcmodel=medlow ;
437
438			# Unfortunately it's not easy to make the kernel be
439			# position-independant, on sparc, that requires relocation support
440			# in the ELF loader to fill in the plt section.
441			HAIKU_KERNEL_PIC_CCFLAGS = -fPIE ;
442			HAIKU_KERNEL_PIC_LINKFLAGS = -shared -fPIE ;
443
444		case x86 :
445			HAIKU_KERNEL_PIC_CCFLAGS = -fno-pic ;
446			HAIKU_KERNEL_CCFLAGS += -march=pentium ;
447			HAIKU_KERNEL_C++FLAGS += -march=pentium ;
448
449		case x86_64 :
450			# Kernel lives in the top 2GB of the address space, use kernel code
451			# model.
452			HAIKU_KERNEL_PIC_CCFLAGS = -fno-pic -mcmodel=kernel ;
453
454			# Disable the red zone, which cannot be used in kernel code due to
455			# interrupts, and always enable the frame pointer so stack traces
456			# are correct.
457			HAIKU_KERNEL_CCFLAGS += -mno-red-zone -fno-omit-frame-pointer ;
458			HAIKU_KERNEL_C++FLAGS += -mno-red-zone -fno-omit-frame-pointer ;
459			HAIKU_KERNEL_PIC_LINKFLAGS += -z max-page-size=0x1000 ;
460			HAIKU_KERNEL_ADDON_LINKFLAGS += -z max-page-size=0x1000 ;
461
462			if x86 in $(HAIKU_ARCHS[2-]) || x86_gcc2 in $(HAIKU_ARCHS[2-]) {
463				Echo "Enable kernel ia32 compatibility" ;
464				HAIKU_KERNEL_DEFINES += _COMPAT_MODE ;
465				HAIKU_KERNEL_COMPAT_MODE = 1 ;
466			}
467	}
468
469	# bootloader-centric flags
470	local bootTarget ;
471	for bootTarget in $(HAIKU_BOOT_TARGETS) {
472		switch $(bootTarget) {
473			case efi :
474				# efi bootloader is PIC
475				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fpic -fno-stack-protector
476					-fPIC -fshort-wchar -Wno-error=unused-variable -Wno-error=main ;
477				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fpic -fno-stack-protector
478					-fPIC -fshort-wchar -Wno-error=unused-variable -Wno-error=main ;
479				switch $(cpu) {
480					case x86 :
481						if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
482							HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -maccumulate-outgoing-args ;
483							HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -maccumulate-outgoing-args ;
484						}
485					case x86_64 :
486						HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -mno-red-zone ;
487						HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -mno-red-zone ;
488						if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
489							HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -maccumulate-outgoing-args ;
490							HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -maccumulate-outgoing-args ;
491						}
492				}
493				HAIKU_BOOT_$(bootTarget:U)_LDFLAGS = -Bstatic -Bsymbolic
494					-nostdlib -znocombreloc -no-undefined ;
495			case bios_ia32 :
496				# bios_ia32 is non-PIC
497				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fno-pic -march=pentium ;
498				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fno-pic -march=pentium ;
499				if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
500					HAIKU_BOOT_$(bootTarget:U)_LDFLAGS += -m elf_i386 ;
501				} else {
502					HAIKU_BOOT_$(bootTarget:U)_LDFLAGS += -m elf_i386_haiku ;
503				}
504				if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) != 1 {
505					HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -Wno-error=main -m32 ;
506					HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -Wno-error=main -m32 ;
507				}
508			case pxe_ia32 :
509				# pxe_ia32 is non-PIC
510				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fno-pic -march=pentium ;
511				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fno-pic -march=pentium ;
512				if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
513					HAIKU_BOOT_$(bootTarget:U)_LDFLAGS += -m elf_i386 ;
514				} else {
515					HAIKU_BOOT_$(bootTarget:U)_LDFLAGS += -m elf_i386_haiku ;
516				}
517				if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) != 1 {
518					HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -Wno-error=main -m32 ;
519					HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -Wno-error=main -m32 ;
520				}
521			case *_m68k :
522				# TODO: make sure all m68k bootloaders are non-PIC
523				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fno-pic -Wno-error=main ;
524				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fno-pic -Wno-error=main ;
525				switch $(cpu) {
526					case m68k :
527						# use only common instructions by default
528						HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -m68020-60 ;
529						HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -m68020-60 ;
530					# TODO: coldfire (FireBee)
531				}
532			case riscv :
533				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -mcmodel=medany -fno-omit-frame-pointer -fno-plt -fno-pic -fno-semantic-interposition ;
534				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -mcmodel=medany -fno-omit-frame-pointer -fno-plt -fno-pic -fno-semantic-interposition ;
535			case * :
536				# all other bootloaders are non-PIC
537				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fno-pic -Wno-error=main ;
538				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fno-pic -Wno-error=main ;
539		}
540	}
541
542	# warning flags
543	HAIKU_KERNEL_WARNING_CCFLAGS = $(HAIKU_WARNING_CCFLAGS_$(architecture)) ;
544	HAIKU_KERNEL_WARNING_C++FLAGS = $(HAIKU_WARNING_C++FLAGS_$(architecture)) ;
545
546	# debug flags
547	local level ;
548	for level in $(HAIKU_DEBUG_LEVELS) {
549		local flags = $(HAIKU_DEBUG_FLAGS) [ FDefines DEBUG=$(level) ] ;
550		HAIKU_KERNEL_DEBUG_$(level)_CCFLAGS
551			= $(HAIKU_DEBUG_$(level)_CCFLAGS_$(architecture)) ;
552		HAIKU_KERNEL_DEBUG_$(level)_C++FLAGS
553			= $(HAIKU_DEBUG_$(level)_C++FLAGS_$(architecture)) ;
554	}
555
556	# defines
557	HAIKU_KERNEL_DEFINES += _KERNEL_MODE ;
558
559	HAIKU_DEFINES_$(architecture)
560		+= BOOT_ARCHIVE_IMAGE_OFFSET=$(HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET) ;
561		# TODO: That doesn't need to be a general define. It's just needed for
562		# compiling (part of) the boot loader.
563
564	# kernel add-on glue code
565	HAIKU_KERNEL_ADDON_BEGIN_GLUE_CODE = <$(architecture)>crtbeginS.o
566		<src!system!glue!$(architecture)>haiku_version_glue.o ;
567	HAIKU_KERNEL_ADDON_END_GLUE_CODE = <$(architecture)>crtendS.o ;
568}
569
570
571rule ArchitectureSetupWarnings architecture
572{
573	# ArchitectureSetupWarnings <architecture> ;
574	#
575	# Sets up compiler warnings and error flags for various subdirectories for
576	# the given packaging architecture.
577
578	if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
579		AppendToConfigVar CCFLAGS :
580			HAIKU_TOP src system libroot posix glibc :
581			-fgnu89-inline -fheinous-gnu-extensions : global ;
582	}
583
584	local cpu = $(HAIKU_CPU_$(architecture)) ;
585	switch $(cpu) {
586		case arm :
587			return ;
588				# we use #warning as placeholders for things to write...
589		case m68k :
590			return ;
591				# we use #warning as placeholders for things to write...
592		case ppc :
593			return ;
594				# we use #warning as placeholders for things to write...
595	}
596
597	# enable -Werror for certain parts of the source tree
598	HAIKU_WERROR_ARCH = $(architecture) ;
599
600	rule EnableWerror dirTokens : scope {
601		# Clang gives way more warnings than GCC, so that code won't compile
602		# with -Werror when using Clang.
603		if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
604			SetConfigVar WARNINGS : HAIKU_TOP $(dirTokens) : treatAsErrors
605				: $(scope) ;
606		}
607	}
608
609	rule EnableStackProtector dirTokens : scope {
610		# enable stack protector, if requested
611		if $(HAIKU_USE_STACK_PROTECTOR) = 1 {
612			AppendToConfigVar CCFLAGS : HAIKU_TOP $(dirTokens) :
613				-fstack-protector : $(scope) ;
614			AppendToConfigVar C++FLAGS : HAIKU_TOP $(dirTokens) :
615				-fstack-protector : $(scope) ;
616		}
617	}
618
619	# Work-around for GCC 2 problem -- despite -Wno-multichar it reports
620	# multichar warnings in headers/private/kernel/debugger_keymaps.h included
621	# by src/system/kernel/arch/x86/arch_debug_console.cpp.
622	if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) = 1 {
623		local file = <src!system!kernel!arch!x86>arch_debug_console.o ;
624		WARNINGS on $(file) = $(WARNINGS) ;
625	}
626
627	EnableWerror src add-ons accelerants ;
628	EnableWerror src add-ons bluetooth ;
629	EnableWerror src add-ons decorators ;
630	EnableWerror src add-ons disk_systems ;
631	EnableWerror src add-ons input_server devices ;
632	EnableWerror src add-ons input_server filters ;
633#	EnableWerror src add-ons input_server methods pen ;
634	EnableWerror src add-ons input_server methods t9 ;
635	EnableWerror src add-ons kernel bluetooth ;
636	EnableWerror src add-ons kernel bus_managers acpi ;
637	EnableWerror src add-ons kernel bus_managers agp_gart ;
638	EnableWerror src add-ons kernel bus_managers ata ;
639	EnableWerror src add-ons kernel bus_managers config_manager ;
640#	EnableWerror src add-ons kernel bus_managers firewire ;
641	EnableWerror src add-ons kernel bus_managers ide ;
642	EnableWerror src add-ons kernel bus_managers isa ;
643	EnableWerror src add-ons kernel bus_managers pci ;
644	EnableWerror src add-ons kernel bus_managers ps2 ;
645	EnableWerror src add-ons kernel bus_managers random ;
646	EnableWerror src add-ons kernel bus_managers scsi ;
647	EnableWerror src add-ons kernel bus_managers tty ;
648	EnableWerror src add-ons kernel bus_managers usb ;
649	EnableWerror src add-ons kernel bus_managers virtio ;
650	EnableWerror src add-ons kernel busses agp_gart ;
651	EnableWerror src add-ons kernel busses ata ;
652	EnableWerror src add-ons kernel busses scsi ;
653	EnableWerror src add-ons kernel busses usb ;
654	EnableWerror src add-ons kernel console ;
655	EnableWerror src add-ons kernel cpu ;
656#	EnableWerror src add-ons kernel debugger ; # gcc2
657#	EnableWerror src add-ons kernel drivers audio ;
658	EnableWerror src add-ons kernel drivers bluetooth ;
659#	EnableWerror src add-ons kernel drivers bus ;
660	EnableWerror src add-ons kernel drivers common ;
661#	EnableWerror src add-ons kernel drivers disk ;
662	EnableWerror src add-ons kernel drivers dvb ;
663#	EnableWerror src add-ons kernel drivers graphics ;
664	EnableWerror src add-ons kernel drivers graphics intel_extreme ;
665#	EnableWerror src add-ons kernel drivers input ;
666	EnableWerror src add-ons kernel drivers joystick ;
667	EnableWerror src add-ons kernel drivers midi ;
668	EnableWerror src add-ons kernel drivers misc ;
669#	EnableWerror src add-ons kernel drivers network ;
670	EnableWerror src add-ons kernel drivers ports ;
671#	EnableWerror src add-ons kernel drivers power ;
672	EnableWerror src add-ons kernel drivers printer ;
673	EnableWerror src add-ons kernel drivers random ;
674	EnableWerror src add-ons kernel drivers tty ;
675	EnableWerror src add-ons kernel drivers video ;
676	EnableWerror src add-ons kernel file_systems bfs ;
677	EnableWerror src add-ons kernel file_systems cdda ;
678#	EnableWerror src add-ons kernel file_systems ext2 ;
679#	EnableWerror src add-ons kernel file_systems fat ;
680#	EnableWerror src add-ons kernel file_systems googlefs ;
681	EnableWerror src add-ons kernel file_systems iso9660 ;
682	EnableWerror src add-ons kernel file_systems layers ;
683#	EnableWerror src add-ons kernel file_systems netfs ;
684#	EnableWerror src add-ons kernel file_systems nfs ;
685	EnableWerror src add-ons kernel file_systems nfs4 ;
686#	EnableWerror src add-ons kernel file_systems ntfs ;
687	EnableWerror src add-ons kernel file_systems packagefs ;
688#	EnableWerror src add-ons kernel file_systems ramfs ;
689#	EnableWerror src add-ons kernel file_systems reiserfs ;
690	EnableWerror src add-ons kernel file_systems udf ;
691	EnableWerror src add-ons kernel file_systems userlandfs ;
692	EnableWerror src add-ons kernel generic ;
693#	EnableWerror src add-ons kernel network datalink_protocols ;
694	EnableWerror src add-ons kernel network devices ;
695	EnableWerror src add-ons kernel network dns_resolver ;
696	EnableWerror src add-ons kernel network notifications ;
697	EnableWerror src add-ons kernel network ppp ;
698	EnableWerror src add-ons kernel network protocols ;
699#	EnableWerror src add-ons kernel network stack ;
700	EnableWerror src add-ons kernel partitioning_systems ;
701	EnableWerror src add-ons kernel power ;
702	EnableWerror src add-ons locale ;
703	EnableWerror src add-ons mail_daemon ;
704	EnableWerror src add-ons media media-add-ons demultiplexer ;
705	EnableWerror src add-ons media media-add-ons dvb ;
706	EnableWerror src add-ons media media-add-ons esound_sink ;
707	EnableWerror src add-ons media media-add-ons finepix_webcam ;
708	EnableWerror src add-ons media media-add-ons firewire_dv ;
709	EnableWerror src add-ons media media-add-ons legacy ;
710	EnableWerror src add-ons media media-add-ons mixer ;
711	EnableWerror src add-ons media media-add-ons multi_audio ;
712	EnableWerror src add-ons media media-add-ons opensound ;
713	EnableWerror src add-ons media media-add-ons radeon ;
714	EnableWerror src add-ons media media-add-ons reader ;
715	EnableWerror src add-ons media media-add-ons tone_producer_demo ;
716	EnableWerror src add-ons media media-add-ons usb_vision ;
717#	EnableWerror src add-ons media media-add-ons usb_webcam ;
718	EnableWerror src add-ons media media-add-ons video_mixer ;
719#	EnableWerror src add-ons media media-add-ons video_producer_demo ;
720	EnableWerror src add-ons media media-add-ons videowindow ;
721	EnableWerror src add-ons media media-add-ons writer ;
722	EnableWerror src add-ons media plugins ape_reader ;
723	EnableWerror src add-ons media plugins au_reader ;
724#	EnableWerror src add-ons media plugins ffmpeg ;
725#	EnableWerror src add-ons media plugins raw_decoder ;
726	EnableWerror src add-ons print ;
727	EnableWerror src add-ons screen_savers ;
728	EnableWerror src add-ons tracker ;
729	EnableWerror src add-ons translators bmp ;
730	EnableWerror src add-ons translators exr ;
731	EnableWerror src add-ons translators gif ;
732	EnableWerror src add-ons translators hvif ;
733	EnableWerror src add-ons translators ico ;
734	EnableWerror src add-ons translators jpeg ;
735#	EnableWerror src add-ons translators jpeg2000 ;
736	EnableWerror src add-ons translators pcx ;
737	EnableWerror src add-ons translators png ;
738	EnableWerror src add-ons translators ppm ;
739	EnableWerror src add-ons translators raw ;
740	EnableWerror src add-ons translators rtf ;
741	EnableWerror src add-ons translators sgi ;
742	EnableWerror src add-ons translators shared ;
743	EnableWerror src add-ons translators stxt ;
744	EnableWerror src add-ons translators tga ;
745	EnableWerror src add-ons translators tiff ;
746	EnableWerror src add-ons translators wonderbrush ;
747	EnableWerror src add-ons print ;
748	EnableWerror src bin desklink ;
749	EnableWerror src bin multiuser ;
750	EnableWerror src bin package ;
751	EnableWerror src bin package_repo ;
752	EnableWerror src bin pkgman ;
753	EnableWerror src libs bsd ;
754	EnableWerror src apps ;
755	EnableWerror src kits ;
756	EnableWerror src preferences ;
757	EnableWerror src servers ;
758	EnableWerror src system boot ;
759	EnableWerror src system kernel ;
760	EnableWerror src system libroot add-ons ;
761	EnableWerror src system libroot os ;
762	EnableWerror src system libroot posix locale ;
763	EnableWerror src system libroot posix wchar ;
764	EnableWerror src system runtime_loader ;
765
766	EnableStackProtector src apps ;
767	EnableStackProtector src kits ;
768	EnableStackProtector src preferences ;
769	EnableStackProtector src servers ;
770	EnableStackProtector src system kernel ;
771}
772
773
774rule MultiArchIfPrimary ifValue : elseValue : architecture
775{
776	# MultiArchIfPrimary <ifValue> : <elseValue>
777	#	[ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
778	#
779	# Returns one of the two given values depending on whether
780	# <architecture> is the primary packaging architecture.
781
782	architecture ?= $(TARGET_PACKAGING_ARCH) ;
783
784	if $(architecture) = $(TARGET_PACKAGING_ARCHS[1]) {
785		return $(ifValue) ;
786	}
787	return $(elseValue) ;
788}
789
790
791rule MultiArchConditionalGristFiles files : primaryGrist : secondaryGrist
792	: architecture
793{
794	# MultiArchConditionalGristFiles <files> : <primaryGrist>
795	#	: <secondaryGrist> [ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
796	#
797	# Returns <files> with their grist set to either <primaryGrist> or
798	# <secondaryGrist> depending on whether <architecture> is the primary
799	# packaging architecture.
800
801	architecture ?= $(TARGET_PACKAGING_ARCH) ;
802
803	local grist = [ MultiArchIfPrimary $(primaryGrist) : $(secondaryGrist)
804		: $(architecture) ] ;
805	return $(files:G=$(grist:E=)) ;
806}
807
808
809rule MultiArchDefaultGristFiles files : gristPrefix : architecture
810{
811	# MultiArchDefaultGristFiles <files> : <gristPrefix>
812	#	[ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
813	#
814	# Convenient shorthand for MultiArchConditionalGristFiles for the common
815	# case that for a secondary packaging architecture the packaging
816	# architecture name shall be appended to the grist while it shall be omitted
817	# for the primary packaging architecture. IOW, if architecture is the
818	# primary packaging architecture, <files> are returned with their grist set
819	# to <gristPrefix>, otherwise <files> are returned with their grist set to
820	# <gristPrefix>!<architecture> respectively <architecture> (if <gristPrefix>
821	# is empty).
822
823	architecture ?= $(TARGET_PACKAGING_ARCH) ;
824
825	local secondaryGrist = $(gristPrefix)!$(architecture) ;
826	secondaryGrist ?= $(architecture) ;
827
828	return [ MultiArchConditionalGristFiles $(files) : $(gristPrefix) :
829		$(secondaryGrist) : $(architecture) ] ;
830}
831
832
833rule MultiArchSubDirSetup architectures
834{
835	# MultiArchSubDirSetup <architectures> ;
836	#
837	# For each of the given packaging architectures <architectures> that are
838	# in the packaging architectures configured for the build (or all configured
839	# packaging architectures, if <architectures> is empty) an object is
840	# prepared that can be used for an "on ... { ... }" block to set up subdir
841	# variables for the respective packaging architecture. Most notably
842	# TARGET_PACKAGING_ARCH, TARGET_ARCH are set to the values for the
843	# respective packaging architecture. The per-subdir variables SOURCE_GRIST,
844	# LOCATE_TARGET, LOCATE_SOURCE, SEARCH_SOURCE, *_LOCATE_TARGET, are reset.
845	# All SUBDIR* and config variables are set to the values they had when this
846	# rule was invoked.
847
848	local result ;
849	architectures ?= $(TARGET_PACKAGING_ARCHS) ;
850	local architecture ;
851	for architecture in $(architectures) {
852		if ! $(architecture) in $(TARGET_PACKAGING_ARCHS) {
853			continue ;
854		}
855
856		local architectureObject = $(architecture:G=<arch-object>) ;
857		result += $(architectureObject) ;
858
859		# Set the variables that default to the values of the respective
860		# variables for the primary architecture.
861		TARGET_PACKAGING_ARCH on $(architectureObject) = $(architecture) ;
862
863		local var ;
864		for var in TARGET_ARCH {
865			$(var) on $(architectureObject) = $($(var)_$(architecture)) ;
866		}
867
868		# Clone the current config variable values and the variables SubDir
869		# resets.
870		for var in $(AUTO_SET_UP_CONFIG_VARIABLES) SUBDIR$(SUBDIRRESET) {
871			$(var) on $(architectureObject) = $($(var)) ;
872		}
873
874		# adjust SOURCE_GRIST and HDRGRIST
875		SOURCE_GRIST on $(architectureObject)
876			= $(SOURCE_GRIST:E=)!$(architecture) ;
877
878		HDRGRIST on $(architectureObject)
879			= $(HDRGRIST:E=)!$(architecture) ;
880
881		# Adjust the subdir's object dirs that are architecture dependent. To
882		# avoid duplicating the code from SetupObjectsDir, we call it. Since it
883		# sets global variables, we set these variables on our object, call
884		# SetupObjectsDir in an "on" block, and grab the new variable values.
885		local hostTarget = HOST TARGET ;
886		local objectDirVars =
887			COMMON_ARCH COMMON_DEBUG DEBUG_$(HAIKU_DEBUG_LEVELS)
888			;
889		objectDirVars =
890			COMMON_PLATFORM_LOCATE_TARGET
891			$(hostTarget)_$(objectDirVars)_LOCATE_TARGET
892			LOCATE_TARGET
893			LOCATE_SOURCE
894			SEARCH_SOURCE
895			;
896
897		for var in $(objectDirVars) {
898			$(var) on $(architectureObject) = ;
899		}
900
901		on $(architectureObject) {
902			SetupObjectsDir ;
903
904			for var in $(objectDirVars) {
905				$(var) on $(architectureObject) = $($(var)) ;
906			}
907		}
908	}
909
910	return $(result) ;
911}
912