我有一个 perl 脚本,我想在没有安装 perl 解释器的系统上使用它(例如 linux 容器)。我用 PAR::Packer 生成了一个二进制文件,一切看起来都很好。
[0] [ishpeck@centbox03 /tmp/examply]$ file ./perlthing/scripts/hello.pl
./perlthing/scripts/hello.pl: Perl script, ASCII text executable
[0] [ishpeck@centbox03 /tmp/examply]$ cat ./perlthing/scripts/hello.pl
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
print "Hello, world!\n"
[0] [ishpeck@centbox03 /tmp/examply]$ cat Makefile
CURDIR=/tmp/examply
TOPDIR=$(CURDIR)/packaging
rpm: perlbin
sh -c 'for x in packaging/BUILD packaging/RPMS/x86_64 packaging/RPMS/x86 packaging/RPMS/arm packaging/SOURCES packaging/SPECS packaging/SRPMS; do mkdir -p $$x; done'
rpmbuild --target x86_64 -bb $(TOPDIR)/SPECS/mypackage.spec --define '_topdir $(TOPDIR)' --define '_arch x86_64' --define '_working_dir $(CURDIR)'
perlbin: perlthing/binaries/hello.pl
perlthing/binaries/hello.pl: perlthing/scripts/hello.pl Makefile
/usr/local/bin/pp -M File::** -M PAR:: --clean --verbose=2 --output=$@ $<
clean:
rm perlthing/binaries/hello.pl
[0] [ishpeck@centbox03 /tmp/examply]$ make perlbin >/dev/null ; echo $?
0
[0] [ishpeck@centbox03 /tmp/examply]$ file perlthing/binaries/hello.pl
perlthing/binaries/hello.pl: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=7d5b38a792018d0202a96ce8645b00591e7ea7c5, stripped
[0] [ishpeck@centbox03 /tmp/examply]$ ./perlthing/binaries/hello.pl
Hello, world!
到目前为止,一切都很好。当我将二进制文件复制到没有 perl 解释器的机器时,它甚至可以工作:
core@CoreOS-1 ~ $ which perl
which: no perl in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/bin)
core@CoreOS-1 ~ $ ./hello.pl
Hello, world!
我想把它打包成一个 RPM:
[0] [ishpeck@centbox03 /tmp/examply]$ cat packaging/SPECS/mypackage.spec
Name: ishy-mypackage
Version: 0.1
Release: 1%{?dist}
Summary: My example package
License: MIT
URL: http://ishpeck.net
%description
This is my example package that includes a perl script bundled as a binary via PAR::Packer
%prep
echo "Prepare scriptlet is empty"
%build
echo "Nothing really to do"
%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/usr/local/bin/
cp %{_working_dir}/perlthing/binaries/hello.pl $RPM_BUILD_ROOT/usr/local/bin/
%files
/usr/local/bin/hello.pl
[0] [ishpeck@centbox03 /tmp/examply]$ grep rpmbuild Makefile
rpmbuild --target x86_64 -bb $(TOPDIR)/SPECS/mypackage.spec --define '_topdir $(TOPDIR)' --define '_arch x86_64' --define '_working_dir $(CURDIR)'
[0] [ishpeck@centbox03 /tmp/examply]$ make
但是当我安装软件包时,perl 二进制文件不再工作了!
[0] [ishpeck@centbox03 /tmp/examply]$ sudo rpm -i ./packaging/RPMS/x86_64/ishy-mypackage-0.1-1.el7.x86_64.rpm
[0] [ishpeck@centbox03 /tmp/examply]$ which hello.pl
/usr/local/bin/hello.pl
[0] [ishpeck@centbox03 /tmp/examply]$ hello.pl
Usage: /usr/local/bin/hello.pl [ -Alib.par ] [ -Idir ] [ -Mmodule ] [ src.par ] [ program.pl ]
/usr/local/bin/hello.pl [ -B|-b ] [-Ooutfile] src.par
Removing files in "/tmp/par-6973687065636b/temp-51174"
Undefined subroutine &File::Find::finddepth called at -e line 11.
END failed--call queue aborted at -e line 616.
我不认为这是我使用pp生成二进制文件的方式的问题,因为如果我直接复制它,它似乎无处不在。我的大脑想把责任归咎于(我如何使用)rpmbuild,但我什至不知道从哪里开始。
我在这里遗漏了一些关于 rpmbuild 的神秘细节吗?
rpmbuild 正在剥离它
默认的 rpmbuild 宏包括
strip
干扰任何神奇的 PAR::Packer 正在执行的命令。如果需要您的资源,我可以在 CentOS 7 perl v5.16.3 上重现该问题。在你的规范顶部添加这个:
buildroot 中的二进制文件未经修改,程序可以正常工作。我不完全确定为什么,您可能想向 Perl rpm 维护者或其他 Perl 黑客询问详细信息。
性能侧边栏
与实际的 perl 解释器相比,我注意到打包二进制文件的速度始终降低 100 倍。对于非平凡的程序,差异可能不太显着。即使这个微不足道的构建大小也只有 4 MB。
我很欣赏将Swiss-Army 电锯的强大功能引入未开化的发行版,但请注意,原生二进制文件不太可能提升性能。