Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1 | // Copyright 2014 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 | // blaze_util.h: Miscellaneous utility functions used by the blaze.cc |
| 16 | // Blaze client. |
| 17 | // |
| 18 | |
Thiago Farina | fdc7f98 | 2015-04-27 23:01:34 +0000 | [diff] [blame^] | 19 | #ifndef DEVTOOLS_BLAZE_MAIN_BLAZE_UTIL_H_ |
| 20 | #define DEVTOOLS_BLAZE_MAIN_BLAZE_UTIL_H_ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | namespace blaze { |
| 26 | |
| 27 | using std::string; |
| 28 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | string GetUserName(); |
| 30 | |
| 31 | // Return the path to the JVM launcher. |
| 32 | string GetJvm(); |
| 33 | |
| 34 | // Returns the given path in absolute form. Does not change paths that are |
| 35 | // already absolute. |
| 36 | // |
| 37 | // If called from working directory "/bar": |
| 38 | // MakeAbsolute("foo") --> "/bar/foo" |
| 39 | // MakeAbsolute("/foo") ---> "/foo" |
| 40 | string MakeAbsolute(string path); |
| 41 | |
| 42 | // mkdir -p path. All newly created directories use the given mode. |
| 43 | // Returns -1 on failure, sets errno. |
| 44 | int MakeDirectories(string path, int mode); |
| 45 | |
| 46 | // Replaces 'content' with contents of file 'filename'. |
| 47 | // Returns false on error. |
| 48 | bool ReadFile(const string &filename, string *content); |
| 49 | |
Damien Martin-Guillerez | a73ab64 | 2015-02-10 14:53:25 +0000 | [diff] [blame] | 50 | // Replaces 'content' with contents of file descriptor 'fd'. |
| 51 | // Returns false on error. |
| 52 | bool ReadFileDescriptor(int fd, string *content); |
| 53 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | // Writes 'content' into file 'filename', and makes it executable. |
| 55 | // Returns false on failure, sets errno. |
| 56 | bool WriteFile(const string &content, const string &filename); |
| 57 | |
| 58 | // Returns true iff the current terminal can support color and cursor movement. |
| 59 | bool IsStandardTerminal(); |
| 60 | |
| 61 | // Returns the number of columns of the terminal to which stdout is |
| 62 | // connected, or 80 if there is no such terminal. |
| 63 | int GetTerminalColumns(); |
| 64 | |
| 65 | // blaze's JVM arch is set at build time (--java_cpu), since the blaze java |
| 66 | // process includes native code. |
| 67 | bool Is64BitBlazeJavabase(); |
| 68 | |
| 69 | // Adds JVM arguments particular to running blaze with JVM v3 or higher. |
| 70 | void AddJVMSpecificArguments(const string &host_javabase, |
| 71 | std::vector<string> *result); |
| 72 | |
| 73 | void ExecuteProgram(string exe, const std::vector<string>& args_vector); |
| 74 | |
| 75 | void ReExecute(const string &executable, int argc, const char *argv[]); |
| 76 | |
| 77 | // If 'arg' matches 'key=value', returns address of 'value'. |
| 78 | // If it matches 'key' alone, returns address of next_arg. |
| 79 | // Returns NULL otherwise. |
Thiago Farina | fdc7f98 | 2015-04-27 23:01:34 +0000 | [diff] [blame^] | 80 | const char* GetUnaryOption(const char *arg, |
| 81 | const char *next_arg, |
| 82 | const char *key); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 83 | |
| 84 | // Returns true iff 'arg' equals 'key'. |
| 85 | // Dies with a syntax error if arg starts with 'key='. |
| 86 | // Returns NULL otherwise. |
| 87 | bool GetNullaryOption(const char *arg, const char *key); |
| 88 | |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 89 | bool CheckValidPort(const string &str, const string &option, string *error); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | |
Thiago Farina | 5cb7eee | 2015-04-02 12:59:26 +0000 | [diff] [blame] | 91 | // Enable messages mostly of interest to developers. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 92 | bool VerboseLogging(); |
| 93 | |
| 94 | // Read the JVM version from a file descriptor. The fd should point |
| 95 | // to the output of a "java -version" execution and is supposed to contains |
| 96 | // a string of the form 'version "version-number"' in the first 255 bytes. |
| 97 | // If the string is found, version-number is returned, else the empty string |
| 98 | // is returned. |
| 99 | string ReadJvmVersion(int fd); |
| 100 | |
| 101 | // Get the version string from the given java executable. The java executable |
| 102 | // is supposed to output a string in the form '.*version ".*".*'. This method |
| 103 | // will return the part in between the two quote or the empty string on failure |
| 104 | // to match the good string. |
| 105 | string GetJvmVersion(string java_exe); |
| 106 | |
| 107 | // Returns true iff jvm_version is at least the version specified by |
| 108 | // version_spec. |
| 109 | // jvm_version is supposed to be a string specifying a java runtime version |
| 110 | // as specified by the JSR-56 appendix A. version_spec is supposed to be a |
| 111 | // version is the format [0-9]+(.[1-9]+)*. |
| 112 | bool CheckJavaVersionIsAtLeast(string jvm_version, string version_spec); |
| 113 | |
| 114 | } // namespace blaze |
Thiago Farina | fdc7f98 | 2015-04-27 23:01:34 +0000 | [diff] [blame^] | 115 | |
| 116 | #endif // DEVTOOLS_BLAZE_MAIN_BLAZE_UTIL_H_ |