# These are some standard rules common to all Makefiles that build 
# libraries.

include $(top_srcdir)/configvars

# This is the slow part of the Makefiles.  Exclude it if we build
# binary distributions.
ifndef top_distdir

ifdef WITH_SOFT_FP
SFPFLAG = -Dsfp004
sfp = sfp
endif

libc := $(lib_name_prefix)$(libid)c$(sfp)$(libsize)$(lib_name_extender)
libiio := $(lib_name_prefix)$(libid)iio$(sfp)$(libsize)$(lib_name_extender)

LIBS := $(libc) $(libiio)

ifeq ($(USE_WEAK_SYMBOLS), yes)
ifdef HAVE_ASM_WEAKEXT_DIRECTIVE
CFLAGS += -DHAVE_ASM_WEAKEXT_DIRECTIVE
else
CFLAGS += -DHAVE_ASM_WEAK_DIRECTIVE
endif
endif

ifeq ($(USE_GNU_LD), yes)
CFLAGS += -DHAVE_GNU_LD
endif

INCLUDES = -nostdinc -I$(top_srcdir)/include -I$(top_srcdir)/src
ASCPPFLAGS = -x assembler-with-cpp
DEFS += -DVERSION=\"$(VERSION)\" -D_LIBC -D_GNU_SOURCE -D_REENTRANT

ifeq ($(libsize), _p)
COMPILE = $(CC) $(WARN) $(cflags) $(filter-out -fomit-frame-pointer, $(CFLAGS)) \
  $(INCLUDES) $(DEFS) $(SFPFLAG)
else
COMPILE = $(CC) $(WARN) $(cflags) $(CFLAGS) $(INCLUDES) $(DEFS) $(SFPFLAG)
endif

csrcdir = $(top_srcdir)/src

-include $(top_srcdir)/CFILES

CFILES := $(LIBCFILES) $(SYSCFILES)
CSRCS := $(filter %.c,$(CFILES))
ASCPPSRCS := $(filter %.cpp,$(CFILES))
ASSRCS := $(filter %.s,$(CFILES))

# I think we can remove libgcc1.c from the distribution, gmon.o is special
# because it is said that it doesn't like the -fomit-frame-pointer option,
# libgcc2.c is a common source file for many routines and div.c is a
# safe C version of the normally used assembler version.
SPECIALCOBJS = crt0.o gmon.o libgcc1.o libgcc2.o div.o 
COBJS := $(filter-out $(SPECIALCOBJS), $(patsubst %.c,%.o,$(CSRCS)))

ASOBJS := $(patsubst %.s,%.o,$(ASSRCS))

# Floating point compare routines for gcc.  All compiled from
# _cmpdf2.cpp and _cmpsf2.cpp.
FCOMPDOBJS = _eqdf2.o _gtdf2.o _ledf2.o _gedf2.o _ltdf2.o
FCOMPSOBJS = _eqsf2.o _gtsf2.o _lesf2.o _gesf2.o _ltsf2.o
FCOMPOBJS = _cmpdf2.o _cmpsf2.o

# These are all compiled from libgcc2.c
GLIB2OBJS = _muldi3.o _divdi3.o _moddi3.o _udivdi3.o _umoddi3.o _negdi2.o \
_lshrdi3.o _lshldi3.o _ashldi3.o _ashrdi3.o _udivmoddi4.o _cmpdi2.o \
_ucmpdi2.o _floatdidf.o _floatdisf.o _fixunsdfsi.o _fixunssfsi.o \
_fixunsdfdi.o _fixdfdi.o _fixunssfdi.o _fixsfdi.o _op_new.o _new_handler.o \
_op_delete.o _trampoline.o __main.o _ctor_list.o _dtor_list.o _ffsdi2.o \
_op_vnew.o _op_vdel.o _eh.o _pure.o

# The functions memset and bzero are compiled from a common source
# file bzero.cpp.
BZEROOBJS = bzero.o memset.o

ASCPPOBJS := $(filter-out bzero.o $(FCOMPOBJS), $(patsubst %.cpp,%.o,$(ASCPPSRCS)))

CDEP_FILES := $(addprefix $(top_srcdir)/deps/,$(patsubst %.c,%.P,$(CSRCS)))
CPPDEP_FILES := $(addprefix $(top_srcdir)/deps/,$(patsubst %.cpp,%.P,$(ASCPPSRCS)))
DEP_FILES := $(CDEP_FILES) $(CPPDEP_FILES)

IIOOBJS = idoprnt.o iscanf.o fprintf.o sprintf.o fscanf.o sscanf.o

OBJS := $(ASOBJS) $(ASCPPOBJS) $(FCOMPDOBJS) $(FCOMPSOBJS) $(GLIB2OBJS) \
$(BZEROOBJS) gmon.o $(COBJS)

endif  # not top_distdir.

.SUFFIXES:

include $(top_srcdir)/rules

all-here: apologies $(top_srcdir)/CFILES $(LIBS)

include $(top_srcdir)/bindist

install-include:

install-man:

uninstall-include:

uninstall-man:

apologies:
	@echo "APOLOGIES: All make runs in this subdirectory are awfully"
	@echo "slow due to several design flaws in the Makefiles and the"
	@echo "size of the directory.  Sorry for the inconvenience!"
	@echo "Please have patience and don't interrupt even if you think"
	@echo "that nothing will happen.  Take your time..."

ifndef top_distdir    # Not needed when making distributions.

# Copy SRCFILES from source directory here.
ifneq ($(USE_WEAK_SYMBOLS), yes)
$(top_srcdir)/CFILES: $(csrcdir)/SRCFILES $(csrcdir)/SYSCALLS
	sed -e 's,^SRCFILES *=,LIBCFILES =,g' $(csrcdir)/SRCFILES >$(top_srcdir)/CFILES
	sed -e 's,^SYSCALLS *=,SYSCFILES =,g' $(csrcdir)/SYSCALLS >>$(top_srcdir)/CFILES
else
$(top_srcdir)/CFILES: $(csrcdir)/SRCFILES
	sed -e 's,^SRCFILES *=,LIBCFILES =,g' $(csrcdir)/SRCFILES >$(top_srcdir)/CFILES
endif

# Normal compile.
$(COBJS):
	@echo '$(COMPILE) -c $(csrcdir)/$(@:.o=.c) -o $@'; \
	$(COMPILE) -Wp,-MD,$(top_srcdir)/deps/$(@:.o=.P) \
	-c $(csrcdir)/$(@:.o=.c) -o $@

# Normal assemble.  No dependencies here.
$(ASOBJS): %.o: $(csrcdir)/*.s
	$(COMPILE) -c $(csrcdir)/$(@:.o=.s) -o $@

# No-float versions for printf and friends.
idoprnt.o iscanf.o:
	@echo '$(COMPILE) -D__NO_FLOAT__ -c $(csrcdir)/$(@:i%.o=%.c) -o $@'; \
	$(COMPILE) -D__NO_FLOAT__ -Wp,-MD,$(top_srcdir)/deps/$(@:.o=.P) \
	-c $(csrcdir)/$(@:i%.o=%.c) -o $@

# gmon.o CANNOT have -fomit-frame-pointer
GMONCOMPILE = $(filter-out -fomit-frame-pointer, $(COMPILE))
gmon.o:
	@echo '$(GMONCOMPILE) -c $(csrcdir)/gmon.c -o $@'; \
	$(GMONCOMPILE) -Wp,-MD,$(top_srcdir)/deps/gmon.P \
	-c $(csrcdir)/gmon.c -o $@

# Objects compiled from libgcc2.c.  Since this is a foreign source we
# turn off -Wall if it was set. 
GLIB2COMPILE = $(filter-out -Wall, $(COMPILE))
$(GLIB2OBJS): %.o: $(csrcdir)/libgcc2.c
	@echo '$(GLIB2COMPILE) -DL$* -c $(csrcdir)/libgcc2.c -o $@'; \
	$(GLIB2COMPILE) -DL$* -Wp,-MD,$(top_srcdir)/deps/$(@:.o=.P) \
	-c $(csrcdir)/libgcc2.c -o $@

# Assembler with preprocessing.	
$(ASCPPOBJS): %.o: $(csrcdir)/%.cpp
	@echo '$(COMPILE) $(ASCPPFLAGS) -c $(csrcdir)/$(@:.o=.cpp) -o $@'; \
	$(COMPILE) $(ASCPPFLAGS) -Wp,-MD,$(top_srcdir)/deps/$(@:.o=.P) \
	-c $(csrcdir)/$(@:.o=.cpp) -o $@; \

# Double floating point compare routines.
$(FCOMPDOBJS): %.o: $(csrcdir)/_cmpdf2.cpp
	@echo '$(COMPILE) -DL$* $(ASCPPFLAGS) -c $(csrcdir)/_cmpdf2.cpp -o $@'; \
	$(COMPILE) -DL$* $(ASCPPFLAGS) -Wp,-MD,$(top_srcdir)/deps/$(@:.o=.P) \
	-c $(csrcdir)/_cmpdf2.cpp -o $@; \

# Single floating point compare routines.
$(FCOMPSOBJS): %.o: $(csrcdir)/_cmpsf2.cpp
	@echo '$(COMPILE) -DL$* $(ASCPPFLAGS) -c $(csrcdir)/_cmpsf2.cpp -o $@'; \
	$(COMPILE) -DL$* $(ASCPPFLAGS) -Wp,-MD,$(top_srcdir)/deps/$(@:.o=.P) \
	-c $(csrcdir)/_cmpsf2.cpp -o $@; \

# Form memset and bzero.
$(BZEROOBJS): %.o: $(csrcdir)/bzero.cpp
	@echo '$(COMPILE) -DL$* $(ASCPPFLAGS) -c $(csrcdir)/bzero.cpp -o $@'; \
	$(COMPILE) -DL$* $(ASCPPFLAGS) -Wp,-MD,$(top_srcdir)/deps/$(@:.o=.P) \
	-c $(csrcdir)/bzero.cpp -o $@; \

# Finally, build the libs.
$(libc): $(OBJS)
	rm -f $@
	$(AR) cru $@ $(OBJS)
	$(RANLIB) $@

$(libiio): $(IIOOBJS)
	rm -f $@
	$(AR) cru $@ $(IIOOBJS)
	$(RANLIB) $@

DEPS_MAGIC := $(shell mkdir $(top_srcdir)/deps > /dev/null 2>&1 || :)

# The /dev/null is added so that make will not warn if there are no
# dependency files (as in the first run).
-include $(DEP_FILES) /dev/null

endif   # not top_distdir.

include $(top_srcdir)/phony

