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_UTF_8 "en_US.UTF-8" 21 #define LOCALE_fr_FR_UTF_8 "fr_FR.UTF-8" 22 #ifdef __linux__ 23 # define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO-8859-1" 24 # define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO-8859-2" 25 #elif defined(_WIN32) 26 # define LOCALE_fr_CA_ISO8859_1 "fr-CA" 27 # define LOCALE_cs_CZ_ISO8859_2 "cs-CZ" 28 #else 29 # define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO8859-1" 30 # define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO8859-2" 31 #endif 32 #define LOCALE_ja_JP_UTF_8 "ja_JP.UTF-8" 33 #define LOCALE_ru_RU_UTF_8 "ru_RU.UTF-8" 34 #define LOCALE_zh_CN_UTF_8 "zh_CN.UTF-8" 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string> 39 #if defined(_WIN32) 40 # include <io.h> // _mktemp_s 41 # include <fcntl.h> // _O_EXCL, ... 42 # include <sys/stat.h> // _S_IREAD, ... 43 #elif __has_include(<unistd.h>) 44 # include <unistd.h> // close 45 #endif 46 47 #if defined(_CS_GNU_LIBC_VERSION) 48 # include <string.h> // strverscmp 49 #endif 50 51 #if defined(_NEWLIB_VERSION) && defined(__STRICT_ANSI__) 52 // Newlib provides this, but in the header it's under __STRICT_ANSI__ 53 extern "C" { 54 int mkstemp(char*); 55 } 56 #endif 57 58 inline std::string get_temp_file_name() { 59 #if defined(_WIN32) 60 while (true) { 61 char Name[] = "libcxx.XXXXXX"; 62 if (_mktemp_s(Name, sizeof(Name)) != 0) 63 abort(); 64 int fd = _open(Name, _O_RDWR | _O_CREAT | _O_EXCL, _S_IREAD | _S_IWRITE); 65 if (fd != -1) { 66 _close(fd); 67 return Name; 68 } 69 if (errno == EEXIST) 70 continue; 71 abort(); 72 } 73 #elif !__has_include(<unistd.h>) 74 // Without `unistd.h` we cannot guarantee that the file is unused, however we 75 // can simply generate a good guess in the temporary folder and create it. 76 constexpr char chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 77 char Name[] = "/tmp/libcxx.XXXXXX"; 78 for (std::size_t i = 0; i < sizeof(Name); ++i) 79 if (Name[i] == 'X') 80 Name[i] = chars[rand() % strlen(chars)]; 81 FILE* file = fopen(Name, "w"); 82 if (!file) 83 abort(); 84 if (fclose(file) == EOF) 85 abort(); 86 return std::string(Name); 87 #else 88 std::string Name = "libcxx.XXXXXX"; 89 int FD = mkstemp(&Name[0]); 90 if (FD == -1) { 91 perror("mkstemp"); 92 abort(); 93 } 94 close(FD); 95 return Name; 96 #endif 97 } 98 99 #if defined(_CS_GNU_LIBC_VERSION) 100 inline bool glibc_version_less_than(char const* version) { 101 std::string test_version = std::string("glibc ") + version; 102 103 std::size_t n = confstr(_CS_GNU_LIBC_VERSION, nullptr, (size_t)0); 104 char *current_version = new char[n]; 105 confstr(_CS_GNU_LIBC_VERSION, current_version, n); 106 107 bool result = strverscmp(current_version, test_version.c_str()) < 0; 108 delete[] current_version; 109 return result; 110 } 111 #endif // _CS_GNU_LIBC_VERSION 112 113 #endif // PLATFORM_SUPPORT_H 114