cmake_minimum_required(VERSION 3.5)

include(cmakeProjectConfig.cmake)

set(BSL_DIR ${CMAKE_SOURCE_DIR}/bsl/${BSL_USED})

if(EXISTS ${BSL_DIR}) # Cheking if the directory exists
	message("${BoldGreen}Compiling for ${BSL_USED} ${ColourReset}")
	set(COMPILER_DEFS ${BSL_DIR}/bslCompiler.cmake)
	set(PROJECT_DEFS ${BSL_DIR}/bslConfig.cmake)
else()
	message( FATAL_ERROR "${BoldRed}Please Select a valid BSL, CMake will exit.${ColourReset}" )
endif()

include(${COMPILER_DEFS})

project(${BSL_USED} ASM C CXX)		#do this intead sf declaring languages in the beginning it will prevent loop errors. 

set(CMAKE_VERBOSE_MAKEFILE off)		#Shoul make print everythign ??
set(EXECUTABLE ${PROJECT_NAME}) #Create the executable

####################################################################################################
#CONFIG FILE
####################################################################################################
set(INTERFACES_DIR ${CMAKE_SOURCE_DIR}/bsl/csl/interfaces)

####################################################################################################
#SUBDIRECTORIES
####################################################################################################
include(${PROJECT_DEFS})
add_subdirectory(bsl)

set(BSL_HEADER_FILE ${CMAKE_SOURCE_DIR}/bsl/raspberry/bsl_raspberry.hpp)

message("${BoldBlue}Project Info  ${ColourReset}")
message("${Blue} |--> Exec Name	\t: ${EXECUTABLE} ${ColourReset}")
message("${Blue} |--> Compiler Def\t: ${COMPILER_DEFS} ${ColourReset}")
message("${Blue} |--> Project Def\t: ${PROJECT_DEFS} ${ColourReset}")
message("${Blue} |--> Interfaces Dir\t: ${INTERFACES_DIR} ${ColourReset}")
message("${Blue} |--> Libs used\t\t: ${EXTRA_LIBS} ${ColourReset}")

####################################################################################################
#EXECUTABLE
####################################################################################################
add_executable(${EXECUTABLE} main.cpp)
target_compile_options(${EXECUTABLE} PRIVATE ${CPP_FLAGS})
target_compile_definitions(${EXECUTABLE} PRIVATE ${CPP_DEFS})
target_include_directories(${EXECUTABLE} PUBLIC ${CPP_INCLUDES})

####################################################################################################
#LINKING EXECUTEABLE
####################################################################################################
if(IS_NO_SYS)
	message("${Cyan}Using specifier Linker script ${ColourReset}")
	message("${Cyan} |--> ${LINKER} ${ColourReset}")
	
	target_link_libraries(${EXECUTABLE} ${EXTRA_LIBS})
	target_link_options(${EXECUTABLE} PRIVATE ${LINKER_FLAGS}) 
else ()	
	target_link_libraries(${EXECUTABLE} ${EXTRA_LIBS})
endif()

####################################################################################################
#CUSTOM COMMANDS
####################################################################################################

if(NEED_OBJCOPY)
	add_custom_command(TARGET ${EXECUTABLE}
        POST_BUILD
		COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE} ${PROJECT_NAME}.hex
		COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE} ${PROJECT_NAME}.bin)
endif()

add_custom_command(TARGET ${EXECUTABLE}
        POST_BUILD
		COMMAND ${CMAKE_SIZE} ${EXECUTABLE})

####################################################################################################
#DOXYGEN
####################################################################################################
find_package(Doxygen)
if (DOXYGEN_FOUND)
    # set input and output files
    set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/doxyfile.in)
    set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/doxyfile)

    # request to configure the file
    configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
    message("Doxygen build started")

    # note the option ALL which allows to build the docs together with the application
    add_custom_target( doc_doxygen ALL
        COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMENT "Generating API documentation with Doxygen"
        VERBATIM )
else (DOXYGEN_FOUND)
  message("Doxygen need to be installed to generate the doxygen documentation")
endif (DOXYGEN_FOUND)
####################################################################################################
#CUSTOM Comments from dev.
####################################################################################################
# Link For header dependency : https://stackoverflow.com/questions/11216408/cmake-dependencies-headers-between-apps-libraries-in-same-project
# This is one possible trick to handle the assenbly compiling.
# We can't use arm-non-eabi-as because it can onaly hande macros.
# So this bizzare Variable makes shure that whne the asembly compiling is called the -x assembler-with-cpp flag is passed
# target_compile_options(${EXECUTABLE} PRIVATE
#    $<$<COMPILE_LANGUAGE:ASM>:-x assembler-with-cpp ${ASM_FLAGS}>)
