#
# Kbuild file for klibc
#

# Tell that we are building klibc
export klibc-build := y

# Generate syscall stubs
klib-y += syscalls/
# Generate socket calls stubs
klib-y += socketcalls/
# zlib, if configured
klib-$(CONFIG_KLIBC_ZLIB) += zlib/
# arch specific .o files
klib-y += arch/$(KLIBCARCHDIR)/

klib-y += vsnprintf.o snprintf.o vsprintf.o sprintf.o \
	  asprintf.o vasprintf.o \
	  vsscanf.o sscanf.o ctypes.o \
	  strntoumax.o strntoimax.o \
	  atoi.o atol.o atoll.o \
	  strtol.o strtoll.o strtoul.o strtoull.o \
	  strtoimax.o strtoumax.o \
	  globals.o exit.o atexit.o onexit.o \
	  execl.o execle.o execv.o execvpe.o execvp.o execlp.o execlpe.o \
	  fork.o vfork.o wait.o wait3.o waitpid.o system.o \
	  setpgrp.o getpgrp.o daemon.o \
	  printf.o vprintf.o fprintf.o vfprintf.o perror.o \
	  statfs.o fstatfs.o umount.o \
	  creat.o open.o openat.o \
	  fread2.o fwrite2.o fgets.o fputc.o fputs.o puts.o putchar.o \
	  sleep.o usleep.o strtotimespec.o strtotimeval.o \
	  raise.o abort.o assert.o alarm.o pause.o \
	  __signal.o sysv_signal.o bsd_signal.o siglist.o sigabbrev.o \
	  siglongjmp.o \
	  sigaction.o sigpending.o sigprocmask.o sigsuspend.o \
	  pselect.o ppoll.o \
	  pread.o pwrite.o \
	  brk.o sbrk.o malloc.o realloc.o zalloc.o calloc.o \
	  mmap.o shm_open.o shm_unlink.o \
	  memcpy.o memcmp.o memset.o memccpy.o memmem.o memswap.o \
	  memmove.o memchr.o memrchr.o bzero.o \
	  strcasecmp.o strncasecmp.o strndup.o strerror.o strsignal.o \
	  strcat.o strchr.o strcmp.o strcpy.o strdup.o strlen.o strnlen.o \
	  strncat.o strlcpy.o strlcat.o \
	  strstr.o strncmp.o strncpy.o strrchr.o \
	  strxspn.o strspn.o strcspn.o strpbrk.o strsep.o strtok.o \
	  strtok_r.o \
	  fnmatch.o \
	  gethostname.o getdomainname.o getcwd.o \
	  seteuid.o setegid.o \
	  getenv.o setenv.o putenv.o __put_env.o unsetenv.o \
	  clearenv.o nullenv.o \
	  getopt.o getopt_long.o readdir.o scandir.o alphasort.o remove.o \
	  syslog.o closelog.o pty.o isatty.o reboot.o \
	  time.o utime.o lseek.o nice.o getpriority.o \
	  qsort.o bsearch.o \
	  lrand48.o jrand48.o mrand48.o nrand48.o srand48.o seed48.o \
	  inet/inet_ntoa.o inet/inet_aton.o inet/inet_addr.o \
	  inet/inet_ntop.o inet/inet_pton.o inet/bindresvport.o \
	  accept.o send.o recv.o \
	  access.o chmod.o chown.o dup2.o mknod.o poll.o rename.o renameat.o \
	  stat.o \
	  lchown.o link.o rmdir.o unlink.o utimes.o lstat.o mkdir.o \
	  readlink.o realpath.o select.o symlink.o pipe.o \
	  ctype/isalnum.o ctype/isalpha.o ctype/isascii.o \
	  ctype/isblank.o ctype/iscntrl.o ctype/isdigit.o \
	  ctype/isgraph.o ctype/islower.o ctype/isprint.o \
	  ctype/ispunct.o ctype/isspace.o ctype/isupper.o \
	  ctype/isxdigit.o ctype/tolower.o ctype/toupper.o \
	  userdb/getgrgid.o userdb/getgrnam.o userdb/getpwnam.o \
	  userdb/getpwuid.o userdb/root_group.o userdb/root_user.o \
	  setmntent.o endmntent.o getmntent.o \
	  stdio/fclose.o stdio/fopen.o stdio/fdopen.o \
	  stdio/fread.o stdio/fwrite.o stdio/fflush.o \
	  stdio/ungetc.o stdio/fgetc.o \
	  stdio/fseek.o stdio/ftell.o stdio/rewind.o \
	  stdio/fileno.o stdio/feof.o stdio/ferror.o stdio/clearerr.o \
	  sysconf/sysconf.o

klib-$(CONFIG_KLIBC_ERRLIST) += errlist.o

ifeq ($(CONFIG_KLIBC_ERRLIST),y)
KLIBCCFLAGS_strerror.o += -DWITH_ERRLIST
endif

# These pass a huge maximum length to the corresponding length-limiting
# functions
KLIBCCFLAGS_sprintf.o += $(call cc-option,-Wno-format-truncation, )
KLIBCCFLAGS_vsprintf.o += $(call cc-option,-Wno-format-truncation, )

# sigsuspend.c includes <klibc/havesyscall.h> generated by syscalls/
# build, so require that to build first
$(obj)/sigsuspend.o: $(obj)/syscalls/klib.list

#####
# Shared definitions
LIBC     := libc.a
SOLIB    := libc.so
SOHASH   := klibc.so
CRT0     := arch/$(KLIBCARCHDIR)/crt0.o
INTERP_O := interp.o

always   := $(LIBC)
ifdef KLIBCSHAREDFLAGS
always   += $(SOLIB) $(SOHASH) $(INTERP_O)
endif
LIBC     := $(call objectify,$(LIBC))
SOLIB    := $(call objectify,$(SOLIB))
SOHASH   := $(call objectify,$(SOHASH))
CRT0     := $(call objectify,$(CRT0))
INTERP_O := $(call objectify,$(INTERP_O))

SOLIBHASH = $(shell cat $(SOLIB).hash)

#####
# Readable errormessages extracted from src..
targets += errlist.c
quiet_cmd_errlist = GEN     $@
      cmd_errlist = $(PERL) $< $(call flags,KLIBCCPPFLAGS) -errlist > $@ \
                    || rm -f $@

$(obj)/errlist.c: $(srctree)/$(src)/makeerrlist.pl
	$(call cmd,errlist)


# all .o files for all dirs
klib-o-files = $(shell cat $(obj)/klib.list \
		$(addsuffix /klib.list, $(klib-dirs)))
######
# Build static library: libc.a
targets += libc.a __static_init.o
quiet_cmd_libc = KLIBCAR $@
      cmd_libc = rm -f $@;						\
                 $(KLIBCAR) Dcq $@					\
		 $(call objectify,__static_init.o) $(klib-o-files);	\
                 $(KLIBCRANLIB) $@

$(LIBC): $(call objectify,__static_init.o) $(obj)/klib.list FORCE
	$(call if_changed,libc)

######
# Build shared library
targets += libc.so __shared_init.o

quiet_cmd_libcso = KLIBCLD $@
      cmd_libcso = $(KLIBCLD) $(KLIBCLDFLAGS) $(KLIBCSHAREDFLAGS) -o $@ \
			--start-group					\
				$(CRT0)					\
				$(call objectify,__shared_init.o)	\
				$(klib-o-files)				\
				$(KLIBCLIBGCC)				\
			--end-group

$(SOLIB): $(call objectify,__shared_init.o) $(obj)/klib.list FORCE
	$(call if_changed,libcso)


#####
# Build sha1 hash values
targets     += klibc.so libc.so.hash
hostprogs-y := sha1hash
clean-files += klibc-???????????????????????????.so

quiet_cmd_solibhash = HASH    $@
      cmd_solibhash = $(KLIBCNM) $< | egrep '^[0-9a-fA-F]+ [ADRTW] ' | \
                                        sort | $(obj)/sha1hash > $@
$(SOLIB).hash: $(SOLIB) $(obj)/sha1hash FORCE
	$(call if_changed,solibhash)

quiet_cmd_sohash = GEN     $@
      cmd_sohash = cat $< > $@;                                           \
                     $(KLIBCSTRIP) $(KLIBCSTRIPFLAGS) $@;                 \
		     chmod a+x $@;					  \
                     rm -f $(obj)/klibc-???????????????????????????.so;   \
                     ln -f $@ $(obj)/klibc-$(SOLIBHASH).so
$(SOHASH): $(SOLIB) $(SOLIB).hash
	$(call cmd,sohash)


#####
# build interp.o
targets += interp.o

quiet_cmd_interp = BUILD   $@
      cmd_interp = $(KLIBCCC) $(klibccflags) -D__ASSEMBLY__     \
                             -DLIBDIR=\"$(SHLIBDIR)\"         \
			     -DSOHASH=\"$(SOLIBHASH)\" \
                             $(KLIBCSTACKFLAGS) \
			     -c -o $@ $<

$(INTERP_O): $(obj)/interp.S $(SOLIB).hash
	$(call if_changed,interp)

#####
# Install klibc
install-rule:
	@echo "  INSTALL klibc to $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib"
	$(Q)$(foreach f, $(LIBC) $(CRT0), \
	  $(shell $(install-data) $(f) \
	          $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib))
ifdef KLIBCSHAREDFLAGS
	$(Q)$(foreach f, $(SOLIB) $(INTERP_O), \
	  $(shell $(install-data) $(f) \
	          $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib))
	$(Q)$(install-lib) $(obj)/klibc-$(SOLIBHASH).so \
	                      $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib
	$(Q)$(install-lib) $(obj)/klibc-$(SOLIBHASH).so \
	                      $(INSTALLROOT)$(SHLIBDIR)
endif

# Directories to visit during clean and install
subdir- := arch/$(KLIBCARCHDIR)/
