# master make rules (gnu make) for building Mastodon system libraries
#

# default build format is a.out
#
ifndef FORMAT
FORMAT=aout
endif

# object directories
#
STATICDIR=obj
SHAREDDIR=shared
DEBUGDIR =debug

# translate SRCS into our three build types (static,shared,static+debug)
#
_OBJS=$(patsubst %.c,%.o,$(SRCS))
__OBJS=$(patsubst %.cc,%.o,$(_OBJS))

STATIC=$(patsubst %,$(STATICDIR)/%,$(__OBJS))
SHARED=$(patsubst %,$(SHAREDDIR)/%,$(__OBJS))
DEBUG =$(patsubst %,$(DEBUGDIR)/%,$(__OBJS))

ifdef LIBNAME
# where libraries go
#
ifndef LIBDIR
LIBDIR=../lib

ifndef INSTALL_LIB
INSTALL_LIB=$(DESTDIR)/usr/lib
endif

endif
LFLAGS += -L$(LIBDIR)

# build library names from LIBNAME
#
ifndef MAJOR
MAJOR=1
endif
ifndef MINOR
MINOR=0
endif
ifeq ($(FORMAT),elf)
SHAREDLIB=$(LIBDIR)/lib$(LIBNAME).so.$(MAJOR).$(MINOR)
endif
STATICLIB=$(LIBDIR)/lib$(LIBNAME).a
DEBUGLIB= $(LIBDIR)/lib$(LIBNAME)_g.a

ALL_LIBS  = build-static build-debug
ifdef LIBNAME
ifeq ($(FORMAT),elf)
ALL_LIBS += build-shared
endif
endif

TARGETS=$(ALL_LIBS)

endif

# rules to build objects (Mastodon-specific; elf is the native
# elf format, aout is the native aout format (if it exists))
#
$(STATICDIR) $(SHAREDDIR) $(DEBUGDIR):
	@[ -d $@ ] || mkdir -p $@

# rules to build shared objects from .c and .cc files
#
$(SHAREDDIR)/%.o: %.c
ifeq ($(FORMAT),elf)
	$(CC) $(CFLAGS) -fPIC -o $(SHAREDDIR)/$*.o -c $*.c
else
	$(CC) $(CFLAGS) -S -o $*.S -c  $*.c
	jumpas -l$(LIBNAME) -j`cd $(SHAREDDIR);pwd` -o $(SHAREDDIR)/$*.o $*.S
	rm -f $*.S
endif

$(SHAREDDIR)/%.o: %.cc
ifeq ($(FORMAT),elf)
	$(CC) $(CFLAGS) -fPIC -o $(SHAREDDIR)/$*.o -c $*.cc
else
	$(CC) $(CFLAGS) -S -o $*.S -c  $*.cc
	jumpas -l$(LIBNAME) -j`cd $(SHAREDDIR);pwd` -o $(SHAREDDIR)/$*.o $*.S
	rm -f $*.S
endif


# rules to build .o files from .c and .cc files (static and debug)
#
$(STATICDIR)/%.o:  %.cc
	$(CC) $(CFLAGS) -o $(STATICDIR)/$*.o -c $*.cc

$(STATICDIR)/%.o:  %.c
	$(CC) $(CFLAGS) -o $(STATICDIR)/$*.o -c $*.c

$(DEBUGDIR)/%.o:  %.cc
	$(CC) $(CFLAGS) -g -o $(DEBUGDIR)/$*.o -c $*.cc

$(DEBUGDIR)/%.o:  %.c
	$(CC) $(CFLAGS) -g -o $(DEBUGDIR)/$*.o -c $*.c


ifdef LIBNAME

# rules to build the libraries
#

$(LIBDIR):
	@[ -d $(LIBDIR) ] || mkdir -p $(LIBDIR)

build-static: $(LIBDIR) $(STATICDIR) $(STATICLIB)

$(STATICLIB): $(STATIC)
	rm -f $(STATICLIB)
	ar crv $(STATICLIB) $(STATIC)
	ranlib $(STATICLIB)

build-debug: $(LIBDIR) $(DEBUGDIR) $(DEBUGLIB)

$(DEBUGLIB): $(DEBUG)
	rm -f $(DEBUGLIB)
	ar crv $(DEBUGLIB) $(DEBUG)
	ranlib $(DEBUGLIB)

ifeq ($(FORMAT),elf)

SONAME=lib$(LIBNAME).so.$(MAJOR).$(MINOR)

build-shared: $(LIBDIR) $(SHAREDDIR) $(SHAREDLIB)

$(SHAREDLIB): $(SHARED)
	$(LD) -shared -soname $(SONAME) -o $(SHAREDLIB) $(SHARED)
endif

endif


# cleaning rules
#
libclean::
	rm -f $(SHARED) $(STATIC) $(DEBUG)

ifdef LIBNAME
libclean::
	rm -f $(SHAREDLIB) $(STATICLIB) $(DEBUGLIB)
endif


# installation rules
#

ifdef LIBNAME
lib-install: $(STATICLIB) $(SHAREDLIB) $(DEBUGLIB)
	install -m 444 -o root -g root -c $(STATICLIB) $(SHAREDLIB) $(DEBUGLIB) $(INSTALL_LIB)
else
lib-install:
endif

man-install:
ifdef MANPAGES
	for x in $(MANPAGES); do \
	    ext=`echo $$x | sed -e 's/.*\(.\)$$/\1/'`; \
	    install -d -m 755 -o man -g man $(DESTDIR)/usr/man/man$$ext; \
	    install -m 444 -o man -g man -c $$x $(DESTDIR)/usr/man/man$$ext; \
	done
ifdef MLINKS
	set - $(MLINKS); \
	while test $$# -ge 2; do \
	    ext1=`echo $$1 | sed -e 's/.*\(.\)$$/\1/'`; \
	    ext2=`echo $$2 | sed -e 's/.*\(.\)$$/\1/'`; \
	    echo ".so man$$ext1/$$1" > $(DESTDIR)/usr/man/man$$ext2/$$2; \
	    chown man.man $(DESTDIR)/usr/man/man$$ext2/$$2; \
	    chmod 444 $(DESTDIR)/usr/man/man$$ext2/$$2; \
	    shift 2;\
	done
endif
endif

hdr-install:
ifdef HEADERS
	install -m 444 -o root -g root -c $(HEADERS) $(DESTDIR)/usr/include
endif
ifdef HDIR
	install -m 755 -o root -g root -d $(DESTDIR)/usr/include/$(HDIR)
	install -m 444 -o root -g root -c $(HFILES) $(DESTDIR)/usr/include/$(HDIR)
endif
