genpackage: add --include-file option to add extra code

We'll use this to inject a 'use SslProbe.pm;' in pve and pmg to avoid
openssl-probe manipulating environment variables.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2023-12-18 09:43:21 +01:00
parent c6d49a8cde
commit c05806d1d8

View File

@ -30,6 +30,10 @@ my $opts = {
'PATH', 'PATH',
"Path to a debug library, usually ./target/debug.", "Path to a debug library, usually ./target/debug.",
], ],
'include-file' => [
'PATH',
"Path to additional perl code to include in the package after the 'use' statements",
],
}; };
sub help { sub help {
@ -49,7 +53,9 @@ if (!@ARGV) {
exit(1); exit(1);
} }
my $params = {}; my $params = {
'include-file' => [],
};
ARGPARSE: while (@ARGV) { ARGPARSE: while (@ARGV) {
my $arg = shift @ARGV; my $arg = shift @ARGV;
@ -62,17 +68,20 @@ ARGPARSE: while (@ARGV) {
for my $o (keys %$opts) { for my $o (keys %$opts) {
if ($arg =~ /^(?:--\Q$o\E=)(.+)$/) { if ($arg =~ /^(?:--\Q$o\E=)(.+)$/) {
my $arg = $1; $arg = $1;
die "multiple --$o options provided\n" if defined($params->{$o});
$params->{$o} = $arg;
next ARGPARSE;
} elsif ($arg =~ /^--\Q$o\E$/) { } elsif ($arg =~ /^--\Q$o\E$/) {
$arg = shift @ARGV; $arg = shift @ARGV;
die "multiple --$o options provided\n" if defined($params->{$o}); } else {
next;
};
die "--$o requires an argument\n" if !defined($arg); die "--$o requires an argument\n" if !defined($arg);
if (ref($params->{$o}) eq 'ARRAY') {
push $params->{$o}->@*, $arg;
} else {
die "multiple --$o options provided\n" if defined($params->{$o});
$params->{$o} = $arg; $params->{$o} = $arg;
next ARGPARSE;
} }
next ARGPARSE;
} }
if ($arg =~ /^-/) { if ($arg =~ /^-/) {
@ -92,6 +101,13 @@ my $lib = $params->{'lib'}
or die "missing --lib parameter\n"; or die "missing --lib parameter\n";
my $lib_tag = $params->{'lib-tag'}; my $lib_tag = $params->{'lib-tag'};
my $debug_libpath = $params->{'debug-libpath'} // ''; my $debug_libpath = $params->{'debug-libpath'} // '';
my $extra_code = '';
for my $file ($params->{'include-file'}->@*) {
open(my $fh, '<', $file) or die "failed to open file '$file' - $!\n";
my $more = do { local $/ = undef; <$fh> };
die "error reading '$file': $!\n" if !defined($more);
$extra_code .= $more;
}
sub pkg2file { sub pkg2file {
return ($_[0] =~ s@::@/@gr) . ".pm"; return ($_[0] =~ s@::@/@gr) . ".pm";
@ -135,6 +151,7 @@ use warnings;
use DynaLoader; use DynaLoader;
{{EXTRA_CODE}}
sub library { '{{LIBRARY}}' } sub library { '{{LIBRARY}}' }
sub autodirs { map { "$_/auto" } @INC; } sub autodirs { map { "$_/auto" } @INC; }
@ -191,6 +208,7 @@ BEGIN {
1; 1;
EOF EOF
$template =~ s/\{\{EXTRA_CODE\}\}/$extra_code/g;
$template =~ s/\{\{LIBRARY_PACKAGE\}\}/$lib_package/g; $template =~ s/\{\{LIBRARY_PACKAGE\}\}/$lib_package/g;
$template =~ s/\{\{LIBRARY_PREFIX\}\}/$lib_prefix/g; $template =~ s/\{\{LIBRARY_PREFIX\}\}/$lib_prefix/g;
$template =~ s/\{\{LIBRARY_TAG\}\}/$lib_tag/g; $template =~ s/\{\{LIBRARY_TAG\}\}/$lib_tag/g;