RGSRC= ../
include $(RGSRC)/envir.mak

ifndef CONFIG_RG_OS
  $(error CONFIG_RG_OS not defined!)
endif

ARCHCONFIG_FIRST_TASKS=create_links rg_config

# Override the define in envir.mak We need the link NOT the real dir.
KERNEL_DIR=$(PWD)/kernel

SUBDIRS=$(KERNEL_DIR)

CLEAN+=$(wildcard $(STATIC_MOD_DIR)/*) $(RGSRC)/$(notdir $(OPENRG_IMG)) $(RGSRC)/$(notdir $(OPENRG_PROD)) layout.bin layout.sec

ifndef CONFIG_RG_OS_VXWORKS
  DISTCLEAN+=$(KERNEL_DIR)/$(AUTOCONF_H)
endif

DISTCLEAN+=$(OPENRG_IMG) $(OPENRG_PROD) $(KERNEL_DIR) $(RG_CONFIG_H)

OTHER_TASKS+=$(OPENRG_IMG) $(RGSRC)/$(notdir $(OPENRG_IMG))

ifeq ($(CONFIG_RG_PROD_IMG),y)
  OTHER_TASKS+=$(OPENRG_PROD) $(RGSRC)/$(notdir $(OPENRG_PROD))
endif

ifdef CONFIG_RG_OS_LINUX
DISTCLEAN+=linux/.config linux 
# Delete of subdirs should be before the normal DISTCLEAN because we can't 
# access 'kernel' and 'linux' softlinks after it.
DISTCLEAN_FIRST_TASK=del_subdirs

CD_EXPORTED_SUBDIRS=$(call GET_REAL_PATH,linux)
CD_EXPORTED_FILES=Rules_common.make
# We need the `real` linux directory and not the soft link.
CD_EXPORTED_DIR_CD_EXPORTED_KERNEL_FULL:=$(shell cd linux && /bin/pwd)
endif

ifeq ($(CONFIG_RG_OS),LINUX_22)
  DISTCLEAN+=linux-2.2
endif

ifeq ($(CONFIG_RG_OS),ECOS)
  SUBDIRS=ecos
  ARCHCONFIG_SUBDIRS=$(SUBDIRS)
  DISTCLEAN_SUBDIRS=$(SUBDIRS)
  CD_EXPORTED_SUBDIRS=$(SUBDIRS)
endif

ifdef CONFIG_RG_FACTORY_SETTINGS
  FACTORY_SETTINGS=$(RGSRC)/pkg/main/rg_factory
endif

ifneq ($(CONFIG_RG_OS),VXWORKS)
  ifneq ($(MAKECMDGOALS),archconfig)
# Adding explicit all: I have to include the boot Makefile before rg.mak for
# distribution purposes but I want the all: the be the first rule. (Ugly? yes,
# working? don't touch)
all:

    include Makefile.vmlinuz
  endif

  CD_EXPORTED_FILES+=Makefile.vmlinuz
endif

include $(RGMK)

$(call RG_DEP,$(OPENRG_IMG),dummy)

$(RGSRC)/$(notdir $(OPENRG_IMG)): $(notdir $(OPENRG_IMG))
	@$(call RG_LN_FUNC,$(notdir $(OPENRG_IMG)),$@)

ifeq ($(CONFIG_RG_PROD_IMG),y)
  ifdef BOOTSTRAP
    BOOTSTRAP_CMD_PARAM=-b $(BOOTSTRAP)
  endif
  ifdef FACTORY_SETTINGS
    FACTORY_SETTINGS_CMD_PARAM=-f $(FACTORY_SETTINGS)
  endif
  ifdef CONFIG_RG_DYN_FLASH_LAYOUT
    FLASH_LAYOUT_CMD_PARAM=-l layout.bin
  endif
$(OPENRG_PROD): $(OPENRG_IMG) $(BOOTSTRAP) $(FACTORY_SETTINGS)
ifdef CONFIG_RG_DYN_FLASH_LAYOUT
	$(RGSRC)/pkg/perm_storage/layout2bin
endif
	$(RGSRC)/pkg/perm_storage/build_flash $(BOOTSTRAP_CMD_PARAM) $(FACTORY_SETTINGS_CMD_PARAM) $(FLASH_LAYOUT_CMD_PARAM) -i $(OPENRG_IMG) -o $@ -p $(FLASH_IMG_OFFSET)

$(RGSRC)/$(notdir $(OPENRG_PROD)): $(notdir $(OPENRG_PROD))
	@$(call RG_LN_FUNC,$(notdir $(OPENRG_PROD)),$@)
endif

modules:
	make -C $(KERNEL_DIR) $@

create_links:
ifdef CONFIG_RG_OS_LINUX
  ifeq ($(CONFIG_RG_OS),LINUX_22)
	$(RG_LN) $(RGSRC)/linux $(PWD)/linux-2.2
	$(RG_LN) $(PWD)/linux-2.2 $(PWD)/linux
  endif
  ifeq ($(CONFIG_RG_OS),LINUX_24)
	$(RG_LN) $(PWD)/linux-2.4 $(PWD)/linux
  endif
	@$(RG_LN) $(PWD)/linux $(KERNEL_DIR)
	@$(MKDIR) $(STATIC_MOD_DIR)
else
ifeq ($(CONFIG_RG_OS),VXWORKS)
	@$(RG_LN) $(PWD)/vx $(KERNEL_DIR)
endif
endif

RG_CONFIG_H=$(RGSRC)/pkg/include/rg_config.h

rg_config:
	@echo configuration set to $(PROD)
	@echo "/* OpenRG: this file was generated automatically from .config by make archconfig */" > $(RG_CONFIG_H)
	@echo "#define AUTOCONF_INCLUDED" >> $(RG_CONFIG_H)
	@$(RGSRC)/pkg/util/mkconfig $(CONFIG_FILE) >> $(RG_CONFIG_H)
ifdef CONFIG_RG_OS_LINUX
	@$(RG_LN) $(RG_CONFIG_H) $(KERNEL_DIR)/$(AUTOCONF_H)
endif

ifdef CONFIG_RG_OS_LINUX
del_subdirs:
	rm -rf $(STATIC_MOD_DIR)
endif

.PHONY: create_links modules rg_config del_subdirs
