#
# Copyright (C) 2003 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

SHELL	:= /bin/sh

srcdir	:= @srcdir@
builddir:= @builddir@

PLATFORM:= @PLATFORM@
PYTHON	:= @PYTHON@
SYNOPSIS:= $(PYTHON) synopsis.py
CXX	:= @CXX@
LDSHARED:= @LDSHARED@
LDSONAME:= @LDSONAME@
MAKEDEP	:= $(CXX) -M
AR	:= @AR@
RANLIB	:= @RANLIB@
LN_S	:= @LN_S@
CPPFLAGS:= @CPPFLAGS@ -D SYNOPSIS_SRC -I $(builddir) -I $(srcdir)
CXXFLAGS:= @CXXFLAGS@
LDFLAGS	:= @LDFLAGS@
LIBS	:= @LIBS@
LIBEXT  := @LIBEXT@
INSTALL := @INSTALL@
INSTALL_DATA := @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT := @INSTALL_SCRIPT@
MAJOR	:= @MAJOR@
MINOR	:= @MINOR@

prefix := @prefix@
exec_prefix := @exec_prefix@
datarootdir := @datarootdir@
datadir := @datadir@
includedir := @includedir@
libdir := @libdir@
sbindir := @sbindir@
bindir := @bindir@

ifneq ($(findstring -fPIC, $(CXXFLAGS)),)
GC_CFLAGS := CFLAGS="@CFLAGS@ -fPIC"
endif
GC_LIB	:= @GC_LIB@
ifdef GC_LIB
CPPFLAGS+= -I $(srcdir)/Synopsis/gc/include
endif

define make_dep
@echo generating dependencies for $(@D)/$(<F)
$(SHELL) -ec '$(MAKEDEP) $(CPPFLAGS) $< | sed "s,$(*F)\\.o[ :]*,$*\\.d $*\\.o : ,g" > $@'
endef

define compile
@echo compiling $(@D)/$(@F)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<
endef

ifneq ($(findstring $(PLATFORM), nt cygwin),)
define link_dso
@echo linking $@
$(LDSHARED) $(LDFLAGS) -o $@ $^ $(LIBS)
endef

define install_dso
@echo installing $@
$(INSTALL_PROGRAM) lib/libSynopsis$(LIBEXT) \
  $(DESTDIR)$(libdir)/libSynopsis$(LIBEXT)
endef

else

define link_dso
@echo linking $@
rm -f $@ $@.$(MAJOR) $@.$(MAJOR).$(MINOR)
$(LDSHARED) $(LDFLAGS) $(LDSONAME)$(@F).$(MAJOR).$(MINOR) \
  -o $@.$(MAJOR).$(MINOR) $^ $(LIBS)
(cd $(@D) && $(LN_S) $(@F).$(MAJOR).$(MINOR) $(@F).$(MAJOR))
(cd $(@D) && $(LN_S) $(@F).$(MAJOR) $(@F))
endef

define install_dso
@echo installing libSynopsis$(LIBEXT)
rm -f $(DESTDIR)$(libdir)/libSynopsis$(LIBEXT) \
  $(DESTDIR)$(libdir)/libSynopsis$(LIBEXT).$(MAJOR) \
  $(DESTDIR)$(libdir)/libSynopsis$(LIBEXT).$(MAJOR).$(MINOR)
$(INSTALL_PROGRAM) lib/libSynopsis$(LIBEXT).$(MAJOR).$(MINOR) \
  $(DESTDIR)$(libdir)/libSynopsis$(LIBEXT).$(MAJOR).$(MINOR)
(cd $(DESTDIR)$(libdir) && $(LN_S) libSynopsis$(LIBEXT).$(MAJOR).$(MINOR) libSynopsis$(LIBEXT).$(MAJOR))
(cd $(DESTDIR)$(libdir) && $(LN_S) libSynopsis$(LIBEXT).$(MAJOR) libSynopsis$(LIBEXT))
endef
endif

define link_app
@echo linking $@
$(CXX) -Llib $(LDFLAGS) -o $@ $< -lSynopsis $(LIBS)
endef

SRC	:= Trace \
           PTree/Node PTree/Encoding PTree/operations \
           PTree/Atoms PTree/Lists \
           PTree/Visitor PTree/Display PTree/Writer \
           SymbolLookup/Symbol SymbolLookup/Scope SymbolLookup/Scopes \
           SymbolLookup/Display SymbolLookup/Walker \
           TypeAnalysis/Type TypeAnalysis/Kit \
           TypeAnalysis/TypeEvaluator \
           TypeAnalysis/ConstEvaluator \
           TypeAnalysis/OverloadResolver \
           Buffer Lexer SymbolFactory Parser

SRC 	:= $(patsubst %, Synopsis/%.cc, $(SRC))

SUP	:= $(patsubst %, Support/%.cc, Path ErrorHandler)

HDR	:= $(patsubst $(srcdir)/%, %, $(wildcard $(srcdir)/Synopsis/*.hh))
HDR	+= $(patsubst $(srcdir)/%, %, $(wildcard $(srcdir)/Synopsis/PTree/*.hh))
HDR	+= $(patsubst $(srcdir)/%, %, $(wildcard $(srcdir)/Synopsis/SymbolLookup/*.hh))
HDR	+= $(patsubst $(srcdir)/%, %, $(wildcard $(srcdir)/Synopsis/TypeAnalysis/*.hh))

OBJ	:= $(patsubst %.cc, %.o, $(SRC))
CC_SYN	:= $(patsubst %, %.sxr.syn, $(SRC))
HDR_SYN	:= $(patsubst %, %.syn, $(HDR))
HDR_SXR_SYN:= $(patsubst %, %.sxr.syn, $(HDR))
DEP	:= $(patsubst %.cc, %.d, $(SRC) tools/display-ptree.cc tools/display-symbols.cc)

LIBRARY	:= lib/libSynopsis$(LIBEXT)
SUPPORT := lib/libSupport.a
TOOLS	:= bin/display-ptree bin/display-symbols
TARGETS	:= $(LIBRARY) $(SUPPORT) $(TOOLS)

vpath %.cc $(srcdir)
vpath %.c $(srcdir)
vpath %.h $(srcdir)
vpath %.hh $(srcdir) $(builddir)

all: $(TARGETS)

$(LIBRARY): $(OBJ) $(GC_LIB)
	$(link_dso)

$(SUPPORT): $(patsubst %.cc, %.o, $(SUP))
	$(AR) cru $@ $^
	$(RANLIB) $@

$(TOOLS): bin/%: tools/%.o $(LIBRARY)
	$(link_app)

doc: cxx.syn cxx-sxr.syn

cxx.syn: $(HDR_SYN)
	@echo linking C++ headers together
	$(SYNOPSIS) link --output=$@ $^

cxx-sxr.syn: $(HDR_SXR_SYN) $(CC_SYN)
	@echo linking all C++ files together
	$(SYNOPSIS) link --output=$@ $^

%.d: %.cc
	$(make_dep)

%.o: %.cc
	$(compile)

Makefile: $(srcdir)/Makefile.in
	./config.status --file Makefile

$(HDR_SYN): %.syn: %
	@echo parsing $^
	mkdir -p $(dir $@)
	$(SYNOPSIS) cxx --output=$@ $?

$(HDR_SXR_SYN): %.sxr.syn: %
	@echo parsing $^
	mkdir -p $(dir $@)
	$(SYNOPSIS) cxx_sxr --output=$@ $?

$(CC_SYN): %.cc.sxr.syn: %.cc
	@echo parsing $^
	mkdir -p $(dir $@)
	$(SYNOPSIS) cxx_sxr --output=$@ $?

# This calls make in the gc dir to create the gc.a file
$(GC_LIB):
	$(MAKE) -C Synopsis/gc $(GC_CFLAGS)

install:
	$(INSTALL) -d $(DESTDIR)$(libdir)
	$(install_dso)
	$(INSTALL) -d $(DESTDIR)$(includedir)/Synopsis
	$(INSTALL) -d $(DESTDIR)$(includedir)/Synopsis/PTree
	$(INSTALL) -d $(DESTDIR)$(includedir)/Synopsis/SymbolLookup
	$(INSTALL) -d $(DESTDIR)$(includedir)/Synopsis/TypeAnalysis
	for header in $(HDR); do \
          $(INSTALL_DATA) $(srcdir)/$$header \
            $(DESTDIR)$(includedir)/`dirname $$header`; \
	done
	$(INSTALL) -d $(DESTDIR)$(libdir)/pkgconfig
	$(INSTALL_DATA) synopsis.pc \
          $(DESTDIR)$(libdir)/pkgconfig/synopsis.pc

clean:
	rm -f Synopsis/*~ Synopsis/*.o Synopsis/*.d Synopsis/*.syn \
        Synopsis/PTree/*.~ Synopsis/PTree/*.o Synopsis/PTree/*.d Synopsis/PTree/*.syn \
        Synopsis/SymbolLookup/*.~ Synopsis/SymbolLookup/*.o Synopsis/SymbolLookup/*.d Synopsis/SymbolLookup/*.syn \
        Synopsis/TypeAnalysis/*.~ Synopsis/TypeAnalysis/*.o Synopsis/TypeAnalysis/*.d Synopsis/TypeAnalysis/*.syn \
        tools/*.~ tools/*.o tools/*.d \
        *.dll *.so *core \
	base_lib

ifeq (,$(filter $(MAKECMDGOALS), depend clean distclean))
-include $(DEP)
endif
