Page 1 of 1

qa-ffi: On finding libc on Linux

Posted: Wed Aug 14, 2013 4:58 pm
by rickyboy
In a recent newlisp build on Linux by user hartrock (Stephan), the test qa-ffi failed because it couldn't find where libc was located on hartrock's Linux system. hartrock was building on a 64 bit system and qa-ffi was looking for the 32 bit libc (which naturally didn't exist on his system). The discussion was fleshed out a little here: viewtopic.php?f=16&t=4377.

The upshot is that the current expression which establishes the libc pointer is now the following.

Code: Select all

(define LIBC (case ostype
               ("Win32" "msvcrt.dll")
               ("OSX" "libc.dylib")
               ("Linux" (if is64bit
                            "/lib/x86_64-linux-gnu/libc.so.6"
                            "/lib/i386-linux-gnu/libc.so.6"))
             ))
But hartrock suggested that the definition might be changed to be more robust ("Works for me (Debian 64-bit), but should be checked for other Linuxes, too"). He's right, of course.

So, here are two methods I found which seem more robust than the current method. I wonder if you Linux gearheads here would scrutinize these.

One method is to ask gcc where libc is located. This method is discussed here: http://stackoverflow.com/questions/9705 ... er#9706172. Here it is implemented.

Code: Select all

(define LIBC
  (case ostype
    ("Win32" "msvcrt.dll")
    ("OSX" "libc.dylib")
    ("Linux"
     (letn (libc-name (first (exec "gcc -print-file-name=libc.so"))
            libc-filetype (first (exec (string "file -b " libc-name))))
       (if (find "ASCII" libc-filetype)
           (first (regex "[^ \t]+/libc\\.so\\.*[0-9]*" (read-file libc-name)))
           libc-name)))
    ))
Another method is to call ldd on the newlisp executable. This resource talks about this: http://mylinuxbook.com/how-to-determine ... ux-system/. Here is that one implemented. It assumes that the user has built the newlisp executable in the build directory.

Code: Select all

(define LIBC
  (case ostype
    ("Win32" "msvcrt.dll")
    ("OSX" "libc.dylib")
    ("Linux"
     (nth 2
      (parse
       (first
        (filter (curry find "libc.so")
                (if (ends-with (real-path) "qa-specific-tests")
                    (exec "ldd ../newlisp")
                    (exec "ldd ./newlisp")))))))
    ))
I've only tested these on an Ubuntu 12.04 system. I favor the first method. Thoughts? Thanks!

Re: qa-ffi: On finding libc on Linux

Posted: Wed Aug 14, 2013 9:42 pm
by Lutz
Thanks Rick. Also runs fine on the next version UBUNTU 13.04 32-bits.