1 // Copyright 2011 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef NINJA_UTIL_H_ 16 #define NINJA_UTIL_H_ 17 18 #ifdef _WIN32 19 #include "win32port.h" 20 #else 21 #include <stdint.h> 22 #endif 23 24 #include <string> 25 #include <vector> 26 using namespace std; 27 28 #ifdef _MSC_VER 29 #define NORETURN __declspec(noreturn) 30 #else 31 #define NORETURN __attribute__((noreturn)) 32 #endif 33 34 /// Log a fatal message and exit. 35 NORETURN void Fatal(const char* msg, ...); 36 37 // Have a generic fall-through for different versions of C/C++. 38 #if defined(__cplusplus) && __cplusplus >= 201703L 39 #define NINJA_FALLTHROUGH [[fallthrough]] 40 #elif defined(__cplusplus) && __cplusplus >= 201103L && defined(__clang__) 41 #define NINJA_FALLTHROUGH [[clang::fallthrough]] 42 #elif defined(__cplusplus) && __cplusplus >= 201103L && defined(__GNUC__) && \ 43 __GNUC__ >= 7 44 #define NINJA_FALLTHROUGH [[gnu::fallthrough]] 45 #elif defined(__GNUC__) && __GNUC__ >= 7 // gcc 7 46 #define NINJA_FALLTHROUGH __attribute__ ((fallthrough)) 47 #else // C++11 on gcc 6, and all other cases 48 #define NINJA_FALLTHROUGH 49 #endif 50 51 /// Log a warning message. 52 void Warning(const char* msg, ...); 53 54 /// Log an error message. 55 void Error(const char* msg, ...); 56 57 /// Canonicalize a path like "foo/../bar.h" into just "bar.h". 58 /// |slash_bits| has bits set starting from lowest for a backslash that was 59 /// normalized to a forward slash. (only used on Windows) 60 bool CanonicalizePath(string* path, uint64_t* slash_bits, string* err); 61 bool CanonicalizePath(char* path, size_t* len, uint64_t* slash_bits, 62 string* err); 63 64 /// Appends |input| to |*result|, escaping according to the whims of either 65 /// Bash, or Win32's CommandLineToArgvW(). 66 /// Appends the string directly to |result| without modification if we can 67 /// determine that it contains no problematic characters. 68 void GetShellEscapedString(const string& input, string* result); 69 void GetWin32EscapedString(const string& input, string* result); 70 71 /// Read a file to a string (in text mode: with CRLF conversion 72 /// on Windows). 73 /// Returns -errno and fills in \a err on error. 74 int ReadFile(const string& path, string* contents, string* err); 75 76 /// Mark a file descriptor to not be inherited on exec()s. 77 void SetCloseOnExec(int fd); 78 79 /// Given a misspelled string and a list of correct spellings, returns 80 /// the closest match or NULL if there is no close enough match. 81 const char* SpellcheckStringV(const string& text, 82 const vector<const char*>& words); 83 84 /// Like SpellcheckStringV, but takes a NULL-terminated list. 85 const char* SpellcheckString(const char* text, ...); 86 87 bool islatinalpha(int c); 88 89 /// Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm). 90 string StripAnsiEscapeCodes(const string& in); 91 92 /// @return the number of processors on the machine. Useful for an initial 93 /// guess for how many jobs to run in parallel. @return 0 on error. 94 int GetProcessorCount(); 95 96 /// @return the load average of the machine. A negative value is returned 97 /// on error. 98 double GetLoadAverage(); 99 100 /// Elide the given string @a str with '...' in the middle if the length 101 /// exceeds @a width. 102 string ElideMiddle(const string& str, size_t width); 103 104 /// Truncates a file to the given size. 105 bool Truncate(const string& path, size_t size, string* err); 106 107 #ifdef _MSC_VER 108 #define snprintf _snprintf 109 #define fileno _fileno 110 #define unlink _unlink 111 #define chdir _chdir 112 #define strtoull _strtoui64 113 #define getcwd _getcwd 114 #define PATH_MAX _MAX_PATH 115 #endif 116 117 #ifdef _WIN32 118 /// Convert the value returned by GetLastError() into a string. 119 string GetLastErrorString(); 120 121 /// Calls Fatal() with a function name and GetLastErrorString. 122 NORETURN void Win32Fatal(const char* function, const char* hint = NULL); 123 #endif 124 125 #endif // NINJA_UTIL_H_ 126