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