Skip to content

hmatrix doesn't build against musl libc #279

@nh2

Description

@nh2

For the advancement of static linking in Haskell (https://github.com/nh2/static-haskell-nix and NixOS/nixpkgs#43795) I'm building all Stackage executables statically, which means building against musl libc instead of glibc.

hmatrix doesn't compile against musl.

The biggest issue seems to be the use of the random_rI() function with is a glibc extension.

I get:

src/Internal/C/vector-aux.c:1066:5: error:
     warning: implicit declaration of function ‘random_r’; did you mean ‘random’? [-Wimplicit-function-declaration]
         random_r(buffer,&res);
         ^~~~~~~~

The code already handles absence of random_r on some platforms:

#if defined (__APPLE__) || (__FreeBSD__)
/* FreeBSD and Mac OS X do not provide random_r(), thread safety cannot be
guaranteed.
For FreeBSD and Mac OS X, nrand48() is much better than random().
See: http://www.evanjones.ca/random-thread-safe.html
*/
#pragma message "randomVector is not thread-safe in OSX and FreeBSD"
#endif

But the way the decision is done is not good: defined (__APPLE__) || (__FreeBSD__).

Instead it should better use the feature test macros as described in the man page, which are specifically designed for this task:

       random_r(), srandom_r(), initstate_r(), setstate_r():
           /* Glibc since 2.19: */ _DEFAULT_SOURCE
               || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE

So you should be able to check for the presence of random_r() by checking

/* Glibc since 2.19: */ _DEFAULT_SOURCE || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE

in your #if. That evaluating to 1 means that random_r() is available, otherwise it may not be available.

I would very much appreciate if you can help me with this as static linking will make numerical Haskell programs much easier to deploy and run in a distributed fashion.


I have researched 30 minutes into whether there's a significantly better alternative function to use in musl vs what you do on FreeBSD but haven't found anything real yet.

So it would probably be sufficient for now if musl went down the FreeBSD/OSX code path.

This article may be interesting:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions