blob: b9f5a518e6b4c9e38cad567194aa828db76046a7 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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 Nienhuys0d56b6c2015-05-12 14:52:30 +000019#ifndef BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_H_
20#define BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_H_
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021
Kristina Chodorow6d7b7ff2015-05-13 16:59:49 +000022#include <sys/types.h>
23
lberkid3d37f02018-04-18 11:44:14 -070024#include <map>
Googler9588b812015-07-23 11:49:37 +000025#include <sstream>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010026#include <string>
27#include <vector>
28
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029namespace blaze {
30
Thiago Farina048bbfc2016-09-21 08:20:41 +000031extern const char kServerPidFile[];
Laszlo Csomor6450c182016-11-24 10:28:20 +000032
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033// 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 Farinafdc7f982015-04-27 23:01:34 +000036const char* GetUnaryOption(const char *arg,
37 const char *next_arg,
38 const char *key);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039
40// Returns true iff 'arg' equals 'key'.
41// Dies with a syntax error if arg starts with 'key='.
Thiago Farinad6894752016-07-26 08:59:40 +000042// Returns false otherwise.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043bool GetNullaryOption(const char *arg, const char *key);
44
Luis Fernando Pino Duqued56e1c62017-03-24 18:04:00 +000045// Searches for 'key' in 'args' using GetUnaryOption. Arguments found after '--'
46// are omitted from the search.
mschallerfd37b512017-07-11 18:21:36 +020047// Returns the value of the 'key' flag iff it occurs in args.
48// Returns NULL otherwise.
Luis Fernando Pino Duqued5e008c2016-12-08 16:59:16 +000049const char* SearchUnaryOption(const std::vector<std::string>& args,
50 const char* key);
51
ajmichael1b3569a2018-01-24 07:43:32 -080052// 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 Duqued5e008c2016-12-08 16:59:16 +000057bool SearchNullaryOption(const std::vector<std::string>& args,
ajmichael1b3569a2018-01-24 07:43:32 -080058 const std::string& flag_name,
59 const bool default_value);
Luis Fernando Pino Duqued5e008c2016-12-08 16:59:16 +000060
Luis Fernando Pino Duque0311fc52016-11-21 13:20:50 +000061// Returns true iff arg is a valid command line argument for bazel.
62bool IsArg(const std::string& arg);
63
ccalvarin755278d2018-06-07 11:05:54 -070064// 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.
68std::string AbsolutePathFromFlag(const std::string& value);
69
mschallerfd37b512017-07-11 18:21:36 +020070// 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.
75bool 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.
82extern 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.
87extern const unsigned int kPostKillGracePeriodSeconds;
88
Laszlo Csomor74ffaf72016-11-24 12:17:20 +000089// Returns the string representation of `value`.
Googler9588b812015-07-23 11:49:37 +000090// Workaround for mingw where std::to_string is not implemented.
91// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015.
92template <typename T>
Thiago Farina80bb0f22016-10-17 15:57:13 +000093std::string ToString(const T &value) {
Loo Rong Jie78fa94a2018-02-27 01:28:53 -080094#if defined(__CYGWIN__) || defined(__MINGW32__)
Googler9588b812015-07-23 11:49:37 +000095 std::ostringstream oss;
96 oss << value;
97 return oss.str();
Loo Rong Jie78fa94a2018-02-27 01:28:53 -080098#else
99 return std::to_string(value);
100#endif
Googler9588b812015-07-23 11:49:37 +0000101}
102
Dmitry Lomov891f5402017-07-19 12:26:39 +0200103// Control the output of debug information by debug_log.
104// Revisit once client logging is fixed (b/32939567).
105void SetDebugLog(bool enabled);
106
lberkid3d37f02018-04-18 11:44:14 -0700107// What WithEnvVar should do with an environment variable
108enum EnvVarAction { UNSET, SET };
109
110// What WithEnvVar should do with an environment variable
111struct 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.
128class 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100139} // namespace blaze
Thiago Farinafdc7f982015-04-27 23:01:34 +0000140
Han-Wen Nienhuys0d56b6c2015-05-12 14:52:30 +0000141#endif // BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_H_