build system overhaul
diff --git a/e2fsprogs/Config.in b/e2fsprogs/Config.in
index 91e873e..0062b2f 100644
--- a/e2fsprogs/Config.in
+++ b/e2fsprogs/Config.in
@@ -5,13 +5,13 @@
 
 menu "Linux Ext2 FS Progs"
 
-config CONFIG_CHATTR
+config CHATTR
 	bool "chattr"
 	default n
 	help
 	  chattr changes the file attributes on a second extended file system.
 
-config CONFIG_E2FSCK
+config E2FSCK
 	bool "e2fsck"
 	default n
 	help
@@ -20,7 +20,7 @@
 	  The normal compat symlinks 'fsck.ext2' and 'fsck.ext3' are also
 	  provided.
 
-config CONFIG_FSCK
+config FSCK
 	bool "fsck"
 	default n
 	help
@@ -28,38 +28,38 @@
 	  In actuality, fsck is simply a front-end for the various file system
 	  checkers (fsck.fstype) available under Linux.
 
-config CONFIG_LSATTR
+config LSATTR
 	bool "lsattr"
 	default n
 	help
 	  lsattr lists the file attributes on a second extended file system.
 
-config CONFIG_MKE2FS
+config MKE2FS
 	bool "mke2fs"
 	default n
 	help
 	  mke2fs is used to create an ext2/ext3 filesystem.  The normal compat
 	  symlinks 'mkfs.ext2' and 'mkfs.ext3' are also provided.
 
-config CONFIG_TUNE2FS
+config TUNE2FS
 	bool "tune2fs"
 	default n
 	help
 	  tune2fs allows the system administrator to adjust various tunable
 	  filesystem parameters on Linux ext2/ext3 filesystems.
 
-config CONFIG_E2LABEL
+config E2LABEL
 	bool "e2label"
 	default n
-	depends on CONFIG_TUNE2FS
+	depends on TUNE2FS
 	help
 	  e2label will display or change the filesystem label on the ext2
 	  filesystem located on device.
 
-config CONFIG_FINDFS
+config FINDFS
 	bool "findfs"
 	default n
-	depends on CONFIG_TUNE2FS
+	depends on TUNE2FS
 	help
 	  findfs will search the disks in the system looking for a filesystem
 	  which has a label matching label or a UUID equal to uuid.
diff --git a/e2fsprogs/Kbuild b/e2fsprogs/Kbuild
new file mode 100644
index 0000000..f35a1ae
--- /dev/null
+++ b/e2fsprogs/Kbuild
@@ -0,0 +1,27 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
+#
+# Licensed under the GPL v2, see the file LICENSE in this tarball.
+
+lib-y:=
+
+lib-$(CONFIG_CHATTR)     += chattr.o
+libs-$(CONFIG_CHATTR)    += e2p/
+
+lib-$(CONFIG_E2FSCK)     += e2fsck.o util.o
+libs-$(CONFIG_E2FSCK)    += blkid/ ext2fs/ uuid/
+
+lib-$(CONFIG_FSCK)       += fsck.o util.o
+libs-$(CONFIG_FSCK)      += blkid/ ext2fs/ uuid/
+
+lib-$(CONFIG_LSATTR)     += lsattr.o
+libs-$(CONFIG_LSATTR)    += e2p/
+
+lib-$(CONFIG_MKE2FS)     += mke2fs.o util.o
+libs-$(CONFIG_MKE2FS)    += e2p/ blkid/ ext2fs/ uuid/
+
+lib-$(CONFIG_TUNE2FS)    += tune2fs.o util.o
+libs-$(CONFIG_TUNE2FS)   += e2p/ blkid/ ext2fs/ uuid/
+
+CFLAGS += -include e2fsprogs/e2fsbb.h
diff --git a/e2fsprogs/Makefile b/e2fsprogs/Makefile
deleted file mode 100644
index 0efa9bb..0000000
--- a/e2fsprogs/Makefile
+++ /dev/null
@@ -1,22 +0,0 @@
-# Makefile for busybox
-#
-# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
-#
-# Licensed under the GPL v2, see the file LICENSE in this tarball.
-
-ifndef top_srcdir
-top_srcdir=..
-endif
-ifndef top_builddir
-top_builddir=..
-endif
-srcdir=$(top_srcdir)/e2fsprogs
-E2FSPROGS_DIR:=./
-include $(top_builddir)/.config
-include $(top_srcdir)/Rules.mak
-include Makefile.in
-all: $(libraries-y)
--include $(top_builddir)/.depend
-
-clean:
-	rm -f *.o *.a */*.o $(AR_TARGET)
diff --git a/e2fsprogs/Makefile.in b/e2fsprogs/Makefile.in
deleted file mode 100644
index 1602716..0000000
--- a/e2fsprogs/Makefile.in
+++ /dev/null
@@ -1,86 +0,0 @@
-# Makefile for busybox
-#
-# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
-#
-# Licensed under the GPL v2, see the file LICENSE in this tarball.
-
-E2FSPROGS_AR:=e2fsprogs.a
-
-E2FSPROGS_DIR:=$(top_builddir)/e2fsprogs
-E2FSPROGS_SRC:=$(top_srcdir)/e2fsprogs
-
-E2FSPROGS_CFLAGS := -include $(E2FSPROGS_SRC)/e2fsbb.h
-
-BLKID_SRC   := cache.c dev.c devname.c devno.c blkid_getsize.c \
-               probe.c read.c resolve.c save.c tag.c list.c
-BLKID_SRCS  := $(patsubst %,blkid/%, $(BLKID_SRC))
-BLKID_OBJS  := $(patsubst %.c,%.o, $(BLKID_SRCS))
-
-E2P_SRC     := fgetsetflags.c fgetsetversion.c pf.c iod.c mntopts.c \
-               feature.c ls.c uuid.c pe.c ostype.c ps.c hashstr.c \
-               parse_num.c
-E2P_SRCS    := $(patsubst %,e2p/%, $(E2P_SRC))
-E2P_OBJS    := $(patsubst %.c,%.o, $(E2P_SRCS))
-
-EXT2FS_SRC  := gen_bitmap.c bitops.c ismounted.c mkjournal.c unix_io.c \
-               rw_bitmaps.c initialize.c bitmaps.c block.c \
-               ind_block.c inode.c freefs.c alloc_stats.c closefs.c \
-               openfs.c io_manager.c finddev.c read_bb.c alloc.c badblocks.c \
-               getsize.c getsectsize.c alloc_tables.c read_bb_file.c mkdir.c \
-               bb_inode.c newdir.c alloc_sb.c lookup.c dirblock.c expanddir.c \
-               dir_iterate.c link.c res_gdt.c icount.c get_pathname.c dblist.c \
-               dirhash.c version.c flushb.c unlink.c check_desc.c valid_blk.c \
-               ext_attr.c bmap.c dblist_dir.c ext2fs_inline.c swapfs.c
-EXT2FS_SRCS := $(patsubst %,ext2fs/%, $(EXT2FS_SRC))
-EXT2FS_OBJS := $(patsubst %.c,%.o, $(EXT2FS_SRCS))
-
-UUID_SRC    := compare.c gen_uuid.c pack.c parse.c unpack.c unparse.c \
-               uuid_time.c
-UUID_SRCS   := $(patsubst %,uuid/%, $(UUID_SRC))
-UUID_OBJS   := $(patsubst %.c,%.o, $(UUID_SRCS))
-
-E2FSPROGS-y:=
-E2FSPROGS-$(CONFIG_CHATTR)     += chattr.o $(E2P_OBJS)
-E2FSPROGS-$(CONFIG_E2FSCK)     += e2fsck.o util.o $(BLKID_OBJS) $(EXT2FS_OBJS) $(UUID_OBJS)
-E2FSPROGS-$(CONFIG_FSCK)       += fsck.o util.o $(BLKID_OBJS) $(EXT2FS_OBJS) $(UUID_OBJS)
-E2FSPROGS-$(CONFIG_LSATTR)     += lsattr.o $(E2P_OBJS)
-E2FSPROGS-$(CONFIG_MKE2FS)     += mke2fs.o util.o $(E2P_OBJS) $(BLKID_OBJS) $(EXT2FS_OBJS) $(UUID_OBJS)
-E2FSPROGS-$(CONFIG_TUNE2FS)    += tune2fs.o util.o $(E2P_OBJS) $(BLKID_OBJS) $(EXT2FS_OBJS) $(UUID_OBJS)
-
-E2FSPROGS-y:=$(sort $(E2FSPROGS-y))
-
-ifneq ($(strip $(E2FSPROGS-y)),)
-libraries-y+=$(E2FSPROGS_DIR)/$(E2FSPROGS_AR)
-endif
-
-E2FSPROGS_SRC-y:=$(patsubst %.o,$(E2FSPROGS_SRC)/%.c,$(E2FSPROGS-y))
-E2FSPROGS_SRC-a:=$(wildcard $(E2FSPROGS_SRC)/*.c) $(patsubst %,$(E2FSPROGS_SRC)/%,$(BLKID_SRCS) $(E2P_SRCS) $(EXT2FS_SRCS) $(UUID_SRCS))
-APPLET_SRC-y+=$(E2FSPROGS_SRC-y)
-APPLET_SRC-a+=$(E2FSPROGS_SRC-a)
-
-# XXX: FIXME: change .c to include their stuff relative to $(E2FSPROGS_SRC)
-E2FSPROGS_TMP_KLUDGE:=$(patsubst %,-I$(E2FSPROGS_SRC)/%,blkid e2fsck e2p ext2fs uuid)
-
-APPLETS_DEFINE-y+=$(E2FSPROGS_CFLAGS) -I$(E2FSPROGS_SRC) $(E2FSPROGS_TMP_KLUDGE)
-APPLETS_DEFINE-a+=$(E2FSPROGS_CFLAGS) -I$(E2FSPROGS_SRC) $(E2FSPROGS_TMP_KLUDGE)
-
-$(E2FSPROGS_DIR)/$(E2FSPROGS_AR): $(patsubst %,$(E2FSPROGS_DIR)/%, $(E2FSPROGS-y))
-	$(do_ar)
-
-$(E2FSPROGS_DIR)/%.o: $(subst $(top_builddir),$(top_srcdir),$(E2FSPROGS_DIR)/%.c)
-	$(compile.c) $(E2FSPROGS_CFLAGS)
-
-# for building out-of-tree we need to make sure that the directories to hold
-# the object tree are created
-$(patsubst %,$(E2FSPROGS_DIR)/%, blkid e2fsck e2p ext2fs uuid):
-	@mkdir -p "$@"
-
-# make sure that the directories are order-only prerequisites. Otherwise we
-# may have object files created after the timestamp of the directory was
-# updated which would lead to spurious rebuilds (as some of the dentries
-# may be older than the dir itself).
-$(patsubst %,$(E2FSPROGS_DIR)/%, $(BLKID_OBJS)):|$(E2FSPROGS_DIR)/blkid
-$(patsubst %,$(E2FSPROGS_DIR)/%, $(E2FSCK_OBJS)):|$(E2FSPROGS_DIR)/e2fsck
-$(patsubst %,$(E2FSPROGS_DIR)/%, $(E2P_OBJS)):|$(E2FSPROGS_DIR)/e2p
-$(patsubst %,$(E2FSPROGS_DIR)/%, $(EXT2FS_OBJS)):|$(E2FSPROGS_DIR)/ext2fs
-$(patsubst %,$(E2FSPROGS_DIR)/%, $(UUID_OBJS)):|$(E2FSPROGS_DIR)/uuid
diff --git a/e2fsprogs/blkid/Kbuild b/e2fsprogs/blkid/Kbuild
new file mode 100644
index 0000000..ba0bee0
--- /dev/null
+++ b/e2fsprogs/blkid/Kbuild
@@ -0,0 +1,18 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
+#
+# Licensed under the GPL v2, see the file LICENSE in this tarball.
+
+lib-y:=
+lib-y	+= cache.o dev.o devname.o devno.o blkid_getsize.o \
+	   probe.o read.o resolve.o save.o tag.o list.o
+
+CFLAGS_dev.o := -include include/busybox.h
+CFLAGS_devname.o := -include include/busybox.h
+CFLAGS_devno.o := -include include/busybox.h
+CFLAGS_blkid_getsize.o := -include include/busybox.h
+CFLAGS_probe.o := -include include/busybox.h
+CFLAGS_save.o := -include include/busybox.h
+CFLAGS_tag.o := -include include/busybox.h
+CFLAGS_list.o := -include include/busybox.h
diff --git a/e2fsprogs/blkid/blkid_getsize.c b/e2fsprogs/blkid/blkid_getsize.c
index 7a2133f..3c5ec5b 100644
--- a/e2fsprogs/blkid/blkid_getsize.c
+++ b/e2fsprogs/blkid/blkid_getsize.c
@@ -14,9 +14,7 @@
 #include "blkidP.h"
 
 #include <stdio.h>
-#if HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 #if HAVE_ERRNO_H
 #include <errno.h>
 #endif
diff --git a/e2fsprogs/blkid/devname.c b/e2fsprogs/blkid/devname.c
index b5c1bc9..7606e40 100644
--- a/e2fsprogs/blkid/devname.c
+++ b/e2fsprogs/blkid/devname.c
@@ -23,9 +23,7 @@
 #if HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
-#if HAVE_SYS_STAT_H
 #include <sys/stat.h>
-#endif
 #if HAVE_ERRNO_H
 #include <errno.h>
 #endif
diff --git a/e2fsprogs/blkid/devno.c b/e2fsprogs/blkid/devno.c
index 065761f..be93666 100644
--- a/e2fsprogs/blkid/devno.c
+++ b/e2fsprogs/blkid/devno.c
@@ -21,9 +21,7 @@
 #if HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
-#if HAVE_SYS_STAT_H
 #include <sys/stat.h>
-#endif
 #include <dirent.h>
 #if HAVE_ERRNO_H
 #include <errno.h>
diff --git a/e2fsprogs/e2p/Kbuild b/e2fsprogs/e2p/Kbuild
new file mode 100644
index 0000000..24a692e
--- /dev/null
+++ b/e2fsprogs/e2p/Kbuild
@@ -0,0 +1,10 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
+#
+# Licensed under the GPL v2, see the file LICENSE in this tarball.
+
+lib-y:=
+lib-y	+= fgetsetflags.o fgetsetversion.o pf.o iod.o mntopts.o \
+           feature.o ls.o uuid.o pe.o ostype.o ps.o hashstr.o \
+           parse_num.o
diff --git a/e2fsprogs/e2p/fgetsetflags.c b/e2fsprogs/e2p/fgetsetflags.c
index 86bf65d..34e4055 100644
--- a/e2fsprogs/e2p/fgetsetflags.c
+++ b/e2fsprogs/e2p/fgetsetflags.c
@@ -63,8 +63,8 @@
 	if (save_errno)
 		errno = save_errno;
 	return r;
-#endif /* HAVE_EXT2_IOCTLS */
 notsupp:
+#endif /* HAVE_EXT2_IOCTLS */
 	errno = EOPNOTSUPP;
 	return -1;
 }
diff --git a/e2fsprogs/ext2fs/Kbuild b/e2fsprogs/ext2fs/Kbuild
new file mode 100644
index 0000000..dc49934
--- /dev/null
+++ b/e2fsprogs/ext2fs/Kbuild
@@ -0,0 +1,18 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
+#
+# Licensed under the GPL v2, see the file LICENSE in this tarball.
+
+lib-y:=
+lib-y	+= gen_bitmap.o bitops.o ismounted.o mkjournal.o unix_io.o \
+	   rw_bitmaps.o initialize.o bitmaps.o block.o \
+	   ind_block.o inode.o freefs.o alloc_stats.o closefs.o \
+	   openfs.o io_manager.o finddev.o read_bb.o alloc.o badblocks.o \
+	   getsize.o getsectsize.o alloc_tables.o read_bb_file.o mkdir.o \
+	   bb_inode.o newdir.o alloc_sb.o lookup.o dirblock.o expanddir.o \
+	   dir_iterate.o link.o res_gdt.o icount.o get_pathname.o dblist.o \
+	   dirhash.o version.o flushb.o unlink.o check_desc.o valid_blk.o \
+	   ext_attr.o bmap.o dblist_dir.o ext2fs_inline.o swapfs.o
+
+CFLAGS += -include e2fsprogs/e2fsbb.h
diff --git a/e2fsprogs/tune2fs.c b/e2fsprogs/tune2fs.c
index 9bd6f4c..01ba31c 100644
--- a/e2fsprogs/tune2fs.c
+++ b/e2fsprogs/tune2fs.c
@@ -45,8 +45,6 @@
 #include "blkid/blkid.h"
 
 #include "busybox.h"
-#include "grp_.h"
-#include "pwd_.h"
 
 static char * device_name = NULL;
 static char * new_label, *new_last_mounted, *new_UUID;
diff --git a/e2fsprogs/uuid/Kbuild b/e2fsprogs/uuid/Kbuild
new file mode 100644
index 0000000..0ae74ae
--- /dev/null
+++ b/e2fsprogs/uuid/Kbuild
@@ -0,0 +1,9 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
+#
+# Licensed under the GPL v2, see the file LICENSE in this tarball.
+
+lib-y:=
+lib-y	+= compare.o gen_uuid.o pack.o parse.o unpack.o unparse.o \
+	   uuid_time.o
diff --git a/e2fsprogs/uuid/gen_uuid.c b/e2fsprogs/uuid/gen_uuid.c
index e5c0f0d..9d700a0 100644
--- a/e2fsprogs/uuid/gen_uuid.c
+++ b/e2fsprogs/uuid/gen_uuid.c
@@ -41,6 +41,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/file.h>
+#include <sys/time.h>
 #ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
 #endif