#
# This file is subject to the terms and conditions of the GNU General Public
# License.  See the file "COPYING" in the main directory of this archive
# for more details.
#
# Copyright (C) 1994, 95, 96, 2003 by Ralf Baechle
# DECStation modifications by Paul M. Antoine, 1996
# Copyright (C) 2002, 2003  Maciej W. Rozycki
#
# This file is included by the global makefile so that you can add your own
# architecture-specific flags and dependencies. Remember to do have actions
# for "archclean" cleaning up for this architecture.
#

#
# Select the object file format to substitute into the linker script.
#
32bit-tool-prefix	= mips-rmt-elf-
32bit-bfd		= elf32-bigmips

ifdef CONFIG_RMT
gcc-abi			= 32
gas-abi			= 32
tool-prefix		= $(32bit-tool-prefix)
UTS_MACHINE		:= mips
endif

ifdef CONFIG_CROSSCOMPILE
CROSS_COMPILE		:= $(tool-prefix)
endif

#
# GCC uses -G 0 -mabicalls -fpic as default.  We don't want PIC in the kernel
# code since it only slows down the whole thing.  At some point we might make
# use of global pointer optimizations but their use of $28 conflicts with
# the current pointer optimization.
#
# The DECStation requires an ECOFF kernel for remote booting, other MIPS
# machines may also.  Since BFD is incredibly buggy with respect to
# crossformat linking we rely on the elf2ecoff tool for format conversion.
#
cflags-y			:= -I $(TOPDIR)/include/asm/gcc
cflags-y			+= -G 0 -mno-abicalls -fno-pic -pipe
#cflags-y			+= $(call check_gcc, -finline-limit=100000,)
LDFLAGS_vmlinux			+= -G 0 -static # -N
MODFLAGS			+= -mlong-calls

cflags-$(CONFIG_SB1XXX_CORELIS)	+= -mno-sched-prolog -fno-omit-frame-pointer

check_warning = $(shell if $(CC) $(1) -c -o /dev/null -xc /dev/null > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi)

#
# Use: $(call set_gccflags,<cpu0>,<isa0>,<cpu1>,<isa1>,<isa2>)
#
# <cpu0>,<isa0> -- preferred CPU and ISA designations (may require
#                  recent tools)
# <cpu1>,<isa1> -- fallback CPU and ISA designations (have to work
#                  with up to the oldest supported tools)
# <isa2>        -- an ISA designation used as an ABI selector for
#                  gcc versions that do not support "-mabi=32"
#                  (depending on the CPU type, either "mips1" or
#                  "mips2")
#
set_gccflags = $(shell \
while :; do \
	cpu=$(1); isa=-$(2); \
	for gcc_opt in -march= -mcpu=; do \
		$(CC) $$gcc_opt$$cpu $$isa -S -o /dev/null \
			-xc /dev/null > /dev/null 2>&1 && \
			break 2; \
	done; \
	cpu=$(3); isa=-$(4); \
	for gcc_opt in -march= -mcpu=; do \
		$(CC) $$gcc_opt$$cpu $$isa -S -o /dev/null \
			-xc /dev/null > /dev/null 2>&1 && \
			break 2; \
	done; \
	break; \
done; \
gcc_abi=-mabi=$(gcc-abi); gcc_cpu=$$cpu; \
if $(CC) $$gcc_abi -S -o /dev/null -xc /dev/null > /dev/null 2>&1; then \
	gcc_isa=$$isa; \
else \
	gcc_abi=; gcc_isa=-$(5); \
fi; \
gas_abi=-Wa,-$(gcc-abi); gas_cpu=$$cpu; gas_isa=-Wa,$$isa; \
while :; do \
	for gas_opt in -Wa,-march= -Wa,-mcpu=; do \
		$(CC) $$gas_abi $$gas_opt$$cpu $$gas_isa -Wa,-Z -c \
			-o /dev/null -xassembler /dev/null > /dev/null 2>&1 && \
			break 2; \
	done; \
	gas_abi=; gas_opt=; gas_cpu=; gas_isa=; \
	break; \
done; \
if test x$(gcc-abi) != x$(gas-abi); then \
	gas_abi="-Wa,-$(gas-abi) -Wa,-mgp$(gcc-abi)"; \
fi; \
echo $$gcc_abi $$gcc_opt$$gcc_cpu $$gcc_isa $$gas_abi $$gas_opt$$gas_cpu $$gas_isa)

#
# CPU-dependent compiler/assembler options for optimization.
#
ifdef CONFIG_RMT
build-bfd			= $(32bit-bfd)
cflags-y			+= $(32bit-isa-y)
endif

#cflags-$(CONFIG_CPU_URMTP1)	+= -falign-functions=1024 -falign-labels=512
#cflags-$(CONFIG_CPU_URMTP1)	+= -Wa,-murmtp -Wa,-rmtbug-xsync -Wa,-rmtbug-nopsync -Wa,-rmtbug-delaynop -Wa,-rmtbug-tnebj
#cflags-$(CONFIG_CPU_URMTP1)	+= -Wa,-rmtbug-syncbj -Wa,-rmtbug-noint
#cflags-$(CONFIG_CPU_URMTP2)	+= 
#cflags-$(CONFIG_CPU_MRMTP1)	+= -falign-functions=1024 -falign-labels=512
#cflags-$(CONFIG_CPU_MRMTP1)	+= -Wa,-murmtp -Wa,-rmtbug-xsync -Wa,-rmtbug-nopsync -Wa,-rmtbug-delaynop -Wa,-rmtbug-tnebj
cflags-$(CONFIG_CPU_MRMTP2)	+= 

#
# ramdisk/initrd support
# You need a compressed ramdisk image, named
# CONFIG_EMBEDDED_RAMDISK_IMAGE. Relative pathnames 
# are relative to arch/rmt/ramdisk/.
#
core-$(CONFIG_EMBEDDED_RAMDISK)	+= arch/rmt/ramdisk/

#
# Board-dependent options and extra files
#

#
# RMT boards
#
core-$(CONFIG_RMT)		+= arch/rmt/boards/
load-$(CONFIG_RMT)		+= 0x80000000

cflags-y			+= -Iinclude/asm-rmt/mach-generic
drivers-$(CONFIG_PCI)		+= arch/rmt/pci/

JIFFIES	= jiffies_64 + 4	# Big-Endian machine

#
# Some machines like the Indy need 32-bit ELF binaries for booting purposes.
# Other need ECOFF, so we build a 32-bit ELF binary for them which we then
# convert to ECOFF using elf2ecoff.
#
# The 64-bit ELF tools are pretty broken so at this time we generate 64-bit
# ELF files from 32-bit files by conversion.
#
#AS += -64
#LDFLAGS += -m elf64bmip

#
# Choosing incompatible machines durings configuration will result in
# error messages during linking.  Select a default linkscript if
# none has been choosen above.
#

AFLAGS_vmlinux.lds.o := \
	-D"LOADADDR=$(load-y)" \
	-D"JIFFIES=$(JIFFIES)" \
	-imacros $(srctree)/include/asm-$(ARCH)/sn/mapped_kernel.h

AFLAGS		+= $(cflags-y)
CFLAGS		+= $(cflags-y)
LDFLAGS			+= --oformat $(32bit-bfd)

head-y := arch/rmt/kernel/head.o arch/rmt/kernel/init_task.o

libs-y			+= arch/rmt/lib/
core-y			+= arch/rmt/kernel/ arch/rmt/mm/ arch/rmt/math-emu/

ifdef CONFIG_MAPPED_KERNEL
vmlinux.64: vmlinux
	$(OBJCOPY) -O $(64bit-bfd) --remove-section=.reginfo \
		--change-addresses=0xc000000080000000 $< $@
else
vmlinux.64: vmlinux
	$(OBJCOPY) -O $(64bit-bfd) --remove-section=.reginfo \
		--change-addresses=0xa800000080000000 $< $@
endif

makeboot =$(Q)$(MAKE) -f scripts/Makefile.build obj=arch/rmt/boot $(1)

vmlinux.ecoff vmlinux.rm200: vmlinux
	+@$(call makeboot,$@)

vmlinux.srec: vmlinux
	+@$(call makeboot,$@)

CLEAN_FILES += vmlinux.ecoff \
	       vmlinux.srec \
	       vmlinux.rm200.tmp \
	       vmlinux.rm200

archclean:
	@$(MAKE) -f scripts/Makefile.clean obj=arch/rmt/boot

# Generate <asm/offset.h 
#
# The default rule is suffering from funny problems on MIPS so we using our
# own ...
#
# ---------------------------------------------------------------------------

define filechk_gen-asm-offset.h
	(set -e; \
	 echo "#ifndef _ASM_OFFSET_H"; \
	 echo "#define _ASM_OFFSET_H"; \
	 echo "/*"; \
	 echo " * DO NOT MODIFY."; \
	 echo " *"; \
	 echo " * This file was generated by arch/$(ARCH)/Makefile"; \
	 echo " *"; \
	 echo " */"; \
	 echo ""; \
	 sed -ne "/^@@@/s///p"; \
	 echo "#endif /* _ASM_OFFSET_H */" )
endef

define filechk_gen-asm-reg.h
	(set -e; \
	 echo "#ifndef _ASM_REG_H"; \
	 echo "#define _ASM_REG_H"; \
	 echo "/*"; \
	 echo " * DO NOT MODIFY."; \
	 echo " *"; \
	 echo " * This file was generated by arch/$(ARCH)/Makefile"; \
	 echo " *"; \
	 echo " */"; \
	 echo ""; \
	 sed -ne "/^@@@/s///p"; \
	 echo "#endif /* _ASM_REG_H */" )
endef

prepare: include/asm-$(ARCH)/offset.h \
	 include/asm-$(ARCH)/reg.h

arch/$(ARCH)/kernel/offset.s: include/asm include/linux/version.h \
				   include/config/MARKER

include/asm-$(ARCH)/offset.h: arch/$(ARCH)/kernel/offset.s
	$(call filechk,gen-asm-offset.h)
include/asm-$(ARCH)/reg.h: arch/$(ARCH)/kernel/reg.s
	$(call filechk,gen-asm-reg.h)

CLEAN_FILES += include/asm-$(ARCH)/offset.h.tmp \
	       include/asm-$(ARCH)/offset.h \
	       include/asm-$(ARCH)/reg.h.tmp \
	       include/asm-$(ARCH)/reg.h \
	       vmlinux.64 \
	       vmlinux.ecoff
