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
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 theCMAKE_LINKER
andCMAKE_CXX_LINK_EXECUTABLE
variables to change the linker command like call told
.Regarding your problems:
map
file ifResetISR
is there--gc-sections
with--discard-none
..._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
Add call it with
Background
-Map
toCMAKE_CXX_LINK_EXECUTABLE
command line for debugging. And--start-group
/--end-group
because my distribution's standard libraries have cyclic dependencies.CMAKE_PREFIX_PATH
andfind_program()
because the paths and names may vary between toolchain vendors or versions (and both accept lists of paths/names).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._DEBUG
and_RELEASE
flags are appended by CMake to the standard flags.References
I found a solution by using the
-specs
flag.with tiva.specs:
and tm4c123g.ld as the linker script: