Skip to content

[libc] Move libc_errno.h to libc/src/__support and make LIBC_ERRNO_MODE_SYSTEM to be header-only. #143187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 11, 2025

Conversation

lntue
Copy link
Contributor

@lntue lntue commented Jun 6, 2025

@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Jun 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 6, 2025

@llvm/pr-subscribers-libc

Author: None (lntue)

Changes

This is the first step in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450


Patch is 308.08 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/143187.diff

407 Files Affected:

  • (modified) libc/cmake/modules/LLVMLibCCompileOptionRules.cmake (+4)
  • (modified) libc/docs/dev/code_style.rst (+2-2)
  • (modified) libc/shared/fp_bits.h (+1)
  • (added) libc/shared/libc_common.h (+20)
  • (modified) libc/shared/rpc_server.h (+1)
  • (modified) libc/shared/str_to_float.h (+1)
  • (modified) libc/shared/str_to_integer.h (+1)
  • (modified) libc/src/__support/CMakeLists.txt (+9)
  • (modified) libc/src/__support/FPUtil/FEnvImpl.h (+1-1)
  • (modified) libc/src/__support/File/dir.cpp (+1-1)
  • (modified) libc/src/__support/File/file.cpp (+1-1)
  • (modified) libc/src/__support/File/linux/file.cpp (+1-1)
  • (modified) libc/src/__support/File/linux/lseekImpl.h (+1-1)
  • (modified) libc/src/__support/HashTable/randomness.h (+1-1)
  • (modified) libc/src/__support/OSUtil/linux/fcntl.cpp (+1-1)
  • (modified) libc/src/__support/OSUtil/linux/vdso.cpp (+1-1)
  • (modified) libc/src/__support/StringUtil/tables/linux_extension_errors.h (+1-1)
  • (added) libc/src/__support/libc_errno.h (+96)
  • (modified) libc/src/__support/threads/linux/thread.cpp (+1-1)
  • (modified) libc/src/dirent/closedir.cpp (+1-1)
  • (modified) libc/src/dirent/opendir.cpp (+1-1)
  • (modified) libc/src/dirent/readdir.cpp (+1-1)
  • (modified) libc/src/errno/CMakeLists.txt (+4-16)
  • (modified) libc/src/errno/libc_errno.cpp (+6-46)
  • (removed) libc/src/errno/libc_errno.h (-47)
  • (modified) libc/src/fcntl/linux/creat.cpp (+1-1)
  • (modified) libc/src/fcntl/linux/open.cpp (+1-1)
  • (modified) libc/src/fcntl/linux/openat.cpp (+1-1)
  • (modified) libc/src/inttypes/strtoimax.cpp (+1-1)
  • (modified) libc/src/inttypes/strtoumax.cpp (+1-1)
  • (modified) libc/src/math/generic/exp10m1f.cpp (+1-1)
  • (modified) libc/src/math/generic/exp2m1f.cpp (+1-1)
  • (modified) libc/src/math/generic/nan.cpp (+1-1)
  • (modified) libc/src/math/generic/nanf.cpp (+1-1)
  • (modified) libc/src/math/generic/nanf128.cpp (+1-1)
  • (modified) libc/src/math/generic/nanf16.cpp (+1-1)
  • (modified) libc/src/math/generic/nanl.cpp (+1-1)
  • (modified) libc/src/poll/linux/poll.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_atfork.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_attr_setdetachstate.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_attr_setguardsize.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_attr_setstack.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_attr_setstacksize.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_condattr_setclock.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_condattr_setpshared.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_create.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_key_create.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_key_delete.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_mutexattr_setpshared.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_mutexattr_setrobust.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_mutexattr_settype.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_rwlock_timedrdlock.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_rwlock_trywrlock.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_rwlock_unlock.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_rwlockattr_setkind_np.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_rwlockattr_setpshared.cpp (+1-1)
  • (modified) libc/src/pthread/pthread_setspecific.cpp (+1-1)
  • (modified) libc/src/sched/linux/sched_get_priority_max.cpp (+1-1)
  • (modified) libc/src/sched/linux/sched_get_priority_min.cpp (+1-1)
  • (modified) libc/src/sched/linux/sched_getaffinity.cpp (+1-1)
  • (modified) libc/src/sched/linux/sched_getparam.cpp (+1-1)
  • (modified) libc/src/sched/linux/sched_getscheduler.cpp (+1-1)
  • (modified) libc/src/sched/linux/sched_rr_get_interval.cpp (+1-1)
  • (modified) libc/src/sched/linux/sched_setaffinity.cpp (+1-1)
  • (modified) libc/src/sched/linux/sched_setparam.cpp (+1-1)
  • (modified) libc/src/sched/linux/sched_setscheduler.cpp (+1-1)
  • (modified) libc/src/sched/linux/sched_yield.cpp (+1-1)
  • (modified) libc/src/search/hcreate.cpp (+1-1)
  • (modified) libc/src/search/hcreate_r.cpp (+1-1)
  • (modified) libc/src/search/hdestroy_r.cpp (+1-1)
  • (modified) libc/src/search/hsearch.cpp (+1-1)
  • (modified) libc/src/search/hsearch_r.cpp (+1-1)
  • (modified) libc/src/signal/linux/kill.cpp (+1-1)
  • (modified) libc/src/signal/linux/sigaction.cpp (+1-1)
  • (modified) libc/src/signal/linux/sigaddset.cpp (+1-1)
  • (modified) libc/src/signal/linux/sigaltstack.cpp (+1-1)
  • (modified) libc/src/signal/linux/sigdelset.cpp (+1-1)
  • (modified) libc/src/signal/linux/sigemptyset.cpp (+1-1)
  • (modified) libc/src/signal/linux/sigfillset.cpp (+1-1)
  • (modified) libc/src/signal/linux/sigprocmask.cpp (+1-1)
  • (modified) libc/src/spawn/posix_spawn_file_actions_addclose.cpp (+1-1)
  • (modified) libc/src/spawn/posix_spawn_file_actions_adddup2.cpp (+1-1)
  • (modified) libc/src/spawn/posix_spawn_file_actions_addopen.cpp (+1-1)
  • (modified) libc/src/spawn/posix_spawn_file_actions_destroy.cpp (+1-1)
  • (modified) libc/src/stdio/fopencookie.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fclose.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fflush.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fgetc.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fgetc_unlocked.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fgets.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fopen.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fputc.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fputs.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fread.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fread_unlocked.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fseek.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fseeko.cpp (+1-1)
  • (modified) libc/src/stdio/generic/ftell.cpp (+1-1)
  • (modified) libc/src/stdio/generic/ftello.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fwrite.cpp (+1-1)
  • (modified) libc/src/stdio/generic/fwrite_unlocked.cpp (+1-1)
  • (modified) libc/src/stdio/generic/getc.cpp (+1-1)
  • (modified) libc/src/stdio/generic/getc_unlocked.cpp (+1-1)
  • (modified) libc/src/stdio/generic/getchar.cpp (+1-1)
  • (modified) libc/src/stdio/generic/getchar_unlocked.cpp (+1-1)
  • (modified) libc/src/stdio/generic/putc.cpp (+1-1)
  • (modified) libc/src/stdio/generic/putchar.cpp (+1-1)
  • (modified) libc/src/stdio/generic/puts.cpp (+1-1)
  • (modified) libc/src/stdio/gpu/fprintf.cpp (+1-1)
  • (modified) libc/src/stdio/gpu/fputs.cpp (+1-1)
  • (modified) libc/src/stdio/gpu/printf.cpp (+1-1)
  • (modified) libc/src/stdio/gpu/puts.cpp (+1-1)
  • (modified) libc/src/stdio/gpu/vfprintf.cpp (+1-1)
  • (modified) libc/src/stdio/gpu/vprintf.cpp (+1-1)
  • (modified) libc/src/stdio/linux/fdopen.cpp (+1-1)
  • (modified) libc/src/stdio/linux/remove.cpp (+1-1)
  • (modified) libc/src/stdio/linux/rename.cpp (+1-1)
  • (modified) libc/src/stdio/printf_core/parser.h (+1-1)
  • (modified) libc/src/stdio/setbuf.cpp (+1-1)
  • (modified) libc/src/stdio/setvbuf.cpp (+1-1)
  • (modified) libc/src/stdlib/atof.cpp (+1-1)
  • (modified) libc/src/stdlib/atoi.cpp (+1-1)
  • (modified) libc/src/stdlib/atol.cpp (+1-1)
  • (modified) libc/src/stdlib/atoll.cpp (+1-1)
  • (modified) libc/src/stdlib/strtod.cpp (+1-1)
  • (modified) libc/src/stdlib/strtod_l.cpp (+1-1)
  • (modified) libc/src/stdlib/strtof.cpp (+1-1)
  • (modified) libc/src/stdlib/strtof_l.cpp (+1-1)
  • (modified) libc/src/stdlib/strtol.cpp (+1-1)
  • (modified) libc/src/stdlib/strtol_l.cpp (+1-1)
  • (modified) libc/src/stdlib/strtold.cpp (+1-1)
  • (modified) libc/src/stdlib/strtold_l.cpp (+1-1)
  • (modified) libc/src/stdlib/strtoll.cpp (+1-1)
  • (modified) libc/src/stdlib/strtoll_l.cpp (+1-1)
  • (modified) libc/src/stdlib/strtoul.cpp (+1-1)
  • (modified) libc/src/stdlib/strtoul_l.cpp (+1-1)
  • (modified) libc/src/stdlib/strtoull.cpp (+1-1)
  • (modified) libc/src/stdlib/strtoull_l.cpp (+1-1)
  • (modified) libc/src/string/strdup.cpp (+1-1)
  • (modified) libc/src/sys/auxv/linux/getauxval.cpp (+1-1)
  • (modified) libc/src/sys/epoll/linux/epoll_create.cpp (+1-1)
  • (modified) libc/src/sys/epoll/linux/epoll_create1.cpp (+1-1)
  • (modified) libc/src/sys/epoll/linux/epoll_ctl.cpp (+1-1)
  • (modified) libc/src/sys/epoll/linux/epoll_pwait.cpp (+1-1)
  • (modified) libc/src/sys/epoll/linux/epoll_pwait2.cpp (+1-1)
  • (modified) libc/src/sys/epoll/linux/epoll_wait.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/madvise.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/mincore.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/mlock.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/mlock2.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/mlockall.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/mmap.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/mprotect.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/mremap.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/msync.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/munlock.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/munlockall.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/munmap.cpp (+2-2)
  • (modified) libc/src/sys/mman/linux/remap_file_pages.cpp (+1-1)
  • (modified) libc/src/sys/mman/linux/shm_common.h (+1-1)
  • (modified) libc/src/sys/prctl/linux/prctl.cpp (+1-1)
  • (modified) libc/src/sys/random/linux/getrandom.cpp (+1-1)
  • (modified) libc/src/sys/resource/linux/getrlimit.cpp (+1-1)
  • (modified) libc/src/sys/resource/linux/setrlimit.cpp (+1-1)
  • (modified) libc/src/sys/select/linux/select.cpp (+1-1)
  • (modified) libc/src/sys/sendfile/linux/sendfile.cpp (+1-1)
  • (modified) libc/src/sys/socket/linux/bind.cpp (+1-1)
  • (modified) libc/src/sys/socket/linux/recv.cpp (+1-1)
  • (modified) libc/src/sys/socket/linux/recvfrom.cpp (+1-1)
  • (modified) libc/src/sys/socket/linux/recvmsg.cpp (+1-1)
  • (modified) libc/src/sys/socket/linux/send.cpp (+1-1)
  • (modified) libc/src/sys/socket/linux/sendmsg.cpp (+1-1)
  • (modified) libc/src/sys/socket/linux/sendto.cpp (+1-1)
  • (modified) libc/src/sys/socket/linux/socket.cpp (+1-1)
  • (modified) libc/src/sys/socket/linux/socketpair.cpp (+1-1)
  • (modified) libc/src/sys/stat/linux/chmod.cpp (+1-1)
  • (modified) libc/src/sys/stat/linux/fchmod.cpp (+1-1)
  • (modified) libc/src/sys/stat/linux/fchmodat.cpp (+1-1)
  • (modified) libc/src/sys/stat/linux/fstat.cpp (+1-1)
  • (modified) libc/src/sys/stat/linux/lstat.cpp (+1-1)
  • (modified) libc/src/sys/stat/linux/mkdir.cpp (+1-1)
  • (modified) libc/src/sys/stat/linux/mkdirat.cpp (+1-1)
  • (modified) libc/src/sys/stat/linux/stat.cpp (+1-1)
  • (modified) libc/src/sys/statvfs/linux/statfs_utils.h (+1-1)
  • (modified) libc/src/sys/time/linux/getitimer.cpp (+1-1)
  • (modified) libc/src/sys/time/linux/setitimer.cpp (+1-1)
  • (modified) libc/src/sys/time/linux/utimes.cpp (+1-1)
  • (modified) libc/src/sys/uio/linux/readv.cpp (+1-1)
  • (modified) libc/src/sys/uio/linux/writev.cpp (+1-1)
  • (modified) libc/src/sys/utsname/linux/uname.cpp (+1-1)
  • (modified) libc/src/sys/wait/wait4Impl.h (+1-1)
  • (modified) libc/src/termios/linux/cfsetispeed.cpp (+1-1)
  • (modified) libc/src/termios/linux/cfsetospeed.cpp (+1-1)
  • (modified) libc/src/termios/linux/tcdrain.cpp (+1-1)
  • (modified) libc/src/termios/linux/tcflow.cpp (+1-1)
  • (modified) libc/src/termios/linux/tcflush.cpp (+1-1)
  • (modified) libc/src/termios/linux/tcgetattr.cpp (+1-1)
  • (modified) libc/src/termios/linux/tcgetsid.cpp (+1-1)
  • (modified) libc/src/termios/linux/tcsendbreak.cpp (+1-1)
  • (modified) libc/src/termios/linux/tcsetattr.cpp (+1-1)
  • (modified) libc/src/threads/thrd_create.cpp (+1-1)
  • (modified) libc/src/time/linux/clock.cpp (+1-1)
  • (modified) libc/src/time/linux/clock_gettime.cpp (+1-1)
  • (modified) libc/src/time/linux/gettimeofday.cpp (+1-1)
  • (modified) libc/src/time/linux/nanosleep.cpp (+1-1)
  • (modified) libc/src/time/linux/timespec_get.cpp (+1-1)
  • (modified) libc/src/time/time.cpp (+1-1)
  • (modified) libc/src/time/time_utils.h (+1-1)
  • (modified) libc/src/time/windows/clock_getres.cpp (+1-1)
  • (modified) libc/src/unistd/linux/access.cpp (+1-1)
  • (modified) libc/src/unistd/linux/chdir.cpp (+1-1)
  • (modified) libc/src/unistd/linux/close.cpp (+1-1)
  • (modified) libc/src/unistd/linux/dup.cpp (+1-1)
  • (modified) libc/src/unistd/linux/dup2.cpp (+1-1)
  • (modified) libc/src/unistd/linux/dup3.cpp (+1-1)
  • (modified) libc/src/unistd/linux/execv.cpp (+1-1)
  • (modified) libc/src/unistd/linux/execve.cpp (+1-1)
  • (modified) libc/src/unistd/linux/fchdir.cpp (+1-1)
  • (modified) libc/src/unistd/linux/fork.cpp (+1-1)
  • (modified) libc/src/unistd/linux/fsync.cpp (+1-1)
  • (modified) libc/src/unistd/linux/ftruncate.cpp (+1-1)
  • (modified) libc/src/unistd/linux/getcwd.cpp (+1-1)
  • (modified) libc/src/unistd/linux/getentropy.cpp (+1-1)
  • (modified) libc/src/unistd/linux/getsid.cpp (+1-1)
  • (modified) libc/src/unistd/linux/isatty.cpp (+1-1)
  • (modified) libc/src/unistd/linux/link.cpp (+1-1)
  • (modified) libc/src/unistd/linux/linkat.cpp (+1-1)
  • (modified) libc/src/unistd/linux/lseek.cpp (+1-1)
  • (modified) libc/src/unistd/linux/pathconf.cpp (+1-1)
  • (modified) libc/src/unistd/linux/pathconf_utils.cpp (+1-1)
  • (modified) libc/src/unistd/linux/pipe.cpp (+2-2)
  • (modified) libc/src/unistd/linux/pipe2.cpp (+1-1)
  • (modified) libc/src/unistd/linux/pread.cpp (+3-3)
  • (modified) libc/src/unistd/linux/pwrite.cpp (+1-1)
  • (modified) libc/src/unistd/linux/read.cpp (+2-2)
  • (modified) libc/src/unistd/linux/readlink.cpp (+1-1)
  • (modified) libc/src/unistd/linux/readlinkat.cpp (+1-1)
  • (modified) libc/src/unistd/linux/rmdir.cpp (+1-1)
  • (modified) libc/src/unistd/linux/symlink.cpp (+1-1)
  • (modified) libc/src/unistd/linux/symlinkat.cpp (+1-1)
  • (modified) libc/src/unistd/linux/syscall.cpp (+1-1)
  • (modified) libc/src/unistd/linux/sysconf.cpp (+1-1)
  • (modified) libc/src/unistd/linux/truncate.cpp (+1-1)
  • (modified) libc/src/unistd/linux/unlink.cpp (+1-1)
  • (modified) libc/src/unistd/linux/unlinkat.cpp (+1-1)
  • (modified) libc/src/unistd/linux/write.cpp (+1-1)
  • (modified) libc/src/unistd/windows/getentropy.cpp (+1-1)
  • (modified) libc/test/IntegrationTest/test.h (+3-6)
  • (modified) libc/test/UnitTest/ErrnoCheckingTest.h (+2-2)
  • (modified) libc/test/UnitTest/ErrnoSetterMatcher.h (+3-3)
  • (modified) libc/test/UnitTest/FPMatcher.h (+4-4)
  • (modified) libc/test/UnitTest/Test.h (+5-6)
  • (modified) libc/test/integration/src/pthread/pthread_create_test.cpp (+2-2)
  • (modified) libc/test/integration/src/pthread/pthread_join_test.cpp (+2-2)
  • (modified) libc/test/integration/src/pthread/pthread_name_test.cpp (+1-1)
  • (modified) libc/test/integration/src/unistd/getcwd_test.cpp (+3-3)
  • (modified) libc/test/integration/startup/linux/tls_test.cpp (+1-1)
  • (modified) libc/test/src/__support/str_to_double_test.cpp (+1-1)
  • (modified) libc/test/src/__support/str_to_float_test.cpp (+1-1)
  • (modified) libc/test/src/__support/str_to_fp_test.h (+2-2)
  • (modified) libc/test/src/__support/str_to_integer_test.cpp (+1-1)
  • (modified) libc/test/src/dirent/dirent_test.cpp (+5-5)
  • (modified) libc/test/src/errno/errno_test.cpp (+2-2)
  • (modified) libc/test/src/fcntl/creat_test.cpp (+1-1)
  • (modified) libc/test/src/fcntl/fcntl_test.cpp (+2-2)
  • (modified) libc/test/src/fcntl/openat_test.cpp (+1-1)
  • (modified) libc/test/src/math/RoundToIntegerTest.h (+1-1)
  • (modified) libc/test/src/math/acosf_test.cpp (+2-2)
  • (modified) libc/test/src/math/acoshf16_test.cpp (+1-1)
  • (modified) libc/test/src/math/acoshf_test.cpp (+2-2)
  • (modified) libc/test/src/math/asin_test.cpp (+1-1)
  • (modified) libc/test/src/math/asinf_test.cpp (+2-2)
  • (modified) libc/test/src/math/asinhf_test.cpp (+2-2)
  • (modified) libc/test/src/math/atan2f_test.cpp (+1-1)
  • (modified) libc/test/src/math/atan_test.cpp (+1-1)
  • (modified) libc/test/src/math/atanf_test.cpp (+2-2)
  • (modified) libc/test/src/math/atanhf_test.cpp (+2-2)
  • (modified) libc/test/src/math/cosf_test.cpp (+2-2)
  • (modified) libc/test/src/math/coshf_test.cpp (+3-3)
  • (modified) libc/test/src/math/cospif_test.cpp (+2-2)
  • (modified) libc/test/src/math/exp10_test.cpp (+2-2)
  • (modified) libc/test/src/math/exp10f_test.cpp (+7-8)
  • (modified) libc/test/src/math/exp10m1f_test.cpp (+4-4)
  • (modified) libc/test/src/math/exp2_test.cpp (+2-2)
  • (modified) libc/test/src/math/exp2f_test.cpp (+7-8)
  • (modified) libc/test/src/math/exp2m1f_test.cpp (+4-5)
  • (modified) libc/test/src/math/exp_test.cpp (+2-2)
  • (modified) libc/test/src/math/expf_test.cpp (+7-8)
  • (modified) libc/test/src/math/expm1_test.cpp (+2-2)
  • (modified) libc/test/src/math/expm1f_test.cpp (+7-8)
  • (modified) libc/test/src/math/log10_test.cpp (+2-2)
  • (modified) libc/test/src/math/log1p_test.cpp (+2-2)
  • (modified) libc/test/src/math/log1pf_test.cpp (+2-2)
  • (modified) libc/test/src/math/log2_test.cpp (+2-2)
  • (modified) libc/test/src/math/log2f_test.cpp (+3-4)
  • (modified) libc/test/src/math/log_test.cpp (+2-2)
  • (modified) libc/test/src/math/powf_test.cpp (+1-1)
  • (modified) libc/test/src/math/sin_test.cpp (+1-1)
  • (modified) libc/test/src/math/sincosf_test.cpp (+2-2)
  • (modified) libc/test/src/math/sinf_test.cpp (+2-2)
  • (modified) libc/test/src/math/sinhf_test.cpp (+3-3)
  • (modified) libc/test/src/math/sinpif_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/FModTest.h (+1-1)
  • (modified) libc/test/src/math/smoke/RoundToIntegerTest.h (+1-1)
  • (modified) libc/test/src/math/smoke/acos_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/acosf16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/acosf_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/acoshf16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/acoshf_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/acospif16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/asinf16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/asinf_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/asinhf16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/asinhf_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/atan2f_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/atanf16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/atanf_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/atanhf16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/atanhf_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/cosf16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/cosf_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/coshf16_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/coshf_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/cospif16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/cospif_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/exp10_test.cpp (+1-1)
  • (modified) libc/test/src/math/smoke/exp10f16_test.cpp (+4-4)
  • (modified) libc/test/src/math/smoke/exp10f_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/exp10m1f16_test.cpp (+4-4)
  • (modified) libc/test/src/math/smoke/exp10m1f_test.cpp (+4-4)
  • (modified) libc/test/src/math/smoke/exp2_test.cpp (+1-1)
  • (modified) libc/test/src/math/smoke/exp2f16_test.cpp (+4-4)
  • (modified) libc/test/src/math/smoke/exp2f_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/exp2m1f16_test.cpp (+4-4)
  • (modified) libc/test/src/math/smoke/exp2m1f_test.cpp (+4-4)
  • (modified) libc/test/src/math/smoke/exp_test.cpp (+1-1)
  • (modified) libc/test/src/math/smoke/expf16_test.cpp (+4-4)
  • (modified) libc/test/src/math/smoke/expf_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/expm1_test.cpp (+1-1)
  • (modified) libc/test/src/math/smoke/expm1f16_test.cpp (+4-4)
  • (modified) libc/test/src/math/smoke/expm1f_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/log10_test.cpp (+1-1)
  • (modified) libc/test/src/math/smoke/log10f16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/log1p_test.cpp (+1-1)
  • (modified) libc/test/src/math/smoke/log1pf_test.cpp (+1-1)
  • (modified) libc/test/src/math/smoke/log2_test.cpp (+1-1)
  • (modified) libc/test/src/math/smoke/log2f16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/log2f_test.cpp (+1-1)
  • (modified) libc/test/src/math/smoke/log_test.cpp (+1-1)
  • (modified) libc/test/src/math/smoke/logf16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/sincosf_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/sinf16_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/sinf_test.cpp (+2-2)
  • (modified) libc/test/src/math/smoke/sinhf16_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/sinhf_test.cpp (+3-3)
  • (modified) libc/test/src/math/smoke/sinpif16_test.cpp (+2-2)
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 0facb0b9be0c1..a98e7276bef80 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -106,6 +106,10 @@ function(_get_compile_options_from_config output_var)
     list(APPEND config_options "-DLIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}")
   endif()
 
+  if(LIBC_CONF_ERRNO_MODE)
+    set(APPEND config_options "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")
+  endif()
+
   set(${output_var} ${config_options} PARENT_SCOPE)
 endfunction(_get_compile_options_from_config)
 
diff --git a/libc/docs/dev/code_style.rst b/libc/docs/dev/code_style.rst
index 0bd3a69ae3ffe..86247966552f9 100644
--- a/libc/docs/dev/code_style.rst
+++ b/libc/docs/dev/code_style.rst
@@ -101,7 +101,7 @@ test infrastructure itself can be affected. To avoid perturbing the unit test
 infrastructure around the setting of ``errno``, the following rules are to be
 followed:
 
-#. A special macro named ``libc_errno`` defined in ``src/errno/libc_errno.h``
+#. A special macro named ``libc_errno`` defined in ``src/__support/libc_errno.h``
    should be used when setting ``errno`` from libc runtime code. For example,
    code to set ``errno`` to ``EINVAL`` should be:
 
@@ -117,7 +117,7 @@ followed:
    `ErrorOr <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/error_or.h>`_
    to return error values.
 
-#. The header file ``src/errno/libc_errno.h`` is shipped as part of the target
+#. The header file ``src/__support/libc_errno.h`` is shipped as part of the target
    corresponding to the ``errno`` entrypoint ``libc.src.errno.errno``. We do
    not in general allow dependencies between entrypoints. However, the ``errno``
    entrypoint is the only exceptional entrypoint on which other entrypoints
diff --git a/libc/shared/fp_bits.h b/libc/shared/fp_bits.h
index 2898c508b7772..e6bb1e17b80c9 100644
--- a/libc/shared/fp_bits.h
+++ b/libc/shared/fp_bits.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_LIBC_SHARED_FP_BITS_H
 #define LLVM_LIBC_SHARED_FP_BITS_H
 
+#include "libc_common.h"
 #include "src/__support/FPUtil/FPBits.h"
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/libc_common.h b/libc/shared/libc_common.h
new file mode 100644
index 0000000000000..837eb3c1f692e
--- /dev/null
+++ b/libc/shared/libc_common.h
@@ -0,0 +1,20 @@
+//===-- Common defines for sharing LLVM libc with LLVM projects -*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_LIBC_COMMON_H
+#define LLVM_LIBC_SHARED_LIBC_COMMON_H
+
+// Use system errno.
+#undef LIBC_ERRNO_MODE
+#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM
+
+#ifndef LIBC_NAMESPACE
+#define LIBC_NAMESPACE __llvm_libc
+#endif // LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SHARED_LIBC_COMMON_H
diff --git a/libc/shared/rpc_server.h b/libc/shared/rpc_server.h
index 5509094b944ad..46e35f13f0eac 100644
--- a/libc/shared/rpc_server.h
+++ b/libc/shared/rpc_server.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_LIBC_SHARED_RPC_SERVER_H
 #define LLVM_LIBC_SHARED_RPC_SERVER_H
 
+#include "libc_common.h"
 #include "src/__support/RPC/rpc_server.h"
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/str_to_float.h b/libc/shared/str_to_float.h
index b133a28e26efc..dcc6027d6c77f 100644
--- a/libc/shared/str_to_float.h
+++ b/libc/shared/str_to_float.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_LIBC_SHARED_STR_TO_FLOAT_H
 #define LLVM_LIBC_SHARED_STR_TO_FLOAT_H
 
+#include "libc_common.h"
 #include "src/__support/str_to_float.h"
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/shared/str_to_integer.h b/libc/shared/str_to_integer.h
index 15bee698d5a6b..6ed38c932662e 100644
--- a/libc/shared/str_to_integer.h
+++ b/libc/shared/str_to_integer.h
@@ -9,6 +9,7 @@
 #ifndef LLVM_LIBC_SHARED_STR_TO_INTEGER_H
 #define LLVM_LIBC_SHARED_STR_TO_INTEGER_H
 
+#include "libc_common.h"
 #include "src/__support/str_to_integer.h"
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index f92499fdbf451..327ff5e0c6a37 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -1,6 +1,15 @@
 add_subdirectory(CPP)
 add_subdirectory(macros)
 
+add_header_library(
+  libc_errno
+  HDRS
+    libc_errno.h
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   block
   HDRS
diff --git a/libc/src/__support/FPUtil/FEnvImpl.h b/libc/src/__support/FPUtil/FEnvImpl.h
index 4c8f34a435bdf..50a101f833c55 100644
--- a/libc/src/__support/FPUtil/FEnvImpl.h
+++ b/libc/src/__support/FPUtil/FEnvImpl.h
@@ -12,10 +12,10 @@
 #include "hdr/fenv_macros.h"
 #include "hdr/math_macros.h"
 #include "hdr/types/fenv_t.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/macros/attributes.h" // LIBC_INLINE
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/architectures.h"
-#include "src/errno/libc_errno.h"
 
 #if defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_FP)
 #if defined(__APPLE__)
diff --git a/libc/src/__support/File/dir.cpp b/libc/src/__support/File/dir.cpp
index 21b0106f70106..aea8862c15f7f 100644
--- a/libc/src/__support/File/dir.cpp
+++ b/libc/src/__support/File/dir.cpp
@@ -11,8 +11,8 @@
 #include "src/__support/CPP/mutex.h" // lock_guard
 #include "src/__support/CPP/new.h"
 #include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h" // For error macros
 #include "src/__support/macros/config.h"
-#include "src/errno/libc_errno.h" // For error macros
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/__support/File/file.cpp b/libc/src/__support/File/file.cpp
index 528542cccf324..303852dbbb717 100644
--- a/libc/src/__support/File/file.cpp
+++ b/libc/src/__support/File/file.cpp
@@ -13,8 +13,8 @@
 #include "hdr/types/off_t.h"
 #include "src/__support/CPP/new.h"
 #include "src/__support/CPP/span.h"
+#include "src/__support/libc_errno.h" // For error macros
 #include "src/__support/macros/config.h"
-#include "src/errno/libc_errno.h" // For error macros
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/__support/File/linux/file.cpp b/libc/src/__support/File/linux/file.cpp
index 824c1f200e8c5..761e352f74ead 100644
--- a/libc/src/__support/File/linux/file.cpp
+++ b/libc/src/__support/File/linux/file.cpp
@@ -15,8 +15,8 @@
 #include "src/__support/File/linux/lseekImpl.h"
 #include "src/__support/OSUtil/fcntl.h"
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/libc_errno.h"     // For error macros
 #include "src/__support/macros/config.h"
-#include "src/errno/libc_errno.h" // For error macros
 
 #include "hdr/fcntl_macros.h" // For mode_t and other flags to the open syscall
 #include <sys/stat.h>    // For S_IS*, S_IF*, and S_IR* flags.
diff --git a/libc/src/__support/File/linux/lseekImpl.h b/libc/src/__support/File/linux/lseekImpl.h
index a034913d9f6ec..300e5c5dd55bf 100644
--- a/libc/src/__support/File/linux/lseekImpl.h
+++ b/libc/src/__support/File/linux/lseekImpl.h
@@ -13,8 +13,8 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
-#include "src/errno/libc_errno.h"
 
 #include <stdint.h>      // For uint64_t.
 #include <sys/syscall.h> // For syscall numbers.
diff --git a/libc/src/__support/HashTable/randomness.h b/libc/src/__support/HashTable/randomness.h
index 244dd41be3eec..6b58a4125f785 100644
--- a/libc/src/__support/HashTable/randomness.h
+++ b/libc/src/__support/HashTable/randomness.h
@@ -14,7 +14,7 @@
 #include "src/__support/macros/attributes.h"
 #include "src/__support/macros/config.h"
 #if defined(LIBC_HASHTABLE_USE_GETRANDOM)
-#include "src/errno/libc_errno.h"
+#include "src/__support/libc_errno.h"
 #include "src/sys/random/getrandom.h"
 #endif
 
diff --git a/libc/src/__support/OSUtil/linux/fcntl.cpp b/libc/src/__support/OSUtil/linux/fcntl.cpp
index 4742b2a00220b..99e16ad58c918 100644
--- a/libc/src/__support/OSUtil/linux/fcntl.cpp
+++ b/libc/src/__support/OSUtil/linux/fcntl.cpp
@@ -15,8 +15,8 @@
 #include "hdr/types/struct_flock64.h"
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
-#include "src/errno/libc_errno.h"
 
 #include <stdarg.h>
 #include <sys/syscall.h> // For syscall numbers.
diff --git a/libc/src/__support/OSUtil/linux/vdso.cpp b/libc/src/__support/OSUtil/linux/vdso.cpp
index 8c9bd3e1bcc72..e4e53c3c2a0f2 100644
--- a/libc/src/__support/OSUtil/linux/vdso.cpp
+++ b/libc/src/__support/OSUtil/linux/vdso.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/CPP/array.h"
 #include "src/__support/CPP/optional.h"
 #include "src/__support/CPP/string_view.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/threads/callonce.h"
 #include "src/__support/threads/linux/futex_word.h"
-#include "src/errno/libc_errno.h"
 #include "src/sys/auxv/getauxval.h"
 #include <linux/auxvec.h>
 
diff --git a/libc/src/__support/StringUtil/tables/linux_extension_errors.h b/libc/src/__support/StringUtil/tables/linux_extension_errors.h
index 425590f6e91c9..de637d60bea97 100644
--- a/libc/src/__support/StringUtil/tables/linux_extension_errors.h
+++ b/libc/src/__support/StringUtil/tables/linux_extension_errors.h
@@ -10,8 +10,8 @@
 #define LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_TABLES_LINUX_EXTENSION_ERRORS_H
 
 #include "src/__support/StringUtil/message_mapper.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
-#include "src/errno/libc_errno.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/__support/libc_errno.h b/libc/src/__support/libc_errno.h
new file mode 100644
index 0000000000000..a48622d754b8a
--- /dev/null
+++ b/libc/src/__support/libc_errno.h
@@ -0,0 +1,96 @@
+//===-- Implementation header for libc_errno --------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H
+#define LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H
+
+// This header is to be consumed by internal implementations, in which all of
+// them should refer to `libc_errno` instead of using `errno` directly from
+// <errno.h> header.
+
+// Unit and hermetic tests should:
+// - #include "src/__support/libc_errno.h"
+// - NOT #include <errno.h>
+// - Only use `libc_errno` in the code
+// - Depend on libc.src.errno.errno
+
+// Integration tests should:
+// - NOT #include "src/__support/libc_errno.h"
+// - #include <errno.h>
+// - Use regular `errno` in the code
+// - Still depend on libc.src.errno.errno
+
+// libc uses a fallback default value, either system or thread local.
+#define LIBC_ERRNO_MODE_DEFAULT 0
+// libc never stores a value; `errno` macro uses get link-time failure.
+#define LIBC_ERRNO_MODE_UNDEFINED 1
+// libc maintains per-thread state (requires C++ `thread_local` support).
+#define LIBC_ERRNO_MODE_THREAD_LOCAL 2
+// libc maintains shared state used by all threads, contrary to standard C
+// semantics unless always single-threaded; nothing prevents data races.
+#define LIBC_ERRNO_MODE_SHARED 3
+// libc doesn't maintain any internal state, instead the embedder must define
+// `int *__llvm_libc_errno(void);` C function.
+#define LIBC_ERRNO_MODE_EXTERNAL 4
+// libc uses system `<errno.h>` `errno` macro directly in the overlay mode; in
+// fullbuild mode, effectively the same as `LIBC_ERRNO_MODE_EXTERNAL`.
+#define LIBC_ERRNO_MODE_SYSTEM 5
+
+#if !defined(LIBC_ERRNO_MODE) || LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_DEFAULT
+#undef LIBC_ERRNO_MODE
+#if defined(LIBC_FULL_BUILD) || !defined(LIBC_COPT_PUBLIC_PACKAGING)
+#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_THREAD_LOCAL
+#else
+#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM
+#endif
+#endif // LIBC_ERRNO_MODE
+
+#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_DEFAULT &&                              \
+    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_UNDEFINED &&                            \
+    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_THREAD_LOCAL &&                         \
+    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SHARED &&                               \
+    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_EXTERNAL &&                             \
+    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM
+#error LIBC_ERRNO_MODE must be one of the following values: \
+LIBC_ERRNO_MODE_DEFAULT, \
+LIBC_ERRNO_MODE_UNDEFINED, \
+LIBC_ERRNO_MODE_THREAD_LOCAL, \
+LIBC_ERRNO_MODE_SHARED, \
+LIBC_ERRNO_MODE_EXTERNAL, \
+LIBC_ERRNO_MODE_SYSTEM
+#endif
+
+#if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SYSTEM
+
+#include <errno.h>
+
+#define libc_errno errno
+
+#else // !LIBC_ERRNO_MODE_SYSTEM
+
+#include "hdr/errno_macros.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+extern "C" int *__llvm_libc_errno() noexcept;
+
+struct Errno {
+  void operator=(int);
+  operator int();
+};
+
+extern Errno libc_errno;
+
+} // namespace LIBC_NAMESPACE_DECL
+
+using LIBC_NAMESPACE::libc_errno;
+
+#endif // LIBC_ERRNO_MODE_SYSTEM
+
+#endif // LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H
diff --git a/libc/src/__support/threads/linux/thread.cpp b/libc/src/__support/threads/linux/thread.cpp
index c531d74c53355..baad26aed6851 100644
--- a/libc/src/__support/threads/linux/thread.cpp
+++ b/libc/src/__support/threads/linux/thread.cpp
@@ -14,9 +14,9 @@
 #include "src/__support/OSUtil/syscall.h" // For syscall functions.
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h" // For error macros
 #include "src/__support/macros/config.h"
 #include "src/__support/threads/linux/futex_utils.h" // For FutexWordType
-#include "src/errno/libc_errno.h"                    // For error macros
 
 #ifdef LIBC_TARGET_ARCH_IS_AARCH64
 #include <arm_acle.h>
diff --git a/libc/src/dirent/closedir.cpp b/libc/src/dirent/closedir.cpp
index 1249ef94cf411..2f8f6f0c044db 100644
--- a/libc/src/dirent/closedir.cpp
+++ b/libc/src/dirent/closedir.cpp
@@ -10,8 +10,8 @@
 
 #include "src/__support/File/dir.h"
 #include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
-#include "src/errno/libc_errno.h"
 
 #include <dirent.h>
 
diff --git a/libc/src/dirent/opendir.cpp b/libc/src/dirent/opendir.cpp
index fee14ef0f558d..bf47d0edac180 100644
--- a/libc/src/dirent/opendir.cpp
+++ b/libc/src/dirent/opendir.cpp
@@ -10,8 +10,8 @@
 
 #include "src/__support/File/dir.h"
 #include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
-#include "src/errno/libc_errno.h"
 
 #include <dirent.h>
 
diff --git a/libc/src/dirent/readdir.cpp b/libc/src/dirent/readdir.cpp
index ad460b5e80b8b..f95f7c1ae8646 100644
--- a/libc/src/dirent/readdir.cpp
+++ b/libc/src/dirent/readdir.cpp
@@ -10,8 +10,8 @@
 
 #include "src/__support/File/dir.h"
 #include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
-#include "src/errno/libc_errno.h"
 
 #include <dirent.h>
 
diff --git a/libc/src/errno/CMakeLists.txt b/libc/src/errno/CMakeLists.txt
index 1d78a5eedff96..2852044e94164 100644
--- a/libc/src/errno/CMakeLists.txt
+++ b/libc/src/errno/CMakeLists.txt
@@ -1,28 +1,16 @@
 # If we are in full build mode, we will provide the errno definition ourselves,
 # and if we are in overlay mode, we will just re-use the system's errno.
-# We are passing LIBC_FULL_BUILD flag in full build mode so that the
-# implementation of libc_errno will know if we are in full build mode or not.
-
-# TODO: Move LIBC_FULL_BUILD flag to _get_common_compile_options.
-set(full_build_flag "")
-if(LLVM_LIBC_FULL_BUILD)
-  set(full_build_flag "-DLIBC_FULL_BUILD")
-endif()
-
-if(LIBC_CONF_ERRNO_MODE)
-  set(errno_config_copts "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")
-endif()
 
 add_entrypoint_object(
   errno
   SRCS
     libc_errno.cpp
   HDRS
-    libc_errno.h     # Include this
-  COMPILE_OPTIONS
-    ${full_build_flag}
-    ${errno_config_copts}
+    ../__support/libc_errno.h
   DEPENDS
     libc.hdr.errno_macros
     libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.attributes
+    libc.src.__support.macros.config
 )
diff --git a/libc/src/errno/libc_errno.cpp b/libc/src/errno/libc_errno.cpp
index d1600d1b050e3..5d319031faf36 100644
--- a/libc/src/errno/libc_errno.cpp
+++ b/libc/src/errno/libc_errno.cpp
@@ -6,51 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "libc_errno.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/attributes.h"
 #include "src/__support/macros/config.h"
 
-// libc uses a fallback default value, either system or thread local.
-#define LIBC_ERRNO_MODE_DEFAULT 0
-// libc never stores a value; `errno` macro uses get link-time failure.
-#define LIBC_ERRNO_MODE_UNDEFINED 1
-// libc maintains per-thread state (requires C++ `thread_local` support).
-#define LIBC_ERRNO_MODE_THREAD_LOCAL 2
-// libc maintains shared state used by all threads, contrary to standard C
-// semantics unless always single-threaded; nothing prevents data races.
-#define LIBC_ERRNO_MODE_SHARED 3
-// libc doesn't maintain any internal state, instead the embedder must define
-// `int *__llvm_libc_errno(void);` C function.
-#define LIBC_ERRNO_MODE_EXTERNAL 4
-// libc uses system `<errno.h>` `errno` macro directly in the overlay mode; in
-// fullbuild mode, effectively the same as `LIBC_ERRNO_MODE_EXTERNAL`.
-#define LIBC_ERRNO_MODE_SYSTEM 5
-
-#if !defined(LIBC_ERRNO_MODE) || LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_DEFAULT
-#undef LIBC_ERRNO_MODE
-#if defined(LIBC_FULL_BUILD) || !defined(LIBC_COPT_PUBLIC_PACKAGING)
-#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_THREAD_LOCAL
-#else
-#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM
-#endif
-#endif // LIBC_ERRNO_MODE
-
-#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_DEFAULT &&                              \
-    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_UNDEFINED &&                            \
-    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_THREAD_LOCAL &&                         \
-    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SHARED &&                               \
-    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_EXTERNAL &&                             \
-    LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM
-#error LIBC_ERRNO_MODE must be one of the following values: \
-LIBC_ERRNO_MODE_DEFAULT, \
-LIBC_ERRNO_MODE_UNDEFINED, \
-LIBC_ERRNO_MODE_THREAD_LOCAL, \
-LIBC_ERRNO_MODE_SHARED, \
-LIBC_ERRNO_MODE_EXTERNAL, \
-LIBC_ERRNO_MODE_SYSTEM
-#endif
-
 namespace LIBC_NAMESPACE_DECL {
 
+#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM
+
 #if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_UNDEFINED
 
 void Errno::operator=(int) {}
@@ -83,14 +46,11 @@ Errno::operator int() { return shared_errno; }
 void Errno::operator=(int a) { *__llvm_libc_errno() = a; }
 Errno::operator int() { return *__llvm_libc_errno(); }
 
-#elif LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SYSTEM
-
-void Errno::operator=(int a) { errno = a; }
-Errno::operator int() { return errno; }
-
 #endif
 
 // Define the global `libc_errno` instance.
 Errno libc_errno;
 
+#endif // LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM
+
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/errno/libc_errno.h b/libc/src/errno/libc_errno.h
deleted file mode 100644
index 44ee2714843ba..0000000000000
--- a/libc/src/errno/libc_errno.h
+++ /dev/null
@@ -1,47 +0,0 @@
-//===-- Implementation header for libc_errno --------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIBC_SRC_ERRNO_LIBC...
[truncated]


// Use system errno.
#undef LIBC_ERRNO_MODE
#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would already be the default behavior? Looks like LIBC_ERRNO_MODE defaults to LIBC_ERRNO_MODE_SYSTEM when it's not a production build?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, without setting LIBC_COPT_PUBLIC_PACKAGING, LIBC_ERRNO_MODE will be set to LIBC_ERRNO_MODE_THREAD_LOCAL.

Previously, everywhere in our code base, it is pointed to LIBC_NAMESPACE::libc_errno symbol, which will be resolved by either libc.src.errno.errno target or libc.src.errno.errno.__internal__ target. This is for being shared with libc++ and other LLVM projects, which will (and should) only use the system errno at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks for correction - I see now that by default including libc/shared from a non-llvm-libc code would pick LIBC_ERRNO_MODE_THREAD_LOCAL, and that's not what we want. I'm OK with somehow forcing LIBC_ERRNO_MODE_SYSTEM somehow, but wonder how to do it reliably. I.e. if you forget to include libc/shared/libc_common.h inside a different libc/shared/ header, you may end up transitively including libc/__support/libc_errno.h, and defining LIBC_ERRNO_MODE to something else...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can enforce that with clang-tidy check and bot?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a use case when LIBC_ERRNO_MODE may be explicitly set to something else but LIBC_ERRNO_MODE_SYSTEM_INLINE when using llvm-libc code though libc/shared? If no, then how about we:

  1. Teach llvm-libc headers to "understand" when they are being used in "shared" mode (i.e. by #define LIBC_SHARED_MODE in libc/shared/libc_common.h header, or by omitting this header file entirely, and using this define in every libc/shared/ file which currently is modified to include libc/shared/libc_common.h)
  2. In src/__support/libc_errno.h: make it an error to explicitly #define LIBC_ERRNO_MODE to anything but LIBC_ERRNO_MODE_SYSTEM_INLINE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be easier to enforce that in libc/shared/libc_common.h, basically just error out if LIBC_ERRNO_MODE is defined and not set as LIBC_ERRNO_MODE_SYSTEM_INLINE. Once it is set as such, src/__support/libc_errno.h will not change it.

That's simpler than putting the enforcement in src/__support/libc_errno.h, because it needs some signal / macro definition from libc/shared folder anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I think this check would also work (provided we add clang-tidy check you refer to to enforce the libc_common.h is included where needed.

Copy link
Contributor

@vonosmas vonosmas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a tiny nit.

@petrhosek
Copy link
Member

Have you considered changing LIBC_ERRNO_MODE_SYSTEM to be just a macro (avoiding the need for introducing LIBC_ERRNO_MODE_SYSTEM_INLINE)?

@vonosmas
Copy link
Contributor

Have you considered changing LIBC_ERRNO_MODE_SYSTEM to be just a macro (avoiding the need for introducing LIBC_ERRNO_MODE_SYSTEM_INLINE)?

There's a TODO in the code and #143454 filed to address this.

@lntue lntue merged commit d87eea3 into llvm:main Jun 11, 2025
14 checks passed
@lntue lntue deleted the libc_errno branch June 11, 2025 20:25
michaelrj-google added a commit to michaelrj-google/llvm-project that referenced this pull request Jun 12, 2025
Since errno was moved in
llvm#143187 the code including it
in llvm#141393 was rendered
incorrect. This patch fixes the include and the cmake depends.
This was referenced Jun 12, 2025
michaelrj-google added a commit that referenced this pull request Jun 12, 2025
Since errno was moved in
#143187 the code including it
in #141393 was rendered
incorrect. This patch fixes the include and the cmake depends.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 12, 2025
Since errno was moved in
llvm/llvm-project#143187 the code including it
in llvm/llvm-project#141393 was rendered
incorrect. This patch fixes the include and the cmake depends.
michaelrj-google added a commit to michaelrj-google/llvm-project that referenced this pull request Jun 12, 2025
Docgen updates the docs when the config options are changed. This update
has been waiting since llvm#143187.
michaelrj-google added a commit that referenced this pull request Jun 12, 2025
Docgen updates the docs when the config options are changed. This update
has been waiting since #143187.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 13, 2025
Docgen updates the docs when the config options are changed. This update
has been waiting since llvm/llvm-project#143187.
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
Since errno was moved in
llvm#143187 the code including it
in llvm#141393 was rendered
incorrect. This patch fixes the include and the cmake depends.
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
Docgen updates the docs when the config options are changed. This update
has been waiting since llvm#143187.
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
Since errno was moved in
llvm#143187 the code including it
in llvm#141393 was rendered
incorrect. This patch fixes the include and the cmake depends.
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
Docgen updates the docs when the config options are changed. This update
has been waiting since llvm#143187.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bazel "Peripheral" support tier build system: utils/bazel libc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants