# -*- makefile -*-

kernel_img_RELOCATABLE = yes
pkglib_PROGRAMS = kernel.img
kernel_img_SOURCES = kern/device.c kern/disk.c kern/dl.c kern/env.c	\
	kern/err.c kern/list.c kern/command.c		\
	kern/corecmd.c kern/file.c kern/fs.c kern/main.c kern/misc.c	\
	kern/parser.c kern/partition.c kern/term.c			\
	kern/rescue_reader.c kern/rescue_parser.c			\
									\
	kern/emu/main.c kern/emu/mm.c kern/emu/misc.c			\
	kern/emu/getroot.c kern/emu/time.c kern/emu/hostdisk.c		\
	kern/emu/hostfs.c kern/emu/console.c				\
									\
	gnulib/progname.c disk/host.c
kernel_img_HEADERS += datetime.h emu/misc.h
kernel_img_CFLAGS = $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) -Wno-char-subscripts -Wno-unused -Wno-deprecated-declarations -Wno-undef -I$(srcdir)/gnulib
kernel_img_LDFLAGS = $(COMMON_LDFLAGS)
TARGET_NO_STRIP = yes

ifneq ($(TARGET_NO_MODULES), yes)
kernel_img_SOURCES += symlist.c kern/$(target_cpu)/dl.c
else
kernel_img_SOURCES += grub_emu_init.c
endif
ifneq ($(target_cpu), i386)
ifneq ($(target_cpu), x86_64)
kernel_img_SOURCES += kern/$(target_cpu)/cache.S
endif
endif

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

ifeq ($(target_cpu), i386)
pkglib_MODULES += cpuid.mod
cpuid_mod_SOURCES = commands/i386/cpuid.c
cpuid_mod_CFLAGS = $(COMMON_CFLAGS)
cpuid_mod_LDFLAGS = $(COMMON_LDFLAGS)
endif

grub_emu_LDFLAGS = $(LIBCURSES)

ifeq ($(enable_grub_emu_usb), yes)
kernel_img_HEADERS += libusb.h

pkglib_MODULES += libusb.mod
libusb_mod_SOURCES = bus/usb/emu/usb.c
libusb_mod_CFLAGS = 
libusb_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For usb.mod
pkglib_MODULES += usb.mod
usb_mod_SOURCES = bus/usb/usb.c
usb_mod_CFLAGS = $(COMMON_CFLAGS)
usb_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For usbtest.mod
pkglib_MODULES += usbtest.mod
usbtest_mod_SOURCES = commands/usbtest.c
usbtest_mod_CFLAGS = $(COMMON_CFLAGS)
usbtest_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For usbms.mod
pkglib_MODULES += usbms.mod
usbms_mod_SOURCES = disk/usbms.c
usbms_mod_CFLAGS = $(COMMON_CFLAGS)
usbms_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For serial.mod.
pkglib_MODULES += usbserial_common.mod
usbserial_common_mod_SOURCES = bus/usb/serial/common.c
usbserial_common_mod_CFLAGS = $(COMMON_CFLAGS)
usbserial_common_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For serial.mod.
pkglib_MODULES += usbserial_pl2303.mod
usbserial_pl2303_mod_SOURCES = bus/usb/serial/pl2303.c
usbserial_pl2303_mod_CFLAGS = $(COMMON_CFLAGS)
usbserial_pl2303_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For serial.mod.
pkglib_MODULES += usbserial_ftdi.mod
usbserial_ftdi_mod_SOURCES = bus/usb/serial/ftdi.c
usbserial_ftdi_mod_CFLAGS = $(COMMON_CFLAGS)
usbserial_ftdi_mod_LDFLAGS = $(COMMON_LDFLAGS)

# For serial.mod.
pkglib_MODULES += serial.mod
serial_mod_SOURCES = term/serial.c
serial_mod_CFLAGS = $(COMMON_CFLAGS)
serial_mod_LDFLAGS = $(COMMON_LDFLAGS)

grub_emu_LDFLAGS += $(LIBUSB)
endif

ifeq ($(enable_grub_emu_sdl), yes)
pkglib_MODULES += sdl.mod
sdl_mod_SOURCES = video/emu/sdl.c
sdl_mod_CFLAGS = 
sdl_mod_LDFLAGS = $(COMMON_LDFLAGS)
grub_emu_LDFLAGS += $(LIBSDL)
kernel_img_HEADERS += sdl.h
endif

ifeq ($(enable_grub_emu_pci), yes)
pkglib_MODULES += pci.mod
pci_mod_SOURCES = bus/emu/pci.c commands/lspci.c
pci_mod_LDFLAGS = $(COMMON_LDFLAGS)
grub_emu_LDFLAGS += $(LIBPCIACCESS)
kernel_img_HEADERS += libpciaccess.h
endif

include $(srcdir)/conf/common.mk

grub_emu_init.h: genemuinitheader.sh $(pkglib_MODULES)
	rm -f $@; echo $(pkglib_MODULES) | sh $(srcdir)/genemuinitheader.sh $(NM)  > $@
DISTCLEANFILES += grub_emu_init.h

grub_emu_init.c: genemuinit.sh $(pkglib_MODULES) grub_emu_init.h
	rm -f $@; echo $(pkglib_MODULES) | sh $(srcdir)/genemuinit.sh $(NM) > $@
DISTCLEANFILES += grub_emu_init.c

CLEANFILES += grub-emu
ifneq ($(TARGET_NO_MODULES), yes)
grub-emu: $(pkglib_PROGRAMS)
	$(CC) -o $@ $^ $(grub_emu_LDFLAGS) $(LDFLAGS)
else
grub-emu: $(pkglib_MODULES) $(pkglib_PROGRAMS)
	$(CC) -o $@ $^ $(grub_emu_LDFLAGS) $(LDFLAGS)
endif
GRUB_EMU=grub-emu

