# ###############################
# WCH(RISC-V) toolchain file for the ch32v003
# ###############################

# ----------------------------------------
# File: riscv64-toolchain.cmake
# Description: Cmake file for the WCH controllers. It has two differnt
# types of kinds things it can output. It can output or build release binaries
# or it output debug builds.
# ----------------------------------------




##################################################
# Configurable Section
##################################################
# Specify the cross-compiler
#set(CROSS_TOOLCHAIN riscv32-unknown-elf-)
#set(CROSS_TOOLCHAIN riscv64-unknown-elf-)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR riscv)
set(CMAKE_SYSTEM_PROCESSOR "CUSTOM")
set(DEBUG_BINARY 1)
set(CMAKE_SYSTEM_VERSION 1)


# Without this flag, CMake is unable to pass the test compilation check
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)


# This logic checks the operating system.
if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "FreeBSD")
    message("OS:: FreeBSD")
    set(NEWLIB /usr/local/riscv/riscv32-unknown-elf/include)
    set(CMAKE_C_COMPILER riscv32-unknown-elf-gcc)
    set(CMAKE_CXX_COMPILER riscv32-unknown-elf-g++)
    set(CMAKE_ASM_COMPILER riscv32-unknown-elf-gcc)
    set(CMAKE_FIND_ROOT_PATH /usr/local/riscv/)

    #set(CMAKE_SYSROOT "/usr/local/riscv/" /usr/local/riscv/riscv32-unknown-elf)

    # Without these two lines it freaks out.
    set(CMAKE_C_STANDARD_COMPUTED_DEFAULT GNU)
    set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT GNU)

elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
    message("OS:: LINUX")
    set(NEWLIB /opt/riscv/riscv32-unknown-elf/include)
    set(CMAKE_C_COMPILER riscv32-unknown-elf-gcc)
    set(CMAKE_CXX_COMPILER riscv32-unknown-elf-g++)
    set(CMAKE_ASM_COMPILER riscv32-unknown-elf-gcc)
    
    # Without these two lines it freaks out.
    set(CMAKE_C_STANDARD_COMPUTED_DEFAULT GNU)
    set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT GNU)
else()
    message("OS:: UNKNOWN?")
    message("Please add your OS details to the toolchain file.")
endif()

# Find the Newlib

# Find the ch32v003fun

#


# ----------------------------------------
# MCU Setup
# ----------------------------------------


#this really shouldn't be needed as it's also defined in the
# `/inc/funconfig.h` file.

# Specify the exact Chip
set(MCU CH32V003)

#Define it for the preprocessor as well.
add_compile_definitions(${MCU}=1)


##################################################
# Advanced Section
##################################################



#-------------------
# General Flags
#-------------------

set(OBJECT_GEN_FLAGS "\
-g \
-Os \
-flto \
-ffunction-sections \
-fdata-sections \
-fmessage-length=0 \
-msmall-data-limit=8 ")

#-------------------
# CFLAGS for ARCH
#-------------------

set(C_FLAGS_ARCH "\
-march=rv32ec \
-mabi=ilp32e \
-I${NEWLIB} \
-nostdlib")

#-------------------
# CFLAGS
#-------------------

# None of the linker flags or the cflags seem to do anything.
# They print out from the messages just fine, but they are ignored.

UNSET(CMAKE_C_FLAGS CACHE)
UNSET(CMAKE_CXX_FLAGS CACHE)

# I'm appending to the existing cflags from the cmake file in the root dir.
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_FLAGS_ARCH} ${OBJECT_GEN_FLAGS}")
#set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${C_FLAGS_ARCH} ${OBJECT_GEN_FLAGS}")
#set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS} ${C_FLAGS_ARCH} ${OBJECT_GEN_FLAGS}" CACHE STRING "" FORCE)
#set(CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS} ${C_FLAGS_ARCH} ${OBJECT_GEN_FLAGS}" CACHE STRING "" FORCE)


#-------------------
# ASMFLAGS for cross
#-------------------


#-------------------
# LFLAGS for cross
#-------------------

#set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_SCRIPT})
#set(LINKER_SCRIPT "./src/ch32v003.ld")
#set(LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/src/linker_script.ld")

#set(LD_FLAGS "-lgcc")
set(LD_FLAGS " \
    -Wl,--print-memory-usage \
    -Wl,-Map=${PROJECT_NAME}.map \
    -lgcc \
    -T ${LINKER_SCRIPT} \
    -Wl,--gc-sections"
)

#Allowing this line causes it to attempt compiler tests and if 
# tosses errors.

#set(CMAKE_EXE_LINKER_FLAGS_INIT "${LD_FLAGS}")


#target_link_options(my_project PRIVATE -static -nostartfiles -T linker_script.ld)
#set(CMAKE_EXE_LINKER_FLAGS_INIT "${LD_FLAGS}")
#set(LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/src/linker_script.ld")

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


#-------------------
# Print out info 
#-------------------

message(STATUS "CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
message(STATUS "CMAKE_INCLUDE_PATH: ${CMAKE_INCLUDE_PATH}")
message(STATUS "CMAKE_FIND_ROOT_PATH: ${CMAKE_FIND_ROOT_PATH}")


# Unset all cache
#unset(SEMIHOSTING)
#unset(CMAKE_TOOLCHAIN_FILE)
#unset(CMAKE_BUILD_TYPE)
