From 63c4e8119e47c59f8497a23f116bc478a4e0e1be Mon Sep 17 00:00:00 2001 From: Andrey Romanina Date: Fri, 22 Mar 2024 11:45:06 +0300 Subject: [PATCH] dev(SF-242): added --version option --- src/CMakeLists.txt | 72 +++++++++++++++++++++++++++++++++------- src/RepositoryPath.cmake | 10 ++++++ src/Version.h | 37 +++++++++++++++++++++ src/main.cpp | 26 +++++++++++++++ 4 files changed, 133 insertions(+), 12 deletions(-) create mode 100644 src/RepositoryPath.cmake create mode 100644 src/Version.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1aa75d2..f4d45b6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -40,13 +40,52 @@ if(DEFINED BOARD) include(${SITARA_DEPOT}/compile/sitara_compile_flags.cmake) endif() -# ������� �������������� nlohman json � GCC7.1 +# Подгружаем внешние репозитории +include(FetchContent) +include(RepositoryPath.cmake) + +FetchContent_Declare( + cxxopts + GIT_REPOSITORY ${cxxopts_path} + GIT_TAG master +) +FetchContent_GetProperties(cxxopts) +if(NOT cxxopts_POPULATED) + FetchContent_Populate(cxxopts) + add_subdirectory(${cxxopts_SOURCE_DIR} ${cxxopts_BINARY_DIR}) +endif() + + +# Получаем информацию о последнем Git Tag. +execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + OUTPUT_VARIABLE GIT_TAG + OUTPUT_STRIP_TRAILING_WHITESPACE +) + +# Получаем информацию о текущем коммите. +execute_process( + COMMAND ${GIT_EXECUTABLE} show-ref --head -s -- "head" + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + OUTPUT_VARIABLE GIT_HEAD + OUTPUT_STRIP_TRAILING_WHITESPACE +) + +# Определяем сведения о версии. +set(GIT_VERSION_INFO GIT_TAG="${GIT_TAG}" GIT_HEAD="${GIT_HEAD}") + + +# убирает предупреждение nlohman json о GCC7.1 add_compile_options(-Wno-psabi) -#���� �� ���������� � ������� ������� undefined reference pthread, �� �������� ���� �������: +#Если не собирается с ошибкой линкера undefined reference pthread, то добавить флаг линкера: #add_link_options(-lrt) add_executable(${TARGET_NAME} ./main.cpp ${SRC_FILES}) +# Записываем сведения о версии. +target_compile_definitions(${TARGET_NAME} PRIVATE ${GIT_VERSION_INFO}) + target_include_directories(${TARGET_NAME} PRIVATE ./ ${INC_DIRS} @@ -56,14 +95,23 @@ target_include_directories(${TARGET_NAME} PRIVATE ) if (CMAKE_BUILD_TYPE STREQUAL Debug) - set(LIB_LIST profinetd osald ${Boost_LIBRARIES}) + set(LIB_LIST + profinetd + osald + ${Boost_LIBRARIES} + cxxopts) elseif(CMAKE_BUILD_TYPE STREQUAL Release) - set(LIB_LIST profinet osal ${Boost_LIBRARIES}) + set(LIB_LIST + profinet + osal + ${Boost_LIBRARIES} + cxxopts) endif() -# ������� �������������� nlohman json � GCC7.1 +# убирает предупреждение nlohman json о GCC7.1 add_compile_options(-Wno-psabi) + set(LIB_ARCH_DIR x86_64) if(DEFINED BOARD) @@ -82,10 +130,10 @@ endif() target_link_directories(${TARGET_NAME} PUBLIC ${LINK_PATH}) -#���� �� ���������� � ������� ������� undefined reference pthread, �� ����� ${Boost_LIBRARIES} �������� pthread ��� -lpthread: +#Если не собирается с ошибкой линкера undefined reference pthread, то после ${Boost_LIBRARIES} добавить pthread или -lpthread: #target_link_libraries (${TARGET_NAME} PUBLIC profinet osal ${Boost_LIBRARIES} pthread) -# ��� BeagleBoneAI: ������ GCC8 ����� ��������� stdc++fs(���������� ) +# Для BeagleBoneAI: версия GCC8 нужно указывать stdc++fs(библиотека ) if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9) set(LIB_LIST ${LIB_LIST} stdc++fs) endif() @@ -98,13 +146,13 @@ install( ) install(FILES - # ����������� �������� ������� ��������� ���������� ����. - # ���� �� ����������, �� � ������ pnet ����� fork ��������� exit � - # ��������� ����������� ��������, ��� ����� �������� � �������������� ������������. + # Копирование заглушки скрипта установки параметров сети. + # Если не копировать, то в недрах pnet после fork вызовется exit и + # и вызовутся деструкторы объектов, что может привести к непредвиденным последствиям. ${CMAKE_CURRENT_SOURCE_DIR}/set_network_parameters - #����������� ����� ������������ + #Копирование файла конфигурации ${CMAKE_CURRENT_SOURCE_DIR}/program_configure.json - #����������� ��������� ����� ������������ ���������� Profinet + #Копирование тестового файла конфигурации устройства Profinet ${CMAKE_CURRENT_SOURCE_DIR}/profinet_device_configure.json DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/bin ) \ No newline at end of file diff --git a/src/RepositoryPath.cmake b/src/RepositoryPath.cmake new file mode 100644 index 0000000..704bdfa --- /dev/null +++ b/src/RepositoryPath.cmake @@ -0,0 +1,10 @@ +# Specify the path to dependencies' git repositories + +cmake_path(SET cxxopts_path "git@sofdev:External/cxxopts.git") + +if ( + NOT DEFINED cxxopts_path + ) + + message(FATAL_ERROR "Path to the dependency git repository is not specified") +endif() diff --git a/src/Version.h b/src/Version.h new file mode 100644 index 0000000..764baf4 --- /dev/null +++ b/src/Version.h @@ -0,0 +1,37 @@ +#ifndef UFC_ASYNCSERIALCONNECTOR_VERSION_H +#define UFC_ASYNCSERIALCONNECTOR_VERSION_H + +#include + +namespace asc { + /** + * Содержит сведения о текущей версии ПО. + */ + class Version { + public: + /** + * Получить текущую версию. + */ + static std::string GetVersion(){ + return !GitTag.empty() + ? GitTag + " " + GitHead + : GitHead; + } + + private: + /** + * Последний TAG текущей ветки. + */ + static const std::string GitTag; + + /** + * Hash текущего коммита. + */ + static const std::string GitHead; + }; +} + +const std::string asc::Version::GitTag = GIT_TAG; +const std::string asc::Version::GitHead = GIT_HEAD; + +#endif // UFC_ASYNCSERIALCONNECTOR_VERSION_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e4cb932..e6336f3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,14 +2,40 @@ #include "profinet.hpp" #include #include +#include #include "program_config.hpp" #include "app.hpp" +#include "Version.h" App app; int main(int argc, char * argv[]) { + cxxopts::Options options( + "ProfinetConnector", + "Profinet io device driver. Designed for profinet controller interaction via ethernet\n" + ); + + options.set_width(160); + + options.add_options() + ("help", "Display this help", cxxopts::value()->default_value("false")) + ("version", "Show git head commit and tag", cxxopts::value()->default_value("false")); + + auto result = options.parse(argc, argv); + + if (result["help"].as()) { + return not(std::cout << options.help() << std::endl); + } + + // Выводим информацию о текущей версии. + if (result["version"].as()) { + std::cout << "version: " << asc::Version::GetVersion() << std::endl; + return 0; + } + + // app.Init("program_configure.json"); app.Run(); -- 2.51.0