On 2023-08-12 20:26, Po Lu wrote:

In file included from boot-time.c:54:0:
boot-time-aux.h: In function 'get_linux_uptime':
boot-time-aux.h:70:3: error: implicit declaration of function 'sysinfo' 
[-Werror=implicit-function-declaration]
    if (sysinfo (&info) >= 0)
    ^
boot-time.c: In function 'get_boot_time_uncached':
boot-time.c:111:26: error: 'BOOT_TIME' undeclared (first use in this function)
        if (ut->ut_type == BOOT_TIME)
                           ^
boot-time.c:111:26: note: each undeclared identifier is reported only once for 
each function it appears in
boot-time.c:126:3: error: implicit declaration of function 'endutent' 
[-Werror=implicit-function-declaration]
    END_UTMP_ENT ();
    ^
cc1: some warnings being treated as errors

Thanks for reporting that. As I understand it, the utmp/utmpx functions are a losing cause on Android since they never return anything. If so, it's simpler to bypass these functions on that platform. Also, Gnulib should bypass sysinfo unless it's available. Please try the attached patch, which I haven't installed onto Emacs master on Savannah. I've tested it only on Ubuntu 23.04.

If this patch works we can propagate it to Gnulib.
From d2db6d8e92282de8ffb5293dd1445e3a2e549ed0 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sat, 12 Aug 2023 23:20:12 -0700
Subject: [PATCH] Temp patch for Android

---
 configure.ac        | 19 -------------------
 lib/boot-time-aux.h | 10 ++++++++++
 lib/boot-time.c     | 26 +++++++++-----------------
 src/conf_post.h     | 34 ----------------------------------
 src/filelock.c      | 14 +++++---------
 5 files changed, 24 insertions(+), 79 deletions(-)

diff --git a/configure.ac b/configure.ac
index 46836073aa0..0234a82b92f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2710,25 +2710,6 @@ AC_DEFUN
 
     # Check for some functions not always present in the NDK.
     AC_CHECK_DECLS([android_get_device_api_level])
-    AC_CHECK_DECLS([endutent, sysinfo], [], [],
-      [[
-#include <sys/sysinfo.h>
-#include <utmp.h>
-]])
-
-    # Establish if BOOT_TIME is defined in utmp.h.
-    AC_CACHE_CHECK([if utmp.h defines BOOT_TIME],
-      [emacs_cv_utmp_h_defines_boot_time],
-      [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
-#include <utmp.h>
-#ifndef BOOT_TIME
-BOOT_TIME not defined
-#endif /* BOOT_TIME */
-]], [[]])], [emacs_cv_utmp_h_defines_boot_time=yes],
-      [emacs_cv_utmp_h_defines_boot_time=no])])
-    AS_IF([test x"$emacs_cv_utmp_h_defines_boot_time" = xyes],
-      [AC_DEFINE([UTMP_H_DEFINES_BOOT_TIME], [1],
-        [Define to 1 if building for Android and utmp.h declares BOOT_TIME])])
 
     # Say this build is really for Android.
     REALLY_ANDROID=yes])])
diff --git a/lib/boot-time-aux.h b/lib/boot-time-aux.h
index 348611fc85c..e782ca6eac6 100644
--- a/lib/boot-time-aux.h
+++ b/lib/boot-time-aux.h
@@ -16,6 +16,14 @@
 
 /* Written by Bruno Haible <[email protected]>.  */
 
+#if defined __linux__ || 9 <= __ANDROID_API__
+# include <sys/sysinfo.h>
+#endif
+#if 9 <= __ANDROID_API__
+/* Absent from some NDK versions, but present in API level 9+.  */
+extern int sysinfo (struct sysinfo *);
+#endif
+
 #define SIZEOF(a) (sizeof(a)/sizeof(a[0]))
 
 #if defined __linux__ || defined __ANDROID__
@@ -65,6 +73,7 @@ get_linux_uptime (struct timespec *p_uptime)
     }
 # endif
 
+# if defined __linux__ || 9 <= __ANDROID_API__
   /* The sysinfo call returns the uptime with a resolution of 1 sec only.  */
   struct sysinfo info;
   if (sysinfo (&info) >= 0)
@@ -73,6 +82,7 @@ get_linux_uptime (struct timespec *p_uptime)
       p_uptime->tv_nsec = 0;
       return 0;
     }
+# endif
 
   return -1;
 }
diff --git a/lib/boot-time.c b/lib/boot-time.c
index d813bfa5825..331711238bc 100644
--- a/lib/boot-time.c
+++ b/lib/boot-time.c
@@ -27,11 +27,6 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#if defined __linux__ || defined __ANDROID__
-# include <sys/sysinfo.h>
-# include <time.h>
-#endif
-
 #if HAVE_SYS_SYSCTL_H && !defined __minix
 # if HAVE_SYS_PARAM_H
 #  include <sys/param.h>
@@ -76,7 +71,12 @@ get_boot_time_uncached (struct timespec *p_boot_time)
 {
   struct timespec found_boot_time = {0};
 
-# if (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_TYPE : HAVE_STRUCT_UTMP_UT_TYPE)
+# ifdef __ANDROID__
+
+  /* Workaround for Android:  */
+  get_android_boot_time (&found_boot_time);
+
+# elif (HAVE_UTMPX_H ? HAVE_STRUCT_UTMPX_UT_TYPE : HAVE_STRUCT_UTMP_UT_TYPE)
 
   /* Try to find the boot time in the /var/run/utmp file.  */
 
@@ -90,7 +90,7 @@ get_boot_time_uncached (struct timespec *p_boot_time)
 
   SET_UTMP_ENT ();
 
-#   if (defined __linux__ && !defined __ANDROID__) || defined __minix
+#   if defined __linux__ || defined __minix
   /* Timestamp of the "runlevel" entry, if any.  */
   struct timespec runlevel_ts = {0};
 #   endif
@@ -111,7 +111,7 @@ get_boot_time_uncached (struct timespec *p_boot_time)
       if (ut->ut_type == BOOT_TIME)
         found_boot_time = ts;
 
-#   if defined __linux__ && !defined __ANDROID__
+#   ifdef __linux__
       if (memcmp (UT_USER (ut), "runlevel", strlen ("runlevel") + 1) == 0
           && memcmp (ut->ut_line, "~", strlen ("~") + 1) == 0)
         runlevel_ts = ts;
@@ -125,7 +125,7 @@ get_boot_time_uncached (struct timespec *p_boot_time)
 
   END_UTMP_ENT ();
 
-#   if defined __linux__ && !defined __ANDROID__
+#   ifdef __linux__
   /* On Raspbian, which runs on hardware without a real-time clock, during boot,
        1. the clock gets set to 1970-01-01 00:00:00,
        2. an entry gets written into /var/run/utmp, with ut_type = BOOT_TIME,
@@ -145,14 +145,6 @@ get_boot_time_uncached (struct timespec *p_boot_time)
     }
 #   endif
 
-#   if defined __ANDROID__
-  if (found_boot_time.tv_sec == 0)
-    {
-      /* Workaround for Android:  */
-      get_android_boot_time (&found_boot_time);
-    }
-#   endif
-
 #   if defined __minix
   /* On Minix, during boot,
        1. an entry gets written into /var/run/utmp, with ut_type = BOOT_TIME,
diff --git a/src/conf_post.h b/src/conf_post.h
index 5f18e5ae4bb..f31e012dc6e 100644
--- a/src/conf_post.h
+++ b/src/conf_post.h
@@ -471,37 +471,3 @@ #define VFORK() vfork ()
 #undef MB_CUR_MAX
 #define MB_CUR_MAX REPLACEMENT_MB_CUR_MAX
 #endif /* REPLACEMENT_MB_CUR_MAX */
-
-#ifdef __ANDROID__
-
-/* The Android NDK r10b omits the function `endutent' that is actually
-   defined in the C library and used by Gnulib.  Define a prototype
-   for it here.  */
-
-#if !HAVE_DECL_ENDUTENT
-extern void endutent (void);
-#endif /* !HAVE_DECL_ENDUTENT */
-
-/* Now define substitutes for BOOT_TIME if necessary.  */
-
-#ifndef UTMP_H_DEFINES_BOOT_TIME
-#include <utmp.h>
-
-#define BOOT_TIME 2
-#endif /* !UTMP_H_DEFINES_BOOT_TIME */
-
-/* sysinfo is also absent from some versions of the NDK, yet is
-   present on API level 9 and above.  */
-
-#if !HAVE_DECL_SYSINFO
-#include <sys/sysinfo.h>
-
-#if __ANDROID_API__ >= 9
-extern int sysinfo (struct sysinfo *info);
-#else /* __ANDROID_API__ < 8 */
-/* Gnulib uses this function unconditionally.  */
-#define sysinfo(ignored) ((void) ignored, (errno = ENOSYS), -1)
-#endif /* __ANDROID_API >= 9 */
-#endif /* !HAVE_DECL_SYSINFO */
-
-#endif /* __ANDROID__ */
diff --git a/src/filelock.c b/src/filelock.c
index f3075b93322..92be20ad8a0 100644
--- a/src/filelock.c
+++ b/src/filelock.c
@@ -118,16 +118,12 @@ get_boot_sec (void)
   if (will_dump_p ())
     return 0;
 
+  struct timespec boot_time;
+  boot_time.tv_sec = 0;
 #ifndef MSDOS
-  {
-    struct timespec boot_time;
-    boot_time.tv_sec = 0;
-    get_boot_time (&boot_time);
-    return boot_time.tv_sec;
-  }
-#else /* MSDOS */
-  return 0;
-#endif /* MSDOS */
+  get_boot_time (&boot_time);
+#endif
+  return boot_time.tv_sec;
 }
 
 /* An arbitrary limit on lock contents length.  8 K should be plenty
-- 
2.39.2

Reply via email to