################################################################################
# \file Makefile
# \version 1.0
#
# \brief
# Makefile for configuring the Cortex-M55 core project
#
################################################################################
# \copyright
# $ Copyright 2025-YEAR Cypress Semiconductor Apache2 $
################################################################################


include ../common.mk

################################################################################
# Basic Configuration
################################################################################

# Name of application (used to derive name of final linked file).
#
# If APPNAME is edited, ensure to update or regenerate launch
# configurations for your IDE.
APPNAME=proj_cm55

# If set to "true" or "1", display full command-lines when building.
VERBOSE=

# Model Name to be loaded to the firmware
NN_MODEL_NAME=TEST_MODEL

# Folder name containing the model and regression data
NN_MODEL_FOLDER=deepcraft

# Choose which cache management strategy for the Ethos-U55
# MTB_ML_ETHOSU_CACHE_MGMT_ALL_LAYERS   -- (default) clear/invalidate cache in all layers
# MTB_ML_ETHOSU_CACHE_MGMT_OUTER_LAYERS -- clear/invalidate cache in the input/output layers only
# MTB_ML_ETHOSU_CACHE_MGMT_CONDITIONAL  -- clear/invalidate cache based on internal state
DEFINES+=MTB_ML_ETHOSU_CACHE_MGMT_TYPE=MTB_ML_ETHOSU_CACHE_MGMT_ALL_LAYERS

################################################################################
# Advanced Configuration
################################################################################

# Enable optional code that is ordinarily disabled by default.
#
# Available components depend on the specific targeted hardware and firmware
# in use. In general, if you have
#
#    COMPONENTS=foo bar
#
# ... then code in directories named COMPONENT_foo and COMPONENT_bar will be
# added to the build
#
COMPONENTS+=

# Like COMPONENTS, but disable optional code that was enabled by default.
DISABLE_COMPONENTS=

CORE=CM55
CORE_NAME=CM55_0
NPU=U55

# By default the build system automatically looks in the Makefile's directory
# tree for source code and builds it. The SOURCES variable can be used to
# manually add source code to the build process from a location not searched
# by default, or otherwise not found by the build system.
SOURCES+=

MODEL_PREFIX=$(subst $\",,$(NN_MODEL_NAME))
CY_IGNORE+=$(NN_MODEL_FOLDER)

# Add additional defines to the build process (without a leading -D).
DEFINES+=CY_RETARGET_IO_CONVERT_LF_TO_CRLF 

# Add additional defines related to ARM Helium and DSP extensions
DEFINES+=ARM_MATH_HELIUM ARM_MATH_DSP ARM_MATH_AUTOVECTORIZE

# Add additional components related to MTB ML
COMPONENTS+=U55

# Like SOURCES, but for include directories. Value should be paths to
# directories (without a leading -I).
INCLUDES+=$(NN_MODEL_FOLDER) $(NN_MODEL_FOLDER)/eval_data

# Add define to build code for CM55 and include additional files
ifeq (cm55, $(ML_DEEPCRAFT_CPU))
DEFINES+=ML_DEEPCRAFT_CM55
DEFINES+=MODEL_FILE=$(NN_MODEL_NAME).h
DEFINES+=VALIDATION_INPUT=$(NN_MODEL_NAME)_data_input.h
DEFINES+=VALIDATION_OUTPUT=$(NN_MODEL_NAME)_data_output.h

# Add the model file based on the inference and data types
SOURCES+=$(wildcard $(NN_MODEL_FOLDER)/$(MODEL_PREFIX).c*)

ifeq (stream, $(ML_DATA_SOURCE))
DEFINES+=USE_STREAM_DATA
COMPONENTS+=ML_MW_STREAM
endif

ifeq (local, $(ML_DATA_SOURCE))
# Add the regression files
SOURCES+=$(wildcard $(NN_MODEL_FOLDER)/eval_data/$(MODEL_PREFIX)_data_input.c*)
SOURCES+=$(wildcard $(NN_MODEL_FOLDER)/eval_data/$(MODEL_PREFIX)_data_output.c*)
endif

# Include the shared source file
SEARCH+=../shared_src/
endif

# Add additional define/components to select the inference engine
COMPONENTS+=ML_TFLM
DEFINES+=TF_LITE_STATIC_MEMORY

# Enable model profiling and store the tensor and weights in socmem
ifeq (cm55, $(ML_DEEPCRAFT_CPU))
DEFINES+=IMAI_PROFILING
DEFINES+=CY_ML_MODEL_MEM=.cy_socmem_data
DEFINES+=CY_ML_ARENA_MEM=.cy_socmem_data
endif

# Select softfp or hardfp floating point. Default is softfp.
VFP_SELECT+=hardfp

# Additional / custom C compiler flags.
#
# NOTE: Includes and defines should use the INCLUDES and DEFINES variable
# above.
CFLAGS+=

# Additional / custom C++ compiler flags.
#
# NOTE: Includes and defines should use the INCLUDES and DEFINES variable
# above.
CXXFLAGS+=

# Additional / custom assembler flags.
#
# NOTE: Includes and defines should use the INCLUDES and DEFINES variable
# above.
ASFLAGS+=

# Additional / custom linker flags.
LDFLAGS+=

# Additional / custom libraries to link in to the application.
LDLIBS+=

# Path to the linker script to use (if empty, use the default linker script).
LINKER_SCRIPT=

# Custom pre-build commands to run.
PREBUILD=

# Custom post-build commands to run.
POSTBUILD=


################################################################################
# Paths
################################################################################

# Relative path to the project directory (default is the Makefile's directory).
#
# This controls where automatic source code discovery looks for code.
CY_APP_PATH=

# Relative path to the shared repo location.
#
# All .mtb files have the format, <URI>#<COMMIT>#<LOCATION>. If the <LOCATION> 
# field begins with $$ASSET_REPO$$, then the repo is deposited in the path 
# specified by the CY_GETLIBS_SHARED_PATH variable. The default location is one 
# directory level above the current app directory. This is used with 
# CY_GETLIBS_SHARED_NAME variable, which specifies the directory name.
CY_GETLIBS_SHARED_PATH=../..

# Directory name of the shared repo location.
#
CY_GETLIBS_SHARED_NAME=mtb_shared

# Absolute path to the compiler's "bin" directory.
#
# The default depends on the selected TOOLCHAIN (GCC_ARM uses the ModusToolbox
# software provided compiler by default).
# CY_COMPILER_PATH?=

include $(CY_TOOLS_DIR)/make/start.mk
