# Makefile for building a fake gcc/binutils toolchain
# that simply spoofs the location of the C library
#
# Copyright (C) 2000-2002 Erik Andersen <andersen@uclibc.org>
#

TOPDIR = ../../
include $(TOPDIR)Rules.mak

UCLIBC_DIR = $(shell (cd ../.. ; /bin/pwd))
GCC_BIN = $(shell which $(CC))
LD_BIN = $(shell which $(LD))
GCCINCDIR:= ${shell $(CC) -print-search-dirs | sed -ne "s/install: \(.*\)/\1include/gp"}

all: gcc-uClibc ld-uClibc

gcc-uClibc.h: Makefile $(TOPDIR)/.config
	@echo "/* this file was autogenerated by make */" > gcc-uClibc.h
	@echo "#define UCLIBC_TARGET_PREFIX " \"$(TARGET_PREFIX)\" >> gcc-uClibc.h
	@echo "#define UCLIBC_DEVEL_PREFIX " \"$(DEVEL_PREFIX)\" >> gcc-uClibc.h
	@echo "#define UCLIBC_BUILD_DIR " \"$(UCLIBC_DIR)\" >> gcc-uClibc.h
	@echo "#define GCC_BIN " \"$(GCC_BIN)\" >> gcc-uClibc.h
	@echo "#define LIBGCC_DIR " \"$(LIBGCC_DIR)\" >> gcc-uClibc.h
	@echo "#define TARGET_ARCH " \"$(TARGET_ARCH)\" >> gcc-uClibc.h
	@echo "#define DYNAMIC_LINKER " \"$(DYNAMIC_LINKER)\" >> gcc-uClibc.h
	@echo "#define BUILD_DYNAMIC_LINKER " \"$(UCLIBC_DIR)/lib/$(UCLIBC_LDSO)\" >> gcc-uClibc.h
ifeq ($(strip $(HAVE_SHARED)),y)
	@echo "#define __UCLIBC_HAS_SHARED__ 1" >> gcc-uClibc.h
else
	@echo "#undef __UCLIBC_HAS_SHARED__" >> gcc-uClibc.h
endif
ifeq ($(strip $(UCLIBC_HAS_MMU)),y)
	@echo "#define __UCLIBC_HAS_MMU__ 1" >> gcc-uClibc.h
else
	@echo "#undef __UCLIBC_HAS_MMU__" >> gcc-uClibc.h
endif
ifeq ($(strip $(HAS_ELF)),y)
	@echo "#define __HAS_ELF__ 1" >> gcc-uClibc.h
else
	@echo "#undef __HAS_ELF__" >> gcc-uClibc.h
endif
ifeq ($(strip $(UCLIBC_CTOR_DTOR)),y)
	@echo "#define __UCLIBC_CTOR_DTOR__ 1" >> gcc-uClibc.h
ifeq ($(strip $(UCLIBC_PROFILING)),y)
	@echo "#define __UCLIBC_PROFILING__ 1" >> gcc-uClibc.h
else
	@echo "#undef __UCLIBC_PROFILING__" >> gcc-uClibc.h
endif
else
	@echo "#undef __UCLIBC_CTOR_DTOR__" >> gcc-uClibc.h
endif

gcc-uClibc: gcc-uClibc.h gcc-uClibc.c
	$(HOSTCC) $(HOSTCFLAGS) -Wl,-s gcc-uClibc.c -o $(TARGET_ARCH)-uclibc-gcc

ld-uClibc:
	@echo "#!/bin/sh" > $(TARGET_ARCH)-uclibc-ld
	@echo "# This file was autogenerated by make" >> $(TARGET_ARCH)-uclibc-ld
	@echo "$(LD_BIN) \$$@ -L$(DEVEL_PREFIX)/usr/lib -L$(DEVEL_PREFIX)/lib "\
		"-L$(UCLIBC_DIR)" >> $(TARGET_ARCH)-uclibc-ld
	chmod a+x $(TARGET_ARCH)-uclibc-ld

install: all
	install -d $(PREFIX)$(DEVEL_TOOL_PREFIX)/bin;
	install -d $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin;
	install -m 755 $(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin/
	install -m 755 $(TARGET_ARCH)-uclibc-ld $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin/
	ln -fs $(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-cc
	ln -fs $(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(DEVEL_TOOL_PREFIX)/bin/gcc
	ln -fs $(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(DEVEL_TOOL_PREFIX)/bin/cc
	ln -fs $(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-ld  $(PREFIX)$(DEVEL_TOOL_PREFIX)/bin/ld
ifeq ($(strip $(UCLIBC_CTOR_DTOR)),y)
	ln -fs $(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-g++
	ln -fs $(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-c++
	ln -fs $(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(DEVEL_TOOL_PREFIX)/bin/c++
	ln -fs $(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(DEVEL_TOOL_PREFIX)/bin/g++
endif
	for app in addr2line ar as cpp gasp nm objcopy \
	    objdump ranlib size strings strip; do \
	  APPNAME=`which $(CROSS)$${app}`; \
	  if [ -x "$$APPNAME" ] ; then \
	  ln -fs "$$APPNAME" $(PREFIX)$(DEVEL_TOOL_PREFIX)/bin/$${app}; \
	  ln -fs "$$APPNAME" $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-$${app}; \
	  fi; \
	done

clean:
	rm -f gcc-uClibc.h *-uclibc-gcc *-uclibc-ld core