# set(PROFILER_DIRECTORY ${LOCALAPPDATA}/${PLATFORM}/EmbeddedEFPs/Profiler)
set(PROFILER_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})

if(NOT DEFINED BSP_CONFIGURATION_com.sysprogs.efp.semihosting.buffer_size)
    set(BSP_CONFIGURATION_com.sysprogs.efp.semihosting.buffer_size "4096")
    set(BSP_CONFIGURATION_com.sysprogs.efp.semihosting.buffer_size "${BSP_CONFIGURATION_com.sysprogs.efp.semihosting.buffer_size}")
endif()

if(NOT DEFINED BSP_CONFIGURATION_com.sysprogs.efp.semihosting.stdio)
    set(BSP_CONFIGURATION_com.sysprogs.efp.semihosting.stdio "1")
    set(BSP_CONFIGURATION_com.sysprogs.efp.semihosting.stdio "${BSP_CONFIGURATION_com.sysprogs.efp.semihosting.stdio}")
endif()

if(NOT DEFINED BSP_CONFIGURATION_com.sysprogs.efp.semihosting.blocking_mode)
    set(BSP_CONFIGURATION_com.sysprogs.efp.semihosting.blocking_mode "1")
    set(BSP_CONFIGURATION_com.sysprogs.efp.semihosting.blocking_mode "${BSP_CONFIGURATION_com.sysprogs.efp.semihosting.blocking_mode}")
endif()

if(NOT DEFINED BSP_CONFIGURATION_com.sysprogs.efp.profiling.semihosting_driver)
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.semihosting_driver "1")
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.semihosting_driver "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.semihosting_driver}")
endif()

if(NOT DEFINED BSP_CONFIGURATION_com.sysprogs.efp.profiling.counter)
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.counter "")
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.counter "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.counter}")
endif()

if(NOT DEFINED BSP_CONFIGURATION_com.sysprogs.efp.profiling.address_validators)
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.address_validators "")
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.address_validators "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.address_validators}")
endif()

if(NOT DEFINED BSP_CONFIGURATION_com.sysprogs.efp.profiling.nosampling)
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.nosampling "0")
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.nosampling "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.nosampling}")
endif()

if(NOT DEFINED BSP_CONFIGURATION_com.sysprogs.efp.profiling.debugger_check)
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.debugger_check "")
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.debugger_check "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.debugger_check}")
endif()

if(NOT DEFINED BSP_CONFIGURATION_com.sysprogs.efp.profiling.rtos)
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.rtos "USE_FREERTOS_IF_FOUND")
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.rtos "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.rtos}")
endif()

if(NOT DEFINED BSP_CONFIGURATION_com.sysprogs.efp.profiling.hold_interrupts)
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.hold_interrupts "")
    set(BSP_CONFIGURATION_com.sysprogs.efp.profiling.hold_interrupts "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.hold_interrupts}")
endif()

add_library(EFP STATIC)
target_sources(EFP PRIVATE
    "${PROFILER_DIRECTORY}/FastSemihosting.cpp"
    "${PROFILER_DIRECTORY}/InstrumentingProfiler.cpp"
    "${PROFILER_DIRECTORY}/SysprogsProfiler.h"
    "${PROFILER_DIRECTORY}/SmallNumberCoder.h"
    "${PROFILER_DIRECTORY}/SysprogsProfilerInterface.h"
    "${PROFILER_DIRECTORY}/ProfilerFreeRTOSHooks.h"
    "${PROFILER_DIRECTORY}/CustomRealTimeWatches.h"
    "${PROFILER_DIRECTORY}/DebuggerChecker.h"
)

add_library(EFPTesting INTERFACE)
target_sources(EFPTesting INTERFACE
    ${PROFILER_DIRECTORY}/TestResourceManager.cpp
)
target_include_directories(EFPTesting INTERFACE
    ${PROFILER_DIRECTORY}
)
target_compile_options(EFP PRIVATE
    -Wno-sign-conversion
    -Wno-conversion
    -Wno-cast-qual
    -Wno-shift-negative-value
    -Wno-maybe-uninitialized
    -Wno-missing-declarations
    -Wno-unused-variable
    -Wno-undef
    $<$<COMPILE_LANGUAGE:CXX>:-Wno-useless-cast>
    $<$<COMPILE_LANGUAGE:CXX>:-Wno-zero-as-null-pointer-constant>
    $<$<COMPILE_LANGUAGE:CXX>:-Wno-old-style-cast>
)

if(NOT ("${BSP_CONFIGURATION_com.sysprogs.efp.profiling.nosampling}" STREQUAL "1"))
    target_sources(EFP PRIVATE "${PROFILER_DIRECTORY}/SamplingProfiler.cpp")
endif()

if(((DEFINED BSP_FRAMEWORK_com.sysprogs.arm.stm32.hal) OR ("${BSP_MCU_FAMILY}" MATCHES "com.sysprogs.generated.keil.family.STM32.*") OR ("${BSP_CONFIGURATION_MBED__TARGET_STM}" STREQUAL "1")) AND (NOT ("${BSP_CONFIGURATION_com.sysprogs.efp.profiling.nosampling}" STREQUAL "1")))
    target_sources(EFP PRIVATE "${PROFILER_DIRECTORY}/ProfilerDriver_STM32_HAL.cpp")
endif()

if(((DEFINED BSP_FRAMEWORK_com.sysprogs.arm.stm32.stdperiph) OR (DEFINED BSP_FRAMEWORK_com.sysprogs.arm.stm32.f0_stdperiph) OR (DEFINED BSP_FRAMEWORK_com.sysprogs.arm.stm32.f1_stdperiph) OR (DEFINED BSP_FRAMEWORK_com.sysprogs.arm.stm32.f2_stdperiph) OR (DEFINED BSP_FRAMEWORK_com.sysprogs.arm.stm32.f4_stdperiph) OR (DEFINED BSP_FRAMEWORK_com.sysprogs.arm.stm32.f7_stdperiph) OR (DEFINED BSP_FRAMEWORK_com.sysprogs.arm.stm32.l0_stdperiph) OR (DEFINED BSP_FRAMEWORK_com.sysprogs.arm.stm32.l1_stdperiph) OR (DEFINED BSP_FRAMEWORK_com.sysprogs.arm.stm32.l4_stdperiph)) AND (NOT ("${BSP_CONFIGURATION_com.sysprogs.efp.profiling.nosampling}" STREQUAL "1")))
    target_sources(EFP PRIVATE "${PROFILER_DIRECTORY}/ProfilerDriver_STM32_StdPeriph.cpp")
endif()

if("${BSP_CONFIGURATION_com.sysprogs.efp.profiling.rtos}" STREQUAL "USE_FREERTOS_IF_FOUND")
    target_sources(EFP PRIVATE "${PROFILER_DIRECTORY}/ProfilerRTOS_FreeRTOS.c")
endif()

target_include_directories(EFP SYSTEM PUBLIC ${PROFILER_DIRECTORY})
#target_compile_definitions(EFP PRIVATE
add_compile_definitions(
    "FAST_SEMIHOSTING_BUFFER_SIZE=${BSP_CONFIGURATION_com.sysprogs.efp.semihosting.buffer_size}"
    "FAST_SEMIHOSTING_BLOCKING_MODE=${BSP_CONFIGURATION_com.sysprogs.efp.semihosting.blocking_mode}"
    "FAST_SEMIHOSTING_STDIO_DRIVER=${BSP_CONFIGURATION_com.sysprogs.efp.semihosting.stdio}"
    "FAST_SEMIHOSTING_PROFILER_DRIVER=${BSP_CONFIGURATION_com.sysprogs.efp.profiling.semihosting_driver}"
    "PROFILER_STM32H7"
    "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.counter}"
    "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.debugger_check}"
    "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.address_validators}"
    "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.rtos}"
    "${BSP_CONFIGURATION_com.sysprogs.efp.profiling.hold_interrupts}"
)
target_compile_options(EFP PRIVATE -include ${PROFILER_DIRECTORY}/ProfilerFreeRTOSHooks.h)

