~aleteoryx/muditaos

92e042add14cac3d9d1a6cdbf7a72eaf0749b33c — Lefucjusz 1 year, 3 months ago 80da585
[BH-2065] Fix race condition between CMake targets

* Fix for the issue that resulted in race
condition between target creating and
populating databases and the one copying
created databases to required directories.

The issue was caused by lack of dependency
between those targets, what allowed CMake
engine to parallelize them. As a result,
databases were copied before their creation
has been finished, what resulted in weird
discrepancies in databases content and
occasional rsync errors.

* Removed unused multicomp_install
function.

* Refactored DB tests readContent
helper.
M CMakeLists.txt => CMakeLists.txt +1 -1
@@ 54,7 54,7 @@ endif()

if (${ENABLE_TESTS})
    enable_testing()
    add_custom_target(check ${CMAKE_CTEST_COMMAND} -V DEPENDS create_test_product_databases)
    add_custom_target(check ${CMAKE_CTEST_COMMAND} -V)
    add_subdirectory(test)
    include(PureCoverage)
endif ()

M cmake/modules/Assets.cmake => cmake/modules/Assets.cmake +2 -2
@@ 28,8 28,8 @@ function(add_assets_target)
            ${_ASSETS_SYSTEM_DEST_DIR}

        # Create 'golden copy' of DBs
        COMMAND mkdir -p ${_ASSETS_SYSTEM_DEST_DIR}/db/factory
        COMMAND rsync -qlptgoDu
        # 'v' flag intentionally left for debugging purposes, can be removed if you're sure it's no longer needed
        COMMAND rsync -vlptgoDu
            ${_ASSETS_SYSTEM_DEST_DIR}/db/*
            ${_ASSETS_SYSTEM_DEST_DIR}/db/factory


M cmake/modules/DownloadAsset.cmake => cmake/modules/DownloadAsset.cmake +0 -2
@@ 17,8 17,6 @@ function(download_asset_release asset_name_in asset_name_out asset_repo asset_ve
    )

    add_custom_target(${asset_name_out}-target DEPENDS ${asset_repo})

#    multicomp_install(PROGRAMS ${CMAKE_BINARY_DIR}/${asset_repo} DESTINATION "./" COMPONENTS Standalone Update)
endfunction()

function(download_asset_release_json

M cmake/modules/Utils.cmake => cmake/modules/Utils.cmake +0 -14
@@ 1,17 1,3 @@
# An equivalent of install() which allows to declare multiple components using
# a custom 'COMPONENTS' clause. This clause must be the last on the
# argument list. The original 'COMPONENT' from install() clause must not appear
# on the argument list.  
function(multicomp_install)
    list(FIND ARGN "COMPONENTS" CLAUSE_INDEX)
    list(SUBLIST ARGN 0 ${CLAUSE_INDEX} INSTALL_ARGN)
    math(EXPR COMPS_INDEX "${CLAUSE_INDEX}+1")
    list(SUBLIST ARGN ${COMPS_INDEX} ${ARGC} COMPONENTS)
    foreach(COMP IN LISTS COMPONENTS)
        install(${INSTALL_ARGN} COMPONENT ${COMP})
    endforeach()
endfunction()

macro(print_var VARIABLE)
    if(${VARIABLE})
        message(STATUS "${Green}${VARIABLE}: '${Orange}${${VARIABLE}}${ColourReset}'")

M image/CMakeLists.txt => image/CMakeLists.txt +1 -0
@@ 5,6 5,7 @@ set(SYSTEM_DEST_DIR ${SYSROOT_PATH}/system_a)
set(USER_DEST_DIR ${SYSROOT_PATH}/user)

set(ASSETS_DEPENDENCIES "json-common-target")
list(APPEND ASSETS_DEPENDENCIES "create_product_databases")

if (${ASSETS_TYPE} STREQUAL "Proprietary")
    list(APPEND ASSETS_DEPENDENCIES "json-proprietary-target")

M module-db/tests/Helpers.cpp => module-db/tests/Helpers.cpp +16 -21
@@ 1,4 1,4 @@
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Helpers.hpp"


@@ 8,39 8,34 @@
#include <set>
#include <algorithm>
#include <utility>
#include <fstream>

namespace
{
    std::string readContent(const std::string &filename) noexcept
    std::string readContent(const std::string &filename)
    {
        auto getFileSize = [](FILE *fd) -> std::size_t {
            std::fseek(fd, 0, SEEK_END);
            const auto size = std::ftell(fd);
            std::rewind(fd);
            return size;
        };

        std::vector<char> fileContent;
        if (const auto fp = std::fopen(filename.c_str(), "r")) {
            const auto fileSize = getFileSize(fp);

            fileContent.reserve(fileSize + 1);
            std::fread(fileContent.data(), 1, fileSize, fp);
            std::fclose(fp);
            fileContent[fileSize] = '\0';
            return fileContent.data();
        std::ifstream file{filename};
        if (!file.is_open()) {
            return {};
        }
        return {};

        const auto fileSize = std::filesystem::file_size(filename);
        auto fileContent    = std::make_unique<char[]>(fileSize + 1);

        file.read(fileContent.get(), fileSize);
        fileContent[fileSize] = '\0';

        return std::string{fileContent.get()};
    }

    std::vector<std::string> readCommands(const std::filesystem::path &filePath)
    {
        const auto fileContent = readContent(filePath.c_str());
        const auto &fileContent = readContent(filePath.c_str());
        std::string currentStatement{};
        std::vector<std::string> statements{};

        std::string line{};
        for (const auto &c : fileContent) {
        for (const auto c : fileContent) {
            if (c != '\n') {
                line += c;
            }

M products/BellHybrid/CMakeLists.txt => products/BellHybrid/CMakeLists.txt +0 -2
@@ 105,7 105,6 @@ if (${PROJECT_TARGET} STREQUAL "TARGET_RT1051")
        SYSROOT sysroot
        DEPENDS
            install_scripts
            create_product_databases
            create_user_directories
            assets
            recovery.bin-target


@@ 121,7 120,6 @@ else()
        LUTS ""
        DEPENDS
            install_scripts
            create_product_databases
            remove_var_directory
            create_user_directories
            assets

M products/PurePhone/CMakeLists.txt => products/PurePhone/CMakeLists.txt +0 -2
@@ 121,7 121,6 @@ if (${PROJECT_TARGET} STREQUAL "TARGET_RT1051")
        SYSROOT sysroot
        DEPENDS
            install_scripts
            create_product_databases
            create_user_directories
            assets
            recovery.bin-target


@@ 136,7 135,6 @@ else()
        SYSROOT sysroot
        DEPENDS
            install_scripts
            create_product_databases
            remove_var_directory
            create_user_directories
            assets

M test/CMakeLists.txt => test/CMakeLists.txt +1 -1
@@ 46,7 46,7 @@ download_asset_json(json-test-proprietary-target
                    ${MUDITA_CACHE_DIR}
    )

set(ASSETS_DEPENDENCIES "")
set(ASSETS_DEPENDENCIES "create_test_product_databases")

if (${ASSETS_TYPE} STREQUAL "Proprietary")
    list(APPEND ASSETS_DEPENDENCIES "json-test-proprietary-target")