Skip to content

Commit abbf75f

Browse files
author
Felipe Torrezan
authored
Improved toolchain file (#17)
* Added improved toolchain file * Updated README.md
1 parent 047c5ea commit abbf75f

2 files changed

Lines changed: 49 additions & 55 deletions

File tree

README.md

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,15 @@ To build a project developed with the IAR Compiler using CMake we need at least
3333
### Configuring the toolchain file
3434
By default, CMake uses what it assumes to be the host platform's default compiler. When the application targets an embedded platform (known as cross-compiling), a toolchain file `<toolchain-file>.cmake` can be used to indicate the desired toolchain's location for its compiler and assembler. This section provides a simple generic template for the __IAR C/C++ Compilers__.
3535

36-
On the example toolchain file from the repository you have cloned, set the following variables to match the product installed in your system:
37-
* Set `CMAKE_SYSTEM_PROCESSOR`:
38-
39-
https://github.com/IARSystems/cmake-tutorial/blob/2c476dc240bf2c0c86bf144863eccdba0c0d38de/examples/iar-toolchain.cmake#L3-L5
40-
41-
* Set `IAR_INSTALL_DIR`:
42-
43-
https://github.com/IARSystems/cmake-tutorial/blob/2c476dc240bf2c0c86bf144863eccdba0c0d38de/examples/iar-toolchain.cmake#L7-L8
44-
45-
<details> <summary><b>Notes on IAR_INSTALL_DIR</b> (Click to unfold)</summary>
46-
47-
>Below you will find some general examples for Windows and Linux:
48-
>
49-
>| IAR_INSTALL_DIR example for | Windows tools | Linux tools |
50-
>| ------------------------------- | ----------------------------------------------------------- | ----------------------------------- |
51-
>| a typical installation location | `"C:/Program Files/IAR Systems/Embedded Workbench N.n"` | `"/opt/iarsystems/bx<arch>-N.nn.n"` |
52-
>| a custom installation location | `"C:/IAR_Systems/EWARM/N.nn.n"` | Not applicable |
53-
> * Replace `N.nn[.n]` or `<arch>` by the actual version or package of the active product.
54-
> * The __IAR_INSTALL_DIR__ is used to set the [__TOOLKIT_DIR__](https://github.com/IARSystems/cmake-tutorial/blob/v2022.06/examples/iar-toolchain.cmake#L17) variable.
55-
> * The __TOOLKIT_DIR__ variable is used to set the [PATH](https://github.com/IARSystems/cmake-tutorial/blob/v2022.06/examples/iar-toolchain.cmake#L20-L24) environment variable with the `bin` directory so that the compiler can be found on the search path. Setting the PATH with this method lasts for the time CMake is running.
56-
57-
</details>
36+
In the [examples/iar-toolchain.cmake](examples/iar-toolchain.cmake) file:
37+
* Set `TOOLKIT` variable to the compiler's target architecture.
38+
```cmake
39+
# Action: Set the `TOOLKIT` variable
40+
# Examples: arm, riscv, rh850, rl78, rx, stm8, 430, 8051, avr or v850
41+
# Alternative: override the default TOOLKIT_DIR (/path/to/installation/<arch>)
42+
set(TOOLKIT arm)
43+
```
44+
>:bulb: The default toolchain file will search for an available compiler in the default installation paths. Alternatively, the `TOOLKIT` variable can be used to set a specific installation directory (e.g., `C:/IAR_Systems/EWARM/N.nn`).
5845
5946
### A minimal project
6047
A CMake project is defined by one or more CMakeLists.txt file(s). Let's understand how a simple `hello-world` project can be configured for the Arm target architecture.

examples/iar-toolchain.cmake

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
11
# Toolchain File for the IAR C/C++ Compiler
22

3-
# Action: Set the `<arch>` to the compiler's target architecture
4-
# Examples: 430, 8051, arm, avr, riscv, rx, rl78, rh850, stm8 or v850
5-
set(CMAKE_SYSTEM_PROCESSOR <arch>)
3+
# Action: Set the `TOOLKIT` variable
4+
# Examples: arm, riscv, rh850, rl78, rx, stm8, 430, 8051, avr or v850
5+
# Alternative: override the default TOOLKIT_DIR (/path/to/installation/<arch>)
6+
set(TOOLKIT arm)
67

7-
# Action: Set the `IAR_INSTALL_DIR` to the tool installation path
8-
set(IAR_INSTALL_DIR "/path/to/install_dir")
8+
# Get the toolchain target from the TOOLKIT
9+
get_filename_component(CMAKE_SYSTEM_PROCESSOR ${TOOLKIT} NAME)
910

10-
# "Generic" is used when cross compiling
11-
set(CMAKE_SYSTEM_NAME Generic)
11+
# IAR C Compiler
12+
find_program(CMAKE_C_COMPILER
13+
NAMES icc${CMAKE_SYSTEM_PROCESSOR}
14+
PATHS ${TOOLKIT}
15+
"$ENV{ProgramFiles}/IAR Systems/*"
16+
"$ENV{ProgramFiles\(x86\)}/IAR Systems/*"
17+
/opt/iarsystems/bx${CMAKE_SYSTEM_PROCESSOR}
18+
PATH_SUFFIXES bin ${CMAKE_SYSTEM_PROCESSOR}/bin
19+
REQUIRED )
20+
21+
# IAR C++ Compiler
22+
find_program(CMAKE_CXX_COMPILER
23+
NAMES icc${CMAKE_SYSTEM_PROCESSOR}
24+
PATHS ${TOOLKIT}
25+
"$ENV{PROGRAMFILES}/IAR Systems/*"
26+
"$ENV{ProgramFiles\(x86\)}/IAR Systems/*"
27+
/opt/iarsystems/bx${CMAKE_SYSTEM_PROCESSOR}
28+
PATH_SUFFIXES bin ${CMAKE_SYSTEM_PROCESSOR}/bin
29+
REQUIRED )
30+
31+
# IAR Assembler
32+
find_program(CMAKE_ASM_COMPILER
33+
NAMES iasm${CMAKE_SYSTEM_PROCESSOR} a${CMAKE_SYSTEM_PROCESSOR}
34+
PATHS ${TOOLKIT}
35+
"$ENV{PROGRAMFILES}/IAR Systems/*"
36+
"$ENV{ProgramFiles\(x86\)}/IAR Systems/*"
37+
/opt/iarsystems/bx${CMAKE_SYSTEM_PROCESSOR}
38+
PATH_SUFFIXES bin ${CMAKE_SYSTEM_PROCESSOR}/bin
39+
REQUIRED )
1240

1341
# Avoids running the linker during try_compile()
1442
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
1543

16-
# Set a generic `TOOLKIT_DIR` location for the supported architectures
17-
set(TOOLKIT_DIR "${IAR_INSTALL_DIR}/${CMAKE_SYSTEM_PROCESSOR}")
18-
19-
# Add the selected IAR toolchain to the PATH (only while CMake is running)
20-
if (UNIX)
21-
set(ENV{PATH} "${TOOLKIT_DIR}/bin:$ENV{PATH}")
22-
else()
23-
set(ENV{PATH} "${TOOLKIT_DIR}/bin;$ENV{PATH}")
24-
endif()
25-
26-
# CMake requires individual variables for the C, C++ and Assembler
27-
# IAR C/C++ Compiler executable name
28-
set(CMAKE_C_COMPILER "icc${CMAKE_SYSTEM_PROCESSOR}")
29-
set(CMAKE_CXX_COMPILER "icc${CMAKE_SYSTEM_PROCESSOR}")
30-
31-
# Automatically set the IAR Assembler executable name
32-
# (depends on the toolchain's linker technology)
33-
list(APPEND _IAR_TOOLCHAINS_ILINK arm riscv rh850 rl78 rx stm8)
34-
if (${CMAKE_SYSTEM_PROCESSOR} IN_LIST _IAR_TOOLCHAINS_ILINK)
35-
# The Assembler executable for toolchains using the ILINK linker
36-
set(CMAKE_ASM_COMPILER "iasm${CMAKE_SYSTEM_PROCESSOR}")
37-
else()
38-
# The Assembler executable for toolchains using the XLINK linker
39-
set(CMAKE_ASM_COMPILER "a${CMAKE_SYSTEM_PROCESSOR}")
40-
endif()
41-
unset(_IAR_TOOLCHAINS_ILINK)
44+
# Set the TOOLKIT_DIR variable for the CMakeLists
45+
get_filename_component(BIN_DIR ${CMAKE_C_COMPILER} DIRECTORY)
46+
get_filename_component(TOOLKIT_DIR ${BIN_DIR} PATH)
47+
unset(BIN_DIR)
48+

0 commit comments

Comments
 (0)