From f939c2bb5513cf83e030e1cda0ac8c88ed3caf1c Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 24 Feb 2022 10:50:09 +0100 Subject: [PATCH] add base files for 'common' loading Signed-off-by: Wolfgang Bumiller --- Proxmox/Lib/Common.pm | 43 ++++++++++++++++++++++++++ Proxmox/Lib/template.pm | 68 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 Proxmox/Lib/Common.pm create mode 100644 Proxmox/Lib/template.pm diff --git a/Proxmox/Lib/Common.pm b/Proxmox/Lib/Common.pm new file mode 100644 index 0000000..d8a0d57 --- /dev/null +++ b/Proxmox/Lib/Common.pm @@ -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; diff --git a/Proxmox/Lib/template.pm b/Proxmox/Lib/template.pm new file mode 100644 index 0000000..d7cee5f --- /dev/null +++ b/Proxmox/Lib/template.pm @@ -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;