Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | // blaze_util.h: Miscellaneous utility functions used by the blaze.cc |
| 16 | // Blaze client. |
| 17 | // |
| 18 | |
Han-Wen Nienhuys | 0d56b6c | 2015-05-12 14:52:30 +0000 | [diff] [blame] | 19 | #ifndef BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_H_ |
| 20 | #define BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_H_ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | |
Kristina Chodorow | 6d7b7ff | 2015-05-13 16:59:49 +0000 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Laszlo Csomor | 26b7a66 | 2016-11-25 13:46:30 +0000 | [diff] [blame^] | 24 | #include <functional> |
Googler | 9588b81 | 2015-07-23 11:49:37 +0000 | [diff] [blame] | 25 | #include <sstream> |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | namespace blaze { |
| 30 | |
Thiago Farina | 048bbfc | 2016-09-21 08:20:41 +0000 | [diff] [blame] | 31 | extern const char kServerPidFile[]; |
Laszlo Csomor | 6450c18 | 2016-11-24 10:28:20 +0000 | [diff] [blame] | 32 | |
| 33 | // TODO(laszlocsomor) 2016-11-21: remove kServerPidSymlink after 2017-05-01 |
| 34 | // (~half a year from writing this comment). By that time old Bazel clients that |
| 35 | // used to write PID symlinks will probably no longer be in use. |
Thiago Farina | 048bbfc | 2016-09-21 08:20:41 +0000 | [diff] [blame] | 36 | extern const char kServerPidSymlink[]; |
Lukacs Berki | d9da60f | 2016-04-26 11:40:24 +0000 | [diff] [blame] | 37 | |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 38 | std::string GetUserName(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | // Returns the given path in absolute form. Does not change paths that are |
| 41 | // already absolute. |
| 42 | // |
| 43 | // If called from working directory "/bar": |
| 44 | // MakeAbsolute("foo") --> "/bar/foo" |
| 45 | // MakeAbsolute("/foo") ---> "/foo" |
Thiago Farina | ac61956 | 2016-04-22 17:44:14 +0000 | [diff] [blame] | 46 | // MakeAbsolute("C:/foo") ---> "C:/foo" |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 47 | std::string MakeAbsolute(const std::string &path); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | // Replaces 'content' with contents of file 'filename'. |
Laszlo Csomor | bc3f264 | 2016-11-22 14:02:07 +0000 | [diff] [blame] | 50 | // If `max_size` is positive, the method reads at most that many bytes; |
| 51 | // otherwise the method reads the whole file. |
Lukacs Berki | 793cd01 | 2016-06-20 09:48:47 +0000 | [diff] [blame] | 52 | // Returns false on error. Can be called from a signal handler. |
Laszlo Csomor | ae16e76 | 2016-11-18 10:16:08 +0000 | [diff] [blame] | 53 | bool ReadFile(const std::string &filename, std::string *content, |
Laszlo Csomor | bc3f264 | 2016-11-22 14:02:07 +0000 | [diff] [blame] | 54 | int max_size = 0); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | |
Laszlo Csomor | 26b7a66 | 2016-11-25 13:46:30 +0000 | [diff] [blame^] | 56 | // Replaces 'content' with data read from a source using `read_func`. |
| 57 | // If `max_size` is positive, the method reads at most that many bytes; |
| 58 | // otherwise the method reads everything. |
Lukacs Berki | 793cd01 | 2016-06-20 09:48:47 +0000 | [diff] [blame] | 59 | // Returns false on error. Can be called from a signal handler. |
Laszlo Csomor | 26b7a66 | 2016-11-25 13:46:30 +0000 | [diff] [blame^] | 60 | bool ReadFrom(const std::function<int(void *, int)> &read_func, |
| 61 | std::string *content, int max_size = 0); |
Damien Martin-Guillerez | a73ab64 | 2015-02-10 14:53:25 +0000 | [diff] [blame] | 62 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | // Writes 'content' into file 'filename', and makes it executable. |
| 64 | // Returns false on failure, sets errno. |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 65 | bool WriteFile(const std::string &content, const std::string &filename); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | |
Laszlo Csomor | ae16e76 | 2016-11-18 10:16:08 +0000 | [diff] [blame] | 67 | // Writes `size` bytes from `data` into file 'filename' and makes it executable. |
| 68 | // Returns false on failure, sets errno. |
| 69 | bool WriteFile(const void* data, size_t size, const std::string &filename); |
| 70 | |
Laszlo Csomor | 26b7a66 | 2016-11-25 13:46:30 +0000 | [diff] [blame^] | 71 | // Writes `size` bytes from `data` into a destination using `write_func`. |
| 72 | // Returns false on failure, sets errno. |
| 73 | bool WriteTo(const std::function<int(const void *, size_t)> &write_func, |
| 74 | const void *data, size_t size); |
| 75 | |
Thiago Farina | 4e4ffd2 | 2016-03-09 17:02:28 +0000 | [diff] [blame] | 76 | // Unlinks the file given by 'file_path'. |
| 77 | // Returns true on success. In case of failure sets errno. |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 78 | bool UnlinkPath(const std::string &file_path); |
Nathan Harmata | bf98f39 | 2016-01-07 22:58:29 +0000 | [diff] [blame] | 79 | |
Googler | 957f374 | 2016-08-31 18:16:24 +0000 | [diff] [blame] | 80 | // Returns true iff the current terminal is running inside an Emacs. |
| 81 | bool IsEmacsTerminal(); |
| 82 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 83 | // Returns true iff the current terminal can support color and cursor movement. |
| 84 | bool IsStandardTerminal(); |
| 85 | |
| 86 | // Returns the number of columns of the terminal to which stdout is |
| 87 | // connected, or 80 if there is no such terminal. |
| 88 | int GetTerminalColumns(); |
| 89 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | // If 'arg' matches 'key=value', returns address of 'value'. |
| 91 | // If it matches 'key' alone, returns address of next_arg. |
| 92 | // Returns NULL otherwise. |
Thiago Farina | fdc7f98 | 2015-04-27 23:01:34 +0000 | [diff] [blame] | 93 | const char* GetUnaryOption(const char *arg, |
| 94 | const char *next_arg, |
| 95 | const char *key); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 96 | |
| 97 | // Returns true iff 'arg' equals 'key'. |
| 98 | // Dies with a syntax error if arg starts with 'key='. |
Thiago Farina | d689475 | 2016-07-26 08:59:40 +0000 | [diff] [blame] | 99 | // Returns false otherwise. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | bool GetNullaryOption(const char *arg, const char *key); |
| 101 | |
Thiago Farina | 5cb7eee | 2015-04-02 12:59:26 +0000 | [diff] [blame] | 102 | // Enable messages mostly of interest to developers. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 103 | bool VerboseLogging(); |
| 104 | |
Lukacs Berki | 00d613a | 2016-04-25 15:39:28 +0000 | [diff] [blame] | 105 | // Read the JVM version from a string. The string should contain the output of a |
| 106 | // "java -version" execution and is supposed to contain a string of the form |
| 107 | // 'version "version-number"' in the first 255 bytes. If the string is found, |
| 108 | // version-number is returned, else the empty string is returned. |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 109 | std::string ReadJvmVersion(const std::string &version_string); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 110 | |
| 111 | // Get the version string from the given java executable. The java executable |
| 112 | // is supposed to output a string in the form '.*version ".*".*'. This method |
| 113 | // will return the part in between the two quote or the empty string on failure |
| 114 | // to match the good string. |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 115 | std::string GetJvmVersion(const std::string &java_exe); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 116 | |
| 117 | // Returns true iff jvm_version is at least the version specified by |
| 118 | // version_spec. |
| 119 | // jvm_version is supposed to be a string specifying a java runtime version |
| 120 | // as specified by the JSR-56 appendix A. version_spec is supposed to be a |
| 121 | // version is the format [0-9]+(.[1-9]+)*. |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 122 | bool CheckJavaVersionIsAtLeast(const std::string &jvm_version, |
| 123 | const std::string &version_spec); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 124 | |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 125 | // Returns true iff arg is a valid command line argument for bazel. |
| 126 | bool IsArg(const std::string& arg); |
| 127 | |
Laszlo Csomor | 74ffaf7 | 2016-11-24 12:17:20 +0000 | [diff] [blame] | 128 | // Returns the string representation of `value`. |
Googler | 9588b81 | 2015-07-23 11:49:37 +0000 | [diff] [blame] | 129 | // Workaround for mingw where std::to_string is not implemented. |
| 130 | // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015. |
| 131 | template <typename T> |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 132 | std::string ToString(const T &value) { |
Googler | 9588b81 | 2015-07-23 11:49:37 +0000 | [diff] [blame] | 133 | std::ostringstream oss; |
| 134 | oss << value; |
| 135 | return oss.str(); |
| 136 | } |
| 137 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 138 | } // namespace blaze |
Thiago Farina | fdc7f98 | 2015-04-27 23:01:34 +0000 | [diff] [blame] | 139 | |
Han-Wen Nienhuys | 0d56b6c | 2015-05-12 14:52:30 +0000 | [diff] [blame] | 140 | #endif // BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_H_ |