Skip to content

Commit f710b2d

Browse files
Android and IOS support using <Random>
1 parent 3115802 commit f710b2d

3 files changed

Lines changed: 66 additions & 24 deletions

File tree

.github/workflows/ci.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ jobs:
6868
- name: Test
6969
run: ctest --test-dir build -C ${{ matrix.build_type }} --output-on-failure
7070

71-
# Android: cross-compile with NDK (no tests — no device)
71+
# Android: cross-compile with NDK (build only — no device to run tests)
7272
android:
7373
name: Android (${{ matrix.abi }})
7474
runs-on: ubuntu-latest
75-
timeout-minutes: 10
75+
timeout-minutes: 15
7676
strategy:
7777
fail-fast: false
7878
matrix:
@@ -81,6 +81,12 @@ jobs:
8181
steps:
8282
- uses: actions/checkout@v4
8383

84+
- name: Cache FetchContent dependencies
85+
uses: actions/cache@v4
86+
with:
87+
path: build/_deps
88+
key: android-${{ matrix.abi }}-deps-${{ hashFiles('tests/CMakeLists.txt') }}
89+
8490
- name: Configure
8591
run: >
8692
cmake -B build
@@ -89,15 +95,16 @@ jobs:
8995
-DANDROID_PLATFORM=android-24
9096
-DCMAKE_BUILD_TYPE=Release
9197
-DMAU_UUID_BUILD_EXAMPLE=ON
98+
-DMAU_UUID_BUILD_TESTS=ON
9299
93100
- name: Build
94101
run: cmake --build build
95102

96-
# iOS: cross-compile with Xcode toolchain (no tests — no device)
103+
# iOS: cross-compile with Xcode toolchain (build only — no device to run tests)
97104
ios:
98105
name: iOS (${{ matrix.arch }})
99106
runs-on: macos-latest
100-
timeout-minutes: 10
107+
timeout-minutes: 15
101108
strategy:
102109
fail-fast: false
103110
matrix:
@@ -106,6 +113,12 @@ jobs:
106113
steps:
107114
- uses: actions/checkout@v4
108115

116+
- name: Cache FetchContent dependencies
117+
uses: actions/cache@v4
118+
with:
119+
path: build/_deps
120+
key: ios-${{ matrix.arch }}-deps-${{ hashFiles('tests/CMakeLists.txt') }}
121+
109122
- name: Configure
110123
run: >
111124
cmake -B build
@@ -114,6 +127,7 @@ jobs:
114127
-DCMAKE_OSX_ARCHITECTURES=${{ matrix.arch }}
115128
-DCMAKE_OSX_DEPLOYMENT_TARGET=15.0
116129
-DMAU_UUID_BUILD_EXAMPLE=ON
130+
-DMAU_UUID_BUILD_TESTS=ON
117131
118132
- name: Build
119133
run: cmake --build build --config Release

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ target_compile_features(MauUUID INTERFACE cxx_std_20)
2424
# --- Platform dependencies ---
2525
if(WIN32)
2626
target_link_libraries(MauUUID INTERFACE ole32)
27-
endif()
28-
29-
if(UNIX AND NOT APPLE)
27+
elseif(UNIX AND NOT APPLE AND NOT ANDROID)
3028
target_link_libraries(MauUUID INTERFACE uuid)
3129
endif()
3230

include/uuid.h

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@
1313
#include <string_view>
1414

1515
#ifdef _WIN32
16-
#define NOMINMAX
17-
#include <objbase.h>
18-
#endif
19-
20-
#ifdef __linux__
21-
#include <uuid/uuid.h>
22-
#endif
23-
24-
#ifdef __APPLE__
25-
#include <uuid/uuid.h>
26-
#endif
27-
28-
#if !defined(_WIN32) && !defined(__linux__) && !defined(__APPLE__)
29-
#error Unsupported platform
16+
#define NOMINMAX
17+
#include <objbase.h>
18+
#elif defined(__APPLE__)
19+
#include <TargetConditionals.h>
20+
#if TARGET_OS_IOS
21+
#include <random>
22+
#else
23+
#include <uuid/uuid.h>
24+
#endif
25+
#elif defined(__linux__) && !defined(__ANDROID__)
26+
#include <uuid/uuid.h>
27+
#else
28+
#include <random>
3029
#endif
3130

3231
namespace MauUUID
@@ -122,16 +121,47 @@ namespace MauUUID
122121

123122
std::memcpy(&m_Bytes[8], guid.Data4, 8);
124123
}
125-
#endif
126-
127-
#if defined(__linux__) || defined(__APPLE__)
124+
#elif defined(__APPLE__) && !TARGET_OS_IOS
125+
/**
126+
* @brief Generate a new UUID.
127+
*/
128+
UUID() noexcept
129+
{
130+
uuid_generate(m_Bytes.data());
131+
}
132+
#elif defined(__linux__) && !defined(__ANDROID__)
128133
/**
129134
* @brief Generate a new UUID.
130135
*/
131136
UUID() noexcept
132137
{
133138
uuid_generate(m_Bytes.data());
134139
}
140+
#else
141+
/**
142+
* @brief Generate a new UUID (portable fallback using std::random).
143+
*/
144+
UUID() noexcept
145+
{
146+
static thread_local std::mt19937_64 rng{ []
147+
{
148+
std::random_device rd;
149+
return (static_cast<uint64_t>(rd()) << 32) | rd();
150+
}() };
151+
152+
std::uniform_int_distribution<uint64_t> dist;
153+
154+
uint64_t const a{ dist(rng) };
155+
uint64_t const b{ dist(rng) };
156+
157+
std::memcpy(m_Bytes.data(), &a, 8);
158+
std::memcpy(m_Bytes.data() + 8, &b, 8);
159+
160+
// RFC 4122 version 4 (random)
161+
m_Bytes[6] = (m_Bytes[6] & 0x0F) | 0x40;
162+
// RFC 4122 variant 1
163+
m_Bytes[8] = (m_Bytes[8] & 0x3F) | 0x80;
164+
}
135165
#endif
136166

137167
~UUID() = default;

0 commit comments

Comments
 (0)