# Makefile for lib$(LIBNAME)
# Help with 'make help'

# PREFIX: installation prefix
# CC: C-Compiler
# CF_DEFINE: Preprocessor definitions
# CF_LIB: extra libraries
# LIBNAME: library name without 'lib'
# CONF_SCRIPT: configure script for library
##PREFIX##
##CC##
##CF_DEFINE##
##CF_LIB##
##LIBNAME##
##CONF_SCRIPT##

CFLAGS = -W -Wall -O2 -fsigned-char -g

#SOFLAGS = -KPIC
SOFLAGS = -fPIC
#SHAREDFLAG = -G
SHAREDFLAG = -shared
DYNAMICFLAG = -Wl,-export-dynamic

RANLIB = ar
INCDIR = $(PREFIX)/include
LIBDIR = $(PREFIX)/lib
BINDIR = $(PREFIX)/bin
STRIXMOD = $(LIBDIR)/strixmod


# H_DAT: .h-files, which are gathered into $(LIBNAME).h
H_DAT = ##HDAT##

# OBJ: object files
OBJ = ##ODAT##

# U_DIR: subdirectories
U_DIR = ##UDIR##

# U_OBJ: object files for subdirectories
U_OBJ = ##UODAT##


all: a so

install:
	mkdir -p $(INCDIR) || true
	mkdir -p $(LIBDIR) || true
	mkdir -p $(BINDIR) || true
	mkdir -p $(STRIXMOD) || true
	test ! -f $(LIBNAME).h && true \
	  || { cp $(LIBNAME).h $(INCDIR)/ \
	       && chmod 644 $(INCDIR)/$(LIBNAME).h; }
	test ! -f lib$(LIBNAME).a && true \
	  || { cp lib$(LIBNAME).a $(LIBDIR)/ \
	       && chmod 644 $(LIBDIR)/lib$(LIBNAME).a; }
	test ! -f $(LIBNAME).so && true \
	  || { rm -f $(LIBDIR)/$(LIBNAME).so; cp $(LIBNAME).so $(LIBDIR)/; }
	test ! -f lib$(LIBNAME).so.* && true \
	  || sh ./_helper/so_install lib$(LIBNAME).so.* "$(LIBDIR)"
	test ! -f $(CONF_SCRIPT) && true \
	  || { rm -f $(BINDIR)/$(CONF_SCRIPT); \
	       cp $(CONF_SCRIPT) $(BINDIR)/ \
	       && chmod 755 $(BINDIR)/$(CONF_SCRIPT); }

uninstall:
	test -d prog && cd prog && $(MAKE) uninstall || true
	test -d strixmod && cd strixmod && $(MAKE) uninstall || true
	rm -f $(LIBDIR)/lib$(LIBNAME).so $(LIBDIR)/lib$(LIBNAME).so.*
	rm -f $(LIBDIR)/lib$(LIBNAME).a $(LIBDIR)/$(LIBNAME).so
	rm -f $(INCDIR)/$(LIBNAME).h
	rm -f $(BINDIR)/$(CONF_SCRIPT)
	rmdir $(STRIXMOD) 2>/dev/null || true

clean: uverz-clean
	test -d prog && cd prog && $(MAKE) clean || true
	test -d strixmod && cd strixmod && $(MAKE) clean || true
	rm -f *.o $(LIBNAME).h config.h config.log
	rm -f lib$(LIBNAME).a $(LIBNAME).so lib$(LIBNAME).so lib$(LIBNAME).so.*
	test -d _helper && cd _helper && $(MAKE) CC=$(CC) clean || true
	rm -f $(CONF_SCRIPT) Makefile

_helper.d:
	cd _helper && $(MAKE) CC=$(CC) all

strix-modules:
	cd strixmod && $(MAKE) all install

programs:
	cd prog && $(MAKE) all install


help:
	@echo "+++++++++++++++++++++++++++++"
	@echo "Makefile targets:"
	@echo " - all:            create static and dynamic library"
	@echo "   - a:            create static library"
	@echo "   - so:           create dynamic library (increments minor-version)"
	@echo " - somaior:        create dynamic library (increments maior-version)"
	@echo " - dl:             create dynamic library for dlopen()"
	@echo " - install:        install library"
	@echo " - strix-modules:  create and install Strix standard modules"
	@echo " - programs:       create and install programs"
	@echo " - uninstall:      uninstall all (library, modules and programs)"
	@echo " - clean:          clean all (library, modules and programs)"
	@echo
	@echo "PREFIX=$(PREFIX)"
	@echo "CC=$(CC)"
	@echo "Library name: $(LIBNAME)"
	@echo "Configure-script: $(CONF_SCRIPT)"
	@echo
	@echo "strixmod/:  Strix standard modules"
	@echo "prog/:      programs"
	@echo "+++++++++++++++++++++++++++++"
	@true


# *** library ***

# static library
a: uverz-all $(OBJ) $(LIBNAME).h
	$(RANLIB) rcs lib$(LIBNAME).a $(OBJ) $(U_OBJ)

# dynamic library for dlopen()
dl: uverz-all $(OBJ) $(LIBNAME).h
	$(CC) $(CFLAGS) $(SOFLAGS) $(SHAREDFLAG) $(OBJ) $(U_OBJ) $(CF_LIB) -lc -o $(LIBNAME).so

# dynamic library increments minor-version
so: _helper.d uverz-all $(OBJ) $(LIBNAME).h
	rm -f lib$(LIBNAME).so.*
	$(CC) $(CFLAGS) $(SOFLAGS) $(SHAREDFLAG) -Wl,-h,`./_helper/so_name "lib$(LIBNAME).so" "$(LIBDIR)" minor1` $(OBJ) $(U_OBJ) $(CF_LIB) -lc -o `./_helper/so_name "lib$(LIBNAME).so" "$(LIBDIR)" minor2`

# dynamic library increments maior-version
somaior: _helper.d uverz-all $(OBJ) $(LIBNAME).h
	rm -f lib$(LIBNAME).so.*
	$(CC) $(CFLAGS) $(SOFLAGS) $(SHAREDFLAG) -Wl,-h,`./_helper/so_name "lib$(LIBNAME).so" "$(LIBDIR)" maior1` $(OBJ) $(U_OBJ) $(CF_LIB) -lc -o `./_helper/so_name "lib$(LIBNAME).so" "$(LIBDIR)" maior2`


# *** $(LIBNAME).h ***

$(LIBNAME).h: $(H_DAT)
	@rm -f $(LIBNAME).h
	@(echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; echo) >> $(LIBNAME).h
	@for hdat in config.h $(H_DAT); \
	do \
	  (echo "/* #######"; echo " * ####### $$hdat #######"; echo " * ####### */"; echo) >> $(LIBNAME).h; \
	  cat $$hdat | grep -v '#include  *"' >> $(LIBNAME).h; \
	  echo >> $(LIBNAME).h; \
	done
	@(echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif') >> $(LIBNAME).h


# *** config.h ***

config.h:
	@echo "*** execute config.sh ***"
	@false


# *** subdirectories ***

uverz-all:
	@for uvz in $(U_DIR); \
	do \
	  (cd $$uvz && $(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS)" SOFLAGS="$(SOFLAGS)" CF_DEFINE="$(CF_DEFINE)" all); \
	done

uverz-clean:
	@for uvz in $(U_DIR); \
	do \
	  (cd $$uvz && $(MAKE) clean); \
	done


# *** object files ***

##ODEP## $(CC) $(CFLAGS) $(SOFLAGS) $(CF_DEFINE) -c
