
# This one is important
set(CMAKE_SYSTEM_NAME Generic)

if("${MEMSIZE}" STREQUAL "")
    message(STATUS "No memory! Variable MEMSIZE is empty! Compilation will fail! Please set the size of your memory through MEMSIZE variable in the root of your project. For Example :\n\tset(MEMSIZE \"32K\")")
endif()
if("${ARCH}" STREQUAL "")
    set(ARCH "rv32i")
else()
    set(ARCH "${ARCH}")
endif()
message(STATUS "Arch=${ARCH}")
if("${ABI}" STREQUAL "")
    set(ABI "ilp32")
else()
    set(ABI "${ABI}")
endif()
message(STATUS "Abi=${ABI}")
if("${F_CPU}" STREQUAL "")
    set(F_CPU "50000000")
    message(STATUS "No frequency set! Variable F_CPU is empty. By default we set to ${F_CPU}Hz")
    set(F_CPU "F_CPU=${F_CPU}")
else()
    set(F_CPU "F_CPU=${F_CPU}")
    message(STATUS "${F_CPU}Hz")
endif()
if("${SECTION_START}" STREQUAL "")
    set(SECTION_START "0")
else()
    set(SECTION_START "${SECTION_START}")
endif()
if("${STACK_SIZE}" STREQUAL "")
    set(STACK_SIZE "512")
else()
    set(STACK_SIZE "${STACK_SIZE}")
endif()
if("${HEAP_SIZE}" STREQUAL "")
    set(HEAP_SIZE "1K")
else()
    set(HEAP_SIZE "${HEAP_SIZE}")
endif()

if(${USE_SYSTEM_STATIC_LIBRARY})
    set(C_LIBS "${C_LIBS} -lArduinoLib -lriscvSoC")
    set(CXX_LIBS "${C_LIBS} -lArduinoLib")
endif()

if(WIN32)
    set(WIN_EXE ".exe")
    set(DIR_NAME "Windows")
else()
    set(DIR_NAME "Linux-x86_64")
endif()
set(RISCV_NAME  "riscv64-unknown-elf")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
# Specify the system library path
set(COMPILER_LIBRARY_PATH "${CMAKE_CURRENT_LIST_DIR}/../..")
# Specify the cross compiler path
if("${TOOLCHAIN_PATH}" STREQUAL "")
    set(TOOLCHAIN_PATH "${CMAKE_CURRENT_LIST_DIR}/../toolchain")
endif()


set(CMAKE_C_COMPILER "${TOOLCHAIN_PATH}/bin/${RISCV_NAME}-gcc${WIN_EXE}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Os -march=${ARCH} -mabi=${ABI} -D${F_CPU} -fpeel-loops -ftree-ter -ffreestanding -ffunction-sections -fdata-sections -Wall -I${COMPILER_LIBRARY_PATH}/system/include")
if(BUILD_AS_BOOTLOADER)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -Werror")
else()
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11")
endif()
SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> -r <TARGET> <OBJECTS>")

set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${CMAKE_C_FLAGS}")
SET(CMAKE_ASM_ARCHIVE_CREATE "<CMAKE_AR> -r <TARGET> <OBJECTS>")

set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PATH}/bin/${RISCV_NAME}-g++${WIN_EXE}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Os -march=${ARCH} -mabi=${ABI} -D${F_CPU} -fpeel-loops -ftree-ter -ffreestanding -ffunction-sections -fdata-sections -fno-rtti -fno-exceptions -fpermissive -Wall -I${COMPILER_LIBRARY_PATH}/system/include -I${COMPILER_LIBRARY_PATH}/cores/riscduinov -std=gnu++11")
SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> -r <TARGET> <OBJECTS>")
set(LNK_FILE "linker_script.ld")
# if(NOT USER_CRT0)
#     set(EXEC_STARTUP "-lcrt0")
# endif()
    set(__LIBS ${C_LIBS})     
    set(CMAKE_C_LINK_EXECUTABLE "${TOOLCHAIN_PATH}/bin/${RISCV_NAME}-gcc${WIN_EXE} -o <TARGET>.elf <OBJECTS> <LINK_LIBRARIES> -march=${ARCH} -mabi=${ABI} --specs=nano.specs --specs=nosys.specs -nostdlib -static -Xlinker --gc-sections -Xlinker --defsym=MEMSIZE=${MEMSIZE} -Xlinker --defsym=SECTION_START=${SECTION_START} -Xlinker --defsym=STACK_SIZE=${STACK_SIZE} -Xlinker --defsym=HEAP_SIZE=${HEAP_SIZE} -mcmodel=medany -nostartfiles -ffreestanding -T${COMPILER_LIBRARY_PATH}/tools/linker_scripts/${LNK_FILE} ${__LIBS} -lc -lm -lgcc")
    # set(CMAKE_C_LINK_EXECUTABLE "${TOOLCHAIN_PATH}/bin/${RISCV_NAME}-gcc${WIN_EXE} -o <TARGET>.elf <OBJECTS> <LINK_LIBRARIES> -march=${ARCH} -mabi=${ABI} --specs=nano.specs --specs=nosys.specs -nostdlib -static -Xlinker --gc-sections -Xlinker --defsym=MEMSIZE=${MEMSIZE} -Xlinker --defsym=SECTION_START=${SECTION_START} -Xlinker --defsym=STACK_SIZE=${STACK_SIZE} -Xlinker --defsym=HEAP_SIZE=${HEAP_SIZE} -mcmodel=medany -nostartfiles -ffreestanding -T${COMPILER_LIBRARY_PATH}/system/linker_scripts/${LNK_FILE} -L${COMPILER_LIBRARY_PATH}/system/lib/riscv ${EXEC_STARTUP} ${__LIBS} -lc -lm -lgcc")
    set(CMAKE_ASM_LINK_EXECUTABLE ${CMAKE_C_LINK_EXECUTABLE})
    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
# Specify Object Copy compiler
set(CMAKE_OBJCOPY_COMPILER "${TOOLCHAIN_PATH}/bin/${RISCV_NAME}-objcopy${WIN_EXE}")
set(CMAKE_OBJCOPY_FLAGS "-R .rel.dyn -R .MIPS.abiflags")
set(CMAKE_SIZE "${TOOLCHAIN_PATH}/bin/${RISCV_NAME}-size${WIN_EXE}")
set(CMAKE_SIZE_FLAGS "--format=berkeley")