#
#  Makefile for `dbootstrap', the Debian GNU/Linux/Hurd installer.
#
# Copyright (c) 2000 Karl M. Hegbloom <karlheg@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#

# Call make with 'no_pointerize=1' to temporarily prevent translation
# message files from being updated by changes to C sources, to speed
# up compilation during development.

PROG   = dbootstrap
PROG_G = dbootstrap_g
PROG_LC = dbootstrap-lc

ifeq ($(USE_LANGUAGE_CHOOSER), true)
all all_potato : $(PROG) $(PROG_LC)
else
all all_potato : $(PROG)
endif

all_woody      : $(PROG) $(PROG_G)

TESTS = 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

_TESTS   := $(addsuffix _test,   $(TESTS))
_TESTS_G := $(addsuffix _g_test, $(TESTS))

tests   : $(_TESTS)
tests_g : $(_TESTS_G)

null:
	@: Use the null target with "make -p" for debugging Makefile.

.PHONY : all all_potato all_woody test test_g

# Set DEBUG to `true' to make a debuggable build.  Leave it `false'
# for production use.  It will be automaticly set to `true' by a
# target local override when you build the *_test targets.
#
# eg: `make DEBUG=true dbootstrap'.
#
DEBUG  = false
# DEBUG  = true

EFENCE = false
ifeq ($(EFENCE),true)
LEFENCE = -lefence
endif

# To compile in the language chooser, set this to `true'.
#
USE_LANGUAGE_CHOOSER ?= false

# Set this to the language with which you want to run the *_test
# targets.  eg: `make TESTLANG=de dbootstrap_test'
#
TESTLANG = C

CC = gcc

-include ../../config

GCCMAJVERSION=$(shell $(CC) --version | sed -n "s/^\([^\.]*\).*/\1/p" )
GCCMINVERSION=$(shell $(CC) --version | sed -n "s/^[^\.]*\.\([^\.]*\)[\.].*/\1/p" )

GCCSUPPORTSOPTSIZE := $(shell			\
if ( test $(GCCMAJVERSION) -eq 2 ) ; then	\
    if ( test $(GCCMINVERSION) -ge 66 ) ; then	\
	echo "true";				\
    else					\
	echo "false";				\
    fi;						\
else						\
    if ( test $(GCCMAJVERSION) -gt 2 ) ; then	\
	echo "true";				\
    else					\
	echo "false";				\
    fi;						\
fi; )


ifeq ($(GCCSUPPORTSOPTSIZE), true)
    OPTIMIZATION := -Os
else
    OPTIMIZATION := -O2
endif

ifeq ($(DEBUG),true)
    CPPFLAGS	= -D_GNU_SOURCE
    CFLAGS	= -Wall -g3 -pipe $(EXTRA_CFLAGS)
    LDFLAGS	= 
    STRIP	= :
else
    CPPFLAGS	= -D_GNU_SOURCE
    CFLAGS	= -Wall $(OPTIMIZATION) -fomit-frame-pointer -fno-builtin -pipe
    LDFLAGS	= -s
    STRIP	= strip --remove-section=.note --remove-section=.comment
endif

_LIBFDISK = ../libfdisk/libfdisk.a

_LIBBOGL  = ../bogl/libbogl.a

${_LIBFDISK} ${_LIBBOGL} :
	${MAKE} -C $(dir $@) $(notdir $@)

LIBS	= $(LEFENCE) -lloadtrm $(_LIBFDISK)
LIBS_G	= $(LEFENCE) $(_LIBBOGL)    -lloadtrm $(_LIBFDISK)

_BUILDTIME := $(shell TZ=UTC date --utc "+%Y.%m.%d-%H:%M%z")

ifndef KERNEL_VERSION_CODE
KERNEL_VERSION_CODE := $(shell echo $$((2 * 65536 + 2 * 256)))
endif

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

SOURCES          := $(wildcard *.c)


UTF_LIBS_BASE   = ../bf-utf
CPPFLAGS_LC     = -DUSE_LANGUAGE_CHOOSER
OBJECTS_LC      = $(OBJECTS:%.o=%.lc.o) langs.o
LIBS_LC = $(UTF_LIBS_BASE)/newt/libnewt.a $(UTF_LIBS_BASE)/slang/src/objs/libslang.a

LIBS_NLC         = -lslang -lnewt
OBJECTS		 = $(SOURCES:%.c=%.o)
OBJECTS_G	 = $(filter-out boxes.o, $(OBJECTS))

$(PROG) : $(LIBS)
$(PROG) : _OBJECTS = $(OBJECTS) $(OBJECTS_2)
$(PROG)	: $(OBJECTS) $(OBJECTS_2) $(_LIBFDISK)

$(PROG_LC) : $(LIBS) $(LIBS_LC) $(OBJECTS_LC)

$(PROG_G) : _LIBS    = $(LIBS_G)
$(PROG_G) : _OBJECTS = $(OBJECTS_G)
$(PROG_G) : $(_LIBBOGL) $(OBJECTS_G) $(_LIBFDISK)

$(PROG) $(PROG_G) :
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(OBJECTS) $(LIBS) $(LIBS_NLC)
	$(STRIP) $@

$(PROG_LC) :
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(OBJECTS_LC) $(LIBS) $(LIBS_LC)
	$(STRIP) $@
	$(MAKE) -C po all-utf


ifeq (${no_pointerize},)
__SOURCES = ${SOURCES}
endif

# These dependancies must be declared here so that 'make --jobs=4' can be used.
po/C.po  : po/dbootstrap.pot po/genc.awk
po/%.mo  : po/%.po
po/%.trm : po/%.mo

po/dbootstrap.pot po/C.po po/%.mo po/%.trm : ${__SOURCES}
	$(MAKE) -C po $(notdir $@)

langs.o: langs/langs.o
	cp $< $@

langs/langs.o :
	$(MAKE) DEBUG="$(DEBUG)" LINGUA="$(LINGUA)"		\
		CPPFLAGS="$(CPPFLAGS) $(CPPFLAGS_LC)" CFLAGS="$(CFLAGS)"	\
		-C langs $(notdir $@)

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

$(OBJECTS) : %.o : .translated/%.c

# Make sure this dir exists right now, as the Makefile is read, before
# targets that need it get run.  Used for .translated/ and .depend/
___ := $(shell [ -d .translated ] || mkdir .translated)

ifeq (${no_pointerize},)
__Cmo = po/C.mo
endif

.translated/%.c : %.c ${__Cmo} lang.h
	echo "#line 1 \"$<\"" > $@
	pointerize -m po/C.mo < $< >> $@

_all_tests = $(_TESTS) $(_TESTS_G) $(PROG)_test $(PROG_G)_test
$(_all_tests) %.test.o : override DEBUG   = true

$(_all_tests) %.test.o : override LINGUA  = $(TESTLANG)
$(_all_tests) %.test.o :          CFLAGS  = -Wall -g3 $(EXTRA_CFLAGS)
$(_all_tests) %.test.o :          LDFLAGS =

$(_TESTS) $(PROG)_test : _obj  = $(OBJECTS)
$(_TESTS) $(PROG)_test : _LIBS = $(LIBS)
$(_TESTS) $(PROG)_test : $(_LIBFDISK)
$(_TESTS) : %_test     : .translated/%.c $(OBJECTS) test.trm

$(_TESTS_G) $(PROG_G)_test : _obj  = $(OBJECTS_G)
$(_TESTS_G) $(PROG_G)_test : _LIBS = $(LIBS_G)
$(_TESTS_G) $(PROG_G)_test : $(_LIBBOGL) $(_LIBFDISK)
$(_TESTS_G) : %_g_test     : .translated/%.c $(OBJECTS_G) test.trm

$(PROG)_test $(PROG_G)_test : CPPFLAGS += -D_EXETESTING_

$(PROG_G)_test : $(OBJECTS_G) test.trm
$(PROG)_test   : $(OBJECTS) test.trm

$(PROG)_test $(PROG_G)_test : $(_obj)
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $(_obj) $(_LIBS) $(LIBS_NLC)

%_test %_g_test :
	rm -f main.o
	$(CC) -c $(CPPFLAGS) $(CFLAGS) -D_TESTING_ -o main.o .translated/main.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) -D_TESTING_ -o $*.test.o .translated/$*.c
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $*.test.o $(filter-out $*.o,$(_obj)) $(_LIBS) $(LIBS_NLC)

%_lc_test:
	rm -f main.o
	$(CC) -c $(CPPFLAGS) $(CPPFLAGS_LC) $(CFLAGS) -D_TESTING_ -o main.o .translated/main.c
	$(CC) -c $(CPPFLAGS) $(CPPFLAGS_LC) $(CFLAGS) -D_TESTING_ -o $*.test.o .translated/$*.c
	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $*.test.o $(filter-out $*.o,$(_obj)) $(_LIBS) $(LIBS_LC)


test.trm: po/$(TESTLANG).po
	$(MAKE) -C po $(TESTLANG).trm
	cp po/$(TESTLANG).trm test.trm 

%.test.o : CPPFLAGS += -D_TESTING_

# Must disable the builtin pattern rule so that building from the
# .translated/%.c works the way it should.
#
%.o : %.c

%.test.o %.o : .translated/%.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<

%.lc.o : .translated/%.c
	$(CC) -c $(CPPFLAGS) $(CPPFLAGS_LC) $(CFLAGS) -o $@ $<

$(UTF_LIBS_BASE)/newt/libnewt.a:
	cd $(UTF_LIBS_BASE)/newt; CFLAGS=-Os ./configure
	$(MAKE) -C $(UTF_LIBS_BASE)/newt

$(UTF_LIBS_BASE)/slang/src/objs/libslang.a:
	cd $(UTF_LIBS_BASE)/slang; CFLAGS=-Os ./configure
	$(MAKE) -C $(UTF_LIBS_BASE)/slang

.PHONY : clean
clean :
	$(MAKE) -C po clean
	$(MAKE) -C langs clean
	rm -rf targetdir_test
	rm -f $(PROG) $(PROG_G) $(PROG_LC) *.o *_test
	rm -rf .translated

.PHONY : distclean
distclean : clean
	$(MAKE) -C po maintainer-clean
	$(MAKE) -C langs distclean
	rm -rf .depend
	rm -f lang.h test.trm C.mo kbddefault.h
	rm -f .tmp.*		# legacy translated files

# The Makefile, because of the include statement below, depends on all
# of the .depend/% files.  Each .depend/% will have the same set of
# dependancies as each corresponding %.o.  When you run a `make', the
# .depend/% will be rebuilt if the %.c file or anything it depends on
# has been modified.
#
# This rule will generate, for each %.c, a .depend/% that defines
# build dependancies for each corresponding %.o, %.test.o, and
# .depend/%.
#
# See: `info make', "Automatic Prerequisites", which is where I got
# the starting recipe for doing this. - karlheg 2000.02.11
#
___ := $(shell [ -d .depend ] || mkdir .depend)

.depend/% : %.c
	set -e;					\
	  touch -a lang.h;			\
	  echo ".depend/$(notdir $@) : $<" > $@; \
	  echo >> $@;				\
	  $(CC) -MM $(CPPFLAGS) $*.c |		\
	    sed -e 's,\($*\)\.o[ :]*,\1.o \1.test.o : ,g' | \
	    sed -e 's,\($*\.c\),.translated/\1,' >> $@; \
	  touch -m $@;

-include $(addprefix .depend/,$(SOURCES:%.c=%))
