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 | |
lberki | d3d37f0 | 2018-04-18 11:44:14 -0700 | [diff] [blame] | 24 | #include <map> |
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 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | // If 'arg' matches 'key=value', returns address of 'value'. |
| 34 | // If it matches 'key' alone, returns address of next_arg. |
| 35 | // Returns NULL otherwise. |
Thiago Farina | fdc7f98 | 2015-04-27 23:01:34 +0000 | [diff] [blame] | 36 | const char* GetUnaryOption(const char *arg, |
| 37 | const char *next_arg, |
| 38 | const char *key); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | |
| 40 | // Returns true iff 'arg' equals 'key'. |
| 41 | // Dies with a syntax error if arg starts with 'key='. |
Thiago Farina | d689475 | 2016-07-26 08:59:40 +0000 | [diff] [blame] | 42 | // Returns false otherwise. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | bool GetNullaryOption(const char *arg, const char *key); |
| 44 | |
Luis Fernando Pino Duque | d56e1c6 | 2017-03-24 18:04:00 +0000 | [diff] [blame] | 45 | // Searches for 'key' in 'args' using GetUnaryOption. Arguments found after '--' |
| 46 | // are omitted from the search. |
mschaller | fd37b51 | 2017-07-11 18:21:36 +0200 | [diff] [blame] | 47 | // Returns the value of the 'key' flag iff it occurs in args. |
| 48 | // Returns NULL otherwise. |
Luis Fernando Pino Duque | d5e008c | 2016-12-08 16:59:16 +0000 | [diff] [blame] | 49 | const char* SearchUnaryOption(const std::vector<std::string>& args, |
| 50 | const char* key); |
| 51 | |
ajmichael | 1b3569a | 2018-01-24 07:43:32 -0800 | [diff] [blame] | 52 | // Searches for '--flag_name' and '--noflag_name' in 'args' using |
| 53 | // GetNullaryOption. Arguments found after '--' are omitted from the search. |
| 54 | // Returns true if '--flag_name' is a flag in args and '--noflag_name' does not |
| 55 | // appear after its last occurrence. If neither '--flag_name' nor |
| 56 | // '--noflag_name' appear, returns 'default_value'. Otherwise, returns false. |
Luis Fernando Pino Duque | d5e008c | 2016-12-08 16:59:16 +0000 | [diff] [blame] | 57 | bool SearchNullaryOption(const std::vector<std::string>& args, |
ajmichael | 1b3569a | 2018-01-24 07:43:32 -0800 | [diff] [blame] | 58 | const std::string& flag_name, |
| 59 | const bool default_value); |
Luis Fernando Pino Duque | d5e008c | 2016-12-08 16:59:16 +0000 | [diff] [blame] | 60 | |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 61 | // Returns true iff arg is a valid command line argument for bazel. |
| 62 | bool IsArg(const std::string& arg); |
| 63 | |
ccalvarin | 755278d | 2018-06-07 11:05:54 -0700 | [diff] [blame] | 64 | // Returns the flag value as an absolute path. For legacy reasons, it accepts |
| 65 | // the empty string as cwd. |
| 66 | // TODO(b/109874628): Assess if removing the empty string case would break |
| 67 | // legitimate uses, and if not, remove it. |
| 68 | std::string AbsolutePathFromFlag(const std::string& value); |
| 69 | |
mschaller | fd37b51 | 2017-07-11 18:21:36 +0200 | [diff] [blame] | 70 | // Wait to see if the server process terminates. Checks the server's status |
| 71 | // immediately, and repeats the check every 100ms until approximately |
| 72 | // wait_seconds elapses or the server process terminates. Returns true if a |
| 73 | // check sees that the server process terminated. Logs to stderr after 5, 10, |
| 74 | // and 30 seconds if the wait lasts that long. |
| 75 | bool AwaitServerProcessTermination(int pid, const std::string& output_base, |
| 76 | unsigned int wait_seconds); |
| 77 | |
| 78 | // The number of seconds the client will wait for the server process to |
| 79 | // terminate itself after the client receives the final response from a command |
| 80 | // that shuts down the server. After waiting this time, if the server process |
| 81 | // remains, the client will forcibly kill the server. |
| 82 | extern const unsigned int kPostShutdownGracePeriodSeconds; |
| 83 | |
| 84 | // The number of seconds the client will wait for the server process to |
| 85 | // terminate after the client forcibly kills the server. After waiting this |
| 86 | // time, if the server process remains, the client will die. |
| 87 | extern const unsigned int kPostKillGracePeriodSeconds; |
| 88 | |
Laszlo Csomor | 74ffaf7 | 2016-11-24 12:17:20 +0000 | [diff] [blame] | 89 | // Returns the string representation of `value`. |
Googler | 9588b81 | 2015-07-23 11:49:37 +0000 | [diff] [blame] | 90 | // Workaround for mingw where std::to_string is not implemented. |
| 91 | // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015. |
| 92 | template <typename T> |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 93 | std::string ToString(const T &value) { |
Loo Rong Jie | 78fa94a | 2018-02-27 01:28:53 -0800 | [diff] [blame] | 94 | #if defined(__CYGWIN__) || defined(__MINGW32__) |
Googler | 9588b81 | 2015-07-23 11:49:37 +0000 | [diff] [blame] | 95 | std::ostringstream oss; |
| 96 | oss << value; |
| 97 | return oss.str(); |
Loo Rong Jie | 78fa94a | 2018-02-27 01:28:53 -0800 | [diff] [blame] | 98 | #else |
| 99 | return std::to_string(value); |
| 100 | #endif |
Googler | 9588b81 | 2015-07-23 11:49:37 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Dmitry Lomov | 891f540 | 2017-07-19 12:26:39 +0200 | [diff] [blame] | 103 | // Control the output of debug information by debug_log. |
| 104 | // Revisit once client logging is fixed (b/32939567). |
| 105 | void SetDebugLog(bool enabled); |
| 106 | |
lberki | d3d37f0 | 2018-04-18 11:44:14 -0700 | [diff] [blame] | 107 | // What WithEnvVar should do with an environment variable |
| 108 | enum EnvVarAction { UNSET, SET }; |
| 109 | |
| 110 | // What WithEnvVar should do with an environment variable |
| 111 | struct EnvVarValue { |
| 112 | // What should be done with the given variable |
| 113 | EnvVarAction action; |
| 114 | |
| 115 | // The value of the variable; ignored if action == UNSET |
| 116 | std::string value; |
| 117 | |
| 118 | EnvVarValue() {} |
| 119 | |
| 120 | EnvVarValue(EnvVarAction action, const std::string& value) |
| 121 | : action(action), |
| 122 | value(value) {} |
| 123 | }; |
| 124 | |
| 125 | // While this class is in scope, the specified environment variables will be set |
| 126 | // to a specified value (or unset). When it leaves scope, changed variables will |
| 127 | // be set to their original values. |
| 128 | class WithEnvVars { |
| 129 | private: |
| 130 | std::map<std::string, EnvVarValue> _old_values; |
| 131 | |
| 132 | void SetEnvVars(const std::map<std::string, EnvVarValue>& vars); |
| 133 | |
| 134 | public: |
| 135 | WithEnvVars(const std::map<std::string, EnvVarValue>& vars); |
| 136 | ~WithEnvVars(); |
| 137 | }; |
| 138 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 139 | } // namespace blaze |
Thiago Farina | fdc7f98 | 2015-04-27 23:01:34 +0000 | [diff] [blame] | 140 | |
Han-Wen Nienhuys | 0d56b6c | 2015-05-12 14:52:30 +0000 | [diff] [blame] | 141 | #endif // BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_H_ |