# // ******************************************************************************
# //  FileName............: makedefs
# // ------------------------------------------------------------------------------
# //  Definitions common to all makefiles
# //
# //  Optional defines:
# //    DEBUG=1   Compile with debug support
# //    LWIP=1    Compile using LwIp ethernet stack (instead of FreeRTOS-Plus-TCP stack)
# //    OPCUA=1   Compile with OpcUa (only available with LWIP=1)
# //
# //  Version/Date    Comment
# //   2026.04.18     - Initial release
# // ******************************************************************************

#******************************************************************************
#
# Check operating system
#
#******************************************************************************
ifdef ProgramFiles
  os=CYGWIN
else
  os:=${shell uname -s}
endif


#
# Set the prefix for the tools to use.
#
ifeq ($(os), CYGWIN)
  PREFIX:=../../Tools/Compiler/arm-none-eabi-windows/bin/arm-none-eabi
else
  PREFIX:=../../Tools/Compiler/arm-none-eabi-linux/bin/arm-none-eabi
endif

# The commands for calling the compiler/archiver/linker
CC="${PREFIX}-gcc"
AR="${PREFIX}-gcc"
LD="${PREFIX}-gcc"

#
# Set the compiler CPU/FPU options.
#
CPU=-mcpu=cortex-m4
FPU=-mfpu=fpv4-sp-d16 -mfloat-abi=hard

#
# The flags passed to the assembler.
#
AFLAGS=-mthumb \
       ${CPU}  \
       ${FPU}  \
       -MD

#
# The flags passed to the compiler.
#
CFLAGS=-mthumb             \
       ${CPU}              \
       ${FPU}              \
       -ffunction-sections \
       -fdata-sections     \
       -MD                 \
       -std=c99            \
       -Wall               \
       -pedantic           \
       -Wfatal-errors      \
       -DPART_${PART}      \
       -D${ROM}            \
       -c

#
# The flags passed to the linker.
#
LDFLAGS+= -mthumb ${CPU} ${FPU}
LDFLAGS+= -Wl,-Map=${OBJDIR}/_map.map
LDFLAGS+= --specs=nano.specs
LDFLAGS+= --specs=nosys.specs
LDFLAGS+= -Wl,--gc-sections

#
# The command for extracting images from the linked executables.
#
OBJCOPY="${PREFIX}-objcopy"
#
# Tell the compiler to include debugging information if the DEBUG environment
# variable is set.
#
ifeq (${DEBUG}, 1)
CFLAGS+=-g -DDEBUG -Wno-pedantic -Wno-unused-variable
endif

# OpcUa only with LWIP
ifeq (${LWIP}, 1)
else
OPCUA=0
endif

# open62541 flags (mbedtls)
ifeq (${OPCUA}, 1)
OPCUA_FLAGS=
OPCUA_FLAGS+=-DFREERTOS_PLUS_OPCUA
OPCUA_FLAGS+="-DMBEDTLS_CONFIG_FILE=<mbedtls_config.h>"
# Instruct the GNU linker to intercept self-test, seed loops, verification
LDFLAGS += -Wl,--wrap=UA_CertificateVerification_verifyCertificate
LDFLAGS += -Wl,--wrap=UA_CertificateVerification_verifyApplicationURI
endif


LDFLAGS += -Wl,--wrap=malloc
LDFLAGS += -Wl,--wrap=free
LDFLAGS += -Wl,--wrap=realloc
LDFLAGS += -Wl,--wrap=calloc


ifeq (${LWIP}, 1)
CFLAGS+=-DFREERTOS_PLUS_LWIP
ifeq (${OPCUA}, 1)
CFLAGS+=$(OPCUA_FLAGS)
endif
else
CFLAGS+=-DFREERTOS_PLUS_TCP
endif


# Also export as assembly (note that we need to redirect the generated output too)
CFLAGS+=-Og -Wa,-adhln -fverbose-asm

#
# Add the tool specific CFLAGS
#
CFLAGS+=${CFLAGSgcc}

AFLAGS+=-Dgcc
CFLAGS+=-Dgcc

#
# Add the include file paths to AFLAGS and CFLAGS.
#
AFLAGS+=${patsubst %,-I%,${subst :, ,${IPATH}}}
CFLAGS+=${patsubst %,-I%,${subst :, ,${IPATH}}}

#
# The rule for building the object file from each C source file.
#
${OBJDIR}${SUFFIX}/%.o: %.c
ifeq ($(os), CYGWIN)
	@echo.  CC    ${<}
	@mkdir $(subst /,\\,$(patsubst %/,%,$(dir $@))) 2>nul || rem
	@${CC} ${CFLAGS} -o ${@} ${<} > ${@}.s
else
	@echo "  CC    ${<}"
	@mkdir -p $(dir $@)
	@${CC} ${CFLAGS} -o ${@} ${<} > ${@}.s
endif

#
# The rule for building the object file from each assembly source file.
#
${OBJDIR}${SUFFIX}/%.o: %.S
ifeq ($(os), CYGWIN)
	@echo.  AS    ${<}
else
	@echo "  AS    ${<}"
endif
	@${CC} ${AFLAGS} -o ${@} -c ${<}

#
# The rule for creating an object library.
#
${OBJDIR}${SUFFIX}/%.a:
ifeq ($(os), CYGWIN)
	@echo.  AR    ${<}
else
	@echo "  AR    ${<}"
endif
	@${AR} -cr ${@} ${^}


#
# Linking the application.
#
ldname="$(SCATTERgcc_$(notdir $(@:.axf=)))"
LDFLAGS+= -T ${ldname} --entry ${ENTRY_${notdir ${@:.axf=}}}

TARGETDEBUGADDTONAME=
ifeq (${DEBUG}, 1)
  TARGETDEBUGADDTONAME=_debug
endif

TARGETSTACKADDTONAME=
ifeq (${LWIP}, 1)
  TARGETSTACKADDTONAME=_lwip
ifeq (${OPCUA}, 1)
  TARGETSTACKADDTONAME=_lwip_opcua
endif
else
  TARGETSTACKADDTONAME=_plustcp
endif

# Create the resulting name
TARGETFILE="$(notdir $(@:.axf=))_$(VERSION)${TARGETSTACKADDTONAME}${TARGETDEBUGADDTONAME}.lbin"

${OBJDIR}${SUFFIX}/%.axf:
ifeq ($(os), CYGWIN)
	@echo.  LD    ${@} ${LNK_SCP}
else
	@echo "  LD    ${@} ${LNK_SCP}"
#	@echo "Used flags:"
#	@echo "  Compiler: "${CFLAGS}
#	@echo "  Assember: "${AFLAGS}
#	@echo "  Linker  : "${LDFLAGS}
#	@echo "Used libraries:"
#	@echo "  "${shell ${CC} ${CFLAGS} -print-libgcc-file-name}
#	@echo "  "${shell ${CC} ${CFLAGS} -print-file-name=libc.a}
#	@echo "  "${shell ${CC} ${CFLAGS} -print-file-name=libm.a}
	@echo ""
endif
	@${LD} ${LDFLAGS} -o ${@} $(filter %.o %.a, ${^})
ifeq (${DEBUG}, 1)
	@${OBJCOPY} -O binary ${@} ${@:.axf=.lbin}
else
	@${OBJCOPY} -O binary -S ${@} ${@:.axf=.lbin}
endif
ifeq ($(os), CYGWIN)
	@copy /B "$(OBJDIR)\$(notdir $(@:.axf=.lbin))" .\bin\"${TARGETFILE}" >nul
else
	@cp -f "${@:.axf=.lbin}" ./bin/"${TARGETFILE}"
endif
	@echo Created ${TARGETFILE}
