1 | //===-- Implementation of libc_errno --------------------------------------===// |
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 "libc_errno.h" |
10 | |
11 | #ifdef LIBC_TARGET_ARCH_IS_GPU |
12 | // LIBC_THREAD_LOCAL on GPU currently does nothing. So essentially this is just |
13 | // a global errno for gpu to use for now. |
14 | extern "C" { |
15 | LIBC_THREAD_LOCAL int __llvmlibc_gpu_errno; |
16 | } |
17 | |
18 | void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_gpu_errno = a; } |
19 | LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_gpu_errno; } |
20 | |
21 | #elif !defined(LIBC_COPT_PUBLIC_PACKAGING) |
22 | // This mode is for unit testing. We just use our internal errno. |
23 | LIBC_THREAD_LOCAL int __llvmlibc_internal_errno; |
24 | |
25 | void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_internal_errno = a; } |
26 | LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_internal_errno; } |
27 | |
28 | #elif defined(LIBC_FULL_BUILD) |
29 | // This mode is for public libc archive, hermetic, and integration tests. |
30 | // In full build mode, we provide the errno storage ourselves. |
31 | extern "C" { |
32 | LIBC_THREAD_LOCAL int __llvmlibc_errno; |
33 | } |
34 | |
35 | void LIBC_NAMESPACE::Errno::operator=(int a) { __llvmlibc_errno = a; } |
36 | LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_errno; } |
37 | |
38 | #else |
39 | // In overlay mode, we simply use the system errno. |
40 | #include <errno.h> |
41 | |
42 | void LIBC_NAMESPACE::Errno::operator=(int a) { errno = a; } |
43 | LIBC_NAMESPACE::Errno::operator int() { return errno; } |
44 | |
45 | #endif // LIBC_FULL_BUILD |
46 | |
47 | namespace LIBC_NAMESPACE { |
48 | // Define the global `libc_errno` instance. |
49 | Errno libc_errno; |
50 | } // namespace LIBC_NAMESPACE |
51 | |