xref: /haiku/build/jam/ArchitectureRules (revision 9a6a20d4689307142a7ed26a1437ba47e244e73f)
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. checking for NULL in strdup, or
25		# for `this` in member functions), which breaks the kernel and various
26		# applications. (Linux does the same.)
27		ccBaseFlags += -fno-delete-null-pointer-checks ;
28
29		if $(architecture) = x86 {
30			# disable some builtins that are incompatible with our definitions
31			ccBaseFlags += -fno-builtin-fork -fno-builtin-vfork ;
32		}
33	}
34
35	# default architecture tuning
36	local cpu = $(HAIKU_CPU_$(architecture)) ;
37	local archFlags ;
38	switch $(cpu) {
39		case ppc : archFlags += -mcpu=440fp ;
40		case arm : archFlags += -march=armv7-a -mfloat-abi=hard ;
41		case arm64 : archFlags += -march=armv8.2-a+fp16 ;
42		case x86 : archFlags += -march=pentium ;
43		case riscv64 : archFlags += -march=rv64gc ;
44	}
45	if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
46		# lld doesn't currently implement R_RISCV_ALIGN relaxation
47		if $(cpu) = riscv64 {
48			ccBaseFlags += -mno-relax ;
49		}
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) +=
112			-Wno-unused-private-field -Wno-gnu-designator
113			-Wno-builtin-requires-header ;
114		HAIKU_WARNING_C++FLAGS_$(architecture) +=
115			-Wno-unused-private-field -Wno-gnu-designator
116			-Wno-builtin-requires-header
117			-Wno-non-c-typedef-for-linkage -Wno-non-power-of-two-alignment ;
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=cpp -Wno-error=register ;
126		# These currently generate too many "false positives."
127		HAIKU_WERROR_FLAGS_$(architecture) += -Wno-error=address-of-packed-member
128			-Wno-error=stringop-overread -Wno-error=array-bounds ;
129		# But these can stay.
130		HAIKU_WERROR_FLAGS_$(architecture) += -Wno-error=cast-align
131			-Wno-error=format-truncation ;
132	} else {
133		HAIKU_WERROR_FLAGS_$(architecture) += -Wno-unknown-pragmas ;
134	}
135
136	# debug flags
137	local debugFlags = -ggdb ;
138
139	# debug 0: suppress asserts
140	HAIKU_DEBUG_0_CCFLAGS_$(architecture) = [ FDefines NDEBUG=$(NDEBUG) ] ;
141	HAIKU_DEBUG_0_C++FLAGS_$(architecture) = [ FDefines NDEBUG=$(NDEBUG) ] ;
142
143	local level ;
144	for level in $(HAIKU_DEBUG_LEVELS[2-]) {
145		local flags = $(debugFlags) [ FDefines DEBUG=$(level) ] ;
146		HAIKU_DEBUG_$(level)_CCFLAGS_$(architecture) = $(flags) ;
147		HAIKU_DEBUG_$(level)_C++FLAGS_$(architecture) = $(flags) ;
148	}
149
150	# TODO: Temporary work-around. Should be defined in the compiler specs
151	HAIKU_LINKFLAGS_$(architecture) += -Xlinker --no-undefined ;
152
153	# private shared kernel/libroot headers
154	HAIKU_PRIVATE_SYSTEM_HEADERS_$(architecture)
155		= [ PrivateHeaders $(DOT) system system/arch/$(cpu) ] ;
156
157	# library and executable glue code
158	local commonGlueCode =
159		<src!system!glue!$(architecture)>init_term_dyn.o
160		<src!system!glue!arch!$(cpu)!$(architecture)>crti.o
161		<src!system!glue!arch!$(cpu)!$(architecture)>crtn.o
162		;
163	HAIKU_LIBRARY_BEGIN_GLUE_CODE_$(architecture) =
164		<src!system!glue!arch!$(cpu)!$(architecture)>crti.o
165		<$(architecture)>crtbeginS.o
166		<src!system!glue!$(architecture)>init_term_dyn.o
167		;
168	HAIKU_LIBRARY_END_GLUE_CODE_$(architecture) =
169		<$(architecture)>crtendS.o
170		<src!system!glue!arch!$(cpu)!$(architecture)>crtn.o
171		;
172	HAIKU_EXECUTABLE_BEGIN_GLUE_CODE_$(architecture) =
173		<src!system!glue!arch!$(cpu)!$(architecture)>crti.o
174		<$(architecture)>crtbeginS.o
175		<src!system!glue!$(architecture)>start_dyn.o
176		<src!system!glue!$(architecture)>init_term_dyn.o
177		;
178	HAIKU_EXECUTABLE_END_GLUE_CODE_$(architecture)
179		= $(HAIKU_LIBRARY_END_GLUE_CODE_$(architecture)) ;
180
181	SEARCH on <$(architecture)>crtbeginS.o <$(architecture)>crtendS.o
182		= $(HAIKU_GCC_LIB_DIR_$(architecture)) ;
183
184	# init library name map
185	local libraryGrist = "" ;
186	if $(architecture) != $(HAIKU_PACKAGING_ARCHS[1]) {
187		libraryGrist = $(architecture) ;
188	}
189	local i ;
190	for i in be bnetapi debug device game locale mail media midi midi2
191			network package root screensaver textencoding tracker
192			translation z {
193		local library = lib$(i).so ;
194		HAIKU_LIBRARY_NAME_MAP_$(architecture)_$(i)
195			= $(library:G=$(libraryGrist)) ;
196	}
197	HAIKU_LIBRARY_NAME_MAP_$(architecture)_localestub
198		= <$(architecture)>liblocalestub.a ;
199	HAIKU_LIBRARY_NAME_MAP_$(architecture)_shared
200		= <$(architecture)>libshared.a ;
201	if $(architecture) = $(HAIKU_PACKAGING_ARCHS[1]) {
202		HAIKU_LIBRARY_NAME_MAP_$(architecture)_input_server
203			= <nogrist>input_server ;
204	} else {
205		HAIKU_LIBRARY_NAME_MAP_$(architecture)_input_server
206			= <$(architecture)>input_server ;
207	}
208}
209
210
211rule KernelArchitectureSetup architecture
212{
213	# KernelArchitectureSetup <architecture> ;
214	#
215	# Initializes the global kernel and boot loader related variables. Those
216	# don't have a packaging architecture suffix, since they are only set for
217	# the primary packaging architecture. <architecture> is the primary
218	# packaging architecture (supplied for convenience).
219
220	HAIKU_KERNEL_ARCH = $(HAIKU_ARCH) ;
221	HAIKU_KERNEL_ARCH_DIR = $(HAIKU_KERNEL_ARCH) ;
222
223	local cpu = $(HAIKU_CPU_$(architecture)) ;
224
225	switch $(cpu) {
226		case ppc :
227			HAIKU_KERNEL_PLATFORM ?= openfirmware ;
228			HAIKU_BOOT_TARGETS += openfirmware ;
229
230			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ; # in kB
231			# offset in floppy image (>= sizeof(haiku_loader))
232			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 384 ; # in kB
233
234		case sparc :
235			HAIKU_KERNEL_PLATFORM ?= openfirmware ;
236			HAIKU_BOOT_TARGETS += openfirmware ;
237
238			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ; # in kB
239			# offset in floppy image (>= sizeof(haiku_loader))
240			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 384 ; # in kB
241
242		case arm :
243			HAIKU_KERNEL_PLATFORM ?= efi ;
244			HAIKU_BOOT_TARGETS += efi ;
245
246			# SOC's like allwinner need an offset to skip the hardcoded initial loader
247			HAIKU_BOOT_SDIMAGE_BEGIN = 20475 ; # in KiB
248
249			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ;
250			# offset in floppy image (>= sizeof(haiku_loader))
251			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 192 ; # in kB - unused yet
252			HAIKU_BOOT_LOADER_BASE ?= 0x1000000 ;
253
254		case arm64 :
255			HAIKU_KERNEL_PLATFORM ?= efi ;
256			HAIKU_BOOT_TARGETS += efi ;
257
258			HAIKU_BOOT_SDIMAGE_BEGIN = 2 ; # in KiB
259
260			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ;
261			# offset in floppy image (>= sizeof(haiku_loader))
262			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 192 ; # in kB - unused yet
263			HAIKU_BOOT_LOADER_BASE ?= 0x1000000 ;
264
265		case x86 :
266			HAIKU_KERNEL_PLATFORM ?= bios_ia32 ;
267			HAIKU_BOOT_TARGETS += bios_ia32 efi pxe_ia32 ;
268
269			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 2880 ; # in kB
270			# offset in floppy image (>= sizeof(haiku_loader))
271			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 384 ; # in kB
272
273			# nasm is required for target arch x86
274			if ! $(HAIKU_NASM) {
275				Exit "HAIKU_NASM not set. Please re-run configure." ;
276			}
277
278		case riscv64 :
279			HAIKU_KERNEL_PLATFORM ?= efi ;
280			HAIKU_BOOT_TARGETS += efi riscv ;
281
282			HAIKU_BOOT_SDIMAGE_BEGIN = 2 ; # KiB
283
284			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ;
285			# offset in floppy image (>= sizeof(haiku_loader))
286			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 192 ; # in kB - unused yet
287			HAIKU_BOOT_LOADER_BASE ?= 0x1000000 ;
288
289		case x86_64 :
290			# x86_64 completely shares the x86 bootloader for MBR.
291			HAIKU_KERNEL_PLATFORM ?= bios_ia32 ;
292			HAIKU_BOOT_TARGETS += bios_ia32 efi pxe_ia32 ;
293
294			HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 2880 ; # in kB
295			# offset in floppy image (>= sizeof(haiku_loader))
296			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 384 ; # in kB
297
298			# x86_64 kernel source is under arch/x86.
299			HAIKU_KERNEL_ARCH_DIR = x86 ;
300
301			# nasm is required for target arch x86_64
302			if ! $(HAIKU_NASM) {
303				Exit "HAIKU_NASM not set. Please re-run configure." ;
304			}
305
306		case m68k :
307			HAIKU_KERNEL_PLATFORM ?= atari_m68k ;
308			HAIKU_BOOT_TARGETS += amiga_m68k atari_m68k next_m68k ;
309			switch $(HAIKU_KERNEL_PLATFORM) {
310				case atari_m68k :
311				{
312					HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ; # in kB
313				}
314				case amiga_m68k :
315				{
316					# for now we have trouble reading from double-sided images
317					HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 880 ; # in kB
318				}
319				case next_m68k :
320				{
321					HAIKU_BOOT_FLOPPY_IMAGE_SIZE = 1440 ; # in kB
322				}
323			}
324			# offset in floppy image (>= sizeof(haiku_loader))
325			HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET = 260 ; # in kB
326			HAIKU_CONTAINER_STRIP_EXECUTABLES on
327				$(HAIKU_FLOPPY_BOOT_IMAGE_CONTAINER_NAME) = 1 ;
328
329		case * :
330			Exit "Currently unsupported target CPU:" $(cpu) ;
331	}
332
333	# private kernel headers to be used when compiling kernel code
334	HAIKU_PRIVATE_KERNEL_HEADERS =
335		[ PrivateHeaders $(DOT) kernel libroot shared
336			kernel/boot/platform/$(HAIKU_KERNEL_PLATFORM) ]
337		[ ArchHeaders $(HAIKU_KERNEL_ARCH_DIR) ]
338		[ FDirName $(HAIKU_COMMON_DEBUG_OBJECT_DIR_$(architecture)) system
339			kernel ]
340		$(HAIKU_PRIVATE_SYSTEM_HEADERS_$(architecture))
341		;
342
343	# C/C++ flags
344	local ccBaseFlags = -ffreestanding -finline -fno-semantic-interposition ;
345
346	# Since GCC 13, autovectorization generates code which causes problems
347	# in various virtual machines (bare metal is apparently unaffected.)
348	# Until this can be resolved, disable for the kernel. (See #18593.)
349	ccBaseFlags += -fno-tree-vectorize ;
350
351	local c++BaseFlags = $(ccBaseFlags) -fno-exceptions ;
352
353	if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
354		c++BaseFlags += -fno-use-cxa-atexit ;
355	}
356
357	HAIKU_KERNEL_CCFLAGS = $(HAIKU_CCFLAGS_$(architecture)) $(ccBaseFlags) ;
358	HAIKU_KERNEL_C++FLAGS = $(HAIKU_C++FLAGS_$(architecture)) $(c++BaseFlags) ;
359	HAIKU_KERNEL_PIC_CCFLAGS = ;
360	HAIKU_KERNEL_PIC_LINKFLAGS = ;
361	HAIKU_KERNEL_ADDON_LINKFLAGS = ;
362
363	if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
364		HAIKU_KERNEL_PIC_LINKFLAGS += -z noseparate-code -z norelro --no-rosegment ;
365		HAIKU_KERNEL_ADDON_LINKFLAGS += -z noseparate-code -z norelro -Wl,--no-rosegment ;
366	}
367
368	# Common boot-related flags which apply to all loaders.
369	HAIKU_BOOT_CCFLAGS = $(HAIKU_CCFLAGS_$(architecture)) $(ccBaseFlags) ;
370	HAIKU_BOOT_C++FLAGS = $(HAIKU_C++FLAGS_$(architecture)) $(c++BaseFlags) ;
371	HAIKU_BOOT_OPTIM = -Os ;
372	HAIKU_BOOT_LINKFLAGS = ;
373	HAIKU_BOOT_LDFLAGS = -Bstatic ;
374
375	# Any special kernel base addresses
376	if $(HAIKU_BOOT_LOADER_BASE) {
377		HAIKU_BOOT_LDFLAGS +=
378			--defsym BOOT_LOADER_BASE=$(HAIKU_BOOT_LOADER_BASE) ;
379	}
380
381	switch $(cpu) {
382		case arm :
383			HAIKU_KERNEL_PIC_LINKFLAGS += -z max-page-size=0x1000 ;
384			HAIKU_KERNEL_ADDON_LINKFLAGS += -z max-page-size=0x1000 ;
385			HAIKU_KERNEL_PIC_CCFLAGS = -fpic ;
386			HAIKU_KERNEL_PIC_LINKFLAGS = -shared ;
387
388		case arm64 :
389			HAIKU_KERNEL_PIC_LINKFLAGS += -z max-page-size=0x1000 ;
390			HAIKU_KERNEL_ADDON_LINKFLAGS += -z max-page-size=0x1000 ;
391			HAIKU_KERNEL_PIC_CCFLAGS = -fpic ;
392			HAIKU_KERNEL_PIC_LINKFLAGS = -shared ;
393
394		case m68k :
395			# We don't want to have to handle emulating missing FPU opcodes for
396			# 040 and 060 in the kernel.
397			HAIKU_KERNEL_CCFLAGS += -m68020-60 ;
398			HAIKU_KERNEL_C++FLAGS += -m68020-60 ;
399
400		case ppc :
401			# Build a position independent PPC kernel. We need to be able to
402			# relocate the kernel, since the virtual address space layout at
403			# boot time is not fixed.
404			HAIKU_KERNEL_PIC_CCFLAGS = -fPIE ;
405			HAIKU_KERNEL_PIC_LINKFLAGS = -shared -fPIE ;
406
407		case riscv64 :
408			# Kernel lives within any single 2 GiB address space.
409			# Default is medlow (-2GiB / +2GiB)
410			HAIKU_KERNEL_CCFLAGS += -mcmodel=medany -fpic ;
411			HAIKU_KERNEL_C++FLAGS += -mcmodel=medany -fpic ;
412			HAIKU_KERNEL_PIC_LINKFLAGS = -shared ;
413
414		case sparc :
415			# The medlow code model is enough (64-bit addresses, programs must
416			# be linked in the low 32 bits of memory. Programs can be
417			# statically or dynamically linked.)
418			HAIKU_KERNEL_CCFLAGS += -mcmodel=medlow ;
419			HAIKU_KERNEL_C++FLAGS += -mcmodel=medlow ;
420
421			# Unfortunately it's not easy to make the kernel be
422			# position-independant, on sparc, that requires relocation support
423			# in the ELF loader to fill in the plt section.
424			HAIKU_KERNEL_PIC_CCFLAGS = -fPIE ;
425			HAIKU_KERNEL_PIC_LINKFLAGS = -shared -fPIE ;
426
427		case x86 :
428			HAIKU_KERNEL_PIC_CCFLAGS = -fno-pic ;
429			HAIKU_KERNEL_CCFLAGS += -march=pentium ;
430			HAIKU_KERNEL_C++FLAGS += -march=pentium ;
431
432		case x86_64 :
433			# Kernel lives in the top 2GB of the address space, use kernel code
434			# model.
435			HAIKU_KERNEL_PIC_CCFLAGS = -fno-pic -mcmodel=kernel ;
436
437			# Disable the red zone, which cannot be used in kernel code due to
438			# interrupts, and always enable the frame pointer so stack traces
439			# are correct.
440			HAIKU_KERNEL_CCFLAGS += -mno-red-zone -fno-omit-frame-pointer ;
441			HAIKU_KERNEL_C++FLAGS += -mno-red-zone -fno-omit-frame-pointer ;
442			HAIKU_KERNEL_PIC_LINKFLAGS += -z max-page-size=0x1000 ;
443			HAIKU_KERNEL_ADDON_LINKFLAGS += -z max-page-size=0x1000 ;
444
445			if x86 in $(HAIKU_ARCHS[2-]) || x86_gcc2 in $(HAIKU_ARCHS[2-]) {
446				Echo "Enable kernel ia32 compatibility" ;
447				HAIKU_KERNEL_DEFINES += _COMPAT_MODE ;
448				HAIKU_KERNEL_COMPAT_MODE = 1 ;
449			}
450	}
451
452	# bootloader-centric flags
453	HAIKU_BOOT_CCFLAGS
454		+= -DBOOT_ARCHIVE_IMAGE_OFFSET=$(HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET) ;
455	HAIKU_BOOT_C++FLAGS
456		+= -DBOOT_ARCHIVE_IMAGE_OFFSET=$(HAIKU_BOOT_ARCHIVE_IMAGE_OFFSET) ;
457
458	local bootTarget ;
459	for bootTarget in $(HAIKU_BOOT_TARGETS) {
460		switch $(bootTarget) {
461			case efi :
462				# efi bootloader is PIC
463				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fpic -fno-stack-protector
464					-fPIC -fshort-wchar -Wno-error=unused-variable -Wno-error=main ;
465				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fpic -fno-stack-protector
466					-fPIC -fshort-wchar -Wno-error=unused-variable -Wno-error=main ;
467				switch $(cpu) {
468					case x86 :
469						if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
470							HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -maccumulate-outgoing-args ;
471							HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -maccumulate-outgoing-args ;
472						}
473					case x86_64 :
474						HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -mno-red-zone ;
475						HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -mno-red-zone ;
476						if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
477							HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -maccumulate-outgoing-args ;
478							HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -maccumulate-outgoing-args ;
479						}
480					case arm :
481						HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -mfloat-abi=soft ;
482						HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -mfloat-abi=soft ;
483
484						# Remove any previous -mfloat-abi=hard setting from compiler flags
485						local fixedBootCCFlags ;
486						local fixedBootC++Flags ;
487						for flag in $(HAIKU_BOOT_CCFLAGS) {
488							if $(flag) = "-mfloat-abi=hard" {
489								continue ;
490							}
491							fixedBootCCFlags += $(flag) ;
492						}
493						for flag in $(HAIKU_BOOT_C++FLAGS) {
494							if $(flag) = "-mfloat-abi=hard" {
495								continue ;
496							}
497							fixedBootC++Flags += $(flag) ;
498						}
499						HAIKU_BOOT_CCFLAGS = $(fixedBootCCFlags) ;
500						HAIKU_BOOT_C++FLAGS = $(fixedBootC++Flags) ;
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	HAIKU_BOOT_DEFINES += _BOOT_MODE ;
571
572	# kernel add-on glue code
573	HAIKU_KERNEL_ADDON_BEGIN_GLUE_CODE = <$(architecture)>crtbeginS.o
574		<src!system!glue!$(architecture)>haiku_version_glue.o ;
575	HAIKU_KERNEL_ADDON_END_GLUE_CODE = <$(architecture)>crtendS.o ;
576}
577
578
579rule ArchitectureSetupWarnings architecture
580{
581	# ArchitectureSetupWarnings <architecture> ;
582	#
583	# Sets up compiler warnings and error flags for various subdirectories for
584	# the given packaging architecture.
585
586	if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
587		AppendToConfigVar CCFLAGS :
588			HAIKU_TOP src system libroot posix glibc :
589			-fgnu89-inline -fheinous-gnu-extensions : global ;
590	}
591
592	local cpu = $(HAIKU_CPU_$(architecture)) ;
593	switch $(cpu) {
594		case arm :
595			return ;
596				# we use #warning as placeholders for things to write...
597		case m68k :
598			return ;
599				# we use #warning as placeholders for things to write...
600		case ppc :
601			return ;
602				# we use #warning as placeholders for things to write...
603	}
604
605	# enable -Werror for certain parts of the source tree
606	HAIKU_WERROR_ARCH = $(architecture) ;
607
608	rule EnableWerror dirTokens : scope {
609		# Clang gives way more warnings than GCC, so that code won't compile
610		# with -Werror when using Clang.
611		if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
612			SetConfigVar WARNINGS : HAIKU_TOP $(dirTokens) : treatAsErrors
613				: $(scope) ;
614		}
615	}
616
617	rule EnableStackProtector dirTokens : scope {
618		# enable stack protector, if requested
619		if $(HAIKU_USE_STACK_PROTECTOR) = 1 {
620			AppendToConfigVar CCFLAGS : HAIKU_TOP $(dirTokens) :
621				-fstack-protector : $(scope) ;
622			AppendToConfigVar C++FLAGS : HAIKU_TOP $(dirTokens) :
623				-fstack-protector : $(scope) ;
624		}
625	}
626
627	# Work-around for GCC 2 problem -- despite -Wno-multichar it reports
628	# multichar warnings in headers/private/kernel/debugger_keymaps.h included
629	# by src/system/kernel/arch/x86/arch_debug_console.cpp.
630	if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) = 1 {
631		local file = <src!system!kernel!arch!x86>arch_debug_console.o ;
632		WARNINGS on $(file) = $(WARNINGS) ;
633	}
634
635	# Enable -Werror for as much of Haiku as possible. Ideally, all of the code would compile
636	# without errors, but that is difficult to achieve due to using multiple compiler versions,
637	# compiling for multiple platforms, and keeping 3rd party code in sync with its upstream
638	# provenance.
639	# This list allows to track where -Werror is enabled. The idea is to enable it as globally
640	# as possible (for example: all of the accelerants), and otherwise list all subdirectories,
641	# commenting out the ones that can't currently be enabled. This allows to easily identify
642	# if a directory has all its subdirectories already compiled with -Werror, and the rule can
643	# be "collapsed" to declare just the parent directory.
644	# Notable places where 3rd party code is used, and fixing problems should be done in
645	# collaboration with upstream:
646	# - The NTFS filesystem driver (upstream: ntfs-3g)
647	# - All network drivers imported from FreeBSD or OpenBSD
648
649	EnableWerror src add-ons accelerants ;
650	EnableWerror src add-ons bluetooth ;
651	EnableWerror src add-ons decorators ;
652	EnableWerror src add-ons disk_systems ;
653	EnableWerror src add-ons input_server ;
654	EnableWerror src add-ons kernel bluetooth ;
655	EnableWerror src add-ons kernel bus_managers ;
656	EnableWerror src add-ons kernel busses ;
657	EnableWerror src add-ons kernel console ;
658	EnableWerror src add-ons kernel cpu ;
659	EnableWerror src add-ons kernel debugger ;
660	EnableWerror src add-ons kernel drivers audio ;
661	EnableWerror src add-ons kernel drivers bluetooth ;
662	EnableWerror src add-ons kernel drivers bus ;
663	EnableWerror src add-ons kernel drivers common ;
664	EnableWerror src add-ons kernel drivers disk ;
665#	EnableWerror src add-ons kernel drivers display ;
666	EnableWerror src add-ons kernel drivers dvb ;
667#	EnableWerror src add-ons kernel drivers graphics ;
668	EnableWerror src add-ons kernel drivers graphics intel_extreme ;
669	EnableWerror src add-ons kernel drivers input ;
670	EnableWerror src add-ons kernel drivers joystick ;
671	EnableWerror src add-ons kernel drivers midi ;
672	EnableWerror src add-ons kernel drivers misc ;
673	EnableWerror src add-ons kernel drivers network ether 3com ;
674#	EnableWerror src add-ons kernel drivers network ether atheros813x ;
675#	EnableWerror src add-ons kernel drivers network ether atheros81xx ;
676#	EnableWerror src add-ons kernel drivers network ether attansic_l1 ;
677#	EnableWerror src add-ons kernel drivers network ether attansic_l2 ;
678#	EnableWerror src add-ons kernel drivers network ether broadcom440x ;
679#	EnableWerror src add-ons kernel drivers network ether broadcom570x ;
680#	EnableWerror src add-ons kernel drivers network ether dec21xxx ;
681	EnableWerror src add-ons kernel drivers network ether etherpci ;
682#	EnableWerror src add-ons kernel drivers network ether intel22x ;
683#	EnableWerror src add-ons kernel drivers network ether ipro100 ;
684#	EnableWerror src add-ons kernel drivers network ether ipro1000 ;
685#	EnableWerror src add-ons kernel drivers network ether jmicron2x0 ;
686#	EnableWerror src add-ons kernel drivers network ether marvell_yukon ;
687#	EnableWerror src add-ons kernel drivers network ether nforce ;
688#	EnableWerror src add-ons kernel drivers network ether pcnet ;
689	EnableWerror src add-ons kernel drivers network ether pegasus ;
690	EnableWerror src add-ons kernel drivers network ether rdc ;
691#	EnableWerror src add-ons kernel drivers network ether rtl8139 ;
692#	EnableWerror src add-ons kernel drivers network ether rtl81xx ;
693	EnableWerror src add-ons kernel drivers network ether sis19x ;
694#	EnableWerror src add-ons kernel drivers network ether sis900 ;
695#	EnableWerror src add-ons kernel drivers network ether syskonnect ;
696	EnableWerror src add-ons kernel drivers network ether usb_asix ;
697	EnableWerror src add-ons kernel drivers network ether usb_davicom ;
698	EnableWerror src add-ons kernel drivers network ether usb_ecm ;
699	EnableWerror src add-ons kernel drivers network ether usb_rndis ;
700	EnableWerror src add-ons kernel drivers network ether via_rhine ;
701	EnableWerror src add-ons kernel drivers network ether virtio ;
702#	EnableWerror src add-ons kernel drivers network ether vt612x ;
703	EnableWerror src add-ons kernel drivers network ether wb840 ;
704#	EnableWerror src add-ons kernel drivers network wlan ;
705	EnableWerror src add-ons kernel drivers network wwan ;
706	EnableWerror src add-ons kernel drivers ports ;
707	EnableWerror src add-ons kernel drivers power ;
708#	EnableWerror src add-ons kernel drivers pty ;
709#	EnableWerror src add-ons kernel drivers sensor ;
710#	EnableWerror src add-ons kernel drivers timer ;
711	EnableWerror src add-ons kernel drivers video ;
712#	EnableWerror src add-ons kernel drivers wmi ;
713	EnableWerror src add-ons kernel file_systems bfs ;
714	EnableWerror src add-ons kernel file_systems cdda ;
715	EnableWerror src add-ons kernel file_systems ext2 ;
716	EnableWerror src add-ons kernel file_systems fat ;
717	EnableWerror src add-ons kernel file_systems googlefs ;
718	EnableWerror src add-ons kernel file_systems iso9660 ;
719	EnableWerror src add-ons kernel file_systems layers ;
720#	EnableWerror src add-ons kernel file_systems netfs ;
721	EnableWerror src add-ons kernel file_systems nfs ;
722	EnableWerror src add-ons kernel file_systems nfs4 ;
723#	EnableWerror src add-ons kernel file_systems ntfs ;
724	EnableWerror src add-ons kernel file_systems packagefs ;
725	EnableWerror src add-ons kernel file_systems ramfs ;
726	EnableWerror src add-ons kernel file_systems reiserfs ;
727	EnableWerror src add-ons kernel file_systems udf ;
728	EnableWerror src add-ons kernel file_systems userlandfs ;
729	EnableWerror src add-ons kernel file_systems xfs ;
730	EnableWerror src add-ons kernel generic ;
731	EnableWerror src add-ons kernel network ;
732	EnableWerror src add-ons kernel partitioning_systems ;
733	EnableWerror src add-ons kernel power ;
734	EnableWerror src add-ons locale ;
735	EnableWerror src add-ons mail_daemon ;
736	EnableWerror src add-ons media media-add-ons ;
737	EnableWerror src add-ons media plugins ape_reader ;
738	EnableWerror src add-ons media plugins au_reader ;
739	EnableWerror src add-ons media plugins ffmpeg ;
740	EnableWerror src add-ons media plugins raw_decoder ;
741	EnableWerror src add-ons print ;
742	EnableWerror src add-ons screen_savers ;
743	EnableWerror src add-ons tracker ;
744	EnableWerror src add-ons translators bmp ;
745	EnableWerror src add-ons translators gif ;
746	EnableWerror src add-ons translators hvif ;
747	EnableWerror src add-ons translators ico ;
748	EnableWerror src add-ons translators jpeg ;
749#	EnableWerror src add-ons translators jpeg2000 ;
750	EnableWerror src add-ons translators pcx ;
751	EnableWerror src add-ons translators png ;
752	EnableWerror src add-ons translators ppm ;
753	EnableWerror src add-ons translators raw ;
754	EnableWerror src add-ons translators rtf ;
755	EnableWerror src add-ons translators sgi ;
756	EnableWerror src add-ons translators shared ;
757	EnableWerror src add-ons translators stxt ;
758	EnableWerror src add-ons translators tga ;
759	EnableWerror src add-ons translators tiff ;
760	EnableWerror src add-ons translators wonderbrush ;
761	EnableWerror src add-ons print ;
762	EnableWerror src bin debug strace ;
763	EnableWerror src bin desklink ;
764	EnableWerror src bin listusb ;
765	EnableWerror src bin multiuser ;
766	EnableWerror src bin package ;
767	EnableWerror src bin package_repo ;
768	EnableWerror src bin pkgman ;
769	EnableWerror src bin writembr ;
770	EnableWerror src libs bsd ;
771	EnableWerror src apps ;
772	EnableWerror src kits ;
773	EnableWerror src preferences ;
774	EnableWerror src servers ;
775	EnableWerror src system boot ;
776	EnableWerror src system kernel ;
777	EnableWerror src system libroot add-ons ;
778	EnableWerror src system libroot os ;
779	EnableWerror src system libroot posix locale ;
780	EnableWerror src system libroot posix wchar ;
781	EnableWerror src system runtime_loader ;
782
783	EnableStackProtector src apps ;
784	EnableStackProtector src kits ;
785	EnableStackProtector src preferences ;
786	EnableStackProtector src servers ;
787	EnableStackProtector src system kernel ;
788}
789
790
791rule MultiArchIfPrimary ifValue : elseValue : architecture
792{
793	# MultiArchIfPrimary <ifValue> : <elseValue>
794	#	[ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
795	#
796	# Returns one of the two given values depending on whether
797	# <architecture> is the primary packaging architecture.
798
799	architecture ?= $(TARGET_PACKAGING_ARCH) ;
800
801	if $(architecture) = $(TARGET_PACKAGING_ARCHS[1]) {
802		return $(ifValue) ;
803	}
804	return $(elseValue) ;
805}
806
807
808rule MultiArchConditionalGristFiles files : primaryGrist : secondaryGrist
809	: architecture
810{
811	# MultiArchConditionalGristFiles <files> : <primaryGrist>
812	#	: <secondaryGrist> [ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
813	#
814	# Returns <files> with their grist set to either <primaryGrist> or
815	# <secondaryGrist> depending on whether <architecture> is the primary
816	# packaging architecture.
817
818	architecture ?= $(TARGET_PACKAGING_ARCH) ;
819
820	local grist = [ MultiArchIfPrimary $(primaryGrist) : $(secondaryGrist)
821		: $(architecture) ] ;
822	return $(files:G=$(grist:E=)) ;
823}
824
825
826rule MultiArchDefaultGristFiles files : gristPrefix : architecture
827{
828	# MultiArchDefaultGristFiles <files> : <gristPrefix>
829	#	[ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
830	#
831	# Convenient shorthand for MultiArchConditionalGristFiles for the common
832	# case that for a secondary packaging architecture the packaging
833	# architecture name shall be appended to the grist while it shall be omitted
834	# for the primary packaging architecture. IOW, if architecture is the
835	# primary packaging architecture, <files> are returned with their grist set
836	# to <gristPrefix>, otherwise <files> are returned with their grist set to
837	# <gristPrefix>!<architecture> respectively <architecture> (if <gristPrefix>
838	# is empty).
839
840	architecture ?= $(TARGET_PACKAGING_ARCH) ;
841
842	local secondaryGrist = $(gristPrefix)!$(architecture) ;
843	secondaryGrist ?= $(architecture) ;
844
845	return [ MultiArchConditionalGristFiles $(files) : $(gristPrefix) :
846		$(secondaryGrist) : $(architecture) ] ;
847}
848
849
850rule MultiArchSubDirSetup architectures
851{
852	# MultiArchSubDirSetup <architectures> ;
853	#
854	# For each of the given packaging architectures <architectures> that are
855	# in the packaging architectures configured for the build (or all configured
856	# packaging architectures, if <architectures> is empty) an object is
857	# prepared that can be used for an "on ... { ... }" block to set up subdir
858	# variables for the respective packaging architecture. Most notably
859	# TARGET_PACKAGING_ARCH, TARGET_ARCH are set to the values for the
860	# respective packaging architecture. The per-subdir variables SOURCE_GRIST,
861	# LOCATE_TARGET, LOCATE_SOURCE, SEARCH_SOURCE, *_LOCATE_TARGET, are reset.
862	# All SUBDIR* and config variables are set to the values they had when this
863	# rule was invoked.
864
865	local result ;
866	architectures ?= $(TARGET_PACKAGING_ARCHS) ;
867	local architecture ;
868	for architecture in $(architectures) {
869		if ! $(architecture) in $(TARGET_PACKAGING_ARCHS) {
870			continue ;
871		}
872
873		local architectureObject = $(architecture:G=<arch-object>) ;
874		result += $(architectureObject) ;
875
876		# Set the variables that default to the values of the respective
877		# variables for the primary architecture.
878		TARGET_PACKAGING_ARCH on $(architectureObject) = $(architecture) ;
879
880		local var ;
881		for var in TARGET_ARCH {
882			$(var) on $(architectureObject) = $($(var)_$(architecture)) ;
883		}
884
885		# Clone the current config variable values and the variables SubDir
886		# resets.
887		for var in $(AUTO_SET_UP_CONFIG_VARIABLES) SUBDIR$(SUBDIRRESET) {
888			$(var) on $(architectureObject) = $($(var)) ;
889		}
890
891		# adjust SOURCE_GRIST and HDRGRIST
892		SOURCE_GRIST on $(architectureObject)
893			= $(SOURCE_GRIST:E=)!$(architecture) ;
894
895		HDRGRIST on $(architectureObject)
896			= $(HDRGRIST:E=)!$(architecture) ;
897
898		# Adjust the subdir's object dirs that are architecture dependent. To
899		# avoid duplicating the code from SetupObjectsDir, we call it. Since it
900		# sets global variables, we set these variables on our object, call
901		# SetupObjectsDir in an "on" block, and grab the new variable values.
902		local hostTarget = HOST TARGET ;
903		local objectDirVars =
904			COMMON_ARCH COMMON_DEBUG DEBUG_$(HAIKU_DEBUG_LEVELS)
905			;
906		objectDirVars =
907			COMMON_PLATFORM_LOCATE_TARGET
908			$(hostTarget)_$(objectDirVars)_LOCATE_TARGET
909			LOCATE_TARGET
910			LOCATE_SOURCE
911			SEARCH_SOURCE
912			;
913
914		for var in $(objectDirVars) {
915			$(var) on $(architectureObject) = ;
916		}
917
918		on $(architectureObject) {
919			SetupObjectsDir ;
920
921			for var in $(objectDirVars) {
922				$(var) on $(architectureObject) = $($(var)) ;
923			}
924		}
925	}
926
927	return $(result) ;
928}
929