SHELL=bash -o pipefail
TOP:=$(abspath ..)
-include $(TOP)/common.mk

LINUXDIR:=$(LINUX_SRC)
BUILDDIR:=$(LINUX_BUILDDIR)
LEONLINK:=$(LINUX_LEONLINK)
TOOLCHAIN:=sparc-linux-
FTP_KERNEL:=$(FTP_BASE)/linux/linux-2.6/kernel
BASENAME:=leon-linux-latest-stable

# mirrors file can override e.g. FTP_BASE or FTP_KERNEL
MIRRORS=$(TOP)/.mirrors
-include $(MIRRORS)

# Modeled after buildroots BR2_JLEVEL.
# If LB_LINUX_JLEVEL is 0, scale the maximum concurrency with the number of
# CPUs.  A coefficient of 2 is used in order to keep processors busy
# while waiting on I/O.
ifeq ($(LB_LINUX_JLEVEL),0)
PARALLEL_JOBS:=$(shell echo \
	$$((2 * `getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1`)))
else
PARALLEL_JOBS:=$(LB_LINUX_JLEVEL)
endif

.PHONY: download unpack installed_check install install_leontar \
	install_from_leondir preconfig postconfig build clean

default: build

# Find Latest Linux dist from Gaisler FTP server
download:
	@$(SCRIPTS)/download.sh $(FTP_KERNEL) $(PWD)/reference $(BASENAME)

installed_check:
	if [ -d $(LINUXDIR) ] ; then \
		echo "Linux already installed - see manual on how to update"; \
		exit 1; \
	  fi

# Many old leon-linux archives does not properly support LINUX_INSTALL_METHOD=-tar
# so only try to use that if the Makefile has a LINUX_TAR_SUPPORT variable
# which tell us that it can handle trying to use -tar
install_from_leondir:
	if [ -h $(LEONLINK) ]; then \
		rm $(LEONLINK); \
	  fi
	ln -s $(LEONDIR) $(LEONLINK)
	if [ -x $(LEONLINK)/pkg/install.sh ]; then \
		$(LEONLINK)/pkg/install.sh; \
	  fi
	cd $(LEONLINK) && \
	  method=$(if $(LB_TRY_USE_TAR),-tar,-git); \
	  if ! grep -q "^LINUX_TAR_SUPPORT=" Makefile; then \
		method=-git; \
	  fi; \
	  make LINUX_TREE=../$(LINUXDIR) MIRRORS=`readlink -m $(MIRRORS)` \
		LINUX_INSTALL_METHOD=$$method install

# Install latest Linux distribution in reference directory, make sure
# to do download target first...
install_latest_stable: installed_check
	$(SCRIPTS)/unpack.sh $(PWD)/reference $(PWD) $(BASENAME)
	@echo INSTALLING...
	filename=`sed -n -e 's/^FILENAME=//gp' $(PWD)/reference/LATEST.$(BASENAME)`; \
	  leonlinuxdir=`tar -tf reference/$$filename | sed -n 1p`; \
	  make LEONDIR=$$leonlinuxdir install_from_leondir
	mv reference/LATEST.$(BASENAME) reference/CURRENT.$(BASENAME)

# Install specific Linux distribution in residing in reference directory
install_leontar: installed_check
	$(if $(strip $(ARCH_NAME)),,echo "No archive name specified"; exit 1;)
	dirname=`tar -tf reference/$(ARCH_NAME)  | sed -n 1p` && \
	  tar -C $(PWD) -xvjf reference/$(ARCH_NAME) && \
	  make LEONDIR=$$dirname install_from_leondir

# Download and install latest Linux distribution
install: download install_latest_stable

# Create build directory and initialize a default LEON config if no
# configuration is present
mkbuilddir:
	-@if [ ! -d $(BUILDDIR) ]; then \
	 mkdir -p $(BUILDDIR); \
	 fi;
	-@if [ ! -f $(BUILDDIR)/.config ]; then \
	 cp $(LEONLINK)/config/defconfig $(BUILDDIR)/.config ; \
	 fi;

# Generate menu/xconfig configuration file
preconfig:

# Fixup before buildroot/linux etc. configuration
pre_postconfig: mkbuilddir
# Update Linux Configuration with PATH to CPIO Image if not updated before
ifeq ($(LB_LINUX_SET_CPIO),y)
	sed 's/^CONFIG_INITRAMFS_SOURCE=.*/CONFIG_INITRAMFS_SOURCE=\"..\/..\/dist\/.rootfs.cpio\"/g' \
	 $(BUILDDIR)/.config > $(BUILDDIR)/.config.tmp
	if diff -q $(BUILDDIR)/.config $(BUILDDIR)/.config.tmp > /dev/null ; then \
	 rm -f $(BUILDDIR)/.config.tmp ; \
	 else \
	 mv $(BUILDDIR)/.config.tmp $(BUILDDIR)/.config ; \
	 fi;
endif

# Fixup after configuration
postconfig:

_build:
	@if [ ! -d $(BUILDDIR) -o ! -f $(BUILDDIR)/.config ] ; then \
	 echo ; \
	 echo "!!! Build directory missing or Linux configuration not created." ; \
	 echo ; \
	 exit 1 ; \
	 fi;
# Update Linux Configuration with PATH to CPIO Image if not updated before
ifeq ($(LB_LINUX_SET_CPIO),y)
	sed 's/^CONFIG_INITRAMFS_SOURCE=.*/CONFIG_INITRAMFS_SOURCE=\"..\/..\/dist\/.rootfs.cpio\"/g' \
	 $(BUILDDIR)/.config > $(BUILDDIR)/.config.tmp
	@if diff -q $(BUILDDIR)/.config $(BUILDDIR)/.config.tmp > /dev/null ; then \
	 rm -f $(BUILDDIR)/.config.tmp ; \
	 else \
	 mv $(BUILDDIR)/.config.tmp $(BUILDDIR)/.config ; \
	 fi;
endif
	cd $(BUILDDIR); \
	 make -j$(PARALLEL_JOBS) -C $(PWD)/$(LINUXDIR) \
		$(if $(V),KBUILD_VERBOSE=$(V),)  ARCH=sparc O=`pwd` CROSS_COMPILE=$(TOOLCHAIN);
	@v=`grep "^VERSION *= *" $(PWD)/$(LINUXDIR)/Makefile | cut -s -d = -f 2` || exit 1; \
	 p=`grep "^PATCHLEVEL *= *" $(PWD)/$(LINUXDIR)/Makefile | cut -s -d = -f 2` || exit 1; \
	 if [ $$v -gt 3 -o $$v -eq 3 -a $$p -ge 5 ] ; then \
	 cp -fa $(BUILDDIR)/vmlinux $(TOP)/output/images/image || exit 1; \
	 else \
	 cp -fa $(BUILDDIR)/arch/sparc/boot/image $(TOP)/output/images/ || exit 1; \
	 fi

build: oldconfig
	$(MAKE) _build 2>&1 | tee $(BUILDDIR)/build.log

clean:
	cd $(BUILDDIR); \
	 make -C $(PWD)/$(LINUXDIR) ARCH=sparc O=`pwd` clean;

xconfig: mkbuilddir
	cd $(BUILDDIR); make -C $(PWD)/$(LINUXDIR) ARCH=sparc O=`pwd` xconfig

gconfig: mkbuilddir
	cd $(BUILDDIR); make -C $(PWD)/$(LINUXDIR) ARCH=sparc O=`pwd` gconfig

menuconfig: mkbuilddir
	cd $(BUILDDIR); make -C $(PWD)/$(LINUXDIR) ARCH=sparc O=`pwd` menuconfig

oldconfig: mkbuilddir
	cd $(BUILDDIR) && make -C $(PWD)/$(LINUXDIR) ARCH=sparc O=`pwd` oldconfig

# olddefconfig might not be available
defaultoldconfig:
	yes "" | make oldconfig

cfg_save:
	@if [ ! -d $(BUILDDIR) ] ; then \
	  echo ; \
	  echo "!!! No Linux Configuration created in project" ; \
	  echo ; \
	  exit 1; \
	  fi;
	mkdir -p $(CFG_BASE)/linux/$(BUILDDIR)
	cp -a $(BUILDDIR)/.config $(CFG_BASE)/linux/$(BUILDDIR)/.config

cfg_load:
	cp -fR $(CFG_BASE)/linux/* .
