#
# kbuild file for usr/ - including initramfs image
#

klibcdirs:;
PHONY += klibcdirs


# Generate builtin.o based on initramfs_data.o
obj-$(CONFIG_BLK_DEV_INITRD) := initramfs_data.o

# initramfs_data.o contains the initramfs_data.cpio.gz image.
# The image is included using .incbin, a dependency which is not
# tracked automatically.
$(obj)/initramfs_data.o: $(obj)/initramfs_data.cpio.gz FORCE

#####
# Generate the initramfs cpio archive

hostprogs-y := gen_init_cpio
initramfs   := $(CONFIG_SHELL) $(srctree)/scripts/gen_initramfs_list.sh
ramfs-input := $(if $(filter-out "",$(CONFIG_INITRAMFS_SOURCE)), \
			$(shell echo $(CONFIG_INITRAMFS_SOURCE)),-d)
ramfs-args  := \
        $(if $(CONFIG_INITRAMFS_ROOT_UID), -u $(CONFIG_INITRAMFS_ROOT_UID)) \
        $(if $(CONFIG_INITRAMFS_ROOT_GID), -g $(CONFIG_INITRAMFS_ROOT_GID))

# .initramfs_data.cpio.gz.d is used to identify all files included
# in initramfs and to detect if any files are added/removed.
# Removed files are identified by directory timestamp being updated
# The dependency list is generated by gen_initramfs.sh -l
ifneq ($(wildcard $(obj)/.initramfs_data.cpio.gz.d),)
	include $(obj)/.initramfs_data.cpio.gz.d
endif

# The $(shell echo $(CONFIG_INITRAMFS_SOURCE)) is to remove the
# gratuitous begin and end quotes from the Kconfig string type.
# Internal, escaped quotes in the Kconfig string will loose the
# escape and become active quotes.
quotefixed_initramfs_source := $(shell echo $(CONFIG_INITRAMFS_SOURCE))

ifneq '$(quotefixed_initramfs_source)' '' # if the INITRAMFS option is set
ifeq '$(wildcard $(quotefixed_initramfs_source))' '' # but it does not exist in the file system
$(warning                                                                    )
$(warning Error: cannot find the initramfs source $(CONFIG_INITRAMFS_SOURCE) )
$(warning If you are using integration with the Sigma Designs MIPSutils      )
$(warning rootfs package, please make sure you have sourced 'rootfs-path.env')
$(warning in the rootfs package dir, or that you have adequately defined the )
$(warning 'SMP86XX_ROOTFS_PATH' environment variable.                        )
$(warning                                                                    )
$(error   Error: $(CONFIG_INITRAMFS_SOURCE) not found.)
endif
endif
 
is_ramfs_input_dir := $(shell if [ -d $(ramfs-input) ]; then echo yes; fi)
ifeq '$(is_ramfs_input_dir)' 'yes'
	initramfs_extra_list := usr/initramfs_default_node_list
endif

quiet_cmd_initfs = GEN     $@
      cmd_initfs = $(initramfs) -o $@ $(ramfs-args) $(ramfs-input) $(initramfs_extra_list)

targets := initramfs_data.cpio.gz
# do not try to update files included in initramfs
$(deps_initramfs): ;

$(deps_initramfs): klibcdirs
# We rebuild initramfs_data.cpio.gz if:
# 1) Any included file is newer then initramfs_data.cpio.gz
# 2) There are changes in which files are included (added or deleted)
# 3) If gen_init_cpio are newer than initramfs_data.cpio.gz
# 4) arguments to gen_initramfs.sh changes
$(obj)/initramfs_data.cpio.gz: $(obj)/gen_init_cpio $(deps_initramfs) klibcdirs
	$(Q)$(initramfs) -l $(ramfs-input) > $(obj)/.initramfs_data.cpio.gz.d
	$(call if_changed,initfs)

is_gawk_installed := $(shell if gawk --version > /dev/null 2>& 1; then echo yes; fi)
ifneq '$(is_gawk_installed)' 'yes'
$(warning The 'gawk' utility is not installed on your system\; however it is a )
$(warning pre-requisite of the SMP86xx dev kit. Please install it before       )
$(warning proceeding.                                                          )
$(warning                                                                      )
$(warning Depending on the specifics of your Linux distribution, you can       )
$(warning consider the following ways of installing 'gawk' onto your system:   )
$(warning  - use a package manager, such as apt-get (apt-get install gawk) or  )
$(warning    yum (yum install gawk).                                           )
$(warning  - google for the package in rpm form ('gawk rpm download').         )
$(warning                                                                      )
$(error Error: 'gawk' not installed.)
endif
