Actions

EmSys

Using Visual Studio Code for ARM Development – Build Tasks

From EdWiki

Revision as of 15:13, 22 March 2022 by Jshankar (Talk | contribs) (CMake CMakeLists.txt)

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

  1. # Toolchain file for arm-none-eabi-gcc
  2. #
  3. set(CMAKE_SYSTEM_NAME Generic)
  4. set(CMAKE_SYSTEM_PROCESSOR ARM)
  5. set(CMAKE_CROSSCOMPILING "TRUE")
  6.  
  7. set(ARM_TOOLCHAIN_DIR "C:/ProgramData/chocolatey/bin")
  8. set(BINUTILS_PATH ${ARM_TOOLCHAIN_DIR}) 
  9.  
  10. set(TOOLCHAIN_PREFIX ${ARM_TOOLCHAIN_DIR}/arm-none-eabi-)
  11.  
  12. set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
  13.  
  14. set(CMAKE_C_COMPILER "${TOOLCHAIN_PREFIX}gcc.exe")
  15. set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
  16. set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PREFIX}g++.exe")
  17.  
  18. set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy CACHE INTERNAL "objcopy tool")
  19. set(CMAKE_SIZE_UTIL ${TOOLCHAIN_PREFIX}size CACHE INTERNAL "size tool")
  20.  
  21. set(CMAKE_FIND_ROOT_PATH ${BINUTILS_PATH})
  22. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  23. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  24. 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.

  1. # CMake file for tm4c123gxl
  2. #
  3. cmake_minimum_required(VERSION 3.15.3)
  4.  
  5. # Optional: print out extra messages to see what is going on. Comment it to have less verbose messages
  6. set(CMAKE_VERBOSE_MAKEFILE ON)
  7.  
  8. # Path to toolchain file. This one has to be before 'project()' below
  9. set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/arm-toolchain.cmake)
  10.  
  11. # Setup project, output and linker file
  12. project(blinky VERSION 0.1)
  13. set(EXECUTABLE ${PROJECT_NAME}.elf)
  14. set(LINKER_FILE ${CMAKE_SOURCE_DIR}/ld/tm4c123gh6pm.ld)
  15.  
  16. enable_language(C ASM)
  17. set(CMAKE_C_STANDARD 99)
  18. set(CMAKE_C_STANDARD_REQUIRED ON)
  19. set(CMAKE_C_EXTENSIONS OFF)
  20.  
  21. # Optional: issue a message to be sure it uses the correct toolchain file.
  22. message(STATUS "CMAKE_TOOLCHAIN_FILE is: ${CMAKE_TOOLCHAIN_FILE}")
  23.  
  24. # List of source files
  25. set(SRC_FILES
  26.         src/main.c
  27.         src/tm4c_startup.c
  28.         inc/tm4c_startup.h
  29.         )
  30.  
  31. # Build the executable based on the source files
  32. add_executable(${EXECUTABLE} ${SRC_FILES})
  33.  
  34. # List of compiler defines, prefix with -D compiler option
  35. target_compile_definitions(${EXECUTABLE} PRIVATE
  36.         -DPART_TM4C123GH6PM
  37.         )
  38.  
  39. # List of include directories
  40. target_include_directories(${EXECUTABLE} PRIVATE
  41.         src 
  42.         inc
  43.         "E:/ti/TivaWare_C_Series-2.2.0.295"
  44.         )
  45.  
  46. # Compiler options
  47. target_compile_options(${EXECUTABLE} PRIVATE
  48.         -mcpu=cortex-m4
  49.         -mthumb
  50.         -mfpu=fpv4-sp-d16
  51.         -mfloat-abi=hard
  52.  
  53.         -fdata-sections
  54.         -ffunction-sections
  55.  
  56.         -Wall
  57.         -O0
  58.         -g3
  59.         )
  60.  
  61. # Linker options
  62. target_link_options(${EXECUTABLE} PRIVATE
  63.         -T${LINKER_FILE}
  64.         -mcpu=cortex-m4
  65.         -mthumb
  66.         -mfpu=fpv4-sp-d16
  67.         -mfloat-abi=hard
  68.         -specs=nano.specs
  69.         -specs=nosys.specs
  70.         -lc
  71.         -lm
  72.         -Wl,-Map=${PROJECT_NAME}.map,--cref
  73.         -Wl,--gc-sections
  74.         -Xlinker -print-memory-usage -Xlinker
  75.         )
  76.  
  77. # Optional: Print executable size as part of the post build process
  78. add_custom_command(TARGET ${EXECUTABLE}
  79.         POST_BUILD
  80.         COMMAND ${CMAKE_SIZE_UTIL} ${EXECUTABLE})
  81.  
  82. # Optional: Create hex, bin and S-Record files after the build
  83. add_custom_command(TARGET ${EXECUTABLE}
  84.         POST_BUILD
  85.         COMMAND ${CMAKE_OBJCOPY} -O srec --srec-len=64 ${EXECUTABLE} ${PROJECT_NAME}.s19
  86.         COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE} ${PROJECT_NAME}.hex
  87.         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