aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp
diff options
context:
space:
mode:
authorGravatar László Csomor <laszlocsomor@google.com>2017-01-27 11:01:41 +0000
committerGravatar Laszlo Csomor <laszlocsomor@google.com>2017-01-27 13:43:08 +0000
commit6f1e31a9aa4ad80e402b9a441d4129c7f1bd6fb7 (patch)
tree196001f4d4e155189fef6c5e4e2d677ae6f4e8e1 /src/main/cpp
parent7e7d6094b26e906ea0714e3b006f1458dfc3b825 (diff)
Bazel client: platform-dependent `strerror`
Move `strerror` calls into errors_<platform>. We have to get rid of direct `errno` reading too, because it doesn't work on Windows native. Fixes https://github.com/bazelbuild/bazel/issues/2411 -- Change-Id: I69ff502487d698aa9e9147f02fd0bc5253e94e64 Reviewed-on: https://cr.bazel.build/8490 PiperOrigin-RevId: 145777524 MOS_MIGRATED_REVID=145777524
Diffstat (limited to 'src/main/cpp')
-rw-r--r--src/main/cpp/BUILD1
-rw-r--r--src/main/cpp/blaze.cc20
-rw-r--r--src/main/cpp/startup_options.cc8
-rw-r--r--src/main/cpp/util/BUILD7
-rw-r--r--src/main/cpp/util/errors.cc7
-rw-r--r--src/main/cpp/util/errors.h6
-rw-r--r--src/main/cpp/util/errors_posix.cc33
-rw-r--r--src/main/cpp/util/errors_windows.cc44
8 files changed, 109 insertions, 17 deletions
diff --git a/src/main/cpp/BUILD b/src/main/cpp/BUILD
index e6bc52b085..d488f2abf7 100644
--- a/src/main/cpp/BUILD
+++ b/src/main/cpp/BUILD
@@ -99,6 +99,7 @@ cc_binary(
deps = [
":blaze_util",
"//src/main/cpp/util",
+ "//src/main/cpp/util:errors",
"//src/main/cpp/util:logging",
"//src/main/cpp/util:strings",
"//src/main/protobuf:command_server_cc_proto",
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 1aaa117721..e26b41dab0 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -304,8 +304,9 @@ static string GetInstallBase(const string &root, const string &self_path) {
devtools_ijar::ZipExtractor::Create(self_path.c_str(), &processor));
if (extractor.get() == NULL) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "\nFailed to open %s as a zip file: (%d) %s",
- globals->options->product_name.c_str(), errno, strerror(errno));
+ "\nFailed to open %s as a zip file: %s",
+ globals->options->product_name.c_str(),
+ blaze_util::GetLastErrorString().c_str());
}
if (extractor->ProcessAll() < 0) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
@@ -777,9 +778,11 @@ static void StartServerAndConnect(const WorkspaceLayout* workspace_layout,
std::this_thread::sleep_until(next_attempt_time);
if (!server_startup->IsStillAlive()) {
- fprintf(stderr, "\nunexpected pipe read status: %s\n"
- "Server presumed dead. Now printing '%s':\n",
- strerror(errno), globals->jvm_log_file.c_str());
+ fprintf(stderr,
+ "\nunexpected pipe read status: %s\n"
+ "Server presumed dead. Now printing '%s':\n",
+ blaze_util::GetLastErrorString().c_str(),
+ globals->jvm_log_file.c_str());
WriteFileToStderrOrDie(globals->jvm_log_file.c_str());
exit(blaze_exit_code::INTERNAL_ERROR);
}
@@ -810,7 +813,7 @@ class ExtractBlazeZipProcessor : public devtools_ijar::ZipExtractorProcessor {
if (!blaze_util::WriteFile(data, size, path)) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
"\nFailed to write zipped file \"%s\": %s", path.c_str(),
- strerror(errno));
+ blaze_util::GetLastErrorString().c_str());
}
}
@@ -834,8 +837,9 @@ static void ActuallyExtractData(const string &argv0,
devtools_ijar::ZipExtractor::Create(argv0.c_str(), &processor));
if (extractor.get() == NULL) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "\nFailed to open %s as a zip file: (%d) %s",
- globals->options->product_name.c_str(), errno, strerror(errno));
+ "\nFailed to open %s as a zip file: %s",
+ globals->options->product_name.c_str(),
+ blaze_util::GetLastErrorString().c_str());
}
if (extractor->ProcessAll() < 0) {
die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
diff --git a/src/main/cpp/startup_options.cc b/src/main/cpp/startup_options.cc
index f824360b69..876b91de99 100644
--- a/src/main/cpp/startup_options.cc
+++ b/src/main/cpp/startup_options.cc
@@ -15,13 +15,13 @@
#include <assert.h>
#include <errno.h> // errno, ENOENT
-#include <string.h> // strerror
#include <cstdio>
#include <cstdlib>
-#include "src/main/cpp/blaze_util_platform.h"
#include "src/main/cpp/blaze_util.h"
+#include "src/main/cpp/blaze_util_platform.h"
+#include "src/main/cpp/util/errors.h"
#include "src/main/cpp/util/exit_code.h"
#include "src/main/cpp/util/file.h"
#include "src/main/cpp/util/file_platform.h"
@@ -360,8 +360,8 @@ string StartupOptions::GetJvm() {
if (!blaze_util::PathExists(java_program)) {
fprintf(stderr, "Couldn't find java at '%s'.\n", java_program.c_str());
} else {
- fprintf(stderr, "Couldn't access %s: %s\n", java_program.c_str(),
- strerror(errno));
+ fprintf(stderr, "Java at '%s' exists but is not executable: %s\n",
+ java_program.c_str(), blaze_util::GetLastErrorString().c_str());
}
exit(1);
}
diff --git a/src/main/cpp/util/BUILD b/src/main/cpp/util/BUILD
index a14034473a..b044d19537 100644
--- a/src/main/cpp/util/BUILD
+++ b/src/main/cpp/util/BUILD
@@ -61,8 +61,13 @@ cc_library(
cc_library(
name = "errors",
- srcs = ["errors.cc"],
+ srcs = ["errors.cc"] + select({
+ "//src:windows": ["errors_windows.cc"],
+ "//src:windows_msvc": ["errors_windows.cc"],
+ "//conditions:default": ["errors_posix.cc"],
+ }),
hdrs = ["errors.h"],
+ visibility = ["//src/main/cpp:__subpackages__"],
deps = [":port"],
)
diff --git a/src/main/cpp/util/errors.cc b/src/main/cpp/util/errors.cc
index 8497545e0b..112f293bf1 100644
--- a/src/main/cpp/util/errors.cc
+++ b/src/main/cpp/util/errors.cc
@@ -14,10 +14,9 @@
#include "src/main/cpp/util/errors.h"
-#include <errno.h>
#include <stdarg.h>
-#include <stdlib.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
namespace blaze_util {
@@ -37,7 +36,7 @@ void pdie(const int exit_status, const char *format, ...) {
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
- fprintf(stderr, ": %s\n", strerror(errno));
+ fprintf(stderr, ": %s\n", GetLastErrorString().c_str());
exit(exit_status);
}
@@ -47,7 +46,7 @@ void PrintError(const char *format, ...) {
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
- fprintf(stderr, ": %s\n", strerror(errno));
+ fprintf(stderr, ": %s\n", GetLastErrorString().c_str());
}
} // namespace blaze_util
diff --git a/src/main/cpp/util/errors.h b/src/main/cpp/util/errors.h
index 833a6ee473..e7d93068f4 100644
--- a/src/main/cpp/util/errors.h
+++ b/src/main/cpp/util/errors.h
@@ -18,6 +18,7 @@
#ifndef BAZEL_SRC_MAIN_CPP_UTIL_ERRORS_H_
#define BAZEL_SRC_MAIN_CPP_UTIL_ERRORS_H_
+#include <string>
#include "src/main/cpp/util/port.h"
namespace blaze_util {
@@ -30,6 +31,11 @@ void pdie(const int exit_status, const char *format, ...) ATTRIBUTE_NORETURN
PRINTF_ATTRIBUTE(2, 3);
void PrintError(const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
+// Returns the last error as a platform-specific error message.
+// The string will also contain the platform-specific error code itself
+// (which is `errno` on Linux/Darwin, and `GetLastError()` on Windows).
+std::string GetLastErrorString();
+
} // namespace blaze_util
#endif // BAZEL_SRC_MAIN_CPP_UTIL_ERRORS_H_
diff --git a/src/main/cpp/util/errors_posix.cc b/src/main/cpp/util/errors_posix.cc
new file mode 100644
index 0000000000..a5e449e424
--- /dev/null
+++ b/src/main/cpp/util/errors_posix.cc
@@ -0,0 +1,33 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <errno.h>
+#include <string.h> // strerror
+#include <sstream>
+#include <string>
+#include "src/main/cpp/util/errors.h"
+
+namespace blaze_util {
+
+using std::string;
+using std::stringstream;
+
+string GetLastErrorString() {
+ int saved_errno = errno;
+ stringstream result;
+ result << "(error: " << saved_errno << "): " << strerror(saved_errno);
+ return result.str();
+}
+
+} // namespace blaze_util
diff --git a/src/main/cpp/util/errors_windows.cc b/src/main/cpp/util/errors_windows.cc
new file mode 100644
index 0000000000..b77ae21b26
--- /dev/null
+++ b/src/main/cpp/util/errors_windows.cc
@@ -0,0 +1,44 @@
+// Copyright 2017 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <windows.h>
+#include <sstream>
+#include <string>
+#include "src/main/cpp/util/errors.h"
+
+namespace blaze_util {
+
+using std::string;
+using std::stringstream;
+
+string GetLastErrorString() {
+ DWORD last_error = ::GetLastError();
+ if (last_error == 0) {
+ return string();
+ }
+
+ char* message_buffer;
+ size_t size = FormatMessageA(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPSTR)&message_buffer, 0, NULL);
+
+ stringstream result;
+ result << "(error: " << last_error << "): " << message_buffer;
+ LocalFree(message_buffer);
+ return result.str();
+}
+
+} // namespace blaze_util