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.

105 lines
2.7 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
core/retarget.c
core/commands.c
core/gpio.c
core/ringbuf.c
core/cmd.c
core/terminal.c
core/tick.c
core/abi.cpp
core/new.cpp
)
target_include_directories(${tgt_name} PRIVATE
${PROJECT_SOURCE_DIR}/core
)
target_link_libraries(${tgt_name} PRIVATE
gd32f40x_periph
)
target_link_options(${tgt_name} PRIVATE
-mthumb -mcpu=cortex-m4 -march=armv7e-m
-mfloat-abi=hard -mfpu=fpv4-sp-d16
-nostartfiles
LINKER:--gc-sections
LINKER:--cref
LINKER:--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
COMMENT "Create Hex 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)