xref: /llvm-project/libcxx/test/support/platform_support.h (revision d07e5c23b40078dcae13f76b091c9e18763ae44a)
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 #ifdef _WIN32
19     // WARNING: Windows does not support UTF-8 codepages.
20     // Locales are "converted" using https://docs.moodle.org/dev/Table_of_locales
21 #   define LOCALE_en_US           "en-US"
22 #   define LOCALE_en_US_UTF_8     "en-US"
23 #   define LOCALE_cs_CZ_ISO8859_2 "cs-CZ"
24 #   define LOCALE_fr_FR_UTF_8     "fr-FR"
25 #   define LOCALE_fr_CA_ISO8859_1 "fr-CA"
26 #   define LOCALE_ru_RU_UTF_8     "ru-RU"
27 #   define LOCALE_zh_CN_UTF_8     "zh-CN"
28 #elif defined(__CloudABI__)
29     // Timezones are integrated into locales through LC_TIMEZONE_MASK on
30     // CloudABI. LC_ALL_MASK can only be used if a timezone has also been
31     // provided. UTC should be all right.
32 #   define LOCALE_en_US           "en_US"
33 #   define LOCALE_en_US_UTF_8     "en_US.UTF-8@UTC"
34 #   define LOCALE_fr_FR_UTF_8     "fr_FR.UTF-8@UTC"
35 #   define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO-8859-1@UTC"
36 #   define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO-8859-2@UTC"
37 #   define LOCALE_ru_RU_UTF_8     "ru_RU.UTF-8@UTC"
38 #   define LOCALE_zh_CN_UTF_8     "zh_CN.UTF-8@UTC"
39 #else
40 #   define LOCALE_en_US           "en_US"
41 #   define LOCALE_en_US_UTF_8     "en_US.UTF-8"
42 #   define LOCALE_fr_FR_UTF_8     "fr_FR.UTF-8"
43 #   ifdef __linux__
44 #       define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO-8859-1"
45 #       define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO-8859-2"
46 #   else
47 #       define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO8859-1"
48 #       define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO8859-2"
49 #   endif
50 #   define LOCALE_ru_RU_UTF_8     "ru_RU.UTF-8"
51 #   define LOCALE_zh_CN_UTF_8     "zh_CN.UTF-8"
52 #endif
53 
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <codecvt>
57 #include <locale>
58 #include <string>
59 #if defined(_WIN32)
60 #   define WIN32_LEAN_AND_MEAN // Reduce overhead of including windows.h
61 #   include <io.h> // _mktemp_s
62 #   include <windows.h> // MAX_PATH, GetTempPath, GetTempFileName
63 #else
64 #   include <unistd.h> // close
65 #endif
66 
67 #if defined(_NEWLIB_VERSION) && defined(__STRICT_ANSI__)
68 // Newlib provides this, but in the header it's under __STRICT_ANSI__
69 extern "C" {
70   int mkstemp(char*);
71 }
72 #endif
73 
74 #ifndef __CloudABI__
75 inline
76 std::string get_temp_file_name()
77 {
78 #if defined(__MINGW32__)
79     char Path[MAX_PATH + 1];
80     char FN[MAX_PATH + 1];
81     do { } while (0 == GetTempPath(MAX_PATH+1, Path));
82     do { } while (0 == GetTempFileName(Path, "libcxx", 0, FN));
83     return FN;
84 #elif defined(_WIN32)
85     char Name[] = "libcxx.XXXXXX";
86     if (_mktemp_s(Name, sizeof(Name)) != 0) abort();
87     return Name;
88 #else
89     std::string Name;
90     int FD = -1;
91     do {
92         Name = "libcxx.XXXXXX";
93         FD = mkstemp(&Name[0]);
94         if (FD == -1 && errno == EINVAL) {
95             perror("mkstemp");
96             abort();
97         }
98     } while (FD == -1);
99     close(FD);
100     return Name;
101 #endif
102 }
103 
104 #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
105 inline
106 std::wstring get_wide_temp_file_name()
107 {
108     return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> >().from_bytes(
109         get_temp_file_name());
110 }
111 #endif // _LIBCPP_HAS_OPEN_WITH_WCHAR
112 
113 #endif // __CloudABI__
114 
115 #if defined(_CS_GNU_LIBC_VERSION)
116 inline bool glibc_version_less_than(char const* version) {
117   std::string test_version = std::string("glibc ") + version;
118 
119   size_t n = confstr(_CS_GNU_LIBC_VERSION, nullptr, (size_t)0);
120   char *current_version = new char[n];
121   confstr(_CS_GNU_LIBC_VERSION, current_version, n);
122 
123   bool result = strverscmp(current_version, test_version.c_str()) < 0;
124   delete[] current_version;
125   return result;
126 }
127 #endif // _CS_GNU_LIBC_VERSION
128 
129 #endif // PLATFORM_SUPPORT_H
130