diff --git a/3rdparty/SConscript b/3rdparty/SConscript index dd5338989..a7a9960d3 100644 --- a/3rdparty/SConscript +++ b/3rdparty/SConscript @@ -19,6 +19,7 @@ thirdparty_versions = { 'json-c': '0.12-20140410', 'libatomic_ops': '7.6.10', 'libunwind': '1.2.1', + 'libuuid': '2.40.1', 'libuv': '1.35.0', 'ltdl': '2.4.6', 'openfec': '1.4.2.11', @@ -132,6 +133,21 @@ elif 'libunwind' in system_dependencies: env = conf.Finish() +# dep: libuuid +if 'libuuid' in autobuild_dependencies: + env.BuildThirdParty(thirdparty_versions, 'libuuid') + +elif 'libuuid' in system_dependencies: + conf = Configure(env, custom_tests=env.CustomTests) + + if not conf.AddPkgConfigDependency('uuid', '--cflags --libs'): + conf.env.AddManualDependency(libs=['uuid']) + + if not conf.CheckLibWithHeaderExt('uuid', 'uuid.h', 'C', run=not is_crosscompiling): + env.Die("libuuid not found (see 'config.log' for details)") + + env = conf.Finish() + # dep: libatomic_ops if 'libatomic_ops' in autobuild_dependencies: env.BuildThirdParty(thirdparty_versions, 'libatomic_ops') diff --git a/SConstruct b/SConstruct index ac2e8c93c..91836fae1 100644 --- a/SConstruct +++ b/SConstruct @@ -224,6 +224,11 @@ AddOption('--disable-libunwind', action='store_true', help='disable libunwind support required for printing backtrace') +AddOption('--disable-libuuid', + dest='disable_libuuid', + action='store_true', + help='disable libuuid support') + AddOption('--disable-alsa', dest='disable_alsa', action='store_true', @@ -815,6 +820,15 @@ else: 'target_libunwind', ]) + if meta.platform in ['linux'] and not GetOption('disable_libuuid'): + env.Append(ROC_TARGETS=[ + 'target_libuuid', + ]) + else: + env.Append(ROC_TARGETS=[ + 'target_nouuid', + ]) + env.Append(ROC_TARGETS=[ 'target_libuv', ]) diff --git a/scripts/scons_helpers/build-3rdparty.py b/scripts/scons_helpers/build-3rdparty.py index bec792135..1f2faac32 100644 --- a/scripts/scons_helpers/build-3rdparty.py +++ b/scripts/scons_helpers/build-3rdparty.py @@ -1170,6 +1170,27 @@ def die(text, *args): execute_make(ctx) install_files(ctx, 'include/*.h', ctx.pkg_inc_dir) install_files(ctx, 'src/.libs/libunwind.a', ctx.pkg_lib_dir) +elif ctx.pkg_name == 'libuuid': + download( + ctx, + 'https://github.com/util-linux/util-linux/archive/refs/tags/v{ctx.pkg_ver}.tar.gz', + 'util-linux-{ctx.pkg_ver}.tar.gz') + unpack(ctx, + 'util-linux-{ctx.pkg_ver}.tar.gz', + 'util-linux-{ctx.pkg_ver}') + changedir(ctx, 'util-linux-{ctx.pkg_ver}') + execute(ctx, './autogen.sh') + execute(ctx, './configure --disable-all-programs --enable-libuuid --host={host} {vars} {flags} {opts}'.format( + host=ctx.toolchain, + vars=format_vars(ctx), + flags=format_flags(ctx, cflags='-fcommon -fPIC'), + opts=' '.join([ + '--disable-shared', + '--enable-static', + ]))) + execute_make(ctx) + install_files(ctx, 'libuuid/uuid.h', ctx.pkg_inc_dir) + install_files(ctx, 'src/.libs/libuuid.a', ctx.pkg_lib_dir) elif ctx.pkg_name == 'openfec': if ctx.variant == 'debug': setattr(ctx, 'res_dir', 'bin/Debug') diff --git a/src/internal_modules/roc_core/target_libuuid/roc_core/uuid.cpp b/src/internal_modules/roc_core/target_libuuid/roc_core/uuid.cpp new file mode 100644 index 000000000..57069021d --- /dev/null +++ b/src/internal_modules/roc_core/target_libuuid/roc_core/uuid.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Roc Streaming authors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "roc_core/uuid.h" + +#include "roc_core/panic.h" + +#include + +namespace roc { +namespace core { + +// Uses libuuid to generate the uuid. +bool uuid_generare(char* buf, size_t buf_sz) { + if (!buf) { + roc_panic("uuid: buffer is null"); + } + if (buf_sz < UuidLen + 1) { + roc_panic("uuid: buffer too small"); + } + + uuid_t u; + + uuid_generate(u); + uuid_unparse_lower(u, buf); + + return true; +} + +} // namespace core +} // namespace roc diff --git a/src/internal_modules/roc_core/uuid.cpp b/src/internal_modules/roc_core/target_nouuid/roc_core/uuid.cpp similarity index 100% rename from src/internal_modules/roc_core/uuid.cpp rename to src/internal_modules/roc_core/target_nouuid/roc_core/uuid.cpp diff --git a/src/tests/roc_core/test_uuid.cpp b/src/tests/roc_core/test_uuid.cpp new file mode 100644 index 000000000..a63aab4cd --- /dev/null +++ b/src/tests/roc_core/test_uuid.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 Roc Streaming authors + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include "roc_core/uuid.h" + +namespace roc { +namespace core { + +TEST_GROUP(uuid) {}; + +TEST(uuid, generate) { + char a_uuid[UuidLen + 1] = {}; + + CHECK(uuid_generare(a_uuid, sizeof(a_uuid)) == true); + CHECK(a_uuid[8] == '-'); + CHECK(a_uuid[13] == '-'); + CHECK(a_uuid[18] == '-'); + CHECK(a_uuid[23] == '-'); + CHECK(a_uuid[UuidLen] == '\0'); +} + +TEST(uuid, generated_with_bigger_buffer) { + char a_uuid[UuidLen + 1 + 4] = {}; + + CHECK(uuid_generare(a_uuid, sizeof(a_uuid)) == true); + CHECK(a_uuid[8] == '-'); + CHECK(a_uuid[13] == '-'); + CHECK(a_uuid[18] == '-'); + CHECK(a_uuid[23] == '-'); + CHECK(a_uuid[UuidLen] == '\0'); +} + +} // namespace core +} // namespace roc