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