ifeq (yes, @prebuild_glibc@)  # prebuild glibc

ARM_TOOLCHAIN_LIB_PATH = $(dir $(shell unset CROSS_C_ROOT_PATH; arm-none-linux-gnueabi-gcc -print-file-name=libc.so.6))/..
TARGET_DIRS = lib usr/lib usr/libexec usr/include

SRC_DIRS = \
	$(foreach dir, $(TARGET_DIRS), \
		$(shell find $(ARM_TOOLCHAIN_LIB_PATH)/$(dir)/ -type d | grep -v usr/include/asm | grep -v usr/include/linux | grep -v usr/lib/locale ) \
	)

SRC_FILES = \
	$(foreach dir, $(TARGET_DIRS), \
		$(shell find $(ARM_TOOLCHAIN_LIB_PATH)/$(dir)/ -xtype f | grep -v usr/include/asm | grep -v usr/include/linux | grep -v usr/lib/locale ) \
	)

all:

install:
	$(foreach dir, $(SRC_DIRS), \
		$(shell install -d -m 0755 \
			$(subst $(ARM_TOOLCHAIN_LIB_PATH), \
				$(CROSS_C_ROOT_PATH), \
				$(dir) \
			) \
		) \
	)
	$(foreach file, $(SRC_FILES), \
		$(shell \
			if [ -L $(file) ]; then \
				cp -af $(file) \
					$(subst $(ARM_TOOLCHAIN_LIB_PATH), \
						$(CROSS_C_ROOT_PATH), \
						$(file) \
					); \
			else \
				install -m 0755 $(file) \
					$(subst $(ARM_TOOLCHAIN_LIB_PATH), \
						$(CROSS_C_ROOT_PATH), \
						$(file) \
					); \
			fi; \
		) \
	)
	@ echo "install glibc."

clean:

.PHONY:	all install clean

else  # bulid glibc from source

SUBDIRS = glibc
INST_SUBDIRS = $(patsubst %,install-%,$(SUBDIRS))
CLEAN_SUBDIRS = $(patsubst %,clean-%,$(SUBDIRS))

all: $(SUBDIRS)

$(SUBDIRS):
	if test ! -f $@/Makefile; then					\
		(cd $@; ./configure --host=arm-none-linux-gnueabi	\
		 --build=`./config.guess`				\
		)							\
	fi;								\
	$(MAKE) -C $@ all

install: $(INST_SUBDIRS)

$(INST_SUBDIRS):
	dir=`echo $@ |sed -e 's/install-//'`;				\
	if test ! -f $$dir/Makefile; then				\
		(cd $${dir}; ./configure --host=arm-none-linux-gnueabi	\
		 --build=`./config.guess`				\
		)							\
	fi;								\
	$(MAKE) -C $${dir} install

clean: $(CLEAN_SUBDIRS)

$(CLEAN_SUBDIRS):
	dir=`echo $@ |sed -e 's/clean-//'`;				\
	if test ! -f $$dir/Makefile; then				\
		(cd $${dir}; ./configure --host=arm-none-linux-gnueabi	\
		 --build=`./config.guess`				\
		)							\
	fi;								\
	$(MAKE) -C $${dir} clean;					\
#	rm $$dir/Makefile


.PHONY:	all install $(SUBDIRS) $(INST_SUBDIRS) $(CLEAN_SUBDIRS)

endif

