aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/util
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/util
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/util')
-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
5 files changed, 92 insertions, 5 deletions
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