IAR Environment Variables in CMake - iarsystems/cmake-tutorial GitHub Wiki

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 they are not automatically propagated to CMake projects.

Helpful Resources

Description

In your CMakeLists.txt use the following CMake commands after project() to get the corresponding IAR IDE Argument Variables counterparts, assuming that the project is using the C language:

# Get the IAR IDE Argument Variables in CMake
cmake_path(GET CMAKE_C_COMPILER PARENT_PATH COMPILER_DIR)
cmake_path(GET COMPILER_DIR PARENT_PATH TOOLKIT_DIR)
cmake_path(GET TOOLKIT_DIR PARENT_PATH EW_DIR)
set(PROJ_DIR "${PROJECT_SOURCE_DIR}")
set(PROJ_FNAME ${PROJECT_NAME})

Note

Referencing CMake Variables differs syntactically from referencing IAR Argument Variables:

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

Example

Now you can use these variables to reference those paths and resources in your CMake project. For example:

project(MyProject C ASM)
# ...
# Get the IAR IDE Argument Variables in CMake
# ...
target_link_options(MyTarget1 PRIVATE --semihosting --config=${TOOLKIT_DIR}/config/generic.icf)
# ...
target_link_options(MyTarget2 PRIVATE --vfe --map=. --config=${PROJ_DIR}/${PROJ_FNAME}.icf)

Target-oriented Argument Variables

CMake offers target-oriented Generator Expressions for accessing Target Artifacts information. For example, when using with a target named tgt:

set(TARGET_DIR $<TARGET_FILE_DIR:tgt>)
set(TARGET_PATH $<TARGET_FILE:tgt>)

These can be consumed directly by target-oriented commands such as target_compile_options(), target_compile_definitions() or target_link_options().

Tip

It is not possible to access the output of generator expressions at configure-time (e.g., via message()). One trick for debugging such expressions at build time is to rely on add_custom_target():

add_custom_target(genex-tgt-dir COMMAND ${CMAKE_COMMAND} -E echo "${TARGET_DIR}")
add_custom_target(genex-tgt-path COMMAND ${CMAKE_COMMAND} -E echo "${TARGET_PATH}")

Then, build with cmake --build ... --target genex-tgt-dir and cmake --build ... --target genex-tgt-path to print the result.

Summary

It is possible to render almost any desired IAR Argument Variables in a CMake project so that its environment becomes more familiar.

⚠️ **GitHub.com Fallback** ⚠️