# -*- makefile -*-

COMMON_ASFLAGS	= -nostdinc -fno-builtin -m32
COMMON_CFLAGS = -fno-builtin -mrtd -mregparm=3 -m32
COMMON_LDFLAGS	= -m32 -nostdlib

# Used by various components.  These rules need to precede them.
script/lexer.c_DEPENDENCIES = grub_script.tab.h

# Images.

GRUB_KERNEL_MACHINE_LINK_ADDR	= 0x8200

ifeq ($(platform), coreboot)

pkglib_PROGRAMS += kernel.img
kernel_img_SOURCES = kern/i386/coreboot/startup.S \
	kern/i386/misc.S \
	kern/i386/coreboot/init.c \
	kern/i386/multiboot_mmap.c \
	kern/i386/halt.c \
	kern/main.c kern/device.c \
	kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \
	kern/misc.c kern/mm.c kern/term.c \
	kern/rescue_parser.c kern/rescue_reader.c \
	kern/time.c kern/list.c kern/handler.c kern/command.c kern/corecmd.c \
	kern/$(target_cpu)/dl.c kern/parser.c kern/partition.c \
	kern/i386/tsc.c kern/i386/pit.c \
	kern/generic/rtc_get_time_ms.c \
	kern/generic/millisleep.c \
	kern/env.c \
	term/i386/pc/vga_text.c term/i386/vga_common.c \
	symlist.c
kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \
	env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h \
	partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \
	machine/boot.h machine/console.h machine/init.h \
	machine/memory.h list.h handler.h command.h i18n.h mm_private.h
kernel_img_CFLAGS = $(COMMON_CFLAGS)
kernel_img_ASFLAGS = $(COMMON_ASFLAGS)
kernel_img_LDFLAGS = $(COMMON_LDFLAGS) -Wl,-N,-S,-Ttext,$(GRUB_KERNEL_MACHINE_LINK_ADDR),-Bstatic

endif

ifeq ($(platform), qemu)

GRUB_BOOT_MACHINE_LINK_ADDR	= 0xffe00

pkglib_IMAGES += boot.img
boot_img_SOURCES = boot/i386/qemu/boot.S
boot_img_ASFLAGS = $(COMMON_ASFLAGS) -DGRUB_BOOT_MACHINE_LINK_ADDR=$(GRUB_BOOT_MACHINE_LINK_ADDR)
boot_img_LDFLAGS = $(COMMON_LDFLAGS) $(TARGET_IMG_LDFLAGS)$(GRUB_BOOT_MACHINE_LINK_ADDR)
boot_img_FORMAT = binary

bin_UTILITIES += grub-mkimage
grub_mkimage_SOURCES = util/i386/pc/grub-mkimage.c util/misc.c \
	util/resolve.c gnulib/progname.c
grub_mkimage_CFLAGS = -DGRUB_KERNEL_MACHINE_LINK_ADDR=$(GRUB_KERNEL_MACHINE_LINK_ADDR)

pkglib_IMAGES += kernel.img
kernel_img_SOURCES = kern/i386/qemu/startup.S \
	kern/i386/misc.S \
	kern/i386/coreboot/init.c \
	kern/i386/qemu/mmap.c \
	kern/i386/halt.c \
	kern/main.c kern/device.c \
	kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c \
	kern/misc.c kern/mm.c kern/term.c \
	kern/rescue_parser.c kern/rescue_reader.c \
	kern/time.c kern/list.c kern/handler.c kern/command.c kern/corecmd.c \
	kern/$(target_cpu)/dl.c kern/parser.c kern/partition.c \
	kern/i386/tsc.c kern/i386/pit.c \
	kern/generic/rtc_get_time_ms.c \
	kern/generic/millisleep.c \
	kern/env.c \
	term/i386/pc/vga_text.c term/i386/vga_common.c \
	symlist.c
kernel_img_HEADERS = boot.h cache.h device.h disk.h dl.h elf.h elfload.h \
	env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h \
	partition.h msdos_partition.h reader.h symbol.h term.h time.h types.h \
	machine/boot.h machine/console.h machine/init.h \
	machine/memory.h list.h handler.h command.h i18n.h mm_private.h
kernel_img_CFLAGS = $(COMMON_CFLAGS) -DGRUB_BOOT_MACHINE_LINK_ADDR=$(GRUB_BOOT_MACHINE_LINK_ADDR)
kernel_img_ASFLAGS = $(COMMON_ASFLAGS) -DGRUB_KERNEL_MACHINE_LINK_ADDR=$(GRUB_KERNEL_MACHINE_LINK_ADDR)
kernel_img_LDFLAGS = $(COMMON_LDFLAGS) $(TARGET_IMG_LDFLAGS)$(GRUB_KERNEL_MACHINE_LINK_ADDR)
kernel_img_FORMAT = binary
endif

MOSTLYCLEANFILES += symlist.c kernel_syms.lst
DEFSYMFILES += kernel_syms.lst

symlist.c: $(addprefix include/grub/,$(kernel_img_HEADERS)) config.h gensymlist.sh
	/bin/sh gensymlist.sh $(filter %.h,$^) > $@ || (rm -f $@; exit 1)

kernel_syms.lst: $(addprefix include/grub/,$(kernel_img_HEADERS)) config.h genkernsyms.sh
	/bin/sh genkernsyms.sh $(filter %.h,$^) > $@ || (rm -f $@; exit 1)

sbin_SCRIPTS += grub-install
grub_install_SOURCES = util/grub-install.in

bin_SCRIPTS += grub-mkrescue
grub_mkrescue_SOURCES = util/grub-mkrescue.in

# Modules.
pkglib_MODULES = play.mod		\
	memdisk.mod pci.mod lspci.mod reboot.mod	\
	halt.mod datetime.mod date.mod datehook.mod	\
	lsmmap.mod mmap.mod

# For boot.mod.
pkglib_MODULES += boot.mod
boot_mod_SOURCES = commands/boot.c
boot_mod_CFLAGS = $(COMMON_CFLAGS)
boot_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For mmap.mod.
mmap_mod_SOURCES = mmap/mmap.c mmap/i386/uppermem.c mmap/i386/mmap.c
mmap_mod_CFLAGS = $(COMMON_CFLAGS)
mmap_mod_LDFLAGS = $(COMMON_LDFLAGS)
mmap_mod_ASFLAGS = $(COMMON_ASFLAGS)

# For reboot.mod.
reboot_mod_SOURCES = commands/reboot.c
reboot_mod_CFLAGS = $(COMMON_CFLAGS)
reboot_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For halt.mod.
halt_mod_SOURCES = commands/halt.c
halt_mod_CFLAGS = $(COMMON_CFLAGS)
halt_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For play.mod.
play_mod_SOURCES = commands/i386/pc/play.c
play_mod_CFLAGS = $(COMMON_CFLAGS)
play_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For memdisk.mod.
memdisk_mod_SOURCES = disk/memdisk.c
memdisk_mod_CFLAGS = $(COMMON_CFLAGS)
memdisk_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For pci.mod
pci_mod_SOURCES = bus/pci.c
pci_mod_CFLAGS = $(COMMON_CFLAGS)
pci_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For lspci.mod
lspci_mod_SOURCES = commands/lspci.c
lspci_mod_CFLAGS = $(COMMON_CFLAGS)
lspci_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For datetime.mod
datetime_mod_SOURCES = lib/i386/datetime.c
datetime_mod_CFLAGS = $(COMMON_CFLAGS)
datetime_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For date.mod
date_mod_SOURCES = commands/date.c
date_mod_CFLAGS = $(COMMON_CFLAGS)
date_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For datehook.mod
datehook_mod_SOURCES = hook/datehook.c
datehook_mod_CFLAGS = $(COMMON_CFLAGS)
datehook_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For lsmmap.mod
lsmmap_mod_SOURCES = commands/lsmmap.c
lsmmap_mod_CFLAGS = $(COMMON_CFLAGS)
lsmmap_mod_LDFLAGS = $(COMMON_LDFLAGS)

include $(srcdir)/conf/i386.mk
include $(srcdir)/conf/common.mk
