c++cmakezephyr-rtoscpputest

How to use CMAKE to have differrent #defines for multiple tests


I am using Zephyr for a project and write tests by using cpputest. In my code I have some buffers. These buffers depend on the hardware underneath (in this case FLASH NAND page size), we can use zephyr to make this variable:

uint8_t buffer[DT_PROP(DT_NODELABEL(flashexternal), user_bytes_per_page)];

The part between in the end becomes #define, in this case:

#define DT_N_NODELABEL_flashexternal_P_user_bytes_per_page 2048

or

#define DT_N_NODELABEL_flashexternal_P_user_bytes_per_page 4096

I want to run the same tests with different defines (so that my code gets tested with both length of buffers).

My CMakeLists.txt looks something like this (shortened for this post):

cmake_minimum_required(VERSION 3.26.0)

project(unit-tests)
set(CPPUTEST_LDFLAGS CppUTest CppUTestExt)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2a -include ${CMAKE_CURRENT_SOURCE_DIR}/autoconf.h")
set(test_main "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
set(TESTS_LIST "ftl_pagesize_4kb;ftl_pagesize_2kb")

set(ftl_pagesize_4kb_test_sources
    ../../app/src/storage/test/ftl-test.cpp
    mocks/zephyr/misc.cpp
    ../../app/src/storage/ftl.cpp
    ../../app/src/storage/mt29f.cpp
)


set(ftl_pagesize_2kb_test_sources
    ../../app/src/storage/test/ftl-test.cpp
    mocks/zephyr/misc.cpp
    ../../app/src/storage/ftl.cpp
    ../../app/src/storage/mt29f.cpp
)

foreach (test-item ${TESTS_LIST})
    message("testing ${test-item}")
    set(TEST_TARGET "${test-item}-test")

    # test main will run the tests, the test cases are collected from test sources
    add_executable(${TEST_TARGET} ${test_main} ${${test-item}_test_sources})
    target_link_libraries(${TEST_TARGET} ${CPPUTEST_LDFLAGS})

    target_include_directories(${TEST_TARGET} PUBLIC
        "../../app/src/", JADA, JADA, JADA
    )
    set(COMPILE_AND_LINK_FLAGS "-O0 -fprofile-arcs --coverage -fno-exceptions -m32 -g")

    if (test-item STREQUAL "ftl_pagesize_2kb")
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2a -include ${CMAKE_CURRENT_SOURCE_DIR}/../../app/src/storage/test/flash_nand_2kpage.hpp")
    endif()

    if (test-item STREQUAL "ftl_pagesize_4kb")
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2a -include ${CMAKE_CURRENT_SOURCE_DIR}/../../app/src/storage/test/flash_nand_4kpage.hpp")
    endif()

    set_target_properties(${TEST_TARGET}
          PROPERTIES
          COMPILE_FLAGS "${COMPILE_AND_LINK_FLAGS}"
          LINK_FLAGS "${COMPILE_AND_LINK_FLAGS}"
    )

    # run the test executable and save the output in junit format so it can be parsed later
    # this step also spits out the gcda files needed for coverage reports
     add_custom_target(${TEST_TARGET}-run ALL COMMAND ./${TEST_TARGET} -v -ojunit
         DEPENDS ${TEST_TARGET} VERBATIM
         COMMENT "executing ${TEST_TARGET}"
     )

endforeach()

As is visible in my CMakeLists.txt I attempt to overwrite CMAKE_CXX_FLAGS for my specific tests.

The content of flash_nand_2kpage.hpp looks like this ( the 4k length is similar with different values)

#include "../../../../tests/unit/autoconf.h"

#define DT_N_NODELABEL_flashexternal_P_user_bytes_per_page 2048
#define DT_N_NODELABEL_flashexternal_P_blocks_per_plane 1024
#define DT_N_NODELABEL_flashexternal_P_spare_bytes_per_page 128
#define DT_N_NODELABEL_flashexternal_P_pages_per_block 64
#define DT_N_NODELABEL_flashexternal_P_num_planes 2

I have also tried:

It seems that my knowledge in CMAKE is insufficient to fix this problem :frowning:

How can I build the same tests twice having some #defines set differently?


Solution

  • FOUND SOLUTION (after question was marked as duplicate)

    if (test-item STREQUAL "ftl_pagesize_4kb")
        set(COMPILE_AND_LINK_FLAGS "${COMPILE_AND_LINK_FLAGS} -include ${CMAKE_CURRENT_SOURCE_DIR}/../../app/src/storage/test/flash_nand_4kpage_only.hpp")
    else()
        set(COMPILE_AND_LINK_FLAGS "${COMPILE_AND_LINK_FLAGS} -include ${CMAKE_CURRENT_SOURCE_DIR}/../../app/src/storage/test/flash_nand_2kpage_only.hpp")
    endif()