abseil has failed to build on GNU/Hurd for a long time. Now
let's make it work! :)

Note that `__GNU__` is the macro for detecting GNU/Hurd. And
`__MACH__` is also defined there besides on Apple platform.
They are both "mach" but with different implementation and
platform details. Here are the works,

* Mark platform features (not) supported by GNU/Hurd.

  * Supports `mmap` and `write`.

  * Not supports `vdso`. It's specific to Linux.

  * Not supports `ELF_SYMBOLIZE` for now. GNU/Hurd uses ELF as
    the binary format. But symbolizing in abseil relies on
    reading object file path from `/proc/self/task/<pid>/maps`
    (Linux specific) or `/proc/self/maps`. GNU/Hurd does have
    the latter. However, due to its micro-kernel design, it's
    currently unable to get the file path info there.

* GNU/Hurd uses different errno and messages than Linux. So
  related things are adjusted accordingly.

* Fix a misuse of `__MACH__`, which should actually be
  `__APPLE__`.

* Fix a missing including of `signal.h` for using `SIGABRT`.
  Otherwise compilation will fail with undefined symbol error
  on GNU/Hurd.

* Fix stack consumption measurement to make it also work on
  GNU/Hurd.
  The problem behind it is that GNU/Hurd uses a totally different
  way to implement signal handling. With `-O2` enabled, the
  `SimpleSignalHandler` will consume less than 100 bytes.
  Although current code already tries to avoid the affection of
  compiler optimization, and it works on other platforms, it's
  still not strict enough for GNU/Hurd. So I change the way to
  avoid optimization by instructing compiler with
  `__attribute__((optimize("O0")))`.

Signed-off-by: Yuqian Yang <crup...@crupest.life>
---
 absl/base/config.h                                | 2 +-
 absl/base/internal/raw_logging.cc                 | 2 +-
 absl/base/internal/strerror_test.cc               | 4 +++-
 absl/debugging/internal/stack_consumption.cc      | 2 +-
 absl/debugging/internal/stack_consumption_test.cc | 8 +-------
 absl/debugging/internal/symbolize.h               | 2 +-
 absl/debugging/internal/vdso_support.h            | 2 +-
 absl/log/internal/test_helpers.cc                 | 4 ++++
 absl/log/log_modifier_methods_test.cc             | 6 ++++--
 absl/log/stripping_test.cc                        | 4 ++--
 10 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/absl/base/config.h b/absl/base/config.h
index 0b22167e..8bb27b6e 100644
--- a/absl/base/config.h
+++ b/absl/base/config.h
@@ -380,7 +380,7 @@ static_assert(ABSL_INTERNAL_INLINE_NAMESPACE_STR[0] != 'h' 
||
     defined(__asmjs__) || defined(__EMSCRIPTEN__) || defined(__Fuchsia__) || \
     defined(__sun) || defined(__myriad2__) || defined(__HAIKU__) ||          \
     defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) ||       \
-    defined(__VXWORKS__) || defined(__hexagon__)
+    defined(__VXWORKS__) || defined(__hexagon__) || defined(__GNU__)
 #define ABSL_HAVE_MMAP 1
 #endif
 
diff --git a/absl/base/internal/raw_logging.cc 
b/absl/base/internal/raw_logging.cc
index d32b40a8..81630372 100644
--- a/absl/base/internal/raw_logging.cc
+++ b/absl/base/internal/raw_logging.cc
@@ -44,7 +44,7 @@
 #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
     defined(__hexagon__) || defined(__Fuchsia__) ||                     \
     defined(__native_client__) || defined(__OpenBSD__) ||               \
-    defined(__EMSCRIPTEN__) || defined(__ASYLO__)
+    defined(__EMSCRIPTEN__) || defined(__ASYLO__) || defined(__GNU__)
 
 #include <unistd.h>
 
diff --git a/absl/base/internal/strerror_test.cc 
b/absl/base/internal/strerror_test.cc
index e32d5b5c..2eccde77 100644
--- a/absl/base/internal/strerror_test.cc
+++ b/absl/base/internal/strerror_test.cc
@@ -39,7 +39,9 @@ TEST(StrErrorTest, ValidErrorCode) {
 TEST(StrErrorTest, InvalidErrorCode) {
   errno = ERANGE;
   EXPECT_THAT(absl::base_internal::StrError(-1),
-              AnyOf(Eq("No error information"), Eq("Unknown error -1")));
+              AnyOf(Eq("No error information"),
+                    Eq("Unknown error -1"),
+                    Eq("Error in unknown error system: FFFFFFFF")));
   EXPECT_THAT(errno, Eq(ERANGE));
 }
 
diff --git a/absl/debugging/internal/stack_consumption.cc 
b/absl/debugging/internal/stack_consumption.cc
index b54a1b28..2713c9da 100644
--- a/absl/debugging/internal/stack_consumption.cc
+++ b/absl/debugging/internal/stack_consumption.cc
@@ -66,7 +66,7 @@ constexpr bool kStackGrowsDown = true;
 // handler. The difference between the stack consumption of these two signals
 // handlers should give us the stack foorprint of interest.
 
-void EmptySignalHandler(int) {}
+__attribute__((optimize("O0"))) void EmptySignalHandler(int signo) {}
 
 // This is arbitrary value, and could be increase further, at the cost of
 // memset()ting it all to known sentinel value.
diff --git a/absl/debugging/internal/stack_consumption_test.cc 
b/absl/debugging/internal/stack_consumption_test.cc
index 0255ac8f..6328edcc 100644
--- a/absl/debugging/internal/stack_consumption_test.cc
+++ b/absl/debugging/internal/stack_consumption_test.cc
@@ -20,21 +20,15 @@
 #include <string.h>
 
 #include "gtest/gtest.h"
-#include "absl/log/log.h"
 
 namespace absl {
 ABSL_NAMESPACE_BEGIN
 namespace debugging_internal {
 namespace {
 
-static void SimpleSignalHandler(int signo) {
+static __attribute__((optimize("O0"))) void SimpleSignalHandler(int signo) {
   char buf[100];
   memset(buf, 'a', sizeof(buf));
-
-  // Never true, but prevents compiler from optimizing buf out.
-  if (signo == 0) {
-    LOG(INFO) << static_cast<void*>(buf);
-  }
 }
 
 TEST(SignalHandlerStackConsumptionTest, MeasuresStackConsumption) {
diff --git a/absl/debugging/internal/symbolize.h 
b/absl/debugging/internal/symbolize.h
index 5593fde6..8e2d7c8e 100644
--- a/absl/debugging/internal/symbolize.h
+++ b/absl/debugging/internal/symbolize.h
@@ -29,7 +29,7 @@
 #ifdef ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE
 #error ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE cannot be directly set
 #elif defined(__ELF__) && defined(__GLIBC__) && !defined(__native_client__) \
-      && !defined(__asmjs__) && !defined(__wasm__)
+      && !defined(__asmjs__) && !defined(__wasm__) && !defined(__GNU__)
 #define ABSL_INTERNAL_HAVE_ELF_SYMBOLIZE 1
 
 #include <elf.h>
diff --git a/absl/debugging/internal/vdso_support.h 
b/absl/debugging/internal/vdso_support.h
index 6562c6c2..cfca78d3 100644
--- a/absl/debugging/internal/vdso_support.h
+++ b/absl/debugging/internal/vdso_support.h
@@ -48,7 +48,7 @@
 
 #ifdef ABSL_HAVE_VDSO_SUPPORT
 #error ABSL_HAVE_VDSO_SUPPORT cannot be directly set
-#else
+#elif !defined(__GNU__)
 #define ABSL_HAVE_VDSO_SUPPORT 1
 #endif
 
diff --git a/absl/log/internal/test_helpers.cc 
b/absl/log/internal/test_helpers.cc
index bfcc9679..72fff4d5 100644
--- a/absl/log/internal/test_helpers.cc
+++ b/absl/log/internal/test_helpers.cc
@@ -18,6 +18,10 @@
 #include <zircon/syscalls.h>
 #endif
 
+#ifdef __GNU__
+#include <signal.h>
+#endif
+
 #include "gtest/gtest.h"
 #include "absl/base/config.h"
 #include "absl/base/log_severity.h"
diff --git a/absl/log/log_modifier_methods_test.cc 
b/absl/log/log_modifier_methods_test.cc
index 4ccde404..9b6a7ec8 100644
--- a/absl/log/log_modifier_methods_test.cc
+++ b/absl/log/log_modifier_methods_test.cc
@@ -180,7 +180,8 @@ TEST(TailCallsModifiesTest, WithPerror) {
       test_sink,
       Send(AllOf(TextMessage(AnyOf(Eq("hello world: Bad file number [9]"),
                                    Eq("hello world: Bad file descriptor [9]"),
-                                   Eq("hello world: Bad file descriptor 
[8]"))),
+                                   Eq("hello world: Bad file descriptor [8]"),
+                                   Eq("hello world: Bad file descriptor 
[1073741833]"))),
                  ENCODED_MESSAGE(HasValues(ElementsAre(
                      EqualsProto(R"pb(literal: "hello world")pb"),
                      EqualsProto(R"pb(literal: ": ")pb"),
@@ -188,7 +189,8 @@ TEST(TailCallsModifiesTest, WithPerror) {
                            EqualsProto(R"pb(str: "Bad file descriptor")pb")),
                      EqualsProto(R"pb(literal: " [")pb"),
                      AnyOf(EqualsProto(R"pb(str: "8")pb"),
-                           EqualsProto(R"pb(str: "9")pb")),
+                           EqualsProto(R"pb(str: "9")pb"),
+                           EqualsProto(R"pb(str: "1073741833")pb")),
                      EqualsProto(R"pb(literal: "]")pb")))))));
 
   test_sink.StartCapturingLogs();
diff --git a/absl/log/stripping_test.cc b/absl/log/stripping_test.cc
index 271fae1d..20231b9f 100644
--- a/absl/log/stripping_test.cc
+++ b/absl/log/stripping_test.cc
@@ -33,7 +33,7 @@
 
 #include <stdio.h>
 
-#if defined(__MACH__)
+#if defined(__APPLE__)
 #include <mach-o/dyld.h>
 #elif defined(_WIN32)
 #include <Windows.h>
@@ -191,7 +191,7 @@ class StrippingTest : public ::testing::Test {
       absl::FPrintF(stderr, "Failed to open /pkg/bin/<binary name>: %s\n", 
err);
     }
     return fp;
-#elif defined(__MACH__)
+#elif defined(__APPLE__)
     uint32_t size = 0;
     int ret = _NSGetExecutablePath(nullptr, &size);
     if (ret != -1) {
-- 
Yuqian Yang <crup...@crupest.life>

Reply via email to