How to wrap a function via CMakeLists.txt

89 views Asked by At

I am implementing some unit test for zephyr and I need to mock a function that is included in a file that I can not remove from the test. So I am trying to use the -Wl,--wrap=symbol flag to create a wrapper that is called instead.

At GCC wrapper is shown that with a command gcc main.c foo.c -Wl,--wrap=foo -o main I would be able wrap foo function.

My problem is that Zephyr is mainly implemented with CMake and I am struggling to make it generate a call like that. How can I set my CMakeLists.txt so that I can link to files and use the wrap function?

I tried the following in my CMakeLists.txt:

# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(motion_monitor_test)

set(SOURCES
    file1_path.c
    file2_path.c
)
target_sources(app PRIVATE ${SOURCES})
target_compile_options(app PRIVATE "-Wl,--wrap=set_tx_power")

But instead of generating one call gcc -Wl,--wrap=set_tx_power -o CMakeFiles/app.dir/file.c.obj -c file1_path.c file2_path.c, it creates to separated calls: gcc -Wl,--wrap=set_tx_power -o CMakeFiles/app.dir/file1.c.obj -c file1_path.c and gcc -Wl,--wrap=set_tx_power -o CMakeFiles/app.dir/file2.c.obj -c file2_path.c.

0

There are 0 answers