Skip to content

Commit 16dd122

Browse files
committed
uuid: port for Windows
This adjusts the UUID code to build on Windows which is needed for the Foundation port itself.
1 parent 67b0d11 commit 16dd122

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ add_library(uuid
6565
target_compile_options(uuid
6666
PUBLIC
6767
-I${install_dir}/System/Library/Frameworks/CoreFoundation.framework/Headers)
68+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
69+
target_compile_definitions(uuid
70+
PRIVATE
71+
_CRT_NONSTDC_NO_WARNINGS
72+
_CRT_SECURE_NO_DEPRECATE
73+
_CRT_SECURE_NO_WARNINGS)
74+
endif()
6875
add_dependencies(uuid CoreFoundation)
6976

7077
set(swift_optimization_flags)

uuid/uuid.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
#include <stdint.h>
3636
#include <string.h>
3737
#include <fcntl.h>
38+
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
3839
#include <unistd.h>
40+
#elif defined(_WIN32)
41+
#include <io.h>
42+
#endif
3943
#include <stdio.h>
4044

4145
#if TARGET_OS_MAC
@@ -61,9 +65,25 @@ static inline void nanotime(struct timespec *tv) {
6165
clock_gettime(CLOCK_MONOTONIC, tv);
6266
}
6367

68+
#elif TARGET_OS_WINDOWS
69+
#include <time.h>
70+
71+
#define WIN32_LEAN_AND_MEAN
72+
#include <windows.h>
73+
74+
static inline void nanotime(struct timespec *tv) {
75+
FILETIME ftTime;
76+
77+
GetSystemTimePreciseAsFileTime(&ftTime);
78+
79+
uint64_t Value = (((uint64_t)ftTime.dwHighDateTime << 32) | ftTime.dwLowDateTime);
80+
81+
tv->tv_sec = Value / 1000000000;
82+
tv->tv_nsec = Value - (tv->tv_sec * 1000000000);
83+
}
6484
#endif
6585

66-
static inline void read_random(void *buffer, u_int numBytes) {
86+
static inline void read_random(void *buffer, unsigned numBytes) {
6787
int fd = open("/dev/urandom", O_RDONLY);
6888
read(fd, buffer, numBytes);
6989
close(fd);
@@ -227,4 +247,4 @@ void
227247
uuid_unparse(const uuid_t uu, uuid_string_t out)
228248
{
229249
uuid_unparse_upper(uu, out);
230-
}
250+
}

0 commit comments

Comments
 (0)