1# ex:ts=8 sw=4:
2# $OpenBSD: Build.pm,v 1.7 2010/11/27 11:55:14 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 strict;
19use warnings;
20
21# the specs used during build are slightly different from the specs at
22# runtime.
23package OpenBSD::Library::Static;
24our @ISA = qw(OpenBSD::Library);
25sub new
26{
27	my ($class, $dir, $stem) = @_;
28	bless {dir => $dir, stem => $stem}, $class;
29}
30
31sub no_match_dispatch
32{
33	my ($library, $spec, $base) = @_;
34	return $spec->no_match_static($library, $base);
35}
36
37sub to_string
38{
39	my $self = shift;
40	return "$self->{dir}/lib$self->{stem}.a";
41}
42
43sub version { ".a" }
44
45sub is_static { 1 }
46
47sub is_better { 0 }
48
49package OpenBSD::Library::Build;
50our @ISA = qw(OpenBSD::Library);
51
52sub static
53{ 'OpenBSD::Library::Static'; }
54
55sub from_string
56{
57	my ($class, $filename) = @_;
58	if (my ($dir, $stem) = $filename =~ m/^(.*)\/lib([^\/]+)\.a$/o) {
59		return $class->static->new($dir, $stem);
60	} else {
61		return $class->SUPER::from_string($filename);
62	}
63}
64
65package OpenBSD::LibSpec;
66sub no_match_static
67{
68	&OpenBSD::LibSpec::no_match_name;
69}
70
71sub findbest
72{
73	my ($spec, $repo, $base) = @_;
74	my $spec2 = OpenBSD::LibSpec::GT->new($spec->{dir}, $spec->{stem},
75	    0, 0);
76	my $r = $spec2->lookup($repo, $base);
77	my $best;
78	for my $candidate (@$r) {
79		if (!defined $best || $candidate->is_better($best)) {
80			$best = $candidate;
81		}
82	}
83	if (defined $best) {
84		if ($best->is_static) {
85			return $best;
86		}
87		if ($spec->match($best, $base)) {
88			return $best;
89		}
90	}
91	return undef;
92}
93
94package OpenBSD::LibSpec::GT;
95our @ISA = qw(OpenBSD::LibSpec);
96sub no_match_major
97{
98	my ($spec, $library) = @_;
99	return $spec->major > $library->major;
100}
101
102sub to_string
103{
104	my $self = shift;
105	return $self->key.">=".$self->major.".".$self->minor;
106
107}
108
109
110package OpenBSD::LibSpec::Build;
111our @ISA = qw(OpenBSD::LibSpec);
112
113sub new_from_string
114{
115	my ($class, $string) = @_;
116
117	$string =~ s/\.$//;
118	if (my ($stem, $strict, $major, $minor) = $string =~ m/^(.*?)(\>?)\=(\d+)\.(\d+)$/o) {
119		return $class->new_object($stem, $strict, $major, $minor);
120	} elsif (($stem, $strict, $major) = $string =~ m/^(.*?)(\>?)\=(\d+)$/o) {
121		return $class->new_object($stem, $strict, $major, 0);
122	} else {
123		return $class->new_object($string, '>', 0, 0);
124	}
125}
126
127sub new_object
128{
129	my ($class, $stem, $strict, $major, $minor) = @_;
130	my $n = $strict eq '' ? "OpenBSD::LibSpec" : "OpenBSD::LibSpec::GT";
131	return $n->new_with_stem($stem, $major, $minor);
132}
133
1341;
135