Skip to content

Commit 12ad95e

Browse files
[libc] implement getrandom for windows
1 parent 8ca4aa5 commit 12ad95e

File tree

16 files changed

+155
-24
lines changed

16 files changed

+155
-24
lines changed

libc/config/windows/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ set(TARGET_LIBC_ENTRYPOINTS
101101
# time.h entrypoints
102102
libc.src.time.time
103103
libc.src.time.clock_getres
104+
105+
# sys/random.h entrypoints
106+
libc.src.sys.random.getrandom
104107
)
105108

106109
set(TARGET_LIBM_ENTRYPOINTS

libc/config/windows/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ set(TARGET_PUBLIC_HEADERS
66
libc.include.errno
77
libc.include.fenv
88
libc.include.math
9+
libc.include.sys_random
910
)

libc/hdr/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ add_proxy_header_library(
189189
libc.include.sys_auxv
190190
)
191191

192+
add_proxy_header_library(
193+
sys_random_macros
194+
HDRS
195+
sys_random_macros.h
196+
FULL_BUILD_DEPENDS
197+
libc.include.llvm-libc-macros.sys_random_macros
198+
libc.include.sys_random
199+
)
200+
192201
add_header_library(wchar_overlay HDRS wchar_overlay.h)
193202

194203
add_proxy_header_library(

libc/hdr/sys_random_macros.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from sys/auxv.h ------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_HDR_SYS_RANDOM_MACROS_H
10+
#define LLVM_LIBC_HDR_SYS_RANDOM_MACROS_H
11+
12+
#if defined(LIBC_FULL_BUILD) || defined(_WIN32)
13+
14+
#include "include/llvm-libc-macros/sys-random-macros.h"
15+
16+
#else // Overlay mode
17+
18+
#include <sys/random.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_SYS_RANDOM_MACROS_H

libc/hdr/types/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ add_proxy_header_library(
9393
libc.include.llvm-libc-types.size_t
9494
)
9595

96+
add_proxy_header_library(
97+
ssize_t
98+
HDRS
99+
ssize_t.h
100+
FULL_BUILD_DEPENDS
101+
libc.include.llvm-libc-types.ssize_t
102+
)
103+
96104
add_proxy_header_library(
97105
mode_t
98106
HDRS

libc/hdr/types/ssize_t.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#ifndef LLVM_LIBC_HDR_TYPES_SSIZE_T_H
99
#define LLVM_LIBC_HDR_TYPES_SSIZE_T_H
1010

11-
#ifdef LIBC_FULL_BUILD
11+
// stddef does not provide ssize_t on windows.
12+
#if defined(LIBC_FULL_BUILD) || defined(_WIN32)
1213

1314
#include "include/llvm-libc-types/ssize_t.h"
1415

libc/include/llvm-libc-types/ssize_t.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
#ifndef LLVM_LIBC_TYPES_SSIZE_T_H
1010
#define LLVM_LIBC_TYPES_SSIZE_T_H
1111

12+
// https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types
13+
#if __has_include(<BaseTsd.h>)
14+
#include <BaseTsd.h>
15+
typedef SSIZE_T ssize_t;
16+
#else
1217
typedef __INT64_TYPE__ ssize_t;
18+
#endif
1319

1420
#endif // LLVM_LIBC_TYPES_SSIZE_T_H

libc/src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ add_subdirectory(strings)
1616
add_subdirectory(wchar)
1717
add_subdirectory(time)
1818

19-
if(${LIBC_TARGET_OS} STREQUAL "linux")
19+
if(${LIBC_TARGET_OS} STREQUAL "linux" OR ${LIBC_TARGET_OS} STREQUAL "windows")
2020
add_subdirectory(dirent)
2121
add_subdirectory(fcntl)
2222
add_subdirectory(pthread)

libc/src/sys/random/getrandom.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#ifndef LLVM_LIBC_SRC_SYS_RANDOM_GETRANDOM_H
1010
#define LLVM_LIBC_SRC_SYS_RANDOM_GETRANDOM_H
1111

12+
#include "hdr/sys_random_macros.h"
13+
#include "hdr/types/ssize_t.h"
1214
#include "src/__support/macros/config.h"
13-
#include <sys/random.h>
1415

1516
namespace LIBC_NAMESPACE_DECL {
1617

libc/src/sys/random/linux/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ add_entrypoint_object(
55
HDRS
66
../getrandom.h
77
DEPENDS
8-
libc.include.sys_random
9-
libc.include.sys_syscall
8+
libc.hdr.sys_random_macros
9+
libc.hdr.types.ssize_t
1010
libc.src.__support.OSUtil.osutil
1111
libc.src.errno.errno
1212
)

0 commit comments

Comments
 (0)