3 # This tests that libraries listed in LD_LIBRARY_PATH take precedence over those listed in RPATH.
6 # A simple test library: libgreeting.so which exports a single function getGreeting() returning the good old hello greeting.
7 libgreeting = stdenv.mkDerivation {
11 const char* getGreeting() { return "Hello, world!"; }
15 echo "$code" > libgreeting.c
20 $CC -c -fpic libgreeting.c
21 $CC -shared libgreeting.o -o $out/lib/libgreeting.so
25 # A variant of libgreeting.so that returns a different message.
26 libgoodbye = libgreeting.overrideAttrs (_: {
29 const char* getGreeting() { return "Goodbye, world!"; }
33 # A simple consumer of libgreeting.so that just prints the greeting to stdout.
34 testProgram = stdenv.mkDerivation {
35 name = "greeting-test";
37 buildInputs = [ libgreeting ];
42 extern const char* getGreeting(void);
50 echo "$code" > greeting-test.c
55 $CC -c greeting-test.c
56 $CC greeting-test.o -lgreeting -o $out/bin/greeting-test
58 # Now test the installed binaries right after compiling them. In particular,
59 # don't do this in installCheckPhase because fixupPhase has been run by then!
64 # Verify that our unmodified binary works as expected.
65 [ "$(greeting-test)" = "Hello, world!" ]
67 # And finally, test that a library in LD_LIBRARY_PATH takes precedence over the linked-in library.
68 [ "$(LD_LIBRARY_PATH=${libgoodbye}/lib greeting-test)" = "Goodbye, world!" ]
73 in stdenv.mkDerivation {
74 name = "test-LD_LIBRARY_PATH";
75 nativeBuildInputs = [ testProgram ];
78 # And for good measure, repeat the tests again from a separate derivation,
79 # as fixupPhase done by the stdenv can (and has!) affect the result.
81 [ "$(greeting-test)" = "Hello, world!" ]
82 [ "$(LD_LIBRARY_PATH=${libgoodbye}/lib greeting-test)" = "Goodbye, world!" ]
87 meta.platforms = lib.platforms.linux;