[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 09:29:49 PDT 2024


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/102005

>From 38600dc4dd7d34037a0cdf6500ba44b7fb39ee3e 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 | 58 +++++++++++++++-----------
 1 file changed, 34 insertions(+), 24 deletions(-)

diff --git a/libcxx/test/support/platform_support.h b/libcxx/test/support/platform_support.h
index ba14b32e3e94d..94aeadb829b8c 100644
--- a/libcxx/test/support/platform_support.h
+++ b/libcxx/test/support/platform_support.h
@@ -40,8 +40,8 @@
 #   include <io.h> // _mktemp_s
 #   include <fcntl.h> // _O_EXCL, ...
 #   include <sys/stat.h> // _S_IREAD, ...
-#else
-#   include <unistd.h> // close
+#elif __has_include(<unistd.h>)
+#  include <unistd.h> // close
 #endif
 
 #if defined(_CS_GNU_LIBC_VERSION)
@@ -55,31 +55,41 @@ 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 !__has_include(<unistd.h>)
+  char buffer[L_tmpnam];
+  char* filename = tmpnam(buffer);
+  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