Skip to content

Commit e7db040

Browse files
authored
[libc][test] split exit tests into two separate tests (#166355)
_Exit(3) is a fairly simple syscall wrapper whereas exit(3) calls atexit-registered functions + whole lot of stuff that require support for sync primitives. Splitting the tests allows testing the former easily (especially for new port projects) --------- Signed-off-by: Shreeyash Pandey <[email protected]>
1 parent ac6daa8 commit e7db040

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

libc/cmake/modules/LLVMLibCArchitectures.cmake

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,37 @@ else()
215215
"Unsupported libc target operating system ${LIBC_TARGET_OS}")
216216
endif()
217217

218+
# If the compiler target triple is not the same as the triple specified by
219+
# LIBC_TARGET_TRIPLE or LLVM_RUNTIMES_TARGET, we will add a --target option
220+
# if the compiler is clang. If the compiler is GCC we just error out as there
221+
# is no equivalent of an option like --target.
222+
if(explicit_target_triple AND
223+
(NOT (libc_compiler_triple STREQUAL explicit_target_triple)))
224+
set(LIBC_CROSSBUILD TRUE)
225+
if(CMAKE_COMPILER_IS_GNUCXX)
226+
message(FATAL_ERROR
227+
"GCC target triple (${libc_compiler_triple}) and the explicity "
228+
"specified target triple (${explicit_target_triple}) do not match.")
229+
else()
230+
list(APPEND
231+
LIBC_COMPILE_OPTIONS_DEFAULT "--target=${explicit_target_triple}")
232+
endif()
233+
endif()
234+
235+
if(LIBC_TARGET_OS_IS_DARWIN)
236+
execute_process(
237+
COMMAND xcrun --sdk macosx --show-sdk-path
238+
OUTPUT_VARIABLE MACOSX_SDK_PATH
239+
RESULT_VARIABLE MACOSX_SDK_PATH_RESULT
240+
OUTPUT_STRIP_TRAILING_WHITESPACE
241+
)
242+
if(MACOSX_SDK_PATH_RESULT EQUAL 0)
243+
list(APPEND LIBC_COMPILE_OPTIONS_DEFAULT "-I" "${MACOSX_SDK_PATH}/usr/include")
244+
else()
245+
message(WARNING "Could not find macOS SDK path. `xcrun --sdk macosx --show-sdk-path` failed.")
246+
endif()
247+
endif()
248+
218249
# Windows does not support full mode build.
219250
if (LIBC_TARGET_OS_IS_WINDOWS AND LLVM_LIBC_FULL_BUILD)
220251
message(FATAL_ERROR "Windows does not support full mode build.")

libc/include/sys/syscall.h.def

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

12-
//TODO: Handle non-linux syscalls
12+
#if defined(__linux__)
1313

1414
#include <asm/unistd.h>
1515

@@ -2361,5 +2361,6 @@
23612361
#define SYS_writev __NR_writev
23622362
#endif
23632363

2364+
#endif // __linux__
23642365

23652366
#endif // LLVM_LIBC_SYS_SYSCALL_H

libc/test/UnitTest/ExecuteFunctionUnix.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ ProcessStatus invoke_in_subprocess(FunctionCaller *func, int timeout_ms) {
5757
}
5858
::close(pipe_fds[1]);
5959

60-
struct pollfd poll_fd {
61-
pipe_fds[0], 0, 0
62-
};
60+
pollfd poll_fd{pipe_fds[0], POLLIN, 0};
6361
// No events requested so this call will only return after the timeout or if
6462
// the pipes peer was closed, signaling the process exited.
6563
if (::poll(&poll_fd, 1, timeout_ms) == -1) {

libc/test/src/stdlib/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,19 @@ if(LLVM_LIBC_FULL_BUILD)
398398
libc-stdlib-tests
399399
SRCS
400400
_Exit_test.cpp
401+
DEPENDS
402+
libc.src.__support.OSUtil.osutil
403+
libc.src.stdlib._Exit
404+
)
405+
406+
add_libc_test(
407+
exit_test
408+
# The EXPECT_EXITS test is only availible for unit tests.
409+
UNIT_TEST_ONLY
410+
SUITE
411+
libc-stdlib-tests
412+
SRCS
413+
exit_test.cpp
401414
DEPENDS
402415
libc.src.stdlib._Exit
403416
libc.src.stdlib.exit

libc/test/src/stdlib/_Exit_test.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/stdlib/_Exit.h"
10-
#include "src/stdlib/exit.h"
1110
#include "test/UnitTest/Test.h"
1211

1312
TEST(LlvmLibcStdlib, _Exit) {
1413
EXPECT_EXITS([] { LIBC_NAMESPACE::_Exit(1); }, 1);
1514
EXPECT_EXITS([] { LIBC_NAMESPACE::_Exit(65); }, 65);
16-
17-
EXPECT_EXITS([] { LIBC_NAMESPACE::exit(1); }, 1);
18-
EXPECT_EXITS([] { LIBC_NAMESPACE::exit(65); }, 65);
1915
}

libc/test/src/stdlib/exit_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===-- Unittests for exit -----------------------------------------------===//
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+
#include "src/stdlib/exit.h"
10+
#include "test/UnitTest/Test.h"
11+
12+
TEST(LlvmLibcStdlib, exit) {
13+
EXPECT_EXITS([] { LIBC_NAMESPACE::exit(1); }, 1);
14+
EXPECT_EXITS([] { LIBC_NAMESPACE::exit(65); }, 65);
15+
}

0 commit comments

Comments
 (0)