blob: b135a967ec0695f9f8f480e9fd945ea72f3e4351 [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001// 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
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
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024#include <string>
25#include <vector>
26
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027namespace blaze {
28
29using std::string;
30
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031string GetUserName();
32
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033// Returns the given path in absolute form. Does not change paths that are
34// already absolute.
35//
36// If called from working directory "/bar":
37// MakeAbsolute("foo") --> "/bar/foo"
38// MakeAbsolute("/foo") ---> "/foo"
39string MakeAbsolute(string path);
40
41// mkdir -p path. All newly created directories use the given mode.
42// Returns -1 on failure, sets errno.
Kristina Chodorow6d7b7ff2015-05-13 16:59:49 +000043int MakeDirectories(const string& path, mode_t mode);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010044
45// Replaces 'content' with contents of file 'filename'.
46// Returns false on error.
47bool ReadFile(const string &filename, string *content);
48
Damien Martin-Guillereza73ab642015-02-10 14:53:25 +000049// Replaces 'content' with contents of file descriptor 'fd'.
50// Returns false on error.
51bool ReadFileDescriptor(int fd, string *content);
52
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010053// Writes 'content' into file 'filename', and makes it executable.
54// Returns false on failure, sets errno.
55bool WriteFile(const string &content, const string &filename);
56
57// Returns true iff the current terminal can support color and cursor movement.
58bool IsStandardTerminal();
59
60// Returns the number of columns of the terminal to which stdout is
61// connected, or 80 if there is no such terminal.
62int GetTerminalColumns();
63
64// blaze's JVM arch is set at build time (--java_cpu), since the blaze java
65// process includes native code.
66bool Is64BitBlazeJavabase();
67
68// Adds JVM arguments particular to running blaze with JVM v3 or higher.
69void AddJVMSpecificArguments(const string &host_javabase,
70 std::vector<string> *result);
71
72void ExecuteProgram(string exe, const std::vector<string>& args_vector);
73
74void ReExecute(const string &executable, int argc, const char *argv[]);
75
76// If 'arg' matches 'key=value', returns address of 'value'.
77// If it matches 'key' alone, returns address of next_arg.
78// Returns NULL otherwise.
Thiago Farinafdc7f982015-04-27 23:01:34 +000079const char* GetUnaryOption(const char *arg,
80 const char *next_arg,
81 const char *key);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010082
83// Returns true iff 'arg' equals 'key'.
84// Dies with a syntax error if arg starts with 'key='.
85// Returns NULL otherwise.
86bool GetNullaryOption(const char *arg, const char *key);
87
Han-Wen Nienhuys36fbe632015-04-21 13:58:08 +000088bool CheckValidPort(const string &str, const string &option, string *error);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010089
Thiago Farina5cb7eee2015-04-02 12:59:26 +000090// Enable messages mostly of interest to developers.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010091bool VerboseLogging();
92
93// Read the JVM version from a file descriptor. The fd should point
94// to the output of a "java -version" execution and is supposed to contains
95// a string of the form 'version "version-number"' in the first 255 bytes.
96// If the string is found, version-number is returned, else the empty string
97// is returned.
98string ReadJvmVersion(int fd);
99
100// Get the version string from the given java executable. The java executable
101// is supposed to output a string in the form '.*version ".*".*'. This method
102// will return the part in between the two quote or the empty string on failure
103// to match the good string.
104string GetJvmVersion(string java_exe);
105
106// Returns true iff jvm_version is at least the version specified by
107// version_spec.
108// jvm_version is supposed to be a string specifying a java runtime version
109// as specified by the JSR-56 appendix A. version_spec is supposed to be a
110// version is the format [0-9]+(.[1-9]+)*.
111bool CheckJavaVersionIsAtLeast(string jvm_version, string version_spec);
112
113} // namespace blaze
Thiago Farinafdc7f982015-04-27 23:01:34 +0000114
Han-Wen Nienhuys0d56b6c2015-05-12 14:52:30 +0000115#endif // BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_H_