From 87bb95a809f1bbd770ec3f0bc5489abfcd7fe91b Mon Sep 17 00:00:00 2001 From: Mats Klepsland Date: Tue, 26 Dec 2023 22:33:12 +0100 Subject: [PATCH 1/6] Use C11 to support older Linux distros C17 is too new for a lot of the distros out there, so use C11 instead. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 97648b7..c375eb0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ project( LANGUAGES C ) -set(CMAKE_C_STANDARD 17) +set(CMAKE_C_STANDARD 11) list(APPEND CMAKE_MODULE_PATH "${simpleconfig_SOURCE_DIR}/cmake") From bd25e8e1027cfdfa3b001b1517e35c42fc0d2308 Mon Sep 17 00:00:00 2001 From: Mats Klepsland Date: Tue, 26 Dec 2023 22:53:30 +0100 Subject: [PATCH 2/6] Build and test using both GCC and Clang in same container This saves having to download and install dependencies for every Ubuntu release twice. --- .github/workflows/ubuntu.yml | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 908367f..45f8474 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -20,12 +20,9 @@ jobs: strategy: matrix: release: ["22.04"] - compiler: ["gcc", "clang"] runs-on: ubuntu-latest container: image: ubuntu:${{ matrix.release }} - env: - CC: ${{ matrix.compiler }} steps: - name: Checkout repository uses: actions/checkout@v3 @@ -36,14 +33,26 @@ jobs: apt-get install -y --no-install-recommends clang build-essential \ cmake libyaml-dev libcmocka-dev - - name: Run CMake + - name: Run CMake (GCC) run: | - mkdir build && cd build - cmake -DCMAKE_BUILD_TYPE=Debug -DSCONF_BUILD_EXAMPLES=on \ - -DSCONF_ENABLE_TESTS=on -DSCONF_ENABLE_ASAN=on .. + mkdir build-gcc && cd build-gcc + CC=gcc cmake -DCMAKE_BUILD_TYPE=Debug -DSCONF_BUILD_EXAMPLES=on \ + -DSCONF_ENABLE_TESTS=on .. - - name: Build code - run: cmake --build build + - name: Run CMake (Clang) + run: | + mkdir build-clang && cd build-clang + CC=clang cmake -DCMAKE_BUILD_TYPE=Debug -DSCONF_BUILD_EXAMPLES=on \ + -DSCONF_ENABLE_TESTS=on .. + + - name: Build code (GCC) + run: cmake --build build-gcc + + - name: Build code (clang) + run: cmake --build build-clang + + - name: Run tests (GCC) + run: cmake --build build-gcc --target test - - name: Run tests - run: cmake --build build --target test + - name: Run tests (clang) + run: cmake --build build-clang --target test From b01f235cb5f08860ca6ba8dad5025cbc411d6ec1 Mon Sep 17 00:00:00 2001 From: Mats Klepsland Date: Thu, 28 Dec 2023 00:03:26 +0100 Subject: [PATCH 3/6] Define cmocka function assert_float_equal() if undefined Older versions of cmocka does not have the assert_float_equal() function, so define it if it is not defined, so we can run the tests on older OS releases. --- tests/sconf_tests.h | 54 ++++++++++++++++++++++++++++ tests/test_sconf_defaults.c | 1 + tests/test_sconf_env_read.c | 1 + tests/test_sconf_opts_parse.c | 1 + tests/test_sconf_set_and_get_float.c | 1 + tests/test_sconf_yaml_read.c | 1 + 6 files changed, 59 insertions(+) create mode 100644 tests/sconf_tests.h diff --git a/tests/sconf_tests.h b/tests/sconf_tests.h new file mode 100644 index 0000000..6204c92 --- /dev/null +++ b/tests/sconf_tests.h @@ -0,0 +1,54 @@ +/* Older versions of cmocka does not have this function defined, so + lets define it so we can run some tests on older OS releases! */ +#ifndef assert_float_equal +#include + +#include + +/* Function copied from cmocka 1.1.7 + (https://gitlab.com/cmocka/cmocka/-/blob/cmocka-1.1.7/src/cmocka.c#L1120) */ +/* Returns 1 if the specified float values are equal, else returns 0. */ +static int float_compare(const float left, + const float right, + const float epsilon) { + float absLeft; + float absRight; + float largest; + float relDiff; + + float diff = left - right; + diff = (diff >= 0.f) ? diff : -diff; + + // Check if the numbers are really close -- needed + // when comparing numbers near zero. + if (diff <= epsilon) { + return 1; + } + + absLeft = (left >= 0.f) ? left : -left; + absRight = (right >= 0.f) ? right : -right; + + largest = (absRight > absLeft) ? absRight : absLeft; + relDiff = largest * FLT_EPSILON; + + if (diff > relDiff) { + return 0; + } + return 1; +} + +void _assert_float_equal(const float a, const float b, const float epsilon) +{ + const int equal = float_compare(a, b, epsilon); + if (!equal) { + fail_msg("%f != %f\n", a, b); + } +} + +#define assert_float_equal(a, b, epsilon) \ + _assert_float_equal((float)a, \ + (float)b, \ + (float)epsilon) + +#endif /* assert_float_equal */ + diff --git a/tests/test_sconf_defaults.c b/tests/test_sconf_defaults.c index d58cf2b..5e68f53 100644 --- a/tests/test_sconf_defaults.c +++ b/tests/test_sconf_defaults.c @@ -7,6 +7,7 @@ #include +#include "sconf_tests.h" #include "sconf.h" static struct SConfMap default_map[] = { diff --git a/tests/test_sconf_env_read.c b/tests/test_sconf_env_read.c index 38cc5cc..b168f60 100644 --- a/tests/test_sconf_env_read.c +++ b/tests/test_sconf_env_read.c @@ -8,6 +8,7 @@ #include +#include "sconf_tests.h" #include "sconf.h" static void test_sconf_env_read_string(void **unused) diff --git a/tests/test_sconf_opts_parse.c b/tests/test_sconf_opts_parse.c index 4d08ba1..af6b356 100644 --- a/tests/test_sconf_opts_parse.c +++ b/tests/test_sconf_opts_parse.c @@ -7,6 +7,7 @@ #include +#include "sconf_tests.h" #include "sconf.h" void my_usage_callback(const char *usage, void *user) diff --git a/tests/test_sconf_set_and_get_float.c b/tests/test_sconf_set_and_get_float.c index 8d522c1..36be508 100644 --- a/tests/test_sconf_set_and_get_float.c +++ b/tests/test_sconf_set_and_get_float.c @@ -7,6 +7,7 @@ #include +#include "sconf_tests.h" #include "sconf.h" static void test_sconf_set_and_get_float(void **unused) diff --git a/tests/test_sconf_yaml_read.c b/tests/test_sconf_yaml_read.c index f5e1271..e0901c1 100644 --- a/tests/test_sconf_yaml_read.c +++ b/tests/test_sconf_yaml_read.c @@ -7,6 +7,7 @@ #include +#include "sconf_tests.h" #include "sconf.h" static void test_valid_yaml_string(void **unused) From 24ad2e0ddb94315e831780c327eef6636f97106b Mon Sep 17 00:00:00 2001 From: Mats Klepsland Date: Thu, 28 Dec 2023 00:22:16 +0100 Subject: [PATCH 4/6] Remove DESCRIPTION from CMakeLists.txt to support cmake < 3.8 --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c375eb0..768489a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.1) project( simpleconfig - DESCRIPTION "A simpler approach to configuration handling in projects" VERSION 1.0.1 LANGUAGES C ) From 2c153b0d8f39b08a0a820574dd26cbd2c6536126 Mon Sep 17 00:00:00 2001 From: Mats Klepsland Date: Thu, 28 Dec 2023 00:34:32 +0100 Subject: [PATCH 5/6] Silence warnings that cause noice in older GCC and Clang versions Older versions of GCC and Clang warns a lot when initializing structs with {0}. Silence these warnings with -Wno-missing-field-initializers -Wno-missing-braces. --- src/CMakeLists.txt | 6 +++--- tests/CMakeLists.txt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aee7411..2e2c363 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,10 +13,10 @@ set(simpleconfig_source yaml.c ) +set(simpleconfig_compile_options -Wall -Wextra -Wno-missing-field-initializers -Wno-missing-braces) + if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(simpleconfig_compile_options -Wall -Wextra -Werror) -else() - set(simpleconfig_compile_options -Wall -Wextra) + set(simpleconfig_compile_options ${simpleconfig_compile_options} -Werror) endif() find_package(yaml REQUIRED) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 87f1306..8cff93f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -24,7 +24,7 @@ set(SCONF_TESTS foreach(X IN LISTS SCONF_TESTS) add_executable(${X} ${X}.c) - target_compile_options(${X} PRIVATE -Wall) + target_compile_options(${X} PRIVATE -Wall -Wno-missing-field-initializers -Wno-missing-braces) target_link_libraries(${X} sconf ${CMOCKA_LIB}) add_test(NAME ${X} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${X} WORKING_DIRECTORY ${simpleconfig_SOURCE_DIR}/tests From 4e6be242b968025c8617b0fcc6f1c86c3359d2e3 Mon Sep 17 00:00:00 2001 From: Mats Klepsland Date: Thu, 28 Dec 2023 00:43:03 +0100 Subject: [PATCH 6/6] Add actions to build using Ubuntu 16.04, 18.04, 20.04 and 22.04 --- .github/workflows/ubuntu.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 45f8474..7535019 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -19,7 +19,7 @@ jobs: ubuntu: strategy: matrix: - release: ["22.04"] + release: ["16.04", "18.04", "20.04", "22.04"] runs-on: ubuntu-latest container: image: ubuntu:${{ matrix.release }}