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