Skip to content

IAR Environment Variables in CMake

Felipe Torrezan edited this page Feb 28, 2024 · 4 revisions

Introduction

The IAR Embedded Workbench IDE classic project format (*.ewp) provides internal environment variables such as for "Project directory" ($PROJ_DIR$) or for "Product directory" ($TOOLKIT_DIR$). These are called "Argument Variables" and are not automatically propagated to CMake projects.

Helpful Resources

Description

It is possible to use CMake Variables to mimic any desired IAR Argument Variables in a CMake Project so that its environment becomes more familiar.

Notice that referencing IAR Argument Variables is syntactically different from referencing CMake Variables:

IAR Argument Variable CMake Variable
$VARIABLE_NAME$ ${VARIABLE_NAME}

Example 1 - TOOLKIT_DIR

  • In your CMakeLists.txt set the following snippet to extract the parent directory of the selected compiler:
# Set the TOOLKIT_DIR variable for the CMakeLists
get_filename_component(BIN_DIR $ENV{CC} DIRECTORY)
get_filename_component(TOOLKIT_DIR ${BIN_DIR} PATH)
unset(BIN_DIR)
  • Then you can use the ${TOOLKIT_DIR} variable to reference that directory in your CMake project. For example:
target_link_options(MyTarget PRIVATE --semihosting --config ${TOOLKIT_DIR}/config/generic.icf)

Example 2 - PROJ_DIR

  • In CMake, PROJECT_SOURCE_DIR can be used to emulate PROJ_DIR:
set(PROJ_DIR ${PROJECT_SOURCE_DIR})
  • Then you can simply use the ${PROJ_DIR} variable to reference that directory in your CMake project. For example:
target_link_options(MyTarget PRIVATE --semihosting --config ${PROJ_DIR}/MyCustomLinkerConfiguration.icf)
Clone this wiki locally