# Maintenance productions for the Lisp directory
# Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.

# This file is part of GNU Emacs.

# GNU Emacs 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, or (at your option)
# any later version.

# GNU Emacs 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 GNU Emacs; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

SHELL = /bin/sh

lisp=@srcdir@
VPATH=@srcdir@
srcdir=@srcdir@/..

# You can specify a different executable on the make command line,
# e.g. "make EMACS=../src/emacs ...".

EMACS = ../src/emacs

# Command line flags for Emacs.  This must include --multibyte,
# otherwise some files will not compile.

EMACSOPT = -batch --no-site-file --multibyte

SOURCES = *.el COPYING Makefile
lisptagsfiles1 = $(lisp)/[a-zA-Z]*.el
lisptagsfiles2 = $(lisp)/[a-zA-Z]*/[a-zA-Z]*.el
ETAGS = ../lib-src/etags

# Elisp files auto-generated.
AUTOGENEL = loaddefs.el \
	cus-load.el \
	finder-inf.el \
	subdirs.el \
	eshell/esh-groups.el

# Files to compile before others during a bootstrap.  This is done to
# speed up the bootstrap process.  The CC files are compiled first
# because CC mode tweaks the compilation process, and requiring
# cc-mode when it is not compiled doesn't work during the
# bootstrapping.

COMPILE_FIRST = \
	$(lisp)/emacs-lisp/byte-opt.el \
	$(lisp)/emacs-lisp/bytecomp.el \
	$(lisp)/subr.el \
	$(lisp)/progmodes/cc-mode.el \
	$(lisp)/progmodes/cc-vars.el

# The actual Emacs command run in the targets below.

emacs = EMACSLOADPATH=$(lisp) $(EMACS) $(EMACSOPT)

# Common command to find subdirectories

setwins=subdirs=`(cd $$wd; find . -type d -print)`; \
	for file in $$subdirs; do \
	   case $$file in */Old | */RCS | */CVS | */CVS/* | */.* | */.*/* | */=* ) ;; \
		*) wins="$$wins $$wd/$$file" ;; \
	   esac; \
        done

setwins_almost=subdirs=`(cd $$wd; find . -type d -print)`; \
	for file in $$subdirs; do \
	   case $$file in */Old | */RCS | */CVS | */CVS/* | */.* | */.*/* | */=* | */obsolete | */term ) ;; \
		*) wins="$$wins $$wd/$$file" ;; \
	   esac; \
        done

doit:

$(lisp)/cus-load.el:
	touch $@
custom-deps: $(lisp)/cus-load.el doit
	wd=$(lisp); $(setwins_almost); \
	echo Directories: $$wins; \
	$(EMACS) $(EMACSOPT) -l cus-dep --eval '(setq generated-custom-dependencies-file "$(lisp)/cus-load.el")' -f custom-make-dependencies $$wins

finder-data: doit
	wd=$(lisp); $(setwins_almost); \
	echo Directories: $$wins; \
	$(EMACS) $(EMACSOPT) -l finder --eval '(setq generated-finder-keywords-file "$(lisp)/finder-inf.el")' -f finder-compile-keywords-make-dist $$wins

$(lisp)/loaddefs.el:
	echo ";;; loaddefs.el --- automatically extracted autoloads" >> $@
	echo ";;" >> $@; echo ";;; Code:" >> $@
	echo "" >> $@
	echo ";;; Local Variables:" >> $@
	echo ";;; version-control: never" >> $@
	echo ";;; no-byte-compile: t" >> $@
	echo ";;; no-update-autoloads: t" >> $@
	echo ";;; End:" >> $@
	echo ";;; loaddefs.el ends here" >> $@
autoloads: $(lisp)/loaddefs.el doit
	wd=$(lisp); $(setwins); \
	echo Directories: $$wins; \
	$(EMACS) $(EMACSOPT) -l autoload --eval '(setq generated-autoload-file "$(lisp)/loaddefs.el")' -f batch-update-autoloads $$wins

$(lisp)/subdirs.el:
	$(MAKE) $(MFLAGS) update-subdirs
update-subdirs: doit
	wd=$(lisp); $(setwins); \
	for file in $$wins; do \
	   $(srcdir)/update-subdirs $$file; \
	done;

updates: update-subdirs autoloads finder-data custom-deps

# Update the AUTHORS file.

update-authors:
	$(emacs) -l authors -f batch-update-authors $(srcdir)/AUTHORS $(srcdir)

TAGS: $(lisptagsfiles1) $(lisptagsfiles2)
	els=`echo $(lisptagsfiles1) $(lisptagsfiles2) | sed -e "s,$(lisp)/loaddefs[^ ]*,," -e "s,$(lisp)/ldefs-boot[^ ]*,,"`; \
	${ETAGS} $$els

TAGS-LISP: $(lisptagsfiles1) $(lisptagsfiles2)
	els=`echo $(lisptagsfiles1) $(lisptagsfiles2) | sed -e "s,$(lisp)/loaddefs[^ ]*,," -e "s,$(lisp)/ldefs-boot[^ ]*,,"`; \
	${ETAGS} -o TAGS-LISP $$els

.SUFFIXES: .elc .el

.el.elc: $(lisp)/subdirs.el
	-$(emacs) -f batch-byte-compile $<

# Compile all Lisp files, but don't recompile those that are up to
# date.  Some files don't actually get compiled because they set the
# local variable no-byte-compile.

# All .elc files are made writable before compilation in case we
# checked out read-only (CVS option -r).  Files MUST be compiled one by
# one.  If we compile several files in a row we can't make sure that
# the compilation environment is clean.  We also set the load-path of
# the Emacs used for compilation to the current directory and its
# subdirectories, to make sure require's and load's in the files being
# compiled find the right files.

compile: $(lisp)/subdirs.el doit
	find $(lisp) -name "*.elc" -print | xargs chmod +w >/dev/null 2>&1; \
	wd=$(lisp); $(setwins);	\
	els=`echo $$wins | tr ' \011' '\012\012' | \
		sed -e 's|\(.\)$$|\1/|' -e 's|^\./||' -e 's|$$|*.el|'`;	\
	for el in $(COMPILE_FIRST) $$els; do \
	  if test -f $$el; \
	  then \
	    echo Compiling $$el; \
	    $(emacs) -f batch-byte-compile-if-not-done $$el || exit 1; \
	  fi \
	done

# Compile all Lisp files.  This is like `compile' but compiles files
# unconditionally.  Some files don't actually get compiled because they
# set the local variable no-byte-compile.

compile-always: $(lisp)/subdirs.el doit
	# `|| true' prevents old Bash versions from getting confused
	# by an error.
	find $(lisp) -name "*.elc" -print | xargs chmod +w >/dev/null 2>&1 || true; \
	wd=$(lisp); $(setwins);	\
	els=`echo $$wins | tr ' \011' '\012\012' | \
		sed -e 's|\(.\)$$|\1/|' -e 's|^\./||' -e 's|$$|*.el|'`;	\
	for el in $(COMPILE_FIRST) $$els; do \
	  if test -f $$el; \
	  then \
	    echo Compiling $$el; \
	    $(emacs) -f batch-byte-compile $$el || exit 1; \
	  fi \
	done

compile-calc:
	for el in `find $(lisp)/calc -name '*.el'`; do \
	  echo Compiling $$el; \
	  $(emacs) -f batch-byte-compile $$el || exit 1; \
	done

# Backup compiled Lisp files in elc.tar.gz.  If that file already
# exists, make a backup of it.

backup-compiled-files:
	-mv $(lisp)/elc.tar.gz $(lisp)/elc.tar.gz~
	-tar czf $(lisp)/elc.tar.gz $(lisp)/*.elc $(lisp)/*/*.elc

# Compile Lisp files, but save old compiled files first.

compile-after-backup: backup-compiled-files compile-always

# Recompile all Lisp files which are newer than their .elc files.
# Note that this doesn't create .elc files.  It only recompiles if an
# .elc is present.

recompile: doit $(lisp)/progmodes/cc-mode.elc
	$(EMACS) $(EMACSOPT) --eval "(batch-byte-recompile-directory 0)" $(lisp)

# CC Mode uses a compile time macro system which causes a compile time
# dependency in cc-mode.elc on the macros in cc-langs.el and the
# version string in cc-defs.el.
$(lisp)/progmodes/cc-mode.elc: \
	$(lisp)/progmodes/cc-mode.el \
	$(lisp)/progmodes/cc-langs.el \
	$(lisp)/progmodes/cc-defs.el
	$(EMACS) $(EMACSOPT) -f batch-byte-compile $(lisp)/progmodes/cc-mode.el

# Prepare a bootstrap in the lisp subdirectory.
#
# Build loaddefs.el, because it's not sure it's up-to-date, and if it's not,
# that might lead to errors during the bootstrap because something fails to
# autoload as expected.  However, if there is no emacs binary, then we can't
# build autoloads yet, so just make sure there's some loaddefs.el file, as
# it's necessary for generating the binary (because loaddefs.el is an
# automatically generated file, we don't want to store it in the source
# repository).

bootstrap-prepare:
	if test -x $(EMACS); then				\
	  $(MAKE) $(MFLAGS) autoloads;				\
	else							\
	  if test ! -r $(lisp)/loaddefs.el; then		\
	    cp $(lisp)/ldefs-boot.el $(lisp)/loaddefs.el;	\
	  fi							\
	fi

maintainer-clean: distclean
	cd $(lisp); rm -f *.elc */*.elc $(AUTOGENEL)

bootstrap-clean:
	cd $(lisp); rm -f *.elc */*.elc

# Generate/update files for the bootstrap process.

bootstrap: update-subdirs autoloads compile

# Generate/update files after the bootstrap process.
# custom-deps needs `preloaded-file-list'.

bootstrap-after: finder-data custom-deps

distclean:
	-rm -f ./Makefile

# Makefile ends here.
