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