Converting a Makefile to CMakeLists.txt for Tiva C

2019-03-06 09:25发布

问题:

I'd like to program my Tiva C Series LaunchPad board in C++ using CMake build process. I downloaded a simple examples to blink the RGB LED I built using make and I'd like to be able to use cmake to start a bigger project.

Here is the Makefile provided in the example :

# Tiva Makefile
# #####################################
#
# Part of the uCtools project
# uctools.github.com
#
#######################################
# user configuration:
#######################################
# TARGET: name of the output file
TARGET = firmware
# MCU: part number to build for
MCU = TM4C123GH6PM
# SOURCES: list of input source sources
SOURCES = main.c startup_gcc.c
# INCLUDES: list of includes, by default, use Includes directory
INCLUDES = -IInclude
# OUTDIR: directory to use for output
OUTDIR = build
# TIVAWARE_PATH: path to tivaware folder
TIVAWARE_PATH = ../tivaware

# LD_SCRIPT: linker script
LD_SCRIPT = $(MCU).ld

# define flags
CFLAGS = -g -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp
CFLAGS +=-Os -ffunction-sections -fdata-sections -MD -std=c99 -Wall
CFLAGS += -pedantic -DPART_$(MCU) -c -I$(TIVAWARE_PATH)
CFLAGS += -DTARGET_IS_BLIZZARD_RA1
LDFLAGS = -T $(LD_SCRIPT) --entry ResetISR --gc-sections

#######################################
# end of user configuration
#######################################
#
#######################################
# binaries
#######################################
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
OBJCOPY = arm-none-eabi-objcopy
RM      = rm -f
MKDIR   = mkdir -p
#######################################

# list of object files, placed in the build directory regardless of source path
OBJECTS = $(addprefix $(OUTDIR)/,$(notdir $(SOURCES:.c=.o)))

# default: build bin
all: $(OUTDIR)/$(TARGET).bin

$(OUTDIR)/%.o: src/%.c | $(OUTDIR)
    $(CC) -o $@ $^ $(CFLAGS)

$(OUTDIR)/a.out: $(OBJECTS)
    $(LD) -o $@ $^ $(LDFLAGS)

$(OUTDIR)/$(TARGET).bin: $(OUTDIR)/a.out
    $(OBJCOPY) -O binary $< $@

# create the output directory
$(OUTDIR):
    $(MKDIR) $(OUTDIR)

clean:
    -$(RM) $(OUTDIR)/*

.PHONY: all clean

My first CMakeLists.txt file based on it :

project(firmware)
cmake_minimum_required(VERSION 2.8)

# this one is important
set(CMAKE_SYSTEM_NAME Generic)
#this one not so much
#set(CMAKE_SYSTEM_VERSION 1)

# specify the toolchain
set(TOOLCHAIN_PREFIX ${PROJECT_SOURCE_DIR}/../toolchain/bin/arm-none-eabi-)
set(CMAKE_C_COMPILER   ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy)
set(CMAKE_AR ${TOOLCHAIN_PREFIX}ar)

# set compiler flags
set(MCU TM4C123GH6PM)
set(COMMON_FLAGS "-mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp \
    -ffunction-sections -fdata-sections -pedantic \
    -MD -DPART_${MCU} -DTARGET_IS_BLIZZARD_RA1")

set(CMAKE_C_FLAGS_DEBUG "-g -Wall ${COMMON_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -std=c++11 ${COMMON_FLAGS}")
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNOTEST ${COMMON_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -std=c++11 -DNOTEST ${COMMON_FLAGS}")

# search for programs in the build host directories
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# add TivaWare header files to the project
include_directories(${PROJECT_SOURCE_DIR}/../tivaware)

# add source files to the project
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

# set linker flags
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS)
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS)
set_target_properties(${PROJECT_NAME}
    PROPERTIES
    LINK_FLAGS "-T ${MCU}.ld --entry ResetISR --gc-sections"
)

# define objcopy macro
macro(OBJCOPY_FILE EXE_NAME)
    set(FO ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}.bin)
    set(FI ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME})
    message(STATUS ${FO})
    add_custom_command(
        OUTPUT ${FO}
        COMMAND ${CMAKE_OBJCOPY}
        ARGS -O binary -I elf32-little ${FI} ${FO}
        DEPENDS ${FI}
    )
    get_filename_component(TGT "${EXE_NAME}" NAME)
    add_custom_target("target-objcopy_${TGT}" ALL DEPENDS ${FO} VERBATIM)
    get_directory_property(extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
    set_directory_properties(
        PROPERTIES
        ADDITIONAL_MAKE_CLEAN_FILES "${extra_clean_files};${FO}"
    )
    set_source_files_properties("${FO}" PROPERTIES GENERATED TRUE)
endmacro(OBJCOPY_FILE)

# set the objcopy for binary file
objcopy_file(${PROJECT_NAME})

It passes the CMake step but when I try to compile using make, I get

arm-none-eabi-g++: error: unrecognized command line option '--gc-sections'

I guess the linkers flags should be used with arm-none-eabi-ld instead. How do I do this ?

Edit1:

I still have no idea how to set correct linker exe and flags but I found that CMake generates a file in firmware.dir/link.txt. Its content is

~/Documents/crh-2016/src/tiva/firmware/../toolchain/bin/arm-none-eabi-g++   -O2 -std=c++11 -fno-exceptions -DNOTEST -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp     -ffunction-sections -fdata-sections -pedantic     -MD -DPART_TM4C123GH6PM -DTARGET_IS_BLIZZARD_RA1    -T ~/Documents/crh-2016/src/tiva/firmware/TM4C123GH6PM.ld --entry ResetISR --gc-sections CMakeFiles/firmware.dir/main.cpp.o CMakeFiles/firmware.dir/startup_gcc.cpp.o  -o firmware  

And I edited it to what I want to fix temporary this issue

~/Documents/crh-2016/src/tiva/firmware/../toolchain/bin/arm-none-eabi-ld -T ~/Documents/crh-2016/src/tiva/firmware/TM4C123GH6PM.ld --entry ResetISR --gc-sections CMakeFiles/firmware.dir/main.cpp.o CMakeFiles/firmware.dir/startup_gcc.cpp.o  -o firmware

But it seems that LD doesn't like .o files generated by G++ because a make says

toolchain/bin/arm-none-eabi-ld: warning: cannot find entry symbol ResetISR; defaulting to 00000000

回答1:

Turning my comment into an answer

Edit: As mentioned by in Marc Glisse in his comment you could pass linker flags in CMAKE_EXE_LINKER_FLAGS with -Wl,XXX, see e.g. cflags '-Wl,-export-dynamic' vs linker flags '-export-dynamic'

Then you won't need to change the linker command to ld.


For the comparibility with your makefile you can use the CMAKE_LINKER and CMAKE_CXX_LINK_EXECUTABLE variables to change the linker command like call to ld.

Regarding your problems:

  • if the linker flags are not taken, just put them directly into the linker command line itself.
  • when getting "cannot find entry symbol"
    • you should check the map file if ResetISR is there
    • you could try to replace --gc-sections with --discard-none
    • if you get "cannot find entry symbol" during CMake's try compile step, then activate the ..._COMPILER_WORKS flags (see below)

I've taken your code and moved it to a toolchain file for readability and to demonstrate what have worked for me on other "bare-metal" cross-compiling:

TM4C123Toolchain.cmake

# this one is important
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(MCU TM4C123GH6PM)

# Optional for testing
#set(CMAKE_C_COMPILER_WORKS 1 CACHE INTERNAL "")
#set(CMAKE_CXX_COMPILER_WORKS 1 CACHE INTERNAL "")

# specify the toolchain
set(CMAKE_PREFIX_PATH "${PROJECT_SOURCE_DIR}/../toolchain/bin")
set(TOOLCHAIN_PREFIX "arm-none-eabi-")

# add processor specific definitions
add_definitions(
    -DPART_TM4C123GH6PM
    -DTARGET_IS_TM4C123_RA1
    -Dgcc
)

# add TivaWare header files to the project
set(TIVAWARE_PATH "${PROJECT_SOURCE_DIR}/../tivaware")
include_directories(${TIVAWARE_PATH})

#list(
#    APPEND _cxx_standard_libraries_list 
#        "-l${TIVAWARE_PATH}/usblib/gcc/libusb.a" 
#        "-l${TIVAWARE_PATH}/driverlib/gcc/libdriver.a"
#)
#unset(CMAKE_CXX_STANDARD_LIBRARIES CACHE)
#string(REPLACE ";" " " CMAKE_CXX_STANDARD_LIBRARIES_INIT "${_cxx_standard_libraries_list}")

set(CMAKE_CXX_COMPILER_ENV_VAR "")

unset(CMAKE_C_COMPILER CACHE)
find_program(CMAKE_C_COMPILER NAMES ${TOOLCHAIN_PREFIX}gcc)
unset(CMAKE_CXX_COMPILER CACHE)
find_program(CMAKE_CXX_COMPILER NAMES ${TOOLCHAIN_PREFIX}g++)

unset(CMAKE_ASM_COMPILER CACHE)
find_program(CMAKE_ASM_COMPILER NAMES ${TOOLCHAIN_PREFIX}as)
unset(CMAKE_OBJCOPY CACHE)
find_program(CMAKE_OBJCOPY NAMES ${TOOLCHAIN_PREFIX}objcopy)
unset(CMAKE_LINKER CACHE)
find_program(CMAKE_LINKER NAMES ${TOOLCHAIN_PREFIX}ld)

# set compiler flags
# NOTE: The following variables are cached by default (CMake<Lang>Information.cmake), 
#       so we have to prefill the cache with our values. They won't be overwritten.
set(CMAKE_C_FLAGS "-mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp \
    -ffunction-sections -fdata-sections -pedantic \
    -MD -DPART_${MCU} -DTARGET_IS_BLIZZARD_RA1" CACHE INTERNAL "" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11" CACHE INTERNAL "" FORCE)
set(CMAKE_C_FLAGS_DEBUG "-g -Wall" CACHE INTERNAL "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall" CACHE INTERNAL "" FORCE)
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNOTEST" CACHE INTERNAL "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNOTEST" CACHE INTERNAL "" FORCE)
set(CMAKE_ASM_FLAGS "-mthumb -mcpu=cortex-m4" CACHE INTERNAL "" FORCE)

set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf" CACHE INTERNAL "" FORCE)          

set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_LINKER> <CMAKE_CXX_LINK_FLAGS> -EL -n -Map=<TARGET_NAME>.map -T ${MCU}.ld --entry ResetISR --gc-sections -o <TARGET> --start-group <OBJECTS> <LINK_LIBRARIES> --end-group --cref")

set(CMAKE_C_USE_RESPONSE_FILE_FOR_OBJECTS 0)
set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_OBJECTS 0)

set(CMAKE_C_RESPONSE_FILE_LINK_FLAG "@")
set(CMAKE_CXX_RESPONSE_FILE_LINK_FLAG "@")

# search for programs in the build host directories
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Add call it with

cmake -DCMAKE_TOOLCHAIN_FILE:string=TM4C123Toolchain.cmake ...

Background

  • I added a map file with -Map to CMAKE_CXX_LINK_EXECUTABLE command line for debugging. And --start-group/--end-group because my distribution's standard libraries have cyclic dependencies.
  • I prefer CMAKE_PREFIX_PATH and find_program() because the paths and names may vary between toolchain vendors or versions (and both accept lists of paths/names).
  • I prefer to FORCE the variable settings, because I don't use the feature that a user can changed cached compiler settings and I prefer changes in the toolchain file to get active without having to run CMake from scratch again.
  • The _DEBUG and _RELEASE flags are appended by CMake to the standard flags.

References

  • cmake - Global linker flag setting (for all targets in directory)
  • What's the CMake syntax to set and use variables?


回答2:

I found a solution by using the -specs flag.

project(firmware)
cmake_minimum_required(VERSION 2.8)

# set cross compilation information
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

# specify the toolchain
set(TOOLCHAIN_PREFIX ${PROJECT_SOURCE_DIR}/../toolchain/bin/arm-none-eabi-)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++)
set(CMAKE_ASM_COMPILER ${TOOLCHAIN_PREFIX}as)
set(CMAKE_AR ${TOOLCHAIN_PREFIX}ar)
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy)
set(CMAKE_OBJDUMP ${TOOLCHAIN_PREFIX}objdump)

enable_language(ASM)

# set compiler flags
set(CPU "-mcpu=cortex-m4")
set(FPU "-mfpu=fpv4-sp-d16 -mfloat-abi=softfp")
set(CMAKE_ASM_FLAGS "-mthumb ${CPU} ${FPU} -MD")
set(CMAKE_C_FLAGS "-mthumb ${CPU} ${FPU} -std=gnu99 -Os -ffunction-sections -fdata-sections -MD -Wall -pedantic")
set(CMAKE_CXX_FLAGS "-mthumb ${CPU} ${FPU} -Os -ffunction-sections -fdata-sections -MD -Wall -pedantic -std=c++11 -fno-exceptions -fno-rtti")

# set linker flags
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
set(CMAKE_EXE_LINKER_FLAGS "-T${PROJECT_SOURCE_DIR}/tm4c123g.ld -specs=${PROJECT_SOURCE_DIR}/tiva.specs")

# add processor specific definitions
add_definitions(-DPART_TM4C123GH6PM)
add_definitions(-DTARGET_IS_TM4C123_RA1)
add_definitions(-Dgcc)

# add TivaWare header files to the project
set(TIVAWARE_PATH "${PROJECT_SOURCE_DIR}/../tivaware")
include_directories(${TIVAWARE_PATH})

# add source files to the project
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

# add linked library to the project
target_link_libraries(${PROJECT_NAME}
    ${TIVAWARE_PATH}/usblib/gcc/libusb.a
    ${TIVAWARE_PATH}/driverlib/gcc/libdriver.a
)

# define objcopy macro
macro(OBJCOPY_FILE EXE_NAME)
    set(FO ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}.bin)
    set(FI ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME})
    message(STATUS ${FO})
    add_custom_command(
        OUTPUT ${FO}
        COMMAND ${CMAKE_OBJCOPY}
        ARGS -O binary ${FI} ${FO}
        DEPENDS ${FI}
    )
    get_filename_component(TGT "${EXE_NAME}" NAME)
    add_custom_target("target-objcopy_${TGT}" ALL DEPENDS ${FO} VERBATIM)
    get_directory_property(extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
    set_directory_properties(
        PROPERTIES
        ADDITIONAL_MAKE_CLEAN_FILES "${extra_clean_files};${FO}"
    )
    set_source_files_properties("${FO}" PROPERTIES GENERATED TRUE)
endmacro(OBJCOPY_FILE)

# set the objcopy for binary file
objcopy_file(${PROJECT_NAME})

with tiva.specs:

*link:
--entry ResetISR --gc-sections 

*lib:
-lm -lc

and tm4c123g.ld as the linker script:

/******************************************************************************
 *
 * Linker configuration file.
 *
 * Copyright (c) 2012-2014 Texas Instruments Incorporated.  All rights reserved.
 * Software License Agreement
 * 
 * Texas Instruments (TI) is supplying this software for use solely and
 * exclusively on TI's microcontroller products. The software is owned by
 * TI and/or its suppliers, and is protected under applicable copyright
 * laws. You may not combine this software with "viral" open-source
 * software in order to form a larger program.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
 * NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
 * NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
 * CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
 * DAMAGES, FOR ANY REASON WHATSOEVER.
 * 
 * This is part of revision 2.1.0.12573 of the EK-TM4C123GXL Firmware Package.
 *
 *****************************************************************************/

MEMORY
{
    FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00040000
    SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00008000
}

SECTIONS
{
    .text :
    {
        _text = .;
        KEEP(*(.isr_vector))
        *(.text*)
        *(.rodata*)
        _etext = .;
    } > FLASH

    .data : AT(ADDR(.text) + SIZEOF(.text))
    {
        _data = .;
        *(vtable)
        *(.data*)
        _edata = .;
    } > SRAM

    .bss :
    {
        _bss = .;
        *(.bss*)
        *(COMMON)
        _ebss = .;
    } > SRAM
}