diff --git a/CMakeLists.txt b/CMakeLists.txt index 1209ccac..b0adc7af 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,6 +107,22 @@ find_package(UUID REQUIRED) target_include_directories(${PROJECT_NAME} PRIVATE ${UUID_INCLUDE_DIR}) target_link_libraries(${PROJECT_NAME} PRIVATE ${UUID_LIBRARIES}) +try_compile(normal_uuid ${CMAKE_BINARY_DIR}/cmaketest + ${PROJECT_SOURCE_DIR}/cmake/tests/normal_uuid_lib_test.cc + LINK_LIBRARIES ${UUID_LIBRARIES} + CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${UUID_INCLUDE_DIR}") +try_compile(ossp_uuid ${CMAKE_BINARY_DIR}/cmaketest + ${PROJECT_SOURCE_DIR}/cmake/tests/ossp_uuid_lib_test.cc + LINK_LIBRARIES ${UUID_LIBRARIES} + CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${UUID_INCLUDE_DIR}") +if(normal_uuid) + add_definitions(-DUSE_OSSP_UUID=0) +elseif(ossp_uuid) + add_definitions(-DUSE_OSSP_UUID=1) +else() + message(FATAL_ERROR "uuid lib error") +endif() + find_package(ZLIB REQUIRED) target_include_directories(${PROJECT_NAME} PRIVATE ${ZLIB_INCLUDE_DIR}) target_link_libraries(${PROJECT_NAME} PRIVATE ${ZLIB_LIBRARIES}) diff --git a/cmake/tests/normal_uuid_lib_test.cc b/cmake/tests/normal_uuid_lib_test.cc new file mode 100644 index 00000000..4e535a4f --- /dev/null +++ b/cmake/tests/normal_uuid_lib_test.cc @@ -0,0 +1,7 @@ +#include +int main() +{ + uuid_t uu; + uuid_generate(uu); + return 0; +} \ No newline at end of file diff --git a/cmake/tests/ossp_uuid_lib_test.cc b/cmake/tests/ossp_uuid_lib_test.cc new file mode 100644 index 00000000..0e251e5c --- /dev/null +++ b/cmake/tests/ossp_uuid_lib_test.cc @@ -0,0 +1,8 @@ +#include +int main() +{ + uuid_t *uuid; + uuid_create(&uuid); + uuid_make(uuid, UUID_MAKE_V1); + return 0; +} \ No newline at end of file diff --git a/lib/src/Utilities.cc b/lib/src/Utilities.cc index 612eefbe..22328058 100644 --- a/lib/src/Utilities.cc +++ b/lib/src/Utilities.cc @@ -260,9 +260,22 @@ std::set splitStringToSet(const std::string &str, std::string getUuid() { +#if USE_OSSP_UUID + uuid_t *uuid; + uuid_create(&uuid); + uuid_make(uuid, UUID_MAKE_V4); + char *str{nullptr}; + size_t len{0}; + uuid_export(uuid, UUID_FMT_BIN, &str, &len); + uuid_destroy(uuid); + std::string ret(binaryStringToHex((const unsigned char *)str, len)); + free(str); + return ret; +#else uuid_t uu; uuid_generate(uu); return binaryStringToHex(uu, 16); +#endif } std::string base64Encode(const unsigned char *bytes_to_encode,