
# Makefile for YumaPro External System Library
#

############### SOURCE PROFILE ##############################

#
# to make a real library, copy this directory contents
# to a new location. You should change the example-system
# C and H source files to new names.   E.g.:
#
#    cp -R libsystem $HOME/mycode
#
# The library libyp_show.so will be installed
# if make install is called from a normal build
#
# The library libyp_show-example.so will be installed
# if make install is called from a package build
#
#  SUBDIR_NM=yp_show
#
SUBDIR_NM=yp_show-example
REAL_NAME=libyp_show.so
SUBDIR_CPP=


############### TARGET PROFILE ##############################

TARGET=../bin

LIB_INST=../lib

ifdef MAC
 INSTALL_PREFIX ?= /usr/local
else
 INSTALL_PREFIX ?= /usr
endif

ifdef FREEBSD
REAL_INST=$(DESTDIR)$(INSTALL_PREFIX)/lib/yumapro
else
ifdef LIB64
REAL_INST=$(DESTDIR)$(INSTALL_PREFIX)/lib64/yumapro
else
REAL_INST=$(DESTDIR)$(INSTALL_PREFIX)/lib/yumapro
endif
endif

REALINC=$(DESTDIR)$(INSTALL_PREFIX)/include


#################### MAKE RULES ########################

BLDALL=

ifndef MAC
 ifdef BITBAKE
  ifndef BASIC
BLDALL= sys_dummy sys_lib
  endif # BASIC
 else
BLDALL= sys_dummy sys_lib
 endif # BITBAKE
endif # MAC

all: $(BLDALL)


#################### PLATFORM DEFINITIONS ############

ifdef PACKAGE_BUILD
# libshow is in the source tree
CINC = -I. -I../../netconf/src/platform -I../../netconf/src/ncx\
 -I../../libtecla -I../../netconf/src/agt  -I../../netconf/src/mgr\
 -I../../netconf/src/ycli
else
# assume libshow in the source tree first, then install location
CINC = -I. -I../../netconf/src/platform \
 -I../../netconf/src/ncx -I../../netconf/src/mgr\
 -I../../netconf/src/ycli\
 -I$(REALINC)/yumapro/platform -I$(REALINC)/yumapro/ncx\
 -I$(REALINC)/yumapro/agt -I$(REALINC)/yumapro/mgr\
 -I$(REALINC)/yumapro/ycli

ifndef NO_LIBTECLA
CINC += -I../../libtecla
endif

endif  # PACKAGE_BUILD

CINC += -I$(REALINC) -I$(REALINC)/libxml2 -I$(REALINC)/libxml2/libxml

ifdef MAC
CINC += -I/usr/local/opt/libxml2/include/libxml2 -I/usr/local/opt/libxml2/include/libxml2/libxml
endif

LBASE=../lib

ifdef DESTDIR
OWNER=
else
ifdef MAC
OWNER=-oroot
else
ifdef CYGWIN
OWNER=
else
OWNER= --owner=root
endif
endif
endif

ifdef USE_WERROR
WERROR= -Werror
else
WERROR=
endif

### GCC + [LINUX or MACOSX]

CWARN=-Wall -Wno-long-long -Wformat-y2k -Winit-self \
	-Wswitch-default -Wunused-parameter \
	-Wextra -Wundef -Wshadow -Wpointer-arith \
	-Wwrite-strings -Wbad-function-cast -Wcast-qual \
	-Waggregate-return -Wstrict-prototypes -Wold-style-definition \
	-Wmissing-prototypes -Wmissing-declarations \
	-Wpacked -Winvalid-pch \
	-Wredundant-decls -Wnested-externs -Winline -std=gnu99 $(WERROR)


ifdef BITBAKE
   CDEFS += -DBITBAKE=1
endif

ifdef BITBAKE
  CCPP = $(CXX)
endif

ifdef COVERITY
  CC = gcc
endif

ifdef WINDOWS
  CC ?= i586-mingw32msvc-gcc
  CCPP ?= i586-mingw32msvc-g++
  LINK ?= i586-mingw32msvc-g++
  RANLIB ?= i586-mingw32msvc-ranlib
else
 ifdef CROSS_TARGET
   CC ?= $(CROSS_TARGET)gcc
   CCPP ?= $(CROSS_TARGET)g++
    AR = $(CROSS_TARGET)ar
    RANLIB = $(CROSS_TARGET)ranlib
    LD = $(CROSS_TARGET)ld
 else
  ifdef UCLINUX
    CC = $(CROSS_COMPILE_APPS)gcc
    CCPP = $(CROSS_COMPILE_APPS)g++
    AR = $(CROSS_COMPILE_APPS)ar
    RANLIB = $(CROSS_COMPILE_APPS)ranlib
    LD = $(CROSS_COMPILE_APPS)ld
  else
   ifdef PACKAGE_BUILD
    CC ?= gcc
    CCPP ?= g++
   else
    ifdef GPROFILE
     CC ?= gcc
     CCPP ?= g++
    else
     ifdef CLANG
     CC ?= clang
     else
     CC ?= gcc
     CCPP ?= g++
     endif # CLANG
    endif # GPROFILE
   endif # PACKAGE_BUILD
  endif # UCLINUX
 endif # CROSS_TARGET
endif # WINDOWS

LINK ?= $(CCPP)
AR ?= $(CROSS_TARGET)ar
RANLIB ?= $(CROSS_TARGET)ranlib

# -Wunreachable-code removed due to -O3
# -O3 changed to -O2 due to code bloat from inline functions

CDEFS=-DDEBUG=1 -DLINUX=1 -DGCC=1

ifndef NOFLOAT
  CDEFS += -DHAS_FLOAT=1
endif

# production (0) or debug (1) build
ifdef DEBUG
  CPPFLAGS+=-ggdb3
else
  CPPFLAGS+=-O2
endif

CFLAGS+=$(CDEFS) $(CWARN)

ifndef CYGWIN
  CPPFLAGS += -fPIC
endif

# memory leak debugging mode
ifdef MEMTRACE
  CPPFLAGS += -DMEMORY_DEBUG=1
endif

ifdef RELEASE
  CPPFLAGS += -DRELEASE=$(RELEASE)
endif

ifdef LIB64
   CPPFLAGS += -DLIB64=1
endif

ifdef MAC
   GRP=
else
ifdef DESTDIR
   GRP=
else
ifdef CYGWIN
   GRP=
else
   GRP=--group=root
endif # CYGWIN
endif # DESTDIR
endif # MAC

ifdef STATIC
LIBSUFFIX=a
else
 ifdef MAC
LIBSUFFIX=dylib
 else
LIBSUFFIX=so
 endif # MAC
endif # STATIC

LFLAGS=-lm
LPATH=-L$(LBASE)

CEES = $(wildcard *.c)

CPEES = $(wildcard *.cpp)

HEES = $(wildcard *.h)

################ OBJS RULE #############
OBJS = $(patsubst %.c,$(TARGET)/%.o,$(CEES))

OBJS += $(patsubst %.cpp,$(TARGET)/%.o,$(CPEES))

################ DEPS RULE #############
DEPS = $(patsubst %.c,%.D,$(wildcard *.c))

DEPS += $(patsubst %.cpp,%.D,$(wildcard *.cpp))

######################## PLATFORM DEFINITIONS #############
PLATFORM_CPP=

CXXFLAGS=-std=c++0x

######################### MAKE DEPENDENCIES ###############
COMPILE.c= $(CC) $(CFLAGS) $(CPPFLAGS) $(PLATFORM_CPP) \
           $(CINC) $(SUBDIR_CPP) $(TARGET_ARCH) -c


$(TARGET)/%.o: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $(PLATFORM_CPP) \
        $(CINC) $(SUBDIR_CPP) $(TARGET_ARCH) -c -o $@ $<

$(TARGET)/%.o: %.cpp
	$(CCPP) $(CXXFLAGS) $(CPPFLAGS) $(PLATFORM_CPP) \
        $(CINC) $(SUBDIR_CPP) $(TARGET_ARCH) -c -o $@ $<



# Common library rule

$(LBASE)/lib%.a: $(OBJS)
	$(AR) cr $@ $(OBJS)
	$(RANLIB) $@

# dependency rule to make temp .D files from .c sources
# all the .D files are collected and appended to the
# appropriate Makefile when 'make depend' is run
# this rule is kept here to make sure it matches COMPILE.c
%.D: %.c
	$(CC) $(CFLAGS) -MM -MG -MT $(TARGET)/$(patsubst %.c,%.o,$<) \
	-Wall -Wcomment $(CPPFLAGS) $(PLATFORM_CPP) $(CINC) \
	$(SUBDIR_CPP) $(TARGET_ARCH) -c $< > $@

%.D: %.cpp
	$(CCPP) $(CXXFLAGS) -MM -MG -MT $(TARGET)/$(patsubst %.cpp,%.o,$<) \
	-Wall -Wcomment $(CPPFLAGS) $(PLATFORM_CPP) $(CINC) \
	$(SUBDIR_CPP) $(TARGET_ARCH) -c $< > $@


################## MAKE DEPENDENCIES #####################
# following depend rule is the GNU version! Other versions TBD
depend: dependencies

dependencies: $(DEPS)
	@if [ ! -f Makefile ]; then \
	    echo "Error: Makefile missing!"; \
	    exit 1; \
	fi
	@rm -f dependencies
	@for i in $(DEPS); do \
	  if [ -f $$i ] ; then \
	    (cat $$i >> dependencies; echo "" >> dependencies) ; \
	  else \
	    (echo "*** Warning: Dependency file $i.D is missing! (Skipping...) ***"; \
	     echo "# Warning: Missing file $$i !!!") ; \
	  fi; \
	done
	@echo "" >> dependencies
# delete the .D files to force make depend to rebuild them each time
# that target is built
#	@rm -f $(DEPS)


################ DEPENDENCIES #########################
# depend rule must be included after the 'all' make rule

ifdef PACKAGE_BUILD
# force .so build in debian and RPM releases
SYS_LIB=$(LIB_INST)/lib$(SUBDIR_NM).so
else
SYS_LIB=$(LIB_INST)/lib$(SUBDIR_NM).$(LIBSUFFIX)
endif # PACKAGE_BUILD


test:


# install the example version
install:
ifndef MAC
 ifndef STATIC
  ifdef BITBAKE
   ifndef BASIC
	mkdir -p $(REAL_INST)
	install $(SYS_LIB) $(REAL_INST)
   endif # BASIC
  else
	mkdir -p $(REAL_INST)
	install $(SYS_LIB) $(REAL_INST)
  endif # BITBAKE
 endif  # STATIC
endif # MAC

uninstall:
ifndef MAC
 ifndef STATIC
	rm -f $(REAL_INST)/lib$(SUBDIR_NM).so
 endif  # STATIC
endif # MAC

real-install:
ifndef STATIC
	mkdir -p $(REAL_INST)
	install $(SYS_LIB) $(REAL_INST)/$(REAL_NAME)
endif  # STATIC

real-uninstall:
ifndef STATIC
	rm -f $(REAL_INST)/$(REAL_NAME)
endif  # STATIC


sys_lib: $(SYS_LIB)


# this dummy rule keeps make from deleting the $(OBJS) as
# intermediate files
sys_dummy: dependencies $(OBJS)

clean:
	rm -f $(OBJS) $(LIB_INST)/lib$(SUBDIR_NM).*

superclean:
	rm -f *~ $(DEPS) dependencies $(OBJS) $(LIB_INST)/lib$(SUBDIR_NM).*

ifdef WINDOWS
RDYNAMIC=
else
 ifdef CYGWIN
RDYNAMIC=
 else
RDYNAMIC=-rdynamic
 endif
endif

$(LIB_INST)/lib$(SUBDIR_NM).so: $(OBJS)
ifdef FREEBSD
	$(CC) $(CFLAGS) -shared $(RDYNAMIC) -Wl,-soname,lib$(SUBDIR_NM).so -o $@ $(OBJS)
else
	$(CC) $(CFLAGS) -shared $(RDYNAMIC) -Wl,-soname,lib$(SUBDIR_NM).so -o $@ $(OBJS)
endif

$(LBASE)/lib$(SUBDIR_NM).dylib: $(OBJS)
	$(CC) $(CFLAGS) -shared -dynamiclib -std=gnu99 -current_version 1.0 \
	-undefined dynamic_lookup \
	-o $@ -install_name lib$(SUBDIR_NM).dylib $(OBJS) -lxml2

.PHONY: all superclean clean test install uninstall \
        distclean depend sys_lib sys_dummy real-install \
	real-uninstall


# prevent the make program from choking on all the symbols
# that get generated from autogenerated make rules
.NOEXPORT:

include ./dependencies
