############################################################################
#          Makefile for the "mini-libc" library.                           #
############################################################################

-include ../../params-soft.mk

ifeq ($(ARCH_NAME),)
$(error Please define in ARCH_NAME parameter in params-soft.mk!)
endif

SRCS = ctype.c   \
       dirent.c  \
       fcntl.c   \
       mman.c    \
       signal.c  \
       stat.c    \
       stdio.c   \
       stdlib.c  \
       string.c  \
       strings.c \
       time.c    \
       unistd.c  \
       wait.c  

OBJS = $(addprefix build/, $(SRCS:.c=.o)) \
       $(HAL_ARCH)/build/core/hal_user.o

INCLUDES = -I.                     \
           -I$(LIBPTHREAD_PATH)    \
           -I$(LIBALMOSMKH_PATH)   \
           -I$(SHARED_INCLUDE)     \
           -I$(HAL)/generic        \
           -I$(HAL_ARCH)/core      \
           -I$(KERNEL) 

libs : build/lib/libc.a  headers

build :
	@mkdir build
	@mkdir build/lib
	@mkdir build/include
	@mkdir build/include/sys

$(HAL_ARCH)/build/core/hal_user.o :
	$(MAKE) -C $(HAL_ARCH)

build/%.o : %.c %.h
	$(CC) $(INCLUDES) $(CFLAGS) -c -o  $@ $<
	$(DU) -D $@ > $@.txt

headers: build
	cp ctype.h   build/include/.
	cp dirent.h  build/include/.
	cp fcntl.h   build/include/.
	cp mman.h    build/include/sys/.
	cp signal.h  build/include/.
	cp stat.h    build/include/sys/.
	cp stdio.h   build/include/.
	cp stdlib.h  build/include/.
	cp string.h  build/include/.
	cp strings.h build/include/.
	cp time.h    build/include/.
	cp unistd.h  build/include/.
	cp wait.h    build/include/sys/.

build/lib/libc.a: build $(OBJS)
	$(AR) rc $@ $(OBJS)
	$(RANLIB) $@

.PHONY = build clean headers

clean:
	rm -rf build/
