iarsystems / cmake-tutorial

Build and test embedded software using the IAR C/C++ Compiler alongside CMake
https://iar.com
MIT License
78 stars 15 forks source link
arm avr build ci cmake compiler cortex-m cspy cspybat ctest ide msp430 rh850 risc-v rl78 rx test-automation testing toolchain trustzone

Building CMake projects with IAR

CMake is an open-source, cross-platform family of tools maintained and supported by Kitware. Among its many features, it essentially provides Makefile Generators and Ninja Generators which compose scripts for cross-compiling C/C++ embedded software projects based on one or more CMakeLists.txt configuration files.

This tutorial offers a short introduction for those seeking information on how to start using the IAR C/C++ Compiler together with CMake from the command line. While this guide is based on the IAR Build Tools for Arm version 9.50.1 on Linux, it should work with other supported IAR products with no or minimal changes.

Prerequisites

Before you begin, you will need to download and install the IAR product, CMake and then clone this repository.

1) Download, install and activate[^1] your IAR product

Product Evaluation IAR Customers (login required)
IAR Build Tools Contact us for Arm (or for others[^2])
IAR Embedded Workbench Download for Arm (or for others[^2])

2) Download and install CMake.

3) Clone this repository to your computer. For more information, see "Cloning a repository".

Building a Basic CMake Project

The most basic CMake project is an executable built from a single source code file. For simple projects like this, a CMakeLists.txt file with about half dozen of commands is all that is required.

Any project's topmost CMakeLists.txt must start by specifying a minimum CMake version using the cmake_minimum_required() command. This establishes policy settings and ensures that CMake functions used in the project are run with a compatible version of CMake.

To start a project, use the project() command to set the project name. This call is required with every project and should be called soon after cmake_minimum_required(). This command can also be used to specify other project level information such as the language(s) used or its version number.

Use the add_executable() command to tell CMake to create an executable using the specified source code files.

Then use target_sources() to list the source files required to build the target.

Use target_compile_options() for setting up the compiler options to build the target.

And finally, set your target's linker options with target_link_options():

# set the minimum required version of CMake to be 3.20
cmake_minimum_required(VERSION 3.20)

# set the project name
project(Tutorial)

# add the executable target
add_executable(tutorial)

# target sources
target_sources(tutorial PRIVATE tutorial.c)

# compiler options
target_compile_options(tutorial PRIVATE --cpu=cortex-m4)

# linker options
target_link_options(tutorial PRIVATE
  --cpu=cortex-m4
  --semihosting)

Enabling the IAR Compiler

CMake uses the host platform's default compiler. When cross-compiling embedded applications, the compiler must be set manually via CMAKE_<lang>_COMPILER variables for each supported language. Additionally, it is possible to specify a build tool via [CMAKE_MAKE_PROGRAM]():

Variable Description Examples
CMAKE_C_COMPILER Must point to the C Compiler executable "C:/Program Files/..../arm/bin/iccarm.exe"
"/opt/iarsystems/bxarm/arm/bin/iccarm"
CMAKE_CXX_COMPILER Must point to the C++ Compiler executable "C:/Program Files/..../arm/bin/iccarm.exe"
"/opt/iarsystems/bxarm/arm/bin/iccarm"
CMAKE_ASM_COMPILER Must point to the Assembler executable "C:/Program Files/..../arm/bin/iasmarm.exe"
"/opt/iarsystems/bxarm/arm/bin/iasmarm"
CMAKE_MAKE_PROGRAM Must point to the build tool executable "C:/Program Files/..../common/bin/ninja.exe"
"/opt/iarsystems/bxarm/common/bin/ninja"

During the configuration phase, CMake reads these variables from:

Configure and Build

We are ready to build our first project! Run CMake to configure the project and then build it with your chosen build tool.

Run

Let's test the application. To run the executable you will need the non-interactive[^3] command line interface for the IAR C-SPY Debugger (cspybat) with the proper drivers for the desired target. Amongst the many ways of accomplishing this, let's take advantage of the add_test() for testing the application in a Arm Cortex-M4 simulated target.

This section is interactive. In this example we will use Arm. So, you will need to update your Tutorial's CMakeLists.txt:

Summary

This tutorial covered the basics on using CMake with the IAR tools from the command line. Proceed to the wiki for additional interactive examples, tips & tricks!

Follow us on GitHub to get updates about tutorials like this and more.

Issues

For reporting CMake software defects use the CMake Issue Tracker.

For technical support contact IAR Customer Support.

For questions related to this tutorial: try the wiki or check earlier issues. If those don't help, create a new issue with detailed information.

[^1]: For more information, see the "Installation and Licensing" guide for your product. If you do not have a license, contact us. [^2]: CMake has built-in IAR C/C++ Compiler support for the following non-Arm architectures: 8051, AVR, MSP430, RH850, RISC-V, RL78, RX, STM8 and V850. [^3]: For interactively debugging of executable files (*.elf) using the C-SPY Debugger from the IAR Embedded Workbench IDE, read this wiki article.