From d907ee944778fe5ae4df96b12f0eaaa129ad9bce Mon Sep 17 00:00:00 2001 From: YZBruh Date: Wed, 23 Jul 2025 18:44:06 +0300 Subject: [PATCH] pmt: Improvement. - At build time, the version, compile time, CMake version, compiler version, and compiler flags are retrieved with the new CMake module and a header is created. This header provides more robust version management and notifications. - Build warnings fixed, code corrected for compilation. - Improvements were made to CMake code. - Modifications were made to the tests to read the library's version information. --- CMakeLists.txt | 15 ++++++---- build.sh | 11 +++---- cmake/generate_headers.cmake | 30 +++++++++++++++++++ include/buildInfo.hpp.in | 29 ++++++++++++++++++ srclib/libhelper/include/libhelper/lib.hpp | 8 ++--- srclib/libhelper/src/Checkers.cpp | 4 +-- srclib/libhelper/src/FileUtil.cpp | 4 +-- srclib/libhelper/src/Sha256.cpp | 2 +- srclib/libhelper/src/Utilities.cpp | 9 +++--- srclib/libhelper/tests/test.cpp | 2 ++ .../include/libpartition_map/lib.hpp | 4 --- srclib/libpartition_map/src/PartitionMap.cpp | 9 +++--- srclib/libpartition_map/tests/test.cpp | 2 ++ 13 files changed, 94 insertions(+), 35 deletions(-) create mode 100644 cmake/generate_headers.cmake create mode 100644 include/buildInfo.hpp.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f3a809..6457d09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,14 +18,19 @@ cmake_minimum_required(VERSION 3.10) project(pmt VERSION 1.0.0) -set(DEBUG_FLAGS -gdwarf-5 -fsanitize=address -fstack-protector -O0) - -# Add compiler flag based on build type +# Set compiler flags +add_compile_options(-Wall -Werror) if(CMAKE_BUILD_TYPE STREQUAL "Debug") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DEBUG_FLAGS}") - set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} ${DEBUG_FLAGS}") + add_compile_options(-gdwarf-5 -fsanitize=address -fstack-protector) endif() +# Add pmt's CMake module(s) +include(cmake/generate_headers.cmake) + +# Generate header(s) +get_property(FLAGS DIRECTORY PROPERTY COMPILE_OPTIONS) +generateBuildInfo(${FLAGS}) + # Add include directories include_directories(include srclib/libhelper/include srclib/libpartition_map/include) diff --git a/build.sh b/build.sh index 7028e7b..370f6c9 100644 --- a/build.sh +++ b/build.sh @@ -21,10 +21,7 @@ BUILD_64="build_arm64-v8a" BUILD_32="build_armeabi-v7a" THIS="$(basename $0)" -echo() -{ - command echo "[$THIS]: $*" -} +echo() { command echo "[$THIS]: $@"; } checks() { @@ -41,7 +38,11 @@ checks() clean() { echo "Cleaning workspace." - rm -rf $BUILD_32 $BUILD_64 srclib/libhelper/tests/dir srclib/libhelper/tests/linkdir srclib/libhelper/tests/file.txt + rm -rf $BUILD_32 $BUILD_64 \ + include/generated \ + srclib/libhelper/tests/dir \ + srclib/libhelper/tests/linkdir \ + srclib/libhelper/tests/file.txt } build() diff --git a/cmake/generate_headers.cmake b/cmake/generate_headers.cmake new file mode 100644 index 0000000..ff6ccba --- /dev/null +++ b/cmake/generate_headers.cmake @@ -0,0 +1,30 @@ +# +# Copyright 2025 Yağız Zengin +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Generate build info (version.hpp.in) +function(generateBuildInfo BUILD_FLAGS) + string(TIMESTAMP BUILD_DATE "%Y-%m-%d") + string(TIMESTAMP BUILD_TIME "%H:%M:%S") + + execute_process(COMMAND bash -c "mkdir -p ${CMAKE_SOURCE_DIR}/include/generated &>/dev/null") + execute_process(COMMAND bash -c "${CMAKE_CXX_COMPILER} --version | head -n 1" + OUTPUT_VARIABLE COMPILER_VERSION_STRING + OUTPUT_STRIP_TRAILING_WHITESPACE) + + configure_file(${CMAKE_SOURCE_DIR}/include/buildInfo.hpp.in ${CMAKE_SOURCE_DIR}/include/generated/buildInfo.hpp @ONLY) + + message(STATUS "Generated header: ${CMAKE_SOURCE_DIR}/include/generated/buildInfo.hpp") +endfunction() diff --git a/include/buildInfo.hpp.in b/include/buildInfo.hpp.in new file mode 100644 index 0000000..cf63a96 --- /dev/null +++ b/include/buildInfo.hpp.in @@ -0,0 +1,29 @@ +/* + Copyright 2025 Yağız Zengin + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#ifndef VERSION_HPP +#define VERSION_HPP + +// General build info +#define BUILD_VERSION "@PROJECT_VERSION@" +#define BUILD_TYPE "@CMAKE_BUILD_TYPE@" +#define BUILD_DATE "@BUILD_DATE@" +#define BUILD_TIME "@BUILD_TIME@" +#define BUILD_FLAGS "@BUILD_FLAGS@" +#define BUILD_CMAKE_VERSION "@CMAKE_VERSION@" +#define BUILD_COMPILER_VERSION "@COMPILER_VERSION_STRING@" + +#endif // #ifndef VERSION_HPP diff --git a/srclib/libhelper/include/libhelper/lib.hpp b/srclib/libhelper/include/libhelper/lib.hpp index db97da4..e604334 100644 --- a/srclib/libhelper/include/libhelper/lib.hpp +++ b/srclib/libhelper/include/libhelper/lib.hpp @@ -17,10 +17,6 @@ #ifndef LIBHELPER_LIB_HPP #define LIBHELPER_LIB_HPP -#define LIBHELPER_MAJOR 1 -#define LIBHELPER_MINOR 0 -#define LIBHELPER_PATCH 0 - #include #include #include @@ -114,11 +110,11 @@ bool eraseDirectoryRecursive(const std::string_view directory); // Getters size_t fileSize(const std::string_view file); -std::string_view readSymlink(const std::string_view entry); +std::string readSymlink(const std::string_view entry); // SHA-256 bool sha256Compare(const std::string_view file1, const std::string_view file2); -std::optional sha256Of(const std::string_view path); +std::optional sha256Of(const std::string_view path); // Utilities bool copyFile(const std::string_view file, const std::string_view dest); diff --git a/srclib/libhelper/src/Checkers.cpp b/srclib/libhelper/src/Checkers.cpp index dfababd..3fc1da5 100644 --- a/srclib/libhelper/src/Checkers.cpp +++ b/srclib/libhelper/src/Checkers.cpp @@ -74,8 +74,8 @@ bool isHardLink(const std::string_view entry) bool areLinked(const std::string_view entry1, const std::string_view entry2) { - const std::string_view st1 = (isSymbolicLink(entry1)) ? readSymlink(entry1) : entry1; - const std::string_view st2 = (isSymbolicLink(entry2)) ? readSymlink(entry2) : entry2; + const std::string st1 = (isSymbolicLink(entry1)) ? readSymlink(entry1) : std::string(entry1.data()); + const std::string st2 = (isSymbolicLink(entry2)) ? readSymlink(entry2) : std::string(entry2.data()); return (st1 == st2); } diff --git a/srclib/libhelper/src/FileUtil.cpp b/srclib/libhelper/src/FileUtil.cpp index 6bda2e4..abdc569 100644 --- a/srclib/libhelper/src/FileUtil.cpp +++ b/srclib/libhelper/src/FileUtil.cpp @@ -223,13 +223,13 @@ bool eraseDirectoryRecursive(const std::string_view directory) return true; } -std::string_view readSymlink(const std::string_view entry) +std::string readSymlink(const std::string_view entry) { char target[PATH_MAX]; ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1)); if (len == -1) { throw Error("Cannot read symlink %s: %s", entry.data(), strerror(errno)); - return entry; + return entry.data(); } target[len] = '\0'; diff --git a/srclib/libhelper/src/Sha256.cpp b/srclib/libhelper/src/Sha256.cpp index a5badb9..991cd53 100644 --- a/srclib/libhelper/src/Sha256.cpp +++ b/srclib/libhelper/src/Sha256.cpp @@ -25,7 +25,7 @@ namespace Helper { -std::optional sha256Of(const std::string_view path) +std::optional sha256Of(const std::string_view path) { if (!fileIsExists(path)) { throw Error("Is not exists or not file: %s", path.data()); diff --git a/srclib/libhelper/src/Utilities.cpp b/srclib/libhelper/src/Utilities.cpp index f274d73..4a78086 100644 --- a/srclib/libhelper/src/Utilities.cpp +++ b/srclib/libhelper/src/Utilities.cpp @@ -24,6 +24,7 @@ #include #include #include +#include namespace Helper { @@ -130,11 +131,9 @@ std::string pathDirname(const std::string_view entry) std::string getLibVersion() { - return std::string( - std::to_string(LIBHELPER_MAJOR) + "." - + std::to_string(LIBHELPER_MINOR) + "." - + std::to_string(LIBHELPER_PATCH) - ); + char vinfo[512]; + sprintf(vinfo, "libhelper %s [%s %s]\nBuildType: %s\nCMakeVersion: %s\nCompilerVersion: %s\nBuildFlags: %s\n", BUILD_VERSION, BUILD_DATE, BUILD_TIME, BUILD_TYPE, BUILD_CMAKE_VERSION, BUILD_COMPILER_VERSION, BUILD_FLAGS); + return std::string(vinfo); } } // namespace Helper diff --git a/srclib/libhelper/tests/test.cpp b/srclib/libhelper/tests/test.cpp index b71a73a..46f345e 100644 --- a/srclib/libhelper/tests/test.cpp +++ b/srclib/libhelper/tests/test.cpp @@ -88,6 +88,8 @@ int main(int argc, char** argv) std::cout << "pathJoin() test 3: " << Helper::pathJoin("mydir/", "/dir2") << std::endl; std::cout << "pathJoin() test 4: " << Helper::pathJoin("mydir", "/dir2") << std::endl; + std::cout << Helper::getLibVersion() << std::endl; + LOG(INFO) << "Info message" << std::endl; LOG(WARNING) << "Warning message" << std::endl; LOG(ERROR) << "Error message" << std::endl; diff --git a/srclib/libpartition_map/include/libpartition_map/lib.hpp b/srclib/libpartition_map/include/libpartition_map/lib.hpp index 0dc3d74..5e0903d 100644 --- a/srclib/libpartition_map/include/libpartition_map/lib.hpp +++ b/srclib/libpartition_map/include/libpartition_map/lib.hpp @@ -17,10 +17,6 @@ #ifndef LIBPARTITION_MAP_LIB_HPP #define LIBPARTITION_MAP_LIB_HPP -#define LIBPARTITION_MAP_MAJOR 1 -#define LIBPARTITION_MAP_MINOR 0 -#define LIBPARTITION_MAP_PATCH 0 - #include #include #include diff --git a/srclib/libpartition_map/src/PartitionMap.cpp b/srclib/libpartition_map/src/PartitionMap.cpp index 66838d1..a11b9de 100644 --- a/srclib/libpartition_map/src/PartitionMap.cpp +++ b/srclib/libpartition_map/src/PartitionMap.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -199,11 +200,9 @@ bool basic_partition_map_builder::operator!() const std::string getLibVersion() { - return std::string( - std::to_string(LIBPARTITION_MAP_MAJOR) + "." - + std::to_string(LIBPARTITION_MAP_MINOR) + "." - + std::to_string(LIBPARTITION_MAP_PATCH) - ); + char vinfo[512]; + sprintf(vinfo, "libpartition_map %s [%s %s]\nBuildType: %s\nCMakeVersion: %s\nCompilerVersion: %s\nBuildFlags: %s\n", BUILD_VERSION, BUILD_DATE, BUILD_TIME, BUILD_TYPE, BUILD_CMAKE_VERSION, BUILD_COMPILER_VERSION, BUILD_FLAGS); + return std::string(vinfo); } } // namespace PartitionMap diff --git a/srclib/libpartition_map/tests/test.cpp b/srclib/libpartition_map/tests/test.cpp index 9cf436a..952f49b 100644 --- a/srclib/libpartition_map/tests/test.cpp +++ b/srclib/libpartition_map/tests/test.cpp @@ -69,6 +69,8 @@ int main(void) { if (MyMap == MyMap2) std::cout << "map1 = map2" << std::endl; if (MyMap != MyMap2) std::cout << "map1 != map2" << std::endl; + + std::cout << PartitionMap::getLibVersion() << std::endl; } catch (PartitionMap::Error& error) { std::cerr << error.what() << std::endl; return 1;