#
# Makefile for the Linux C library
#

TOPDIR=.

include $(TOPDIR)/Makeconfig

ifeq ($(NYS),true)
NYS_DIR_SUFFIX=-nys
endif

DEPEND_DIRS= bsd cvt ctype curses-old des dirent \
	gdbm  grp inet io libio locale login malloc math misc \
	mntent netgroup posix pwd regex rpc setjmp signal stdlib \
	string sysdeps termcap time ufc compat gcc \
	malloc-930716 $(YPDIR) $(NLSDIR)

CLEAN_DIRS= bsd cvt ctype curses-old des dirent \
	gdbm grp inet io libio locale login malloc math misc \
	mntent netgroup posix pwd regex rpc setjmp signal stdlib \
	string sysdeps termcap time ufc compat gcc \
	malloc-930716 $(YPDIR) $(NLSDIR) sbin jump

ifeq ($(LITE),true)
# for light C library without curses, gdbm
DIRS = bsd cvt ctype des dirent grp inet io libio locale login \
	malloc-930716 misc mntent posix pwd regex rpc setjmp signal \
	stdlib string sysdeps termcap time ufc compat gcc # $(YPDIR)
else
ifeq ($(CHECKER),true)
DIRS= bsd cvt ctype des dirent grp inet io \
	libio locale login math misc mntent netgroup posix pwd regex \
	rpc setjmp signal stdlib string sysdeps termcap time ufc \
	gcc $(YPDIR) $(NLSDIR) gdbm
else
ifeq ($(MATH),true)
DIRS= math sysdeps
else
DIRS= bsd cvt ctype curses-old des dirent gdbm grp \
	inet io \
	libio locale login malloc math misc mntent netgroup posix \
	pwd regex rpc setjmp signal stdlib string sysdeps termcap \
	time ufc compat gcc malloc-930716 $(YPDIR) $(NLSDIR)
endif
endif
endif

ifeq ($(SHARED),true)
DIRS:= $(DIRS) jump
CLEAN_DIRS:= $(CLEAN_DIRS) jump
endif

all: lib

lib:
	for i in $(DIRS); do \
		echo making $@ in $$i; \
		($(MAKE) -C $$i $@) || exit 1; \
	done

depend:
	for i in $(DEPEND_DIRS); do \
		echo making $@ in $$i; \
		($(MAKE) -C $$i $@) || exit 1; \
	done

clean realclean:
	$(RM) -f core *.o *.s *.sa *.so.* *.a verify.out
	$(RM) -rf $(OBJS_DIRS)
	for i in $(CLEAN_DIRS); do \
	   echo making $@ in $$i; \
	   ($(MAKE) -C $$i $@) || exit 1; \
	done

install: install.shared install.static install.debug install.profile

install.lite:
	(cd jump; $(MAKE) install LITE=true)

install.shared:
	(cd jump; $(MAKE) install)

install.static:
	-$(AR) -d static/libc.a __.SYMDEF
	(cd static; $(RM) -rf tmpcopy; $(MKDIR) tmpcopy; \
	 cd tmpcopy; \
	 $(AR) -x ../../libalias.a; \
	 $(AR) $(AR_FLAGS) ../libc.a *.o; \
	 $(RM) *; \
	 $(AR) -x ../../libmalias.a; \
	 $(AR) $(AR_FLAGS) ../libm.a *.o; \
	 cd ..; $(RM) -rf tmpcopy; \
	 for l in *.a; do \
	   $(REALRANLIB) $$l; done; \
	 cp *.a crt0.o $(TARGET_LIB_DIR))
	(cd $(TARGET_LIB_DIR); \
	 rm -f libdbm.a; \
	 ln -sf libgdbm.a libdbm.a)

install.debug:
	-$(AR) -d debug/libg.a __.SYMDEF
	(cd debug; $(RM) -rf tmpcopy; $(MKDIR) tmpcopy; \
	 cd tmpcopy; \
	 $(AR) -x ../../libalias.a; \
	 $(AR) $(AR_FLAGS) ../libg.a *.o; \
	 cd ..; $(RM) -rf tmpcopy; \
	 for l in *.a; do \
	   $(REALRANLIB) $$l; done; \
	 cp *.a $(TARGET_LIBEXTRA_DIR); \
	 cp libc$(NYS_DIR_SUFFIX)/mcheck-init.o $(TARGET_LIBEXTRA_DIR)/libmcheck.a)

install.profile:
	-$(AR) -d profile/libc_p.a __.SYMDEF
	(cd profile; $(RM) -rf tmpcopy; $(MKDIR) tmpcopy; \
	 cd tmpcopy; \
	 $(AR) -x ../../libalias.a; \
	 $(AR) $(AR_FLAGS) ../libc_p.a *.o; \
	 cd ..; $(RM) -rf tmpcopy; \
	 for l in *.a; do \
	   $(REALRANLIB) $$l; done; \
	 cp *.a gcrt0.o $(TARGET_LIBEXTRA_DIR))

install.checker:
	(cd checker; $(MAKE) install)
