# This line is needed on some machines.
@SET_MAKE@

# Don't change this line.  Define EXTRALIBS before this line if you
# wish to add any libraries.
LIBS=@LIBS@ $(EXTRALIBS)

# This is normall the install program in the source directory
INSTALL=@INSTALL@

SRCDIR=@srcdir@
VPATH=@srcdir@

# These are used while compiling
LIBDIR_SRC = @LIBDIR@
TMP_BINDIR = @BINDIR@
TMP_BUILDDIR = @BUILDDIR@
TMP_DOCDIR = @DOCDIR@
TMP_LIBDIR = $(TMP_BUILDDIR)/lib

prefix = @prefix@
exec_prefix = $(prefix)/bin
lib_prefix = $(prefix)/lib/pike

#
# use bison please, yacc isn't good enough.
#
YACC=@YACC@
YFLAGS=-d -v

# If you don't have 'strchr', then add next flag to CFLAGS.
# -Dstrchr=index -Dstrrchr=rindex
#
PROFIL=
#PROFIL=-pg

#
# OS dependant flags
#
OSFLAGS=

#
#Enable warnings from the compiler, if wanted.
#
WARN=@WARN@

#add extra defines here
# Be sure to use -g  when looking for bugs
DEFINES=@CPPFLAGS@

# -O should work with all compilers
OPTIMIZE=@OPTIMIZE@

# Preprocessor flags.
PREFLAGS=-I. -I$(SRCDIR) $(DEFINES)
OTHERFLAGS=@CFLAGS@ @CPPFLAGS@ $(OSFLAGS) $(OPTIMIZE) $(WARN) $(PROFIL)
NOOPTFLAGS=@CFLAGS@ @CPPFLAGS@ $(OSFLAGS) $(WARN) $(PROFIL)
CFLAGS=$(PREFLAGS) $(OTHERFLAGS)

CC=@CC@
CPP=@CPP@

AR=@AR@

LD=$(CC)
LDFLAGS=$(CFLAGS) @LDFLAGS@ @LINKFORSHARED@

RUNPIKE=$(TMP_BUILDDIR)/pike -m $(TMP_LIBDIR)/master.pike $(PIKEOPTS)

MOST_MAKE_FLAGS = "prefix=$(prefix)" "exec_prefix=$(exec_prefix)" "CC=$(CC)" "TMP_BINDIR=$(TMP_BINDIR)" "DEFINES=$(DEFINES)" "TMP_LIBDIR=$(TMP_LIBDIR)" "RUNPIKE=$(RUNPIKE)" "INSTALL=$(INSTALL)" "NOOPTFLAGS=$(NOOPTFLAGS)" "AR=$(AR)"
MAKE_FLAGS = $(MOST_MAKE_FLAGS) "OTHERFLAGS=$(OTHERFLAGS)" 

# Add alloca.o if you don't have alloca() on your machine.
# Add ualarm.o if you don't have ualarm() on your machine.
#
OBJ= \
 language.o \
 constants.o \
 cyclic.o \
 array.o \
 backend.o \
 builtin_functions.o \
 callback.o \
 encode.o \
 docode.o \
 dynamic_buffer.o \
 dynamic_load.o \
 error.o \
 fd_control.o \
 fsort.o \
 gc.o \
 hashtable.o \
 las.o \
 lex.o \
 multiset.o \
 signal_handler.o \
 pike_types.o \
 main.o \
 mapping.o \
 pike_memory.o \
 module.o \
 module_support.o \
 object.o \
 opcodes.o \
 operators.o \
 peep.o \
 port.o \
 program.o \
 rusage.o \
 stralloc.o \
 stuff.o \
 threads.o \
 version.o \
 svalue.o @EXTRA_OBJS@

#
# User callable targets
#


all: module_objects
	$(MAKE) $(MAKE_FLAGS) pike

pike: $(OBJ) compile_interpret modules/linker_options
	-@mv pike pike.old 2>/dev/null || true
	$(LD) $(LDFLAGS) $(OBJ) interpret.o `cat modules/linker_options` $(LIBS) -o pike

# purify
pure: $(OBJ) compile_interpret module_objects
	-@mv pike pike.old 2>/dev/null || true
	purify -free-queue-length=500 -inuse-at-exit=yes -chain-length=12 gcc $(LDFLAGS) $(OBJ) interpret.o `cat modules/linker_options` $(LIBS) -o pike

# purecov
cover: $(OBJ) compile_interpret module_objects
	-@mv pike pike.old 2>/dev/null || true
	purecov purify -free-queue-length=500 -inuse-at-exit=yes -chain-length=12 gcc $(LDFLAGS) $(OBJ) interpret.o `cat modules/linker_options` $(LIBS) -o pike

# quantify
quant: $(OBJ) compile_interpret module_objects
	-@mv pike pike.old 2>/dev/null || true
	quantify gcc $(LDFLAGS) $(OBJ) interpret.o `cat modules/linker_options` $(LIBS) -o pike

# Several optimizers have problems with interpret.c
# First try compiling with optimization and if that doesn't work, without.
compile_interpret : force
	$(MAKE) $(MAKE_FLAGS) interpret.o || NO_ULIMIT=yes $(MAKE) $(MOST_MAKE_FLAGS) "OTHERFLAGS=$(NOOPTFLAGS)" interpret.o

force :

# install
install:
	@echo
	@echo Installing Pike...
	@echo
	@for d in "$(prefix)" "$(exec_prefix)" "$(prefix)/lib" "$(lib_prefix)" "$(prefix)/include" "$(prefix)/include/pike"; do if [ \! -d "$$d/." ]; then echo "$$d"; mkdir "$$d"; chmod 755 "$$d"; else : ; fi; done
	@for d in `cd "$(TMP_LIBDIR)"; find . -type d -print`; do if [ \! -d "$(lib_prefix)/$$d/." ]; then echo "$(lib_prefix)/$$d"; mkdir "$(lib_prefix)/$$d"; chmod 755 "$(lib_prefix)/$$d/."; else : ; fi; done
	@if [ -f "$(exec_prefix)/pike" ]; then \
	  echo Renaming old "$(exec_prefix)/pike" to "$(exec_prefix)/pike.old"; \
	  mv -f "$(exec_prefix)/pike" "$(exec_prefix)/pike.old"; \
	else : ; fi
	@echo "$(exec_prefix)/pike"; $(INSTALL) ./pike $(exec_prefix)
	@if [ -f $(SRCDIR)/../bin/hilfe ]; then \
	  echo "$(exec_prefix)/hilfe"; \
	  if [ "$(exec_prefix)" != "/usr/local/bin" -a \
	       "$(exec_prefix)" != "/usr/local/bin/" ]; then \
	    if [ -f $(exec_prefix)/hilfe ]; then \
	      mv $(exec_prefix)/hilfe $(exec_prefix)/hilfe.old; \
	    else : ; fi && \
	    sed -e 's!/usr/local/bin!$(exec_prefix)!' <$(SRCDIR)/../bin/hilfe \
	      >$(exec_prefix)/hilfe && \
	    chmod 755 $(exec_prefix)/hilfe; \
	  else $(INSTALL) $(SRCDIR)/../bin/hilfe $(exec_prefix); fi; \
	else : ; fi
	@for a in $(SRCDIR)/*.h *.h ; do $(INSTALL) $$a $(prefix)/include/pike ; done
	@for f in `cd "$(TMP_LIBDIR)"; find . -type f -print`; do echo "$(lib_prefix)/$$f"; if [ -f "$(lib_prefix)/$$f" ]; then mv -f "$(lib_prefix)/$$f" "$(lib_prefix)/$$f.old"; else : ; fi; cp "$(TMP_LIBDIR)/$$f" "$(lib_prefix)/$$f"; done

# tidy up a bit
tidy:
	-rm -f *.o core y.output y.tab.c y.tab.h
	-rm -f $(TMP_BINDIR)/core *.o *.i *.i~ testsuite

# make clean
clean: tidy
	-( cd modules; ${MAKE} $(MAKE_FLAGS) clean )
	-rm -f TAGS tags
	-rm -f yacc.acts yacc.debug yacc.tmp *.debug.log a.out

# make _really_ clean
spotless: clean
	rm -f Makefile machine.h
	find . -type f '(' -name '*~' -o -name '.*~' -o -name core -o -name '.nfs*' -name '#*#' ')' -print | xargs rm -f
	-rm -f lexical.c mon.out *.ln config.status
	-rm -f $(TMP_BINDIR)/*~ $(TMP_BINDIR)/#*#
	-rm -f pike pike.old l.outa*
	-rm -f modules/*/testsuite
	-rm -rf lib

# create tags
tags:
	ctags *.c

TAGS:
	etags -t *.h *.c

# verify / debug
verify_modules:
	( cd modules ; $(MAKE) $(MAKE_FLAGS) verify )

verbose_verify_modules:
	( cd modules ; $(MAKE) $(MAKE_FLAGS) verbose_verify )

verify: testsuite verify_modules
	$(RUNPIKE) $(TMP_BINDIR)/test_pike.pike testsuite

check: verify

sure: verify

# verify / debug verbose
verbose_verify: testsuite verbose_verify_modules
	$(RUNPIKE) $(TMP_BINDIR)/test_pike.pike testsuite --verbose

# verify & debug VERBOSE
gdb_verify: testsuite
	@echo >.gdbinit handle SIGFPE nostop noprint pass
	@echo >>.gdbinit handle SIGUSR1 nostop noprint pass
	@echo >>.gdbinit run -Mmodules -DNOT_INSTALLED -m $(TMP_LIBDIR)/master.pike $(PIKEOPTS) $(TMP_BINDIR)/test_pike.pike testsuite -v -v -f
	gdb ./pike
	@rm .gdbinit

# run hilfe, for interactive testing
run_hilfe:
	$(RUNPIKE) $(TMP_BINDIR)/hilfe

feature_list:
	$(RUNPIKE) $(TMP_BINDIR)/feature_list

lib: $(LIBDIR_SRC) $(LIBDIR_SRC)/master.pike $(LIBDIR_SRC)/modules $(LIBDIR_SRC)/include
	cp -r $(LIBDIR_SRC) .
	-touch ./lib

# make export archive (requires compiled Pike)
# Do not compile in source tree if you want to use this!
# Beware that export archive this includes bison/yacc/byacc source
# and thus has to follow the rules stated in that code.
export: new_peep_engine $(SRCDIR)/language.c $(SRCDIR)/language.h depend
	chmod +x $(SRCDIR)/install-sh
	$(RUNPIKE) $(TMP_BINDIR)/export.pike Odbc Oracle _Crypto

full_export: new_peep_engine $(SRCDIR)/language.c $(SRCDIR)/language.h depend
	chmod +x $(SRCDIR)/install-sh
	$(RUNPIKE) $(TMP_BINDIR)/export.pike

bin_export:
	PATH=$(TMP_BINDIR)/fakes:$$PATH INSTALL_LOG=`pwd`/log make install INSTALL=install
	$(RUNPIKE) $(TMP_BINDIR)/parse_install_log <log $(SRCDIR) $(prefix)

new_peep_engine:
	$(RUNPIKE) $(TMP_BINDIR)/mkpeep.pike $(SRCDIR)/peep.in >$(SRCDIR)/peep_engine.c

$(SRCDIR)/peep_engine.c: $(SRCDIR)/peep.in
	echo "" >$(SRCDIR)/peep_engine.c
	-$(RUNPIKE) $(TMP_BINDIR)/mkpeep.pike $(SRCDIR)/peep.in >$(SRCDIR)/peep_engine.c

peep.o: $(SRCDIR)/peep_engine.c

# make dependencies
depend: $(SRCDIR)/language.c
	gcc -MM $(PREFLAGS) $(SRCDIR)/*.c | $(TMP_BINDIR)/fixdepends.sh $(SRCDIR)
	( cd modules ; $(MAKE) $(MAKE_FLAGS) depend )
	$(SRCDIR)/configure

docs:
	mkdir docs

html_docs: docs
	$(RUNPIKE) $(TMP_BINDIR)/htmlify_docs docs $(TMP_DOCDIR) `echo $(SRCDIR)/modules/*/doc`

lobotomize_crypto:
	( cd $(SRCDIR)/modules/_Crypto ; ./.build_lobotomized_crypto  )
	( cd $(SRCDIR)/modules/_Lobotomized_Crypto ; autoconf )
#
# Pike internal targets
#
module_objects: machine.h global.h modules/static_module_makefile modules/dynamic_module_makefile $(SRCDIR)/language.h lib
	( cd modules ; ${MAKE} $(MAKE_FLAGS) )

language.o: $(SRCDIR)/language.c $(SRCDIR)/object.h $(SRCDIR)/interpret.h $(SRCDIR)/program.h

$(SRCDIR)/language.h: $(SRCDIR)/language.yacc
	@echo "Expect 1 shift/reduce conflict."
	$(YACC) $(YFLAGS) $(SRCDIR)/language.yacc
	mv y.tab.c $(SRCDIR)/language.c
	mv y.tab.h $(SRCDIR)/language.h

$(SRCDIR)/language.c: $(SRCDIR)/language.h
	touch $(SRCDIR)/language.c

# Internal testing target
run_yacc: $(SRCDIR)/language.c

modules/modlist.h:
	( cd modules ; ${MAKE} $(MAKE_FLAGS) )

modules/modlist_headers.h: modules/modlist.h
	touch modules/modlist_headers.h

module.o:  modules/modlist_headers.h modules/modlist.h

$(SRCDIR)/configure: $(SRCDIR)/configure.in
	cd $(SRCDIR) && autoconf

config.status: $(SRCDIR)/configure
	./config.status --recheck

modules/static_module_makefile: $(SRCDIR)/modules/static_module_makefile.in config.status
	CONFIG_FILES=modules/static_module_makefile CONFIG_HEADERS="" ./config.status

modules/dynamic_module_makefile: $(SRCDIR)/@dmmsrc@ config.status
	CONFIG_FILES=modules/dynamic_module_makefile:@dmmsrc@ CONFIG_HEADERS="" ./config.status

Makefile: $(SRCDIR)/Makefile.in $(SRCDIR)/dependencies config.status
	CONFIG_FILES=Makefile CONFIG_HEADERS="" ./config.status
	@echo "Run make again"
	@exit 1

$(SRCDIR)/machine.h.in: $(SRCDIR)/stamp-h.in
$(SRCDIR)/stamp-h.in: $(SRCDIR)/configure.in $(SRCDIR)/acconfig.h
	cd $(SRCDIR) && autoheader
	echo foo > $(SRCDIR)/stamp-h.in

machine.h: stamp-h
stamp-h: $(SRCDIR)/machine.h.in config.status
	CONFIG_FILES="" CONFIG_HEADERS=machine.h ./config.status

testsuite: $(SRCDIR)/testsuite.in
	$(TMP_BINDIR)/mktestsuite $(SRCDIR)/testsuite.in >testsuite

@dependencies@
