1# ex:ts=8 sw=4:
2# $OpenBSD: Build.pm,v 1.8 2023/06/13 09:07:18 espie Exp $
3#
4# Copyright (c) 2010 Marc Espie <espie@openbsd.org>
5#
6# Permission to use, copy, modify, and distribute this software for any
7# purpose with or without fee is hereby granted, provided that the above
8# copyright notice and this permission notice appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17#
18use v5.36;
19
20# the specs used during build are slightly different from the specs at
21# runtime.
22package OpenBSD::Library::Static;
23our @ISA = qw(OpenBSD::Library);
24sub new($class, $dir, $stem)
25{
26	bless {dir => $dir, stem => $stem}, $class;
27}
28
29sub no_match_dispatch($library, $spec, $base)
30{
31	return $spec->no_match_static($library, $base);
32}
33
34sub to_string($self)
35{
36	return "$self->{dir}/lib$self->{stem}.a";
37}
38
39sub version($) { ".a" }
40
41sub is_static($) { 1 }
42
43sub is_better($, $) { 0 }
44
45package OpenBSD::Library::Build;
46our @ISA = qw(OpenBSD::Library);
47
48sub static($)
49{ 'OpenBSD::Library::Static'; }
50
51sub from_string($class, $filename)
52{
53	if (my ($dir, $stem) = $filename =~ m/^(.*)\/lib([^\/]+)\.a$/o) {
54		return $class->static->new($dir, $stem);
55	} else {
56		return $class->SUPER::from_string($filename);
57	}
58}
59
60package OpenBSD::LibSpec;
61sub no_match_static	# forwarder
62{
63	&OpenBSD::LibSpec::no_match_name;
64}
65
66sub findbest($spec, $repo, $base)
67{
68	my $spec2 = OpenBSD::LibSpec::GT->new($spec->{dir}, $spec->{stem},
69	    0, 0);
70	my $r = $spec2->lookup($repo, $base);
71	my $best;
72	for my $candidate (@$r) {
73		if (!defined $best || $candidate->is_better($best)) {
74			$best = $candidate;
75		}
76	}
77	if (defined $best) {
78		if ($best->is_static) {
79			return $best;
80		}
81		if ($spec->match($best, $base)) {
82			return $best;
83		}
84	}
85	return undef;
86}
87
88package OpenBSD::LibSpec::GT;
89our @ISA = qw(OpenBSD::LibSpec);
90sub no_match_major($spec, $library)
91{
92	return $spec->major > $library->major;
93}
94
95sub to_string($self)
96{
97	return $self->key.">=".$self->major.".".$self->minor;
98
99}
100
101
102package OpenBSD::LibSpec::Build;
103our @ISA = qw(OpenBSD::LibSpec);
104
105sub new_from_string($class, $string)
106{
107	$string =~ s/\.$//;
108	if (my ($stem, $strict, $major, $minor) = $string =~ m/^(.*?)(\>?)\=(\d+)\.(\d+)$/o) {
109		return $class->new_object($stem, $strict, $major, $minor);
110	} elsif (($stem, $strict, $major) = $string =~ m/^(.*?)(\>?)\=(\d+)$/o) {
111		return $class->new_object($stem, $strict, $major, 0);
112	} else {
113		return $class->new_object($string, '>', 0, 0);
114	}
115}
116
117sub new_object($class, $stem, $strict, $major, $minor)
118{
119	my $n = $strict eq '' ? "OpenBSD::LibSpec" : "OpenBSD::LibSpec::GT";
120	return $n->new_with_stem($stem, $major, $minor);
121}
122
1231;
124