# Makefile for dbootstrap
#
# Yes, we like rewriting this makefile over and over again....

# Some things we can set to change compilation:
# DEBUG=true	- turns on debugging (-g)
# SMALL=true	- turns on size optimizations (-Os -fomit-frame-point)
# LC=true	- turns on language chooser
#		  This needs to agree with USE_LANG_CHOOSER
# FB=true	- turns on "graphical" verson of dbootstrap
# EFENCE=true	- compile with efence
# POINTERIZE=false = turns off translations

-include ../../config

# These are the default values:
DEBUG	?= false
SMALL	?= true
LC = $(USE_LANGUAGE_CHOOSER)
FB	?= false
EFENCE	?= false
POINTERIZE ?= true
LINGUA	?= C
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
DEBUG	:= false
SMALL	:= false
endif

# General compiler stuff
CC	 := gcc
CFLAGS	 := -Wall -D_GNU_SOURCE -pipe -I. $(EXTRA_CFLAGS) 
LDFLAGS	 :=
BFLIBS	 := ../libfdisk/libfdisk.a
LIBS	 := -lloadtrm $(BFLIBS) -lnewt -lslang
ifeq ($(LC),true)
BUILD	 := build-lc/
else
BUILD	 := build/
endif

ifeq ($(DEBUG),true)
CFLAGS	+= -g -DDEBUG
LDFLAGS	+=
STRIP	:= :
else
ifeq ($(SMALL),true)
CFLAGS	+= -Os -fomit-frame-pointer -fno-builtin
LDFLAG	+=
STRIP	:=  strip --remove-section=.note --remove-section=.comment
else
CFLAGS	+= -O2
STRIP	:= :
endif
endif

# for euc-jp encoded japanese string
ifeq ($(LINGUA),ja)
USE_SHRINK_STRING = true
endif
ifdef USE_SHRINK_STRING
CPPFLAGS	+= -DUSE_SHRINK_STRING
OBJECT_SS	+= shrink_string.o
endif

ifeq ($(EFENCE),true)
LIBS	+= -lefence
endif

ifeq ($(LC),true)
CFLAGS	+= -DUSE_LANGUAGE_CHOOSER -DUSE_LANGUAGE_VARIANTS -DUTF8
endif

ifdef KBD
CFLAGS += -DKBD=\"$(KBD)\"
endif

ifeq ($(FB),true)
CFLAGS	+= -DGRAPHICAL
LIBS	+= /usr/lib/libbogl.a
endif

# constants
ifeq ($(LC),true)
DBOOTSTRAP	:= dbootstrap-lc
else
DBOOTSTRAP	:= dbootstrap
endif
BUILDTIME	:= $(shell TZ=UTC date --utc "+%Y.%m.%d-%H:%M%z")
ifndef KERNEL_VERSION_CODE
# fake it... (2.2)
KERNEL_VERSION_CODE := $(shell echo $$((2 * 65535 + 2 * 256)))
endif

CFLAGS	+= -DBUILDTIME='"$(BUILDTIME)"' -DARCHNAME='"$(architecture)"'
CFLAGS	+= -DKERNEL_VERSION_CODE=${KERNEL_VERSION_CODE} -DKVER='"${KVER}"'
ifdef FONT
CFLAGS	+= -DFONT='"$(FONT)"'
endif

# Default build target:
all: $(DBOOTSTRAP) po/po_stamp

# Targets
DBOOTSTRAP_MODULES	:= $(subst .c,,$(wildcard *.c))
TEST_MODULES		:= interactive_shell	\
			   boxes		\
			   release_notes	\
			   main_menu		\
			   kbdconfig		\
			   choose_medium	\
			   floppy_merge		\
			   pcmcia		\
			   netconfig		\
			   tzconfig		\
			   partition_config	\
			   select_not_mounted	\
			   bootconfig		\
			   reboot_system	\
			   net-fetch

ifeq ($(POINTERIZE),false)
SRCEXT			:= c
OBJEXT			:= o
else
SRCEXT			:= t.c
OBJEXT			:= to
LANG_H			:= lang.h
endif
DBOOTSTRAP_TARGETS	= $(addprefix $(BUILD),$(addsuffix .$(OBJEXT),$(DBOOTSTRAP_MODULES)))
TEST_TARGETS		= $(addprefix $(BUILD),$(addsuffix .$(OBJEXT),$(TEST_MODULES)))

ifeq ($(LC),true)
DBOOTSTRAP_TARGETS += langs/langs.o
langs/langs.o:
	$(MAKE) -C langs
endif

# Rules
ifneq ($(BUILD),)
$(BUILD):
	@test -d $(BUILD) || mkdir -p $(BUILD)
endif

show_settings:
	@echo Compilation settings:
	@echo CC	= $(CC)
	@echo CFLAGS	= $(CFLAGS)
	@echo LDFLAGS	= $(LDFLAGS)
	@echo LIBS	= $(LIBS)
	@echo BUILD	= $(BUILD)
	@echo DEBUG	= $(DEBUG)
	@echo SMALL	= $(SMALL)
	@echo LC	= $(LC)
	@echo FB	= $(FB)
	@echo EFENCE	= $(EFENCE)
	@echo POINTERIZE = $(POINTERIZE)
	@echo LINGUA	= $(LINGUA)

# i18n stuff
po/po_stamp:
	make -C po

po/%:
	make -C po $(notdir $@)

$(OBJECT_SS) :
	$(MAKE) -C shrink_string LINGUA="$(LINGUA)"

# libraries
$(BFLIBS):
	$(MAKE) -C $(dir $@) $(notdir $@)

# object build rules
# Generate dependencies (these are included at the end of this file 
$(BUILD)%.d: %.c
	gcc -MM $(CFLAGS) $< | sed -e "1s/.*:/$(subst /,\\/,$@):/" > $@

# Translations...
$(BUILD)%.t.c: %.c po/$(LINGUA).mo
	@echo "#line 1 \"$<\"" > $@
	pointerize -m po/$(LINGUA).mo -m po/C.mo < $< >> $@

$(BUILD)%_test.$(OBJEXT): CFLAGS += -D_TESTING_
$(BUILD)%_test.$(OBJEXT): $(BUILD)%.$(SRCEXT) $(BUILD)%.d $(LANG_H)
	$(CC) $(CFLAGS) -c -o $@ $<

$(BUILD)%.$(OBJEXT): $(BUILD)%.$(SRCEXT) $(BUILD)%.d $(LANG_H)
	$(CC) $(CFLAGS) -c -o $@ $<

lang.h: po/C.mo
	gen-header $<
	echo >> $@

test.trm: po/$(LINGUA).trm
	cp $^ $@

# dbootstrap and dbootstrap_test
$(BUILD)$(DBOOTSTRAP)_test: CFLAGS += -D_EXETESTING_
$(BUILD)$(DBOOTSTRAP) $(BUILD)$(DBOOTSTRAP)_test: $(BUILD) $(DBOOTSTRAP_TARGETS) $(BFLIBS)
	$(CC) -o $@ $(DBOOTSTRAP_TARGETS) $(LDFLAGS) $(LIBS)
	$(STRIP) $@

# unit test targets, e.g. main_menu_test
$(BUILD)%_test: $(BUILD) $(BUILD)main_test.$(OBJEXT) \
	$(BUILD)%_test.$(OBJEXT) $(DBOOTSTRAP_TARGETS) test.trm
	$(CC) -o $@ $(BUILD)main_test.$(OBJEXT) $(BUILD)$*_test.$(OBJEXT) \
		$(filter-out $< $(BUILD)main.$(OBJEXT) $(BUILD)$*.$(OBJEXT),$(DBOOTSTRAP_TARGETS)) $(LDFLAGS) $(LIBS)
	$(STRIP) $@

# this is just for convenience to move things from the build directory
# back to the source (this is meant only for the final target, not the intermediates)

ifneq ($(BUILD),)
%: $(BUILD)%
	cp $(BUILD)$@ $@
endif

# Clean rules
.PHONY: clean
clean:
	$(MAKE) -C po $@
	$(MAKE) -C langs $@
	rm -f $(DBOOTSTRAP) $(BUILD)$(DBOOTSTRAP) *_test dbootstrap-lc
	rm -f $(BUILD)*.o $(BUILD)*.to $(BUILD)*.t.c $(BUILD)*_test $(BUILD)*.d

distclean: clean
	$(MAKE) -C po $@
	$(MAKE) -C langs $@
	rm -f lang.h test.trm C.mo kbddefault.h
	test -z "$(BUILD)" || rm -rf $(BUILD)

.PHONY: all all_potato all_woody clean distclean

The_DFiles = $(wildcard $(BUILD)*.d)
ifneq ($(words $(The_DFiles)),0)
include $(The_DFiles)
endif

$(DBOOTSTRAP):	$(BUILD)$(DBOOTSTRAP)
