blob: a43deea75de30e99d4eb11d030779faea3e1e517 [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
Googler9588b812015-07-23 11:49:37 +000024#include <sstream>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025#include <string>
26#include <vector>
27
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028namespace blaze {
29
30using std::string;
31
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010032string GetUserName();
33
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034// Returns the given path in absolute form. Does not change paths that are
35// already absolute.
36//
37// If called from working directory "/bar":
38// MakeAbsolute("foo") --> "/bar/foo"
39// MakeAbsolute("/foo") ---> "/foo"
Thiago Farina2fd78902015-05-18 11:37:59 +000040string MakeAbsolute(const string &path);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041
42// mkdir -p path. All newly created directories use the given mode.
43// Returns -1 on failure, sets errno.
Thiago Farina2fd78902015-05-18 11:37:59 +000044int MakeDirectories(const string &path, mode_t mode);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045
46// Replaces 'content' with contents of file 'filename'.
47// Returns false on error.
48bool ReadFile(const string &filename, string *content);
49
Damien Martin-Guillereza73ab642015-02-10 14:53:25 +000050// Replaces 'content' with contents of file descriptor 'fd'.
51// Returns false on error.
52bool ReadFileDescriptor(int fd, string *content);
53
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010054// Writes 'content' into file 'filename', and makes it executable.
55// Returns false on failure, sets errno.
56bool WriteFile(const string &content, const string &filename);
57
58// Returns true iff the current terminal can support color and cursor movement.
59bool IsStandardTerminal();
60
61// Returns the number of columns of the terminal to which stdout is
62// connected, or 80 if there is no such terminal.
63int GetTerminalColumns();
64
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065// Adds JVM arguments particular to running blaze with JVM v3 or higher.
66void AddJVMSpecificArguments(const string &host_javabase,
67 std::vector<string> *result);
68
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010069// If 'arg' matches 'key=value', returns address of 'value'.
70// If it matches 'key' alone, returns address of next_arg.
71// Returns NULL otherwise.
Thiago Farinafdc7f982015-04-27 23:01:34 +000072const char* GetUnaryOption(const char *arg,
73 const char *next_arg,
74 const char *key);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010075
76// Returns true iff 'arg' equals 'key'.
77// Dies with a syntax error if arg starts with 'key='.
78// Returns NULL otherwise.
79bool GetNullaryOption(const char *arg, const char *key);
80
Thiago Farina5cb7eee2015-04-02 12:59:26 +000081// Enable messages mostly of interest to developers.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010082bool VerboseLogging();
83
84// Read the JVM version from a file descriptor. The fd should point
85// to the output of a "java -version" execution and is supposed to contains
86// a string of the form 'version "version-number"' in the first 255 bytes.
87// If the string is found, version-number is returned, else the empty string
88// is returned.
89string ReadJvmVersion(int fd);
90
91// Get the version string from the given java executable. The java executable
92// is supposed to output a string in the form '.*version ".*".*'. This method
93// will return the part in between the two quote or the empty string on failure
94// to match the good string.
Thiago Farina2fd78902015-05-18 11:37:59 +000095string GetJvmVersion(const string &java_exe);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010096
97// Returns true iff jvm_version is at least the version specified by
98// version_spec.
99// jvm_version is supposed to be a string specifying a java runtime version
100// as specified by the JSR-56 appendix A. version_spec is supposed to be a
101// version is the format [0-9]+(.[1-9]+)*.
Thiago Farina2fd78902015-05-18 11:37:59 +0000102bool CheckJavaVersionIsAtLeast(const string &jvm_version,
103 const string &version_spec);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100104
Googler9588b812015-07-23 11:49:37 +0000105// Converts a project identifier to string.
106// Workaround for mingw where std::to_string is not implemented.
107// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52015.
108template <typename T>
109string ToString(const T& value) {
110 std::ostringstream oss;
111 oss << value;
112 return oss.str();
113}
114
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100115} // namespace blaze
Thiago Farinafdc7f982015-04-27 23:01:34 +0000116
Han-Wen Nienhuys0d56b6c2015-05-12 14:52:30 +0000117#endif // BAZEL_SRC_MAIN_CPP_BLAZE_UTIL_H_