include VERSION

# Override this to make script work with external Linux kernel sources
LINUX_TREE=linux-src
TOP:=$(PWD)
TOOLCHAIN=sparc-linux-
LEON_BRANCH=leon-$(KERNEL_BASE_VERSION)-$(KERNEL_EXTRAVERSION)
LEON_TAG=installed-$(LEON_BRANCH)

LINUX_GIT_BASE:=git://git.kernel.org/pub/scm
LINUX_OFFICIAL_REPO=$(LINUX_GIT_BASE)/linux/kernel/git/torvalds/linux.git
LINUX_OFFICIAL_STABLE_REPO=$(LINUX_GIT_BASE)/linux/kernel/git/stable/linux-stable.git
LINUX_INSTALL_METHOD?=-git

LINUX_TAR_BASE=https://www.kernel.org/pub/linux/kernel/v6.x
LINUX_TAR_DIR=linux-6.13.12
LINUX_TAR_SUPPORT=y # Checked by Linuxbuild
LINUX_TAR_FILE=$(LINUX_TAR_DIR).tar.gz
LINUX_TAR_URL=$(LINUX_TAR_BASE)/$(LINUX_TAR_FILE)

LINUX_LINK_SUPPORT=y # Checked by Linuxbuild

# Let user override e.g. LINUX_OFFICIAL_... or LINUX_TAR_BASE with faster mirrors (OPTIONAL)
MIRRORS=../../.mirrors
-include $(MIRRORS)

default:
	@echo You must specify build target
	@echo see README
	@echo

install:
	if [ $(LINUX_TAR_SUPPORT) = "y" -a "x$(LINUX_INSTALL_METHOD)" = "x-tar" ]; then \
		make install-tar ; \
	 elif [ $(LINUX_LINK_SUPPORT) = "y" -a "x$(LINUX_INSTALL_METHOD)" = "x-link" ]; then \
		make install-link ; \
	 else \
		make install-git; \
	 fi

install-tar:
	if [ ! -f $(LINUX_TAR_FILE) ]; then $(TOP)/get.sh $(LINUX_TAR_URL); fi
	tar xf $(LINUX_TAR_FILE)
	mv $(LINUX_TAR_DIR) $(LINUX_TREE)
	cat $(TOP)/patches/* | patch -p1 -d $(LINUX_TREE)

# Target that download an install sources
install-git:
	@if ! `git --version > /dev/null` ; then \
	 echo ; \
	 echo "!!! You need to install git in order to download Linux sources";\
	 echo ; \
	 fi

# Download Git kernel is not already downloaded
	if [ ! -d "$(LINUX_TREE)" ]; then \
	 git clone $(LINUX_OFFICIAL_REPO) $(LINUX_TREE); \
	 fi

# Remove old LEON branch if already there
	-cd $(LINUX_TREE); \
	 git clean -f; \
	 git reset --hard; \
	 git checkout master; \
	 git branch -D $(LEON_BRANCH) > /dev/null 2>&1 || true;

# Update master branch if not already updated
	cd $(LINUX_TREE); \
	 git pull;

# Get stable branch if LEON is on stable branch, remove old stable if it exists
ifneq ($(KERNEL_BASE_VERSION),$(KERNEL_STABLE_VERSION))
	-cd $(LINUX_TREE); \
	 git branch -D $(KERNEL_BASE_VERSION)-stable > /dev/null 2>&1 || true; \
	 git fetch $(LINUX_OFFICIAL_STABLE_REPO) refs/heads/linux-$(KERNEL_BASE_VERSION).y:refs/heads/$(KERNEL_BASE_VERSION)-stable;
endif

# Create branch for this LEON version, and check it out
	cd $(LINUX_TREE); \
	 git branch $(LEON_BRANCH) $(LINUX_GIT_COMMIT); \
	 git checkout $(LEON_BRANCH);

# Install patches
	cd $(LINUX_TREE); \
	 git am $(TOP)/patches/*

# Tag end result so that the prefix from localversion-leon and the git status
# that gets added to the printed version does not both describe the LEON Linux
# patch set
	cd $(LINUX_TREE); \
	 git tag -m $(LEON_TAG) $(LEON_TAG)

# Just softlink in a source directory from somewhere else
install-link:
	test -d "$(LINUX_LINK_TARGET)"
	ln -s "$(LINUX_LINK_TARGET)" "$(LINUX_TREE)"

#
# Different Configure interfaces
#

menuconfig:
	cd $(LINUX_TREE); make ARCH=sparc menuconfig

xconfig:
	cd $(LINUX_TREE); make ARCH=sparc xconfig

gconfig:
	cd $(LINUX_TREE); make ARCH=sparc gconfig


# Build LEON Linux Kernel, virtual Image.
#
# The image will be located in $(LINUX_TREE)/arch/sparc/boot/image
build:
	cd $(LINUX_TREE); \
	 make ARCH=sparc CROSS_COMPILE=$(TOOLCHAIN) && ls -l arch/sparc/boot/image

# Build LEON Linux Kernel, for u-boot
#
# Note that you may need to set UIMAGE_LOADADDR if you RAM is not located
# at 0x40000000. For example if RAM is at 0x00000000 set add
# UIMAGE_LOADADDR=0x00004000 to the build line below.
#
# The image will be located in $(LINUX_TREE)/arch/sparc/boot/uImage
uImage:
	cd $(LINUX_TREE); \
	 make ARCH=sparc CROSS_COMPILE=$(TOOLCHAIN) uImage &&  ls -l arch/sparc/boot/uImage

# Build Linux Kernel Modules
modules:
	cd $(LINUX_TREE); make ARCH=sparc CROSS_COMPILE=$(TOOLCHAIN) modules
