#
#                             IPSTB Project
#                   ---------------------------------
#
# Copyright (C) 2009 NXP B.V.,
# All Rights Reserved.
#
# Filename: buildroot/Makefile
#
#
# Rev Date        Author      Comments
#-------------------------------------------------------------------------------
#  20 20061206    steel       move to opt/nxp
#  21 20070207    batelaan    Update names of config files. Add diffconfig and updconfig targets.
#  22 20070216    batelaan    Update to new buildroot version 20070123
#  23 20070302    batelaan    Add mechanism for determining source tarballs used by buildroot.
#  24 20070418    batelaan    Move *.config to subdir configs.
#  25 20070525    steel       add target flags
#  26 20070703    batelaan    Allow config-specific buildroot and busybox config files.
#  27 20070822    batelaan    buildroot.config target should depend on input config file.
#  28 20071009    amccurdy    Don't include full PATH when setting TARGET_CROSS
#  29 20071210    batelaan    Support initramfs...config filenames.
#  30 20080130    laird       Update buildroot version.
#  31 20080201    laird       Update variable names that are used by buildroot.
#  32 20080214    batelaan    New buildroot version needs different buildroot_busybox_BUILDDIR.
#  33 20080222    batelaan    New utility targets buildroot.<pkg>.{dirclean,clean,make}
#  34 20080222    batelaan    Detect when buildroot.patch is out of date, and fail if we can't repatch.
#  35 20080319    laird       Update version.
#  36 20080407    batelaan    Correct buildroot_busybox_BUILDDIR
#  37 20080423    amccurdy    Generate unified diffs in XXX.diffconfig targets
#  38 20080425    batelaan    Use compact format for diffconfig target.
#                             Use prjconfig for config file spec.
#  39 20080523    laird       Update buildroot.
#  40 20080523    batelaan    Use busybox's own menuconfig command.
#  41 20080528    batelaan    Use CONFEDIT macros.
#                             Prevent unintentional overwriting of modified .config file.
#  42 20080528    batelaan    Fix make syntax error in error handling code
#  43 20080718    batelaan    Fix buildroot_xxx_BUILDDIR to now use gmake wildcard function.
#  44 20080723    laird       Set LINUX26_DIR so that packages can use it in buildroot.
#  45 20080724    batelaan    Don't use : in buildroot_xxx_BUILDDIR value (causes ssh to be used)
#  46 20080926    batelaan    Use _TMMAKEJOBS i.s.o. CONFIG_NUM_BUILD_JOBS variable for -j option.
#  47 20081013    batelaan    Check for warnings re incorrect symbols config stage. CONFEDIT macros changed.
#  48 20081015    batelaan    Forgive C compilation warnings in make oldconfig.
#  49 20081218    laird       Support sysrootable compilers.
#  50 20090115    laird       Bounce buildroot version.
#50.1.1 20090115  batelaan    Add buildroot.toolchain target.
#  51 20090115    laird       Bounce buildroot version and fix sysroot handling.
#  52 20090116    laird       Merged from 'Makefile-SOUsb225#51:makefile:ipstb#101' and 'Makefile-SOUsb225#50.1.1:makefile:ipstb#101' by laird.
#  53 20090212    laird       Bounce to stable buildroot version (3 monthly release cycle now)
#  54 20090312    batelaan    Add buildroot_AVAILABLECONFIGFILE and include $(buildroot_AVAILABLECONFIGFILE)
#  55 20090324    batelaan    Pass -j1 to buildroot make to prevent parallelism problems.
#  56 20090327    batelaan    libc/uclibc capable.
#  57 20090604    laird       Update buildroot_VERSION 2009.02 -> 2009.05, remove TARGET_LDFLAGS from buildroot_DEFCONFIGOPTS (not needed anymore ?)
#  58 20090617    amccurdy    Add buildroot and busybox udiffconfig targets
#  59 20090625    laird       Add LF support.
#  xx 20091016    amccurdy    Add buildroot_CONFEDIT_DIRECTFB_MULTIAPP

# File: buildroot/Makefile
# Makefile to control building of buildroot.
#

include $(PRJROOT)/etc/first.mk

$(eval $(PKG_START))

PKG=buildroot
buildroot_VERSION:=2009.05

# Variable: STAGINGDIR
# Directory where buildroot puts its intermediate build results,
# from which buildroot then copies to <ROOTFS>.

$(eval $(PKG_SETUP_VARS))

# Define: buildroot_CONFIG
# Input file path for buildroot configuration, under configuration control.
#
# The file name is specified by <buildroot_CONFIG_FILE>
# and searched for in directories:
# - <buildroot_DI_SRCDIR>/configs
# - <buildroot_SRCDIR>/configs
#
# See also <buildroot_busybox_CONFIG>.
buildroot_CONFIG := $(firstword $(wildcard \
  $(buildroot_DI_SRCDIR)/configs/$(buildroot_CONFIG_FILE) \
  $(buildroot_SRCDIR)/configs/$(buildroot_CONFIG_FILE) \
))

ifeq "$(wildcard $(buildroot_CONFIG))" ""
  $(warning $$buildroot_CONFIG_FILE is "$(buildroot_CONFIG_FILE)")
  $(warning Check value of buildroot config file option(s) in prjconfig!)
  $(error $$buildroot_CONFIG setup error)
endif

# Macro: buildroot_AVAILABLECONFIGFILE
# Evaluates to the first file that exists, which is either:-
# - <buildroot_BUILDDIR>/.config
# - <buildroot_CONFIG>
buildroot_AVAILABLECONFIGFILE=$(firstword $(shell ls $(buildroot_BUILDDIR)/.config $(buildroot_CONFIG) 2>/dev/null))

# Include it so that we can use some variables from it in checking whether
# certain components/applications need/can be built.
$(call INCLUDE_MAKEFILE,$(buildroot_AVAILABLECONFIGFILE))

# Macro: buildroot_CONFIG_FILE
# Name of the buildroot configuration file.
# Used by <buildroot_CONFIG>.
# Must be set outside this Makefile, usually in
# <platform_control_cpu_linux.mk> or <platform_dsp_cpu.mk> .

# Define: buildroot_busybox_CONFIG
# Input file path for buildroot busybox configuration, under configuration control.
#
# The file name is specified by <buildroot_busybox_CONFIG_FILE>
# and searched for in directories:
# - <buildroot_DI_SRCDIR>/configs
# - <buildroot_SRCDIR>/configs
buildroot_busybox_CONFIG := $(firstword $(wildcard \
  $(buildroot_DI_SRCDIR)/configs/$(buildroot_busybox_CONFIG_FILE) \
  $(buildroot_SRCDIR)/configs/$(buildroot_busybox_CONFIG_FILE) \
))

ifeq "$(wildcard $(buildroot_busybox_CONFIG))" ""
  $(warning $$buildroot_busybox_CONFIG_FILE is "$(buildroot_busybox_CONFIG_FILE)")
  $(warning Check value of busybox config file option(s) in prjconfig!))
  $(error $$buildroot_busybox_CONFIG setup error))
endif

# Macro: buildroot_busybox_CONFIG_FILE
# Name of the busybox configuration file.
# Used by <buildroot_busybox_CONFIG>.
# Must be set outside this Makefile, usually in
# <platform_control_cpu_linux.mk> or <platform_dsp_cpu.mk> .

# Define: buildroot_xxx_BUILDDIR
# Directory where package xxx is built by buildroot.
# If it non-existant, or not yet built, it returns:-
# Oops!-Package-{xxx}-not-yet-built-or-non-existant
#
# Usage: $(call buildroot_xxx_BUILDDIR,PACKAGENAME)
buildroot_xxx_BUILDDIR=\
  $(firstword \
    $(shell ls -d 2>/dev/null \
      $(buildroot_BUILDDIR)/project_build_$(ARCH)$(filter el,$(_TMTGTENDIAN))*/*/$1-[0-9]* \
      $(buildroot_BUILDDIR)/build_$(ARCH)$(filter el,$(_TMTGTENDIAN))*/$1-[0-9]* \
      $(buildroot_BUILDDIR)/project_build_$(ARCH)$(filter el,$(_TMTGTENDIAN))*/*/$1-* \
      $(buildroot_BUILDDIR)/build_$(ARCH)$(filter el,$(_TMTGTENDIAN))*/$1-*) \
    Oops!-Package-$1-not-yet-built-or-non-existant)
# Note we use $(shell ls ...) instead of $(wildcard ...) since wildcard
# function uses make's internal cache storing info on whether files exist.
# thus if some target produces the file, but not it is not a make target,
# then make will not know about it if make has looked the filepath up before!

# Define: buildroot_busybox_BUILDDIR
# Directory where busybox is built by buildroot.
buildroot_busybox_BUILDDIR=$(buildroot_BUILDDIR)/project_build_$(ARCH)$(filter el,$(_TMTGTENDIAN))*/*/busybox-*

# Define: buildroot_busybox_ENABLED
# Value of variable <BR2_PACKAGE_BUSYBOX> (empty or y).
# This is extracted from either:-
# - <buildroot_BUILDDIR>/.config
# - <buildroot_CONFIG>
buildroot_busybox_ENABLED=$(patsubst BR2_PACKAGE_BUSYBOX=%,%,$(shell \
  grep 'BR2_PACKAGE_BUSYBOX=y' $(shell \
    ls $(wildcard $(buildroot_BUILDDIR)/.config $(buildroot_busybox_CONFIG) /dev/null 2>/dev/null))))

#Define the gnu host target name.
ifeq "$(TARGET)" "i386-linux-gnu"
  buildroot_TARGET_NAME=i386-linux-gnu
else
  buildroot_TARGET_NAME=$(shell $(SHELL) -c '$(CROSS_COMPILE)gcc -dumpmachine')
endif

# Define: buildroot_VAR_CHECK
# Set of commands to check that <ROOTFS> and <STAGINGDIR> have expected values,
# namely:-
# - <ROOTFS> should be $$(CPUBUILDROOT)/rootfs
# - <STAGINGDIR> should be $$(CPUBUILDROOT)/staging
define buildroot_VAR_CHECK
	@$(if $(filter-out $(CPUBUILDROOT)/rootfs,$(ROOTFS)),$(error For buildroot $$ROOTFS should be $$(CPUBUILDROOT)/rootfs))
	@$(if $(filter-out $(CPUBUILDROOT)/staging,$(STAGINGDIR)),$(error For buildroot $$STAGINGDIR should be $$(CPUBUILDROOT)/staging))
endef

# ccache can't do a good job of making directory names that don't
# exist relative so we ensure that /usr/local/include exists (for
# MIPS1) and /include exists (for MIPS2.)
define buildroot.untar_POSTUNTARCMDS
	$(Q)mkdir -p $(STAGINGDIR)/opt/nxp/{bin,include,lib}
	$(Q)mkdir -p $(STAGINGDIR)/usr/local/include
	$(Q)mkdir -p $(STAGINGDIR)/include
endef

# Setup default options for buildroot......
buildroot_PATCHOPTS:=--multipatch
buildroot_DEFCFLAGS:= $(DEFCFLAGS) $(DEFCDEFINES) $(DEFCINCLUDES) -O2
buildroot_DEFLDFLAGS:= $(DEFLDFLAGS) -lm

# Do we have a compiler with sysroot support? Can only pass --sysroot if we do!
# This test has only be proved true for comparing ELDK -> Tuxbuilder.
# This may not hold true for other toolchains.......
buildroot_SYSROOT_SUPPORT=$(shell $(CROSS_COMPILE)gcc -v 2>&1 | egrep -- '--with-(build-)?sysroot' )
buildroot_DEFCFLAGS+=$(if $(buildroot_SYSROOT_SUPPORT),--sysroot=$(STAGINGDIR)) -ggdb -L$(STAGINGDIR)/lib -lm
buildroot_DEFLDFLAGS+=$(if $(buildroot_SYSROOT_SUPPORT),--sysroot=$(STAGINGDIR)) -ggdb

# Make sure that buildroot is built with Large file support (>2GB)
buildroot_DEFCFLAGS+=-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64

# Define: buildroot_DEFCONFIGOPTS
# Options for configuring buildroot.
# Sets:-
# - DL_DIR to <TARBALLS> (where it downloads all of the versions of the tools using wget)
# - BR2_PACKAGE_BUSYBOX_CONFIG is the location of the busybox.config file,
# set to <buildroot_busybox_CONFIG>.
# - TARGET_DIR to <ROOTFS>
# - STAGING_DIR to environment variable <STAGINGDIR>
# - TARGET_CROSS to filename prefix of gcc. Historically this included the directory path,
#   ie TARGET_CROSS=$(patsubst %gcc,%,$(shell $(SHELL) -c 'type -p $(CROSS_COMPILE)gcc'))
#   but passing the full path doesn't seem to be necessary and it disables ccache...
# - BR2_SOFT_FLOAT is set if -msoft-float is found is the <buildroot_CFLAGS>
# - BR2_ENABLE_DEBUG is set if we are building a debug release.
# - LINUX26_DIR is set to point to <KSRC> so that packages that need kernel headers can be built.

# Passing -L$(STAGINGDIR)lib is a hack to make avahi compile. It seems
# that its version of libtool isn't clever enough to understand
# --sysroot so it doesn't look for libglib-2.0 in the right places. It
# shouldn't be necessary really. I suspect that setting
# AVAHI_AUTORECONF would solve this but its autoconf files don't work
# with whatever version of autoconf we're trying to use. :(
#
# We need -lm for DirectFB. Quite how it can't work it out for itself
# I don't know but it might just be the stuff that Dave added.
buildroot_DEFCONFIGOPTS=\
	DL_DIR=$(TARBALLS) \
	BR2_PACKAGE_BUSYBOX_CONFIG=$(buildroot_busybox_CONFIG) \
	TARGET_DIR=$(ROOTFS) \
	STAGING_DIR=$(STAGINGDIR) \
	TARGET_CFLAGS="$(buildroot_CFLAGS)" \
	TARGET_CROSS=$(CROSS_COMPILE) \
	KERNEL_CROSS=$(CROSS_COMPILE) \
	$(if $(filter -msoft-float,$(buildroot_CFLAGS)),BR2_SOFT_FLOAT=y) \
	$(if $(filter debug,$(_TMTGTREL)),BR2_ENABLE_DEBUG=y) \
	-j1 \
	BR2_JLEVEL=$(_TMMAKEJOBS) \
	LINUX26_DIR=$(KSRC)

# Optionally sets: -
# - BR2_TOOLCHAIN_SYSROOT_PATH to <CROSS_COMPILE_ROOTFS> if we have a toolchain with sysroot support.
buildroot_DEFCONFIGOPTS+=$(if $(buildroot_SYSROOT_SUPPORT),BR2_TOOLCHAIN_SYSROOT_PATH="$(CROSS_COMPILE_ROOTFS)")

buildroot_DEFMAKEOPTS=$(buildroot_DEFCONFIGOPTS)
buildroot_DEFINSTALLOPTS=$(buildroot_DEFCONFIGOPTS)

# If MAKE is set then buildroot will export it's definition of MAKE
# which includes -j$(BR2_JLEVEL) to all sub-makes including ones
# spawned with $(MAKE1).
buildroot_MAKECMD=unset CROSS CROSS_COMPILE MAKE && $(MAKECMD) -C $(buildroot_BUILDDIR) $(buildroot_MAKEOPTS)

$(call PKG_DEFINE_TARGETS,help clean untar)

# Define: buildroot.clean_EXTRACMDS
# Deletes <STAGINGDIR>.
define buildroot.clean_EXTRACMDS
	rm -rf $(STAGINGDIR)
endef

#-------------------------------------------------------------------------------
# UNTAR
#-------------------------------------------------------------------------------

# If all buildroot sources are in the overlay directory, we don't need a tarfile.
#
ifeq "$(shell echo $(buildroot_VERSION))" "XXXX.XX"

    define buildroot.untar_CMDS
        @echo "  UNTAR Package buildroot (custom: no tarfile)"
        $(Q)if [ -d $(buildroot_BUILDDIR) ]; then \
          echo "Already exists: $(buildroot_BUILDDIR)"; echo "Make target 'buildroot.clean' (or 'clean') first."; \
          exit 1; fi
        $(Q)mkdir -p $(buildroot_BUILDDIR)
        $(Q)cd $(dir $(buildroot_BUILDDIR)) && rm -rf buildroot && ln -s $(notdir $(buildroot_BUILDDIR)) buildroot
        $(buildroot.untar_POSTUNTARCMDS)
        $(if $(buildroot_UNTARINSIDE),,$(Q)if [ ! -d $(buildroot_BUILDDIR) ]; then echo 'Untar did not create $(buildroot_BUILDDIR) !'; exit 1; fi)
        $(buildroot.untar_EXTRACMDS)
    endef

endif

#-------------------------------------------------------------------------------
# CONFIG
#-------------------------------------------------------------------------------

buildroot_CONFEDIT_TOOLCHAIN = echo 'Configuring toolchain for $(GCC_PREFIX)'; \
				  $(call CONFEDIT_SET_STRING,$(buildroot_BUILDDIR),BR2_TOOLCHAIN_EXTERNAL_PATH,$(CROSS_COMPILE_BASE)/); \
				  $(call CONFEDIT_SET_STRING,$(buildroot_BUILDDIR),BR2_TOOLCHAIN_EXTERNAL_PREFIX,$(patsubst %-,%,$(buildroot_TARGET_NAME))); \
				  $(call CONFEDIT_SET_STRING,$(buildroot_BUILDDIR),BR2_TOOLCHAIN_EXTERNAL_LIB_C,$(CROSS_COMPILE_BASE)/$(CROSS_COMPILE_PREFIX)/lib/$(CROSS_COMPILE_LIBC)); \
				  $(call CONFEDIT_SET_STRING,$(buildroot_BUILDDIR),BR2_TOOLCHAIN_EXTERNAL_LIBS,$(CROSS_COMPILE_LIB_FILES))

buildroot_CONFEDIT_ENDIANNESS	= echo 'Configuring endianness'; \
				  $(call CONFEDIT_$(if $(filter el,$(_TMTGTENDIAN)),SET,UNSET),$(buildroot_BUILDDIR),BR2_mipsel); \
				  $(call CONFEDIT_$(if $(filter el,$(_TMTGTENDIAN)),UNSET,SET),$(buildroot_BUILDDIR),BR2_mips); \
				  $(call CONFEDIT_SET_STRING,$(buildroot_BUILDDIR),BR2_ARCH,mipsel); \
				  $(call CONFEDIT_SET_STRING,$(buildroot_BUILDDIR),BR2_ENDIAN,$(if $(filter el,$(_TMTGTENDIAN)),LITTLE,BIG))

buildroot_CONFEDIT_DIRECTFB_MULTIAPP = \
				  echo 'Configuring DirectFB Multi-app support'; \
				  $(call CONFEDIT_$(if $(buildroot_ENABLE_DIRECTFB_MULTIAPP),SET,UNSET),$(buildroot_BUILDDIR),BR2_PACKAGE_DIRECTFB_MULTI); \
				  $(call CONFEDIT_$(if $(buildroot_ENABLE_DIRECTFB_MULTIAPP),SET,UNSET),$(buildroot_BUILDDIR),BR2_PACKAGE_LINUX_FUSION)

buildroot_CONFEDIT_WMV		= echo 'Configuring WMV'; \
				  $(call CONFEDIT_$(if $(CONFIG_WMV),SET,UNSET),$(buildroot_BUILDDIR),BR2_PACKAGE_GST_PLUGINS_BASE_GSTRTSP); \
				  $(call CONFEDIT_$(if $(CONFIG_WMV),SET,UNSET),$(buildroot_BUILDDIR),BR2_PACKAGE_GST_PLUGINS_BASE_GSTSDP); \
				  $(call CONFEDIT_$(if $(CONFIG_WMV),SET,UNSET),$(buildroot_BUILDDIR),BR2_PACKAGE_GST_PLUGINS_GOOD_PLUGIN_RTSP); \
				  $(call CONFEDIT_$(if $(CONFIG_WMV),SET,UNSET),$(buildroot_BUILDDIR),BR2_PACKAGE_GST_PLUGINS_UGLY); \
				  $(call CONFEDIT_$(if $(CONFIG_WMV),SET,UNSET),$(buildroot_BUILDDIR),BR2_PACKAGE_GST_PLUGINS_UGLY_PLUGIN_ASFDEMUX)


#buildroot_CHECK_FILE_IS_NOT_CHANGED = $(call CHECK_FILE_IS_NOT_CHANGED,$(buildroot_BUILDDIR)/.config,$(buildroot_BUILDDIR)/.config.refcopy, \
#  echo 'The buildroot.config target would overwrite this file which you may not want!'; \
#  echo 'Please verify that this is ok or save the modified file'; \
#  echo '(for example, using  prjmake buildroot.meldconfig).'; \
#  echo 'Then run: "rm $(buildroot_BUILDDIR)/.config.refcopy"'; \
#  echo 'and repeat the original command.')

define buildroot.config_CMDS
	$(buildroot_VAR_CHECK)
	$(Q)$(buildroot_CHECK_FILE_IS_NOT_CHANGED)
	$(Q)$(call CONFEDIT_INIT,$(buildroot_BUILDDIR),$(buildroot_CONFIG))
	$(Q)$(buildroot_CONFEDIT_TOOLCHAIN)
	$(Q)$(buildroot_CONFEDIT_ENDIANNESS)
	$(Q)$(buildroot_CONFEDIT_DIRECTFB_MULTIAPP)
	$(Q)$(buildroot_CONFEDIT_WMV)
	$(Q)$(call CONFEDIT_EXECUTE,$(buildroot_BUILDDIR))
	$(buildroot_MAKECMD) oldconfig 2>$(buildroot_BUILDDIR)/oldconfig.err
	$(Q)$(call CHECK_MAKE_OLDCONFIG_ERRORS,Buildroot make oldconfig,$(buildroot_BUILDDIR),oldconfig.err)
	$(Q)cd $(buildroot_BUILDDIR) && cp -fa .config .config.refcopy
endef
$(call PKG_TARGET, buildroot.config, buildroot.$(config_PREDECESSOR))
$(call OBJ_TARGET_OTHER_DEPS,buildroot.config,$(buildroot_CONFIG) $(BUILDROOT)/.prjconfig)

define buildroot.menuconfig_CMDS
	$(Q)$(buildroot_MAKECMD) menuconfig
endef
$(call PKG_TARGET, buildroot.menuconfig, buildroot.config)

define buildroot.kernel.menuconfig_CMDS
	$(Q)$(buildroot_MAKECMD) linux26-menuconfig
endef
$(call PKG_TARGET, buildroot.kernel.menuconfig, buildroot.config)

buildroot.meldconfig:
	-meld $(buildroot_CONFIG) $(buildroot_BUILDDIR)/.config

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

buildroot.udiffconfig:
	-diff -u $(buildroot_CONFIG) $(buildroot_BUILDDIR)/.config

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

define BUILDROOT_BUSYBOX_DO_IF_ENABLED
	$(if $(buildroot_busybox_ENABLED),\
	  if [ -f $(buildroot_BUILDDIR)/.config ]; then \
	    $1; \
	  else \
	    echo 'Busybox package not yet built.'; exit 1; \
	  fi, \
	  echo 'BR2_PACKAGE_BUSYBOX not set, so busybox is not built'; exit 1)
endef

define buildroot.busybox.menuconfig_CMDS
	$(Q)$(call BUILDROOT_BUSYBOX_DO_IF_ENABLED,$(buildroot_MAKECMD) busybox-menuconfig)
endef
$(call PKG_TARGET, buildroot.busybox.menuconfig, buildroot.config)

buildroot.busybox.meldconfig: buildroot.config
	-$(Q)$(call BUILDROOT_BUSYBOX_DO_IF_ENABLED,meld $(buildroot_busybox_CONFIG) $(buildroot_busybox_BUILDDIR)/.config)

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

buildroot.busybox.udiffconfig:
	-$(Q)$(call BUILDROOT_BUSYBOX_DO_IF_ENABLED,diff -u $(buildroot_busybox_CONFIG) $(buildroot_busybox_BUILDDIR)/.config)

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

buildroot.helpmid::
	@printf 'make buildroot.menuconfig\t\t- Runs make menuconfig to allow changing buildroot build .config file\n'
	@printf 'make buildroot.meldconfig\t\t- Runs meld on buildroot source and build .config files\n'
	@printf 'make buildroot.diffconfig\t\t- Runs "diff $${DIFFCONFIG_OPTS}" on buildroot source and build .config files\n'
	@printf 'make buildroot.udiffconfig\t\t- Runs "diff -u" on buildroot source and build .config files\n'
	@printf 'make buildroot.updconfig\t\t- Updates buildroot source .config file from the build .config file\n'
	@printf '     buildroot source .config file\t- $(patsubst $(PRJROOT)/%,%,$(buildroot_CONFIG))\n'
	@printf '     buildroot build  .config file\t- $(patsubst $(PRJROOT)/%,%,$(buildroot_BUILDDIR)/.config)\n'
	@printf 'make buildroot.busybox.menuconfig\t- Runs make menuconfig to allow changing busybox build .config file\n'
	@printf 'make buildroot.busybox.meldconfig\t- Runs meld on busybox source and build .config files\n'
	@printf 'make buildroot.busybox.diffconfig\t- Runs "diff $${DIFFCONFIG_OPTS}" on busybox source and build .config files\n'
	@printf 'make buildroot.busybox.udiffconfig\t- Runs "diff -u" on busybox source and build .config files\n'
	@printf 'make buildroot.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'

#-------------------------------------------------------------------------------
# Patch
#-------------------------------------------------------------------------------

# Name of generated file used by mechanism for determining source tarballs used by buildroot.
# See ./record_package_source.pl file.
BUILDROOT_record_package_source_file=record_package_source.mk

# File to indicate that buildroot.patch target has finished. It prevents buildroot.patch
# from being run again. This is needed since we need to force 'cleaning' buildroot
# packages, they are otherwise not rebuilt after patching.
BUILDROOT_patch_guard=$(call TSFILE,buildroot.patch@guard)

define buildroot.patch_EXTRACMDSBEFORE
	$(Q)if [ -f $(BUILDROOT_patch_guard) ]; then \
		echo ''; \
		echo '**ERROR: buildroot.patch is out-of-date, but patch results may have already been used'; \
		echo '(since file "$(BUILDROOT_patch_guard)" exists).'; \
		echo 'I cannot handle re-patching buildroot automatically! :-('; \
		echo 'Please run buildroot.clean first. IF you know what you'; \
		echo 'are doing (!), and you know which buildroot package needs'; \
		echo 'repatching/rebuilding (call it xxx), then use this workaround:'; \
		echo '    rm $(BUILDROOT_patch_guard)'; \
		echo '    make buildroot.patch'; \
		echo '    make buildroot.xxx.dirclean buildroot.xxx.make'; \
		exit 1; \
	fi
endef

define buildroot.patch_EXTRACMDS
	$(Q)rm -f $(buildroot_BUILDDIR)/local
	$(Q)ln -s $(buildroot_SRCDIR)/local $(buildroot_BUILDDIR)/local
	$(Q)$(if $(filter true,$(PRJRELEASEBUILD)),cd $(buildroot_BUILDDIR); \
	  echo "# WARNING: auto-generated by NXP buildsystem" >$(BUILDROOT_record_package_source_file); \
	  perl $(buildroot_SRCDIR)/record_package_source.pl `find local/. package target  toolchain -name '*.mk'` >>$(BUILDROOT_record_package_source_file); \
	  if ! grep $(BUILDROOT_record_package_source_file) Makefile >/dev/null; then \
	    chmod +w Makefile; \
	    echo "include $(BUILDROOT_record_package_source_file)" >>Makefile; \
	  fi)
	$(Q)$(call TSSET,buildroot.patch@guard)
endef
$(call PKG_DEFINE_TARGETS,patch)

#-------------------------------------------------------------------------------
# TOOLCHAIN SETUP
#-------------------------------------------------------------------------------
# Target: buildroot.toolchain
# Installs the cross-compiler header and library files into ${<STAGINGDIR>}.
define buildroot.toolchain_CMDS
	$(Q)$(buildroot_MAKECMD) cross_compiler
endef
$(call PKG_TARGET, buildroot.toolchain, buildroot.$(make_PREDECESSOR))

#-------------------------------------------------------------------------------
# MAKE
#-------------------------------------------------------------------------------
define buildroot.make_CMDS
	$(Q)$(buildroot_MAKECMD)
endef
$(call PKG_TARGET, buildroot.make, buildroot.$(make_PREDECESSOR) buildroot.toolchain rootfs.init)

#-------------------------------------------------------------------------------
# INSTALL
#-------------------------------------------------------------------------------
# Nothing todo here just tell the user so they don't get worried
define buildroot.install_CMDS
	@echo "Buildroot has already been installed via buildroot.make"
endef
$(call PKG_TARGET, buildroot.install, buildroot.$(install_PREDECESSOR))

#-------------------------------------------------------------------------------
# Target: buildroot.xxx.dirclean
# Makes the xxx-dirclean target in the buildroot build directory.
# Usage:-
# <start code>
# make buildroot.xxx.dirclean
# <end code>

# Target: buildroot.xxx.clean
# Makes the xxx-clean target in the buildroot build directory.
# Usage:-
# <start code>
# make buildroot.xxx.clean
# <end code>

# Target: buildroot.xxx.make
# Makes the xxx target in the buildroot build directory.
# Usage:-
# <start code>
# make buildroot.xxx.make
# <end code>

buildroot.%.dirclean:
	$(Q)$(buildroot_MAKECMD) $*-dirclean
buildroot.helpmid:: buildroot.help.xxx.dirclean
buildroot.help.xxx.dirclean:; @printf '%-40s- Makes the xxx-dirclean target in the buildroot build directory\n' 'make buildroot.xxx.dirclean'

buildroot.%.clean: buildroot.config
	$(Q)$(buildroot_MAKECMD) $*-clean
buildroot.helpmid:: buildroot.help.xxx.clean
buildroot.help.xxx.clean:; @printf '%-40s- Makes the xxx-clean target in the buildroot build directory\n' 'make buildroot.xxx.clean'

buildroot.%.make: buildroot.config
	$(Q)$(buildroot_MAKECMD) $*
buildroot.helpmid:: buildroot.help.xxx.make
buildroot.help.xxx.make:; @printf '%-40s- Makes the xxx-make target in the buildroot build directory\n' 'make buildroot.xxx.make'



#-------------------------------------------------------------------------------
# Target: buildroot.makepackage
# Build a package using buildroot.
# Usage:-
# <start code>
# make buildroot.makepackage package=YOURPACKAGENAME
# <end code>
buildroot.makepackage:
	$(if $(filter undefined,$(origin package)), \
	  @echo "You need to specify a package to build; e.g."; \
	  echo "  make buildroot.makepackage package=directfb"; \
	  exit 1 \
	, \
	  $(buildroot_MAKECMD) $(package)\
	)
buildroot.helpmid:: buildroot.help.makepackage
buildroot.help.makepackage:
	@printf '%-40s- Makes the xxx target int the buildroot build direcotory\n' 'make buildroot.makepackage package=xxx'
	@printf '%-40s  (replaced by buildroot.xxx.make)\n' ''

#-------------------------------------------------------------------------------
buildroot.helpmid::
	@printf '\nBuildroot has been configured to build the following packages:\n'
	$Q$(buildroot_MAKECMD) --no-print-directory '$$(TARGETS).expr'

$(eval $(PKG_END))

include $(PRJROOT)/etc/last.mk
