# Copyright (C) 2021 SCARV project <info@scarv.org>
#
# Use of this source code is restricted per the MIT license, a copy of which 
# can be found at https://opensource.org/licenses/MIT (or should be included 
# as LICENSE.txt within the associated archive or repository).

# include build-related content for algorithm
include ${REPO_HOME}/src/${ALG}/Makefile.in

# include build-related content for architecture
#include ${REPO_HOME}/src/share/arch/${ARCH}/Makefile.in

# parameterise source code using environment variables
ifeq "${API}" "aead"
GCC_FLAGS += -DAPI_AEAD
endif

ifeq "${API}" "hash"
GCC_FLAGS += -DAPI_HASH
endif
ifeq "${IMP}" "nist"
GCC_FLAGS += -DALG="\"${ALG}\"" -DAPI="\"${API}\"" ${CONF} -DARCH="\"${ARCH}\"" -DIMP="\"${IMP}\""
else
GCC_FLAGS += -DALG="\"${ALG}\"" -DAPI="\"${API}\"" ${CONF} -DARCH="\"${ARCH}\"" -DIMP="\"${IMP}\"" -DLWISE
endif

# -----------------------------------------------------------------------------

ifeq "${IMP}" "nist"
export LWISE_INCLUDES += ${REPO_HOME}/build/${ALG} ${REPO_HOME}/src/share ${REPO_HOME}/src/share/nist ${REPO_HOME}/src/share/arch/${ARCH}
else
export LWISE_INCLUDES += ${REPO_HOME}/build/${ALG} ${REPO_HOME}/src/share ${REPO_HOME}/src/share/nist ${REPO_HOME}/src/share/arch/${ARCH} ${REPO_HOME}/src/${ALG}/arch/${ARCH} ${REPO_HOME}/src/${ALG}/imp/${IMP} ${REPO_HOME}/src/${ALG}/imp/share
endif

export LWISE_SOURCES  += $(wildcard $(addsuffix /*.c,   ${LWISE_INCLUDES})) 
export LWISE_SOURCES  += $(wildcard $(addsuffix /*.cpp, ${LWISE_INCLUDES})) 
export LWISE_SOURCES  += $(wildcard $(addsuffix /*.s,   ${LWISE_INCLUDES}))
export LWISE_SOURCES  += $(wildcard $(addsuffix /*.S,   ${LWISE_INCLUDES}))
export LWISE_HEADERS  += $(wildcard $(addsuffix /*.h,   ${LWISE_INCLUDES}))

export       ALL_INCLUDES := ${LWISE_INCLUDES} ${NIST_INCLUDES} ${REPO_HOME}/build/${ALG}
INCLUDES += $(foreach dir,$(ALL_INCLUDES),-I$(dir))

export       SOURCES  := ${LWISE_SOURCES}  ${NIST_SOURCES}
export       HEADERS  := ${LWISE_HEADERS}  ${NIST_HEADERS}

# -----------------------------------------------------------------------------

${REPO_HOME}/build/${ALG}       :
	@mkdir --parents ${@}

${SOURCES} : ${REPO_HOME}/build/${ALG}/kat_${API}.c ${REPO_HOME}/build/${ALG}/kat_${API}.h

${REPO_HOME}/build/${ALG}/kat_${API}.h : ${NIST_KAT}
	@python3 ${REPO_HOME}/src/share/kat.py --api="${API}" --header < ${<} > ${@}
${REPO_HOME}/build/${ALG}/kat_${API}.c : ${NIST_KAT}
	@python3 ${REPO_HOME}/src/share/kat.py --api="${API}" --source < ${<} > ${@}
	


C_SRCS += $(filter %.c, ${SOURCES})
CXX_SRCS += $(filter %.cpp, ${SOURCES})
ASM_SRCS += $(filter %.S, ${SOURCES})
 

# -----------------------------------------------------------------------------

.PHONY : run debug scan

dump  :
	@echo "ALG        = ${ALG}"

	@echo "API        = ${API}"
	@echo "ARCH       = ${ARCH}"
	@echo "IMP        = ${IMP}"

	@echo "CONF       = ${CONF}"

	@echo "NIST_HOME  = ${NIST_HOME}"
	@echo "NIST_IMP   = ${NIST_IMP}"
	@echo "NIST_KAT   = ${NIST_KAT}"

	@echo "GCC_PREFIX = ${GCC_PREFIX}"
	@echo "GCC_PATHS  = ${GCC_PATHS}"
	@echo "GCC_FLAGS  = ${GCC_FLAGS}"
	@echo "GCC_LIBS   = ${GCC_LIBS}"

	@echo "INCLUDES   = ${INCLUDES}"
	@echo "SOURCES    = ${SOURCES}"
	@echo "HEADERS    = ${HEADERS}"
	@echo "TARGETS    = ${TARGETS}"

	@echo "ASM_SRCS = ${ASM_SRCS}"

# =============================================================================
