Skip to content

Commit 5793c84

Browse files
committed
[libc] Add write(2) implementation for Linux and FDReader test utility
Summary: Adds `write` for Linux and FDReader utility which should be useful for some stdio tests as well. Reviewers: sivachandra, PaulkaToast Reviewed By: sivachandra Subscribers: mgorny, tschuett, libc-commits Differential Revision: https://reviews.llvm.org/D78184
1 parent 8c94d61 commit 5793c84

File tree

18 files changed

+259
-0
lines changed

18 files changed

+259
-0
lines changed

libc/config/linux/api.td

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ def SizeT : TypeDecl<"size_t"> {
1212
}];
1313
}
1414

15+
def SSizeT : TypeDecl<"ssize_t"> {
16+
let Decl = [{
17+
#define __need_ssize_t
18+
#include <__posix-types.h>
19+
}];
20+
}
21+
1522
def OffT : TypeDecl<"off_t"> {
1623
let Decl = [{
1724
#define __need_off_t
@@ -308,3 +315,14 @@ def ThreadsAPI : PublicAPI<"threads.h"> {
308315
"thrd_join",
309316
];
310317
}
318+
319+
def UniStdAPI : PublicAPI<"unistd.h"> {
320+
let TypeDeclarations = [
321+
SSizeT,
322+
SizeT,
323+
];
324+
325+
let Functions = [
326+
"write",
327+
];
328+
}

libc/include/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ add_gen_header(
9191
.llvm_libc_common_h
9292
)
9393

94+
add_gen_header(
95+
unistd
96+
DEF_FILE unistd.h.def
97+
GEN_HDR unistd.h
98+
DEPENDS
99+
.llvm_libc_common_h
100+
)
101+
94102
# TODO: Not all platforms will have a include/sys directory. Add the sys
95103
# directory and the targets for sys/*.h files conditional to the OS requiring
96104
# them.

libc/include/__posix-types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
1414
typedef __INT64_TYPE__ off_t;
1515
#define __llvm_libc_off_t_defined
1616
#endif // __need_off_t
17+
18+
#if defined(__need_ssize_t) && !defined(__llvm_libc_ssize_t_defined)
19+
typedef __INT64_TYPE__ ssize_t;
20+
#define __llvm_libc_ssize_t_defined
21+
#endif // __need_ssize_t

libc/include/unistd.h.def

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- C standard library header unistd.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_UNISTD_H
10+
#define LLVM_LIBC_UNISTD_H
11+
12+
#include <__llvm-libc-common.h>
13+
14+
%%public_api()
15+
16+
#endif // LLVM_LIBC_UNISTD_H

libc/lib/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ add_entrypoint_library(
3535
libc.src.threads.mtx_unlock
3636
libc.src.threads.thrd_create
3737
libc.src.threads.thrd_join
38+
39+
# unistd.h entrypoints
40+
libc.src.unistd.write
3841
)
3942

4043
add_entrypoint_library(

libc/spec/posix.td

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def ConstRestrictStructSigactionPtr : ConstType<RestrictStructSigactionPtr>;
1212

1313
def POSIX : StandardSpec<"POSIX"> {
1414
NamedType OffTType = NamedType<"off_t">;
15+
NamedType SSizeTType = NamedType<"ssize_t">;
1516

1617
HeaderSpec Errno = HeaderSpec<
1718
"errno.h",
@@ -174,9 +175,27 @@ def POSIX : StandardSpec<"POSIX"> {
174175
]
175176
>;
176177

178+
HeaderSpec UniStd = HeaderSpec<
179+
"unistd.h",
180+
[], // Macros
181+
[
182+
SSizeTType,
183+
SizeTType,
184+
],
185+
[], // Enumerations
186+
[
187+
FunctionSpec<
188+
"write",
189+
RetValSpec<SSizeTType>,
190+
[ArgSpec<IntType>, ArgSpec<ConstVoidPtr>, ArgSpec<SizeTType>]
191+
>,
192+
]
193+
>;
194+
177195
let Headers = [
178196
Errno,
179197
SysMMan,
180198
Signal,
199+
UniStd,
181200
];
182201
}

libc/spec/spec.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def CharType : NamedType<"char">;
4646

4747
// Common types
4848
def VoidPtr : PtrType<VoidType>;
49+
def ConstVoidPtr : ConstType<VoidPtr>;
4950
def SizeTType : NamedType<"size_t">;
5051
def FloatPtr : PtrType<FloatType>;
5152

libc/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ add_subdirectory(string)
88
# TODO: Add this target conditional to the target OS.
99
add_subdirectory(sys)
1010
add_subdirectory(threads)
11+
add_subdirectory(unistd)
1112

1213
add_subdirectory(__support)

libc/src/unistd/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
3+
endif()
4+
5+
add_entrypoint_object(
6+
write
7+
ALIAS
8+
DEPENDS
9+
.${LIBC_TARGET_OS}.write
10+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_entrypoint_object(
2+
write
3+
SRCS
4+
write.cpp
5+
HDRS
6+
../write.h
7+
DEPENDS
8+
libc.include.unistd
9+
libc.config.linux.linux_syscall_h
10+
libc.include.sys_syscall
11+
libc.src.errno.__errno_location
12+
)

0 commit comments

Comments
 (0)