Difference between revisions of "Using Visual Studio Code for ARM Development – Build Tasks"
From EdWiki
m (→CMake CMakeLists.txt) |
m (→Using Visual Studio Code for ARM Development – Build Tasks) |
||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
== Using Visual Studio Code for ARM Development – Build Tasks == | == Using Visual Studio Code for ARM Development – Build Tasks == | ||
− | We are going to use [https://cmake.org/ CMake] with Make to build the project. [https://cmake.org/ CMake] needs some information where to find the tools. A simple and easy way is to to add a the following file to the project. I have named it ''arm-toolchain.cmake'' and placed it in the project root folder. | + | We are going to use [https://cmake.org/ CMake] with [https://www.gnu.org/software/make/ Make] to build the project. [https://cmake.org/ CMake] needs some information where to find the tools. A simple and easy way is to to add a the following file to the project. I have named it ''arm-toolchain.cmake'' and placed it in the project root folder. |
=== Toolchain CMake File === | === Toolchain CMake File === |
Revision as of 15:18, 22 March 2022
Using Visual Studio Code for ARM Development – Build Tasks
We are going to use CMake with Make to build the project. CMake needs some information where to find the tools. A simple and easy way is to to add a the following file to the project. I have named it arm-toolchain.cmake and placed it in the project root folder.
Toolchain CMake File
# Toolchain file for arm-none-eabi-gcc
#
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR ARM)
set(CMAKE_CROSSCOMPILING "TRUE")
set(ARM_TOOLCHAIN_DIR "C:/ProgramData/chocolatey/bin")
set(BINUTILS_PATH ${ARM_TOOLCHAIN_DIR})
set(TOOLCHAIN_PREFIX ${ARM_TOOLCHAIN_DIR}/arm-none-eabi-)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_C_COMPILER "${TOOLCHAIN_PREFIX}gcc.exe")
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PREFIX}g++.exe")
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy CACHE INTERNAL "objcopy tool")
set(CMAKE_SIZE_UTIL ${TOOLCHAIN_PREFIX}size CACHE INTERNAL "size tool")
set(CMAKE_FIND_ROOT_PATH ${BINUTILS_PATH})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
ARM_TOOLCHAIN_DIR specifies the compiler to be used. Additionally it defines extra tools as size and objcopy.
CMake CMakeLists.txt
To tell CMake what to do, create a file name CMakeList.txt in the project root.
# CMake file for tm4c123gxl
#
cmake_minimum_required(VERSION 3.15.3)
# Optional: print out extra messages to see what is going on. Comment it to have less verbose messages
set(CMAKE_VERBOSE_MAKEFILE ON)
# Path to toolchain file. This one has to be before 'project()' below
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/arm-toolchain.cmake)
# Setup project, output and linker file
project(blinky VERSION 0.1)
set(EXECUTABLE ${PROJECT_NAME}.elf)
set(LINKER_FILE ${CMAKE_SOURCE_DIR}/ld/tm4c123gh6pm.ld)
enable_language(C ASM)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
# Optional: issue a message to be sure it uses the correct toolchain file.
message(STATUS "CMAKE_TOOLCHAIN_FILE is: ${CMAKE_TOOLCHAIN_FILE}")
# List of source files
set(SRC_FILES
src/main.c
src/tm4c_startup.c
inc/tm4c_startup.h
)
# Build the executable based on the source files
add_executable(${EXECUTABLE} ${SRC_FILES})
# List of compiler defines, prefix with -D compiler option
target_compile_definitions(${EXECUTABLE} PRIVATE
-DPART_TM4C123GH6PM
)
# List of include directories
target_include_directories(${EXECUTABLE} PRIVATE
src
inc
"E:/ti/TivaWare_C_Series-2.2.0.295"
)
# Compiler options
target_compile_options(${EXECUTABLE} PRIVATE
-mcpu=cortex-m4
-mthumb
-mfpu=fpv4-sp-d16
-mfloat-abi=hard
-fdata-sections
-ffunction-sections
-Wall
-O0
-g3
)
# Linker options
target_link_options(${EXECUTABLE} PRIVATE
-T${LINKER_FILE}
-mcpu=cortex-m4
-mthumb
-mfpu=fpv4-sp-d16
-mfloat-abi=hard
-specs=nano.specs
-specs=nosys.specs
-lc
-lm
-Wl,-Map=${PROJECT_NAME}.map,--cref
-Wl,--gc-sections
-Xlinker -print-memory-usage -Xlinker
)
# Optional: Print executable size as part of the post build process
add_custom_command(TARGET ${EXECUTABLE}
POST_BUILD
COMMAND ${CMAKE_SIZE_UTIL} ${EXECUTABLE})
# Optional: Create hex, bin and S-Record files after the build
add_custom_command(TARGET ${EXECUTABLE}
POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O srec --srec-len=64 ${EXECUTABLE} ${PROJECT_NAME}.s19
COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE} ${PROJECT_NAME}.hex
COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE} ${PROJECT_NAME}.bin)
The most important sections/entries are:
- project(<your project name here>): Give your project a name
- set(LINKER_FILE <your linker file here>): specify the linker file name
- set(SRC_FILES <your source files here>): list of source files to compile
- target_compile_definitions(${EXECUTABLE} PRIVATE <compiler defines here>): list of compiler #defines
- target_include_directories(${EXECUTABLE} PRIVATE <list of include dir>): list of include directories
- target_compile_options(${EXECUTABLE} PRIVATE <compiler options>): list of compiler options
- target_link_options(${EXECUTABLE} PRIVATE <linker options>): list of linker options
This completes setting up the configuration for the build.
CMake Build Output
[variant] Loaded new set of variants [kit] Successfully loaded 2 kits from C:\Users\shankarappa\AppData\Local\CMakeTools\cmake-tools-kits.json [proc] Executing command: C:\ProgramData\chocolatey\bin\arm-none-eabi-gcc.exe -v [main] Configuring folder: blinky [proc] Executing command: E:\msys64\mingw64\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\ProgramData\chocolatey\bin\arm-none-eabi-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\ProgramData\chocolatey\bin\arm-none-eabi-g++.exe -Se:/tm4c123gxl/blinky -Be:/tm4c123gxl/blinky/build -G "Unix Makefiles" [cmake] Not searching for unused variables given on the command line. [cmake] -- CMAKE_TOOLCHAIN_FILE is: E:/tm4c123gxl/blinky/arm-toolchain.cmake [cmake] -- Configuring done [cmake] -- Generating done [cmake] -- Build files have been written to: E:/tm4c123gxl/blinky/build [main] Configuring folder: blinky [proc] Executing command: E:\msys64\mingw64\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\ProgramData\chocolatey\bin\arm-none-eabi-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\ProgramData\chocolatey\bin\arm-none-eabi-g++.exe -Se:/tm4c123gxl/blinky -Be:/tm4c123gxl/blinky/build -G "Unix Makefiles" [cmake] Not searching for unused variables given on the command line. [cmake] -- CMAKE_TOOLCHAIN_FILE is: E:/tm4c123gxl/blinky/arm-toolchain.cmake [cmake] -- Configuring done [cmake] -- Generating done [cmake] -- Build files have been written to: E:/tm4c123gxl/blinky/build