################################################################################
# \file Makefile
# \version 1.0
#
# \brief
# Makefile for configuring the Cortex-M33 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_cm33_ns

# 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 the inference engine
# tflm      -- TensorFlow Lite for Microcontrollers inference engine with interpreter
# tflm_less -- TensorFlow Lite for Microcontrollers inference engine interpreter-less
NN_INFERENCE_ENGINE=tflm

# Choose to enable NNLITE NPU - yes or no
NN_NPU_ENABLE=yes

################################################################################
# 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=CM33
CORE_NAME=CM33_0

# 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+=

# Select only the regression and model files that belong to the desired
# settings. 
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

# Check if should use NNLITE
ifeq (no, $(NN_NPU_ENABLE))
COMPONENTS+=ML_CM33_ONLY
else
COMPONENTS+=NNLITE2
endif

# 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 CM33 and include additional files
ifeq (cm33, $(ML_DEEPCRAFT_CPU))
DEFINES+=ML_DEEPCRAFT_CM33
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 (tflm_less, $(NN_INFERENCE_ENGINE))
SOURCES+=$(wildcard $(NN_MODEL_FOLDER)/$(MODEL_PREFIX)_tflm_less_model.c*)
endif

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
ifeq (tflm, $(NN_INFERENCE_ENGINE))
COMPONENTS+=ML_TFLM
DEFINES+=TF_LITE_STATIC_MEMORY
endif

ifeq (tflm_less, $(NN_INFERENCE_ENGINE))
COMPONENTS+=ML_TFLM_LESS
DEFINES+=TF_LITE_STATIC_MEMORY TF_LITE_MICRO_USE_OFFLINE_OP_USER_DATA TF_LITE_STRIP_ERROR_STRINGS
endif


# Enable model profiling and store the tensor and weights in socmem
ifeq (cm33, $(ML_DEEPCRAFT_CPU))
DEFINES+=IMAI_PROFILING
DEFINES+=CY_ML_MODEL_MEM=.cy_sram_code
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
# IDE provided compiler by default).
# CY_COMPILER_PATH?=

include $(CY_TOOLS_DIR)/make/start.mk
