fetchtoolchain:
	cd downloads; wget https://ftp.gnu.org/gnu/binutils/binutils-2.35.tar.bz2
	cd downloads; wget https://ftp.gnu.org/gnu/gcc/gcc-10.2.0/gcc-10.2.0.tar.xz
	cd downloads; wget https://musl.libc.org/releases/musl-1.2.1.tar.gz
	cd src; git clone https://github.com/openrisc/linux.git
	#cd src; git clone git://github.com/openrisc/binutils-gdb.git
	#cd src/binutils-gdb;git checkout or1k-2.30
	#cd src; git clone git://github.com/openrisc/or1k-gcc.git
	#cd src/or1k-gcc; git checkout or1k-7.2.0
	##cd src; git clone git://git.openrisc.net/stefan/linux
	##cd src; mv or1k-linux linux
	##cd src/linux; git checkout smp
	#cd src; git clone git://git.musl-libc.org/musl
	#cd src/musl; git checkout tags/v1.1.19
	#cd src; git clone git://github.com/openrisc/or1ksim.git

precheck:
	#toolchain_stage4
	#mkdir -p src/linux/arch/openrisc/support/initramfs/root
	#mkdir -p src/linux/arch/openrisc/support/initramfs/tmp
	#mkdir -p src/linux/arch/openrisc/support/initramfs/lib
	#mkdir -p src/linux/arch/openrisc/support/initramfs/usr/lib
	#cp -Pr $(SYSROOT)/lib/* src/linux/arch/openrisc/support/initramfs/lib/
	#cp -Pr $(SYSROOT)/usr/lib* src/linux/arch/openrisc/support/initramfs/usr/lib/
	#cp -Pr $(SYSROOT)/usr/lib/* src/linux/arch/openrisc/support/initramfs/usr/lib/
	#cd src/linux; make

uclibc_toolchain:
	$(MAKE) toolchain_binutils
	$(MAKE) toolchain_linux
	$(MAKE) toolchain_gcc1
	$(MAKE) toolchain_uclibc
	$(MAKE) toolchain_gcc2

musl_toolchain:
	mkdir -p $(SYSROOT)
	mkdir -p $(TOOLCHAIN_DIR)/$(TOOLCHAIN_TARGET)
	-ln -s . $(TOOLCHAIN_DIR)/$(TOOLCHAIN_TARGET)/usr
	$(MAKE) toolchain_binutils2
	$(MAKE) toolchain_gcc1_upstream
	$(MAKE) toolchain_linux
	$(MAKE) toolchain_musl
	$(MAKE) toolchain_gcc2

glibc_toolchain:
	mkdir -p $(SYSROOT)
	mkdir -p $(TOOLCHAIN_DIR)/$(TOOLCHAIN_TARGET)
	$(MAKE) toolchain_binutils2
	$(MAKE) toolchain_gcc1
	$(MAKE) toolchain_linux
	$(MAKE) toolchain_glibc
	$(MAKE) toolchain_gcc3

binutils_VERSION=-2.35
toolchain_binutils2:
	$(call extractpatch,binutils,$(binutils_VERSION))
	#cd src/binutils; patch -Np1 -i ../../patches/binutils-patch.diff
	#cd src/binutils; sed -i -e 's,MAKEINFO="$MISSING makeinfo",MAKEINFO=true,g' configure
	mkdir -p src/binutils-build
	rm -rf src/binutils-build/*
	cd src/binutils-build;../../src/binutils/configure \
	--disable-werror \
        --target=$(TOOLCHAIN_TARGET) \
        --prefix=$(TOOLCHAIN_DIR) \
	--with-sysroot
	cd src/binutils-build; make MAKEINFO=true
	cd src/binutils-build; make install MAKEINFO=true

toolchain_binutils:
	mkdir -p src/binutils-gdb-build
	rm -rf src/binutils-gdb-build/*
	cd src/binutils-gdb-build;../../src/binutils-gdb/configure \
        --target=$(TOOLCHAIN_TARGET) \
        --prefix=$(TOOLCHAIN_DIR) \
        --disable-itcl \
        --disable-tk \
        --disable-tcl \
        --disable-winsup \
        --disable-libgui \
        --disable-rda \
        --disable-sid \
        --disable-sim \
        --disable-gdb \
        --with-sysroot \
        --disable-newlib \
        --disable-libgloss \
        --disable-werror \
        --without-docdir
        #--disable-shared
	#cd src/or1k-src-build; echo "MAKEINFO = :" >> Makefile
	cd src/binutils-gdb-build; make MAKEINFO=true
	#cd src/binutils-gdb-build; make install MAKEINFO=true

toolchain_linux:
	cd src/linux/; make ARCH=openrisc INSTALL_HDR_PATH=$(SYSROOT)/usr headers_install
	cd src/linux/; make ARCH=openrisc INSTALL_HDR_PATH=$(TOOLCHAIN_DIR)/$(TOOLCHAIN_TARGET) headers_install


binutils_VERSION=-10.2.0
toolchain_gcc1_upstream:
	$(call extractpatch,gcc,$(binutils_VERSION))
	mkdir -p src/gcc-build
	rm -rf src/gcc-build/*
	cd src/gcc-build; ../../src/gcc/configure \
	--target=$(TOOLCHAIN_TARGET) \
	--prefix=$(TOOLCHAIN_DIR) \
	--disable-libssp \
	--disable-nls \
	--enable-languages=c \
	--with-newlib \
        --without-headers \
	--disable-libgomp \
	--disable-libmudflap \
	--disable-shared \
	--disable-threads \
	--disable-decimal-float \
	--disable-libquadmath \
	--disable-libatomic \
        --disable-libvtv \
        --disable-libstdcxx \
	--disable-multilib
	#cd src/gcc-build; echo "MAKEINFO = :" >> Makefile
	cd src/gcc-build; make
	cd src/gcc-build; make install

toolchain_gcc1:
	cd src/or1k-gcc; git clean -d -f
	cd src/or1k-gcc; git reset --hard
	mkdir -p src/gcc-build
	rm -rf src/gcc-build/*
	cd src/gcc-build; CFLAGS="-O0 -g0" ../../src/or1k-gcc/configure \
	--target=$(TOOLCHAIN_TARGET) \
	--prefix=$(TOOLCHAIN_DIR) \
	--disable-libssp \
	--disable-nls \
	--enable-languages=c \
	--with-newlib \
	--disable-libgomp \
	--disable-libmudflap \
	--disable-shared \
	--disable-threads \
	--disable-decimal-float \
	--disable-libquadmath \
	--disable-libatomic \
	--disable-multilib
	#cd src/gcc-build; echo "MAKEINFO = :" >> Makefile
	cd src/gcc-build; make
	cd src/gcc-build; make install


musl_VERSION=-1.2.1
toolchain_musl:
	$(call extractpatch,musl,$(musl_VERSION))
	#cd src/musl; make distclean
	#cd src/musl; git clean -d -f
	#cd src/musl; git reset --hard
	cd src/musl; ./configure \
	--enable-debug \
	--enable-optimize \
	CC=$(TOOLCHAIN_TARGET)-gcc \
 	--prefix=$(TOOLCHAIN_DIR)/$(TOOLCHAIN_TARGET)
	cd src/musl; make
	cd src/musl; make install
	#cp patches/stddef.h $(SYSROOT)/usr/include/

toolchain_uclibc:
	mkdir -p $(SYSROOT)/usr/bin
	cd src/uClibc-or1k; make clean
	cd src/uClibc-or1k; make distclean
	cd src/uClibc-or1k; make ARCH=or1k defconfig
	cp patches/CONFIG_UCLIBC src/uClibc-or1k/.config
	#sed -i~ -e "s;.*CROSS_COMPILER_PREFIX.*;CROSS_COMPILER_PREFIX=\"$(TOOLCHAIN_TARGET)-\";" src/uClibc-or1k/.config
	cd src/uClibc-or1k; make PREFIX=$(SYSROOT)
	cd src/uClibc-or1k; make PREFIX=$(SYSROOT) utils
	cd src/uClibc-or1k; make PREFIX=$(SYSROOT) install
	#ln -f -s ld-uClibc.so.0 $(SYSROOT)/lib/ld.so.1
	cp src/uClibc-or1k/utils/ldd $(SYSROOT)/usr/bin/

toolchain_glibc:
	#cd src/or1k-glibc; git reset --hard
	#cd src/or1k-glibc; git clean -d -f
	mkdir -p src/glibc-build
	rm -rf src/glibc-build/*
	cd src/glibc-build; 			\
	../or1k-glibc/configure $(CONFIG_HOST) 	\
	--with-headers=${SYSROOT}/usr/include --prefix=/usr	\
	--enable-kernel=3.0.0
	cd src/glibc-build; make \
	-C ${CURDIR}/src/or1k-glibc/locale \
	-r objdir="${CURDIR}/src/glibc-build" C-translit.h
	cd src/glibc-build; make
	cd src/glibc-build; make install DESTDIR=$(SYSROOT)


toolchain_gcc2:
	rm -rf src/gcc-build/*
	cd src/gcc-build; ../../src/gcc/configure \
	--target=$(TOOLCHAIN_TARGET) \
	--prefix=$(TOOLCHAIN_DIR) \
	--enable-languages=c,c++,fortran \
	--disable-libmudflap \
	--disable-libsanitizer \
	--disable-multilib	\
	--disable-nls \
	--enable-shared=libstdc++,libgomp,libatomic,libsupc++ \
	--with-sysroot=$(SYSROOT)
	cd src/gcc-build; make
	cd src/gcc-build; make install
	#rm -rf $(TOOLCHAIN_DIR)/lib/gcc/or1k-linux-musl/*/include-fixed/
	#rm -rf $(TOOLCHAIN_DIR)/lib/gcc/or1k-linux-musl/*/include/stddef.h
	##cp -P $(TOOLCHAIN_DIR)/$(TOOLCHAIN_TARGET)/lib/libstdc* $(SYSROOT)/usr/lib/
	##cp -P $(TOOLCHAIN_DIR)/$(TOOLCHAIN_TARGET)/lib/libgcc* $(SYSROOT)/usr/lib/

#gcc2 vor glibc
toolchain_gcc3:
	rm -rf src/gcc-build/*
	cd src/gcc-build; ../../src/or1k-gcc/configure \
	--target=$(TOOLCHAIN_TARGET) \
	--prefix=$(TOOLCHAIN_DIR) \
	--enable-languages=c,c++ \
	--enable-threads=posix \
	--disable-libmudflap \
	--disable-libgomp \
	--disable-libsanitizer \
	--disable-multilib	\
	--disable-nls \
	--enable-vtable-verify	\
	--with-sysroot=$(SYSROOT)
	cd src/gcc-build; make
	cd src/gcc-build; make install
	##cp -P $(TOOLCHAIN_DIR)/$(TOOLCHAIN_TARGET)/lib/libstdc* $(SYSROOT)/usr/lib/
	##cp -P $(TOOLCHAIN_DIR)/$(TOOLCHAIN_TARGET)/lib/libgcc* $(SYSROOT)/usr/lib/

