[klibc] klibc integrated in the kernel

Sam Ravnborg sam at ravnborg.org
Mon Jul 26 02:11:46 PDT 2004


Gave it a shot during the weekend - but still some work left.
So far managed to build klibc including tracking of dependencies
etc.
Also make clean works.

Started on the applications side and have preferred syntax
for the Makefile ready (very Kbuild alike).

For info only here is my WIP patch.

During next week I expect to get the final build of
klibc in all three variants fixed.
I also hope to make building programs ready - with current syntax.

Building with separate output directory is also broken, but will
be fixed soon also. Should be trivial though.


When building a kernel the verbose output from syscalls.pl,
makeerrlist.pl and socketcalls.pl is annoying. Can a new option
be added to make them less verbose?
We could trigger the verbose format is KBUILD_VERBOSE is != 0.
So passing an option such as '-v' for verbose could be fine.
A 0/1 option could be even easie to implement in the
Makefile.

Note: The below is based on work original made by Kai Germaschewski
et al. but I gave it a fresh shot since there were several
corner issues that needed to be dealt with better.

Two new files introduced:
scripts/Makefile.user   <= early userspace stuff
scripts/Makefile.host   <= shared with kernel build

Comments welcome.

	Sam

===== scripts/Makefile.build 1.43 vs edited =====
--- 1.43/scripts/Makefile.build	2004-03-16 11:29:21 +01:00
+++ edited/scripts/Makefile.build	2004-07-24 12:22:20 +02:00
@@ -277,88 +277,9 @@ targets += $(multi-used-y) $(multi-used-
 
 # Compile programs on the host
 # ===========================================================================
-# host-progs := bin2hex
-# Will compile bin2hex.c and create an executable named bin2hex
-#
-# host-progs    := lxdialog
-# lxdialog-objs := checklist.o lxdialog.o
-# Will compile lxdialog.c and checklist.c, and then link the executable
-# lxdialog, based on checklist.o and lxdialog.o
-#
-# host-progs      := qconf
-# qconf-cxxobjs   := qconf.o
-# qconf-objs      := menu.o
-# Will compile qconf as a C++ program, and menu as a C program.
-# They are linked as C++ code to the executable qconf
-
-# host-progs := conf
-# conf-objs  := conf.o libkconfig.so
-# libkconfig-objs := expr.o type.o
-# Will create a shared library named libkconfig.so that consist of
-# expr.o and type.o (they are both compiled as C code and the object file
-# are made as position independent code).
-# conf.c is compiled as a c program, and conf.o is linked together with
-# libkconfig.so as the executable conf.
-# Note: Shared libraries consisting of C++ files are not supported  
-#
-
-# Create executable from a single .c file
-# host-csingle -> Executable
-quiet_cmd_host-csingle 	= HOSTCC  $@
-      cmd_host-csingle	= $(HOSTCC) $(hostc_flags) $(HOST_LOADLIBES) -o $@ $<
-$(host-csingle): %: %.c FORCE
-	$(call if_changed_dep,host-csingle)
-
-# Link an executable based on list of .o files, all plain c
-# host-cmulti -> executable
-quiet_cmd_host-cmulti	= HOSTLD  $@
-      cmd_host-cmulti	= $(HOSTCC) $(HOSTLDFLAGS) -o $@ \
-			  $(addprefix $(obj)/,$($(@F)-objs)) \
-			  $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
-$(host-cmulti): %: $(host-cobjs) $(host-cshlib) FORCE
-	$(call if_changed,host-cmulti)
-
-# Create .o file from a single .c file
-# host-cobjs -> .o
-quiet_cmd_host-cobjs	= HOSTCC  $@
-      cmd_host-cobjs	= $(HOSTCC) $(hostc_flags) -c -o $@ $<
-$(host-cobjs): %.o: %.c FORCE
-	$(call if_changed_dep,host-cobjs)
-
-# Link an executable based on list of .o files, a mixture of .c and .cc
-# host-cxxmulti -> executable
-quiet_cmd_host-cxxmulti	= HOSTLD  $@
-      cmd_host-cxxmulti	= $(HOSTCXX) $(HOSTLDFLAGS) -o $@ \
-			  $(foreach o,objs cxxobjs,\
-			  $(addprefix $(obj)/,$($(@F)-$(o)))) \
-			  $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
-$(host-cxxmulti): %: $(host-cobjs) $(host-cxxobjs) $(host-cshlib) FORCE
-	$(call if_changed,host-cxxmulti)
-
-# Create .o file from a single .cc (C++) file
-quiet_cmd_host-cxxobjs	= HOSTCXX $@
-      cmd_host-cxxobjs	= $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
-$(host-cxxobjs): %.o: %.cc FORCE
-	$(call if_changed_dep,host-cxxobjs)
-
-# Compile .c file, create position independent .o file
-# host-cshobjs -> .o
-quiet_cmd_host-cshobjs	= HOSTCC  -fPIC $@
-      cmd_host-cshobjs	= $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $<
-$(host-cshobjs): %.o: %.c FORCE
-	$(call if_changed_dep,host-cshobjs)
-
-# Link a shared library, based on position independent .o files
-# *.o -> .so shared library (host-cshlib)
-quiet_cmd_host-cshlib	= HOSTLLD -shared $@
-      cmd_host-cshlib	= $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \
-			  $(addprefix $(obj)/,$($(@F:.so=-objs))) \
-			  $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
-$(host-cshlib): %: $(host-cshobjs) FORCE
-	$(call if_changed,host-cshlib)
-
-targets += $(host-csingle)  $(host-cmulti) $(host-cobjs)\
-	   $(host-cxxmulti) $(host-cxxobjs) $(host-cshlib) $(host-cshobjs) 
+ifdef host-progs
+include $(srctree)/scripts/Makefile.host
+endif
 
 # Descending
 # ---------------------------------------------------------------------------
===== scripts/Makefile.clean 1.13 vs edited =====
--- 1.13/scripts/Makefile.clean	2003-09-11 12:01:23 +02:00
+++ edited/scripts/Makefile.clean	2004-07-26 00:32:12 +02:00
@@ -7,6 +7,11 @@ src := $(obj)
 .PHONY: __clean
 __clean:
 
+# Shorthand for $(Q)$(MAKE) scripts/Makefile.clean obj=dir
+# Usage:
+# $(Q)$(MAKE) $(clean)=dir
+clean := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.clean obj
+
 include $(obj)/Makefile
 
 # Figure out what we need to build from the various variables
@@ -31,7 +36,7 @@ subdir-ymn      := $(sort $(subdir-ym) $
 subdir-ymn	:= $(addprefix $(obj)/,$(subdir-ymn))
 __clean-files	:= $(wildcard $(addprefix $(obj)/, \
 		   $(extra-y) $(EXTRA_TARGETS) $(always) $(host-progs) \
-		   $(targets) $(clean-files)))
+		   $(user-progs) $(targets) $(clean-files)))
 
 # ==========================================================================
 
@@ -60,7 +65,5 @@ $(subdir-ymn):
 
 cmd = @$(if $($(quiet)cmd_$(1)),echo '  $($(quiet)cmd_$(1))' &&) $(cmd_$(1))
 
-# Shorthand for $(Q)$(MAKE) scripts/Makefile.clean obj=dir
-# Usage:
-# $(Q)$(MAKE) $(clean)=dir
-clean := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.clean obj
+.PHONY: FORCE
+FORCE:
===== scripts/Makefile.lib 1.24 vs edited =====
--- 1.24/scripts/Makefile.lib	2004-03-16 11:29:21 +01:00
+++ edited/scripts/Makefile.lib	2004-07-24 11:35:11 +02:00
@@ -272,7 +272,13 @@ cmd = @$(if $($(quiet)cmd_$(1)),echo '  
 # Usage is deprecated, because make do not see this as an invocation of make.
 descend =$(Q)$(MAKE) -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj=$(1) $(2)
 
-# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
+# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj
 # Usage:
 # $(Q)$(MAKE) $(build)=dir
 build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
+
+# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.user obj
+# Usage:
+# $(Q)$(MAKE) $(user)=dir
+user := -rR -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.user obj
+
===== usr/Makefile 1.9 vs edited =====
--- 1.9/usr/Makefile	2003-07-28 00:52:32 +02:00
+++ edited/usr/Makefile	2004-07-26 00:33:52 +02:00
@@ -5,6 +5,20 @@ host-progs  := gen_init_cpio
 
 clean-files := initramfs_data.cpio.gz
 
+# Visit this dir during clean
+clean-rule := $(MAKE) $(clean)=$(obj)/lib
+
+# Building klibc
+klibc := $(obj)/lib
+
+$(initramfs-y): $(klibc)
+
+.PHONY: $(klibc)
+$(klibc): FORCE
+	$(Q)$(MAKE) $(user)=$@
+
+# Building initramfs image
+# ------------------------------------------------------------------------
 # initramfs_data.o contains the initramfs_data.cpio.gz image.
 # The image is included using .incbin, a dependency which is not
 # tracked automatically.
@@ -15,9 +29,13 @@ $(obj)/initramfs_data.o: $(obj)/initramf
 # but we need the information for the build as well, so it's duplicated
 # here.
 
-# Commented out for now
-# initramfs-y := $(obj)/root/hello
+initramfs-y := $(obj)/kinit
 
+.PHONY: $(initramfs-y)
+$(initramfs-y): $(klibc)
+	$(Q)$(MAKE) $(user)=$@
+	
+# Generate fs image from programs listed in $(initramfs-y)
 quiet_cmd_cpio = CPIO    $@
       cmd_cpio = ./$< > $@
 
@@ -30,4 +48,5 @@ $(obj)/initramfs_data.cpio.gz: $(obj)/in
 	$(call if_changed,gzip)
 
 targets += initramfs_data.cpio.gz
+
 
===== usr/kinit/Makefile 1.1 vs edited =====
--- 1.1/usr/kinit/Makefile	2004-07-24 19:04:28 +02:00
+++ edited/usr/kinit/Makefile	2004-07-24 21:29:20 +02:00
@@ -1,31 +1,14 @@
-SRCROOT = ..
-include $(SRCROOT)/MCONFIG
-include $(SRCROOT)/MRULES
+# Makefile for kinit - tiny init program
+user-progs := kinit
 
-MAKEDEPS = -Wp,-MD,.$(subst /,-,$*).d
-CFLAGS	= $(MAKEDEPS) $(OPTFLAGS) $(REQFLAGS) -W -Wall \
-	-I$(SRCROOT)/ipconfig \
-	-I$(SRCROOT)/nfsmount \
-#	-DINI_DEBUG -g
+kinit-y    := nfsroot.o do_mounts.o
 
-LIBS	= \
-	$(SRCROOT)/nfsmount/libnfsmount.a \
-	$(SRCROOT)/ipconfig/libipconfig.a \
-	$(KLIBC) $(LIBGCC)
-PROGS	= kinit
-OBJS	= kinit.o do_mounts.o nfsroot.o
+kinit-$(CONFIG_IP_PNP)   += ipconfig/
+kinit-$(CONFIG_ROOT_NFS) += nfsmount/
 
-all: $(PROGS)
+# Needs header files here
+USERCFLAGS += -I$(obj)/ipconfig -I$(obj)/nfsmount
 
-kinit: $(OBJS) $(LIBS) $(CRT0)
-	$(LD) $(LDFLAGS) -o $@ $(CRT0) $(OBJS) $(LIBS)
+# Debug
+USERCFLAGS +=-DINI_DEBUG
 
-clean:
-	-rm -f *.o kinit
-
-spotless: clean
-	-rm -f .*.d *~
-
-ifneq ($(wildcard .*.d),)
-include $(wildcard .*.d)
-endif
===== usr/kinit/ipconfig/Makefile 1.1 vs edited =====
--- 1.1/usr/kinit/ipconfig/Makefile	2004-07-24 19:04:33 +02:00
+++ edited/usr/kinit/ipconfig/Makefile	2004-07-24 21:30:28 +02:00
@@ -1,28 +1,11 @@
-include MCONFIG
+user-progs := ipconfig
 
-COMMON_OBJS = main.o netdev.o packet.o
-DHCP_OBJS   = dhcp_proto.o
-BOOTP_OBJS  = bootp_proto.o
-PROG        = ipconfig
-LIB	    = libipconfig.a
-LIBS        = $(KLIBC) $(LIBGCC)
+# Common
+obj-y := netdev.o packet.o
+# DHCP
+obj-y += dhcp_proto.o
+# BOOTP
+obj-y += bootp_proto.o
 
-OBJS := $(COMMON_OBJS) $(BOOTP_OBJS) $(DHCP_OBJS)
-
-all:	$(PROG) $(LIB)
-
-ipconfig: $(OBJS) $(CRT0) $(LIBS)
-	$(LD) $(LDFLAGS) -o $@ $(CRT0) $(OBJS) $(LIBS)
-
-$(LIB): $(OBJS)
-	$(AR) cru $(LIB) $(OBJS)
-
-clean:
-	$(RM) *.o $(PROG) $(LIB) core
-
-spotless: clean
-	$(RM) *~ .*.d
-
-ifneq ($(wildcard .*.d),)
-include $(wildcard .*.d)
-endif
+# ipconfig program uses all of obj-y objects
+ipconfig-y := main.o $(obj-y)
===== usr/kinit/nfsmount/Makefile 1.1 vs edited =====
--- 1.1/usr/kinit/nfsmount/Makefile	2004-07-24 19:04:53 +02:00
+++ edited/usr/kinit/nfsmount/Makefile	2004-07-24 21:18:25 +02:00
@@ -1,31 +1,6 @@
-include MCONFIG
+user-progs := nfsmount dummypmap_test
 
-COMMON_OBJS = main.o mount.o portmap.o dummypmap.o sunrpc.o
-PROG        = nfsmount
-LIB	    = libnfsmount.a
-LIBS        = $(KLIBC) $(LIBGCC)
+obj-y      :=  mount.o portmap.o sunrpc.o
 
-OBJS := $(COMMON_OBJS) $(BOOTP_OBJS) $(DHCP_OBJS)
-
-all:	$(PROG) $(LIB)
-
-nfsmount: $(OBJS) $(CRT0) $(LIBS)
-	$(LD) $(LDFLAGS) -o $@ $(CRT0) $(OBJS) $(LIBS)
-	cp -f $@ $@.g
-	$(STRIP) $@
-
-dummypmap: dummypmap_test.o
-	$(LD) $(LDFLAGS) -o $@ $(CRT0) $^ $(LIBS)
-
-$(LIB): $(OBJS)
-	$(AR) cru $(LIB) $(OBJS)
-
-clean:
-	$(RM) *.o $(PROG) $(LIB) core
-
-spotless: clean
-	$(RM) *~ .*.d *~
-
-ifneq ($(wildcard .*.d),)
-include $(wildcard .*.d)
-endif
+#Same .o files used for nfsmount executable
+nfsmount-y := main.o $(obj-y)
===== usr/kinit/nfsmount/dummypmap.c 1.1 vs edited =====
--- 1.1/usr/kinit/nfsmount/dummypmap.c	2004-07-24 19:04:56 +02:00
+++ edited/usr/kinit/nfsmount/dummypmap.c	2004-07-24 21:22:50 +02:00
@@ -133,14 +133,4 @@ int dummy_portmap(int sock, FILE *portma
 		       (struct sockaddr *)&sin, addrlen);
 	}
 }
-      
-#ifdef TEST
-int main(int argc, char *argv[])
-{
-  if ( argc > 1 )
-    portmap_file = fopen(argv[1], "a");
-
-  return dummy_portmap();
-}
-#endif
 
===== usr/kinit/nfsmount/dummypmap_test.c 1.1 vs edited =====
--- 1.1/usr/kinit/nfsmount/dummypmap_test.c	2004-07-24 19:04:58 +02:00
+++ edited/usr/kinit/nfsmount/dummypmap_test.c	2004-07-24 21:23:02 +02:00
@@ -1,2 +1,9 @@
-#define TEST
-#include "dummypmap.c"
+#include "dummypmap.h"
+
+int main(int argc, char *argv[])
+{
+	if ( argc > 1 )
+		portmap_file = fopen(argv[1], "a");
+		
+	return dummy_portmap();
+}
===== usr/lib/Makefile 1.1 vs edited =====
--- 1.1/usr/lib/Makefile	2004-07-24 02:52:29 +02:00
+++ edited/usr/lib/Makefile	2004-07-26 00:46:36 +02:00
@@ -1,15 +1,8 @@
 #
-# Makefile
+# Makefile for building klibc
 #
-# Main makefile
-#
-
-# Include configuration rules
-include MCONFIG
 
-TESTS   = $(patsubst %.c,%,$(wildcard tests/*.c)) \
-	  $(patsubst %.c,%.shared,$(wildcard tests/*.c))
-LIBOBJS = vsnprintf.o snprintf.o vsprintf.o sprintf.o \
+libc-y := vsnprintf.o snprintf.o vsprintf.o sprintf.o \
 	  vsscanf.o sscanf.o ctypes.o \
 	  strntoumax.o strntoimax.o \
 	  atoi.o atol.o atoll.o \
@@ -43,124 +36,147 @@ LIBOBJS = vsnprintf.o snprintf.o vsprint
 	  inet/inet_ntoa.o inet/inet_aton.o inet/inet_addr.o \
 	  inet/inet_ntop.o inet/inet_pton.o inet/bindresvport.o \
 	  send.o recv.o
-ifeq ($(ERRLIST),1)
-LIBOBJS += errlist.o
-endif
 
-SOLIB   = libc.so
-SOHASH  = klibc.so
+# FIXME
+CONFIG_KLIBC_ERRLIST=y
+  libc-$(CONFIG_KLIBC_ERRLIST) += errlist.o
+cflags-$(CONFIG_KLIBC_ERRLIST) += -DWITH_ERRLIST
+USERCFLAGS += $(cflags-y)
 
-CRT0    = crt0.o
-LIB     = libc.a
+# Add any architecture-specific rules
+include $(obj)/arch/$(ARCH)/Makefile.inc
 
-all: tests $(CRT0) $(LIB) $(SOLIB) klibc.so
+# Generate the the files in syscalls/, socketcall/ and
+# save the names into $(syscall-objs)/$(socketcall-objs)
+# But don't bother if we are cleaning
+ifndef clean
+-include $(obj)/syscalls.mk $(obj)/socketcalls.mk
+endif
 
-# Add any architecture-specific rules
-include arch/$(ARCH)/Makefile.inc
-EMAIN ?= -e main
+#####
+# Generate socket calls stubs
+# Based on input from SOCKETCALLS.def generate socket call stubs
+targets    += socketcalls.mk
+clean-rule += rm -rf $(obj)/socketcalls
+quiet_cmd_socketcalls = GEN     $@
+      cmd_socketcalls = $(PERL) $(src)/socketcalls.pl $(obj) $(ARCH) $< > $@ \
+                        || rm -f $@
+
+$(obj)/socketcalls.mk: $(src)/SOCKETCALLS.def $(src)/socketcalls.pl $(src)/socketcommon.h
+	$(call cmd,socketcalls)
+
+
+#####
+# Generate syscalls stubs
+# Based on list in SYSCALLS.def generate stubs for sys calls. Actual arch code
+# is defined in an arch specific perl file
+targets    += SYSCALLS.i syscalls.nrs syscalls.mk
+
+# Side effect of running syscalls.pl
+clean-rule += ;rm -f $(objtree)/usr/include/klibc/havesyscall.h
+# All the syscall stubs
+clean-rule += ;rm -rf $(obj)/syscalls
+
+# We pass -ansi to keep cpp from define e.g. "i386" as well as "__i386__"
+quiet_cmd_syscall.i = GEN     $@
+      cmd_syscall.i = $(USERCC) $(usercflags) -D__ASSEMBLY__ \
+                                -ansi -x assembler-with-cpp -E -o $@ $<
+
+$(obj)/SYSCALLS.i: $(src)/SYSCALLS.def FORCE
+	$(call if_changed_dep,syscall.i)
+
+quiet_cmd_syscall.nrs = GEN     $@
+      cmd_syscall.nrs =  $(USERCC) $(usercflags) -Wp,-dM -x c -E -o $@ $<
+$(obj)/syscalls.nrs: $(srctree)/usr/include/sys/syscall.h FORCE
+	$(call if_changed_dep,syscall.nrs)
+
+
+quit_cmd_syscalls = GEN   $@
+     cmd_syscalls = $(PERL) $(src)/syscalls.pl $< $(srctree) $(obj) \
+                            $(ARCH) $(BITSIZE) $(obj)/syscalls.nrs > $@ \
+                    || rm -f $@
+
+$(obj)/syscalls.mk: $(obj)/SYSCALLS.i $(src)/syscalls.pl \
+                    $(srctree)/usr/lib/arch/$(ARCH)/sysstub.ph \
+		    $(src)/syscommon.h $(obj)/syscalls.nrs
+	$(call cmd,syscalls)
+
+#####
+# Readable errormessages extracted from src..
+targets += errlist.c
+quiet_cmd_errlist = GEN     $@
+      cmd_errlist = $(PERL) $< -errlist > $@ || rm -f $@
+      
+$(obj)/errlist.c: $(obj)/makeerrlist.pl
+	$(call cmd,errlist)
+
+#####
+# Start building klibc
+always := libc.a libc.so klibc.so
+
+#kludge...
+targets    += arch/$(ARCH)/crt0.o
+host-progs := sha1hash
+
+# Shared dependencies
+targets += $(libc-y) $(ARCHOBJS)
+targets += $(syscall-objs) $(socketcall-objs)
+
+libc-deps = $(call objectify, $(libc-y) $(ARCHOBJS)) \
+            $(call objectify, $(syscall-objs) $(socketcall-objs))
+
+######
+# Build static library
+targets += libc.a __static_init.o
+
+quiet_cmd_libc = USERAR  $@
+      cmd_libc = rm -f $@; $(USERAR) rcs $@ $(filter-out FORCE,$^)
+
+$(obj)/libc.a: $(call objectify, __static_init.o) $(libc-dpes) FORCE
+	$(call if_changed,libc)
+
+######
+# Build shared library
+targets += libc.so __shared_init.o
+
+quiet_cmd_libcso = LD      $@
+      cmd_libcso = $(USERLD) $(USERLDFLAGS) $(USERSHAREDFLAGS) \
+                             -o $@ $(filter-out FORCE,$^)
+
+$(obj)/libc.so: $(call objectify, arch/$(ARCH)/crt0.o __shared_init.o) \
+                $(libc-deps) FORCE
+	$(call if_changed,libcso)
+
+#####
+# Build with hash
+targets += klibc.so
+host-progs := sha1hash
+
+quiet_cmd_klibc.so = GEN     $@
+      cmd_klibc.so = cat $< > $@; $(USERSTRIP) $(USERSTRIPFLAGS) $@;     \
+                     rm -f $(obj)/klibc-??????????????????????.so;       \
+                     ln -f $@ $(obj)/klibc-`cat $(obj)/libc.so.hash`.so
+
+$(obj)/klibc.so: $(obj)/libc.so $(obj)/libc.so.hash $(obj)/sha1hash FORCE
+	$(call if_changed,klibc.so)
+
+
+targets += libc.so.hash
+quiet_cmd_libc.so.hash = HASH    $@
+      cmd_libc.so.hash = $(USERNM) $< | egrep '^[0-9a-fA-F]+ [ADRTW] ' | \
+                                        sort | $(obj)/sha1hash > $@
 
-tests: $(TESTS)
+$(obj)/libc.so.hash: $(obj)/libc.so $(obj)/sha1hash FORCE
+	$(call if_changed,libc.so.hash)
 
-tests/%.o : tests/%.c
-	$(CC) $(CFLAGS) -c -o $@ $<
 
-# This particular file uses a bunch of formats gcc don't know of, in order
-# to test the full range of our vsnprintf() function.  This outputs a bunch
-# of useless warnings unless we tell it not to.
-tests/testvsnp.o : tests/testvsnp.c
-	$(CC) $(CFLAGS) -Wno-format -c -o $@ $<
-
-tests/% : tests/%.o $(LIB) $(CRT0)
-	$(LD) $(LDFLAGS) -o $@ $(CRT0) $< $(LIB) $(LIBGCC)
-	cp $@ $@.stripped
-	$(STRIP) $@.stripped
-
-tests/%.shared : tests/%.o interp.o $(SOLIB)
-	$(LD) $(LDFLAGS) -o $@ $(EMAIN) interp.o tests/$*.o -R $(SOLIB) $(LIBGCC)
-	cp $@ $@.stripped
-	$(STRIP) $@.stripped
-
-$(LIB): __static_init.o $(LIBOBJS) $(ARCHOBJS) syscalls/static.obj socketcalls/static.obj
-	rm -f $(LIB)
-	$(AR) cq $(LIB) __static_init.o $(LIBOBJS) $(ARCHOBJS) \
-		$(wildcard syscalls/*.o) $(wildcard socketcalls/*.o)
-	$(RANLIB) $(LIB)
-
-$(SOLIB): $(CRT0) __shared_init.o $(LIBOBJS) $(ARCHOBJS) syscalls/static.obj socketcalls/static.obj
-	$(LD) $(LDFLAGS) $(SHAREDFLAGS) -o $@ \
-		$(CRT0) __shared_init.o $(LIBOBJS) $(ARCHOBJS) \
-	        $(wildcard syscalls/*.o) $(wildcard socketcalls/*.o) \
-		$(LIBGCC)
-
-sha1hash: sha1hash.c
-	$(HOST_CC) $(HOST_CFLAGS) $(HOST_LDFLAGS) -o $@ $<
-
-$(SOLIB).hash: $(SOLIB) sha1hash
-	$(NM) $(SOLIB) | \
-		egrep '^[0-9a-fA-F]+ [ADRTW] ' | sort | ./sha1hash > $@
-
-$(SOHASH): $(SOLIB) $(SOLIB).hash
-	cp -f $(SOLIB) $@
-	$(STRIP) $@
-	rm -f klibc-??????????????????????.so
-	ln -f $@ klibc-`cat $(SOLIB).hash`.so
 
 interp.o: interp.S $(SOLIB).hash
 	$(CC) $(CFLAGS) -D__ASSEMBLY__ -DLIBDIR=\"$(SHLIBDIR)\" \
 		-DSOHASH=\"`cat $(SOLIB).hash`\" \
 		-c -o $@ $<
 
-crt0.o: arch/$(ARCH)/crt0.o
-	cp arch/$(ARCH)/crt0.o .
-
-errlist.c:
-	$(PERL) makeerrlist.pl -errlist > $@ || rm -f $@
-
-# We pass -ansi to keep cpp from define e.g. "i386" as well as "__i386__"
-SYSCALLS.i: SYSCALLS.def
-	$(CC) $(CFLAGS) -D__ASSEMBLY__ -ansi -x assembler-with-cpp -E -o $@ $<
 
-syscalls.nrs: include/sys/syscall.h arch/$(ARCH)/include/klibc/archsys.h ../linux/include/asm/unistd.h
-	$(CC) $(CFLAGS) -Wp,-dM -x c -E -o $@ include/sys/syscall.h
+# Additional cleanup upon make clean
+clean-files += *.so *.hash
 
-syscalls.dir: SYSCALLS.i syscalls.pl arch/$(ARCH)/sysstub.ph syscommon.h syscalls.nrs
-	rm -rf syscalls
-	mkdir syscalls
-	$(PERL) syscalls.pl SYSCALLS.i $(ARCH) $(BITSIZE) syscalls.nrs
-	touch $@
-
-include/klibc/havesyscall.h: syscalls.dir
-	: Generated by side effect
-
-socketcalls.dir: SOCKETCALLS.def socketcalls.pl socketcommon.h
-	rm -rf socketcalls
-	mkdir socketcalls
-	$(PERL) socketcalls.pl $(ARCH) SOCKETCALLS.def
-	touch $@
-
-%/static.obj: %.dir
-	$(MAKE) objects-$(basename $(notdir $@)) DIR=$*/
-
-STATIC = $(addsuffix .o,$(basename $(wildcard $(DIR)*.[cS])))
-
-objects-static: $(STATIC)
-	touch $(DIR)static.obj
-
-clean: archclean
-	find . -type f -a \( -name \*.[isoa] -o -name \*.l[iso] \) -print0 | xargs -0rt rm -f
-	rm -f *.a *.so *.hash *.syms *.stripped
-	rm -f $(TESTS) tests/*.stripped
-	rm -rf syscalls syscalls.dir
-	rm -rf socketcalls socketcalls.dir
-	rm -f sha1hash errlist.c
-
-spotless: clean
-	find . \( -name \*~ -o -name '.*.d' \) -not -type d -print0 | \
-		xargs -0rt rm -f
-
-bitsize:
-	@echo $(BITSIZE)
-
-ifneq ($(wildcard $(DIR).*.d),)
-include $(wildcard $(DIR).*.d)
-endif
===== usr/lib/makeerrlist.pl 1.1 vs edited =====
--- 1.1/usr/lib/makeerrlist.pl	2004-07-24 03:03:57 +02:00
+++ edited/usr/lib/makeerrlist.pl	2004-07-25 14:33:24 +02:00
@@ -10,7 +10,7 @@ use FileHandle;
 %errors  = ();
 %errmsg  = ();
 $maxerr  = -1;
-$rootdir = '../linux/include/';	# Must have trailing /
+$rootdir = 'include/';	# Must have trailing /
 
 sub parse_file($) {
     my($file) = @_;
===== usr/lib/socketcalls.pl 1.1 vs edited =====
--- 1.1/usr/lib/socketcalls.pl	2004-07-24 03:05:15 +02:00
+++ edited/usr/lib/socketcalls.pl	2004-07-25 13:26:33 +02:00
@@ -1,10 +1,16 @@
 #!/usr/bin/perl
-($arch, $file) = @ARGV;
+($obj, $arch, $file) = @ARGV;
 
 if (!open(FILE, "< $file")) {
     print STDERR "$file: $!\n";
     exit(1);
 }
+if (!-d "${obj}/socketcalls") {
+    mkdir("${obj}/socketcalls")
+       or die "$0: cannot create ${obj}/socketcalls: $!";
+}
+
+print "socketcall-objs := ";
 
 while ( defined($line = <FILE>) ) {
     chomp $line;
@@ -24,10 +30,12 @@ while ( defined($line = <FILE>) ) {
 	    push(@cargs, "$arg a".$i++);
 	}
 	$nargs = $i;
-
+	
+        print " \\\n\tsocketcalls/${name}.o";
+	
 	if ( $arch eq 'i386' ) {
-	    open(OUT, '>', "socketcalls/${name}.S")
-		or die "$0: Cannot open socketcalls/${name}.S\n";
+	    open(OUT, '>', "${obj}/socketcalls/${name}.S")
+		or die "$0: Cannot open ${obj}/socketcalls/${name}.S\n";
 
 	    print OUT "#include <sys/socketcalls.h>\n";
 	    print OUT "\n";
@@ -41,8 +49,8 @@ while ( defined($line = <FILE>) ) {
 	    print OUT "\t.size ${name},.-${name}\n";
 	    close(OUT);
 	} else {
-	    open(OUT, '>', "socketcalls/${name}.c")
-		or die "$0: Cannot open socketcalls/${name}.c\n";
+	    open(OUT, '>', "${obj}/socketcalls/${name}.c")
+		or die "$0: Cannot open ${obj}/socketcalls/${name}.c\n";
 
 	    print OUT "#include \"socketcommon.h\"\n";
 	    print OUT "\n";
===== usr/lib/syscalls.pl 1.1 vs edited =====
--- 1.1/usr/lib/syscalls.pl	2004-07-24 03:06:18 +02:00
+++ edited/usr/lib/syscalls.pl	2004-07-25 12:29:12 +02:00
@@ -3,9 +3,14 @@
 # Script to parse the SYSCALLS file and generate appropriate
 # stubs.
 
-($file, $arch, $bits, $unistd) = @ARGV;
+($file, $srctree, $obj, $arch, $bits, $unistd) = @ARGV;
 
-require "arch/$arch/sysstub.ph";
+require "${srctree}/usr/lib/arch/$arch/sysstub.ph";
+
+if (!-d "${obj}/syscalls") {
+    mkdir("${obj}/syscalls")
+        or die "$0: cannot make ${obj}/syscalls: $!";
+}
 
 if (!open(UNISTD, '<', $unistd)) {
     printf STDERR "$0: $unistd: $!\n";
@@ -21,8 +26,8 @@ while ( defined($line = <UNISTD>) ) {
 }
 close(UNISTD);
 
-if (!open(HAVESYS, '>', "include/klibc/havesyscall.h")) {
-    printf STDERR "$0: include/klibc/havesyscall.h: $!\n";
+if (!open(HAVESYS, '>', "${srctree}/usr/include/klibc/havesyscall.h")) {
+    printf STDERR "$0: ${srctree}/usr/include/klibc/havesyscall.h: $!\n";
     exit(1);
 }
 
@@ -34,6 +39,8 @@ if (!open(FILE, '<', $file)) {
     exit(1);
 }
 
+print "syscall-objs := ";
+
 while ( defined($line = <FILE>) ) {
     chomp $line;
     $line =~ s/\s*(|[\#;].*)$//; # Strip comments and trailing blanks
@@ -92,6 +99,8 @@ while ( defined($line = <FILE>) ) {
 	@args = split(/\s*\,\s*/, $argv);
 
 	print HAVESYS "#define _KLIBC_HAVE_SYSCALL_${fname} ${sname}\n";
+	                                      
+        print " \\\n\tsyscalls/${fname}.o";
 	make_sysstub($fname, $type, $sname, $stype, @args);
     } else {
 	print STDERR "$file:$.: Could not parse input: \"$line\"\n";
===== usr/lib/arch/i386/sysstub.ph 1.1 vs edited =====
--- 1.1/usr/lib/arch/i386/sysstub.ph	2004-07-24 02:54:32 +02:00
+++ edited/usr/lib/arch/i386/sysstub.ph	2004-07-26 00:52:43 +02:00
@@ -8,7 +8,7 @@
 sub make_sysstub($$$$@) {
     my($fname, $type, $sname, $stype, @args) = @_;
 
-    open(OUT, '>', "syscalls/${fname}.S");
+    open(OUT, '>', "${obj}/syscalls/${fname}.S");
     print OUT "#include <asm/unistd.h>\n";
     print OUT "\n";
     print OUT "\t.type ${fname},\@function\n";
--- /dev/null	2004-06-15 23:54:05.000000000 +0200
+++ klibc-kernel/scripts/Makefile.user	2004-07-26 00:54:36.722449408 +0200
@@ -0,0 +1,198 @@
+# ==========================================================================
+# Support for building klibc and related programs
+# ==========================================================================
+#
+# Userspace programs are identified by:
+# user-progs := file
+# And expected to be a single file: file.c
+# If user programs have compositie objects use
+# file-y := file.o file-a.o file-b.o
+
+src := $(obj)
+
+.phony: __build
+__build:
+
+# Defines used when compiling early userspace (klibc & programs)
+# ---------------------------------------------------------------------------
+KLIBSRC         := usr/lib
+
+# Arch specific definitions for klibc
+include $(KLIBSRC)/arch/$(ARCH)/MCONFIG
+
+USERWARNFLAGS   := -Wall -Wpointer-arith -Wwrite-strings                 \
+                   -Wstrict-prototypes -Winline
+
+USERCROSS       := $(CROSS_COMPILE)
+
+USERLD          := $(USERCROSS)ld
+USERCC          := $(USERCROSS)gcc
+USERAR          := $(USERCROSS)ar
+USERSTRIP       := $(USERCROSS)strip
+USERNM          := $(USERCROSS)nm
+
+USERCPPFLAGS    := -I$(KLIBSRC)/arch/$(ARCH)/include                     \
+                   -I$(srctree)/usr/include/bits$(BITSIZE)               \
+                   -I$(srctree)/usr/include                              \
+                   -Iinclude                                             \
+                   -D__KLIBC__ -DBITSIZE=$(BITSIZE)
+USERCFLAGS      := $(USERCPPFLAGS) $(ARCHREQFLAGS) $(OPTFLAGS)           \
+                   $(USERWARNFLAGS)
+USERAFLAGS      := -D__ASSEMBLY__ $(USERCPPFLAGS)
+USERSTRIPFLAGS  := --strip-all -R .comment -R .note
+
+USERLIBGCC      := $(shell $(USERCC) --print-libgcc)
+USERSHAREDFLAGS := $(SHAREDFLAGS)
+USERCRT0        := $(KLIBSRC)/arch/$(ARCH)/crt0.o
+USERLIBC        := $(KLIBSRC)/libc.a
+
+export USERLD USERCC USERAR USERSTRIP USERNM
+export USERCFLAGS USERAFLAGS USERLIBGCC USERSHAREDFLAGS USERSTRIPFLAGS
+export USERCRT0 USERLIBC
+
+# kernel configuration
+include .config
+
+
+# Add $(obj)/ for paths that is not absolute
+objectify	 = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
+
+# Makefile in the directory that is being build
+include $(obj)/Makefile
+
+
+#build-multi	:= $(foreach o,$(targets),$(if $(deps_$(o)),$(o)))
+#build-y		:= $(foreach o,$(build-multi),$(deps_$(o)))
+
+include scripts/Makefile.lib
+
+#build-multi	:= $(addprefix $(obj)/,$(build-multi))
+#build-y		:= $(call objectify,$(build-y))
+#real-objs-y	+= $(build-y)
+
+__build: $(subdir-ym) $(targets)
+	@:
+
+# Compile C sources (.c)
+# ---------------------------------------------------------------------------
+
+usercflags	= -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(USERCFLAGS) 
+
+quiet_cmd_cc_s_c = USERCC  $@
+      cmd_cc_s_c = $(USERCC) $(usercflags) -S -o $@ $< 
+
+%.s: %.c FORCE
+	$(call if_changed_dep,cc_s_c)
+
+quiet_cmd_cc_i_c = USERCPP $@
+      cmd_cc_i_c = $(USERCPP) $(usercflags)   -o $@ $<
+
+%.i: %.c FORCE
+	$(call if_changed_dep,cc_i_c)
+
+quiet_cmd_cc_o_c = USERCC  $@
+      cmd_cc_o_c = $(USERCC) $(usercflags) -c -o $@ $<
+
+%.o: %.c FORCE
+	$(call if_changed_dep,cc_o_c)
+
+# Compile assembler sources (.S)
+# ---------------------------------------------------------------------------
+
+useraflags	= -Wp,-MD,$(depfile) -Iusr/lib -Iinclude \
+                  $(USERAFLAGS) $(NOSTDINC_FLAGS)
+
+quiet_cmd_as_s_S = USERCPP $@
+      cmd_as_s_S = $(USERCPP) $(useraflags)   -o $@ $< 
+
+%.s: %.S FORCE
+	$(call if_changed_dep,as_s_S)
+
+quiet_cmd_as_o_S = USERAS  $@
+      cmd_as_o_S = $(USERCC) $(useraflags) -c -o $@ $<
+
+%.o: %.S FORCE
+	$(call if_changed_dep,as_o_S)
+
+targets += $(real-objs-y) $(targets)
+
+# User defined commands...
+# ==========================================================================
+
+$(build-multi) : FORCE
+	$(call if_changed,$(subst $(obj)/,,$@))
+
+# Compile userspace programs for the target
+# ===========================================================================
+
+ifdef user-progs
+
+user-progs	:= $(addprefix $(obj)/,$(user-progs))
+user-single	:= $(user-progs)
+
+__build : $(user-progs)
+
+quiet_cmd_user-ld-single = USERLD  $@
+      cmd_user-ld-single = $(USERLD) $(USERLDFLAGS) -o $@ 		\
+			   $(USERCRT0) $< $(USERLIBC) $(USERLIBGCC);	\
+                           --start-group                                \
+			   $(filter-out FORCE,$^)                       \
+                           --end-group $(USERLIBGCC)                    \
+			   $(USERSTRIP) $(USERSTRIPFLAGS) $@
+
+$(user-single): %: %.o $(USERCRT0) $(USERLIBC) FORCE
+	$(call if_changed,user-ld-single)
+
+targets += $(user-single) $(user-single:=.o)
+
+endif
+
+# Compile user libraries
+# ===========================================================================
+ifdef user-libs
+
+user-libs      := $(addprefix $(obj)/,$(user-libs))
+user-lib-single        := $(user-libs)
+__build        : $(user-libs)
+
+quiet_cmd_user-ar-single = USERAR  $@
+      cmd_user-ar-single = $(USERAR) cru $@ $(filter-out FORCE,$^)
+
+$(user-lib-single): %.a: %.o FORCE
+	$(call if_changed,user-ar-single)
+
+targets += $(user-lib-single)
+
+endif
+
+# Compile programs on the host
+# ===========================================================================
+ifdef host-progs
+include $(srctree)/scripts/Makefile.host
+endif
+
+# Descending
+# ---------------------------------------------------------------------------
+
+.PHONY: $(subdir-ym)
+$(subdir-ym):
+	$(Q)$(MAKE) $(user)=$@
+
+# Add FORCE to the prequisites of a target to force it to be always rebuilt.
+# ---------------------------------------------------------------------------
+
+.PHONY: FORCE
+
+FORCE:
+
+# Read all saved command lines and dependencies for the $(targets) we
+# may be building above, using $(if_changed{,_dep}). As an
+# optimization, we don't need to read them if the target does not
+# exist, we will rebuild anyway in that case.
+
+targets := $(wildcard $(sort $(targets)))
+cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
+
+ifneq ($(cmd_files),)
+  include $(cmd_files)
+endif
--- /dev/null	2004-06-15 23:54:05.000000000 +0200
+++ klibc-kernel/scripts/Makefile.host	2004-07-24 12:22:33.000000000 +0200
@@ -0,0 +1,86 @@
+# Compile programs on the host
+# ===========================================================================
+# host-progs := bin2hex
+# Will compile bin2hex.c and create an executable named bin2hex
+#
+# host-progs    := lxdialog
+# lxdialog-objs := checklist.o lxdialog.o
+# Will compile lxdialog.c and checklist.c, and then link the executable
+# lxdialog, based on checklist.o and lxdialog.o
+#
+# host-progs      := qconf
+# qconf-cxxobjs   := qconf.o
+# qconf-objs      := menu.o
+# Will compile qconf as a C++ program, and menu as a C program.
+# They are linked as C++ code to the executable qconf
+
+# host-progs := conf
+# conf-objs  := conf.o libkconfig.so
+# libkconfig-objs := expr.o type.o
+# Will create a shared library named libkconfig.so that consist of
+# expr.o and type.o (they are both compiled as C code and the object file
+# are made as position independent code).
+# conf.c is compiled as a c program, and conf.o is linked together with
+# libkconfig.so as the executable conf.
+# Note: Shared libraries consisting of C++ files are not supported  
+#
+
+# Create executable from a single .c file
+# host-csingle -> Executable
+quiet_cmd_host-csingle 	= HOSTCC  $@
+      cmd_host-csingle	= $(HOSTCC) $(hostc_flags) $(HOST_LOADLIBES) -o $@ $<
+$(host-csingle): %: %.c FORCE
+	$(call if_changed_dep,host-csingle)
+
+# Link an executable based on list of .o files, all plain c
+# host-cmulti -> executable
+quiet_cmd_host-cmulti	= HOSTLD  $@
+      cmd_host-cmulti	= $(HOSTCC) $(HOSTLDFLAGS) -o $@ \
+			  $(addprefix $(obj)/,$($(@F)-objs)) \
+			  $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
+$(host-cmulti): %: $(host-cobjs) $(host-cshlib) FORCE
+	$(call if_changed,host-cmulti)
+
+# Create .o file from a single .c file
+# host-cobjs -> .o
+quiet_cmd_host-cobjs	= HOSTCC  $@
+      cmd_host-cobjs	= $(HOSTCC) $(hostc_flags) -c -o $@ $<
+$(host-cobjs): %.o: %.c FORCE
+	$(call if_changed_dep,host-cobjs)
+
+# Link an executable based on list of .o files, a mixture of .c and .cc
+# host-cxxmulti -> executable
+quiet_cmd_host-cxxmulti	= HOSTLD  $@
+      cmd_host-cxxmulti	= $(HOSTCXX) $(HOSTLDFLAGS) -o $@ \
+			  $(foreach o,objs cxxobjs,\
+			  $(addprefix $(obj)/,$($(@F)-$(o)))) \
+			  $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
+$(host-cxxmulti): %: $(host-cobjs) $(host-cxxobjs) $(host-cshlib) FORCE
+	$(call if_changed,host-cxxmulti)
+
+# Create .o file from a single .cc (C++) file
+quiet_cmd_host-cxxobjs	= HOSTCXX $@
+      cmd_host-cxxobjs	= $(HOSTCXX) $(hostcxx_flags) -c -o $@ $<
+$(host-cxxobjs): %.o: %.cc FORCE
+	$(call if_changed_dep,host-cxxobjs)
+
+# Compile .c file, create position independent .o file
+# host-cshobjs -> .o
+quiet_cmd_host-cshobjs	= HOSTCC  -fPIC $@
+      cmd_host-cshobjs	= $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $<
+$(host-cshobjs): %.o: %.c FORCE
+	$(call if_changed_dep,host-cshobjs)
+
+# Link a shared library, based on position independent .o files
+# *.o -> .so shared library (host-cshlib)
+quiet_cmd_host-cshlib	= HOSTLLD -shared $@
+      cmd_host-cshlib	= $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \
+			  $(addprefix $(obj)/,$($(@F:.so=-objs))) \
+			  $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
+$(host-cshlib): %: $(host-cshobjs) FORCE
+	$(call if_changed,host-cshlib)
+
+targets += $(host-csingle)  $(host-cmulti) $(host-cobjs)\
+	   $(host-cxxmulti) $(host-cxxobjs) $(host-cshlib) $(host-cshobjs) 
+
+



More information about the klibc mailing list