Ninja
util.h
Go to the documentation of this file.
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 #if !defined(__has_cpp_attribute)
30 # define __has_cpp_attribute(x) 0
31 #endif
32 
33 #if __has_cpp_attribute(noreturn)
34 # define NORETURN [[noreturn]]
35 #else
36 # define NORETURN // nothing for old compilers
37 #endif
38 
39 /// Log a fatal message and exit.
40 NORETURN void Fatal(const char* msg, ...);
41 
42 // Have a generic fall-through for different versions of C/C++.
43 #if __has_cpp_attribute(fallthrough)
44 # define NINJA_FALLTHROUGH [[fallthrough]]
45 #elif defined(__clang__)
46 # define NINJA_FALLTHROUGH [[clang::fallthrough]]
47 #else
48 # define NINJA_FALLTHROUGH // nothing
49 #endif
50 
51 /// Log a warning message.
52 void Warning(const char* msg, ...);
53 void Warning(const char* msg, va_list ap);
54 
55 /// Log an error message.
56 void Error(const char* msg, ...);
57 void Error(const char* msg, va_list ap);
58 
59 /// Log an informational message.
60 void Info(const char* msg, ...);
61 void Info(const char* msg, va_list ap);
62 
63 /// Canonicalize a path like "foo/../bar.h" into just "bar.h".
64 /// |slash_bits| has bits set starting from lowest for a backslash that was
65 /// normalized to a forward slash. (only used on Windows)
66 void CanonicalizePath(std::string* path, uint64_t* slash_bits);
67 void CanonicalizePath(char* path, size_t* len, uint64_t* slash_bits);
68 
69 /// Appends |input| to |*result|, escaping according to the whims of either
70 /// Bash, or Win32's CommandLineToArgvW().
71 /// Appends the string directly to |result| without modification if we can
72 /// determine that it contains no problematic characters.
73 void GetShellEscapedString(const std::string& input, std::string* result);
74 void GetWin32EscapedString(const std::string& input, std::string* result);
75 
76 /// Read a file to a string (in text mode: with CRLF conversion
77 /// on Windows).
78 /// Returns -errno and fills in \a err on error.
79 int ReadFile(const std::string& path, std::string* contents, std::string* err);
80 
81 /// Mark a file descriptor to not be inherited on exec()s.
82 void SetCloseOnExec(int fd);
83 
84 /// Given a misspelled string and a list of correct spellings, returns
85 /// the closest match or NULL if there is no close enough match.
86 const char* SpellcheckStringV(const std::string& text,
87  const std::vector<const char*>& words);
88 
89 /// Like SpellcheckStringV, but takes a NULL-terminated list.
90 const char* SpellcheckString(const char* text, ...);
91 
92 bool islatinalpha(int c);
93 
94 /// Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).
95 std::string StripAnsiEscapeCodes(const std::string& in);
96 
97 /// @return the number of processors on the machine. Useful for an initial
98 /// guess for how many jobs to run in parallel. @return 0 on error.
99 int GetProcessorCount();
100 
101 /// @return the load average of the machine. A negative value is returned
102 /// on error.
103 double GetLoadAverage();
104 
105 /// a wrapper for getcwd()
106 std::string GetWorkingDirectory();
107 
108 /// Truncates a file to the given size.
109 bool Truncate(const std::string& path, size_t size, std::string* err);
110 
111 #ifdef _MSC_VER
112 #define snprintf _snprintf
113 #define fileno _fileno
114 #define chdir _chdir
115 #define strtoull _strtoui64
116 #define getcwd _getcwd
117 #define PATH_MAX _MAX_PATH
118 #endif
119 
120 #ifdef _WIN32
121 /// Convert the value returned by GetLastError() into a string.
122 std::string GetLastErrorString();
123 
124 /// Calls Fatal() with a function name and GetLastErrorString.
125 NORETURN void Win32Fatal(const char* function, const char* hint = NULL);
126 
127 /// Naive implementation of C++ 20 std::bit_cast(), used to fix Clang and GCC
128 /// [-Wcast-function-type] warning on casting result of GetProcAddress().
129 template <class To, class From>
130 inline To FunctionCast(From from) {
131  static_assert(sizeof(To) == sizeof(From), "");
132  To result;
133  memcpy(&result, &from, sizeof(To));
134  return result;
135 }
136 #endif
137 
138 int platformAwareUnlink(const char* filename);
139 
140 #endif // NINJA_UTIL_H_
int GetProcessorCount()
Definition: util.cc:814
void CanonicalizePath(std::string *path, uint64_t *slash_bits)
Canonicalize a path like "foo/../bar.h" into just "bar.h".
const char * SpellcheckString(const char *text,...)
Like SpellcheckStringV, but takes a NULL-terminated list.
Definition: util.cc:517
int platformAwareUnlink(const char *filename)
Definition: util.cc:1025
bool islatinalpha(int c)
Definition: util.cc:566
const char * SpellcheckStringV(const std::string &text, const std::vector< const char * > &words)
Given a misspelled string and a list of correct spellings, returns the closest match or NULL if there...
std::string GetWorkingDirectory()
a wrapper for getcwd()
Definition: util.cc:992
void GetWin32EscapedString(const std::string &input, std::string *result)
void Error(const char *msg,...)
Log an error message.
Definition: util.cc:104
void SetCloseOnExec(int fd)
Mark a file descriptor to not be inherited on exec()s.
Definition: util.cc:480
#define NORETURN
Definition: util.h:36
bool Truncate(const std::string &path, size_t size, std::string *err)
Truncates a file to the given size.
void Warning(const char *msg,...)
Log a warning message.
Definition: util.cc:91
void GetShellEscapedString(const std::string &input, std::string *result)
Appends |input| to |*result|, escaping according to the whims of either Bash, or Win32's CommandLineT...
int ReadFile(const std::string &path, std::string *contents, std::string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
void Info(const char *msg,...)
Log an informational message.
Definition: util.cc:117
NORETURN void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:67
std::string StripAnsiEscapeCodes(const std::string &in)
Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).
double GetLoadAverage()
Definition: util.cc:981
unsigned long long uint64_t
Definition: win32port.h:29