include VERSION

# Override this to make script work with external Linux-2.6 sources
LINUX_TREE=linux-2.6
TOP:=$(PWD)
TOOLCHAIN=sparc-linux-
ifeq ($(BRANCH),)
LEON_BRANCH=leon-$(VERSION)
else
LEON_BRANCH=leon-$(BRANCH)-$(VERSION)
endif

LINUX_GIT_BASE:=git://git.kernel.org/pub/scm
LINUX_OFFICIAL_REPO:=$(LINUX_GIT_BASE)/linux/kernel/git/torvalds/linux-2.6.git
LINUX_OFFICIAL_STABLE_REPO:=$(LINUX_GIT_BASE)/linux/kernel/git/stable/linux-$(LINUX_GIT_STABLE).y.git 

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

# Target that download an install sources
install:
	@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);

# 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 ($(LINUX_GIT_STABLE),)
	-cd $(LINUX_TREE); \
	 git branch -D $(LINUX_GIT_STABLE)-stable; \
	 git fetch $(LINUX_OFFICIAL_STABLE_REPO) refs/heads/master:refs/heads/$(LINUX_GIT_STABLE)-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/*


#
# 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
