[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:37:05 PDT 2024


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

>From e4408fcaf74aff96b076c932611d2ba3a18fdeab 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 1/2] [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 | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/libcxx/test/support/platform_support.h b/libcxx/test/support/platform_support.h
index ba14b32e3e94d..b19e9a73c6597 100644
--- a/libcxx/test/support/platform_support.h
+++ b/libcxx/test/support/platform_support.h
@@ -15,6 +15,7 @@
 #define PLATFORM_SUPPORT_H
 
 #include "test_macros.h"
+#include <cerrno>
 
 // locale names
 #define LOCALE_en_US_UTF_8     "en_US.UTF-8"
@@ -40,8 +41,10 @@
 #   include <io.h> // _mktemp_s
 #   include <fcntl.h> // _O_EXCL, ...
 #   include <sys/stat.h> // _S_IREAD, ...
-#else
+#elif __has_include(<unistd.h>)
 #   include <unistd.h> // close
+#else
+#   define TEST_TEMP_FALLBACK
 #endif
 
 #if defined(_CS_GNU_LIBC_VERSION)
@@ -71,6 +74,16 @@ std::string get_temp_file_name()
             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]);

>From bdd4b6918047b44c6b6dd04ad4bbfd1aa0770b16 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 5 Aug 2024 10:36:56 -0500
Subject: [PATCH 2/2] Update platform_support.h

---
 libcxx/test/support/platform_support.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libcxx/test/support/platform_support.h b/libcxx/test/support/platform_support.h
index b19e9a73c6597..1a08398b21cfc 100644
--- a/libcxx/test/support/platform_support.h
+++ b/libcxx/test/support/platform_support.h
@@ -15,7 +15,6 @@
 #define PLATFORM_SUPPORT_H
 
 #include "test_macros.h"
-#include <cerrno>
 
 // locale names
 #define LOCALE_en_US_UTF_8     "en_US.UTF-8"



More information about the libcxx-commits mailing list