From 42db405cd67d8a49f738f3ab89d5a4b68232354e Mon Sep 17 00:00:00 2001 From: Naresh Nayak Date: Thu, 19 Sep 2024 03:47:22 +0000 Subject: [PATCH] Updated CMake build to selectively build unit tests and check for availability of cmocka library. Signed-off-by: Naresh Nayak --- CMakeLists.txt | 37 +++++++++++++++++++++---------------- README.md | 8 +++++++- test_all.sh | 10 ++++++---- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 030b1a9..eaa81a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,22 +96,27 @@ target_link_libraries(hello-world-listener open1722 open1722examples) enable_testing() -# find_package(cmocka 1.1.0 REQUIRED) - -list(APPEND TEST_TARGETS test-aaf) -list(APPEND TEST_TARGETS test-avtp) -list(APPEND TEST_TARGETS test-can) -list(APPEND TEST_TARGETS test-crf) -list(APPEND TEST_TARGETS test-cvf) -list(APPEND TEST_TARGETS test-rvf) -# list(APPEND TEST_TARGETS test-stream) - -foreach(TEST_TARGET IN LISTS TEST_TARGETS) - add_executable(${TEST_TARGET} "unit/${TEST_TARGET}.c") - target_include_directories(${TEST_TARGET} PRIVATE "include") - target_link_libraries(${TEST_TARGET} open1722 cmocka m) - add_test(NAME ${TEST_TARGET} COMMAND "${PROJECT_BINARY_DIR}/${TEST_TARGET}") -endforeach() +if (UNIT_TESTING) + + find_package(cmocka 1.1.0 REQUIRED) + + if (cmocka_FOUND) + list(APPEND TEST_TARGETS test-aaf) + list(APPEND TEST_TARGETS test-avtp) + list(APPEND TEST_TARGETS test-can) + list(APPEND TEST_TARGETS test-crf) + list(APPEND TEST_TARGETS test-cvf) + list(APPEND TEST_TARGETS test-rvf) + + foreach(TEST_TARGET IN LISTS TEST_TARGETS) + add_executable(${TEST_TARGET} "unit/${TEST_TARGET}.c") + target_include_directories(${TEST_TARGET} PRIVATE "include") + target_link_libraries(${TEST_TARGET} open1722 cmocka m) + add_test(NAME ${TEST_TARGET} COMMAND "${PROJECT_BINARY_DIR}/${TEST_TARGET}") + endforeach() + endif(cmocka_FOUND) +endif(UNIT_TESTING) + #### Install ################################################################## diff --git a/README.md b/README.md index aee5ea0..eec2941 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ This repository is organized as follows: Before building Open1722 make sure you have installed the following software : * CMake >= 3.20 -* CMocka >= 1.1.0 +* CMocka >= 1.1.0 (For running unit tests. This can be installed on Debian/Ubuntu using ```sudo apt install libcmocka-dev```) Alternatively, you can use VS Code to run the provided dev container which takes care of the dependencies. @@ -61,6 +61,12 @@ $ cmake .. $ make ``` +To execute available unit tests, set the Cmake variable UNIT_TESTING. +``` +$ cmake .. -DUNIT_TESTING=on +$ make +``` + The build can be cleaned using the following command: ``` $ make clean diff --git a/test_all.sh b/test_all.sh index ae10dbb..8f2662b 100755 --- a/test_all.sh +++ b/test_all.sh @@ -1,7 +1,9 @@ -#!/bin/bash -set -ev +#!/bin/bash +set -ev -./build_all.sh +rm -rf build +mkdir build cd build +cmake .. -DUNIT_TESTING=on +make -j`nproc` make test -# ninja coverage-html