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 
27 #ifdef _MSC_VER
28 #define NORETURN __declspec(noreturn)
29 #else
30 #define NORETURN __attribute__((noreturn))
31 #endif
32 
33 /// Log a fatal message and exit.
34 NORETURN void Fatal(const char* msg, ...);
35 
36 // Have a generic fall-through for different versions of C/C++.
37 #if defined(__cplusplus) && __cplusplus >= 201703L
38 #define NINJA_FALLTHROUGH [[fallthrough]]
39 #elif defined(__cplusplus) && __cplusplus >= 201103L && defined(__clang__)
40 #define NINJA_FALLTHROUGH [[clang::fallthrough]]
41 #elif defined(__cplusplus) && __cplusplus >= 201103L && defined(__GNUC__) && \
42     __GNUC__ >= 7
43 #define NINJA_FALLTHROUGH [[gnu::fallthrough]]
44 #elif defined(__GNUC__) && __GNUC__ >= 7 // gcc 7
45 #define NINJA_FALLTHROUGH __attribute__ ((fallthrough))
46 #else // C++11 on gcc 6, and all other cases
47 #define NINJA_FALLTHROUGH
48 #endif
49 
50 /// Log a warning message.
51 void Warning(const char* msg, ...);
52 
53 /// Log an error message.
54 void Error(const char* msg, ...);
55 
56 /// Canonicalize a path like "foo/../bar.h" into just "bar.h".
57 /// |slash_bits| has bits set starting from lowest for a backslash that was
58 /// normalized to a forward slash. (only used on Windows)
59 bool CanonicalizePath(std::string* path, uint64_t* slash_bits,
60                       std::string* err);
61 bool CanonicalizePath(char* path, size_t* len, uint64_t* slash_bits,
62                       std::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 std::string& input, std::string* result);
69 void GetWin32EscapedString(const std::string& input, std::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 std::string& path, std::string* contents, std::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 std::string& text,
82                               const std::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 std::string StripAnsiEscapeCodes(const std::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 std::string ElideMiddle(const std::string& str, size_t width);
103 
104 /// Truncates a file to the given size.
105 bool Truncate(const std::string& path, size_t size, std::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 std::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