# Makefile for prjconfig
# Derived from buildroot2
#
# Copyright (C) 2002-2005 Erik Andersen <andersen@codepoet.org>
# Copyright (C) 2008 NXP B.V.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Library 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 Library General Public License for more
# details.
#
# You should have received a copy of the GNU Library 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

# Select the compiler needed to build binaries for your development system
HOSTCC    = gcc
CC=$(HOSTCC)
CFLAGS=$(HOSTCFLAGS)
HOSTCFLAGS= -Wall -Wstrict-prototypes \
  -DKBUILD_NO_NLS \
  -I$(CURDIR)

# Building on Ubuntu 9.10 generates "warning: value computed is not used"
# warnings (assignments via ncurses wattrset macro). Temp hack to ignore them.
HOSTCFLAGS += -Wno-unused-value

# Output destination. May be overridden from command line.
ifdef PRJROOT
  DESTDIR = $(PRJROOT)/.prjconfig.TMP
else
  DESTDIR = $(CURDIR)/output.TMP
endif
$(shell mkdir -p $(DESTDIR) $(DESTDIR)/lxdialog)

# Ensure consistent sort order, 'gcc -print-search-dirs' behavior, etc.
LC_ALL:= C

ifdef DEBUG
  HOSTCFLAGS += -g -DDBG_PRINT_ENABLED
  NATIVE_LDFLAGS += -g
endif

# Verbosity control. V=1 enables commands to be echoed.
Q=$(if $(filter true 1 yes,$V),,@)

TARGETS = $(DESTDIR)/conf $(DESTDIR)/mconf $(DESTDIR)/prjfixdep
.PHONY: all
all: $(TARGETS)

.PHONY: conf mconf
conf : $(DESTDIR)/conf
mconf: $(DESTDIR)/mconf
prjfixdep: $(DESTDIR)/prjfixdep

ifeq ($(shell uname),SunOS)
LIBS = -lcurses
else
LIBS = -lncurses
endif
ifeq (/usr/include/ncurses/ncurses.h, $(wildcard /usr/include/ncurses/ncurses.h))
	HOSTNCURSES += -I/usr/include/ncurses -DCURSES_LOC="<ncurses.h>"
else
ifeq (/usr/include/ncurses/curses.h, $(wildcard /usr/include/ncurses/curses.h))
	HOSTNCURSES += -I/usr/include/ncurses -DCURSES_LOC="<ncurses/curses.h>"
else
ifeq (/usr/local/include/ncurses/ncurses.h, $(wildcard /usr/local/include/ncurses/ncurses.h))
	HOSTCFLAGS += -I/usr/local/include/ncurses -DCURSES_LOC="<ncurses.h>"
else
ifeq (/usr/local/include/ncurses/curses.h, $(wildcard /usr/local/include/ncurses/curses.h))
	HOSTCFLAGS += -I/usr/local/include/ncurses -DCURSES_LOC="<ncurses/curses.h>"
else
ifeq (/usr/include/ncurses.h, $(wildcard /usr/include/ncurses.h))
	HOSTNCURSES += -DCURSES_LOC="<ncurses.h>"
else
	HOSTNCURSES += -DCURSES_LOC="<curses.h>"
endif
endif
endif
endif
endif

CONF_SRC      = conf.c
MCONF_SRC     = mconf.c
PRJFIXDEP_SRC = prjfixdep.c
LXD_SRC       = lxdialog/checklist.c lxdialog/menubox.c lxdialog/textbox.c \
                lxdialog/yesno.c lxdialog/inputbox.c lxdialog/util.c

SHARED_SRC    = $(DESTDIR)/zconf.tab.c
SHARED_DEPS  := lkc.h lkc_proto.h $(DESTDIR)/lkc_defs.h expr.h $(DESTDIR)/zconf.tab.h $(DESTDIR)/lex.zconf.c $(DESTDIR)/zconf.hash.c $(MAKEFILE_LIST)
CONF_OBJS     = $(patsubst %.c,$(DESTDIR)/%.o, $(CONF_SRC))
MCONF_OBJS    = $(patsubst %.c,$(DESTDIR)/%.o, $(MCONF_SRC) $(LXD_SRC))
PRJFIXDEP_OBJS= $(patsubst %.c,$(DESTDIR)/%.o, $(PRJFIXDEP_SRC))
SHARED_OBJS   = $(patsubst $(DESTDIR)/%.c,$(DESTDIR)/%.o, $(SHARED_SRC))

$(DESTDIR)/conf: $(CONF_OBJS) $(SHARED_OBJS) $(MAKEFILE_LIST)
	@echo '  HOSTLD  $@'
	$(Q)$(HOSTCC) $(NATIVE_LDFLAGS) $(CONF_OBJS) $(SHARED_OBJS) -o $@

$(DESTDIR)/mconf: $(MCONF_OBJS) $(SHARED_OBJS) $(MAKEFILE_LIST)
	@echo '  HOSTLD  $@'
	$(Q)$(HOSTCC) $(NATIVE_LDFLAGS) $(MCONF_OBJS) $(SHARED_OBJS) -o $@ $(LIBS)
mconf: ncurses

$(DESTDIR)/prjfixdep: $(PRJFIXDEP_OBJS) $(MAKEFILE_LIST)
	@echo '  HOSTLD  $@'
	$(Q)$(HOSTCC) $(NATIVE_LDFLAGS) $(PRJFIXDEP_OBJS) -o $@

$(CONF_OBJS): $(DESTDIR)/%.o : %.c $(SHARED_DEPS)
	@echo '  HOSTCC  $<'
	$(Q)$(HOSTCC) $(HOSTCFLAGS) -I. -c $< -o $@

$(MCONF_OBJS): $(DESTDIR)/%.o : %.c $(SHARED_DEPS)
	@echo '  HOSTCC  $<'
	$(Q)$(HOSTCC) $(HOSTCFLAGS) $(HOSTNCURSES) -I. -c $< -o $@

$(PRJFIXDEP_OBJS): $(DESTDIR)/%.o : %.c $(MAKEFILE_LIST)
	@echo '  HOSTCC  $<'
	$(Q)$(HOSTCC) $(HOSTCFLAGS) $(HOSTNCURSES) -I. -c $< -o $@

$(DESTDIR)/lkc_defs.h: lkc_proto.h $(MAKEFILE_LIST)
	$(Q)sed < $< > $@ 's/P(\([^,]*\),.*/#define \1 (\*\1_p)/'

$(DESTDIR)/zconf.tab.o: $(DESTDIR)/zconf.tab.c $(DESTDIR)/lex.zconf.c $(DESTDIR)/zconf.hash.c confdata.c expr.c symbol.c menu.c $(SHARED_DEPS)
	@echo '  HOSTCC  $<'
	$(Q)$(HOSTCC) $(HOSTCFLAGS) -I. -c $< -o $@

###
# The following requires flex/bison/gperf.
# To use the _shipped versions, comment the following line.
# If you are modifying the flex/bison/gperf src, uncomment it.

build: all
ifneq "$(filter build,$(MAKECMDGOALS))" ""
  LKC_GENPARSER := 1
endif

ifdef LKC_GENPARSER

$(DESTDIR)/%.tab.c $(DESTDIR)/%.tab.h: %.y
	bison -t -d -v -b $* -p $(notdir $*) -o $(DESTDIR)/$*.tab.c $<

$(DESTDIR)/lex.%.c: %.l
	flex -P$(notdir $*) -o$@ $<
	rm lex.backup

$(DESTDIR)/%.hash.c: %.gperf
	gperf < $< > $@

else

#$(DESTDIR)/lex.zconf.o: $(DESTDIR)/lex.zconf.c $(SHARED_DEPS)
#	$(HOSTCC) $(HOSTCFLAGS) -I. -c $< -o $@

$(DESTDIR)/lex.zconf.c: lex.zconf.c_shipped
	@echo '  SHIPPED $<'
	$(Q)cp -f $< $@

$(DESTDIR)/zconf.tab.c: zconf.tab.c_shipped
	@echo '  SHIPPED $<'
	$(Q)cp -f $< $@

$(DESTDIR)/zconf.tab.h: zconf.tab.h_shipped
	@echo '  SHIPPED $<'
	$(Q)cp -f $< $@

$(DESTDIR)/zconf.hash.c: zconf.hash.c_shipped
	@echo '  SHIPPED $<'
	$(Q)cp -f $< $@
endif

.PHONY: ncurses

ncurses:
	@echo "main() {}" > lxtemp.c
	@if $(HOSTCC) lxtemp.c $(LIBS) ; then \
		$(RM) lxtemp.c a.out; \
	else \
		$(RM) lxtemp.c; \
		echo -e "\007" ;\
		echo ">> Unable to find the Ncurses libraries." ;\
		echo ">>" ;\
		echo ">> You must have Ncurses installed in order" ;\
		echo ">> to use 'make menuconfig'" ;\
		echo ">>" ;\
		echo ">> Maybe you want to try 'make config', which" ;\
		echo ">> doesn't depend on the Ncurses libraries." ;\
		echo ;\
		exit 1 ;\
	fi

.PHONY: tidy
tidy: clean
tidy: TARGETS=

.PHONY: clean
clean:
	$(RM) $(DESTDIR)/*.o *~ core $(TARGETS) $(PRJFIXDEP_OBJS) $(MCONF_OBJS) $(CONF_OBJS) \
		zconf.output $(DESTDIR)/zconf.tab.c $(DESTDIR)/zconf.hash.c $(DESTDIR)/zconf.tab.h $(DESTDIR)/lex.zconf.c $(DESTDIR)/lex.backup $(DESTDIR)/lkc_defs.h


.PHONY: shipped
shipped:
	for f in *_shipped; do \
		echo "cp -f $(DESTDIR)/$${f%_shipped} $$f"; \
		cp -f $(DESTDIR)/$${f%_shipped} $$f; \
	done
