SHELL=sh.exe
# SHELL=cmd.exe 

RM = rm -rf
# CP = COPY /Y 
RM = CMD /C DEL /F
RMDIR = CMD /C RD /S /Q
RMALL = CMD /C DEL /F /S
ECHO=echo
PYTHON=Python.exe
DOXYGEN = Doxygen.exe
AISGEN = AISgen_d800k008.exe

# com port is set in environment setting PGM_DEBUG_PORT in My Computer properties->Advanced Settings->Environment variables
DEBUG_PORT=$(PGM_DEBUG_PORT)


# libraries needs to be built first
DEPENDENCIES=libStarterWare
LIBRARIES = libIniFile libYAFFS
UNITTESTING = UnitTest
BOOTLOADER = Bootloader


LIBRARIES_EXT = ${LIBRARIES:%=%.lib}
UNITTESTING_EXT = ${UNITTESTING:%=%.out}
BOOTLOADER_EXT = ${BOOTLOADER:%=%.out}


UNITTESTING_DIR = ${UNITTESTING:%=%/$(TARGET)/}
LIBRARIES_DIR = ${LIBRARIES:%=%/$(TARGET)/}
DEPENDENCIES_DIR = ${DEPENDENCIES:%=%/$(TARGET)/}
BOOTLOADER_DIR = ${BOOTLOADER:%=%/$(TARGET)/}

UNITTESTING_TARGET=$(join $(UNITTESTING_DIR), $(UNITTESTING_EXT))
LIBRARIES_TARGET = $(join $(LIBRARIES_DIR), $(LIBRARIES_EXT))
BOOTLOADER_TARGET = $(join $(BOOTLOADER_DIR), $(BOOTLOADER_EXT))


TEST_TARGETS = $(UNITTESTING_TARGET)
TEST_TARGETS += $(LIBRARIES_TARGET)
TEST_TARGETS += $(BOOTLOADER_TARGET)

TARGET=Debug
BUILDENV = /ti/ccsv6/eclipse/eclipsec.exe -noSplash
WORKSPACE="ccsWorkspace"


UNITTESTING_PROJ=${UNITTESTING:%=$(realpath %)}
LIBRARIES_PROJ=${LIBRARIES:%=$(realpath %)}
DEPENDENCIES_PROJ=${DEPENDENCIES:%=$(realpath %)}
BOOTLOADER_PROJ=${BOOTLOADER:%=$(realpath %)}

	


# unit testing related parameters
CAPTURE_SERIAL=$(PYTHON) CaptureSerial.py

BAUDRATE=115200
TIMEOUT=3000
PATTERN="(<\?xml.*\?>)(.*<unittest-results)(.*</unittest-results>)"
REPL="\g<1>\n<?xml-stylesheet type='text/xsl' href='unittest.xsl'?>\g<2> title='$(DATE)'\g<3>"
OUTFILE=unittest-results.xml
CMD="$(PYTHON) UnitTestingResult.py --f $(TARGET)/$(UNITTESTING_EXT) --tilog ../log.xml"

FAILPATTERN="(.*<failure)"

# flint checker verifies for MISRA-CPP compliance. you need to provide the path to flint.py in case is not in projects folder
RUN_FLINT_CHECK=$(PYTHON) ../flint/FlintChecker.py
FLINT_SURPRESS_PATH = ../flint/

WATCHDOG=1
PGM_MAJOR=0
PGM_MINOR=5
PGM_PATCH=0
BOOT_MAJOR=0
BOOT_MINOR=1
BOOT_PATCH=2

    
PGMVERSION_=$(PGM_MAJOR).$(PGM_MINOR).$(PGM_PATCH)
BOOTVERSION_=$(BOOT_MAJOR).$(BOOT_MINOR).$(BOOT_PATCH)


.PHONY: all
all: clean ver build lint tests firmware

.PHONY: ver
ver: 	
	cd $(BOOTLOADER_PROJ); export BOOTVERSION=$(BOOTVERSION_); ./bootsvnget.bat $(BOOTLOADER_PROJ)


.PHONY: build
build: ver
	$(call importProjects, $(LIBRARIES_PROJ))
	export WATCHDOG_ENABLED=$(WATCHDOG); $(BUILDENV) -data $(WORKSPACE) -application com.ti.ccstudio.apps.projectBuild -ccs.buildType full -ccs.projects $(LIBRARIES)
	$(BUILDENV) -data $(WORKSPACE) -application com.ti.ccstudio.apps.projectBuild -ccs.buildType full -ccs.projects $(LIBRARIES)

	$(call checking, $(TEST_TARGETS))	
	
.PHONY: clean
clean:
# rm doesn't work, but assuming build folder is empty so not needed
#	$(RM) flint/flint_suppress.log 
#	$(RM) flint/flint_errors.html

	$(call importProjects, $(LIBRARIES_PROJ) )
	$(BUILDENV) -data $(WORKSPACE) -application com.ti.ccstudio.apps.projectBuild -ccs.buildType clean -ccs.projects $(LIBRARIES) 
	
	
.PHONY: lint
lint: 
	$(call checking_lint, $(LIBRARIES_PROJ))

	
.PHONY: tests
tests:
# the following script blocks forever until output is received from the board. timeout of 30 is added in script.
	$(PYTHON) ResetBoard.py
	cd $(UNITTESTING); $(CAPTURE_SERIAL) --port $(DEBUG_PORT) --baudrate $(BAUDRATE) --timeout $(TIMEOUT) --pattern $(PATTERN) --repl $(REPL) --outfile $(OUTFILE) --cmd $(CMD) --failPattern $(FAILPATTERN)

	
	
.PHONY: robottest	
robottest: rtest routputparse

rtest:
	$(PYTHON) ResetBoard.py
	$(PYTHON) PGM_sysbios/JtagFirmwareLoader.py --f PGM_sysbios/$(TARGET)/PGM_sysbios.out --tilog Robot/log.xml
	pybot.bat -d ".\Robot\FW\Run" ".\Robot" 
	
routputparse: 
	$(PYTHON) "./Robot/FW/Run/outputParser.py"
	
firmware:
	export BOOTVERSION=$(BOOTVERSION_); export PGMVERSION=$(PGMVERSION_); cd FlashingTools; ./GenerateFirmware.bat ftp


	
.PHONY : docs
docs: 
	$(call checking_docs, $(LIBRARIES_PROJ))



	
# at the moment CCSv5.1 doesn't set errorlevel so we have to check if the file was actually created. will be fixed in CCSv5.2
# function checking receives parameter the list of files to check

importProjects=@set -e; if [ -d "$(WORKSPACE)" ]; then rm -rf $(WORKSPACE); fi; \
	for i in $(1); do ($(ECHO) "Importing $$i"; \
	($(BUILDENV) -data $(WORKSPACE) -application com.ti.ccstudio.apps.projectImport -ccs.location "$$i"); \
	); done



checking = @set -e; for i in $(1); do ($(ECHO) "Checking $$i"; \
		if [ -f "$$i" ]; then \
			$(ECHO) "Found $$i"; \
		else \
			echo "Exiting with error. $$i not found."; \
			exit 1; \
		fi \
	); done

	

checking_lint = @set -e; for i in $(1); do (echo "Lint $$i"; \
		cd "$$i"; $(RUN_FLINT_CHECK) --flint $(FLINT_SURPRESS_PATH) --path "$$i"; \
	); done	

checking_docs = @set -e; for i in $(1); do (echo "Doxygen $$i"; \
				if [ -f "$$i/Doxyfile" ]; then \
					cd "$$i"; $(DOXYGEN) Doxyfile; \
				fi \
	); done	
	
	
# LINT_SOURCES = $(wildcard *.cpp) $(wildcard */*.cpp) $(wildcard *.h) $(wildcard */*.h) ; $(ECHO) $(LINT_SOURCES); \