jtmoon79 / super-speedy-syslog-searcher

Speedily search and merge log messages by datetime
MIT License
38 stars 1 forks source link

fails to compile (`cargo install`) on Alpine Linux (utmpx.h declarations) #100

Closed jtmoon79 closed 6 months ago

jtmoon79 commented 1 year ago

Summary

cargo install super_speedy_syslog_searcher fails on Alpine Linux 3.17.

Current behavior

root@alpine:~
$ cargo install super_speedy_syslog_searcher
    Updating crates.io index
  Installing super_speedy_syslog_searcher v0.5.59
   Compiling libc v0.2.141
...
   Compiling super_speedy_syslog_searcher v0.5.59
error[E0432]: unresolved import `uapi::c::__timeval`
   --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/super_speedy_syslog_searcher-0.5.59/src/data/utmpx.rs:113:13
    |
113 |             __timeval,
    |             ^^^^^^^^^
    |             |
    |             no `__timeval` in `c`
    |             help: a similar name exists in the module: `timeval`

error[E0277]: the trait bound `i32: From<i64>` is not satisfied
   --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/super_speedy_syslog_searcher-0.5.59/src/data/utmpx.rs:426:54
    |
426 |         let tv_sec: tv_sec_type = entry.ut_tv.tv_sec.into();
    |                                                      ^^^^ the trait `From<i64>` is not implemented for `i32`
    |
    = help: the following other types implement trait `From<T>`:
              <i32 as From<NonZeroI32>>
              <i32 as From<bool>>
              <i32 as From<i16>>
              <i32 as From<i8>>
              <i32 as From<u16>>
              <i32 as From<u8>>
    = note: required for `i64` to implement `Into<i32>`

error[E0277]: the trait bound `i32: From<i64>` is not satisfied
   --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/super_speedy_syslog_searcher-0.5.59/src/data/utmpx.rs:427:57
    |
427 |         let tv_usec: tv_usec_type = entry.ut_tv.tv_usec.into();
    |                                                         ^^^^ the trait `From<i64>` is not implemented for `i32`
    |
    = help: the following other types implement trait `From<T>`:
              <i32 as From<NonZeroI32>>
              <i32 as From<bool>>
              <i32 as From<i16>>
              <i32 as From<i8>>
              <i32 as From<u16>>
              <i32 as From<u8>>
    = note: required for `i64` to implement `Into<i32>`

Some errors have detailed explanations: E0277, E0432.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `super_speedy_syslog_searcher` due to 3 previous errors
error: failed to compile `super_speedy_syslog_searcher v0.5.59`, intermediate artifacts can be found at `/tmp/cargo-installWPo68r`

Reviewing the resident utmpx.h,

$ cat /usr/include/utmpx.h
#ifndef _UTMPX_H
#define _UTMPX_H

#ifdef __cplusplus
extern "C" {
#endif

#include <features.h>

#define __NEED_pid_t
#define __NEED_time_t
#define __NEED_suseconds_t
#define __NEED_struct_timeval

#include <bits/alltypes.h>

struct utmpx {
        short ut_type;
        short __ut_pad1;
        pid_t ut_pid;
        char ut_line[32];
        char ut_id[4];
        char ut_user[32];
        char ut_host[256];
        struct {
                short __e_termination;
                short __e_exit;
        } ut_exit;
#if __BYTE_ORDER == 1234
        int ut_session, __ut_pad2;
#else
        int __ut_pad2, ut_session;
#endif
        struct timeval ut_tv;
        unsigned ut_addr_v6[4];
        char __unused[20];
};

//...

Indeed, there is a utmpx.ut_tv but no utmpx.__timeval.

Suggested behavior

It should build and install.

Environment:

jtmoon79 commented 1 year ago

tl;dr A good way to allow Alpine builds is using #[cfg(accessible(..))], e.g.

// if not defined __timeval then define it
#[cfg(not(accessible(::uapi::c::__timeval)))]
#[doc(hidden)]
#[derive(Clone, Copy)]
#[repr(C)]
pub struct __timeval {
    pub tv_sec: i32,
    pub tv_usec: i32,
}

These uapi imports have been handled by conditional compilation based on target_os. See https://github.com/jtmoon79/super-speedy-syslog-searcher/blob/0.5.59/src/data/utmpx.rs#L33-L117

But there is currently no way to further distinguish at build-time that this is Alpine Linux, e.g. there is no #[cfg(target_os_flavor = "Alpine")].

The ideal mechanism is cfg(accessible). This could be used to entirely replace conditional compilation based on target_os and would be more reliable.

Currently, #[cfg(accessible(..))] is still unimplemented https://github.com/rust-lang/rust/issues/64797

jtmoon79 commented 1 year ago

Version 0.6.62 fails less

   Compiling super_speedy_syslog_searcher v0.6.62
error[E0432]: unresolved import `uapi::c::__timeval`
   --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/super_speedy_syslog_searcher-0.6.62/src/data/utmpx.rs:118:13
    |
118 |             __timeval,
    |             ^^^^^^^^^
    |             |
    |             no `__timeval` in `c`
    |             help: a similar name exists in the module: `timeval`
jtmoon79 commented 6 months ago

Verified this built on Alpine 3.17 x64.