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 <stdarg.h> |
25 | |
26 | #include <string> |
27 | #include <vector> |
28 | |
29 | #ifdef _MSC_VER |
30 | #define NORETURN __declspec(noreturn) |
31 | #else |
32 | #define NORETURN __attribute__((noreturn)) |
33 | #endif |
34 | |
35 | /// Log a fatal message and exit. |
36 | NORETURN void Fatal(const char* msg, ...); |
37 | |
38 | // Have a generic fall-through for different versions of C/C++. |
39 | #if defined(__cplusplus) && __cplusplus >= 201703L |
40 | #define NINJA_FALLTHROUGH [[fallthrough]] |
41 | #elif defined(__cplusplus) && __cplusplus >= 201103L && defined(__clang__) |
42 | #define NINJA_FALLTHROUGH [[clang::fallthrough]] |
43 | #elif defined(__cplusplus) && __cplusplus >= 201103L && defined(__GNUC__) && \ |
44 | __GNUC__ >= 7 |
45 | #define NINJA_FALLTHROUGH [[gnu::fallthrough]] |
46 | #elif defined(__GNUC__) && __GNUC__ >= 7 // gcc 7 |
47 | #define NINJA_FALLTHROUGH __attribute__ ((fallthrough)) |
48 | #else // C++11 on gcc 6, and all other cases |
49 | #define NINJA_FALLTHROUGH |
50 | #endif |
51 | |
52 | /// Log a warning message. |
53 | void Warning(const char* msg, ...); |
54 | void Warning(const char* msg, va_list ap); |
55 | |
56 | /// Log an error message. |
57 | void Error(const char* msg, ...); |
58 | void Error(const char* msg, va_list ap); |
59 | |
60 | /// Log an informational message. |
61 | void Info(const char* msg, ...); |
62 | void Info(const char* msg, va_list ap); |
63 | |
64 | /// Canonicalize a path like "foo/../bar.h" into just "bar.h". |
65 | /// |slash_bits| has bits set starting from lowest for a backslash that was |
66 | /// normalized to a forward slash. (only used on Windows) |
67 | void CanonicalizePath(std::string* path, uint64_t* slash_bits); |
68 | void CanonicalizePath(char* path, size_t* len, uint64_t* slash_bits); |
69 | |
70 | /// Appends |input| to |*result|, escaping according to the whims of either |
71 | /// Bash, or Win32's CommandLineToArgvW(). |
72 | /// Appends the string directly to |result| without modification if we can |
73 | /// determine that it contains no problematic characters. |
74 | void GetShellEscapedString(const std::string& input, std::string* result); |
75 | void GetWin32EscapedString(const std::string& input, std::string* result); |
76 | |
77 | /// Read a file to a string (in text mode: with CRLF conversion |
78 | /// on Windows). |
79 | /// Returns -errno and fills in \a err on error. |
80 | int ReadFile(const std::string& path, std::string* contents, std::string* err); |
81 | |
82 | /// Mark a file descriptor to not be inherited on exec()s. |
83 | void SetCloseOnExec(int fd); |
84 | |
85 | /// Given a misspelled string and a list of correct spellings, returns |
86 | /// the closest match or NULL if there is no close enough match. |
87 | const char* SpellcheckStringV(const std::string& text, |
88 | const std::vector<const char*>& words); |
89 | |
90 | /// Like SpellcheckStringV, but takes a NULL-terminated list. |
91 | const char* SpellcheckString(const char* text, ...); |
92 | |
93 | bool islatinalpha(int c); |
94 | |
95 | /// Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm). |
96 | std::string StripAnsiEscapeCodes(const std::string& in); |
97 | |
98 | /// @return the number of processors on the machine. Useful for an initial |
99 | /// guess for how many jobs to run in parallel. @return 0 on error. |
100 | int GetProcessorCount(); |
101 | |
102 | /// @return the load average of the machine. A negative value is returned |
103 | /// on error. |
104 | double GetLoadAverage(); |
105 | |
106 | /// Elide the given string @a str with '...' in the middle if the length |
107 | /// exceeds @a width. |
108 | std::string ElideMiddle(const std::string& str, size_t width); |
109 | |
110 | /// Truncates a file to the given size. |
111 | bool Truncate(const std::string& path, size_t size, std::string* err); |
112 | |
113 | #ifdef _MSC_VER |
114 | #define snprintf _snprintf |
115 | #define fileno _fileno |
116 | #define unlink _unlink |
117 | #define chdir _chdir |
118 | #define strtoull _strtoui64 |
119 | #define getcwd _getcwd |
120 | #define PATH_MAX _MAX_PATH |
121 | #endif |
122 | |
123 | #ifdef _WIN32 |
124 | /// Convert the value returned by GetLastError() into a string. |
125 | std::string GetLastErrorString(); |
126 | |
127 | /// Calls Fatal() with a function name and GetLastErrorString. |
128 | NORETURN void Win32Fatal(const char* function, const char* hint = NULL); |
129 | #endif |
130 | |
131 | #endif // NINJA_UTIL_H_ |
132 | |