What's the best practice for a project that runs on ARM MCU while do unittest on PC?

I am currently working on a project that runs on an ARM Cortex-M4 MCU using the arm-none-eabi-gcc toolchain. However, the unit tests for this project are based on the regular gcc toolchain and run on a PC.

Initially, I defined a custom build type and used -DCMAKE_BUILD_TYPE=Test to build the unit tests. However, I found that this approach had some drawbacks. For instance, options such as -g were not added automatically, and I had to check CMAKE_BUILD_TYPE at multiple locations for different settings. Since I don’t know exactly how CMAKE_BUILD_TYPE works behind the scenes, there may be other issues that I haven’t discovered yet.

Therefore, I would like to know what the best practice is for building such an application.

It’s tricky, dealing with separate targets for the host system & target system, because you must use the same toolchain for the entire build.

CMake does support using an emulator to run tests, see the variable CMAKE_CROSSCOMPILING_EMULATOR. The workflow this supports is building everything for the target system, but running tests & custom commands on the host system (via this emulator).

You could also do something like using ExternalProject to run a child cmake build with a different toolchain, but that’s probably more trouble than just manually building that in a separate build tree.

1 Like

Thank you for your reply.
Using an emulator to run unit tests is indeed an option. The advantage is that it allows for the use of the same compiler and compilation options, resulting in better testing outcomes. However, the disadvantage is that it can be slower to run and may require additional work to handle standard library functions such as printf.

1 Like