#
#   This makefile will construct the iBCS standard libc shared library
#   file.
#
#   It will build the shared library only. Perhaps someone else will wish
#   to construct library files which are applicable for compiling ibcs
#   applications under Linux, however, this is not my target.
#

.EXPORT_ALL_VARIABLES:

# Base directory containing the COFF versions of the assembler, linker, etc.
# (the bintools package.)

include	./Makerules

ifeq (./Makeconfig,$(wildcard ./Makeconfig))
include ./Makeconfig
endif

ifdef COFF
all:	Makeconfig libc.sa
else
all: Makeconfig ibcssubdirs
endif

DATE := $(shell date +%y%m%d;)

CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
	  else if [ -x /bin/bash ]; then echo /bin/bash; \
	  else echo sh; fi ; fi)
#
# AR flags to construct the archive library
ARFLAGS	=-r

# Text and data load addresses. The .bss section follows .data
TEXT	=0xa0000000
DATA	=0xa0400000

# linker flags
LDFLAGS	=-s -S -M -L$(CWD)/libs -Ttext $(TEXT) -Tdata $(DATA)

# List of the standard libraries
LDLIB	=-lsysdeps -lctype -lstdio -lcvt -lstdlib  -lstring \
	 -li386 -lsignal -lmalloc

LDLIBS	=$(LDLIB) $(LDLIB) -lmissing $(LDLIB)

#
# Indicate additional directories to comb, depending on what type of library
# we want.
#
ifdef COFF
ADDL_SUBDIRS	= header missing 
endif

ifdef ELF
ADDL_SUBDIRS	= dirent svr4 locale nls time pwd elf svr4/socket svr4/nsl
endif

# List of subdirectories from this level to all lower parts.
SUBDIRS	=string sysdeps ctype convert stdlib malloc stdio \
	sysdeps/i386 signal $(ADDL_SUBDIRS)

ifdef COFF
libc.sa:	ibcssubdirs tools/coff.stamp
		$(LD) $(LDFLAGS) -o libc_s header/header.o	\
		sysdeps/_exit.o $(LDLIBS) >lib_s.map
		tools/coff.stamp libc_s

tools/coff.stamp:
		(cd tools; make coff.stamp)
endif

ibcssubdirs:
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i; done

Makeconfig: Makefile Makeconfig.in
	echo CWD=`pwd` > Makeconfig
	cat Makeconfig.in >> Makeconfig

dep:
	set -e; for i in $(SUBDIRS); do $(MAKE) -C $$i dep; done

clean:
	rm -f libc.sa libc.map `find . -name '*.[ioas]' -print`
	touch .depend foobar~
	rm -f `find . -name '.depend' -print`
	rm -f `find . -name '*~' -print`
	rm -f Makeconfig
	(cd tools; make clean)
	(cd abi_plus; make clean)

dist:   clean
	rm -rf libc_s-*.tar.gz
	tar cf - * | gzip > libc_s-$(DATE).tar.gz
