Skip to content

Commit

Permalink
Using libuuid to generate uuids
Browse files Browse the repository at this point in the history
  • Loading branch information
nolan-veed authored and gavv committed Nov 22, 2024
1 parent 3edfe98 commit 7ccb022
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 3rdparty/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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')
Expand Down
14 changes: 14 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
])
Expand Down
21 changes: 21 additions & 0 deletions scripts/scons_helpers/build-3rdparty.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
36 changes: 36 additions & 0 deletions src/internal_modules/roc_core/target_libuuid/roc_core/uuid.cpp
Original file line number Diff line number Diff line change
@@ -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 <uuid/uuid.h>

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
File renamed without changes.
41 changes: 41 additions & 0 deletions src/tests/roc_core/test_uuid.cpp
Original file line number Diff line number Diff line change
@@ -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 <CppUTest/TestHarness.h>

#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

0 comments on commit 7ccb022

Please sign in to comment.