cmake_minimum_required(VERSION 3.16...3.24)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_VERBOSE_MAKEFILE 0)
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

# user configurations: use -D option to change these on command line
set(IMC_INSERT_COMPILER_CHECKPOINTS 1 CACHE BOOL "")
set(IMC_ENABLE_EXTENSION 0 CACHE BOOL "")
set(IMC_USE_CHECKPOINT_PASS_COUNTER 1 CACHE BOOL "")
set(IMC_USE_CHECKPOINT_VOLTAGE_CHECK 0 CACHE BOOL "")
set(IMC_CHECKPOINT_PASS_COUNT 100 CACHE STRING "")

# set(CMAKE_C_COMPILER arm-none-eabi-gcc) #tested with: clang, arm-none-eabi-gcc
set(CMAKE_C_COMPILER /home/ybkim/workspace/imc/llvm/llvm-17/build/bin/clang)
set(CMAKE_SYSROOT "/opt/gcc-arm-none-eabi/arm-none-eabi")
set(ARM_CORTEXM_MULTI_DIR "/opt/gcc-arm-none-eabi/arm-none-eabi/lib/thumb/v8-m.main+fp/hard")
set(ARM_CORTEXM_BUILTINS_DIR "/opt/gcc-arm-none-eabi/lib/gcc/arm-none-eabi/11.2.1/thumb/v8-m.main+fp/hard")
set(OPT_BIN /home/ybkim/workspace/imc/llvm/llvm-17/build/bin/opt)
set(LLC_BIN /home/ybkim/workspace/imc/llvm/llvm-17/build/bin/llc)
set(LLVM_OPT_PLUGIN /home/ybkim/workspace/imc/loop_duplication/build/lib/libImcInsertCheckpoints.so)

set(SRC_FILES
    Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM33_NTZ/non_secure/port.c
    Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM33_NTZ/non_secure/portasm.c
    Middlewares/Third_Party/FreeRTOS/Source/tasks.c
)

include_directories(
    Core/Inc 
    Drivers/STM32L5xx_HAL_Driver/Inc 
    Drivers/STM32L5xx_HAL_Driver/Inc/Legacy 
    Drivers/CMSIS/Device/ST/STM32L5xx/Include 
    Drivers/CMSIS/Include 
    USB_Device/App 
    USB_Device/Target 
    Middlewares/ST/STM32_USB_Device_Library/Core/Inc 
    Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc 
    Middlewares/Third_Party/FreeRTOS/Source/include 
    Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2 
    Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM33_NTZ/non_secure 
)

# link_directories(
#     ${ARM_CORTEXM_BUILTINS_DIR}
#     ${ARM_CORTEXM_MULTI_DIR}
# )

# if(IMC_ENABLE_EXTENSION)
#     include(imc_extension.cmake)
#     add_compile_definitions(imcUSE_IMC_EXTENSION=1)
# else()
#     add_compile_definitions(imcUSE_IMC_EXTENSION=0)
# endif()

# compile flags

add_compile_definitions(USE_HAL_DRIVER STM32L552xx)
set(CMAKE_SYSTEM_NAME Generic)

set(CPU "-mcpu=cortex-m33")
set(FPU "-mfpu=fpv5-sp-d16")
set(FLOAT-ABI "-mfloat-abi=hard")
set(MCU "${CPU} -mthumb ${FPU} ${FLOAT-ABI}")
set(OPT "-O1")
set(C_FLAGS_NO_ASM )
set(C_FLAGS_EXTRA "-Wall -Wno-char-subscripts -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -Wno-format -nostdlib -fdata-sections -ffunction-sections")

set(COMPILER_SPECIFIC_CFLAGS )
if (CMAKE_C_COMPILER MATCHES "clang")
    set(TARGET-TRIPPLE "--target=armv8m-none-eabi --sysroot=${CMAKE_SYSROOT}")
    set(COMPILER_SPECIFIC_CFLAGS "${TARGET-TRIPPLE}")
endif()

set(COMMON_CC_FLAGS "${MCU} ${TARGET-TRIPPLE} ${OPT} ${C_FLAGS_EXTRA} ${COMPILER_SPECIFIC_CFLAGS}")

set(CMAKE_C_FLAGS "${COMMON_CC_FLAGS} ${C_FLAGS_NO_ASM}")

# asm options
set(ASM_OPTIONS "-x assembler-with-cpp -Wno-null-character")
set(CMAKE_ASM_FLAGS "${COMMON_CC_FLAGS} ${ASM_OPTIONS}")

# linker options
# set(ARM_CORTEXM_BUILTINS "-L${ARM_CORTEXM_BUILTINS_DIR}")
# set(LD_ADDITIONAL_FILES "${ARM_CORTEXM_BUILTINS_DIR}/crti.o ${ARM_CORTEXM_BUILTINS_DIR}/crtn.o ${IMC_LINK_OBJS}")

# set(LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/STM32L552ZETX_FLASH.ld")
# set(CMAKE_EXE_LINKER_FLAGS "${LD_ADDITIONAL_FILES} -T ${LINKER_SCRIPT}")

project(imc_freeRTOS C ASM)
add_library(imc_freeRTOS ${SRC_FILES})
# add_executable(imc_freeRTOS ${SRC_FILES})

# target_compile_options(imc_freeRTOS PRIVATE "-g")
# set_target_properties(imc_freeRTOS PROPERTIES LINK_DEPENDS ${LINKER_SCRIPT})