## This is Architecture independant part of the makefile

uses HAVE_OPTION_TABLE
uses CONFIG_AP_CODE_IN_CAR

makedefine CPP:= $(CC) -x assembler-with-cpp -DASSEMBLY -E
makedefine LIBGCC_FILE_NAME := $(shell $(CC) -print-libgcc-file-name)
makedefine GCC_INC_DIR := $(shell $(CC) -print-search-dirs | sed -ne "s/install: \(.*\)/\1include/gp")

makedefine CPPFLAGS := -I$(TOP)/src/include -I$(TOP)/src/arch/$(ARCH)/include -I$(GCC_INC_DIR) $(CPUFLAGS)
makedefine CFLAGS := $(CPU_OPT) $(CPPFLAGS) -Os -nostdinc -nostdlib -fno-builtin  -Wall

makedefine HOSTCFLAGS:= -Os -Wall

makerule ldscript.ld   
	depends	"ldoptions $(LDSUBSCRIPTS-1)" 
	action	"echo '/*ldoptions*/' > $@; cat ldoptions >> $@ ; for file in $(LDSUBSCRIPTS-1) ; do echo /\* $$file \*/ >> $@; cat $$file >> $@ ; done"
end

#makerule cpuflags   
#	depends	"Makefile.settings" 
#	action	"perl -e 'print \"CPUFLAGS :=\n\"; foreach $$var (split(\" \", $$ENV{VARIABLES})) { if (exists($$ENV{$$var})) { print \"CPUFLAGS += -D$$var\" . (length($$ENV{$$var})?\"=\x27$$ENV{$$var}\x27\":\"\") .\"\n\"} else { print \"CPUFLAGS += -U$$var\n\"} }' > $@"
#end

#makerule ldoptions   
#	depends	"Makefile.settings" 
#	action	"perl -e 'foreach $$var (split(\" \", $$ENV{VARIABLES})) { if ($$ENV{$$var} =~ m/^(0x[0-9a-fA-F]+|0[0-7]+|[0-9]+)$$/) { print \"$$var = $$ENV{$$var};\n\"; }}' > $@"
#end

makerule linuxbios.strip  
	depends	"linuxbios" 
	action	"$(OBJCOPY) -O binary linuxbios linuxbios.strip"
end

makerule linuxbios.a
        depends "$(OBJECTS)"
        action  "rm -f linuxbios.a"
        action  "ar cr linuxbios.a $(OBJECTS)"
end

makerule linuxbios_ram.o
	depends	"$(DRIVER) linuxbios.a $(LIBGCC_FILE_NAME)" 
	action	"$(CC) -nostdlib -r -o $@ c_start.o $(DRIVER) linuxbios.a $(LIBGCC_FILE_NAME)"
end

makerule linuxbios_ram
	depends	"linuxbios_ram.o $(TOP)/src/config/linuxbios_ram.ld ldoptions" 
	action	"$(CC) -nostdlib -nostartfiles -static -o $@ -T $(TOP)/src/config/linuxbios_ram.ld linuxbios_ram.o"
	action 	"$(CROSS_COMPILE)nm -n linuxbios_ram | sort > linuxbios_ram.map"
end

##
## By default compress the part of linuxbios that runs from RAM
##
makedefine LINUXBIOS_RAM-$(CONFIG_COMPRESS):=linuxbios_ram.nrv2b
makedefine LINUXBIOS_RAM-$(CONFIG_UNCOMPRESSED):=linuxbios_ram.bin

makerule linuxbios_ram.bin 
	depends	"linuxbios_ram" 
	action	"$(OBJCOPY) -O binary $< $@"
end

makerule linuxbios_ram.nrv2b 
	depends	"linuxbios_ram.bin nrv2b" 
	action	"./nrv2b e $< $@"
end

makerule linuxbios_ram.rom
	depends	"$(LINUXBIOS_RAM-1)" 
	action	"cp $(LINUXBIOS_RAM-1) linuxbios_ram.rom"
end

makedefine LINUXBIOS_APC:=

if CONFIG_AP_CODE_IN_CAR
	#for ap code in cache

	makerule linuxbios_apc.a
		depends "apc_auto.o"
		action  "rm -f linuxbios_apc.a"
		action  "ar cr linuxbios_apc.a apc_auto.o"
	end

	makerule linuxbios_apc.o
		depends "linuxbios_apc.a c_start.o $(LIBGCC_FILE_NAME)"
        action  "$(CC) -nostdlib -r -o $@ c_start.o linuxbios_apc.a $(LIBGCC_FILE_NAME)"
	end

	makerule linuxbios_apc
		depends "linuxbios_apc.o $(TOP)/src/config/linuxbios_apc.ld ldoptions"
		action  "$(CC) -nostdlib -nostartfiles -static -o $@ -T $(TOP)/src/config/linuxbios_apc.ld linuxbios_apc.o"
		action  "$(CROSS_COMPILE)nm -n linuxbios_apc | sort > linuxbios_apc.map"
	end

	##
	## By default compress the part of linuxbios that runs from cache as ram
	##
	makedefine LINUXBIOS_APC-$(CONFIG_COMPRESS):=linuxbios_apc.nrv2b
	makedefine LINUXBIOS_APC-$(CONFIG_UNCOMPRESSED):=linuxbios_apc.bin

	makerule linuxbios_apc.bin
		depends "linuxbios_apc"
		action  "$(OBJCOPY) -O binary $< $@"
	end

	makerule linuxbios_apc.nrv2b
        depends "linuxbios_apc.bin nrv2b"
        action  "./nrv2b e $< $@"
	end

	makerule linuxbios_apc.rom
		depends "$(LINUXBIOS_APC-1)"
		action  "cp $(LINUXBIOS_APC-1) linuxbios_apc.rom"
	end

	makedefine LINUXBIOS_APC:=linuxbios_apc.rom

end

makedefine LINUXBIOS_RAM_ROM:=linuxbios_ram.rom

makerule linuxbios   
	depends	"crt0.o $(INIT-OBJECTS) $(LINUXBIOS_APC) $(LINUXBIOS_RAM_ROM) ldscript.ld"
	action	"$(CC) -nostdlib -nostartfiles -static -o $@ -T ldscript.ld crt0.o $(INIT-OBJECTS)"
	action	"$(CROSS_COMPILE)nm -n linuxbios | sort > linuxbios.map"
end

#makerule crt0.S   
#	depends	"$(CRT0)" 
#	action		"cp $< $@"
#end

# the buildrom tool
makerule buildrom 
	depends	"$(TOP)/util/buildrom/buildrom.c" 
	action	"$(HOSTCC) -o $@ $<"
end

# Force crt0.s (which has build time version code in it to rebuild every time)
makedefine .PHONY : crt0.s
makerule crt0.s   
	depends	"crt0.S $(CRT0_INCLUDES)" 
	action	"@echo \"$(CPP) ... $< > $@ \""
	action	"$(CPP) $(CPPFLAGS) -I. -I$(TOP)/src  $< > $@.new && mv $@.new $@"
end

makerule crt0.o  
	depends	"crt0.s" 
	action	"@echo $(CC) ... -o $@ $<"
	action	"@$(CC) -c $(CPU_OPT) -o $@ $<"
end

makerule etags   
	depends	"$(SOURCES)" 
	action	"etags $(SOURCES)"
end
makerule tags   
	depends	"$(SOURCES)" 
	action	"ctags $(SOURCES)"
end
makerule LinuxBIOSDoc.config
	depends	"$(TOP)/src/config/LinuxBIOSDoc.config" 
	action "cat $(TOP)/src/config/LinuxBIOSDoc.config > LinuxBIOSDoc.config"
        action "echo 'INPUT=$(SOURCES)' >> LinuxBIOSDoc.config"
end
makerule documentation   
	depends	"LinuxBIOSDoc.config"
	action	"doxygen LinuxBIOSDoc.config"
end

makerule ./romcc   
	depends	"$(TOP)/util/romcc/romcc.c" 
	action	"$(HOSTCC) -g $(HOSTCFLAGS) $< -o $@"
end

makerule build_opt_tbl   
	depends	"$(TOP)/util/options/build_opt_tbl.c $(TOP)/src/include/pc80/mc146818rtc.h $(TOP)/src/include/boot/linuxbios_tables.h Makefile.settings Makefile"
	action	"$(HOSTCC) $(HOSTCFLAGS) $(CPUFLAGS) $< -o $@" 
end

#makerule /$(TARGET_DIR)/option_table.c
#	depends	"build_opt_tbl $(MAINBOARD)/cmos.layout" 
#	action	"./build_opt_tbl -b  --config $(MAINBOARD)/cmos.layout"
#end

makerule option_table.c
	depends	"build_opt_tbl $(MAINBOARD)/cmos.layout" 
	action	"./build_opt_tbl -b  --config $(MAINBOARD)/cmos.layout --header option_table.h"
end

makerule option_table.h
	depends	"build_opt_tbl $(MAINBOARD)/cmos.layout" 
	action	"./build_opt_tbl -b  --config $(MAINBOARD)/cmos.layout --header option_table.h"
end

if HAVE_OPTION_TABLE
object ./option_table.o 
#special rule
#makerule option_table.o 
#	depends	"option_table.c" 
#	action	"$(CC) -c $(CFLAGS) -o $@ $<"
#end
# object option_table.o 
end

makerule clean  
	action	"rm -f linuxbios.* *~"
	action	"rm -f linuxbios"
	action	"rm -f ldscript.ld"
	action	"rm -f a.out *.s *.l *.o *.E *.inc"
	action	"rm -f TAGS tags romcc*"
	action	"rm -f docipl buildrom* chips.c *chip.c linuxbios_apc* linuxbios_ram* linuxbios_pay*"
	action	"rm -f build_opt_tbl* nrv2b* option_table.c crt0.S"
end

# do standard config files that the user need not specify
# for now, this is just 'lib', but it may be more later. 
dir /lib
dir /console
dir /stream
dir /devices
dir /pc80
dir /boot