set(LIB_NAME CMSIS)
if(NOT DEFINED MCU_SERIES)
    message(FATAL_ERROR "MCU_SERIES NOT DEFINED. Please define it like stm32fxx")
else()
    string(TOUPPER ${MCU_SERIES} MCU_SERIES_UPPERCASE)
endif()

project(${LIB_NAME}
        LANGUAGES C ASM
        )
if (NOT DEFINED MCU)
    message(FATAL_ERROR "PLEASE DEFINE MCU i.e STM32F427xx")
endif ()

if (NOT GTEST)
    if (NOT ${CORTEX-M} STREQUAL "3" AND NOT ${CORTEX-M} STREQUAL "1" AND NOT ${CORTEX-M} STREQUAL "0" AND NOT ${CORTEX-M} STREQUAL "4" AND NOT ${CORTEX-M} STREQUAL "3" AND NOT ${CORTEX-M} STREQUAL "55")
        message(FATAL_ERROR "Error: please set CORTEX-M")
    endif ()

    if (NOT DEFINED COMPILE_OPTIONS)
        set(COMPILE_OPTIONS -ffunction-sections -fdata-sections -fno-common -fmessage-length=0 $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti> -mcpu=cortex-m${CORTEX-M} -mthumb -mthumb-interwork)
        message(WARNING "${LIB_NAME} compile with default options")
    else ()
        set(COMPILE_OPTIONS ${COMPILE_OPTIONS})
    endif()
endif()

add_library(${LIB_NAME} INTERFACE)
target_include_directories(${LIB_NAME} INTERFACE Include Device/ST/${MCU_SERIES_UPPERCASE}/Include)
target_compile_definitions(${LIB_NAME} INTERFACE "${MCU}")
if (NOT ${CORTEX-M} STREQUAL "3" AND NOT ${CORTEX-M} STREQUAL "1" AND NOT ${CORTEX-M} STREQUAL "0" AND NOT ${CORTEX-M} STREQUAL "4" AND NOT ${CORTEX-M} STREQUAL "3" AND NOT ${CORTEX-M} STREQUAL "55")
    message(FATAL_ERROR "Error: please set CORTEX-M")
else ()
    target_compile_options(${LIB_NAME} BEFORE INTERFACE ${COMPILE_OPTIONS})
endif ()