Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makefile.PL: optionally use Alien::librdkafka #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 100 additions & 8 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,109 @@ use warnings;
use Config;
use ExtUtils::MakeMaker;
use ExtUtils::PkgConfig;
use Text::ParseWords qw(shellwords);

use constant LIBRDKAFKA_MIN_VERSION => "1.0.0";

# Fix for OSX:
$ENV{PKG_CONFIG_PATH} .= ':/usr/local/opt/openssl/lib/pkgconfig/'
if $^O eq 'darwin';
# rdkafka.pc has libcrypto as a required dependency, so
# if libcrypto can't be found, pkg-config won't return
# values for rdkafka.
# Problem: in OSX, the default libcrypto lives in a non-default
# location, meaning that without some tweaking like above,
# we never get values for rdkafka.

sub prepare_lddlflags {
my ($lddlflags) = @_;
# `-Wl,-z,now` breaks static linking to librdkafka.a, and
# the perl you get from pacman in ArchLinux has that by default
# in lddlflags.
# Similar with --as-needed
$lddlflags =~ s<,-z[,= ]now><>g;
$lddlflags =~ s<,--as-needed\b><>g;
$lddlflags =~ s<-Wl\s>< >g; # did we remove every linker option?
return $lddlflags;
}

my @archive_after;
my $makemaker_libs = [];
my $makemaker_inc = '';

my $ccflags = $Config{'ccflags'} // '';
my $lddlflags = prepare_lddlflags($Config{'lddlflags'} // '');

# If you have a local librdkafka installation and you want to use that instead
# of the system-wide, do something like:
# PKG_CONFIG_PATH=/usr/local/lib/pkgconfig perl Makefile.PL
# or whatever is the actual installation path
my %rdkafka = ExtUtils::PkgConfig->find('rdkafka');
eval {
my %rdkafka = ExtUtils::PkgConfig->find('rdkafka');
$makemaker_libs = '-lpthread -lrdkafka';
$lddlflags .= ' ' . $rdkafka{libs};
$makemaker_inc = $rdkafka{cflags};
warn sprintf(
"WARNING: Installed librdkafka version %s is lower than tested %s",
$rdkafka{modversion},
LIBRDKAFKA_MIN_VERSION
) if $rdkafka{modversion} lt LIBRDKAFKA_MIN_VERSION;

print "Compiling Net::Kafka with librdkafka $rdkafka{modversion}\n\n";

1;
} or do {
my $pkg_config_error = $@ || 'Zombie error';

warn sprintf(
"WARNING: Installed librdkafka version %s is lower than tested %s",
$rdkafka{modversion},
LIBRDKAFKA_MIN_VERSION
) if $rdkafka{modversion} lt LIBRDKAFKA_MIN_VERSION;
eval {
require Alien::librdkafka;
$makemaker_inc .= ' ' . Alien::librdkafka->cflags;
my $libs = Alien::librdkafka->install_type eq 'share'
? Alien::librdkafka->libs_static || Alien::librdkafka->libs
: Alien::librdkafka->libs
;
# cmake/OSX bug: -lz gets transformed into
# -l/abs/path/to/libz.tbd
# in systems with Apple's "built-in dynamic linker cache"
$libs =~ s<\B-l/></>g;

my $lib_ext = $Config{lib_ext};
foreach my $maybe_lib ( shellwords($libs) ) {
if ( $maybe_lib =~ m/\B-l(\S+)/ ) {
# -lfoo
push @$makemaker_libs, $maybe_lib;
}
elsif ( $maybe_lib =~ m<-L(\S+)> ) {
my $lib_dir = $1;
# /path/to/libs
# add '-Wl,-rpath=$dir'
$libs = '-Wl,-rpath,' . $lib_dir . ' ' . $libs;
}
elsif ( $maybe_lib =~ m<^/.+$lib_ext$> ) {
# .a file in pkg-config --libs output -- this needs to come after the -o
push @archive_after, $maybe_lib;
}
}

$lddlflags .= ' ' . $libs;

1;
} or do {
my $alien_librdkafka_error = $@ || 'Zombie error';
die "pkg-config nor Alien::librdkafka could find kafka; bailing out!\n"
."PkgConfig error: $pkg_config_error\n"
."Alien::librdkafka error: $alien_librdkafka_error\n"
;
};
};

print "Compiling Net::Kafka with librdkafka $rdkafka{modversion}\n\n";

WriteMakefile(
INC => $makemaker_inc,
LIBS => $makemaker_libs,
LDDLFLAGS => $lddlflags,
CCFLAGS => $ccflags,
PERL_ARCHIVE_AFTER => join(' ', @archive_after),
NAME => 'Net::Kafka',
DISTNAME => 'Net-Kafka',
AUTHOR => [
Expand All @@ -40,7 +125,6 @@ WriteMakefile(
LICENSE => 'perl',
VERSION_FROM => 'lib/Net/Kafka.pm',
ABSTRACT_FROM => 'lib/Net/Kafka.pm',
LIBS => [ "-lrdkafka -lpthread" ],
OBJECT => '$(O_FILES)',
TYPEMAPS => ['typemap'],
PREREQ_PM => {
Expand All @@ -50,6 +134,14 @@ WriteMakefile(
},
META_MERGE => {
"meta-spec" => { version => 2 },
prereqs => {
configure => {
# optionally use Alien::librdkafka
recommends => {
'Alien::librdkafka' => '0.01',
},
},
},
resources => {
homepage => 'https://github.com/bookingcom/perl-Net-Kafka',
bugtracker => {
Expand Down