Subject: [PATCH v2] syscalls: Add support for musl libc

From: [email-masked]
Date: Sat, 9 Sep 2023 15:04:28 +0200

From: Sören Tempel <[email-masked]>

This commit allows using Guix on a foreign distro which uses musl libc,
for example, Alpine Linux. Such a distro is detected via the new
linux-musl? variable based on the %host-type.

Using the new linux-musl? variable, we can now implement musl-specific
quirks. The two compatibility problems I encountered in this regard are
that musl dose not export a readdir64 and statfs64 symbol. On musl,
these two functions are implemented as CPP macros that expand to
readdir/statfs. For this reason, a case-distinction was added.

The existing linux? variable is now set to a truth value if the
host-system is either a linux-gnu or a linux-musl. A new linux-gnu?
variable can be used to detect linux-gnu systems.

The patch has been tested on Alpine Linux and is already used for the
downstream Guix package shipped in Alpine Linux's package repository.

* guix/build/syscalls.scm (linux-gnu?): New variable.
* guix/build/syscalls.scm (linux-musl?): New variable.
* guix/build/syscalls.scm (linux?): Truth value on musl or GNU Linux.
* guix/build/syscalls.scm (readdir-procedure): Support musl libc.
* guix/build/syscalls.scm (statfs): Support musl libc.

Signed-off-by: Sören Tempel <[email-masked]>
---
Changes since v1: Also add special handling for musl libc to the statfs
procedure. Instead of checking the %host-type, it may also be possible to
the lack of statfs64/readdir64 symbols during ./configure time.

 guix/build/syscalls.scm | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm
index d947b010d3..416fdc768c 100644
--- a/guix/build/syscalls.scm
+++ b/guix/build/syscalls.scm
@@ -836,7 +836,9 @@ (define-record-type <file-system>
 (define-syntax fsword                             ;fsword_t
   (identifier-syntax long))
 
-(define linux? (string-contains %host-type "linux-gnu"))
+(define linux-gnu?  (string-contains %host-type "linux-gnu"))
+(define linux-musl? (string-contains %host-type "linux-musl"))
+(define linux?      (or linux-gnu? linux-musl?))
 
 (define-syntax define-statfs-flags
   (syntax-rules (linux hurd)
@@ -905,7 +907,11 @@ (define-c-struct %statfs                          ;<bits/statfs.h>
   (spare            (array fsword 4)))
 
 (define statfs
-  (let ((proc (syscall->procedure int "statfs64" '(* *))))
+  (let ((proc (syscall->procedure int (cond
+                                        (linux-gnu?  "statfs64")
+                                        (linux-musl? "statfs")
+                                        (else (error "unknown linux variant")))
+                                      '(* *))))
     (lambda (file)
       "Return a <file-system> data structure describing the file system
 mounted at FILE."
@@ -1232,7 +1238,12 @@ (define closedir*
 
 (define (readdir-procedure name-field-offset sizeof-dirent-header
                            read-dirent-header)
-  (let ((proc (syscall->procedure '* "readdir64" '(*))))
+  (let ((proc (syscall->procedure '*
+                                  (cond
+                                    (linux-gnu?  "readdir64")
+                                    (linux-musl? "readdir")
+                                    (else (error "unknown linux variant")))
+                                  '(*))))
     (lambda* (directory #:optional (pointer->string pointer->string/utf-8))
       (let ((ptr (proc directory)))
         (and (not (null-pointer? ptr))