[klibc] kbuild: Included kinit,
nfsmount and ipconfig in kernel build
Sam Ravnborg
sam at ravnborg.org
Sun Jul 31 15:02:45 PDT 2005
The kinit (and the rest) are build both as static and shared linked
executables. To support this in a clean way the support in
Kbuild.klibc was reworked considerably.
Now to tell when you want a static linked executables use:
static-y := binary
Likewise for shared executables use:
shared-y := binary
To test things out a Kbuild file was created for kinit, ipconfig and nfsmount.
Things worked out pretty well and the syntax is kept nice and readable.
Due to the fact that kinit uses ipconfig and nfsmount they were in the
kernel placed as subdirectories for kinit. This is the only sane way to
express the dependencies when doing recursive build like in the kernel.
With the current scheme a .o file is created for ipconfig and nfsmount.
If we are better served with a ar file then this is easy to fix.
Signed-off-by: Sam Ravnborg <sam at ravnborg.org>
---
commit 6ec5f2d923dc945d942a36ed7875733678644712
tree 71e225a468135f4162e143a08b38da15cfe8b7f8
parent e7daa9d72c3c7ea2cf23ff34bf601989ced7c6e1
author Sam Ravnborg <sam at mars.(none)> Mon, 01 Aug 2005 00:01:34 +0200
committer Sam Ravnborg <sam at mars.(none)> Mon, 01 Aug 2005 00:01:34 +0200
cp-to-kernel.sh | 18 +++++++
gzip/Kbuild | 4 +-
ipconfig/Kbuild | 35 +++++++++++++
kinit/Kbuild | 27 ++++++++++
nfsmount/Kbuild | 28 +++++++++++
scripts/Kbuild.klibc | 132 ++++++++++++++++++++++++++++----------------------
usr/Kbuild | 3 +
7 files changed, 187 insertions(+), 60 deletions(-)
diff --git a/cp-to-kernel.sh b/cp-to-kernel.sh
--- a/cp-to-kernel.sh
+++ b/cp-to-kernel.sh
@@ -38,6 +38,24 @@ if [ ! -d $kernel/usr/gzip ]; then
fi
cp -R gzip/* $kernel/usr/gzip
+echo "Copying kinit"
+if [ ! -d $kernel/usr/kinit ]; then
+ mkdir -p $kernel/usr/kinit
+fi
+cp -R kinit/* $kernel/usr/kinit
+
+echo "Copying ipconfig"
+if [ ! -d $kernel/usr/kinit/ipconfig ]; then
+ mkdir -p $kernel/usr/kinit/ipconfig
+fi
+cp -R ipconfig/* $kernel/usr/kinit/ipconfig
+
+echo "Copying nfsmount"
+if [ ! -d $kernel/usr/kinit/nfsmount ]; then
+ mkdir -p $kernel/usr/kinit/nfsmount
+fi
+cp -R nfsmount/* $kernel/usr/kinit/nfsmount
+
echo "Copying kbuild files"
cp scripts/Kbuild.klibc $kernel/scripts
# Newer kernel versions have Kbuild.include, so do not overwrite it
diff --git a/gzip/Kbuild b/gzip/Kbuild
--- a/gzip/Kbuild
+++ b/gzip/Kbuild
@@ -3,8 +3,8 @@
#
# The gzip executable
-user-progs := gzip
-gzip-y := gzip.o util.o unzip.o inflate.o
+static-y := gzip
+gzip-y := gzip.o util.o unzip.o inflate.o
# Additional targets
always := gunzip zcat gzip.stripped
diff --git a/ipconfig/Kbuild b/ipconfig/Kbuild
new file mode 100644
--- /dev/null
+++ b/ipconfig/Kbuild
@@ -0,0 +1,35 @@
+#
+# Kbuild file for ipconfig
+#
+
+static-y := static/ipconfig
+shared-y := shared/ipconfig
+
+# common .o files
+objs := main.o netdev.o packet.o
+# dhcp
+objs += dhcp_proto.o
+# bootp
+objs += bootp_proto.o
+
+
+# TODO - do we want a stripped version
+# TODO - do we want the static.g + shared.g directories?
+
+
+# Create built-in.o with all object files (used by kinit)
+obj-y := $(objs)
+
+# .o files used to built executables
+static/ipconfig-y := $(objs)
+shared/ipconfig-y := $(objs)
+
+ifeq (a,b)
+# TODO - can we replace this with built-in.o?
+$(LIB): $(OBJS)
+ $(AR) cru $(LIB) $(OBJS)
+
+install: all
+ $(INSTALL_EXEC) shared/* $(INSTALLROOT)$(INSTALLDIR)/$(CROSS)bin
+
+endif
diff --git a/kinit/Kbuild b/kinit/Kbuild
new file mode 100644
--- /dev/null
+++ b/kinit/Kbuild
@@ -0,0 +1,27 @@
+#
+# Kbuild file for kinit
+#
+
+static-y := kinit
+kinit-y := kinit.o do_mounts.o nfsroot.o getintfile.o initrd.o
+kinit-y += open.o readfile.o
+
+kinit-y += ipconfig/
+kinit-y += nfsmount/
+
+
+# kinit.c include ipconfig header file
+USERCFLAGS_kinit.o := -I$(src)/ipconfig
+# So do nfsroot
+USERCFLAGS_nfsroot.o := -I$(src)/ipconfig -I$(src)/nfsmount
+
+
+# TODO - stripped image
+
+ifeq (a,b)
+
+# This should perhaps be a shared binary?
+install: all
+ $(INSTALL_EXEC) $(PROGS) $(INSTALLROOT)$(INSTALLDIR)/$(CROSS)bin
+
+endif
diff --git a/nfsmount/Kbuild b/nfsmount/Kbuild
new file mode 100644
--- /dev/null
+++ b/nfsmount/Kbuild
@@ -0,0 +1,28 @@
+#
+# kbuild file for nfsmount
+#
+
+static-y := static/nfsmount
+#FIXME - build is broken static-y := dummypmap
+shared-y := shared/nfsmount
+
+objs := main.o mount.o portmap.o dummypmap.o sunrpc.o
+
+# Create built-in.o with all .o files (used by kinit)
+obj-y := $(objs)
+
+# .o files used for executables
+static/nfsmount-y := $(objs)
+shared/nfsmount-y := $(objs)
+
+# dummypmap uses a single .o file (rename src file?)
+dummypmap-y := dummypmap_test.o
+
+# TODO - do we want a stripped version
+# TODO - do we want the static.g + shared.g directories?
+
+ifeq (a,b)
+
+install: all
+ $(INSTALL_EXEC) shared/* $(INSTALLROOT)$(INSTALLDIR)/$(CROSS)bin
+endif
diff --git a/scripts/Kbuild.klibc b/scripts/Kbuild.klibc
--- a/scripts/Kbuild.klibc
+++ b/scripts/Kbuild.klibc
@@ -6,17 +6,35 @@
#
# Kbuild:
#
-# user-progs := cat
+# static-y := cat
+# # This will compile a file named cat.c -> the executable 'cat'
+# # The executable will be linked statically
+#
+# shared-y := cats
+# # This will compile a file named cats.c -> the executable 'cats'
+# # The executable will be linked shared
#
-# This will compile a file named cat.c -> the executable 'cat'
-#
# If the user space program consist of more files do the following:
# Kbuild:
#
-# user-progs := ipconfig
+# static-y := ipconfig
# ipconfig-y := main.o netdev.c
+# So ipconfig will be linked statically using the two .o files
+# specified with ipconfig-y.
+#
+# To set directory wide CFLAGS use:
+# EXTRA_USERCFLAGS := -DDEBUG
+# To set directory wide AFLAGS use:
+# EXTRA_USERAFLAGS := -DDEBUG
+#
+# To set target specific CFLAGS (for .c files) use
+# USERCFLAGS-main.o := -DDEBUG=3
+# To set target specific AFLAGS (for .s files) use
+# USERAFLAGS-main.o := -DDEBUG=3
src := $(obj)
+# Preset target and make sure it is a ':=' variable
+targets :=
.phony: __build
__build:
@@ -55,8 +73,10 @@ USERSTRIPFLAGS := --strip-all -R .comme
USERLIBGCC := $(shell $(USERCC) --print-libgcc)
USERSHAREDFLAGS := $(SHAREDFLAGS)
-USERCRT0 := $(KLIBSRC)/arch/$(ARCH)/crt0.o
-USERLIBC := $(KLIBSRC)/libc.a
+USERCRT0 := $(KLIBOBJ)/arch/$(ARCH)/crt0.o
+USERLIBC := $(KLIBOBJ)/libc.a
+USERCRTSHARED := $(KLIBOBJ)/interp.o
+USERLIBCSHARED := $(KLIBOBJ)/libc.so
#
# This indicates the location of the final version of the shared library.
@@ -79,21 +99,23 @@ objectify = $(foreach o,$(1),$(if $(filt
include $(obj)/Kbuild
#####
-# user-progs := handling
-
-# user-progs based on a single .c file (with same name)
-user-single := $(foreach p, $(user-progs), $(if $($(p)-y),,$(p)))
-
+# static-y + shared-y handling
+user-progs := $(static-y) $(shared-y)
+# user-progs based on a single .o file (with same name + .o)
+user-objs := $(foreach p, $(user-progs), $(if $($(p)-y),,$(p)))
+user-objs := $(addsuffix .o, $(user-objs))
# user-progs which is based on several .o files
-user-multi := $(foreach p, $(user-progs), $(if $($(p)-y),$(p)))
+user-multi := $(foreach p, $(user-progs), $(if $($(p)-y),$(p)))
# objects used for user-progs with more then one .o file
-user-multi-objs := $(foreach p, $(user-multi), $($(p)-y))
+user-objs += $(foreach p, $(user-multi), $($(p)-y))
# objects build in this dir
-user-real-objs := $(patsubst %/,,$(user-multi-objs))
-# Directories we need to visit before user-multi-obs are up-to-date
-user-dirs := $(patsubst %/,%,$(filter %/, $(user-multi-objs)))
+user-real-objs := $(patsubst %/,,$(user-objs))
+# Directories we need to visit before user-objs are up-to-date
+user-dirs := $(patsubst %/,%,$(filter %/, $(user-objs)))
# replace all dir/ with dir/built-in.o
-user-multi-objs := $(patsubst %/, %/built-in.o, $(user-multi-objs))
+user-objs := $(patsubst %/, %/built-in.o, $(user-objs))
+
+targets += $(static-y) $(shared-y)
# $(output-dirs) are a list of directories that contain object files
output-dirs := $(dir $(user-dirs))
@@ -102,14 +124,14 @@ output-dirs += $(foreach f, $(hostprogs-
output-dirs := $(strip $(sort $(filter-out ./,$(output-dirs))))
# prefix so we get full dir
-user-progs := $(addprefix $(obj)/,$(user-progs))
-user-single := $(addprefix $(obj)/,$(user-single))
-user-multi := $(addprefix $(obj)/,$(user-multi))
-user-multi-objs := $(addprefix $(obj)/,$(user-multi-objs))
+static-y := $(addprefix $(obj)/,$(static-y))
+shared-y := $(addprefix $(obj)/,$(shared-y))
+user-objs := $(addprefix $(obj)/,$(user-objs))
user-real-objs := $(addprefix $(obj)/,$(user-real-objs))
output-dirs := $(addprefix $(obj)/,$(output-dirs))
user-dirs := $(addprefix $(obj)/,$(user-dirs))
subdir-y := $(addprefix $(obj)/,$(subdir-y))
+obj-y := $(addprefix $(obj)/,$(obj-y))
always := $(addprefix $(obj)/,$(always))
targets := $(addprefix $(obj)/,$(targets))
@@ -120,14 +142,12 @@ _useraflags = $(USERAFLAGS) $(EXTRA_U
usercflags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(_usercflags)
useraflags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(_useraflags)
-ifneq ($(KBUILD_SRC),)
# Create output directory if not already present
_dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
# Create directories for object files if directory does not exist
# Needed when obj-y := dir/file.o syntax is used
_dummy := $(foreach d,$(output-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
-endif
# Do we have to make a built-in.o in this dir?
ifneq ($(strip $(obj-y) $(obj-n) $(obj-)),)
@@ -195,45 +215,44 @@ ifdef user-progs
# Compile userspace programs for the target
# ===========================================================================
-__build : $(user-dirs) $(user-progs)
+__build : $(user-dirs) $(static-y) $(shared-y)
# Descend if needed
$(sort $(addsuffix /built-in.o,$(user-dirs))): $(user-dirs) ;
-# link program that has only a single .o file
-quiet_cmd_user-ld-single = USERLD $@
- cmd_user-ld-single = $(USERLD) $(USERLDFLAGS) -o $@ \
- $(USERCRT0) $< \
- $(filter-out FORCE,$^) \
- $(USERLIBC) $(USERLIBGCC) \
- $(USERLIBGCC); \
- $(USERSTRIP) $(USERSTRIPFLAGS) $@
-
-$(user-single): %: %.o $(USERCRT0) $(USERLIBC) FORCE
- $(call if_changed,user-ld-single)
-
-targets += $(user-single) $(user-single:=.o)
-
-# link programs that consist of more than one .o file
-multi-deps = $($(subst $(obj)/,,$@-y))
-link-multi-deps = $(addprefix $(obj)/, \
- $(patsubst %/, %/built-in.o, $(multi-deps)))
-
-quiet_cmd_user-ld-multi = USERLD $@
- cmd_user-ld-multi = $(USERLD) $(USERLDFLAGS) -o $@ \
- $(USERCRT0) \
- $(link-multi-deps) \
- $(USERLIBC) $(USERLIBGCC) \
- $(USERLIBGCC); \
- $(USERSTRIP) $(USERSTRIPFLAGS) $@
-
-$(user-multi): $(user-multi-objs) FORCE
- $(call if_changed,user-ld-multi)
-
-targets += $(user-multi) $(user-real-objs)
+# Define dependencies for link of progs
+# For the simple program:
+# file.o => file
+# A program with multiple objects
+# filea.o, fileb.o => file
+# A program with .o files in another dir
+# dir/built-in.o filea.o => file
+
+stripobj = $(subst $(obj)/,,$@)
+addbuiltin = $(addprefix $(obj)/, $(patsubst %/, %/built-in.o, $(1)))
+link-deps = $(if $($(stripobj)-y), $(call addbuiltin, $($(stripobj)-y)), $@.o)
+
+quiet_cmd_ld-static = USERLD $@
+ cmd_ld-static = $(USERLD) $(USERLDFLAGS) -o $@ \
+ $(USERCRT0) \
+ $(link-deps) \
+ $(USERLIBC) $(USERLIBGCC)
+
+$(static-y): $(user-objs) FORCE
+ $(call if_changed,ld-static)
+
+quiet_cmd_ld-shared = USERLD $@
+ cmd_ld-shared = $(USERLD) $(USERLDFLAGS) -o $@ \
+ -e main $(USERCRTSHARED) \
+ $(link-deps) \
+ $(USERLIBCSHARED) $(USERLIBGCC)
+
+$(shared-y): $(user-objs) FORCE
+ $(call if_changed,ld-shared)
+
+targets += $(user-real-objs)
endif
-
# Compile programs on the host
# ===========================================================================
ifdef hostprogs-y
@@ -277,7 +296,6 @@ quiet_cmd_strip = STRIP $@
# 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))
diff --git a/usr/Kbuild b/usr/Kbuild
--- a/usr/Kbuild
+++ b/usr/Kbuild
@@ -5,6 +5,7 @@
# klibc definitions - to be moved to top-level Makefile later
export KLIBSRC := $(srctree)/$(src)/klibc
export KLIBINC := $(srctree)/$(src)/include
+export KLIBOBJ := $(objtree)/$(obj)/klibc
obj-y := initramfs_data.o
always := klibc/libc.a
@@ -18,7 +19,7 @@ subdir- := klibc
$(obj)/klibc/libc.a: FORCE
$(Q)$(MAKE) -rR -f $(srctree)/scripts/Kbuild.klibc obj=$(obj)/klibc
-initramfs-y += gzip
+initramfs-y += gzip kinit
initramfs-y := $(addprefix $(obj)/,$(initramfs-y))
.PHONY: $(initramfs-y)
More information about the klibc
mailing list