########################################################################################################################
# File Name  : Makefile.wrapper
# Description: This makefile is a wrapper to support DaVinci Cfg5 and Cfg6 folder structures
# Project    : Vector Basic Runtime System
# Module     : vBaseEnv
#              using Vector PES MakeSupport 4.1
#
#-----------------------------------------------------------------------------------------------------------------------
# COPYRIGHT
#-----------------------------------------------------------------------------------------------------------------------
# Copyright (c) 2025 by Vector Informatik GmbH.                                                  All rights reserved.
#
#-----------------------------------------------------------------------------------------------------------------------
# REVISION HISTORY
#-----------------------------------------------------------------------------------------------------------------------
# Version   Date        Author  Change Id      Description
# ----------------------------------------------------------------------------------------------------------------------
# 00.05.00  2025-06-13  visbwa  BASEENV-13947  Initial creation for DaVinci Configurator 6 support of PES MakeSupport
#           2025-06-18  visbwa                 Added support for "custom gendata environment", e.g. used by TscFwHlp
# 00.05.01  2025-06-25  visbwa                 Fixed CFG5_TEMPDATA_DIR (key sensitive!)
# 01.00.00  2025-07-23  visbwa                 Final release; changed most warning to info
# 01.01.00  2025-10-14  visbwa                 Workaround to support CANbedded Make include structure
########################################################################################################################

########################################################################################################################
# EXAMPLE CODE ONLY
#-----------------------------------------------------------------------------------------------------------------------
# This Example Code is only intended for illustrating an example of a possible BSW integration and BSW configuration.
# The Example Code has not passed any quality control measures and may be incomplete. The Example Code is neither
# intended nor qualified for use in series production. The Example Code as well as any of its modifications and/or
# implementations must be tested with diligent care and must comply with all quality requirements which are necessary
# according to the state of the art before their use.
########################################################################################################################

#------------------------------------------------------------------------------
# Path to the Cfg6 GenData and Template folders. They are configurable in General.json.
# The paths are relative to the location of the Makefile.
# The default is set to fit with the Cfg6 default folder structure. It needs to be adapted, if this is changed.
# These values are not used within a Cfg5 environment.
#------------------------------------------------------------------------------
CFG6_GENDATA_DIR = ../Output/Source/GenData
CFG6_TEMPDATA_DIR = ../Output/Source/Templates

#------------------------------------------------------------------------------
# Path to the Cfg5 Template folder. It is configurable in Cfg5 Project Settings.
# The path is relative to the location of the Makefile.
# The default is set to fit with the Cfg5 default folder structure. It needs to be adapted, if this is changed.
# This value is not used within a Cfg6 environment.
# The GenData folder for Cfg5 environments is set within Makefile.project.part.defines.
#------------------------------------------------------------------------------
CFG5_TEMPDATA_DIR = Source

#------------------------------------------------------------------------------
# Try to include the project specific settings from DaVinci Cfg5 folder structure
#
# root
# |- Appl (Makefile, Makefile.config.generated)
#    |- GenData
#    |- Source (LinkerScriptFile)
#------------------------------------------------------------------------------
-include Makefile.config.generated

ifeq ($(BRS_GENERATED_HW_CONFIG_VERSION),)
  $(info BUILDENVIRONMENT: No Cfg5 environment detected)
else
  $(info BUILDENVIRONMENT: Cfg5 environment detected)
  CFG5_ENVIRONMENT_DETECTED = 1
endif

#------------------------------------------------------------------------------
# Try to include the project specific settings from DaVinci Cfg6 folder structure
#
# root
# |- Appl (Makefile)
# |- Output
#    |- Source
#       |- GenData
#          |- Make (Makefile.config.generated)
#       |- Templates (LinkerScriptFile)
#------------------------------------------------------------------------------
-include $(CFG6_GENDATA_DIR)/Make/Makefile.config.generated

ifeq ($(BRS_GENERATED_CFG6_HW_CONFIG_VERSION),)
  $(info BUILDENVIRONMENT: No Cfg6 environment detected)
else
  $(info BUILDENVIRONMENT: Cfg6 environment detected)
  CFG6_ENVIRONMENT_DETECTED = 1
endif

#------------------------------------------------------------------------------
# Workaround for CANbedded TLM to include Os_Core.mak in Os_rules.mak correctly
# because BSW_MAKE_DIR is not yet set in this case.
# (with CANbedded, OS Makefiles are included in MPPD directly and need BSW_MAKE_DIR!)
#------------------------------------------------------------------------------
ifeq ($(BSW_MAKE_DIR),)
  # check for the CANbedded TLM use case using the internal/external structure
  # and set the BSW_MAKE_DIR varaible according to the detected folder structure
  ifneq ("$(wildcard ../../../internal/external/Components/Os/Make)","")
    $(info BUILDENVIRONMENT: CANbedded TLM detected! Workaround for OS Makefile usage in MPPD necessary...)
    $(info BUILDENVIRONMENT: BSW_MAKE_DIR set to Make (component based folder structure))
    BSW_MAKE_DIR = Make
  else ifneq ("$(wildcard ../../../internal/external/BSW/Os/mak)","")
    $(info BUILDENVIRONMENT: CANbedded TLM detected! Workaround for OS Makefile usage in MPPD necessary...)
    $(info BUILDENVIRONMENT: BSW_MAKE_DIR set to mak (legacy folder structure))
    BSW_MAKE_DIR = mak
  endif
endif

#------------------------------------------------------------------------------
# Include of SWCP created MPPD file
#------------------------------------------------------------------------------
include Makefile.project.part.defines

#------------------------------------------------------------------------------
# If neither Cfg5 nor Cfg6 environment was detected, check as fallback case
# if the GENDATA_DIR variable was set in MPPD file and the configured path exists. 
# If it exists, use GENDATA_DIR in the following.
#------------------------------------------------------------------------------
ifeq ($(CFG6_ENVIRONMENT_DETECTED),)
  ifeq ($(CFG5_ENVIRONMENT_DETECTED),)
    ifneq ($(GENDATA_DIR),)
      ifeq ("$(wildcard $(GENDATA_DIR))","")
        $(warning BUILDENVIRONMENT: No gendata environment detected. The path, set in GENDATA_DIR, does not exist: $(GENDATA_DIR))
      else
        $(info BUILDENVIRONMENT: Custom gendata environment detected via MPPD. GENDATA_DIR: $(GENDATA_DIR))
        CUSTOM_GENDATA_ENVIRONMENT_DETECTED = 1
      endif
    else
      $(warning BUILDENVIRONMENT: No GENDATA_DIR variable is set. Not able to detect a custom gendata environment)
    endif
  endif
endif

#------------------------------------------------------------------------------
# Plausibility checks
#------------------------------------------------------------------------------
ifeq ($(CFG6_ENVIRONMENT_DETECTED),)
  ifeq ($(CFG5_ENVIRONMENT_DETECTED),)
    ifeq ($(CUSTOM_GENDATA_ENVIRONMENT_DETECTED),)
      $(error BUILDENVIRONMENT: Neither Cfg5, Cfg6 nor custom environment detected! Check your environment)
    endif
  endif
else
  ifeq ($(CFG5_ENVIRONMENT_DETECTED),1)
   $(error BUILDENVIRONMENT: Both environments detected in parallel, Cfg5 and Cfg6! Check your environment)
  endif
endif

#------------------------------------------------------------------------------
# Static setting of path variables
#------------------------------------------------------------------------------
ifeq ($(CFG5_ENVIRONMENT_DETECTED),1)
  TEMPDATA_DIR = $(CFG5_TEMPDATA_DIR)
  $(info BUILDENVIRONMENT: For Cfg5 environment, GENDATA_DIR is set in Makefile.project.part.defines: $(GENDATA_DIR))
  $(info BUILDENVIRONMENT: TEMPDATA_DIR set to Cfg5 supposed value: $(CFG5_TEMPDATA_DIR))
  $(info BUILDENVIRONMENT: To change this path, edit the following file: Makefile.wrapper)
endif

ifeq ($(CFG6_ENVIRONMENT_DETECTED),1)
  GENDATA_DIR = $(CFG6_GENDATA_DIR)
  TEMPDATA_DIR = $(CFG6_TEMPDATA_DIR)
  ADDITIONAL_INCLUDES += $(TEMPDATA_DIR)
  $(info BUILDENVIRONMENT: GENDATA_DIR set to Cfg6 supposed value: $(CFG6_GENDATA_DIR))
  $(info BUILDENVIRONMENT: TEMPDATA_DIR set to Cfg6 supposed value: $(CFG6_TEMPDATA_DIR) and added to ADDITIONAL_INCLUDES)
  $(info BUILDENVIRONMENT: To change these paths, edit the following file: Makefile.wrapper)
endif

ifeq ($(CUSTOM_GENDATA_ENVIRONMENT_DETECTED), 1)
  # GENDATA_DIR is taken from Makefile.project.part.defines
  # Try to set TEMPDATA_DIR correctly based on subfolder existence in gendata folder
  ifneq ("$(wildcard $(GENDATA_DIR)/../Source)","")
    TEMPDATA_DIR = $(GENDATA_DIR)/../Source
    $(info BUILDENVIRONMENT: TEMPDATA_DIR set to value: $(TEMPDATA_DIR))
    $(info BUILDENVIRONMENT: Custom Cfg5 environment expected. Try to include Makefile.config.generated...)
    include $(GENDATA_DIR)/../Makefile.config.generated
  else ifneq ("$(wildcard $(GENDATA_DIR)/../Templates)","")
    TEMPDATA_DIR = $(GENDATA_DIR)/../Templates
    ADDITIONAL_INCLUDES += $(TEMPDATA_DIR)
    $(info BUILDENVIRONMENT: TEMPDATA_DIR set to value: $(TEMPDATA_DIR) and added to ADDITIONAL_INCLUDES)
    $(info BUILDENVIRONMENT: Custom Cfg6 environment expected. Try to include Makefile.config.generated...)
    include $(GENDATA_DIR)/Make/Makefile.config.generated
  else
    TEMPDATA_DIR = $(GENDATA_DIR)
    $(info BUILDENVIRONMENT: TEMPDATA_DIR set to GENDATA_DIR value: $(TEMPDATA_DIR))
    $(info BUILDENVIRONMENT: Complete custom environment expected. Try to include Makefile.config.generated...)
    include $(GENDATA_DIR)/Makefile.config.generated
  endif

  $(info BUILDENVIRONMENT: To change these paths, edit GENDATA_DIR in Makefile.project.part.defines)
endif

$(info ********************************************************************************)
