Skip to content

Using IAR Build Tools with CMake Presets

Felipe Torrezan edited this page Nov 5, 2025 · 13 revisions

This article brings a configuration approach that enables developers to integrate the IAR C/C++ Compiler toolchain with CMake's preset system.

Description

CMakePresets.json, introduced in CMake 3.19, is an optional file that should be located at the top-level of your project and allows for reproducible and shareable build configurations without relying on environment variables or command-line arguments. This integration is particularly useful for cross-compilation to architectures like ARM Cortex-M, RISC-V, Renesas RL78, and Renesas RX, leveraging IAR's high-performance compilers while maintaining CMake's flexibility.

CMake Presets contain information how to configure, build and test a project. Below you can see an outline of how it looks like:

{
    "configurePresets": [
        {
            // ...
        }
    ],
    "buildPresets": [
        {
            // ...
        }
    ],
    "testPresets": [
        {
            // ...
        }
    ]
}

Template

Before entering CMake Presets, make sure your compiler is working properly as in the example below (for arm):

echo "main(){}" | /path/to/iccarm --output /tmp/test.o -

The template below comes pre-configured to work with cxarm-9.70.1 on Linux or Windows:

{
    "version": 10,
    "configurePresets": [
        {
            "name": "default",
            "hidden": true,
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Linux"
            },
            "environment": {
                "CC":  "/opt/iar/cxarm/arm/bin/iccarm",
                "CXX": "/opt/iar/cxarm/arm/bin/iccarm",
                "ASM": "/opt/iar/cxarm/arm/bin/iasmarm"
            }
        },
        {
            "name": "default",
            "hidden": true,
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Windows"
            },
            "environment": {
                "CC":  "C:/iar/cxarm-9.70.1/arm/bin/iccarm.exe",
                "CXX": "C:/iar/cxarm-9.70.1/arm/bin/iccarm.exe",
                "ASM": "C:/iar/cxarm-9.70.1/arm/bin/iasmarm.exe"
            }
        },
        {
            "name": "cxarm",
            "inherits": "default",
            "displayName": "CXARM",
            "description": "Configure project for CXARM",
            "generator": "Ninja Multi-Config",
            "binaryDir": "${sourceDir}/build",
            "cacheVariables": {
                "CMAKE_EXPORT_COMPILE_COMMANDS": true,
                "CMAKE_CONFIGURATION_TYPES": {
                    "type": "STRING",
                    "value": "Debug;Release;RelWithDebInfo;MinSizeRel;HighSpeed;MaxSpeed"
                },
                "CMAKE_C_FLAGS_HIGHSPEED": {
                    "type": "STRING",
                    "value": "-Ohs"
                },
                "CMAKE_CXX_FLAGS_HIGHSPEED": {
                    "type": "STRING",
                    "value": "-Ohs"
                },
                "CMAKE_C_FLAGS_MAXSPEED": {
                    "type": "STRING",
                    "value": "-Ohs --no_size_constraints"
                },
                "CMAKE_CXX_FLAGS_MAXSPEED": {
                    "type": "STRING",
                    "value": "-Ohs --no_size_constraints"
                }
            }
        }
    ],
    "buildPresets": [
        {
            "name": "debug",
            "displayName": "Debug",
            "description": "Disable optimizations - include debug information.",
            "configurePreset": "cxarm",
            "configuration": "Debug"
        },
        {
            "name": "release",
            "displayName": "Release",
            "description": "Optimize for speed - exclude debug information.",
            "configurePreset": "cxarm",
            "configuration": "Release"
        },
        {
            "name": "relwithdebinfo",
            "displayName": "RelWithDebInfo",
            "description": "Optimize for speed - include debug information.",
            "configurePreset": "cxarm",
            "configuration": "RelWithDebInfo"
        },
        {
            "name": "minsizerel",
            "displayName": "MinSizeRel",
            "description": "Optimize for smallest binary size - exclude debug information.",
            "configurePreset": "cxarm",
            "configuration": "MinSizeRel"
        },
                {
            "name": "highspeed",
            "displayName": "HighSpeed",
            "description": "Optimize for high execution speed - exclude debug information.",
            "configurePreset": "cxarm",
            "configuration": "HighSpeed"
        },
        {
            "name": "maxspeed",
            "displayName": "MaxSpeed",
            "description": "Optimize for maximum execution speed - exclude debug information.",
            "configurePreset": "cxarm",
            "configuration": "MaxSpeed"
        }
    ]
}

This template adds two custom build configuration for "High-Speed" and "Max Speed" which are of great use with the IAR C/C++ Compiler.

As an alternative to specifying the CC, CXX, and ASM environment variables within an "environment": { ... } block under "configurePresets", you can instead use a "toolchainFile": "${sourceDir}/path/to/iar-toolchain.cmake" entry. In the toolchain file, you can directly set variables for CMAKE_<lang>_COMPILER (e.g., CMAKE_C_COMPILER, CMAKE_CXX_COMPILER).

In addition, the CMakePresets.json file is also consumed by the CMake Tools VS Code Extension and integrates well with the lightweight IDE. A complete example can be seen in modern-workflow.

Building a Project with CMake Presets

Below you will find some simple sequence of commands on how to configure and build a project using CMake presets:

# Configure the project (Debug, Release, RelWithDebInfo, MinSizeRel, HighSpeed and MaxSpeed)
$ cmake --preset cxarm

# Build all configurations
$ cmake --build --preset debug
$ cmake --build --preset release
$ cmake --build --preset relwithdebinfo
$ cmake --build --preset minsizerel
$ cmake --build --preset highspeed
$ cmake --build --preset maxspeed

For inspecting the generated command lines for your project use cmake --build --preset <preset-name> --verbose.

Summary

CMake presets are a great way to programmatically setup the underlying host environment for use within cross-compiling scenarios, even if it is not mandatory for any CMake project per se (e.g., using a toolchain file). The proposed template can be used as an starting point, checked into version control, and extended as needed.

Related Resources

Clone this wiki locally