src = real.c client.c proxy.c wrap.c wrap_fopen.c libmooproxy.c do.c
objs = $(src:.c=.o)
debugobjs = $(src:.c=.o.debug)

include ../makeinfo
include ../mooix.conf

MOOLIB=libmooproxy.so
SONAME=0

LIBS = -ldl -nostdlib
ifeq ($(shell uname), Linux)
LIBS+= -lgcc
endif

MY_CFLAGS = -finline-functions -O4 -fPIC -DPIC # -DDEBUG -DALWAYS_DEBUG
CFLAGS += $(MY_CFLAGS)
CPPFLAGS:=$(CPPFLAGS) -DMOOSOCK="VARDIR \"/run/mood\"" -DMOOFILE="\"$(MOOFILE)\""
LDDFLAGS=-Wl,-soname -Wl,$(MOOLIB).$(SONAME)

$(MOOLIB): $(objs)
	$(CC) $(CFLAGS) $(LDDFLAGS) -shared $(objs) $(LIBS) -o $(MOOLIB).$(SONAME)
	rm -f $(MOOLIB)
	$(LN_S) $(MOOLIB).$(SONAME) $(MOOLIB)

$(MOOLIB).debug: $(debugobjs)
	$(CC) $(CFLAGS) $(LDDFLAGS) -shared $(debugobjs) $(LIBS) -o $(MOOLIB).debug

install:
	$(INSTALL) -d $(PREFIX)/$(libdir)
	$(INSTALL) $(MOOLIB).$(SONAME) $(PREFIX)/$(libdir)
ifndef $(NO_SOLINKS)
	$(LN_S) $(MOOLIB).$(SONAME) $(PREFIX)/$(libdir)/$(MOOLIB)
endif

uninstall:
	rm -f $(PREFIX)/$(libdir)/$(MOOLIB).$(SONAME)
	rm -f $(PREFIX)/$(libdir)/$(MOOLIB)
	-rmdir -p $(PREFIX)/$(libdir)

test: $(MOOLIB).debug
	$(MAKE) -C t test MOOLIB=$(MOOLIB).debug

clean:
	rm -f *~ $(objs) $(debugobjs) $(MOOLIB) \
		$(MOOLIB).$(SONAME) $(MOOLIB).debug core 
	make -C t clean

realclean: clean
	rm -f .depend

ifneq ($(shell arch), parisc64)
# Build wrap.c without all the defines, especially any that might turn on
# 64 bit versions of stuff, to make sure the right versions get wrapped.
wrap.o:
	$(CC) $(MY_CFLAGS) -c -o wrap.o wrap.c

wrap.o.debug:
	$(CC) $(MY_CFLAGS) -DDEBUG -c -o wrap.o.debug wrap.c
endif
# On hppa, wrap.c must be built with all defines or execve wrapping doesn't
# work. However, fopen wrapping requires the defines be off, since fopen64
# conflicts with the one in libgcc on hppa, and the defines somehow expose
# that conflict. Therefore, I split the fopen wrapping off into a separate
# file, wrap_fopen.c. It will always be built without the defines.
wrap_fopen.o:
	$(CC) $(MY_CFLAGS) -c -o wrap_fopen.o wrap_fopen.c

wrap_fopen.o.debug:
	$(CC) $(MY_CFLAGS) -DDEBUG -c -o wrap_fopen.o.debug wrap_fopen.c

*.o: Makefile

%.o.debug: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -DDEBUG -c -o $@ $<

.depend: *.c *.h
	cc -MM *.c > .depend

-include .depend
