-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMakefile.PL
More file actions
214 lines (191 loc) · 7.68 KB
/
Makefile.PL
File metadata and controls
214 lines (191 loc) · 7.68 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use 5.008000;
use ExtUtils::MakeMaker;
use List::Util qw/uniq/;
use Cwd qw/abs_path/;
use File::Basename qw/dirname/;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
my $verbose = defined $ARGV[0] && $ARGV[0] eq "verbose";
my $c = {};
chomp(my $pc = `which pkg-config`);
if ($pc) {
chomp( $c->{includedir} = `$pc --variable includedir libsodium` );
chomp( $c->{libdir} = `$pc --variable libdir libsodium` );
chomp( $c->{modversion} = `$pc --modversion libsodium` );
}
my %config = (
NAME => 'Crypt::Sodium',
VERSION_FROM => 'lib/Crypt/Sodium.pm', # finds $VERSION
PREREQ_PM => {}, # e.g., Module::Name => 1.1
($] >= 5.005 ? ## Add these new keywords supported since 5.005
(ABSTRACT_FROM => 'lib/Crypt/Sodium.pm', # retrieve abstract from module
AUTHOR => 'Michael Gregorowicz <mike@mg2.org>') : ()),
LIBS => [], # e.g., '-lm'
DEFINE => '-std=gnu99 -Wno-pointer-sign', # e.g., '-DHAVE_SOMETHING'
INC => '', # e.g., '-I. -I/usr/include/other'
# Un-comment this if you add C files to link with later:
# OBJECT => '$(O_FILES)', # link all the C files too
clean => {
# this file is created before building, and should be installed.. but cleaned when 'make clean' is run.
FILES => 'lib/Crypt/Sodium/BuildVersion.pm',
},
);
my $i = 0;
if (scalar(keys %$c) > 0) {
# pkg-config situation works out to a 4
if ($c->{modversion}) {
$i = 4;
my ($major, $minor, $revision) = split(/\./, $c->{modversion});
$config{DEFINE} .= " -DP5CS_LIBMAJ=$major -DP5CS_LIBMIN=$minor -DP5CS_LIBREV=$revision -DP5CS_LIBVER=" . '\"' . $c->{modversion} . '\"';
write_ver_file($c->{modversion});
}
if ($c->{libdir}) {
$c->{libdir} = "-lsodium -L$c->{libdir}";
}
} else {
# if this file exists, let's make some assumptions
if (-f "/usr/local/include/sodium.h") {
$c->{includedir} = "/usr/local/include";
$c->{libdir} = "-lsodium -L/usr/local/lib";
}
# let's see if we can find a better (admittedly self-serving) bet...
if (-d "/usr/local/Cellar/libsodium") {
# just build against the newest homebrew libsodium this situation should work out to '3'.
$c->{modversion} = (sort {$b <=> $a} `ls /usr/local/Cellar/libsodium`)[0];
if (-f "/usr/local/Cellar/libsodium/$c->{modversion}/include/sodium.h") {
$i += 3;
$c->{libdir} = "-lsodium -L/usr/local/Cellar/libsodium/$c->{modversion}t/lib";
$c->{includedir} = "/usr/local/Cellar/libsodium/$c->{modversion}/include";
my ($major, $minor, $revision) = split(/\./, $c->{modversion});
# far fetched but..
$config{DEFINE} .= " -DP5CS_LIBMAJ=$major -DP5CS_LIBMIN=$minor -DP5CS_LIBREV=$revision -DP5CS_LIBVER=" . '\"' . $c->{modversion} . '\"';
write_ver_file($c->{modversion});
}
} else {
# Alien::Sodium (as of Jun 11 2018) supplies 1.0.8 anyway, so if we find it, just make sure we have the
# environment set up to use it if nothing better was found. $i should work out to 2.
eval "use Alien::Sodium;";
unless ($@) {
$i = 2;
$c->{libdir} = join(' ', uniq(split(/ /, Alien::Sodium->libs)));
$c->{includedir} = join(' ', uniq(split/ /, Alien::Sodium->cflags));
}
$c->{modversion} = '1.0.8';
write_ver_file();
$config{DEFINE} .= " -DP5CS_LIBMAJ=1 -DP5CS_LIBMIN=0 -DP5CS_LIBREV=8 -DP5CS_LIBVER=" . '\"1.0.8\"';
}
}
if ($c->{libdir}) {
$config{LIBS}->[0] = $c->{libdir};
} else {
$config{LIBS}->[0] = "-lsodium";
}
if ($c->{includedir}) {
$config{INC} = "-I. -I$c->{includedir}";
} else {
$config{INC} = "-I.";
}
$config{DEFINE} .= " " . extra_define_flags($c->{modversion});
print "\n" if $verbose;
WriteMakefile(%config);
if ($i >= 3) {
print "\n[ :D ] Found libsodium $c->{modversion} in $c->{libdir}" if $verbose;
if ($i == 3) {
print " .. using unconventional means." if $verbose;
} elsif ($i == 4) {
print " .. using pkg-config." if $verbose;
}
print "\n" if $verbose;
print "Please run 'make' and 'make test' to build Crypt::Sodium\n\n" if $verbose;
} elsif ($i == 2) {
print "\n[ :| ] Found Alien::Sodium -- Assuming it provides libsodium v1.0.8\n" if $verbose;
print "Please run 'make' and 'make test' to build Crypt::Sodium\n\n" if $verbose;
} else {
print "\n[ :| ] Couldn't find pkg-config or pkg-config info for libsodium... assuming minimum v1.0.8\n" if $verbose;
if ($c->{includedir}) {
print "Please run 'make' and 'make test' to build Crypt::Sodium\n\n" if $verbose;
} else {
print "If you're sure you have libsodium 1.0.8 or higher installed, please run 'make' and 'make test' to build Crypt::Sodium\n\n" if $verbose;
}
}
sub extra_define_flags {
my ($lsv) = @_;
my @edefs;
# version 1.0.9
# introduced crypto_pwhash_argon2i
push(@edefs, "-D" . enable_feature(
'1.0.9',
P5CS_CPWH => "crypto_pwhash namespace introduced for password hashing functionality",
));
# introduced "speed record" BLAKE2b and state
push(@edefs, "-D" . enable_feature(
'1.0.9',
P5CS_GH_BLAKE2B => "crypto_generichash_blake2bxx functionality",
));
# introduced "speed record" BLAKE2b and state
push(@edefs, "-D" . enable_feature(
'1.0.9',
P5CS_GH_BLAKE2B => "crypto_generichash_blake2bxx functionality",
));
# version 1.0.12
# introduced multiple _keygen() variants for the different algorithms
push(@edefs, "-D" . enable_feature(
'1.0.12',
P5CS_KG_VAR => "xx_keygen and xx_keypair variants present for various algorithms",
));
# introduced ed25519ph; multi-part signature API
push(@edefs, "-D" . enable_feature(
'1.0.12',
P5CS_ED25519PH => "Ed25519ph multi-part signature algorithm functionality",
));
# crypto_kdf present in 1.0.12
push(@edefs, "-D" . enable_feature(
'1.0.12',
P5CS_CKDF => "crypto_kdf namespace available for key derivation functionality",
));
# randombytes_buf_deterministic in 1.0.12
push(@edefs, "-D" . enable_feature(
'1.0.12',
P5CS_DRNG => "randombytes_buf_deterministic psuedo random number generator",
));
return join(' ', @edefs);
}
sub enable_feature {
my ($reqver, $def, $desc) = @_;
my $enable;
if (vercmp($c->{modversion}, $reqver)) {
print "[ +++ ] feature $def - ENABLED" if $verbose;
$enable = 1;
} else {
print "[ --- ] feature $def - DSABLED" if $verbose;
}
if ($desc) {
print " - $desc" if $verbose;
}
print " - req v$reqver or higher; have v$c->{modversion}\n" if $verbose;
return $enable ? $def : undef;
}
sub vercmp {
my ($ay, $be) = map { join('', map { $_ * 100 } split(/\./, $_)) } @_;
if ($ay > $be) {
return 1;
}
return undef;
}
sub write_ver_file {
my ($lsv) = @_;
$lsv //= '1.0.8';
my ($major, $minor, $revision) = split(/\./, $lsv);
my $dist_path = dirname(abs_path(__FILE__));
if (-d "$dist_path/lib/Crypt/Sodium") {
# we're in the right place.
open my $fh, '>', "$dist_path/lib/Crypt/Sodium/BuildVersion.pm";
print $fh "package Crypt::Sodium::BuildVersion;\n";
print $fh 'our $VERSION = ' . "'v$lsv';\n";
print $fh 'our $MAJOR = ' . $major . ";\n";
print $fh 'our $MINOR = ' . $minor . ";\n";
print $fh 'our $REVISION = ' . $revision . ";\n";
print $fh "1;\n";
close $fh;
}
}