
add_executable(${PROJECT_NAME}
    main.c
    ch32fun.c
    #blink/blink.c
)

target_include_directories(${PROJECT_NAME} PUBLIC
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/inc/
)

target_link_libraries(${PROJECT_NAME} PRIVATE 
    #blink
    gcc
)


# The `-ffunction-sections` and `-fdata-sections` need to be used 
# in the compile and linker options otherwise it doesnt't
# correctly remove the unused functions.
target_compile_options(${PROJECT_NAME} PUBLIC
    -g
    -Os
    -flto
    -ffunction-sections
    -fdata-sections
    -fmessage-length=0
    -msmall-data-limit=8
    -march=rv32ec
    -mabi=ilp32e
    -DCH32V003=1
)

target_link_options(${PROJECT_NAME} PUBLIC
    -flto
    -ffunction-sections
    -fdata-sections
    -fmessage-length=0
    -msmall-data-limit=8
    -march=rv32ec
    -mabi=ilp32e
    -static-libgcc
    -nostdlib
    -L${CMAKE_SOURCE_DIR}/src
    -Wl,--print-memory-usage
    -Wl,-Map=${PROJECT_NAME}.map
    -Wl,--gc-sections
    -T${CMAKE_SOURCE_DIR}/src/linker_script.ld
)


set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".elf")

# Convert output to hex and binary
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex
)



add_subdirectory(ADC)
add_subdirectory(RegEdit)
add_subdirectory(blink)
