If you want it to take effect for XML::Parser::Expat, you have to put the declaration in ./Expat/Makefile.PL, not ./Makefile.PL.
For example, your ./Expat/Makefile.PL could read:
...
WriteMakefile(
NAME => 'XML::Parser::Expat',
C => ['Expat.c'],
LIBS => $libs,
XSPROTOARG => '-noprototypes',
VERSION_FROM => 'Expat.pm',
LDDLFLAGS => $Config{lddlflags}." -Wl,-rpath,$ENV{ORIGIN}/../lib",
+ # <-- add this
@extras
);
Sample build session:
$ ORIGIN=/usr/local/foo/bar perl Makefile.PL
Writing Makefile for XML::Parser::Expat
Writing Makefile for XML::Parser
$ make
...
rm -f ../blib/arch/auto/XML/Parser/Expat/Expat.so
LD_RUN_PATH="/lib" gcc -shared -O2 -L/usr/local/lib -fstack-protecto
+r -Wl,-rpath,/usr/local/foo/bar/../lib Expat.o -o ../blib/arch/auto/
+XML/Parser/Expat/Expat.so \
-lexpat \
...
$ readelf -d ./blib/arch/auto/XML/Parser/Expat/Expat.so | grep Lib
0x000000000000000f (RPATH) Library rpath: [/usr/local/fo
+o/bar/../lib]
0x000000000000001d (RUNPATH) Library runpath: [/usr/local/
+foo/bar/../lib]
Similarly with the dynamic_lib => { ... } variant:
WriteMakefile(
NAME => 'XML::Parser::Expat',
C => ['Expat.c'],
LIBS => $libs,
XSPROTOARG => '-noprototypes',
VERSION_FROM => 'Expat.pm',
dynamic_lib => {
OTHERLDFLAGS => "-Wl,-rpath,$ENV{ORIGIN}/../lib"
},
@extras
);
$ ORIGIN=/usr/local/foo/bar perl Makefile.PL
Writing Makefile for XML::Parser::Expat
Writing Makefile for XML::Parser
$ make
...
rm -f ../blib/arch/auto/XML/Parser/Expat/Expat.so
LD_RUN_PATH="/lib" gcc -shared -O2 -L/usr/local/lib -fstack-protecto
+r Expat.o -Wl,-rpath,/usr/local/foo/bar/../lib -o ../blib/arch/auto/X
+ML/Parser/Expat/Expat.so \
-lexpat \
...
$ readelf -d ./blib/arch/auto/XML/Parser/Expat/Expat.so | grep Lib
0x000000000000000f (RPATH) Library rpath: [/usr/local/fo
+o/bar/../lib]
0x000000000000001d (RUNPATH) Library runpath: [/usr/local/
+foo/bar/../lib]
(Note the "-Wl,-rpath,/usr/local/foo/bar/../lib" part in the linking commands above, and its different position.)