
BUSYBOX       = busybox-1.2.1
STATIC_CYGWIN = t:/src


all : unpack
	$(MAKE) -C $(BUSYBOX) bbmake -f $(PWD)/Makefile
	strip busybox.exe

config : unpack
	$(MAKE) -C $(BUSYBOX) menuconfig
	cp patches/bb.config patches/bb.config.old
	cp $(BUSYBOX)/.config patches/bb.config
	$(MAKE) clean


unpack : $(BUSYBOX) 

.PHONY : all unpack bbmake clean config



#
# Patch the busybox tarball:
#
# 1) get rid of some dll references to cygwin1.dll
# 2) create an empty libcygwin.a, to prevent accidental 
#           linking with the real one
# 3) add some missing cygwin functionality
# 4) install our validated busybox tool configuration
#           (this is just the precooked result of a 
#                make menuconfig in an unpacked busybox
#                directory)
#

$(BUSYBOX) : $(BUSYBOX).tar
	tar xovf $(BUSYBOX).tar
	ar cr $(BUSYBOX)/libcygwin.a
	(cd $(BUSYBOX); unzip ../patches/include)
	sed "s/__declspec(dllimport)//" /usr/include/ctype.h  >         $(BUSYBOX)/include/ctype.h
	sed "s/__declspec(dllimport)//" /usr/include/getopt.h >         $(BUSYBOX)/include/getopt.h
	cat patches/additional.c $(BUSYBOX)/shell/ash.c > tmp; mv tmp   $(BUSYBOX)/shell/ash.c
	cp patches/bb.config                                            $(BUSYBOX)/.config


#
# Build busybox, and link it again using static cygwin
# the link command just is a modified version of the link
# command stolen from a normal VERBOSE=1 busybox build:
# The busybox build fails in the final stage during stripping, 
# because the executable name has an unexpected file extension (.exe).
# We are not interested in this file anyway, so proceed.
#
bbmake :
	- make CC="gcc -I./include -include include/patches.h"   # to let our patched header files be used
	gcc -static -L. \
             `cat $(STATIC_CYGWIN)/wrapsyscalls` \
             -nostartfiles -T$(STATIC_CYGWIN)/applink.sc $(STATIC_CYGWIN)/cygwin1.o \
             -Iinclude -Iinclude -Ilibbb \
             -D_GNU_SOURCE -Wall -Wstrict-prototypes -Wshadow -funsigned-char -fno-builtin-strlen \
             -DNDEBUG -Os -march=i386 -mpreferred-stack-boundary=2 \
             -falign-functions=1 -falign-jumps=1 -falign-loops=1 \
             -fomit-frame-pointer -ffunction-sections -fdata-sections \
             -Wall -Wstrict-prototypes -Wshadow -funsigned-char \
             -fno-builtin-strlen -static -o ../busybox \
             -Wl,--start-group applets/applets.c \
             applets/busybox.c applets/version.c \
             applets/applets.a archival/archival.a \
             archival/libunarchive/libunarchive.a \
             coreutils/coreutils.a debianutils/debianutils.a \
             editors/editors.a findutils/findutils.a \
             miscutils/miscutils.a shell/shell.a \
             libpwdgrp/libpwdgrp.a coreutils/libcoreutils/libcoreutils.a \
             libbb/libbb.a -lm -Wl,--end-group


clean:; rm -rf $(BUSYBOX) busybox.exe
