# =============================================================================
# $Id: GNUmakefile,v 1.27 2005/07/11 22:39:06 jimb Exp $
# Author		: Jim Brooks http://www.jimbrooks.org
# Description	: GNUmakefile for "hypersrc".
# =============================================================================

-include GNUmakefile.config

AUTO_DOC_DIR = html_autogen

# Where to install.
#
ifeq ($(USER),root)
		INSTALL_DIR=/usr/local/bin
else
		INSTALL_DIR=$(HOME)/bin
endif

INSTALL			= install
INSTALL_OWNER	=
MAN_DIR			= /usr/local/man/man1

PROGNAME		= hypersrc
OUT				= out
SRC				= src
INC_DIR 		= $(SRC)/h

C_SRCS_		=	main.c			\
				widgets.c		\
				list.c			\
                history.c		\
			    text.c			\
			    font.c			\
                notebook.c		\
                prefs.c			\
				find.c			\
		        popup.c			\
                statusbar.c		\
                focusbar.c		\
		        tags.c			\
		        menu.c			\
		        toolbar.c		\
		        file.c			\
		        module.c		\
		        parser.c		\
		        funcgraph.c		\
		        funcmisc.c		\
		        treefunc.c		\
                handlers.c		\
                handlers_tree.c	\
                misc.c			\
		        utils.c			\
		        sort.c			\
			    hilite.c		\
			    hilite_c.c		\
			    hilite_forth.c	\
			    hilite_perl.c	\
			    hilite_python.c	\
                sys_unix.c

# Define SPEED to disable extra checks (or use "make speed").
#
#SPEED = y
ifdef SPEED
C_OPTS += -DHYPERSRC_SPEED
endif

# For releases.
#
ifndef DEBUG
RELEASE = y
endif

# Optimization must be enabled to inline functions.
#
ifdef SPEED
C_OPTIMIZE = -O3 -finline-functions
else

ifndef DEBUG
C_OPTIMIZE = -O2 -finline-functions
else
C_OPTIMIZE = -O2 -finline-functions
#C_OPTIMIZE = -O
endif

endif

# Strip symbol information for smaller executable (unless DEBUG).
ifndef DEBUG
ifndef PROFILE
LD_FLAGS	+= -s
endif
endif

# *** DEPRECATED *** -- threaded code was removed (but could be reinstated)
# Define THREADS to enable multi-threading.
#
#THREADS = y
#ifdef THREADS
#LIBS		+= -lpthread -lgthread
#C_OPTS		+= -D_REENTRANT -DHYPERSRC_THREADS
#endif

# For debugging.
#
ifdef DEBUG
C_DEBUG		= -g
C_OPTS		+= -DHYPERSRC_DEBUG
C_SRCS_		+= debug.c
endif

# This will compile pedantic self-checks (at expense of speed).
# Though, a few critical self-checks are always compiled.
#
ifndef SPEED
C_OPTS		+= -DHYPERSRC_PEDANTIC
endif

# This will enable profiling (for gprof).
#
ifdef PROFILE
C_OPTS		+= -DHYPERSRC_PROFILE
PROF_OPTS	= -pg
endif

# Controls whether g_assert() will be compiled.
# Without g_assert(), program will run faster but skips checks.
#
ifdef SPEED
C_OPTS		+= -DG_DISABLE_ASSERT
endif

# For measuring startup time (minus GUI).
#
ifdef MEASURE
C_OPTS		+= -DHYPERSRC_MEASURE
endif

# Compiler warnings.
#
C_WARNINGS	= -Wall -Wuninitialized
ifdef DEBUG
C_WARNINGS	+= -Wshadow
#C_WARNINGS	+= -Werror
endif

ifndef GTK_CONFIG
GTK_CONFIG	 = gtk-config
GLIB_CONFIG	 = glib-config
GNOME_CONFIG = gnome-config
endif

# -----	editing the rest should usually be unnecessary ------------------------

CC			= gcc
CC_FLAGS	= -I$(INC_DIR) $(PROF_OPTS) $(C_DEBUG) $(C_OPTIMIZE) $(C_OPTS) $(C_WARNINGS) `$(GTK_CONFIG) --cflags` `$(GNOME_CONFIG) --cflags gnomeui`
# Workaround: linking with distcc caused link errors on Mandrake 8.1 (but not Redhat 8).
ifeq ($(CC),distcc)
LD			= gcc
else
LD			= $(CC)
endif
LD_FLAGS	+= $(PROF_OPTS) `$(GTK_CONFIG) --libs` `$(GNOME_CONFIG) --libs gnomeui`
C_SRCS		= $(addprefix $(SRC)/,$(C_SRCS_))
C_OBJS_		= $(C_SRCS_:.c=.o)
C_OBJS		= $(addprefix $(OUT)/,$(C_OBJS_))

all:		$(OUT)  $(OUT)/hypersrc

release:
		@$(MAKE) RELEASE=y

speed:
		@$(MAKE) SPEED=y

debug:
		@$(MAKE) DEBUG=y

profile:
		@$(MAKE) PROFILE=y

profile_speed:
		@$(MAKE) PROFILE=y SPEED=y

measure:
		@$(MAKE) MEASURE=y

$(OUT):
		mkdir $(OUT)

$(OUT)/hypersrc:  $(C_OBJS)
		$(LD) -o $(OUT)/hypersrc $(C_OBJS) $(LD_FLAGS) $(LIBS)

$(OUT)/%.o:     $(SRC)/%.c
		$(CC) -c -o $@ $(CC_FLAGS) $(subst $(OUT)/,$(SRC)/,$<)

# -----------------------------------------------------------------------------

depend dep:  $(OUT)
		touch $(OUT)/GNUmakefile.depend-tmp
		makedepend  -I$(INC_DIR)  -Y  -f$(OUT)/GNUmakefile.depend-tmp  $(C_SRCS)  >/dev/null 2>/dev/null
		cat  $(OUT)/GNUmakefile.depend-tmp  |  sed 's/src\//$(OUT)\//'  >  $(OUT)/GNUmakefile.depend
		rm -f  $(OUT)/GNUmakefile.depend-tmp.bak  $(OUT)/GNUmakefile.depend-tmp

install:  $(OUT)  $(OUT)/hypersrc  $(INSTALL_DIR)
		@# Change paths in the script Hypersrc.pl according to install dir.
		@
		rm -f $(INSTALL_DIR)/hypersrc
		rm -f $(INSTALL_DIR)/Hypersrc.pl
		rm -f $(INSTALL_DIR)/ctags.pl
		cat  Hypersrc.pl  \
		| sed 's|\$hypersrc = "hypersrc"|\$hypersrc = "${INSTALL_DIR}/hypersrc"|'  \
		| sed 's|\$ctags_pl = "ctags.pl"|\$ctags_pl = "${INSTALL_DIR}/ctags.pl"|'  >  $(INSTALL_DIR)/Hypersrc.pl
		@
		cp  $(OUT)/hypersrc  $(INSTALL_DIR)
		cp  ctags.pl  $(INSTALL_DIR)
		@
		chmod  +rx  $(INSTALL_DIR)/hypersrc
		chmod  +rx  $(INSTALL_DIR)/Hypersrc.pl
		chmod  +rx  $(INSTALL_DIR)/ctags.pl
ifeq ($(USER),root)
		@echo "------------------------------------------------------------------"
		@echo "Installing man page"
		@echo "------------------------------------------------------------------"
		$(INSTALL) -d -m 755 $(INSTALL_OWNER) $(MAN_DIR)
		$(INSTALL) -m 444 $(INSTALL_OWNER) hypersrc.1 $(MAN_DIR)
endif

# Installing is pointless unless $(OUT)/hypersrc was built.
$(INSTALL_DIR): $(OUT) $(OUT)/hypersrc
		@echo "------------------------------------------------------------------"
		@echo "Installing at $(INSTALL_DIR)"
		@echo "------------------------------------------------------------------"
		@
		mkdir -p $(INSTALL_DIR)

# For DEVELOPMENT.
#
install_symlinks: uninstall
		@echo "------------------------------------------------------------------"
		@echo "Installing symlinks at $(INSTALL_DIR)"
		@echo "------------------------------------------------------------------"
		ln -s $${PWD}/$(OUT)/hypersrc $(INSTALL_DIR)/hypersrc
		ln -s $${PWD}/Hypersrc.pl     $(INSTALL_DIR)/Hypersrc.pl
		ln -s $${PWD}/ctags.pl        $(INSTALL_DIR)/ctags.pl

uninstall:
		rm -f $(INSTALL_DIR)/hypersrc
		rm -f $(INSTALL_DIR)/Hypersrc.pl
		rm -f $(INSTALL_DIR)/ctags.pl
ifeq ($(USER),root)
		rm -f $(MAN_DIR)/hypersrc.1
endif

docs doc autodoc autodocs:
	@echo Auto-generating HTML docs...
	@./autogen_docs_html.sh $(AUTO_DOC_DIR) >/dev/null 2>/dev/null
	@echo --------------------------------------------------------
	@echo HTML documents auto-generated at ./$(AUTO_DOC_DIR)/
	@echo --------------------------------------------------------

clean: 
# Don't remove C files if SRC and OUT are the same!
ifneq ($(OUT),$(SRC))
ifneq ($(OUT),$(SRCLIB))
		rm -f  $(OUT)/*.[ch]
endif
endif
		rm -f  $(OUT)/*.[oa]  $(OUT)/$(PROGNAME)
		rm -f  $(OUT)/*core* core core.* $(OUT)/*gmon* *.gmon *.gmon gmon*
		rm -f  $(OUT)/y.tab.[cho]  $(OUT)/lex.yy.[cho]

distclean realclean clean-all: clean
		rm -f  $(OUT)/*  makefile  GNUmakefile.config
		rm -f  TAGS  tags  $(SRC)/TAGS  $(SRC)/tags
		rm -f  $(SRC)/*~   $(SRC)/.#*
		rm -f  $(SRC)/h/*~ $(SRC)/h/.#*
		if [ -d $(OUT) ] ; then rmdir $(OUT); fi
		rm -rf $(AUTO_DOC_DIR)

-include $(OUT)/GNUmakefile.depend
