summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/OpenBSD/LibSpec/Build.pm
blob: 4dc3dbc7cc9791d3d39b73237debc72a164c653b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# ex:ts=8 sw=4:
# $OpenBSD: Build.pm,v 1.7 2010/11/27 11:55:14 espie Exp $
#
# Copyright (c) 2010 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# 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;

# the specs used during build are slightly different from the specs at
# runtime.
package OpenBSD::Library::Static;
our @ISA = qw(OpenBSD::Library);
sub new
{
	my ($class, $dir, $stem) = @_;
	bless {dir => $dir, stem => $stem}, $class;
}

sub no_match_dispatch
{
	my ($library, $spec, $base) = @_;
	return $spec->no_match_static($library, $base);
}

sub to_string
{
	my $self = shift;
	return "$self->{dir}/lib$self->{stem}.a";
}

sub version { ".a" }

sub is_static { 1 }

sub is_better { 0 }

package OpenBSD::Library::Build;
our @ISA = qw(OpenBSD::Library);

sub static
{ 'OpenBSD::Library::Static'; }

sub from_string
{
	my ($class, $filename) = @_;
	if (my ($dir, $stem) = $filename =~ m/^(.*)\/lib([^\/]+)\.a$/o) {
		return $class->static->new($dir, $stem);
	} else {
		return $class->SUPER::from_string($filename);
	}
}

package OpenBSD::LibSpec;
sub no_match_static
{
	&OpenBSD::LibSpec::no_match_name;
}

sub findbest
{
	my ($spec, $repo, $base) = @_;
	my $spec2 = OpenBSD::LibSpec::GT->new($spec->{dir}, $spec->{stem},
	    0, 0);
	my $r = $spec2->lookup($repo, $base);
	my $best;
	for my $candidate (@$r) {
		if (!defined $best || $candidate->is_better($best)) {
			$best = $candidate;
		}
	}
	if (defined $best) {
		if ($best->is_static) {
			return $best;
		}
		if ($spec->match($best, $base)) {
			return $best;
		}
	}
	return undef;
}

package OpenBSD::LibSpec::GT;
our @ISA = qw(OpenBSD::LibSpec);
sub no_match_major
{
	my ($spec, $library) = @_;
	return $spec->major > $library->major;
}

sub to_string
{
	my $self = shift;
	return $self->key.">=".$self->major.".".$self->minor;

}


package OpenBSD::LibSpec::Build;
our @ISA = qw(OpenBSD::LibSpec);

sub new_from_string
{
	my ($class, $string) = @_;

	$string =~ s/\.$//;
	if (my ($stem, $strict, $major, $minor) = $string =~ m/^(.*?)(\>?)\=(\d+)\.(\d+)$/o) {
		return $class->new_object($stem, $strict, $major, $minor);
	} elsif (($stem, $strict, $major) = $string =~ m/^(.*?)(\>?)\=(\d+)$/o) {
		return $class->new_object($stem, $strict, $major, 0);
	} else {
		return $class->new_object($string, '>', 0, 0);
	}
}

sub new_object
{
	my ($class, $stem, $strict, $major, $minor) = @_;
	my $n = $strict eq '' ? "OpenBSD::LibSpec" : "OpenBSD::LibSpec::GT";
	return $n->new_with_stem($stem, $major, $minor);
}

1;