
# Makefile for YumaPro External System Library
# Not using top-sil.make because this is not a SIL module

############### 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_system.so will be installed
# if make install is called from a normal build
#
# The library libyp_system-example.so will be installed
# if make install is called from a package build
#
#  SUBDIR_NM=yp_system
#
SUBDIR_NM=yp_system-example
REAL_NAME=libyp_system.so
STATIC_NAME=libyp_system.a
SUBDIR_CPP=

############### TARGET PROFILE ##############################
include ../../top-common.make

#################### MAKE RULES ########################
all: sys_dummy sys_lib

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

LBASE=../lib
LFLAGS += -lm
LPATH=-L$(LBASE)
CEES = $(wildcard *.c)
HEES = $(wildcard *.h)

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

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

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

######################### 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 $@ $<


# 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 $< > $@


################## 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

SYS_LIB_A=$(LIB_INST)/lib$(SUBDIR_NM).a
SYS_STAT_LIB=$(LIB_INST)/libyp_system-static.a

ifdef CYGWIN
LAGT=
else
LAGT=-L$(REAL_INST)
endif # CYGWIN

test:


# install the example version

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


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


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


sys_lib: $(SYS_LIB)


ifdef UCLINUX
 ifdef STATIC_SERVER
  ifndef NETCONFD_LIB
	if [ ! -f $(SYS_STAT_LIB) ]; then \
	  cp $(SYS_LIB_A) $(SYS_STAT_LIB); \
	fi
  endif # NETCONFD_LIB
 endif # STATIC_SERVER
else
 ifdef STATIC_SERVER
	if [ ! -f $(SYS_STAT_LIB) ]; then \
	  cp $(SYS_LIB_A) $(SYS_STAT_LIB); \
	fi
 endif # STATIC_SERVER
endif # UCLINUX

# 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).*
	rm -f $(SYS_STAT_LIB)

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

distclean: superclean


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


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

include ./dependencies
