From 52c648ab166b2acc3b57ae2cdfd5f90a98322ac2 Mon Sep 17 00:00:00 2001 From: Nick Logozzo Date: Sat, 27 Jan 2024 15:21:22 -0500 Subject: [PATCH] Revert back to libuuid --- .github/workflows/linux.yml | 2 +- .github/workflows/windows.yml | 2 +- CMakeLists.txt | 16 ++++++--- README.md | 2 +- cmake/FindLibuuid.cmake | 17 +++++++++ cmake/config.cmake.in | 40 ++++++++++++++------- include/helpers/stringhelpers.h | 4 --- src/helpers/stringhelpers.cpp | 61 +++++++++++++++++++++++++++++++-- 8 files changed, 119 insertions(+), 25 deletions(-) create mode 100644 cmake/FindLibuuid.cmake diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 31a04e6..7e4963f 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -47,7 +47,7 @@ jobs: uses: johnwason/vcpkg-action@v5 id: vcpkg with: - pkgs: boost-locale curl gettext-libintl glib gtest jsoncpp libsecret maddy openssl stduuid + pkgs: boost-locale curl gettext-libintl glib gtest jsoncpp libsecret libuuid maddy openssl triplet: x64-linux cache-key: ${{ matrix.config.os }} revision: 7032c5759f823fc8699a5c37d5f7072464ccb9a8 diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 5d8ce68..1018e16 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -34,7 +34,7 @@ jobs: uses: johnwason/vcpkg-action@v5 id: vcpkg with: - pkgs: boost-locale curl gettext-libintl gtest jsoncpp maddy openssl stduuid + pkgs: boost-locale curl gettext-libintl gtest jsoncpp maddy openssl triplet: x64-windows cache-key: ${{ matrix.config.os }} revision: 7032c5759f823fc8699a5c37d5f7072464ccb9a8 diff --git a/CMakeLists.txt b/CMakeLists.txt index 16636c7..6fc5335 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,6 @@ include(CTest) #libnick Setup add_compile_definitions(SQLITE_HAS_CODEC) -add_compile_definitions(UUID_SYSTEM_GENERATOR) if(LINUX) add_compile_definitions(HAVE_USLEEP) endif() @@ -76,8 +75,7 @@ find_package(CURL REQUIRED) find_package(jsoncpp CONFIG REQUIRED) find_package(Intl REQUIRED) find_package(OpenSSL REQUIRED) -find_package(stduuid CONFIG REQUIRED) -target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost Boost::locale CURL::libcurl JsonCpp::JsonCpp Intl::Intl OpenSSL::SSL OpenSSL::Crypto stduuid) +target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost Boost::locale CURL::libcurl JsonCpp::JsonCpp Intl::Intl OpenSSL::SSL OpenSSL::Crypto) if(USING_VCPKG) find_package(unofficial-maddy CONFIG REQUIRED) target_link_libraries(${PROJECT_NAME} PUBLIC unofficial::maddy::maddy) @@ -97,6 +95,16 @@ elseif(LINUX) pkg_check_modules(gthread REQUIRED IMPORTED_TARGET gthread-2.0) pkg_check_modules(libsecret REQUIRED IMPORTED_TARGET libsecret-1) target_link_libraries(${PROJECT_NAME} PUBLIC Threads::Threads PkgConfig::glib PkgConfig::gio PkgConfig::gmodule PkgConfig::gobject PkgConfig::gthread PkgConfig::libsecret) + if(USING_VCPKG) + find_package(unofficial-libuuid CONFIG REQUIRED) + target_link_libraries(${PROJECT_NAME} PUBLIC unofficial::UUID::uuid) + else() + find_package(Libuuid REQUIRED) + if(Libuuid_FOUND) + target_include_directories(${PROJECT_NAME} INTERFACE ${Libuuid_INCLUDE_DIRS}) + target_link_libraries(${PROJECT_NAME} INTERFACE ${Libuuid_LIBRARIES}) + endif() + endif() endif() #libnick Install @@ -107,7 +115,7 @@ install(TARGETS ${PROJECT_NAME} EXPORT "${PROJECT_NAME}Targets" LIBRARY DESTINAT install(DIRECTORY "${CMAKE_SOURCE_DIR}/include/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}") install(FILES "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") install(EXPORT "${PROJECT_NAME}Targets" FILE "${PROJECT_NAME}Targets.cmake" NAMESPACE ${PROJECT_NAME}:: DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") -install(FILES "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake" "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") +install(FILES "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake" "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}ConfigVersion.cmake" "${CMAKE_SOURCE_DIR}/cmake/FindLibuuid.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") export(EXPORT "${PROJECT_NAME}Targets" FILE "${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}Targets.cmake" NAMESPACE ${PROJECT_NAME}::) #libnick Test diff --git a/README.md b/README.md index e2f45d4..004aa9c 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,12 @@ The following are a list of dependencies used by libnick. - maddy - openssl - Used for sqlcipher, as libnick manually includes and compiles sqlcipher. -- stduuid ### Linux Only The above dependencies must be installed, plus the following for linux systems: - glib - libsecret +- libuuid ## Consuming libnick via vcpkg libnick is available through `vcpkg`. diff --git a/cmake/FindLibuuid.cmake b/cmake/FindLibuuid.cmake new file mode 100644 index 0000000..c4b3cf2 --- /dev/null +++ b/cmake/FindLibuuid.cmake @@ -0,0 +1,17 @@ +find_path(Libuuid_INCLUDE_DIRS uuid/uuid.h) +find_library(Libuuid_LIBRARIES uuid) + +if(Libuuid_LIBRARIES AND Libuuid_INCLUDE_DIRS) + set(Libuuid_FOUND YES) + if(NOT Libuuid_FIND_QUIETLY) + message(STATUS "Found libuuid: ${Libuuid_LIBRARIES}") + endif() +else() + if(Libuuid_FIND_REQUIRED) + message(SEND_ERROR "Could NOT find libuuid") + else() + if(NOT Libuuid_FIND_QUIETLY) + message(STATUS "Could NOT find libuuid") + endif() + endif() +endif() \ No newline at end of file diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in index 2a078c9..10cc14a 100644 --- a/cmake/config.cmake.in +++ b/cmake/config.cmake.in @@ -1,24 +1,40 @@ @PACKAGE_INIT@ -include("${CMAKE_CURRENT_LIST_DIR}/libnickTargets.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") include(CMakeFindDependencyMacro) +if(DEFINED ENV{VCPKG_ROOT}) + file(TO_CMAKE_PATH $ENV{VCPKG_ROOT} VCPKG_ROOT) + if(EXISTS "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake") + set(USING_VCPKG ON) + endif() +endif() + find_dependency(Boost REQUIRED COMPONENTS locale) find_dependency(CURL REQUIRED) -find_dependency(jsoncpp REQUIRED) +find_dependency(jsoncpp CONFIG REQUIRED) find_dependency(Intl REQUIRED) find_dependency(OpenSSL REQUIRED) -find_dependency(unofficial-maddy REQUIRED) +if(USING_VCPKG) + find_dependency(unofficial-maddy REQUIRED) +else() + find_dependency(maddy REQUIRED) +endif() if(LINUX) - find_dependency(Threads REQUIRED) - find_dependency(PkgConfig REQUIRED) - pkg_check_modules(glib REQUIRED IMPORTED_TARGET glib-2.0) - pkg_check_modules(gio REQUIRED IMPORTED_TARGET gio-2.0) - pkg_check_modules(gmodule REQUIRED IMPORTED_TARGET gmodule-2.0) - pkg_check_modules(gobject REQUIRED IMPORTED_TARGET gobject-2.0) - pkg_check_modules(gthread REQUIRED IMPORTED_TARGET gthread-2.0) - pkg_check_modules(libsecret REQUIRED IMPORTED_TARGET libsecret-1) - find_dependency(unofficial-libuuid CONFIG REQUIRED) + find_dependency(Threads REQUIRED) + find_dependency(PkgConfig REQUIRED) + pkg_check_modules(glib REQUIRED IMPORTED_TARGET glib-2.0) + pkg_check_modules(gio REQUIRED IMPORTED_TARGET gio-2.0) + pkg_check_modules(gmodule REQUIRED IMPORTED_TARGET gmodule-2.0) + pkg_check_modules(gobject REQUIRED IMPORTED_TARGET gobject-2.0) + pkg_check_modules(gthread REQUIRED IMPORTED_TARGET gthread-2.0) + pkg_check_modules(libsecret REQUIRED IMPORTED_TARGET libsecret-1) + if(USING_VCPKG) + find_dependency(unofficial-libuuid CONFIG REQUIRED) + else() + list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}) + find_dependency(Libuuid REQUIRED) + endif() endif() check_required_components(libnick) \ No newline at end of file diff --git a/include/helpers/stringhelpers.h b/include/helpers/stringhelpers.h index db0db66..5299a66 100644 --- a/include/helpers/stringhelpers.h +++ b/include/helpers/stringhelpers.h @@ -3,10 +3,6 @@ #define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING -#ifndef UUID_SYSTEM_GENERATOR -#define UUID_SYSTEM_GENERATOR -#endif - #include #include #include diff --git a/src/helpers/stringhelpers.cpp b/src/helpers/stringhelpers.cpp index f30dab5..2073a20 100644 --- a/src/helpers/stringhelpers.cpp +++ b/src/helpers/stringhelpers.cpp @@ -8,7 +8,11 @@ #include #include #include -#include +#ifdef _WIN32 +#include +#elif defined(__linux__) +#include +#endif namespace Nickvision { @@ -85,7 +89,60 @@ namespace Nickvision std::string StringHelpers::newGuid() noexcept { - return uuids::to_string(uuids::uuid_system_generator{}()); + std::array guid; +#ifdef _WIN32 + GUID win; + CoInitializeEx(nullptr, COINIT_MULTITHREADED); + if (CoCreateGuid(&win) != S_OK) + { + return ""; + } + guid = { + (unsigned char)((win.Data1 >> 24) & 0xFF), + (unsigned char)((win.Data1 >> 16) & 0xFF), + (unsigned char)((win.Data1 >> 8) & 0xFF), + (unsigned char)((win.Data1) & 0xff), + + (unsigned char)((win.Data2 >> 8) & 0xFF), + (unsigned char)((win.Data2) & 0xff), + + (unsigned char)((win.Data3 >> 8) & 0xFF), + (unsigned char)((win.Data3) & 0xFF), + + (unsigned char)win.Data4[0], + (unsigned char)win.Data4[1], + (unsigned char)win.Data4[2], + (unsigned char)win.Data4[3], + (unsigned char)win.Data4[4], + (unsigned char)win.Data4[5], + (unsigned char)win.Data4[6], + (unsigned char)win.Data4[7] + }; +#elif defined(__linux__) + uuid_generate(guid.data()); +#endif + std::ostringstream out; + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[0]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[1]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[2]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[3]); + out << "-"; + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[4]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[5]); + out << "-"; + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[6]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[7]); + out << "-"; + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[8]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[9]); + out << "-"; + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[10]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[11]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[12]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[13]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[14]); + out << std::setfill('0') << std::setw(2) << std::hex << static_cast(guid[15]); + return out.str(); } bool StringHelpers::isValidUrl(const std::string& s) noexcept