CMake搭建GHS工具链编译RH850/U2A16

编译参数和链接参数,都是参考GHS IDE中编译的输出文件。在编译输出目录中,会有很多 .o 文件,里面有每个文件的编译命令。编译参数参数可以参考该出处。链接参数参考GHS IDE工程主目标的链接命令,在最下面的command窗口中,会有完整的链接命令,链接参数可以参考该位置。

ghs_ccv850.cmake

set(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_C_COMPILER_WORKS TRUE CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER_WORKS TRUE CACHE INTERNAL "")
set(CMAKE_C_COMPILER_FORCED TRUE CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER_FORCED TRUE CACHE INTERNAL "")
set(CMAKE_C_COMPILER_ID_RUN TRUE CACHE INTERNAL "")
set(CMAKE_CXX_COMPILER_ID_RUN TRUE CACHE INTERNAL "")

set(CMAKE_C_COMPILER "C:/ghs/comp_202015/ccv850.exe")
set(CMAKE_CXX_COMPILER "C:/ghs/comp_202015/cxv850.exe")
set(CMAKE_AR "C:/ghs/comp_202015/cxv850.exe")
set(CMAKE_ASM_COMPILER "C:/ghs/comp_202015/ccv850.exe")

set(LINK_TOOL_COMPILER "C:/ghs/comp_202015/ccv850.exe")

set(GHS_TOOLSET_ROOT "C:/ghs")

set(CMAKE_C_FLAGS "-MD -cpu=rh850g4mh --preprocess_linker_directive_full -large_sda -large_zda -farcalls -ghsmc_core_count=1 -ghsmc_file="${CMAKE_SOURCE_DIR}"/StartupBuild.ghsmc -D__RH850_U2A__ -D__RH850_U2A__ -D__RH850_U2A16__ -fnofunctions -sda=all -dwarf2 -shorten_loads -no_init_ram_at_startup -g -no_data_delete -no_uvfd -no_codefactor -no_ignore_debug_references -Osize -object_dir=OBJ")
set(CMAKE_ASM_FLAGS "-MD -G -Odebug -bsp generic -cpu=rh850g4mh -pic -pid -ghsmc_core_count=1 -ghsmc_file="${CMAKE_SOURCE_DIR}"/StartupBuild.ghsmc -object_dir=OBJ")

set(CMAKE_C_LINK_FLAGS "")
set(CMAKE_C_LINK_EXECUTABLE "${LINK_TOOL_COMPILER}    -o ")

主目录CMakeLists.txt

cmake_minimum_required(VERSION 3.2)

#设置编译器
set(CMAKE_TOOLCHAIN_FILE ./cmake/ghs_ccv850.cmake)

#设置cmake工程名
project(${PRODUCT_TYPE} LANGUAGES C ASM)

set(PRODUCT_TYPE u2a16)

#设置目标文件
set(PROJECT_TARGET ${PRODUCT_TYPE}.elf)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

#添加源文件到目标文件
ADD_EXECUTABLE(${PROJECT_TARGET} ${SOURCES})

#设置链接选项
set(LINK_OPTIONS -cpu=rh850g4mh -MD -bsp generic -pic -pid -ghsmc_core_count=1 -ghsmc_file=${CMAKE_SOURCE_DIR}/StartupBuild.ghsmc -object_dir=OBJS -T ${CMAKE_SOURCE_DIR}/platform/Linker/U2A16_Common.ld
    )
target_link_options(${PROJECT_TARGET} PRIVATE ${LINK_OPTIONS})

#添加库文件到目标文件
target_link_libraries(${PROJECT_TARGET} )

#增加子目录
add_subdirectory(platform)
add_subdirectory(src)

汇编启动代码的编译
由于CMake无法识别 .850 后缀的汇编代码,直接修改后缀,使用 ccv850 工具链编译是会有问题,目前看到的现象是无法进行预处理,会导致一些不需要的符号被编译进去,导致链接失败。
因此的解决办法是对于这些启动文件进行单独编译,再把 .o 加到链接参数里面。

startup


cmake_minimum_required(VERSION 3.1)

set(SOURCES_LIST
    ${CMAKE_CURRENT_SOURCE_DIR}/Interrupt_Handler.c
    ${CMAKE_CURRENT_SOURCE_DIR}/Interrupt_VectorTable.c
)

set(HDR_PATH_LIST
    # ${CMAKE_SOURCE_DIR}/platform/startup
)

add_dependencies(
    ${PROJECT_TARGET} startup
    )

add_custom_target(
    startup
    DEPENDS ${CMAKE_SOURCE_DIR}/build/OBJ/coldreset.o ${CMAKE_SOURCE_DIR}/build/OBJ/exception.o ${CMAKE_SOURCE_DIR}/build/OBJ/set_csa.o
)

add_custom_command(
    OUTPUT ${CMAKE_SOURCE_DIR}/build/OBJ/coldreset.o
    COMMAND ${CMAKE_C_COMPILER} -c -MD -G -Odebug -bsp generic -cpu=rh850g4mh -pic -pid -ghsmc_core_count=1 -ghsmc_file=${CMAKE_SOURCE_DIR}/StartupBuild.ghsmc -object_dir=${CMAKE_SOURCE_DIR}/build/OBJ -filetype.assembly ${CMAKE_CURRENT_SOURCE_DIR}/coldreset.850 -o coldreset.o
)

add_custom_command(
    OUTPUT ${CMAKE_SOURCE_DIR}/build/OBJ/exception.o
    COMMAND ${CMAKE_C_COMPILER} -c -MD -G -Odebug -bsp generic -cpu=rh850g4mh -pic -pid -ghsmc_core_count=1 -ghsmc_file=${CMAKE_SOURCE_DIR}/StartupBuild.ghsmc -object_dir=${CMAKE_SOURCE_DIR}/build/OBJ -filetype.assembly ${CMAKE_CURRENT_SOURCE_DIR}/exception.850 -o exception.o
)

add_custom_command(
    OUTPUT ${CMAKE_SOURCE_DIR}/build/OBJ/set_csa.o
    COMMAND ${CMAKE_C_COMPILER} -c -MD -G -Odebug -bsp generic -cpu=rh850g4mh -pic -pid -ghsmc_core_count=1 -ghsmc_file=${CMAKE_SOURCE_DIR}/StartupBuild.ghsmc -object_dir=${CMAKE_SOURCE_DIR}/build/OBJ -filetype.assembly ${CMAKE_CURRENT_SOURCE_DIR}/set_csa.850 -o set_csa.o
)

target_link_options(${PROJECT_TARGET} PRIVATE ${CMAKE_SOURCE_DIR}/build/OBJ/coldreset.o ${CMAKE_SOURCE_DIR}/build/OBJ/exception.o ${CMAKE_SOURCE_DIR}/build/OBJ/set_csa.o)
target_sources(${PROJECT_TARGET} PRIVATE ${SOURCES_LIST})
target_include_directories(${PROJECT_TARGET} PRIVATE ${HDR_PATH_LIST})

你可能感兴趣的:(cmake)