#
#  Copyright (c) 2021 Texas Instruments Incorporated - http://www.ti.com
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#
#  *  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
#  *  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  *  Neither the name of Texas Instruments Incorporated nor the names of
#     its contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
#  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

#
#  ======== Makefile ========
#

EXBASE = ./
include $(EXBASE)/products.mak

.PHONY: all debug release

TARGET = sddf_drv

SOC_VER = SOC_AM64X
PROC_VER = 7R5
VFP_VER = vfpv3d16
OBJEXT = obj

# Source directories
SRC_DIR                     = src

# Source files
SRCS                        = $(wildcard $(SRC_DIR)/*.c)

# Object & dependency file directories
OBJ_DIR                     = bin/$(PROFILE)/obj
OBJ_DIRS = $(OBJ_DIR)

# Object files
OBJS                        = $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.$(OBJEXT),$(notdir $(SRCS))))

# Include generated dependency files
-include $(patsubst %.$(OBJEXT),%.$(OBJEXT).dep,$(OBJS))

.PRECIOUS: %/compiler.opt %/linker.cmd

all: release
  
    
#  ======== rule for debug build configuration ========
debug:
	$(MAKE) PROFILE=debug $(TARGET).x

#  ======== rule for release build configuration ========
release:
	$(MAKE) PROFILE=release $(TARGET).x

    
#  ======== rule for archiver ========
$(TARGET).x: bin/$(PROFILE)/$(TARGET).lib
bin/$(PROFILE)/$(TARGET).lib: $(OBJS)
	@$(ECHO) "#"
	@$(ECHO) "# Making $@ ..."
	$(AR) $(ARFLAGS) $@ $^
    
#
#  ======== rules for compilations of source files ========
#
$(OBJ_DIR)/%.$(OBJEXT): $(SRC_DIR)/%.c | $(OBJ_DIR)
	@$(ECHO) "#"
	@$(ECHO) "# Making $< $@ ..."
	$(CC) $(CPPFLAGS) $(CFLAGS) --output_file=$@ -fc $<

#  ======== rule for creation of output directories ========
$(OBJ_DIRS):
	@echo "mkdir $@"
	$(shell $(MKDIR) -p $@)

install:
	@$(ECHO) "#"
	@$(ECHO) "# Making $@ ..."
	@$(MKDIR) $(EXEC_DIR)/debug
	$(CP) bin/debug/$(TARGET).lib $(EXEC_DIR)/debug
	@$(MKDIR) $(EXEC_DIR)/release
	$(CP) bin/release/$(TARGET).lib $(EXEC_DIR)/release

help:
	@$(ECHO) "make                   # build executable"
	@$(ECHO) "make clean             # clean everything"

clean::
	$(RMDIR) bin

    
#  ======== install validation ========
ifeq (install,$(MAKECMDGOALS))
ifeq (,$(EXEC_DIR))
$(error must specify EXEC_DIR)
endif
endif

#  ======== tool chain macros ========
CGTOOLS = $(ti_targets_elf_R5F)

CC = $(CGTOOLS)/bin/armcl
AR = $(CGTOOLS)/bin/armar

# compiler flags
CPPFLAGS =
#CFLAGS = -k -mv$(PROC_VER) --code_state=32 --float_support=$(VFP_VER) --fp_mode=strict --vectorize=off --little_endian --enum_type=packed -diag_warning=225 --display_error_number -ppd=$@.dep -ppa $(CPREDEFS) $(CCPROFILE_$(PROFILE)) $(CINCDIRS)
CFLAGS = -mv$(PROC_VER) --code_state=32 --float_support=$(VFP_VER) --fp_mode=strict --vectorize=off --little_endian --enum_type=packed -diag_warning=225 --display_error_number -ppd=$@.dep -ppa $(CPREDEFS) $(CCPROFILE_$(PROFILE)) $(CINCDIRS)

CPREDEFS = --define=ARM_MATH_MATRIX_CHECK --define=ARM_MATH_ROUNDING --define=__FPU_PRESENT=1 --define=ARM_MATH_CM4 --define=$(SOC_VER)

PDK_ROOT_DIR = $(PDK_INSTALL_DIR)/packages

CINCDIRS = -I"$(CGTOOLS)/include" \
    -I"$(PDK_ROOT_DIR)" \
    -I"../firmware" \
    -I"./include" \
    -I"./src"
    
CCPROFILE_debug = --symdebug:dwarf
CCPROFILE_release = -O3 --opt_for_speed=5

# archiver flags
ARFLAGS = r

#  ======== standard macros ========
ifneq (,$(wildcard $(XDC_INSTALL_DIR)/bin/echo.exe))
    # use these on Windows
    CP      = $(XDC_INSTALL_DIR)/bin/cp
    ECHO    = $(XDC_INSTALL_DIR)/bin/echo
    MKDIR   = $(XDC_INSTALL_DIR)/bin/mkdir -p
    RM      = $(XDC_INSTALL_DIR)/bin/rm -f
    RMDIR   = $(XDC_INSTALL_DIR)/bin/rm -rf
else
    # use these on Linux
    CP      = cp
    ECHO    = echo
    MKDIR   = mkdir -p
    RM      = rm -f
    RMDIR   = rm -rf
endif
    
#  ======== debug, rule to show build variables ========      
.show_build_vars:
	@echo Profile:
	@echo $(PROFILE)
	@echo #
	@echo Source file directory:
	@echo $(SRC_DIR)
	@echo #
	@echo Object file directory:
	@echo $(OBJ_DIR)
	@echo #
	@echo Source files
	@echo $(SRCS)
	@echo #
	@echo Object files
	@echo $(OBJS)
	@echo #
	@echo Dependency files
	@echo $(patsubst %.$(OBJEXT),%.$(OBJEXT).dep,$(OBJS))
