.PHONY: clean build_all MakeTmpDirectories CreateAndCleanBinDirectory CleanTmpDirectory PrintCompilerVersion PrintIncludePaths

# ------------------------------------------------------------------------------
# Begin Expert build area using TYP_RULE: Watch out!
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Consider the final Build.bat parameter TYP_RULE.
# The build supports a feature for the kind of rebuild (if any).
# There are several possible kinds of rebuild. The most common
# kind of rebuild is TYP_REBUILD=NONE. For example, the regular
# build command that is used frequently at the developer's desk
# is not a rebuld and it has TYP_REBUILD=NONE.
# When a developer performs a full rebuild at the developer desk,
# the rebuild type is TYP_REBUILD=REBUILD. Also the rebuild command
# on the build server has TYP_REBUILD=REBUILD.
# Special builds such as deliveries on the server or for integrators
# wil occasionally use TYP_REBUILD=REBUILD_DELIVERY. This is activated
# by setting the final Build.bat parameter (TYP_RULE) to
# TYP_RULE=rebuild_delivery.
# ------------------------------------------------------------------------------
TYP_REBUILD   = NONE

ifeq ($(TYP_RULE),rebuild)
TYP_REBUILD  := REBUILD
endif

ifeq ($(TYP_RULE),rebuild_delivery)
TYP_REBUILD  := REBUILD_DELIVERY
endif

# ------------------------------------------------------------------------------
# End Expert build area using TYP_RULE
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Get $(TYP_NAME) from Make.bat originally from Build.bat.
# Here, $(TYP_NAME) is something like F0Bw16_App_default.
# ------------------------------------------------------------------------------
TYP_NAME     := $(TYP_NAME)


# ------------------------------------------------------------------------------
# See also Make_005_UserSettings.gmk
# Set the default MISRA type.
# Note that TYP_TEST and TYP_COVER are supplied directly in command line parameters to build.bat.
# TYP_MISRA can (in the future) be over-written with user-specific build options in Make_005_UserSettings.gmk
# ------------------------------------------------------------------------------
TYP_MISRA = QAC

# ------------------------------------------------------------------------------
# See also Make_005_UserSettings.gmk
# Set the default MISRA output format to "sql", which will be reset in the UserSettings file.
# This way the user will get the textual output, and as a consequence,
# if the UserSettings file is deleted on the server, then the format will be "sql".
# ------------------------------------------------------------------------------
MISRA_OUTPUT_SQL_FORMAT = SQL

# ------------------------------------------------------------------------------
# Punctuation
# ------------------------------------------------------------------------------
SPACE :=
SPACE +=

DOLLAR = $$
DQUOTE = \"
DQ     = "

COMMA  = ,

# ------------------------------------------------------------------------------
# Additional included makefiles
# ------------------------------------------------------------------------------
-include $(CURDIR)/../Make/Make_005_UserSettings.gmk
include $(CURDIR)/../Make/Make_010_Paths.gmk
include $(CURDIR)/../Make/Make_020_Tools.gmk
include $(CURDIR)/../Make/Make_030_Flags.gmk
include $(CURDIR)/../Make/Make_050_Files.gmk
include $(CURDIR)/../Make/Make_070_Link.gmk
include $(CURDIR)/../Make/Make_100_HexManip.gmk
include $(CURDIR)/../Make/Make_110_PCLint.gmk
include $(CURDIR)/../Make/Make_120_Misra.gmk
include $(CURDIR)/../Make/Make_130_Cover.gmk
include $(CURDIR)/../Make/Make_150_Sizes.gmk
include $(CURDIR)/../Make/Make_180_PreBuild.gmk
include $(CURDIR)/../Make/Make_200_PostBuild.gmk
include $(CURDIR)/../Make/Make_500_Rules.gmk

# ------------------------------------------------------------------------------
# Helper files used in the build
# ------------------------------------------------------------------------------
FILE_BIN_NAME      = $(PATH_BIN_NAME)/$(TYP_NAME)

INCLUDE_PATHS_FILE    = $(PATH_TMP_NAME_OBJ)/_compile_paths.txt
INCLUDE_PATHS_FILE_VS = $(PATH_TMP_NAME_OBJ)/_compile_paths_vs.txt
LINK_OBJECTS_FILE     = $(PATH_TMP_NAME_OBJ)/_object_files.txt

# ------------------------------------------------------------------------------
# Basic Rules
# ------------------------------------------------------------------------------
MakeTmpDirectories:
	@$(GNUECHO) +++ making tmp directories
	@if not exist $(subst /,\,$(PATH_TMP_BASE))      $(MKDIR) $(subst /,\,$(PATH_TMP_BASE))
	@if not exist $(subst /,\,$(PATH_TMP_NAME))      $(MKDIR) $(subst /,\,$(PATH_TMP_NAME))
	@if not exist $(subst /,\,$(PATH_TMP_NAME_ERR))  $(MKDIR) $(subst /,\,$(PATH_TMP_NAME_ERR))
	@if not exist $(subst /,\,$(PATH_TMP_NAME_OBJ))  $(MKDIR) $(subst /,\,$(PATH_TMP_NAME_OBJ))
	@if not exist $(subst /,\,$(PATH_TMP_NAME_LST))  $(MKDIR) $(subst /,\,$(PATH_TMP_NAME_LST))
	@if not exist $(subst /,\,$(PATH_TMP_NAME_QAC))  $(MKDIR) $(subst /,\,$(PATH_TMP_NAME_QAC))

CreateAndCleanBinDirectory:
	@$(GNUECHO) +++ cleaning bin directory
	@if not exist $(subst /,\,$(PATH_BIN_BASE))      $(MKDIR) $(subst /,\,$(PATH_BIN_BASE))
	@if not exist $(subst /,\,$(PATH_BIN_NAME))      $(MKDIR) $(subst /,\,$(PATH_BIN_NAME))
	@if exist $(subst /,\,$(PATH_BIN_NAME)/*.*) $(QDEL) $(subst /,\,$(PATH_BIN_NAME)/*.*)  > NUL  2> NUL


CleanTmpDirectory:
	@$(GNUECHO) +++ cleaning tmp directories
	@$(GNUECHO)
	@-$(QDEL) $(subst /,\,$(PATH_TMP_NAME_ERR)/*.*) > NUL  2> NUL
	@-$(QDEL) $(subst /,\,$(PATH_TMP_NAME_LST)/*.*) > NUL  2> NUL
	@-$(QDEL) $(subst /,\,$(PATH_TMP_NAME_OBJ)/*.*) > NUL  2> NUL
	@-$(QDEL) $(subst /,\,$(PATH_TMP_NAME_QAC)/*.*) > NUL  2> NUL

PrintRebuildType:
	@$(GNUECHO)
	@$(GNUECHO) +++ print rebuild type +++
	@$(GNUECHO) TYP_REBUILD $(TYP_REBUILD)
	@$(GNUECHO)

PrintCompilerVersion:
	@$(GNUECHO)
	@$(GNUECHO) +++ print GNUmake version +++
	@$(MAKE) -v
	@$(GNUECHO)
	@$(GNUECHO) +++ print compiler version +++
	@$(RULE_SHOW_COMPILER_VERSION_CMD)
	@$(GNUECHO) $(CC)
	@$(GNUECHO)

PrintIncludePaths:
	@$(GNUECHO) +++ print compiler include paths (for VisualStudio browsing) +++
	@$(GNUECHO) > $(INCLUDE_PATHS_FILE_VS)
	@FOR %%A IN ($(subst $(CURDIR)/,$(DOLLAR)^(SolutionDir^),$(C_INCLUDES))) DO echo|set /p DummyText=%%A;>>$(subst /,\,$(INCLUDE_PATHS_FILE_VS))
	@$(GNUECHO) >> $(INCLUDE_PATHS_FILE_VS)
	@$(CAT) $(INCLUDE_PATHS_FILE_VS)
	@$(GNUECHO)
	@$(GNUECHO) +++ creating compiler include path file $(subst \,/,$(INCLUDE_PATHS_FILE)) +++
	@$(GNUECHO)
	@$(GNUECHO) > $(INCLUDE_PATHS_FILE)
	@FOR %%A IN ($(subst \,/,$(C_INC_PARAM))) DO echo %%A>>$(subst /,\,$(INCLUDE_PATHS_FILE))
	@$(GNUECHO)
	@$(GNUECHO) +++ print compiler include paths (as used by the compiler) +++
	@$(CAT) $(INCLUDE_PATHS_FILE)
	@$(GNUECHO)
	@$(GNUECHO) +++ creating objects file $(subst \,/,$(LINK_OBJECTS_FILE)) +++
	@$(GNUECHO) > $(LINK_OBJECTS_FILE)
	@FOR %%A IN ($(subst \,/,$(FILES_TMP_OBJ))) DO echo %%A>>$(subst /,\,$(LINK_OBJECTS_FILE))
	@$(GNUECHO)
	@$(GNUECHO) +++ print compiler definitions (for VisualStudio browsing) +++
	@$(GNUECHO) $(addsuffix ;,$(C_DEFINES) $(C_DEFINES_TOOL_INTERNAL))
	@$(GNUECHO)
	@$(GNUECHO) +++ print compiler definitions (as used by the compiler) +++
	@$(GNUECHO) $(C_DEF_PARAM)

PrintCompilerFlags:
	@$(GNUECHO)
	@$(GNUECHO) +++ print compiler CFLAGS_DEFAULT (for code review) +++
	@$(GNUECHO) $(CFLAGS_DEFAULT)
	@$(GNUECHO)
	@$(GNUECHO) +++ print compiler CPPFLAGS (for code review) +++
	@$(GNUECHO) $(CPPFLAGS)
	@$(GNUECHO)
	@$(GNUECHO) +++ print linker LDFLAGS (for code review) +++
	@$(GNUECHO) $(LDFLAGS)
	@$(GNUECHO)

PrintDebugInfo:
	@$(GNUECHO) Additional debug information:
	@$(GNUECHO) PATH_SRC_CUST_VAR_BUILD : $(PATH_SRC_CUST_VAR_BUILD)
	@$(GNUECHO) PATH_SRC_CUST           : $(PATH_SRC_CUST)
	@$(GNUECHO) PATH_SRC_CUST_SWC/SwcExample/Api: $(PATH_SRC_CUST_SWC)/SwcExample/Api
	@$(GNUECHO)
	@$(GNUECHO) ENV_MAKE_CDD            : $(ENV_MAKE_CDD)
	@$(GNUECHO) PATH_SRC_CDD            : $(PATH_SRC_CDD)
	@$(GNUECHO) FILES_SRC_CDD           : $(FILES_SRC_CDD)
	@$(GNUECHO)
	@$(GNUECHO) ENV_MAKE_SWC            : $(ENV_MAKE_SWC)
	@$(GNUECHO) PATH_SRC_CUST_SWC       : $(PATH_SRC_CUST_SWC)
	@$(GNUECHO) PATH_SRC_SWC            : $(PATH_SRC_SWC) This is empty, and should be empty.
	@$(GNUECHO) FILES_SRC_SWC           : $(FILES_SRC_SWC)
	@$(GNUECHO)
	@$(GNUECHO) PATH_SRC_CUST           : $(PATH_SRC_CUST)
	@$(GNUECHO) ENV_MAKE_CUST_VAR_BUILD : $(ENV_MAKE_CUST_VAR_BUILD)
	@$(GNUECHO) C_DEFINES : $(C_DEFINES)
	@$(GNUECHO)
	@$(GNUECHO) FILES_COVER             : $(FILES_COVER)
	@$(GNUECHO)


# ------------------------------------------------------------------------------
# Rules: Main
# ------------------------------------------------------------------------------
SingleCore_Clean          : CleanTmpDirectory

SingleCore_Prepare        : MakeTmpDirectories CreateAndCleanBinDirectory PrintRebuildType PrintCompilerVersion PrintIncludePaths PrintCompilerFlags PrintDebugInfo DoMisraPrepare PreBuild

MultiCore_Build_C         : LinkObjectFiles

SingleCore_Finalize       : HexManip ALSize PostBuild DoMisraFinalize
