DYNAMIC_LINKER=/lib/ld-linux.so.0

ALPINE_SHARED_LIB = libAlpine.so
PROG = a4n_server

COPTFLAGS?=-O -pipe -g
INCLUDES= -I. -I.. -I../lib_req_resp
VPATH = $(S)/lib_req_resp

CC = gcc
LD = ld

COPTS=	${INCLUDES}
CFLAGS=	${COPTFLAGS} ${CWARNFLAGS} ${DEBUG} ${COPTS}

CFLAGS += -DALPINE

DEFINED_PROF=	${PROF}
ifdef PROF
CFLAGS+=	-malign-functions=4
#if ${PROFLEVEL} >= 2
#IDENT+=	-DGPROF4 -DGUPROF
#PROF+=	-mprofiler-epilogue
#endif
endif

# Put configuration-specific C flags last (except for ${PROF}) so that they
# can override the others.
CFLAGS+=	${CONF_CFLAGS}

.IMPSRC = $(filter %.c %.s, $^)
.TARGET = $@

NORMAL_C= ${CC} -c ${CFLAGS} ${PROF} ${.IMPSRC}
NORMAL_C_C= ${CC} -c ${CFLAGS} ${PROF} ${.IMPSRC}
NORMAL_S= ${CC} -c ${ASM_CFLAGS} ${.IMPSRC}
PROFILE_C= ${CC} -c ${CFLAGS} ${.IMPSRC}

SYSTEM_DEP= Makefile ${SYSTEM_OBJS}
SYSTEM_OBJS= ${OBJS} \

BEFORE_DEPEND=

OBJS= a4n_server.o native_syscalls.o req_resp.o server_req_resp.o \
	alpine_fake.o sendrecvmsg.o fd_mgmt.o alpine_support.o

CFILES= a4n_server.c native_syscalls.c req_resp.c server_req_resp.c \
	alpine_fake.c sendrecvmsg.c fd_mgmt.c

SFILES=
MFILES=
CLEAN= $(ALPINE_SHARED_LIB) $(PROG)

.PHONY:	all modules

#all: $(ALPINE_SHARED_LIB) $(PROG)
all:  $(OBJS) $(PROG)

#$(PROG): $(OBJS) ../alpine_support/libAlpineSupport.a \
#		../lwip-contrib/ports/unix/proj/lib/liblwip.so
#	$(CC) -o $(PROG) $(OBJS) ../alpine_support/libAlpineSupport.a \
#	../lwip-contrib/ports/unix/proj/lib/liblwip.so
$(PROG): $(OBJS) ../lwip-contrib/ports/unix/proj/lib/liblwip.so
	$(CC) -o $(PROG) $(OBJS) ../lwip-contrib/ports/unix/proj/lib/liblwip.so

$(ALPINE_SHARED_LIB): $(S)/sys/compile/NINT/libAlpineSys.a \
		      $(S)/alpine_support/libAlpineSupport.a \
		      $(S)/alpine_socket/libAlpineSocket.a \
		      $(S)/alpine_scheduler/libAlpineScheduler.a
	$(LD) -o $@ \
	      -L $(S)/sys/compile/NINT \
	      -L $(S)/alpine_support/ \
	      -shared --export-dynamic -Bdynamic \
	      --whole-archive $^ --no-whole-archive

#
# XXX: The link command below copied from what gcc does when it
# creates and executable (gcc -v /tmp/xxx.c)
#
#$(PROG): $(ALPINE_SHARED_LIB) $(SYSTEM_OBJS) Makefile
#	$(LD) -o $@ \
#		-E -m elf_i386 \
#		-dynamic-linker $(DYNAMIC_LINKER) \
#		/usr/lib/crt1.o /usr/lib/crti.o  \
#		$(SYSTEM_OBJS) \
#		-L. --whole-archive -lAlpine --no-whole-archive \
#		-lpcap -lc \
#		/usr/lib/crtn.o

DEPEND_EXISTS = $(shell test -f .depend && echo yes)
ifneq ($(strip $(DEPEND_EXISTS)), yes)
${SYSTEM_OBJS}: $(filter %.h, ${BEFORE_DEPEND})
endif

clean:
	rm -f *.o *.a *.so *.So *.ko *.s eddep errs \
	      linterrs makelinks \
	      tags \
	      ${CLEAN}

#lint: /tmp
#	@lint -hbxn -DGENERIC -Dvolatile= ${COPTS} \
#	  $S/$M/$M/Locore.c ${CFILES} ioconf.c | \
#	    grep -v 'struct/union .* never defined' | \
#	    grep -v 'possible pointer alignment problem'

# This is a hack.  BFD "optimizes" away dynamic mode if there are no
# dynamic references.  We could probably do a '-Bforcedynamic' mode like
# in the a.out ld.  For now, this works.
hack.So: Makefile
	touch hack.c
	${CC} -shared -nostdlib hack.c -o hack.So
	rm -f hack.c

cleandepend:
	rm -f .depend

# Dependencies listed by hand. Ack !

a4n_server.o: a4n_server.c
	${NORMAL_C}
native_syscalls.o: 
	ln -s ../lib_req_resp/native_syscalls.o .
req_resp.o: 
	ln -s ../lib_req_resp/req_resp.o .
server_req_resp.o: server_req_resp.c
	${NORMAL_C}
alpine_fake.o: alpine_fake.c
	${NORMAL_C}
sendrecvmsg.o: sendrecvmsg.c
	${NORMAL_C}
fd_mgmt.o: fd_mgmt.c
	${NORMAL_C}
alpine_support.o: alpine_support.c
	${NORMAL_C}

# DO NOT DELETE THIS LINE -- make depend uses it
