add base files for 'common' loading

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2022-02-24 10:50:09 +01:00
parent 15c39cb258
commit f939c2bb55
2 changed files with 111 additions and 0 deletions

43
Proxmox/Lib/Common.pm Normal file
View File

@ -0,0 +1,43 @@
package Proxmox::Lib::Common;
=head1 NAME
Proxmox::Lib::Common - base module for rust bindings common between PVE and PMG
=head1 SYNOPSIS
package Proxmox::RS::CalendarEvent;
use base 'Proxmox::Lib::Common';
BEGIN { __PACKAGE__->bootstrap(); }
1;
=head1 DESCRIPTION
This is the base modules for bindings which are provided by both PVE and PMG. This will ensure that
either Proxmox::Lib::PVE or Proxmox::Lib::PMG have been loaded (in that order) and then use
whichever was loaded.
=cut
use vars qw(@ISA);
sub library {
return '-current';
}
BEGIN {
my $data = ($::{'proxmox-rs-library'} //= {});
my $base = $data->{-package};
if ($base) {
push @ISA, $base;
} else {
eval { require Proxmox::Lib::PVE and push @ISA, 'Proxmox::Lib::PVE'; };
eval { require Proxmox::Lib::PMG and push @ISA, 'Proxmox::Lib::PVE'; } if $@;
die $@ if $@;
}
}
1;

68
Proxmox/Lib/template.pm Normal file
View File

@ -0,0 +1,68 @@
package Proxmox::Lib::{{PRODUCT}};
=head1 NAME
Proxmox::Lib::{{PRODUCT}} - base module for {{PRODUCT}} rust bindings
=head1 SYNOPSIS
package {{PRODUCT}}::RS::SomeBindings;
use base 'Proxmox::Lib::{{PRODUCT}}';
BEGIN { __PACKAGE__->bootstrap(); }
1;
=head1 DESCRIPTION
This is the base module of all {{PRODUCT}} bindings.
Its job is to ensure the 'lib{{LIBRARY}}.so' library is loaded and provide a 'bootstrap' class
method to load the actual code.
=cut
use DynaLoader;
sub library {
return '{{LIBRARY}}';
}
sub load : prototype($) {
my ($pkg) = @_;
my $mod_name = $pkg->library();
my @dirs = (map "-L$_/auto", @INC);
my $mod_file = DynaLoader::dl_findfile({{DEBUG_LIBPATH}}@dirs, $mod_name);
die "failed to locate shared library for $mod_name (lib${mod_name}.so)\n" if !$mod_file;
my $lib = DynaLoader::dl_load_file($mod_file)
or die "failed to load library '$mod_file'\n";
my $data = ($::{'proxmox-rs-library'} //= {});
$data->{$mod_name} = $lib;
$data->{-current} //= $lib;
$data->{-package} //= $pkg;
}
sub bootstrap {
my ($pkg) = @_;
my $mod_name = $pkg->library();
my $bootstrap_name = 'boot_' . ($pkg =~ s/::/__/gr);
my $lib = $::{'proxmox-rs-library'}
or die "rust library not available for '{PRODUCT}'\n";
$lib = $lib->{$mod_name};
my $sym = DynaLoader::dl_find_symbol($lib, $bootstrap_name);
die "failed to locate '$bootstrap_name'\n" if !defined $sym;
my $boot = DynaLoader::dl_install_xsub($bootstrap_name, $sym, "src/FIXME.rs");
$boot->();
}
BEGIN { __PACKAGE__->load(); }
1;