[libcxx-commits] [libcxx] [libcxx] Add fallback to standard C when `unistd` is unavailable (PR #102005)
Joseph Huber via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Aug 5 08:38:30 PDT 2024
https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/102005
>From d9c5a90b3bbd60e95fc740306b6c073d8db1710e Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 5 Aug 2024 10:32:26 -0500
Subject: [PATCH] [libcxx] Add fallback to standard C when `unistd` is
unavailable
Summary:
This utility function gets a temp file to use for tests. It either uses
WIN32 or POSIX to create it. Some targets only follow the C standard,
and this test case will fail. This patch simply adds a fallback that
uses the `tmpnam` function from standard C. This function isn't ideal,
but it is good enough for our use-case.
---
libcxx/test/support/platform_support.h | 57 +++++++++++++++-----------
1 file changed, 34 insertions(+), 23 deletions(-)
diff --git a/libcxx/test/support/platform_support.h b/libcxx/test/support/platform_support.h
index ba14b32e3e94d..0900503c95831 100644
--- a/libcxx/test/support/platform_support.h
+++ b/libcxx/test/support/platform_support.h
@@ -40,8 +40,10 @@
# include <io.h> // _mktemp_s
# include <fcntl.h> // _O_EXCL, ...
# include <sys/stat.h> // _S_IREAD, ...
+#elif __has_include(<unistd.h>)
+# include <unistd.h> // close
#else
-# include <unistd.h> // close
+# define TEST_TEMP_FALLBACK
#endif
#if defined(_CS_GNU_LIBC_VERSION)
@@ -55,31 +57,40 @@ extern "C" {
}
#endif
-inline
-std::string get_temp_file_name()
-{
+inline std::string get_temp_file_name() {
#if defined(_WIN32)
- while (true) {
- char Name[] = "libcxx.XXXXXX";
- if (_mktemp_s(Name, sizeof(Name)) != 0) abort();
- int fd = _open(Name, _O_RDWR | _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE);
- if (fd != -1) {
- _close(fd);
- return Name;
- }
- if (errno == EEXIST)
- continue;
- abort();
+ while (true) {
+ char Name[] = "libcxx.XXXXXX";
+ if (_mktemp_s(Name, sizeof(Name)) != 0)
+ abort();
+ int fd = _open(Name, _O_RDWR | _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE);
+ if (fd != -1) {
+ _close(fd);
+ return Name;
}
+ if (errno == EEXIST)
+ continue;
+ abort();
+ }
+#elif defined(TEST_TEMP_FALLBACK)
+ char* filename = tmpnam(nullptr);
+ if (!filename)
+ abort();
+ FILE* file = fopen(filename, "w");
+ if (!file)
+ abort();
+ if (fclose(file) == EOF)
+ abort();
+ return std::string(filename);
#else
- std::string Name = "libcxx.XXXXXX";
- int FD = mkstemp(&Name[0]);
- if (FD == -1) {
- perror("mkstemp");
- abort();
- }
- close(FD);
- return Name;
+ std::string Name = "libcxx.XXXXXX";
+ int FD = mkstemp(&Name[0]);
+ if (FD == -1) {
+ perror("mkstemp");
+ abort();
+ }
+ close(FD);
+ return Name;
#endif
}
More information about the libcxx-commits
mailing list