#
#                             IPSTB Project
#                   ---------------------------------
#
# Copyright (C) 2009 NXP B.V.,
# All Rights Reserved.
#
# Filename: initramfs.mk
#
#
# Rev Date       Author      Comments
#-------------------------------------------------------------------------------
#   1 20060915   batelaan    Initial
#   2 20061129   batelaan    Use it for dsp rootfs.
#   3 20070124   batelaan    Check config files presence.
#                            Use new BUILDCONFIG_ID_ENDIAN_INDEPENDENT var.
#   4 20070219   batelaan    Correct *.meldconfig and *.menuconfig targets.
#   5 20070219   batelaan    Update with buildroot changes.
#   6 20070221   batelaan    Implement initramfs.patch_EXTRACMDS
#   7 20070223   batelaan    Added *.updconfig and *.diffconfig targets.
#   8 20070418   batelaan    Move *.config into configs subdir.
#   9 20070703   batelaan    Rename config files.
#  11 20070914   batelaan    Remove workaround for rootfs on local disks.
#  12 20071210   batelaan    Config files moved to buildroot directory.
#  13 20080425   batelaan    Simplify. Assume if initramfs is used then no
#                            other rootfs is also used.
#  14 20080523   batelaan    Use busybox's own menuconfig command.
#16.1.1 20090216 laird       Use buildroot version id.
#  18 20090403   amccurdy    Don't stripdir here, it's now called from kernel makefile
#  xx 20090812   amccurdy    Set initramfs_VERSION to $(buildroot_VERSION) instead of 'build'
#  xx 20090812   amccurdy    Fix untar_EXTRACMDS

# File: linux/initramfs.mk
# Makefile to control building of an initramfs for linux (the kernel).
#
# The resultant filesystem is <initramfs_ROOTFS>.
#
# The implementation of building initramfs reuses some of the buildroot package
# command definition variables, e.g. <buildroot.untar_CMDS> <buildroot.patch_CMDS>
# <buildroot.config_CMDS> and <buildroot.make_CMDS>.
# Various buildroot variables are redefined when these targets are executed.

ifeq "$(linux_USE_INITRAMFS)" "true"

include $(PRJROOT)/etc/first.mk

$(eval $(PKG_START))

PKG:=initramfs
# It's irrelevant what the version is. What counts is the version of buildroot we use
initramfs_VERSION:=$(buildroot_VERSION)
initramfs_UNTARINSIDE:=true
# Don't build this package normally, it is built as part of the linux package.
initramfs_SKIPBUILD:=true

$(eval $(PKG_SETUP_VARS))

# Inherit some variables from the buildroot package:
initramfs_PATCHES=$(buildroot_PATCHES)
initramfs_TARBALL=$(buildroot_TARBALL)

# Variable: initramfs_BUILDDIR
# Directory where initramfs build takes place.
# Buildroot package is untarred here.
initramfs_BUILDDIR=$(CPUBUILDROOT)/packages/initramfs.build

# Variable: initramfs_ROOTFS
# The resultant initramfs root filesystem directory.
# The buildroot package will install into this.
initramfs_ROOTFS=$(initramfs_BUILDDIR)/rootfs

# Variable: initramfs_STAGINGDIR
# The staging directory that the buildroot package for initramfs uses.
initramfs_STAGINGDIR=$(initramfs_BUILDDIR)/staging

# Variable: initramfs_SRCDIRREAL
# Saved value of <initramfs_SRCDIR>. Needed for setting up <initramfs.patch> target
# where we temporarily have to redefine <initramfs_SRCDIR>.
initramfs_SRCDIRREAL:=$(value initramfs_SRCDIR)

$(call PKG_DEFINE_TARGETS,help clean untar)

# Macro: initramfs.untar_EXTRACMDS
# Gets rid of extra buildroot subdirectory below <initramfs_BUILDDIR>.
define initramfs.untar_EXTRACMDS
	$(Q)cd $(initramfs_BUILDDIR)/buildroot-$(buildroot_VERSION) && \
		mv $$(/bin/ls -A) .. && cd .. && rmdir buildroot-$(buildroot_VERSION)
endef

# Macro: initramfs_busybox_BUILDDIR
# Build directory for the busybox package within buildroot for initramfs.
initramfs_busybox_BUILDDIR=$(buildroot_BUILDDIR)/build_$(ARCH)_glibc/busybox-*

#-------------------------------------------------------------------------------
# BUILDROOT OVERRIDES
#-------------------------------------------------------------------------------

# Macro: initramfs_BUILDROOT_TARGETS_VARS_OVERRIDE
# List of variable overrides such that we can resuse the buildroot mechanism
# for building initramfs.
#
# See: <initramfs_BUILDROOT_TARGETS_USED>
initramfs_BUILDROOT_TARGETS_VARS_OVERRIDE=\
	initramfs_SRCDIR=$(buildroot_SRCDIR) \
	initramfs_DI_SRCDIR=$(buildroot_DI_SRCDIR) \
	buildroot_BUILDDIR=$(initramfs_BUILDDIR) \
	ROOTFS=$(initramfs_ROOTFS) \
	STAGINGDIR=$(initramfs_STAGINGDIR) \
	buildroot_VAR_CHECK=

# Macro: initramfs_BUILDROOT_TARGETS_USED
# List of targets where we have to do variable overrides such that we can resuse the buildroot mechanism
# for building initramfs.
#
# See: <initramfs_BUILDROOT_TARGETS_VARS_OVERRIDE> <OBJ_TARGET_OVERRIDES>
initramfs_BUILDROOT_TARGETS_USED=\
	initramfs.menuconfig \
	initramfs.meldconfig \
	initramfs.diffconfig \
	initramfs.updconfig \
	initramfs.busybox.menuconfig \
	initramfs.busybox.meldconfig \
	initramfs.busybox.diffconfig \
	initramfs.busybox.updconfig \
	initramfs.patch \
	initramfs.config \
	initramfs.make \
	initramfs.help

# Setup target variable overrides:
$(foreach target, $(initramfs_BUILDROOT_TARGETS_USED),\
	$(call OBJ_TARGET_OVERRIDES,$(target), $(value initramfs_BUILDROOT_TARGETS_VARS_OVERRIDE)))

#-------------------------------------------------------------------------------
# PATCH
#-------------------------------------------------------------------------------
# Target: initramfs.patch
# Uses <buildroot_SRCDIR> as input.
initramfs_SRCDIR=$(buildroot_SRCDIR)
define initramfs.patch_EXTRACMDS
	$(Q)rm -f $(initramfs_BUILDDIR)/local
	$(Q)ln -s $(buildroot_SRCDIR)/local $(initramfs_BUILDDIR)/local
endef
$(call PKG_DEFINE_TARGETS,patch)
initramfs_SRCDIR:=$(value initramfs_SRCDIRREAL)
initramfs_PATCHOPTS=$(buildroot_PATCHOPTS)

#-------------------------------------------------------------------------------
# CONFIG
#-------------------------------------------------------------------------------
# Target: buildroot.config
# Uses <buildroot.config_CMDS>.
# First creates <initramfs_ROOTFS> and <initramfs_STAGINGDIR>.
define initramfs.config_CMDS
	$(Q)mkdir -p $(initramfs_ROOTFS) $(initramfs_STAGINGDIR)
	$(buildroot.config_CMDS)
endef
$(call PKG_DEFINE_TARGETS,config)

# Target: initramfs.menuconfig
# Runs menuconfig target on buildroot for initramfs.
# Modifies <initramfs_busybox_BUILDDIR>/.config .
initramfs.menuconfig:
	$(Q)$(buildroot_MAKECMD) menuconfig

# Target: initramfs.meldconfig
# Compares source buildroot config file (<buildroot_CONFIG>) used for initramfs
# with the one actually used in the build (<initramfs_BUILDDIR>/.config).
initramfs.meldconfig:
	-meld $(buildroot_CONFIG) $(buildroot_BUILDDIR)/.config

initramfs.diffconfig:
	-diff $(DIFFCONFIG_OPTS) $(buildroot_CONFIG) $(buildroot_BUILDDIR)/.config

initramfs.updconfig:
	cp -f $(buildroot_BUILDDIR)/.config $(buildroot_CONFIG)

# Target: initramfs.menuconfig
# Runs menuconfig target on buildroot for initramfs.
# Modifies <initramfs_busybox_BUILDDIR>/.config .
initramfs.busybox.menuconfig:
	$(Q)$(call BUILDROOT_BUSYBOX_DO_IF_ENABLED,$(buildroot_MAKECMD) busybox-menuconfig)

# Target: initramfs.busybox.meldconfig
# Compares source busybox config file (<buildroot_busybox_CONFIG>)
# with the one actually used in the build (<initramfs_busybox_BUILDDIR>/.config).
initramfs.busybox.meldconfig:
	-$(Q)$(call BUILDROOT_BUSYBOX_DO_IF_ENABLED,meld $(buildroot_busybox_CONFIG) $(buildroot_busybox_BUILDDIR)/.config)

initramfs.busybox.diffconfig:
	-$(Q)$(call BUILDROOT_BUSYBOX_DO_IF_ENABLED,diff $(DIFFCONFIG_OPTS) $(buildroot_busybox_CONFIG) $(buildroot_busybox_BUILDDIR)/.config)

initramfs.busybox.updconfig:
	$(Q)$(call BUILDROOT_BUSYBOX_DO_IF_ENABLED,cp -f $(buildroot_busybox_BUILDDIR)/.config $(buildroot_busybox_CONFIG))

#-------------------------------------------------------------------------------
# MAKE
#-------------------------------------------------------------------------------

# Target: initramfs.make
# Builds initramfs. This target is added as a prerequisite of target <linux.make>.
# We again ensure that $<initramfs_ROOTFS> exists because rootfs.clean could have removed it.
define initramfs.make_CMDS
	$(Q)mkdir -p $(initramfs_ROOTFS) $(initramfs_STAGINGDIR)
	$(buildroot.make_CMDS)
endef
$(call PKG_DEFINE_TARGETS,make)

# Make <initramfs.make> target a prerequisite of target <linux.make>.
$(call OBJ_TARGET_DEPS, linux.make, initramfs.make)
# Ditto for clean.
$(call OBJ_TARGET_DEPS, linux.clean, initramfs.clean)

initramfs.help:
	@printf 'make initramfs.menuconfig\t\t- Runs make menuconfig to allow changing initramfs build .config file\n'
	@printf 'make initramfs.meldconfig\t\t- Runs meld on initramfs source and build .config files\n'
	@printf 'make initramfs.diffconfig\t\t- Runs diff on initramfs source and build .config files\n'
	@printf 'make initramfs.updconfig\t\t- Updates initramfs source .config file from the build .config file\n'
	@printf '     initramfs source .config file\t- $(patsubst $(PRJROOT)/%,%,$(buildroot_CONFIG))\n'
	@printf '     initramfs build  .config file\t- $(patsubst $(PRJROOT)/%,%,$(initramfs_BUILDDIR)/.config)\n'
	@printf 'make initramfs.busybox.menuconfig\t- Runs make menuconfig to allow changing busybox build .config file\n'
	@printf 'make initramfs.busybox.meldconfig\t- Runs meld on busybox source and build .config files\n'
	@printf 'make initramfs.busybox.diffconfig\t- Runs diff on busybox source and build .config files\n'
	@printf 'make initramfs.busybox.updconfig\t- Updates busybox source from build .config file\n'
	@printf '     busybox source .config file\t- $(patsubst $(PRJROOT)/%,%,$(buildroot_busybox_CONFIG))\n'
	@printf '     busybox build  .config file\t- $(patsubst $(PRJROOT)/%,%,$(buildroot_busybox_BUILDDIR)/.config)\n'
	@printf 'Note: initramfs is built using buildroot.\n'

#-------------------------------------------------------------------------------
$(eval $(PKG_END))

include $(PRJROOT)/etc/last.mk

endif # ifeq "$(linux_USE_INITRAMFS)" "true"
