# Makefile.in for building crosstool-NG
# This file serves as source for the ./configure operation

MAKEFLAGS += --no-print-directory --no-builtin-rules

all: Makefile build

# Check if Makefile is up to date:
Makefile: Makefile.in
	@echo "$< did changed: you must re-run './configure'"
	@false

###############################################################################
# Configuration variables

VERSION:= @@VERSION@@
BINDIR := @@BINDIR@@
LIBDIR := @@LIBDIR@@
DOCDIR := @@DOCDIR@@
MANDIR := @@MANDIR@@
DATE   := @@DATE@@
LOCAL  := @@LOCAL@@
MAKE   := $(shell which $(MAKE) || type -p $(MAKE) || echo /usr/bin/make)

###############################################################################
# Global make rules

TARGETS := bin lib doc man

build: $(patsubst %,build-%,$(TARGETS))

install: local-test build $(patsubst %,install-%,$(TARGETS))

clean: $(patsubst %,clean-%,$(TARGETS))

distclean: clean
	@echo "  RM     'Makefile'"
	@rm -f Makefile

uninstall: local-test $(patsubst %,uninstall-%,$(TARGETS))

###############################################################################
# Specific make rules

#--------------------------------------
# Build rules

build-bin: ct-ng
	@echo "  CHMOD  '$<'"
	@chmod a+x $<

build-lib:

build-doc:

build-man: docs/ct-ng.1.gz

docs/ct-ng.1.gz: docs/ct-ng.1
	@echo "  GZIP   '$@'"
	@gzip -c9 $< >$@

%: %.in Makefile
	@echo "  SED    '$@'"
	@sed -r -e 's,@@CT_MAKE@@,$(MAKE),g;'       \
	        -e 's,@@CT_BINDIR@@,$(BINDIR),g;'   \
	        -e 's,@@CT_LIBDIR@@,$(LIBDIR),g;'   \
	        -e 's,@@CT_DOCDIR@@,$(DOCDIR),g;'   \
	        -e 's,@@CT_MANDIR@@,$(MANDIR),g;'   \
	        -e 's,@@CT_VERSION@@,$(VERSION),g;'	\
	        -e 's,@@CT_DATE@@,$(DATE),g;'       \
	     $@.in >$@

#--------------------------------------
# Clean rules

clean-bin:
	@echo "  RM     'ct-ng'"
	@rm -f ct-ng

clean-lib:

clean-doc:

clean-man:
	@echo "  RM     'docs/ct-ng.1'"
	@rm -f docs/ct-ng.1
	@echo "  RM     'docs/ct-ng.1.gz'"
	@rm -f docs/ct-ng.1.gz

#--------------------------------------
# Check for --local setup

# If using locally, don't install
local-test:
	@if [ "$(LOCAL)" = "1" ]; then                                          \
	     echo "You're using local copy as runtime. You can't (un)install."; \
	     false;                                                             \
	 fi

#--------------------------------------
# Install rules

install-bin: $(BINDIR)
	@echo "  INST   'ct-ng'"
	@install -m 755 ct-ng "$(BINDIR)/ct-ng"

# If one is hacking crosstool-NG, the patch set might change between any two
# installations of the same VERSION, thus the patches must be removed prior
# to being installed. It is simpler to remove the whole lib/ directory, as it
# is the goal of the install-lib rule to install the lib/ directory...
install-lib: uninstall-lib $(LIBDIR) install-lib-main install-lib-samples

install-lib-main: $(LIBDIR)
	@for src_dir in arch config kconfig patches scripts tools; do           \
	     echo "  INST   '$${src_dir}/'";                                    \
	     tar cf - --exclude=.svn $${src_dir} |(cd "$(LIBDIR)"; tar xf -);   \
	 done
	@rm -f "$(LIBDIR)/tools/addToolVersion.sh"
	@echo "  INST   'steps.mk'"
	@install -m 644 steps.mk "$(LIBDIR)/steps.mk"

# Samples need a little love:
#  - change every occurrence of CT_TOP_DIR to CT_LIB_DIR
install-lib-samples: $(LIBDIR) install-lib-main
	@echo "  INST   'samples/'"
	@tar cf - --exclude=.svn samples |(cd "$(LIBDIR)"; tar xf -)
	@for samp_file in "$(LIBDIR)/samples/"*"/crosstool.config"; do                          \
	     sed -r -i -e 's,\$$\{CT_TOP_DIR\},\$$\{CT_LIB_DIR\},g;' $${samp_file};             \
	     sed -r -i -e 's,^(CT_WORK_DIR)=.*,\1="\$${CT_TOP_DIR}/targets",;' $${samp_file};   \
	 done

install-doc: $(DOCDIR)
	@for doc_file in docs/CREDITS docs/overview.txt; do \
	     echo "  INST   '$${doc_file}'";                \
	     install -m 644 "$${doc_file}" "$(DOCDIR)";     \
	 done

install-man: $(MANDIR)
	@echo "  INST   'ct-ng.1.gz'"
	@install -m 644 docs/ct-ng.1.gz "$(MANDIR)"

$(sort $(BINDIR) $(LIBDIR) $(DOCDIR) $(MANDIR)):
	@echo "  MKDIR  '$@'"
	@install -m 755 -d "$@"


#--------------------------------------
# Uninstall rules

uninstall-bin:
	@echo "  RM     '$(BINDIR)/ct-ng'"
	@rm -f "$(BINDIR)/ct-ng"

uninstall-lib:
	@echo "  RMDIR  '$(LIBDIR)/'"
	@rm -rf "$(LIBDIR)"

uninstall-doc:
	@echo "  RMDIR  '$(DOCDIR)/'"
	@rm -rf "$(DOCDIR)"

uninstall-man:
	@echo "  RM     '$(MANDIR)/ct-ng.1.gz'"
	@rm -f "$(MANDIR)/ct-ng.1"{,.gz}