[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
Tue Aug 6 10:37:52 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 1/5] [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
}
>From c5d076e95ced17e5ed61213a41cb4b83e773e399 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 5 Aug 2024 15:03:41 -0500
Subject: [PATCH 2/5] Alternatve version that doesn't use `tmpnam`.
---
libcxx/test/support/platform_support.h | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/libcxx/test/support/platform_support.h b/libcxx/test/support/platform_support.h
index 94aeadb829b8c..4136ff590c437 100644
--- a/libcxx/test/support/platform_support.h
+++ b/libcxx/test/support/platform_support.h
@@ -71,10 +71,13 @@ inline std::string get_temp_file_name() {
abort();
}
#elif !__has_include(<unistd.h>)
- char buffer[L_tmpnam];
- char* filename = tmpnam(buffer);
- if (!filename)
- abort();
+ // Without `unistd.h` we cannot guarnatee that the file is unused, however we
+ // can simply generate a good guess in the temporary folder and create it.
+ constexpr char chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ char Name[] = "/tmp/libcxx.XXXXXX";
+ for (int i = 0; i < sizeof(Name); ++i)
+ if (Name[i] == 'X')
+ Name[i] = chars[rand() % strlen(chars)];
FILE* file = fopen(filename, "w");
if (!file)
abort();
>From 892cb514d7fbde3504d34f2471c301f44f4f3a23 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 5 Aug 2024 16:07:15 -0500
Subject: [PATCH 3/5] Update platform_support.h
---
libcxx/test/support/platform_support.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/test/support/platform_support.h b/libcxx/test/support/platform_support.h
index 4136ff590c437..0e78d1a92d32d 100644
--- a/libcxx/test/support/platform_support.h
+++ b/libcxx/test/support/platform_support.h
@@ -75,7 +75,7 @@ inline std::string get_temp_file_name() {
// can simply generate a good guess in the temporary folder and create it.
constexpr char chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char Name[] = "/tmp/libcxx.XXXXXX";
- for (int i = 0; i < sizeof(Name); ++i)
+ for (std::size_t i = 0; i < sizeof(Name); ++i)
if (Name[i] == 'X')
Name[i] = chars[rand() % strlen(chars)];
FILE* file = fopen(filename, "w");
>From 19568ecdd8989f9b6496fdc0fa01ddb0a39142bb Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 5 Aug 2024 16:24:40 -0500
Subject: [PATCH 4/5] Update platform_support.h
---
libcxx/test/support/platform_support.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/test/support/platform_support.h b/libcxx/test/support/platform_support.h
index 0e78d1a92d32d..64c5f61cf40e0 100644
--- a/libcxx/test/support/platform_support.h
+++ b/libcxx/test/support/platform_support.h
@@ -83,7 +83,7 @@ inline std::string get_temp_file_name() {
abort();
if (fclose(file) == EOF)
abort();
- return std::string(filename);
+ return std::string(Name);
#else
std::string Name = "libcxx.XXXXXX";
int FD = mkstemp(&Name[0]);
>From cf1396f6a359f82f11939c395fdd7ccae6a83bd5 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Tue, 6 Aug 2024 12:37:43 -0500
Subject: [PATCH 5/5] Update libcxx/test/support/platform_support.h
Co-authored-by: Mark de Wever <zar-rpg at xs4all.nl>
---
libcxx/test/support/platform_support.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/test/support/platform_support.h b/libcxx/test/support/platform_support.h
index 64c5f61cf40e0..0d4fa63b03f10 100644
--- a/libcxx/test/support/platform_support.h
+++ b/libcxx/test/support/platform_support.h
@@ -71,7 +71,7 @@ inline std::string get_temp_file_name() {
abort();
}
#elif !__has_include(<unistd.h>)
- // Without `unistd.h` we cannot guarnatee that the file is unused, however we
+ // Without `unistd.h` we cannot guarantee that the file is unused, however we
// can simply generate a good guess in the temporary folder and create it.
constexpr char chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char Name[] = "/tmp/libcxx.XXXXXX";
More information about the libcxx-commits
mailing list