-
Notifications
You must be signed in to change notification settings - Fork 18
IAR Environment Variables in CMake
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.
- IAR Argument Variables
CMAKE_<lang>_COMPILERCMAKE_COMMANDPROJECT_NAMEPROJECT_SOURCE_DIRadd_custom_target()cmake_path()message()set()
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} |
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)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.
It is possible to render almost any desired IAR Argument Variables in a CMake project so that its environment becomes more familiar.
This is the cmake-tutorial wiki. Back to Wiki Home
- IAR Compiler options in a CMake project
- IAR ILINK options in a CMake project
- Language-specific target options
- Selecting build types
- Using Ninja Multi-Config
- Filing a build log
- Multi-file compilation
- Invoking IAR binary utilities
- Use the IAR ELF Tool to convert executable targets to their binary formats
- Using IAR Build Tools with CMake Presets