# Makefile for 3pp/linux-st710x


### Requires these modules

REQUIRES += base/common/hw/include
REQUIRES += makesystem


### Component specification

COMPONENT_IS_3PP  = true

COMPONENT_TARGETS = $(TARGET_NAME_IPSTB_SH4)
NEEDS_ABS_PATHS = true


### Tar archive

ver          = 2.6.17
SRC_ARCHIVE  = linux-$(ver).tar.bz2
SRC_DIR      = linux-$(ver)
src_done     = $(SRC_DIR)/.source_done
modules_dir  = modules

yaffs2_archive = yaffs2-2006.07.06.tar.gz

pwc_version = v4l2-20061003-042702
pwc_dir     = pwc-$(pwc_version)
pwc_archive = $(pwc_dir).tar.bz2

PATCHES	     = $(CURDIR)/PATCHES

dist_include = $(TARGET_DIST_DIR)/include

kreatel_stuff = include drivers

config = kbox.config


### Targets

linux_kernel = kernel.pre

OTHER_TARGETS += $(TARGET_INSTALL_DIR)/linux/$(linux_kernel)
OTHER_TARGETS += $(TARGET_INSTALL_DIR)/include/linux
OTHER_TARGETS += $(TARGET_INSTALL_DIR)/linux/modules

gen_init_cpio_src = $(SRC_DIR)/usr/gen_init_cpio.c
gen_init_cpio = $(SRC_DIR)/usr/gen_init_cpio
OTHER_TARGETS += $(HOST_DIST_DIR)/bin/gen_init_cpio

SCRIPT_TARGETS += $(SRC_DIR)/scripts/gen_initramfs_list.sh

DOC_TARGETS = $(SRC_DIR)/COPYING
DOC_OMIT_SOURCE_DIR = yes
DOC_PATH = ../../doc/doc


### Extra cleanup

CLEANUP_FILES += $(SRC_DIR) $(modules_dir)
CLEANUP_FILES += $(linux_kernel) $(CURDIR)/opt


### Common.mk

include ../../../makesystem/common.mk


### Dependencies

$(src_done): $(SRC_ARCHIVE) $(PATCHES_DEP)
	$(UNPACK_SRC_ARCHIVE)
	$(APPLY_PATCHES)

	@echo "Adding yaffs2 support"
	@tar -xzf external_packages/$(yaffs2_archive)
	@mkdir -p $(SRC_DIR)/fs/yaffs
	@cp yaffs2/*.c $(SRC_DIR)/fs/yaffs/
	@cp yaffs2/*.h $(SRC_DIR)/fs/yaffs/
	@cp yaffs2/Makefile.kernel $(SRC_DIR)/fs/yaffs/Makefile
	@cp yaffs2/patches/yaffs_mtdif2.c $(SRC_DIR)/fs/yaffs/
	@rm -rf yaffs2

	@echo "Adding ST cryptocore"
	@cd $(SRC_DIR) && patch -p1 < ../cryptocore/linux-sh4-2.6.11.12-stm20-33-cryptocore.patch
	@cp cryptocore/cryptocore.h $(SRC_DIR)/include/linux/stm
	@cp cryptocore/cryptocore_v16.c $(SRC_DIR)/drivers/stm/cryptocore

	@echo "Adding network driver (7100)"
	@cp smsc_911x/* $(SRC_DIR)/drivers/net/smsc_911x
	patch -d $(SRC_DIR) -p1 < patches/stfdma_reboot.patch

	@echo "Updating network driver (7109)"
	@tar -xzf moto_stmmac_test.tgz -C $(SRC_DIR)
	patch -d $(SRC_DIR) -p1 < patches/smsc911x_crypto.patch
	patch -d $(SRC_DIR) -p1 < patches/stmmac.patch
	patch -d $(SRC_DIR) -p1 < patches/stmmac.c.quota.patch
	patch -d $(SRC_DIR) -p1 < patches/stmmac_reboot.patch
	patch -d $(SRC_DIR) -p1 < patches/phy_remove.patch

	@echo "Adding KreaTV support build patch"
	patch -d $(SRC_DIR) -p1 < patches/kreatv_support.patch

	@echo "Adding K-box drivers"
	@for dir in $(kreatel_stuff) ; do \
	  for f in $$(find $$dir); do \
            if [ \( ! -z "$${f##*svn*}" \) -a \( -f "$$f" \) ]; then \
	      echo "Copying" $$f "to $(SRC_DIR)/$${f%/*}" ; \
	      mkdir -p $(SRC_DIR)/$${f%/*} ; \
	      cp $$f $(SRC_DIR)/$${f%/*} ; \
	    fi ; \
	  done ; \
	done
	@touch $@

LMAKE := $(BSG_DEVSETUP) BSG_INCLUDE=$(dist_include) \
         $(MAKE) -C $(SRC_DIR) ARCH=sh CROSS_COMPILE="$(CROSS_PREFIX)"

$(linux_kernel): $(src_done) $(config)
	cp $(config) $(SRC_DIR)/.config
	$(LMAKE) oldconfig
	$(LMAKE) dep
	$(LMAKE) vmlinux
	cp $(SRC_DIR)/vmlinux $@
	$(LMAKE) modules 
#	rm -rf $(CURDIR)/$(modules_dir)
	$(LMAKE) modules_install \
	  DEPMOD=true INSTALL_MOD_PATH=$(CURDIR)/$(modules_dir)

$(SCRIPT_TARGETS): $(src_done)

$(gen_init_cpio_src): $(src_done)

$(gen_init_cpio): $(gen_init_cpio_src)
	$(HOST_CC) $< -o $@

$(HOST_DIST_DIR)/bin/gen_init_cpio: $(gen_init_cpio)
	cp $< $@

oldconfig:
	$(LMAKE) oldconfig

menuconfig:
	$(LMAKE) menuconfig

dep:
	$(LMAKE) dep

clean:
	$(LMAKE) clean

vmlinux:
	$(LMAKE) vmlinux

modules:
	$(LMAKE) modules 
	rm -rf $(CURDIR)/$(modules_dir)
	$(LMAKE) modules_install \
	  DEPMOD=true INSTALL_MOD_PATH=$(CURDIR)/$(modules_dir)

.PHONY: vmlinux modules

$(TARGET_INSTALL_DIR)/linux/$(linux_kernel): $(linux_kernel)
	mkdir -p $(TARGET_INSTALL_DIR)/linux
	cp $(linux_kernel) $(TARGET_INSTALL_DIR)/linux

$(TARGET_INSTALL_DIR)/include/linux: $(linux_kernel)
	rm -rf $(TARGET_INSTALL_DIR)/include/linux
	rm -rf $(TARGET_INSTALL_DIR)/include/mtd
	rm -rf $(TARGET_INSTALL_DIR)/include/video
	rm -rf $(TARGET_INSTALL_DIR)/include/sound
	rm -rf $(TARGET_INSTALL_DIR)/include/media
	rm -rf $(TARGET_INSTALL_DIR)/include/asm-sh
	rm -rf $(TARGET_INSTALL_DIR)/include/asm-generic
	rm -rf $(TARGET_INSTALL_DIR)/include/asm
	mkdir -p $(TARGET_INSTALL_DIR)/include
	cp -a $(SRC_DIR)/include/linux $(TARGET_INSTALL_DIR)/include
	mkdir -p $(TARGET_INSTALL_DIR)/include/net
	cp -a $(SRC_DIR)/include/net/checksum.h $(TARGET_INSTALL_DIR)/include/net
	cp -a $(SRC_DIR)/include/asm-sh $(TARGET_INSTALL_DIR)/include
	cp -a $(SRC_DIR)/include/asm-generic $(TARGET_INSTALL_DIR)/include
	cp -a $(SRC_DIR)/include/mtd $(TARGET_INSTALL_DIR)/include
	cp -a $(SRC_DIR)/include/video $(TARGET_INSTALL_DIR)/include
	cp -a $(SRC_DIR)/include/sound $(TARGET_INSTALL_DIR)/include
	cp -a $(SRC_DIR)/include/media $(TARGET_INSTALL_DIR)/include
	cp $(SRC_DIR)/drivers/serial/stasc.h $(TARGET_INSTALL_DIR)/include/linux
	cp $(SRC_DIR)/drivers/media/video/pwc/pwc-ioctl.h $(TARGET_INSTALL_DIR)/include/linux
	cp $(SRC_DIR)/fs/yaffs/yportenv.h $(TARGET_INSTALL_DIR)/include/linux
	touch $(TARGET_INSTALL_DIR)/include/linux/devextras.h
	cd $(TARGET_INSTALL_DIR)/include && ln -sf asm-sh asm
	@touch $@

$(TARGET_INSTALL_DIR)/linux/modules: $(linux_kernel)
	rm -rf $(TARGET_INSTALL_DIR)/linux/modules
	mkdir -p $(TARGET_INSTALL_DIR)/linux \
	  $(TARGET_INSTALL_DIR)/linux/modules
	cp $$(find modules/lib/modules -type f -print) \
	  $(TARGET_INSTALL_DIR)/linux/modules
	@touch $@

$(DOC_TARGETS): $(src_done)

