xref: /llvm-project/libcxx/test/support/platform_support.h (revision 64e4dd329c0c9d4c462594df004afd3102a54412)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // Define a bunch of macros that can be used in the tests instead of
10 //  implementation defined assumptions:
11 //   - locale names
12 //   - floating point number string output
13 
14 #ifndef PLATFORM_SUPPORT_H
15 #define PLATFORM_SUPPORT_H
16 
17 // locale names
18 #define LOCALE_en_US           "en_US"
19 #define LOCALE_en_US_UTF_8     "en_US.UTF-8"
20 #define LOCALE_fr_FR_UTF_8     "fr_FR.UTF-8"
21 #ifdef __linux__
22 #    define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO-8859-1"
23 #    define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO-8859-2"
24 #elif defined(_WIN32)
25 #    define LOCALE_fr_CA_ISO8859_1 "fr-CA"
26 #    define LOCALE_cs_CZ_ISO8859_2 "cs-CZ"
27 #else
28 #    define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO8859-1"
29 #    define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO8859-2"
30 #endif
31 #define LOCALE_ja_JP_UTF_8     "ja_JP.UTF-8"
32 #define LOCALE_ru_RU_UTF_8     "ru_RU.UTF-8"
33 #define LOCALE_zh_CN_UTF_8     "zh_CN.UTF-8"
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <codecvt>
38 #include <locale>
39 #include <string>
40 #if defined(_WIN32)
41 #   include <io.h> // _mktemp_s
42 #   include <fcntl.h> // _O_EXCL, ...
43 #   include <sys/stat.h> // _S_IREAD, ...
44 #else
45 #   include <unistd.h> // close
46 #endif
47 
48 #if defined(_NEWLIB_VERSION) && defined(__STRICT_ANSI__)
49 // Newlib provides this, but in the header it's under __STRICT_ANSI__
50 extern "C" {
51   int mkstemp(char*);
52 }
53 #endif
54 
55 inline
56 std::string get_temp_file_name()
57 {
58 #if defined(_WIN32)
59     while (true) {
60         char Name[] = "libcxx.XXXXXX";
61         if (_mktemp_s(Name, sizeof(Name)) != 0) abort();
62         int fd = _open(Name, _O_RDWR | _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE);
63         if (fd != -1) {
64             _close(fd);
65             return Name;
66         }
67         if (errno == EEXIST)
68             continue;
69         abort();
70     }
71 #else
72     std::string Name;
73     int FD = -1;
74     do {
75         Name = "libcxx.XXXXXX";
76         FD = mkstemp(&Name[0]);
77         if (FD == -1 && errno == EINVAL) {
78             perror("mkstemp");
79             abort();
80         }
81     } while (FD == -1);
82     close(FD);
83     return Name;
84 #endif
85 }
86 
87 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
88 inline
89 std::wstring get_wide_temp_file_name()
90 {
91     return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> >().from_bytes(
92         get_temp_file_name());
93 }
94 #endif // _LIBCPP_HAS_OPEN_WITH_WCHAR
95 
96 #if defined(_CS_GNU_LIBC_VERSION)
97 inline bool glibc_version_less_than(char const* version) {
98   std::string test_version = std::string("glibc ") + version;
99 
100   size_t n = confstr(_CS_GNU_LIBC_VERSION, nullptr, (size_t)0);
101   char *current_version = new char[n];
102   confstr(_CS_GNU_LIBC_VERSION, current_version, n);
103 
104   bool result = strverscmp(current_version, test_version.c_str()) < 0;
105   delete[] current_version;
106   return result;
107 }
108 #endif // _CS_GNU_LIBC_VERSION
109 
110 #endif // PLATFORM_SUPPORT_H
111