move the interface to SharedLibs to be somewhat object oriented
authorespie <espie@openbsd.org>
Sun, 21 May 2023 16:07:35 +0000 (16:07 +0000)
committerespie <espie@openbsd.org>
Sun, 21 May 2023 16:07:35 +0000 (16:07 +0000)
accordingly, load it "just in time" in State.

Most calls get simplified, and we can save more state for later.

usr.sbin/pkg_add/OpenBSD/Dependencies.pm
usr.sbin/pkg_add/OpenBSD/Dependencies/SolverBase.pm
usr.sbin/pkg_add/OpenBSD/OldLibs.pm
usr.sbin/pkg_add/OpenBSD/PkgAdd.pm
usr.sbin/pkg_add/OpenBSD/PkgCheck.pm
usr.sbin/pkg_add/OpenBSD/PkgCreate.pm
usr.sbin/pkg_add/OpenBSD/SharedLibs.pm
usr.sbin/pkg_add/OpenBSD/State.pm

index 6dafcbb..1e40ca0 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: Dependencies.pm,v 1.173 2019/10/13 16:22:58 espie Exp $
+# $OpenBSD: Dependencies.pm,v 1.174 2023/05/21 16:07:35 espie Exp $
 #
 # Copyright (c) 2005-2010 Marc Espie <espie@openbsd.org>
 #
@@ -17,7 +17,6 @@
 use strict;
 use warnings;
 
-use OpenBSD::SharedLibs;
 use OpenBSD::Dependencies::SolverBase;
 
 package _cache;
@@ -368,8 +367,8 @@ sub find_old_lib
 
        my $r = $state->repo->installed->match_locations(OpenBSD::Search::PkgSpec->new(".libs-".$pattern));
        for my $try (map {$_->name} @$r) {
-               OpenBSD::SharedLibs::add_libs_from_installed_package($try, $state);
-               if ($self->check_lib_spec($base, $lib, {$try => 1})) {
+               $state->shlibs->add_libs_from_installed_package($try);
+               if ($self->check_lib_spec($state, $base, $lib, {$try => 1})) {
                        return $try;
                }
        }
index dd664f1..36580de 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: SolverBase.pm,v 1.13 2021/06/21 14:36:48 espie Exp $
+# $OpenBSD: SolverBase.pm,v 1.14 2023/05/21 16:07:35 espie Exp $
 #
 # Copyright (c) 2005-2018 Marc Espie <espie@openbsd.org>
 #
@@ -127,7 +127,7 @@ sub find_in_already_done
        my ($self, $solver, $state, $obj) = @_;
 
 
-       my $r = $solver->check_lib_spec($solver->{localbase}, $obj,
+       my $r = $solver->check_lib_spec($state, $solver->{localbase}, $obj,
            $self->{known});
        if ($r) {
                $self->say_found($state, $obj, $state->f("package #1", $r));
@@ -142,9 +142,9 @@ sub find_in_extra_sources
        my ($self, $solver, $state, $obj) = @_;
        return undef if !$obj->is_valid || defined $obj->{dir};
 
-       OpenBSD::SharedLibs::add_libs_from_system($state->{destdir}, $state);
-       for my $dir (OpenBSD::SharedLibs::system_dirs()) {
-               if ($solver->check_lib_spec($dir, $obj, {system => 1})) {
+       $state->shlibs->add_libs_from_system($state->{destdir});
+       for my $dir ($state->shlibs->system_dirs) {
+               if ($solver->check_lib_spec($state, $dir, $obj, {system => 1})) {
                        $self->say_found($state, $obj, $state->f("#1/lib", $dir));
                        return 'system';
                }
@@ -157,11 +157,11 @@ sub find_in_new_source
        my ($self, $solver, $state, $obj, $dep) = @_;
 
        if (defined $solver->{set}{newer}{$dep}) {
-               OpenBSD::SharedLibs::add_libs_from_plist($solver->{set}{newer}{$dep}->plist, $state);
+               $state->shlibs->add_libs_from_plist($solver->{set}{newer}{$dep}->plist);
        } else {
-               OpenBSD::SharedLibs::add_libs_from_installed_package($dep, $state);
+               $state->shlibs->add_libs_from_installed_package($dep);
        }
-       if ($solver->check_lib_spec($solver->{localbase}, $obj, {$dep => 1})) {
+       if ($solver->check_lib_spec($state, $solver->{localbase}, $obj, {$dep => 1})) {
                $self->say_found($state, $obj, $state->f("package #1", $dep));
                return $dep;
        }
@@ -373,8 +373,7 @@ sub solve_wantlibs
                                $solver->errsay_library($state, $h);
                        }
                        $okay = 0;
-                       OpenBSD::SharedLibs::report_problem($state,
-                           $lib->spec);
+                       $state->shlibs->report_problem($lib->spec);
                }
        }
        if (!$okay) {
@@ -409,8 +408,8 @@ sub dependencies
 
 sub check_lib_spec
 {
-       my ($self, $base, $spec, $dependencies) = @_;
-       my $r = OpenBSD::SharedLibs::lookup_libspec($base, $spec);
+       my ($self, $state, $base, $spec, $dependencies) = @_;
+       my $r = $state->shlibs->lookup_libspec($base, $spec);
        for my $candidate (@$r) {
                if ($dependencies->{$candidate->origin}) {
                        return $candidate->origin;
index 94c4057..646f662 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: OldLibs.pm,v 1.14 2023/05/16 14:31:26 espie Exp $
+# $OpenBSD: OldLibs.pm,v 1.15 2023/05/21 16:07:35 espie Exp $
 #
 # Copyright (c) 2004-2010 Marc Espie <espie@openbsd.org>
 #
@@ -214,9 +214,8 @@ sub do_save_libs
 
 
        if ($state->{not}) {
-               require OpenBSD::SharedLibs;
 
-               OpenBSD::SharedLibs::add_libs_from_plist($stub_list, $state);
+               $state->shlibs->add_libs_from_plist($stub_list);
                $stub_list->to_cache;
                $o->plist->to_cache;
        } else {
index 9ad1878..da35e24 100644 (file)
@@ -1,7 +1,7 @@
 #! /usr/bin/perl
 
 # ex:ts=8 sw=4:
-# $OpenBSD: PkgAdd.pm,v 1.139 2023/05/19 07:30:40 espie Exp $
+# $OpenBSD: PkgAdd.pm,v 1.140 2023/05/21 16:07:35 espie Exp $
 #
 # Copyright (c) 2003-2014 Marc Espie <espie@openbsd.org>
 #
@@ -671,7 +671,6 @@ use OpenBSD::PackageInfo;
 use OpenBSD::PackageName;
 use OpenBSD::PkgCfl;
 use OpenBSD::Add;
-use OpenBSD::SharedLibs;
 use OpenBSD::UpdateSet;
 use OpenBSD::Error;
 
@@ -892,7 +891,7 @@ sub really_add
        for my $handle ($set->newer) {
                my $pkgname = $handle->pkgname;
                my $plist = $handle->plist;
-               OpenBSD::SharedLibs::add_libs_from_plist($plist, $state);
+               $state->shlibs->add_libs_from_plist($plist);
                OpenBSD::Add::tweak_plist_status($plist, $state);
                OpenBSD::Add::register_installation($plist, $state);
                add_installed($pkgname);
index eeba23a..d42c36b 100644 (file)
@@ -1,7 +1,7 @@
 #! /usr/bin/perl
 
 # ex:ts=8 sw=4:
-# $OpenBSD: PkgCheck.pm,v 1.76 2022/03/15 08:12:53 espie Exp $
+# $OpenBSD: PkgCheck.pm,v 1.77 2023/05/21 16:07:35 espie Exp $
 #
 # Copyright (c) 2003-2014 Marc Espie <espie@openbsd.org>
 #
@@ -21,7 +21,6 @@ use strict;
 use warnings;
 
 use OpenBSD::AddCreateDelete;
-use OpenBSD::SharedLibs;
 
 package Installer::State;
 our @ISA = qw(OpenBSD::PkgAdd::State);
@@ -77,8 +76,8 @@ sub find_dependencies
 
 sub mark_indirect_depends
 {
-       my $self = shift;
-       $self->mark_available_lib(@_);
+       my ($self, $pkgname, $state) = @_;
+       $self->mark_available_lib($pkgname, $state->shlibs);
 }
 
 sub cache_depends
@@ -290,7 +289,7 @@ package OpenBSD::PackingElement::Wantlib;
 sub find_dependencies
 {
        my ($self, $state, $l, $checker, $pkgname) = @_;
-       my $r = OpenBSD::SharedLibs::lookup_libspec($state->{localbase},
+       my $r = $state->shlibs->lookup_libspec($state->{localbase},
            $self->spec);
        if (defined $r && @$r != 0) {
                my $okay = 0;
@@ -854,7 +853,7 @@ sub sanity_check
 sub dependencies_check
 {
        my ($self, $state, $l) = @_;
-       OpenBSD::SharedLibs::add_libs_from_system($state->{destdir}, $state);
+       $state->shlibs->add_libs_from_system($state->{destdir});
        $self->for_all_packages($state, $l, "Direct dependencies", sub {
                my $name = shift;
                $state->log->set_context($name);
@@ -897,7 +896,7 @@ sub package_files_check
                } else {
                        $plist->thorough_check($state);
                }
-               $plist->mark_available_lib($plist->pkgname, $state);
+               $plist->mark_available_lib($plist->pkgname, $state->shlibs);
        });
 }
 
index 22d287f..0efe938 100644 (file)
@@ -1,6 +1,6 @@
 #! /usr/bin/perl
 # ex:ts=8 sw=4:
-# $OpenBSD: PkgCreate.pm,v 1.187 2023/05/17 15:51:58 espie Exp $
+# $OpenBSD: PkgCreate.pm,v 1.188 2023/05/21 16:07:35 espie Exp $
 #
 # Copyright (c) 2003-2014 Marc Espie <espie@openbsd.org>
 #
@@ -21,7 +21,6 @@ use warnings;
 
 use OpenBSD::AddCreateDelete;
 use OpenBSD::Dependencies::SolverBase;
-use OpenBSD::SharedLibs;
 use OpenBSD::Signer;
 
 package OpenBSD::PkgCreate::State;
@@ -964,8 +963,7 @@ sub solve_wantlibs
                    $solver->{to_register}{$h}, $state,
                    $lib->spec);
                $okay = 0;
-               OpenBSD::SharedLibs::report_problem($state,
-                   $lib->spec) if $final;
+               $state->shlibs->report_problem($lib->spec) if $final;
        }
        if (!$okay && $final) {
                $solver->dump($state);
@@ -1104,7 +1102,7 @@ sub really_solve_from_ports
                        $self->to_cache($plist, $diskcache);
                }
        }
-       OpenBSD::SharedLibs::add_libs_from_plist($plist, $state);
+       $state->shlibs->add_libs_from_plist($plist);
        $self->{tag_finder}->find_in_plist($plist, $dep->{pkgpath});
        $self->add_dep($plist);
        return $plist->pkgname;
index 71b35fb..9ade3a1 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: SharedLibs.pm,v 1.59 2017/09/16 12:04:13 espie Exp $
+# $OpenBSD: SharedLibs.pm,v 1.60 2023/05/21 16:07:35 espie Exp $
 #
 # Copyright (c) 2003-2010 Marc Espie <espie@openbsd.org>
 #
 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
-use strict;
-use warnings;
+use v5.36;
 
 use OpenBSD::Paths;
 use OpenBSD::LibSpec;
 
 package OpenBSD::PackingElement;
 
-sub mark_available_lib
+sub mark_available_lib($, $, $)
 {
 }
 
 package OpenBSD::PackingElement::Lib;
 
-sub mark_available_lib
+sub mark_available_lib($self, $pkgname, $object)
 {
-       my ($self, $pkgname, $state) = @_;
-       OpenBSD::SharedLibs::register_libname($self->fullname,
-           $pkgname, $state);
+       $object->register_libname($self->fullname, $pkgname);
 }
 
 package OpenBSD::SharedLibs;
 use File::Basename;
 use OpenBSD::Error;
 
-our $repo = OpenBSD::LibRepo->new;
+sub _basestate($)
+{
+       require OpenBSD::Basestate;
+       return 'OpenBSD::BaseState';
+}
 
-sub register_library
+sub new($class, $state = $class->_basestate)
 {
-       my ($lib, $pkgname) = @_;
-       $repo->register($lib, $pkgname);
+       bless {
+           state => $state,
+           repo => OpenBSD::LibRepo->new,
+           printed => {},
+           done_plist => {},
+           done_system => 0
+           }, $class;
 }
 
-sub register_libname
+
+sub register_library($self, $lib, $pkgname)
+{
+       $self->{repo}->register($lib, $pkgname);
+}
+
+sub register_libname($self, $name, $pkgname)
 {
-       my ($name, $pkgname, $state) = @_;
        my $lib = OpenBSD::Library->from_string($name);
        if ($lib->is_valid) {
-               register_library($lib, $pkgname);
+               $self->register_library($lib, $pkgname);
        } else {
-               $state->errsay("Bogus library in #1: #2", $pkgname, $name)
-                   unless $pkgname eq 'system';
+               $self->{state}->errsay("Bogus library in #1: #2", $pkgname, 
+                   $name) unless $pkgname eq 'system';
        }
 
 }
 
 sub find_best
 {
-       my ($class, $stem) = @_;
-       return $repo->find_best($stem);
+       my ($self, $stem) = @_;
+       return $self->{repo}->find_best($stem);
 }
 
-my $done_plist = {};
-
-sub system_dirs
+sub system_dirs($)
 {
        return OpenBSD::Paths->library_dirs;
 }
 
-sub add_libs_from_system
+sub add_libs_from_system($self, $destdir)
 {
-       my ($destdir, $state) = @_;
-       return if $done_plist->{'system'};
-       $done_plist->{'system'} = 1;
-       for my $dirname (system_dirs()) {
+       return if $self->{done_system};
+       $self->{done_system} = 1;
+       for my $dirname ($self->system_dirs) {
                opendir(my $dir, $destdir.$dirname."/lib") or next;
                while (my $d = readdir($dir)) {
                        next unless $d =~ m/\.so/;
-                       register_libname("$dirname/lib/$d", 'system', $state);
+                       $self->register_libname("$dirname/lib/$d", 'system');
                }
                closedir($dir);
        }
 }
 
-sub add_libs_from_installed_package
+sub add_libs_from_installed_package($self, $pkgname)
 {
-       my ($pkgname, $state) = @_;
-       return if $done_plist->{$pkgname};
-       $done_plist->{$pkgname} = 1;
+       return if $self->{done_plist}{$pkgname};
+       $self->{done_plist}{$pkgname} = 1;
        my $plist = OpenBSD::PackingList->from_installation($pkgname,
            \&OpenBSD::PackingList::LibraryOnly);
        return if !defined $plist;
 
-       $plist->mark_available_lib($pkgname, $state);
+       $plist->mark_available_lib($pkgname, $self);
 }
 
-sub add_libs_from_plist
+sub add_libs_from_plist($self, $plist)
 {
-       my ($plist, $state) = @_;
        my $pkgname = $plist->pkgname;
-       return if $done_plist->{$pkgname};
-       $done_plist->{$pkgname} = 1;
-       $plist->mark_available_lib($pkgname, $state);
+       return if $self->{done_plist}{$pkgname};
+       $self->{done_plist}{$pkgname} = 1;
+       $plist->mark_available_lib($pkgname, $self);
 }
 
-sub lookup_libspec
+sub lookup_libspec($self, $base, $spec)
 {
-       my ($base, $spec) = @_;
-       return $spec->lookup($repo, $base);
+       return $spec->lookup($self->{repo}, $base);
 }
 
-my $printed = {};
 
-sub report_problem
+sub report_problem($self, $spec)
 {
-       my ($state, $spec) = @_;
        my $name = $spec->to_string;
+       my $state = $self->{state};
        my $base = $state->{localbase};
-       my $approx = $spec->lookup_stem($repo);
+       my $approx = $spec->lookup_stem($self->{repo});
+       my $printed = $self->{printed};
 
        my $r = "";
        if (!$spec->is_valid) {
index 6ca9fdb..46d55f9 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: State.pm,v 1.72 2023/05/19 07:25:16 espie Exp $
+# $OpenBSD: State.pm,v 1.73 2023/05/21 16:07:35 espie Exp $
 #
 # Copyright (c) 2007-2014 Marc Espie <espie@openbsd.org>
 #
@@ -164,6 +164,13 @@ OpenBSD::Auto::cache(installpath,
                }
        });
 
+OpenBSD::Auto::cache(shlibs,
+       sub {
+               my $self = shift;
+               require OpenBSD::SharedLibs;
+               return $self->{shlibs} //= OpenBSD::SharedLibs->new($self);
+       });
+
 sub usage_is
 {
        my ($self, @usage) = @_;