xref: /llvm-project/libcxx/test/support/platform_support.h (revision 3ee9a50a146c585fc92f79e742c538261f98bd4e)
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 #include "test_macros.h"
18 
19 // locale names
20 #define LOCALE_en_US           "en_US"
21 #define LOCALE_en_US_UTF_8     "en_US.UTF-8"
22 #define LOCALE_fr_FR_UTF_8     "fr_FR.UTF-8"
23 #ifdef __linux__
24 #    define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO-8859-1"
25 #    define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO-8859-2"
26 #elif defined(_WIN32)
27 #    define LOCALE_fr_CA_ISO8859_1 "fr-CA"
28 #    define LOCALE_cs_CZ_ISO8859_2 "cs-CZ"
29 #else
30 #    define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO8859-1"
31 #    define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO8859-2"
32 #endif
33 #define LOCALE_ja_JP_UTF_8     "ja_JP.UTF-8"
34 #define LOCALE_ru_RU_UTF_8     "ru_RU.UTF-8"
35 #define LOCALE_zh_CN_UTF_8     "zh_CN.UTF-8"
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <codecvt>
40 #include <locale>
41 #include <string>
42 #if defined(_WIN32)
43 #   include <io.h> // _mktemp_s
44 #   include <fcntl.h> // _O_EXCL, ...
45 #   include <sys/stat.h> // _S_IREAD, ...
46 #else
47 #   include <unistd.h> // close
48 #endif
49 
50 #if defined(_CS_GNU_LIBC_VERSION)
51 # include <string.h> // strverscmp
52 #endif
53 
54 #if defined(_NEWLIB_VERSION) && defined(__STRICT_ANSI__)
55 // Newlib provides this, but in the header it's under __STRICT_ANSI__
56 extern "C" {
57   int mkstemp(char*);
58 }
59 #endif
60 
61 inline
62 std::string get_temp_file_name()
63 {
64 #if defined(_WIN32)
65     while (true) {
66         char Name[] = "libcxx.XXXXXX";
67         if (_mktemp_s(Name, sizeof(Name)) != 0) abort();
68         int fd = _open(Name, _O_RDWR | _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE);
69         if (fd != -1) {
70             _close(fd);
71             return Name;
72         }
73         if (errno == EEXIST)
74             continue;
75         abort();
76     }
77 #else
78     std::string Name;
79     int FD = -1;
80     do {
81         Name = "libcxx.XXXXXX";
82         FD = mkstemp(&Name[0]);
83         if (FD == -1 && errno == EINVAL) {
84             perror("mkstemp");
85             abort();
86         }
87     } while (FD == -1);
88     close(FD);
89     return Name;
90 #endif
91 }
92 
93 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
94 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
95 inline
96 std::wstring get_wide_temp_file_name()
97 {
98     return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> >().from_bytes(
99         get_temp_file_name());
100 }
101 #endif // _LIBCPP_HAS_OPEN_WITH_WCHAR
102 _LIBCPP_SUPPRESS_DEPRECATED_POP
103 
104 #if defined(_CS_GNU_LIBC_VERSION)
105 inline bool glibc_version_less_than(char const* version) {
106   std::string test_version = std::string("glibc ") + version;
107 
108   size_t n = confstr(_CS_GNU_LIBC_VERSION, nullptr, (size_t)0);
109   char *current_version = new char[n];
110   confstr(_CS_GNU_LIBC_VERSION, current_version, n);
111 
112   bool result = strverscmp(current_version, test_version.c_str()) < 0;
113   delete[] current_version;
114   return result;
115 }
116 #endif // _CS_GNU_LIBC_VERSION
117 
118 #endif // PLATFORM_SUPPORT_H
119