Using CMake with IAR Embedded Workbench

Technical Note 190701

Targets:
Arm, RL78, RH850, RX, AVR, MSP430, RISC-V

Component:
General

Updated:
11/6/2019 12:51 PM

Disclaimer(s)

IAR Systems does not support the CMake platform itself, that is, when it comes to problems using or setting up CMake, contact your provider.

A prerequisite is basic understanding of the CMake concept, as well as how to use the IAR build tools from command line.

Introduction

CMake is an open-source, cross-platform family of tools designed to build, test, and package software. It controls the software compilation process using platform and compiler independent configuration files. This Technical Note shows how to use CMake together with IAR Embedded Workbench.

Discussion

Tools to install

  • IAR Embedded Workbench for Arm, RL78, RH850, RX, AVR, RISC-V, or MSP430.
  • CMake v3.15.2 (or higher) which supports Arm, RL78, RH850, RX, AVR, RISC-V, and MSP430.
  • Microsoft NMake (CMake is able to generate different makefile and workspace formats and in this example we are using NMake)

For convenience, make sure that the path to the CMake and NMake executables are added to the Windows PATH environment variable. It allows you to run these executables without having to give the full paths.

Preparing the toolchain file (toolchain-ewarm.cmake)

In cross-compiling scenarios, a ‘toolchain file’ (.cmake) must be specified with information about the compiler, assembler, linker, and their respective option settings. The following file can be used for IAR Embedded Workbench for Arm version 8.x:

### BEGIN CMAKE_TOOLCHAIN_FILE
# "Generic" is used when cross compiling
set(CMAKE_SYSTEM_NAME Generic)

# Set the EW installation root directory
#(Avoid spaces in the path or you need to escape them)
set(EW_ROOT_DIR "c:/ew/arm-8323/arm")

# Compiler flags needed to compile for this CPU
set(CPU_FLAGS "--cpu ARM9E")

# Set up the CMake variables for compiler and assembler
# (The reason for both C and CXX variables is that CMake
# treats C and C++ tools individually)
set(CMAKE_C_COMPILER "${EW_ROOT_DIR}/bin/iccarm.exe" "${CPU_FLAGS} --dlib_config normal")
set(CMAKE_CXX_COMPILER "${EW_ROOT_DIR}/bin/iccarm.exe" "${CPU_FLAGS} --dlib_config normal")
set(CMAKE_ASM_COMPILER "${EW_ROOT_DIR}/bin/iasmarm.exe" "${CPU_FLAGS}")

# Set up the CMake variables for the linker
set(LINKER_SCRIPT "${EW_ROOT_DIR}/config/linker/ST/str91xFx44.icf")
set(CMAKE_C_LINK_FLAGS "--semihosting --config ${LINKER_SCRIPT}")
set(CMAKE_CXX_LINK_FLAGS "--semihosting --config ${LINKER_SCRIPT}")
### END CMAKE_TOOLCHAIN_FILE

The examples

The following two examples will use IAR Embedded Workbench for Arm, CMake, and NMake to build an executable file. The file structure is described, together with the contents of the CMakeLists.txt files.

Example 1 – Mixing C and assembler

This example shows how to build an application consisting of both assembler and C source files. It also shows how to set a preprocessor symbol in the CMake environment, to be used in the source files.

Function

An integer value returned from an assembler function is printed to the console, in the C main() function.

Example 2 – Creating and using libraries

In this example, a library is created followed by a demo project that uses this library.

Function

A library is built that includes a function that returns an integer (input value plus one). In the next step, an application is built that uses this library to print the value to the console, in the C main() function.

Build the examples

Build each one of the examples above like this:

1.    Run CMake

Browse to the example folder and use this CMake command line to create a makefile in the build folder:

cmake -H. -Bbuild -G "NMake Makefiles" -DCMAKE_TOOLCHAIN_FILE=toolchain-ewarm.cmake

2.    Run NMake

Browse to the build folder and use this command line to create the executable file my_program.elf:

Nmake Makefile

Debugging

The example downloads above include batch files for building the applications, as well as executing them in C-SPY batch mode via the command line utility cspybat.exe.

You can also use the IAR Embedded Workbench IDE to load C-SPY with an application that was built outside the IDE, for example applications built with CMake. Such a “Debug only” project can be reused whenever you rebuild your executable file and the only project options required to be specified are those that are debugger-related. See this Technical Note for further instructions.

Conclusion

This tech note gives basic knowledge of building with CMake for IAR Embedded Workbench and gives suggestions on how to debug an image built with CMake.

CMake is currently adapted to support IAR Embedded Workbench for Arm, RL78, RH850, RX, AVR, RISC-V, or MSP430. In the future, the CMake team might add support for other IAR Embedded Workbench products.

All product names are trademarks or registered trademarks of their respective owners.

© IAR Systems 1995-2019 - All rights reserved.

We use cookies on this website to provide you with a better experience. You need to accept cookies to continue using this site. Cookies