blob: cdef849034af973b78639026096e06822b1bec58 [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
Laszlo Csomor26b7a662016-11-25 13:46:30 +000024#include <functional>
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
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 Farina048bbfc2016-09-21 08:20:41 +000036extern const char kServerPidSymlink[];
Lukacs Berkid9da60f2016-04-26 11:40:24 +000037
Thiago Farina80bb0f22016-10-17 15:57:13 +000038std::string GetUserName();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040// 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 Farinaac619562016-04-22 17:44:14 +000046// MakeAbsolute("C:/foo") ---> "C:/foo"
Thiago Farina80bb0f22016-10-17 15:57:13 +000047std::string MakeAbsolute(const std::string &path);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010048
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010049// Replaces 'content' with contents of file 'filename'.
Laszlo Csomorbc3f2642016-11-22 14:02:07 +000050// If `max_size` is positive, the method reads at most that many bytes;
51// otherwise the method reads the whole file.
Lukacs Berki793cd012016-06-20 09:48:47 +000052// Returns false on error. Can be called from a signal handler.
Laszlo Csomorae16e762016-11-18 10:16:08 +000053bool ReadFile(const std::string &filename, std::string *content,
Laszlo Csomorbc3f2642016-11-22 14:02:07 +000054 int max_size = 0);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010055
Laszlo Csomor26b7a662016-11-25 13:46:30 +000056// 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 Berki793cd012016-06-20 09:48:47 +000059// Returns false on error. Can be called from a signal handler.
Laszlo Csomor26b7a662016-11-25 13:46:30 +000060bool ReadFrom(const std::function<int(void *, int)> &read_func,
61 std::string *content, int max_size = 0);
Damien Martin-Guillereza73ab642015-02-10 14:53:25 +000062
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010063// Writes 'content' into file 'filename', and makes it executable.
64// Returns false on failure, sets errno.
Thiago Farina80bb0f22016-10-17 15:57:13 +000065bool WriteFile(const std::string &content, const std::string &filename);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010066
Laszlo Csomorae16e762016-11-18 10:16:08 +000067// Writes `size` bytes from `data` into file 'filename' and makes it executable.
68// Returns false on failure, sets errno.
69bool WriteFile(const void* data, size_t size, const std::string &filename);
70
Laszlo Csomor26b7a662016-11-25 13:46:30 +000071// Writes `size` bytes from `data` into a destination using `write_func`.
72// Returns false on failure, sets errno.
73bool WriteTo(const std::function<int(const void *, size_t)> &write_func,
74 const void *data, size_t size);
75
Thiago Farina4e4ffd22016-03-09 17:02:28 +000076// Unlinks the file given by 'file_path'.
77// Returns true on success. In case of failure sets errno.
Thiago Farina80bb0f22016-10-17 15:57:13 +000078bool UnlinkPath(const std::string &file_path);
Nathan Harmatabf98f392016-01-07 22:58:29 +000079
Googler957f3742016-08-31 18:16:24 +000080// Returns true iff the current terminal is running inside an Emacs.
81bool IsEmacsTerminal();
82
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010083// Returns true iff the current terminal can support color and cursor movement.
84bool 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.
88int GetTerminalColumns();
89
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010090// 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 Farinafdc7f982015-04-27 23:01:34 +000093const char* GetUnaryOption(const char *arg,
94 const char *next_arg,
95 const char *key);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010096
97// Returns true iff 'arg' equals 'key'.
98// Dies with a syntax error if arg starts with 'key='.
Thiago Farinad6894752016-07-26 08:59:40 +000099// Returns false otherwise.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100100bool GetNullaryOption(const char *arg, const char *key);
101
Thiago Farina5cb7eee2015-04-02 12:59:26 +0000102// Enable messages mostly of interest to developers.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100103bool VerboseLogging();
104
Lukacs Berki00d613a2016-04-25 15:39:28 +0000105// 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 Farina80bb0f22016-10-17 15:57:13 +0000109std::string ReadJvmVersion(const std::string &version_string);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100110
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 Farina80bb0f22016-10-17 15:57:13 +0000115std::string GetJvmVersion(const std::string &java_exe);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100116
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 Farina80bb0f22016-10-17 15:57:13 +0000122bool CheckJavaVersionIsAtLeast(const std::string &jvm_version,
123 const std::string &version_spec);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100124
Luis Fernando Pino Duque0311fc52016-11-21 13:20:50 +0000125// Returns true iff arg is a valid command line argument for bazel.
126bool IsArg(const std::string& arg);
127
Laszlo Csomor74ffaf72016-11-24 12:17:20 +0000128// Returns the string representation of `value`.
Googler9588b812015-07-23 11:49:37 +0000129// Workaround for mingw where std::to_string is not implemented.
130// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015.
131template <typename T>
Thiago Farina80bb0f22016-10-17 15:57:13 +0000132std::string ToString(const T &value) {
Googler9588b812015-07-23 11:49:37 +0000133 std::ostringstream oss;
134 oss << value;
135 return oss.str();
136}
137
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100138} // namespace blaze
Thiago Farinafdc7f982015-04-27 23:01:34 +0000139
Han-Wen Nienhuys0d56b6c2015-05-12 14:52:30 +0000140#endif // BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_H_