Blob Blame History Raw
From fe315e15b2cba288a16f07a146e085216b2bcc1c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
Date: Mon, 30 Mar 2015 15:20:33 +0200
Subject: [PATCH] Use system libbson library if possible
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Petr Písař <ppisar@redhat.com>
---
 inc/Module/Install/PRIVATE/Mongo.pm | 86 ++++++++++++++++++++++++++++---------
 1 file changed, 66 insertions(+), 20 deletions(-)

diff --git a/inc/Module/Install/PRIVATE/Mongo.pm b/inc/Module/Install/PRIVATE/Mongo.pm
index 8709ddd..8f41076 100644
--- a/inc/Module/Install/PRIVATE/Mongo.pm
+++ b/inc/Module/Install/PRIVATE/Mongo.pm
@@ -60,6 +60,8 @@ END
 sub mongo {
     my ($self, @mongo_vars) = @_;
 
+    my $inc = '-I.';
+
     my $ccflags = $self->makemaker_args->{CCFLAGS} || $Config{ccflags};
     $ccflags = "" unless defined $ccflags;
 
@@ -70,6 +72,9 @@ sub mongo {
     $ccflags .= " -D_GNU_SOURCE"
         if HAS_GCC && $ccflags !~ /-D_GNU_SOURCE/;
 
+    my $ldflags = $self->makemaker_args->{LDFLAGS};
+    $ldflags = "" unless defined $ldflags;
+
     # MSWin32 requires newer gcc (if using gcc)
     if ( $^O eq 'MSWin32' ) {
         check_for_outdated_win_gcc;
@@ -97,36 +102,49 @@ sub mongo {
         }
     }
 
-    # needed to compile bson library
-    $ccflags .= " -DBSON_COMPILATION ";
-
-    my $conf = $self->configure_bson;
-
-    if ($conf->{BSON_WITH_OID32_PT} || $conf->{BSON_WITH_OID64_PT}) {
-        my $pthread = $^O eq 'solaris' ? " -pthreads " : " -pthread ";
-        $ccflags .= $pthread;
-        my $ldflags = $self->makemaker_args->{LDFLAGS};
-        $ldflags = "" unless defined $ldflags;
-        $self->makemaker_args( LDFLAGS => "$ldflags $pthread" );
-    }
+    # Use system libbson if possible
+    my $conf = $self->check_system_bson;
+    my $bson_compilation = 1;
+    if ($conf->{found}) {
+        $bson_compilation = 0;
+        # I assume $conf->{cflags} contains -I only recognized by
+        # EU::MM's INC. Otherwise we should use ExtUtils::PkgConfig directly.
+        $inc .= ' ' . $conf->{cflags};
+        # I assume $conf->{ldflags} contains -L and -l only recognized by
+        # EU::MM's LIBS. Otherwise we should use ExtUtils::PkgConfig directly.
+        $self->add_libs($conf->{ldflags});
+    } else {
+        # needed to compile bson library
+        $inc .= ' -Ibson';
+        $ccflags .= " -DBSON_COMPILATION ";
+
+        $conf = $self->configure_bson;
+
+        if ($conf->{BSON_WITH_OID32_PT} || $conf->{BSON_WITH_OID64_PT}) {
+            my $pthread = $^O eq 'solaris' ? " -pthreads " : " -pthread ";
+            $ccflags .= $pthread;
+            $ldflags .= $pthread;
+        }
 
-    if ( $conf->{BSON_HAVE_CLOCK_GETTIME} ) {
-        my $libs = $self->makemaker_args->{LIBS};
-        $libs = "" unless defined $libs;
-        $self->makemaker_args( LIBS => "$libs -lrt" );
+        if ( $conf->{BSON_HAVE_CLOCK_GETTIME} ) {
+            $self->add_libs("-lrt");
+        }
     }
 
     $self->makemaker_args( CCFLAGS => $ccflags );
 
-    $self->xs_files;
+    $self->xs_files($bson_compilation);
+
+    $self->makemaker_args( INC   => $inc );
 
-    $self->makemaker_args( INC   => '-I. -Ibson' );
+    # I think EU::MM ignores LDFLAGS
+    $self->makemaker_args( LDFLAGS => $ldflags );
 
     return;
 }
 
 sub xs_files {
-    my ($self) = @_;
+    my ($self, $bson_compilation) = @_;
     my (@clean, @OBJECT, %XS);
 
     for my $xs (<xs/*.xs>) {
@@ -138,7 +156,7 @@ sub xs_files {
         push @clean, $o;
     }
 
-    for my $c (<*.c>, <bson/*.c>) {
+    for my $c (<*.c>, ($bson_compilation ? (<bson/*.c>) : ())) {
         (my $o = $c) =~ s/\.c$/\$(OBJ_EXT)/i;
 
         push @OBJECT, $o;
@@ -165,6 +183,34 @@ HERE
     return;
 }
 
+sub add_libs {
+    my ($self, $new_libs) = @_;
+
+    my $libs = $self->makemaker_args->{LIBS};
+    $libs = [""] unless defined $libs;
+    for my $part ( @$libs) {
+      $part .= (length($part)  ? " " : "") . "$new_libs";
+    }
+    $self->makemaker_args->{LIBS} = $libs;;
+}
+
+sub check_system_bson {
+    my ($self) = @_;
+    my %conf;
+
+    my $ca = Config::AutoConf->new;
+    $ca->push_lang("C");
+    if ($ca->pkg_config_package_flags('libbson-1.0')) {
+        $conf{found} = 1;
+        $conf{cflags} = $ca->_get_extra_compiler_flags;
+        $conf{ldflags} = $ca->_get_extra_linker_flags;
+    } else {
+        $conf{found} = 0;
+    }
+
+    return \%conf;
+}
+
 # Quick and dirty autoconf substitute
 sub configure_bson {
     my ($self) = @_;
-- 
2.5.0