You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

122 lines
3.2 KiB

cmake_minimum_required(VERSION 3.16)
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE MiniSizeRel)
endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if (NOT DEFINED DEVICE_TYPE)
set(DEVICE_TYPE "GD32F407RK")
endif()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib")
if (NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/lib/arm-elf.cmake)
endif()
set(GD32F4X_CHIP_TYPE GD32F407)
set(gd32f40x_root ${CMAKE_CURRENT_LIST_DIR}/Firmware)
include(gd32f40x_periph)
set(tgt_name "gd32f40x")
project(${tgt_name} C CXX ASM)
add_executable(${tgt_name})
set_target_properties(${tgt_name}
PROPERTIES
CXX_STANDARD 20)
target_sources(${tgt_name} PRIVATE
src/main.c
src/gd32f4xx_it.c
src/board.c
src/pinctrl.c
src/usart0.c
core/printfi.c
core/ymodem.c
core/console_ymodem.c
core/console.c
core/retarget.c
core/commands.c
core/gpio.c
core/ringbuf.c
core/terminal.c
core/tick.c
core/abi.cpp
core/new.cpp
)
execute_process(COMMAND git rev-parse --short HEAD OUTPUT_VARIABLE _git_commit)
#execute_process(COMMAND git log -1 --format=%cd --date=format:%Y%m%d OUTPUT_VARIABLE _git_date)
string(STRIP ${_git_commit} git_commit)
#string(STRIP ${_git_date} git_date)
#string(JOIN - TGT_COMMIT ${git_date} ${git_commit})
target_include_directories(${tgt_name} PRIVATE
${PROJECT_SOURCE_DIR}/core
)
target_link_libraries(${tgt_name} PRIVATE
gd32f40x_periph
)
target_compile_definitions(${tgt_name} PRIVATE
_GNU_SOURCE
GIT_COMMIT="commit ${git_commit}"
)
target_link_options(${tgt_name} PRIVATE
-mthumb -mcpu=cortex-m4 -march=armv7e-m
-mfloat-abi=hard -mfpu=fpv4-sp-d16
-nostartfiles
-Wl,--gc-sections
-Wl,--cref
-Wl,--sort-section=alignment
-Wl,-Map,${tgt_name}.map
# -Wl,-pie -Wl,--no-dynamic-linker
-specs=nosys.specs
-specs=nano.specs
$<$<NOT:$<CONFIG:Debug>>:-s>)
set(lds_name ${CMAKE_BINARY_DIR}/linker.ld)
tgt_add_linker_script(${tgt_name} PRIVATE ${lds_name})
# gcc -E -P -x -c <infile> -o <outfile>
# -E: gcc only run preprocessor
# -P: prevents preprocessor from generating linemarkers (#line directives)
#-x c: tells GCC treat linker script as C source file
add_custom_target(gen_lds
${CMAKE_C_COMPILER} -E -P -x c ${PROJECT_SOURCE_DIR}/lib/gd32f4xx_linker.ld -o ${lds_name}
COMMENT "Generate link script"
VERBATIM)
add_dependencies(${tgt_name} gen_lds)
set(OUTPUT_NAME $<TARGET_FILE:${tgt_name}>)
add_custom_command(TARGET ${tgt_name}
POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O ihex ${OUTPUT_NAME} ${tgt_name}.hex
COMMAND ${CMAKE_OBJCOPY} -O binary ${OUTPUT_NAME} ${tgt_name}.bin
COMMENT "Create Hex & Binary File"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
VERBATIM
)
add_custom_target(flash
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/lib/gdbscript.txt gdbscript.txt
COMMAND sed${CMAKE_EXECUTABLE_SUFFIX} -i "s#%DEVICE_TYPE%#${DEVICE_TYPE}#;s#%TARGET_FILE%#${OUTPUT_NAME}#" gdbscript.txt
COMMAND ${CMAKE_GDB} --batch -nx -x gdbscript.txt ${OUTPUT_NAME}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS ${tgt_name}
COMMENT "Flash elf to device"
VERBATIM)
install(TARGETS ${tgt_name}
DESTINATION ${PROJECT_SOURCE_DIR}/out)