Skip to content

Commit

Permalink
make Makefile.PL a bit more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
mgregoro committed Jun 12, 2018
1 parent 5f1c625 commit 90be0dd
Showing 1 changed file with 70 additions and 10 deletions.
80 changes: 70 additions & 10 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use 5.008000;
use ExtUtils::MakeMaker;
use List::Util qw/uniq/;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.

Expand All @@ -18,30 +19,89 @@ my %config = (
($] >= 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 => ['-lsodium'], # e.g., '-lm'
LIBS => [], # e.g., '-lm'
DEFINE => '-std=gnu99 -Wno-pointer-sign', # e.g., '-DHAVE_SOMETHING'
INC => '-I.', # e.g., '-I. -I/usr/include/other'
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
);

my $i = 0;
if (scalar(keys %$c) > 0) {
$c->{includedir} && $i++ and $config{INC} = "-I. -I$c->{includedir}";
$c->{libdir} && $i++ and $config{LIBS} = "-L$c->{libdir} -lsodium";
# pkg-config situation works out to a 4
if ($c->{modversion}) {
$i++;
$i = 4;
my ($major, $minor, $revision) = split(/\./, $c->{modversion});
$config{DEFINE} .= " -DP5CS_LIBMAJ=$major -DP5CS_LIBMIN=$minor -DP5CS_LIBREV=$revision -DP5CS_LIBVER=" . '\"' . $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/$newest/include/sodium.h") {
$i += 3;
$c->{libdir} = "-lsodium -L/usr/local/Cellar/libsodium/$newest/lib";
$c->{includedir} = "/usr/local/Cellar/libsodium/$newest/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} . '\"';
}
} 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));
}

$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{DEFINE} .= " -DP5CS_LIBMAJ=1 -DP5CS_LIBMIN=0 -DP5CS_LIBREV=8 -DP5CS_LIBVER=" . '\"1.0.8\"';
$config{LIBS}->[0] = "-lsodium";
}

if ($c->{includedir}) {
$config{INC} = "-I. -I$c->{includedir}";
} else {
$config{INC} = "-I.";
}

WriteMakefile(%config);

if ($i == 3) {
print "\n[ :D ] Found libsodium $c->{modversion} in $c->{libdir}; headers in $c->{includedir}\n\n";
if ($i >= 3) {
print "\n[ :D ] Found libsodium $c->{modversion} in $c->{libdir}; headers in $c->{includedir}";
if ($i == 3) {
print " .. using unconventional means.";
} elsif ($i == 4) {
print " .. using pkg-config.";
}
print "\n";
print "Please run 'make' and 'make test' to build Crypt::Sodium\n\n";
} elsif ($i == 2) {
print "\n[ :| ] Found Alien::Sodium -- Assuming it provides libsodium v1.0.8\n";
print "Please run 'make' and 'make test' to build Crypt::Sodium\n\n";
} else {
print "\n[ :| ] Couldn't find pkg-config or pkg-config info for libsodium... assuming minimum v1.0.8\n\n";
}
print "\n[ :| ] Couldn't find pkg-config or pkg-config info for libsodium... assuming minimum v1.0.8\n";
if ($c->{includedir}) {
print "Please run 'make' and 'make test' to build Crypt::Sodium\n\n";
} 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";
}
}

0 comments on commit 90be0dd

Please sign in to comment.