[klibc] [PATCH/RFC] klibc/kbuild: use separate kbuild files for each klibc subdirectory

Sam Ravnborg sam at ravnborg.org
Sun Jul 9 14:23:07 PDT 2006


This fixes a long standing issue where it was not possible to
do "make usr/klibc/arch/x86_64/longjmp.o" in the kernel.

The principle is that all .o files to be part of klibc are listed
with klib-y. For each directory a klib.list file is made that specify
all .o file and the final AR then adds all .o files to create libc.a.

This patch introduce the infrastructure and converts x86_64 to use it.
And it breaks all other architectures...

Signed-off-by: Sam Ravnborg <sam at ravnborg.org>
---


This is as more a RFC than a real patch. It changes the kbuild part of
klibc to understand subdirectories when building the library.
My limited testing so far says that it works and it has solved
a few outstanding issues. Now for example touching SYSCALLS.def does
indeed cause a rebuild of klibc library _and_ all programs.

But it breaks all architectures in the process - but fix should be
trivial. Only that I'm too tired in my eyes to do it right away and
wanted to see if this gave some feedback from the list.

Luc van Oosten suggested the use of MRI scripts for ar but in the end
it became simpler to just use a file that listed all .o files.
Also MRI scripts was according to 'info ar' only for backward
compatibility.

Comments?

	Sam


 scripts/Kbuild.klibc               |   67 ++++++++++++++++++++++++++++++++----
 scripts/Makefile.clean             |   32 ++++++++---------
 usr/klibc/Kbuild                   |   59 +++++++++++++-------------------
 usr/klibc/arch/x86_64/Kbuild       |    8 ++++
 usr/klibc/arch/x86_64/Makefile.inc |   13 -------
 usr/klibc/socketcalls/Kbuild       |    6 ++-
 usr/klibc/syscalls/Kbuild          |    6 ++-
 usr/klibc/zlib/Kbuild              |   10 +++++
 8 files changed, 123 insertions(+), 78 deletions(-)

diff --git a/scripts/Kbuild.klibc b/scripts/Kbuild.klibc
index c2294dc..2d1e262 100644
--- a/scripts/Kbuild.klibc
+++ b/scripts/Kbuild.klibc
@@ -14,14 +14,29 @@ # shared-y := cats
 # # This will compile a file named cats.c -> the executable 'cats'
 # # The executable will be linked shared
 #
-# If the userspace program consist of more files do the following:
+# If the userspace program consist of composite files do the following:
 # Kbuild:
 #
-# static-y   := ipconfig
-# ipconfig-y := main.o netdev.c
+# static-y   := kinit
+# kinit-y := main.o netdev.c
 # So ipconfig will be linked statically using the two .o files
 # specified with ipconfig-y.
 #
+# Are part of the program located in a sub-directory do like this:
+# kinit-y += ipconfig/
+#
+# And in the subdirectory:
+# ipconfig/Kbuild:
+# lib-y := packet.o dhcp_proto.o
+#
+#####
+# For a klibc libary file do like this
+# klibc/Kbuild
+# klib-y := error.o pipe.o zlib/
+#
+#####
+# Handling of compiler/linker options
+#
 # To set directory wide CFLAGS use:
 # EXTRA_KLIBCCFLAGS := -DDEBUG
 # To set directory wide AFLAGS use:
@@ -146,10 +161,22 @@ kprog-objs := $(patsubst %/, %/lib.a, $(
 
 targets += $(static-y) $(shared-y)
 
+#####
+# klib-y handling
+# .o files to build in this dir
+klib-real-objs := $(patsubst %/,,$(klib-y))
+# Directories we need to visit before libs are up-to-date
+klib-dirs := $(patsubst %/,%,$(filter %/, $(klib-y)))
+# replace all dir/ with dir/klib.list
+klib-objs := $(patsubst %/, %/klib.list, $(klib-y))
+# remove all dirs
+klib-y := $(filter-out %/, $(klib-y))
+
 # $(output-dirs) are a list of directories that contain object files
 output-dirs := $(dir $(kprog-dirs) $(kprog-objs))
 output-dirs += $(foreach f, $(hostprogs-y) $(targets), \
                $(if $(dir $(f)), $(dir $(f))))
+output-dirs += $(klib-dirs)
 output-dirs := $(strip $(sort $(filter-out ./,$(output-dirs))))
 
 # prefix so we get full dir
@@ -160,9 +187,13 @@ kprog-real-objs := $(addprefix $(obj)/,$
 output-dirs     := $(addprefix $(obj)/,$(output-dirs))
 kprog-dirs      := $(addprefix $(obj)/,$(kprog-dirs))
 subdir-y        := $(addprefix $(obj)/,$(subdir-y))
-lib-y           := $(addprefix $(obj)/,$(lib-y))
 always          := $(addprefix $(obj)/,$(always))
 targets         := $(addprefix $(obj)/,$(targets))
+lib-y           := $(addprefix $(obj)/,$(lib-y))
+klib-y          := $(addprefix $(obj)/,$(klib-y))
+klib-objs       := $(addprefix $(obj)/,$(klib-objs))
+klib-real-objs  := $(addprefix $(obj)/,$(klib-real-objs))
+klib-dirs       := $(addprefix $(obj)/,$(klib-dirs))
 
 #####
 # Handle options to gcc. Support building with separate output directory
@@ -194,7 +225,7 @@ lib-target := $(obj)/lib.a
 endif
 
 __build: $(subdir-y) $(lib-target) $(always)
-	@:
+	$(Q):
 
 # Compile C sources (.c)
 # ---------------------------------------------------------------------------
@@ -242,6 +273,28 @@ cmd_link_o_target = $(if $(strip $(lib-y
 targets += $(lib-target) $(lib-y)
 endif # lib-target
 
+#
+# Create klib.list
+#
+# Do we have to create a klibc library file in this dir?
+ifneq ($(strip $(klib-y) $(klib-n) $(klib-)),)
+klib-target := $(obj)/klib.list
+endif
+
+ifdef klib-target
+# include this in build
+__build: $(klib-target) $(klib-dirs)
+
+# descend if needed
+$(sort $(addsuffix /klib.list,$(klib-dirs))): $(klib-dirs) ;
+
+# create klib.list
+quiet_cmd_klib-list = LIST    $@
+      cmd_klib-list = echo $(klib-y) > $@
+$(klib-target): $(klib-objs) FORCE
+	$(call if_changed,klib-list)
+targets += $(klib-target) $(klib-y)
+endif # klib-target
 
 ifdef kprogs
 # Compile klibc-programs for the target
@@ -311,8 +364,8 @@ endif
 # Descending
 # ---------------------------------------------------------------------------
 
-.PHONY: $(subdir-y) $(kprog-dirs)
-$(sort $(subdir-y) $(kprog-dirs)):
+.PHONY: $(subdir-y) $(kprog-dirs) $(klib-dirs)
+$(sort $(subdir-y) $(kprog-dirs) $(klib-dirs)):
 	$(Q)$(MAKE) $(klibc)=$@
 
 # Add FORCE to the prequisites of a target to force it to be always rebuilt.
diff --git a/scripts/Makefile.clean b/scripts/Makefile.clean
index 8974ea5..a588749 100644
--- a/scripts/Makefile.clean
+++ b/scripts/Makefile.clean
@@ -19,23 +19,18 @@ include $(if $(wildcard $(kbuild-dir)/Kb
 # Figure out what we need to build from the various variables
 # ==========================================================================
 
-__subdir-y	:= $(patsubst %/,%,$(filter %/, $(obj-y)))
-subdir-y	+= $(__subdir-y)
-__subdir-m	:= $(patsubst %/,%,$(filter %/, $(obj-m)))
-subdir-m	+= $(__subdir-m)
-__subdir-n	:= $(patsubst %/,%,$(filter %/, $(obj-n)))
-subdir-n	+= $(__subdir-n)
-__subdir-	:= $(patsubst %/,%,$(filter %/, $(obj-)))
-subdir-		+= $(__subdir-)
+subdirs := $(subdir-y) $(subdir-m) $(subdir-n) $(subdir-)
+subdirs += $(patsubst %/,%,$(filter %/, $(obj-y)))
+subdirs += $(patsubst %/,%,$(filter %/, $(obj-m)))
+subdirs += $(patsubst %/,%,$(filter %/, $(obj-n)))
+subdirs += $(patsubst %/,%,$(filter %/, $(obj-)))
 
-# Subdirectories we need to descend into
-
-subdir-ym	:= $(sort $(subdir-y) $(subdir-m))
-subdir-ymn      := $(sort $(subdir-ym) $(subdir-n) $(subdir-))
+subdirs += $(patsubst %/,%,$(filter %/, $(klib-y)))
+subdirs += $(patsubst %/,%,$(filter %/, $(klib-)))
 
-# Add subdir path
+# Subdirectories we need to descend into
+subdirs := $(addprefix $(obj)/,$(sort $(subdirs)))
 
-subdir-ymn	:= $(addprefix $(obj)/,$(subdir-ymn))
 
 # build a list of files to remove, usually releative to the current
 # directory
@@ -43,7 +38,8 @@ # directory
 __clean-files	:= $(extra-y) $(EXTRA_TARGETS) $(always) \
 		   $(targets) $(clean-files)             \
 		   $(host-progs)                         \
-		   $(hostprogs-y) $(hostprogs-m) $(hostprogs-)
+		   $(hostprogs-y) $(hostprogs-m) $(hostprogs-) \
+		   klib.list
 
 # as clean-files is given relative to the current directory, this adds
 # a $(obj) prefix, except for absolute paths
@@ -67,7 +63,7 @@ quiet_cmd_cleandir = CLEAN   $(__clean-d
       cmd_cleandir = rm -rf $(__clean-dirs)
 
 
-__clean: $(subdir-ymn)
+__clean: $(subdirs)
 ifneq ($(strip $(__clean-files)),)
 	+$(call cmd,clean)
 endif
@@ -87,8 +83,8 @@ # ======================================
 # Descending
 # ---------------------------------------------------------------------------
 
-.PHONY: $(subdir-ymn)
-$(subdir-ymn):
+.PHONY: $(subdirs)
+$(subdirs):
 	$(Q)$(MAKE) $(clean)=$@
 
 # If quiet is set, only print short version of command
diff --git a/usr/klibc/Kbuild b/usr/klibc/Kbuild
index a1e8763..0f4eadf 100644
--- a/usr/klibc/Kbuild
+++ b/usr/klibc/Kbuild
@@ -3,9 +3,9 @@ # Kbuild file for klibc
 #
 
 # Tell that we are building klibc
-klibc-build := y
+export klibc-build := y
 
-libc-y := vsnprintf.o snprintf.o vsprintf.o sprintf.o \
+klib-y := vsnprintf.o snprintf.o vsprintf.o sprintf.o \
 	  asprintf.o vasprintf.o \
 	  vsscanf.o sscanf.o ctypes.o \
 	  strntoumax.o strntoimax.o \
@@ -52,19 +52,15 @@ libc-y := vsnprintf.o snprintf.o vsprint
 	  ctype/ispunct.o ctype/isspace.o ctype/isupper.o \
 	  ctype/isxdigit.o ctype/tolower.o ctype/toupper.o
 
-libc-$(CONFIG_KLIBC_ERRLIST) += errlist.o
+klib-$(CONFIG_KLIBC_ERRLIST) += errlist.o
 
 ifeq ($(CONFIG_KLIBC_ERRLIST),y)
 KLIBCCFLAGS_strerror.o += -DWITH_ERRLIST
 endif
 
-libc-$(CONFIG_KLIBC_ZLIB)    += \
-	zlib/adler32.o zlib/compress.o zlib/crc32.o zlib/gzio.o \
-	zlib/uncompr.o zlib/deflate.o zlib/trees.o zlib/zutil.o \
-	zlib/inflate.o zlib/infback.o zlib/inftrees.o zlib/inffast.o
-
-# zlib specific flag
-KLIBCCFLAGS += -DDYNAMIC_CRC_TABLE
+klib-$(CONFIG_KLIBC_ZLIB) += zlib/
+# arch specific .o files
+klib-y += arch/$(KLIBCARCHDIR)/
 
 #####
 # Add any architecture-specific rules
@@ -72,14 +68,14 @@ include $(obj)/arch/$(KLIBCARCHDIR)/Make
 
 #####
 # Shared definitions
-LIB      := libc.a
+LIBC     := libc.a
 SOLIB    := libc.so
 SOHASH   := klibc.so
 CRT0     := arch/$(KLIBCARCHDIR)/crt0.o
 INTERP_O := interp.o
 
-always   := $(CRT0) $(LIB) $(SOLIB) $(SOHASH) $(INTERP_O)
-LIB      := $(call objectify,$(LIB))
+always   := $(LIBC) $(SOLIB) $(SOHASH) $(INTERP_O)
+LIBC     := $(call objectify,$(LIBC))
 SOLIB    := $(call objectify,$(SOLIB))
 SOHASH   := $(call objectify,$(SOHASH))
 CRT0     := $(call objectify,$(CRT0))
@@ -87,17 +83,10 @@ INTERP_O := $(call objectify,$(INTERP_O)
 
 SOLIBHASH = $(shell cat $(SOLIB).hash)
 
-targets  += arch/$(KLIBCARCHDIR)/crt0.o
-targets  += $(libc-y) $(KLIBCARCHOBJS)
-
 # Generate syscall stubs
-subdir-y += syscalls
+klib-y += syscalls/
 # Generate socket calls stubs
-subdir-y += socketcalls
-
-# Tell make to descend before building libs
-$(obj)/syscalls/syscalls.list: $(obj)/syscalls
-$(obj)/socketcalls/socketcalls.list: $(obj)/socketcalls
+klib-y += socketcalls/
 
 #####
 # Readable errormessages extracted from src..
@@ -108,20 +97,20 @@ quiet_cmd_errlist = GEN     $@
 $(obj)/errlist.c: $(srctree)/$(src)/makeerrlist.pl
 	$(call cmd,errlist)
 
-# full list of dependencies for klibc
-libc-deps = $(call objectify, $(libc-y) $(KLIBCARCHOBJS)) \
-            $(call objectify, syscalls/syscalls.list socketcalls/socketcalls.list)
 
+# 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) cq $@ \
-		 	$(patsubst %.list,`cat %.list`,$(filter-out FORCE,$^)); \
+      cmd_libc = rm -f $@;						\
+                 $(KLIBCAR) cq $@					\
+		 $(call objectify,__static_init.o) $(klib-o-files);	\
                  $(KLIBCRANLIB) $@
 
-$(LIB): $(call objectify,__static_init.o) $(libc-deps) FORCE
+$(LIBC): $(call objectify,__static_init.o) $(obj)/klib.list FORCE
 	$(call if_changed,libc)
 
 ######
@@ -130,12 +119,14 @@ targets += libc.so __shared_init.o
 
 quiet_cmd_libcso = KLIBCLD $@
       cmd_libcso = $(KLIBCLD) $(KLIBCLDFLAGS) $(KLIBCSHAREDFLAGS) -o $@ \
-			--start-group \
-			$(patsubst %.list,`cat %.list`,$(filter-out FORCE,$^)) \
-			$(KLIBCLIBGCC) \
+			--start-group					\
+				$(CRT0)					\
+				$(call objectify,__shared_init.o)	\
+				$(klib-o-files)				\
+				$(KLIBCLIBGCC)				\
 			--end-group
 
-$(SOLIB): $(CRT0) $(call objectify,__shared_init.o) $(libc-deps) FORCE
+$(SOLIB): $(call objectify,__shared_init.o) $(obj)/klib.list FORCE
 	$(call if_changed,libcso)
 
 
@@ -177,7 +168,7 @@ #####
 # Install klibc
 install-rule:
 	@echo "  INSTALL klibc to $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib"
-	$(Q)$(foreach f, $(LIB) $(SOLIB) $(CRT0) $(INTERP_O), \
+	$(Q)$(foreach f, $(LIBC) $(SOLIB) $(CRT0) $(INTERP_O), \
 	  $(shell $(install-data) $(f) \
 	          $(INSTALLROOT)$(INSTALLDIR)/$(KLIBCCROSS)lib))
 	$(Q)$(install-lib) $(obj)/klibc-$(SOLIBHASH).so \
diff --git a/usr/klibc/arch/x86_64/Kbuild b/usr/klibc/arch/x86_64/Kbuild
new file mode 100644
index 0000000..3f69c5b
--- /dev/null
+++ b/usr/klibc/arch/x86_64/Kbuild
@@ -0,0 +1,8 @@
+# -*- makefile -*-
+#
+# klibc files for x86_64
+
+always  := crt0.o
+targets := crt0.o
+
+klib-y := setjmp.o syscall.o sigreturn.o vfork.o
diff --git a/usr/klibc/arch/x86_64/Makefile.inc b/usr/klibc/arch/x86_64/Makefile.inc
index 4bfe56a..93346a2 100644
--- a/usr/klibc/arch/x86_64/Makefile.inc
+++ b/usr/klibc/arch/x86_64/Makefile.inc
@@ -1,18 +1,5 @@
-# -*- makefile -*-
-#
 # arch/x86_64/Makefile.inc
 #
 # Special rules for this architecture.  Note that this is actually
 # included from the main Makefile, and that pathnames should be
 # accordingly.
-#
-
-KLIBCARCHOBJS = \
-	arch/$(KLIBCARCH)/setjmp.o \
-	arch/$(KLIBCARCH)/syscall.o \
-	arch/$(KLIBCARCH)/sigreturn.o \
-	arch/$(KLIBCARCH)/vfork.o
-
-KLIBCARCHSOOBJS = $(patsubst %.o,%.lo,$(KLIBCARCHOBJS))
-
-archclean:
diff --git a/usr/klibc/socketcalls/Kbuild b/usr/klibc/socketcalls/Kbuild
index c106182..f0fc9a8 100644
--- a/usr/klibc/socketcalls/Kbuild
+++ b/usr/klibc/socketcalls/Kbuild
@@ -9,12 +9,12 @@ ifeq ($(clean),)
 endif
 
 # Listing of all .o files
-always := socketcalls.list
+always := klib.list
 
 #####
 # Generate socket calls stubs
 # Based on input from SOCKETCALLS.def generate socket call stubs
-targets     := socketcalls.list
+targets     := klib.list
 targets     += socketcalls.mk
 targets	    += SOCKETCALLS.i
 targets     += $(socketcall-objs)
@@ -26,7 +26,7 @@ quiet_cmd_makelist = LIST    $@
       cmd_makelist = echo '$(filter-out FORCE,$^)' > $@
 
 # Create list of all files
-$(obj)/socketcalls.list: $(call objectify,$(socketcall-objs)) FORCE
+$(obj)/klib.list: $(call objectify,$(socketcall-objs)) FORCE
 	$(call if_changed,makelist)
 
 # Generate assembler file (.i)
diff --git a/usr/klibc/syscalls/Kbuild b/usr/klibc/syscalls/Kbuild
index a1d408d..8a5b94d 100644
--- a/usr/klibc/syscalls/Kbuild
+++ b/usr/klibc/syscalls/Kbuild
@@ -9,7 +9,7 @@ ifeq ($(clean),)
 endif
 
 # Listing of all .o files
-always := syscalls.list
+always := klib.list
 
 
 #####
@@ -17,7 +17,7 @@ # 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.mk
-targets += syscalls.list
+targets += klib.list
 targets += SYSCALLS.i syscalls.nrs
 targets += typesize.c typesize.o typesize.bin
 targets += $(syscall-objs)
@@ -34,7 +34,7 @@ quiet_cmd_makelist = LIST    $@
       cmd_makelist = echo '$(filter-out FORCE,$^)' > $@
 
 # Create list of all files
-$(obj)/syscalls.list: $(call objectify,$(syscall-objs)) FORCE
+$(obj)/klib.list: $(call objectify,$(syscall-objs)) FORCE
 	$(call if_changed,makelist)
 
 # Generate assembler file (.i)
diff --git a/usr/klibc/zlib/Kbuild b/usr/klibc/zlib/Kbuild
new file mode 100644
index 0000000..a561d42
--- /dev/null
+++ b/usr/klibc/zlib/Kbuild
@@ -0,0 +1,10 @@
+# zlib
+
+klib-y := adler32.o compress.o crc32.o gzio.o
+klib-y += uncompr.o deflate.o trees.o zutil.o
+klib-y += inflate.o infback.o inftrees.o inffast.o
+
+# zlib specific flag
+EXTRA_KLIBCCFLAGS := -DDYNAMIC_CRC_TABLE
+
+
-- 
1.4.1.rc2.gfc04



More information about the klibc mailing list