1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "win32port.h"
29 #define NORETURN __declspec(noreturn)
31 #define NORETURN __attribute__((noreturn))
34 /// Log a fatal message and exit.
35 NORETURN void Fatal(const char* msg, ...);
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__) && \
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
51 /// Log a warning message.
52 void Warning(const char* msg, ...);
54 /// Log an error message.
55 void Error(const char* msg, ...);
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,
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);
71 /// Read a file to a string (in text mode: with CRLF conversion
73 /// Returns -errno and fills in \a err on error.
74 int ReadFile(const string& path, string* contents, string* err);
76 /// Mark a file descriptor to not be inherited on exec()s.
77 void SetCloseOnExec(int fd);
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);
84 /// Like SpellcheckStringV, but takes a NULL-terminated list.
85 const char* SpellcheckString(const char* text, ...);
87 bool islatinalpha(int c);
89 /// Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).
90 string StripAnsiEscapeCodes(const string& in);
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();
96 /// @return the load average of the machine. A negative value is returned
98 double GetLoadAverage();
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);
104 /// Truncates a file to the given size.
105 bool Truncate(const string& path, size_t size, string* err);
108 #define snprintf _snprintf
109 #define fileno _fileno
110 #define unlink _unlink
112 #define strtoull _strtoui64
113 #define getcwd _getcwd
114 #define PATH_MAX _MAX_PATH
118 /// Convert the value returned by GetLastError() into a string.
119 string GetLastErrorString();
121 /// Calls Fatal() with a function name and GetLastErrorString.
122 NORETURN void Win32Fatal(const char* function, const char* hint = NULL);
125 #endif // NINJA_UTIL_H_