xref: /haiku/build/jam/ArchitectureRules (revision 610f99c838cb661ff85377789ffd3ad4ff672a08)
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 = 320 ; # 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 = 320 ; # 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 * :
542				# all other bootloaders are non-PIC
543				HAIKU_BOOT_$(bootTarget:U)_CCFLAGS += -fno-pic -Wno-error=main ;
544				HAIKU_BOOT_$(bootTarget:U)_C++FLAGS += -fno-pic -Wno-error=main ;
545		}
546	}
547
548	# warning flags
549	HAIKU_KERNEL_WARNING_CCFLAGS = $(HAIKU_WARNING_CCFLAGS_$(architecture)) ;
550	HAIKU_KERNEL_WARNING_C++FLAGS = $(HAIKU_WARNING_C++FLAGS_$(architecture)) ;
551
552	# debug flags
553	local level ;
554	for level in $(HAIKU_DEBUG_LEVELS) {
555		local flags = $(HAIKU_DEBUG_FLAGS) [ FDefines DEBUG=$(level) ] ;
556		HAIKU_KERNEL_DEBUG_$(level)_CCFLAGS
557			= $(HAIKU_DEBUG_$(level)_CCFLAGS_$(architecture)) ;
558		HAIKU_KERNEL_DEBUG_$(level)_C++FLAGS
559			= $(HAIKU_DEBUG_$(level)_C++FLAGS_$(architecture)) ;
560	}
561
562	# defines
563	HAIKU_KERNEL_DEFINES += _KERNEL_MODE ;
564
565	# kernel add-on glue code
566	HAIKU_KERNEL_ADDON_BEGIN_GLUE_CODE = <$(architecture)>crtbeginS.o
567		<src!system!glue!$(architecture)>haiku_version_glue.o ;
568	HAIKU_KERNEL_ADDON_END_GLUE_CODE = <$(architecture)>crtendS.o ;
569}
570
571
572rule ArchitectureSetupWarnings architecture
573{
574	# ArchitectureSetupWarnings <architecture> ;
575	#
576	# Sets up compiler warnings and error flags for various subdirectories for
577	# the given packaging architecture.
578
579	if $(HAIKU_CC_IS_CLANG_$(architecture)) = 1 {
580		AppendToConfigVar CCFLAGS :
581			HAIKU_TOP src system libroot posix glibc :
582			-fgnu89-inline -fheinous-gnu-extensions : global ;
583	}
584
585	local cpu = $(HAIKU_CPU_$(architecture)) ;
586	switch $(cpu) {
587		case arm :
588			return ;
589				# we use #warning as placeholders for things to write...
590		case m68k :
591			return ;
592				# we use #warning as placeholders for things to write...
593		case ppc :
594			return ;
595				# we use #warning as placeholders for things to write...
596	}
597
598	# enable -Werror for certain parts of the source tree
599	HAIKU_WERROR_ARCH = $(architecture) ;
600
601	rule EnableWerror dirTokens : scope {
602		# Clang gives way more warnings than GCC, so that code won't compile
603		# with -Werror when using Clang.
604		if $(HAIKU_CC_IS_CLANG_$(architecture)) != 1 {
605			SetConfigVar WARNINGS : HAIKU_TOP $(dirTokens) : treatAsErrors
606				: $(scope) ;
607		}
608	}
609
610	rule EnableStackProtector dirTokens : scope {
611		# enable stack protector, if requested
612		if $(HAIKU_USE_STACK_PROTECTOR) = 1 {
613			AppendToConfigVar CCFLAGS : HAIKU_TOP $(dirTokens) :
614				-fstack-protector : $(scope) ;
615			AppendToConfigVar C++FLAGS : HAIKU_TOP $(dirTokens) :
616				-fstack-protector : $(scope) ;
617		}
618	}
619
620	# Work-around for GCC 2 problem -- despite -Wno-multichar it reports
621	# multichar warnings in headers/private/kernel/debugger_keymaps.h included
622	# by src/system/kernel/arch/x86/arch_debug_console.cpp.
623	if $(HAIKU_CC_IS_LEGACY_GCC_$(architecture)) = 1 {
624		local file = <src!system!kernel!arch!x86>arch_debug_console.o ;
625		WARNINGS on $(file) = $(WARNINGS) ;
626	}
627
628	EnableWerror src add-ons accelerants ;
629	EnableWerror src add-ons bluetooth ;
630	EnableWerror src add-ons decorators ;
631	EnableWerror src add-ons disk_systems ;
632	EnableWerror src add-ons input_server devices ;
633	EnableWerror src add-ons input_server filters ;
634	EnableWerror src add-ons input_server methods pen ;
635	EnableWerror src add-ons input_server methods t9 ;
636	EnableWerror src add-ons kernel bluetooth ;
637	EnableWerror src add-ons kernel bus_managers acpi ;
638	EnableWerror src add-ons kernel bus_managers agp_gart ;
639	EnableWerror src add-ons kernel bus_managers ata ;
640	EnableWerror src add-ons kernel bus_managers config_manager ;
641	EnableWerror src add-ons kernel bus_managers firewire ;
642	EnableWerror src add-ons kernel bus_managers ide ;
643	EnableWerror src add-ons kernel bus_managers isa ;
644	EnableWerror src add-ons kernel bus_managers pci ;
645	EnableWerror src add-ons kernel bus_managers ps2 ;
646	EnableWerror src add-ons kernel bus_managers random ;
647	EnableWerror src add-ons kernel bus_managers scsi ;
648	EnableWerror src add-ons kernel bus_managers tty ;
649	EnableWerror src add-ons kernel bus_managers usb ;
650	EnableWerror src add-ons kernel bus_managers virtio ;
651	EnableWerror src add-ons kernel busses agp_gart ;
652	EnableWerror src add-ons kernel busses ata ;
653#	EnableWerror src add-ons kernel busses i2c ;
654#	EnableWerror src add-ons kernel busses mmc ;
655#	EnableWerror src add-ons kernel busses random ;
656	EnableWerror src add-ons kernel busses scsi ;
657	EnableWerror src add-ons kernel busses usb ;
658#	EnableWerror src add-ons kernel busses virtio ;
659	EnableWerror src add-ons kernel console ;
660	EnableWerror src add-ons kernel cpu ;
661	EnableWerror src add-ons kernel debugger ;
662#	EnableWerror src add-ons kernel drivers audio ;
663	EnableWerror src add-ons kernel drivers bluetooth ;
664	EnableWerror src add-ons kernel drivers bus ;
665	EnableWerror src add-ons kernel drivers common ;
666#	EnableWerror src add-ons kernel drivers disk ;
667	EnableWerror src add-ons kernel drivers dvb ;
668#	EnableWerror src add-ons kernel drivers graphics ;
669	EnableWerror src add-ons kernel drivers graphics intel_extreme ;
670#	EnableWerror src add-ons kernel drivers input ;
671	EnableWerror src add-ons kernel drivers joystick ;
672	EnableWerror src add-ons kernel drivers midi ;
673	EnableWerror src add-ons kernel drivers misc ;
674#	EnableWerror src add-ons kernel drivers network ;
675	EnableWerror src add-ons kernel drivers ports ;
676	EnableWerror src add-ons kernel drivers power ;
677	EnableWerror src add-ons kernel drivers printer ;
678	EnableWerror src add-ons kernel drivers random ;
679	EnableWerror src add-ons kernel drivers tty ;
680	EnableWerror src add-ons kernel drivers video ;
681	EnableWerror src add-ons kernel file_systems bfs ;
682	EnableWerror src add-ons kernel file_systems cdda ;
683	EnableWerror src add-ons kernel file_systems ext2 ;
684#	EnableWerror src add-ons kernel file_systems fat ;
685#	EnableWerror src add-ons kernel file_systems googlefs ;
686	EnableWerror src add-ons kernel file_systems iso9660 ;
687	EnableWerror src add-ons kernel file_systems layers ;
688#	EnableWerror src add-ons kernel file_systems netfs ;
689#	EnableWerror src add-ons kernel file_systems nfs ;
690	EnableWerror src add-ons kernel file_systems nfs4 ;
691#	EnableWerror src add-ons kernel file_systems ntfs ;
692	EnableWerror src add-ons kernel file_systems packagefs ;
693#	EnableWerror src add-ons kernel file_systems ramfs ;
694#	EnableWerror src add-ons kernel file_systems reiserfs ;
695	EnableWerror src add-ons kernel file_systems udf ;
696	EnableWerror src add-ons kernel file_systems userlandfs ;
697	EnableWerror src add-ons kernel generic ;
698	EnableWerror src add-ons kernel network ;
699	EnableWerror src add-ons kernel partitioning_systems ;
700	EnableWerror src add-ons kernel power ;
701	EnableWerror src add-ons locale ;
702	EnableWerror src add-ons mail_daemon ;
703	EnableWerror src add-ons media media-add-ons demultiplexer ;
704	EnableWerror src add-ons media media-add-ons dvb ;
705	EnableWerror src add-ons media media-add-ons esound_sink ;
706	EnableWerror src add-ons media media-add-ons finepix_webcam ;
707	EnableWerror src add-ons media media-add-ons firewire_dv ;
708	EnableWerror src add-ons media media-add-ons legacy ;
709	EnableWerror src add-ons media media-add-ons mixer ;
710	EnableWerror src add-ons media media-add-ons multi_audio ;
711	EnableWerror src add-ons media media-add-ons opensound ;
712	EnableWerror src add-ons media media-add-ons radeon ;
713	EnableWerror src add-ons media media-add-ons reader ;
714	EnableWerror src add-ons media media-add-ons tone_producer_demo ;
715	EnableWerror src add-ons media media-add-ons usb_vision ;
716#	EnableWerror src add-ons media media-add-ons usb_webcam ;
717	EnableWerror src add-ons media media-add-ons video_mixer ;
718	EnableWerror src add-ons media media-add-ons video_producer_demo ;
719	EnableWerror src add-ons media media-add-ons videowindow ;
720	EnableWerror src add-ons media media-add-ons writer ;
721	EnableWerror src add-ons media plugins ape_reader ;
722	EnableWerror src add-ons media plugins au_reader ;
723#	EnableWerror src add-ons media plugins ffmpeg ;
724	EnableWerror src add-ons media plugins raw_decoder ;
725	EnableWerror src add-ons print ;
726	EnableWerror src add-ons screen_savers ;
727	EnableWerror src add-ons tracker ;
728	EnableWerror src add-ons translators bmp ;
729	EnableWerror src add-ons translators exr ;
730	EnableWerror src add-ons translators gif ;
731	EnableWerror src add-ons translators hvif ;
732	EnableWerror src add-ons translators ico ;
733	EnableWerror src add-ons translators jpeg ;
734#	EnableWerror src add-ons translators jpeg2000 ;
735	EnableWerror src add-ons translators pcx ;
736	EnableWerror src add-ons translators png ;
737	EnableWerror src add-ons translators ppm ;
738	EnableWerror src add-ons translators raw ;
739	EnableWerror src add-ons translators rtf ;
740	EnableWerror src add-ons translators sgi ;
741	EnableWerror src add-ons translators shared ;
742	EnableWerror src add-ons translators stxt ;
743	EnableWerror src add-ons translators tga ;
744	EnableWerror src add-ons translators tiff ;
745	EnableWerror src add-ons translators wonderbrush ;
746	EnableWerror src add-ons print ;
747	EnableWerror src bin desklink ;
748	EnableWerror src bin multiuser ;
749	EnableWerror src bin package ;
750	EnableWerror src bin package_repo ;
751	EnableWerror src bin pkgman ;
752	EnableWerror src libs bsd ;
753	EnableWerror src apps ;
754	EnableWerror src kits ;
755	EnableWerror src preferences ;
756	EnableWerror src servers ;
757	EnableWerror src system boot ;
758	EnableWerror src system kernel ;
759	EnableWerror src system libroot add-ons ;
760	EnableWerror src system libroot os ;
761	EnableWerror src system libroot posix locale ;
762	EnableWerror src system libroot posix wchar ;
763	EnableWerror src system runtime_loader ;
764
765	EnableStackProtector src apps ;
766	EnableStackProtector src kits ;
767	EnableStackProtector src preferences ;
768	EnableStackProtector src servers ;
769	EnableStackProtector src system kernel ;
770}
771
772
773rule MultiArchIfPrimary ifValue : elseValue : architecture
774{
775	# MultiArchIfPrimary <ifValue> : <elseValue>
776	#	[ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
777	#
778	# Returns one of the two given values depending on whether
779	# <architecture> is the primary packaging architecture.
780
781	architecture ?= $(TARGET_PACKAGING_ARCH) ;
782
783	if $(architecture) = $(TARGET_PACKAGING_ARCHS[1]) {
784		return $(ifValue) ;
785	}
786	return $(elseValue) ;
787}
788
789
790rule MultiArchConditionalGristFiles files : primaryGrist : secondaryGrist
791	: architecture
792{
793	# MultiArchConditionalGristFiles <files> : <primaryGrist>
794	#	: <secondaryGrist> [ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
795	#
796	# Returns <files> with their grist set to either <primaryGrist> or
797	# <secondaryGrist> depending on whether <architecture> is the primary
798	# packaging architecture.
799
800	architecture ?= $(TARGET_PACKAGING_ARCH) ;
801
802	local grist = [ MultiArchIfPrimary $(primaryGrist) : $(secondaryGrist)
803		: $(architecture) ] ;
804	return $(files:G=$(grist:E=)) ;
805}
806
807
808rule MultiArchDefaultGristFiles files : gristPrefix : architecture
809{
810	# MultiArchDefaultGristFiles <files> : <gristPrefix>
811	#	[ : <architecture> = $(TARGET_PACKAGING_ARCH) ] ;
812	#
813	# Convenient shorthand for MultiArchConditionalGristFiles for the common
814	# case that for a secondary packaging architecture the packaging
815	# architecture name shall be appended to the grist while it shall be omitted
816	# for the primary packaging architecture. IOW, if architecture is the
817	# primary packaging architecture, <files> are returned with their grist set
818	# to <gristPrefix>, otherwise <files> are returned with their grist set to
819	# <gristPrefix>!<architecture> respectively <architecture> (if <gristPrefix>
820	# is empty).
821
822	architecture ?= $(TARGET_PACKAGING_ARCH) ;
823
824	local secondaryGrist = $(gristPrefix)!$(architecture) ;
825	secondaryGrist ?= $(architecture) ;
826
827	return [ MultiArchConditionalGristFiles $(files) : $(gristPrefix) :
828		$(secondaryGrist) : $(architecture) ] ;
829}
830
831
832rule MultiArchSubDirSetup architectures
833{
834	# MultiArchSubDirSetup <architectures> ;
835	#
836	# For each of the given packaging architectures <architectures> that are
837	# in the packaging architectures configured for the build (or all configured
838	# packaging architectures, if <architectures> is empty) an object is
839	# prepared that can be used for an "on ... { ... }" block to set up subdir
840	# variables for the respective packaging architecture. Most notably
841	# TARGET_PACKAGING_ARCH, TARGET_ARCH are set to the values for the
842	# respective packaging architecture. The per-subdir variables SOURCE_GRIST,
843	# LOCATE_TARGET, LOCATE_SOURCE, SEARCH_SOURCE, *_LOCATE_TARGET, are reset.
844	# All SUBDIR* and config variables are set to the values they had when this
845	# rule was invoked.
846
847	local result ;
848	architectures ?= $(TARGET_PACKAGING_ARCHS) ;
849	local architecture ;
850	for architecture in $(architectures) {
851		if ! $(architecture) in $(TARGET_PACKAGING_ARCHS) {
852			continue ;
853		}
854
855		local architectureObject = $(architecture:G=<arch-object>) ;
856		result += $(architectureObject) ;
857
858		# Set the variables that default to the values of the respective
859		# variables for the primary architecture.
860		TARGET_PACKAGING_ARCH on $(architectureObject) = $(architecture) ;
861
862		local var ;
863		for var in TARGET_ARCH {
864			$(var) on $(architectureObject) = $($(var)_$(architecture)) ;
865		}
866
867		# Clone the current config variable values and the variables SubDir
868		# resets.
869		for var in $(AUTO_SET_UP_CONFIG_VARIABLES) SUBDIR$(SUBDIRRESET) {
870			$(var) on $(architectureObject) = $($(var)) ;
871		}
872
873		# adjust SOURCE_GRIST and HDRGRIST
874		SOURCE_GRIST on $(architectureObject)
875			= $(SOURCE_GRIST:E=)!$(architecture) ;
876
877		HDRGRIST on $(architectureObject)
878			= $(HDRGRIST:E=)!$(architecture) ;
879
880		# Adjust the subdir's object dirs that are architecture dependent. To
881		# avoid duplicating the code from SetupObjectsDir, we call it. Since it
882		# sets global variables, we set these variables on our object, call
883		# SetupObjectsDir in an "on" block, and grab the new variable values.
884		local hostTarget = HOST TARGET ;
885		local objectDirVars =
886			COMMON_ARCH COMMON_DEBUG DEBUG_$(HAIKU_DEBUG_LEVELS)
887			;
888		objectDirVars =
889			COMMON_PLATFORM_LOCATE_TARGET
890			$(hostTarget)_$(objectDirVars)_LOCATE_TARGET
891			LOCATE_TARGET
892			LOCATE_SOURCE
893			SEARCH_SOURCE
894			;
895
896		for var in $(objectDirVars) {
897			$(var) on $(architectureObject) = ;
898		}
899
900		on $(architectureObject) {
901			SetupObjectsDir ;
902
903			for var in $(objectDirVars) {
904				$(var) on $(architectureObject) = $($(var)) ;
905			}
906		}
907	}
908
909	return $(result) ;
910}
911