xref: /openbsd-src/usr.sbin/pkg_add/OpenBSD/LibSpec/Build.pm (revision 039cbdaaca23c9e872a2bab23f91224c76c0f23b)
19660dd3bSespie# ex:ts=8 sw=4:
2*039cbdaaSespie# $OpenBSD: Build.pm,v 1.8 2023/06/13 09:07:18 espie Exp $
39660dd3bSespie#
49660dd3bSespie# Copyright (c) 2010 Marc Espie <espie@openbsd.org>
59660dd3bSespie#
69660dd3bSespie# Permission to use, copy, modify, and distribute this software for any
79660dd3bSespie# purpose with or without fee is hereby granted, provided that the above
89660dd3bSespie# copyright notice and this permission notice appear in all copies.
99660dd3bSespie#
109660dd3bSespie# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
119660dd3bSespie# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
129660dd3bSespie# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
139660dd3bSespie# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
149660dd3bSespie# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
159660dd3bSespie# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
169660dd3bSespie# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
179660dd3bSespie#
18*039cbdaaSespieuse v5.36;
199660dd3bSespie
209660dd3bSespie# the specs used during build are slightly different from the specs at
219660dd3bSespie# runtime.
229660dd3bSespiepackage OpenBSD::Library::Static;
239660dd3bSespieour @ISA = qw(OpenBSD::Library);
24*039cbdaaSespiesub new($class, $dir, $stem)
259660dd3bSespie{
269660dd3bSespie	bless {dir => $dir, stem => $stem}, $class;
279660dd3bSespie}
289660dd3bSespie
29*039cbdaaSespiesub no_match_dispatch($library, $spec, $base)
309660dd3bSespie{
319660dd3bSespie	return $spec->no_match_static($library, $base);
329660dd3bSespie}
339660dd3bSespie
34*039cbdaaSespiesub to_string($self)
359660dd3bSespie{
369660dd3bSespie	return "$self->{dir}/lib$self->{stem}.a";
379660dd3bSespie}
389660dd3bSespie
39*039cbdaaSespiesub version($) { ".a" }
409660dd3bSespie
41*039cbdaaSespiesub is_static($) { 1 }
429660dd3bSespie
43*039cbdaaSespiesub is_better($, $) { 0 }
449660dd3bSespie
459660dd3bSespiepackage OpenBSD::Library::Build;
469660dd3bSespieour @ISA = qw(OpenBSD::Library);
479660dd3bSespie
48*039cbdaaSespiesub static($)
499660dd3bSespie{ 'OpenBSD::Library::Static'; }
509660dd3bSespie
51*039cbdaaSespiesub from_string($class, $filename)
529660dd3bSespie{
539660dd3bSespie	if (my ($dir, $stem) = $filename =~ m/^(.*)\/lib([^\/]+)\.a$/o) {
549660dd3bSespie		return $class->static->new($dir, $stem);
559660dd3bSespie	} else {
569660dd3bSespie		return $class->SUPER::from_string($filename);
579660dd3bSespie	}
589660dd3bSespie}
599660dd3bSespie
609660dd3bSespiepackage OpenBSD::LibSpec;
61*039cbdaaSespiesub no_match_static	# forwarder
629660dd3bSespie{
639660dd3bSespie	&OpenBSD::LibSpec::no_match_name;
649660dd3bSespie}
659660dd3bSespie
66*039cbdaaSespiesub findbest($spec, $repo, $base)
6789a05b10Sespie{
6889a05b10Sespie	my $spec2 = OpenBSD::LibSpec::GT->new($spec->{dir}, $spec->{stem},
6989a05b10Sespie	    0, 0);
7089a05b10Sespie	my $r = $spec2->lookup($repo, $base);
7189a05b10Sespie	my $best;
7289a05b10Sespie	for my $candidate (@$r) {
7389a05b10Sespie		if (!defined $best || $candidate->is_better($best)) {
7489a05b10Sespie			$best = $candidate;
7589a05b10Sespie		}
7689a05b10Sespie	}
7789a05b10Sespie	if (defined $best) {
7889a05b10Sespie		if ($best->is_static) {
7989a05b10Sespie			return $best;
8089a05b10Sespie		}
8189a05b10Sespie		if ($spec->match($best, $base)) {
8289a05b10Sespie			return $best;
8389a05b10Sespie		}
8489a05b10Sespie	}
8589a05b10Sespie	return undef;
8689a05b10Sespie}
8789a05b10Sespie
889660dd3bSespiepackage OpenBSD::LibSpec::GT;
899660dd3bSespieour @ISA = qw(OpenBSD::LibSpec);
90*039cbdaaSespiesub no_match_major($spec, $library)
919660dd3bSespie{
929660dd3bSespie	return $spec->major > $library->major;
939660dd3bSespie}
949660dd3bSespie
95*039cbdaaSespiesub to_string($self)
969660dd3bSespie{
9770f0d948Sespie	return $self->key.">=".$self->major.".".$self->minor;
989660dd3bSespie
999660dd3bSespie}
1009660dd3bSespie
1019660dd3bSespie
1029660dd3bSespiepackage OpenBSD::LibSpec::Build;
1039660dd3bSespieour @ISA = qw(OpenBSD::LibSpec);
1049660dd3bSespie
105*039cbdaaSespiesub new_from_string($class, $string)
1069660dd3bSespie{
1079660dd3bSespie	$string =~ s/\.$//;
1083d3e69b4Sespie	if (my ($stem, $strict, $major, $minor) = $string =~ m/^(.*?)(\>?)\=(\d+)\.(\d+)$/o) {
1099660dd3bSespie		return $class->new_object($stem, $strict, $major, $minor);
1103d3e69b4Sespie	} elsif (($stem, $strict, $major) = $string =~ m/^(.*?)(\>?)\=(\d+)$/o) {
1119660dd3bSespie		return $class->new_object($stem, $strict, $major, 0);
1129660dd3bSespie	} else {
1139660dd3bSespie		return $class->new_object($string, '>', 0, 0);
1149660dd3bSespie	}
1159660dd3bSespie}
1169660dd3bSespie
117*039cbdaaSespiesub new_object($class, $stem, $strict, $major, $minor)
1189660dd3bSespie{
1199660dd3bSespie	my $n = $strict eq '' ? "OpenBSD::LibSpec" : "OpenBSD::LibSpec::GT";
1209660dd3bSespie	return $n->new_with_stem($stem, $major, $minor);
1219660dd3bSespie}
1229660dd3bSespie
1239660dd3bSespie1;
124