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