Alien-Base-Wrapper

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

     );

DESCRIPTION

    This module acts as a wrapper around one or more Alien modules. It is
    designed to work with Alien::Base based aliens, but it should work with
    any Alien which uses the same essential API.

    In the first example (from the command line), this class acts as a
    wrapper around the compiler and linker that Perl is configured to use.
    It takes the normal compiler and linker flags and adds the flags
    provided by the Aliens specified, and then executes the command. It
    will print the command to the console so that you can see exactly what
    is happening.

    In the second example (from Makefile.PL non-dynamic), this class is
    used to generate the appropriate ExtUtils::MakeMaker (EUMM) arguments
    needed to WriteMakefile.

    In the third example (from Makefile.PL dynamic), we do a quick check to
    see if the simple linker flag -lfoo will work, if so we use that. If
    not, we use a wrapper around the compiler and linker that will use the
    alien flags that are known at build time. The problem that this form
    attempts to solve is that compiler and linker flags typically need to
    be determined at configure time, when a distribution is installed,
    meaning if you are going to use an Alien module then it needs to be a
    configure prerequisite, even if the library is already installed and
    easily detected on the operating system.

    The author of this module believes that the third (from Makefile.PL
    dynamic) form is somewhat unnecessary. Alien modules based on
    Alien::Base have a few prerequisites, but they are well maintained and
    reliable, so while there is a small cost in terms of extra
    dependencies, the overall reliability thanks to reduced overall
    complexity.

FUNCTIONS

 cc

     % perl -MAlien::Base::Wrapper=Alien::Foo -e cc -- cflags

    Invoke the C compiler with the appropriate flags from Alien::Foo and
    what is provided on the command line.

 ld

     % perl -MAlien::Base::Wrapper=Alien::Foo -e ld -- ldflags

    Invoke the linker with the appropriate flags from Alien::Foo and what
    is provided on the command line.

 mm_args

     my %args = Alien::Base::Wrapper->mm_args;

    Returns arguments that you can pass into WriteMakefile to compile/link
    against the specified Aliens.

 mb_args

lib/Alien/Base/Wrapper.pm  view on Meta::CPAN

use strict;
use warnings;
use 5.008001;
use Config;
use Text::ParseWords qw( shellwords );

# ABSTRACT: Compiler and linker wrapper for Alien
our $VERSION = '0.05'; # VERSION


my @cflags_I;
my @cflags_other;
my @ldflags_L;
my @ldflags_l;
my @ldflags_other;
my @mm;
my @mb;

sub _reset
{
  @cflags_I      = ();
  @cflags_other  = ();
  @ldflags_L     = ();
  @ldflags_l     = ();
  @ldflags_other = ();
  @mm            = ();
  @mb            = ();
}


sub cc
{
  my @command = (
    shellwords($Config{cc}),
    @cflags_I,
    @cflags_other,
    @ARGV,
  );
  print "@command\n" unless $ENV{ALIEN_BASE_WRAPPER_QUIET};
  exec @command;
}


sub ld
{
  my @command = (
    shellwords($Config{ld}),
    @ldflags_L,
    @ldflags_other,
    @ARGV,
    @ldflags_l,
  );
  print "@command\n" unless $ENV{ALIEN_BASE_WRAPPER_QUIET};
  exec @command;
}


sub mm_args
{
  @mm;
}

lib/Alien/Base/Wrapper.pm  view on Meta::CPAN


  foreach my $alien (@aliens)
  {
    if($alien eq '!export')
    {
      $export = 0;
    }
    $alien = "Alien::$alien" unless $alien =~ /::/;
    my $alien_pm = $alien . '.pm';
    $alien_pm =~ s/::/\//g;
    require $alien_pm unless eval { $alien->can('cflags') } && eval { $alien->can('libs') };
    my $cflags;
    my $libs;
    if($alien->install_type eq 'share' && $alien->can('cflags_static'))
    {
      $cflags = $alien->cflags_static;
      $libs   = $alien->libs_static;
    }
    else
    {
      $cflags = $alien->cflags;
      $libs   = $alien->libs;
    }
    
    push @cflags_I,     grep  /^-I/, shellwords $cflags;
    push @cflags_other, grep !/^-I/, shellwords $cflags;
    
    push @ldflags_L,     grep  /^-L/,    shellwords $libs;
    push @ldflags_l,     grep  /^-l/,    shellwords $libs;
    push @ldflags_other, grep !/^-[Ll]/, shellwords $libs;
  }
  
  my @cflags_define = grep  /^-D/, @cflags_other;
  my @cflags_other2 = grep !/^-D/, @cflags_other;
  
  @mm = ();

  push @mm, INC       => _join @cflags_I                             if @cflags_I;
  push @mm, CCFLAGS   => _join(@cflags_other2) . " $Config{ccflags}" if @cflags_other2;
  push @mm, DEFINE    => _join(@cflags_define)                       if @cflags_define;

  push @mm, LIBS      => [@ldflags_l];
  my @ldflags = (@ldflags_L, @ldflags_other);
  push @mm, LDDLFLAGS => _join(@ldflags) . " $Config{lddlflags}"     if @ldflags;
  push @mm, LDFLAGS   => _join(@ldflags) . " $Config{ldflags}"       if @ldflags;

  @mb = ();
  
  push @mb, extra_compiler_flags => _join(@cflags_I, @cflags_other);
  push @mb, extra_linker_flags   => _join(@ldflags_l);
  
  if(@ldflags)
  {
    push @mb, config => {
      lddlflags => _join(@ldflags) . " $Config{lddlflags}",
      ldflags   => _join(@ldflags) . " $Config{ldflags}",
    },
  }
  
  if($export)
  {
    my $caller = caller;
    no strict 'refs';
    *{"${caller}::cc"} = \&cc;
    *{"${caller}::ld"} = \&ld;
  }  

lib/Alien/Base/Wrapper.pm  view on Meta::CPAN

 );

=head1 DESCRIPTION

This module acts as a wrapper around one or more L<Alien> modules.  It is designed to work
with L<Alien::Base> based aliens, but it should work with any L<Alien> which uses the same
essential API.

In the first example (from the command line), this class acts as a wrapper around the
compiler and linker that Perl is configured to use.  It takes the normal compiler and
linker flags and adds the flags provided by the Aliens specified, and then executes the
command.  It will print the command to the console so that you can see exactly what is
happening.

In the second example (from Makefile.PL non-dynamic), this class is used to generate the
appropriate L<ExtUtils::MakeMaker> (EUMM) arguments needed to C<WriteMakefile>.

In the third example (from Makefile.PL dynamic), we do a quick check to see if the simple
linker flag C<-lfoo> will work, if so we use that.  If not, we use a wrapper around the
compiler and linker that will use the alien flags that are known at build time.  The
problem that this form attempts to solve is that compiler and linker flags typically
need to be determined at I<configure> time, when a distribution is installed, meaning
if you are going to use an L<Alien> module then it needs to be a configure prerequisite,
even if the library is already installed and easily detected on the operating system.

The author of this module believes that the third (from Makefile.PL dynamic) form is
somewhat unnecessary.  L<Alien> modules based on L<Alien::Base> have a few prerequisites,
but they are well maintained and reliable, so while there is a small cost in terms of extra
dependencies, the overall reliability thanks to reduced overall complexity.

=head1 FUNCTIONS

=head2 cc

 % perl -MAlien::Base::Wrapper=Alien::Foo -e cc -- cflags

Invoke the C compiler with the appropriate flags from C<Alien::Foo> and what
is provided on the command line.

=head2 ld

 % perl -MAlien::Base::Wrapper=Alien::Foo -e ld -- ldflags

Invoke the linker with the appropriate flags from C<Alien::Foo> and what
is provided on the command line.

=head2 mm_args

 my %args = Alien::Base::Wrapper->mm_args;

Returns arguments that you can pass into C<WriteMakefile> to compile/link against
the specified Aliens.

=head2 mb_args

t/alien_base_wrapper.t  view on Meta::CPAN


$ENV{ALIEN_BASE_WRAPPER_QUIET} = 1;

subtest 'export' => sub {

  {
    package
      Alien::Foo1;

    sub install_type { 'share' }    
    sub cflags {}
    sub libs {}

    package
      Alien::Bar1;

    sub install_type { 'share' }    
    sub cflags {}
    sub libs {}
  
    package
      Foo::Bar1;
    use Alien::Base::Wrapper qw( Alien::Foo1 Alien::Bar1 );
  }
  
  ok(
    Foo::Bar1->can('cc'),
    'can cc',

t/alien_base_wrapper.t  view on Meta::CPAN


subtest 'system' => sub {

  Alien::Base::Wrapper::_reset();

  {
    package
      Alien::Foo2;
    
    sub install_type { 'system' }
    sub cflags { '-I/foo/include -DBAR=1' }
    sub cflags_static { 'wrong' }
    sub libs   { '-L/foo/lib -lfoo'   }
    sub libs_static { 'wrong' }
  }
  
  Alien::Base::Wrapper->import('Foo2');
  
  is(
    exec_arrayref {
      local @ARGV = qw( one two three );
      Alien::Base::Wrapper::cc();

t/alien_base_wrapper.t  view on Meta::CPAN


subtest 'share' => sub {

  Alien::Base::Wrapper::_reset();

  {
    package
      Alien::Foo3;
    
    sub install_type { 'share' }
    sub cflags { '-I/foo/include -DBAR=1' }
    sub cflags_static { '-I/foo/include -DBAR=2' }
    sub libs   { '-L/foo/lib -lfoo'   }
    sub libs_static { '-L/foo/lib -lfoo -lbaz' }
  }
  
  Alien::Base::Wrapper->import('Alien::Foo3');

  is(
    exec_arrayref {
      local @ARGV = qw( one two three );
      Alien::Base::Wrapper::cc();

t/alien_base_wrapper.t  view on Meta::CPAN


subtest 'share sans static' => sub {

  Alien::Base::Wrapper::_reset();

  {
    package
      Alien::Foo4;
    
    sub install_type { 'share' }
    sub cflags { '-I/foo/include -DBAR=1' }
    sub libs   { '-L/foo/lib -lfoo'   }
  }
  
  Alien::Base::Wrapper->import('Alien::Foo4');

  is(
    exec_arrayref {
      local @ARGV = qw( one two three );
      Alien::Base::Wrapper::cc();
    },

t/alien_base_wrapper.t  view on Meta::CPAN


subtest 'combine aliens' => sub {

  Alien::Base::Wrapper::_reset();
  
  {
    package
      Alien::Foo5;
      
    sub install_type { 'system' }
    sub cflags { '-I/foo/include -DFOO5=1' }
    sub libs   { '-L/foo/lib --ld-foo -lfoo' }
    
    package
      Alien::Bar5;
    
    sub install_type { 'share' }
    sub cflags { '-I/bar/include -DBAR5=1' }
    sub libs   { '-L/foo/lib --ld-bar -lbar' }
  }

  Alien::Base::Wrapper->import('Alien::Foo5', 'Alien::Bar5');
  
  is(
    exec_arrayref {
      local @ARGV = qw( one two three );
      Alien::Base::Wrapper::cc();
    },

t/alien_base_wrapper.t  view on Meta::CPAN

    my %mb_args = Alien::Base::Wrapper->mb_args;

    if(eval q{ require YAML })
    {
      note YAML::Dump(\%mb_args);
    }
    
    is(
      \%mb_args,
      hash {
        field extra_compiler_flags => '-I/foo/include -I/bar/include -DFOO5=1 -DBAR5=1';
        field extra_linker_flags   => '-lfoo -lbar';
        field config => hash {
          field lddlflags => T();
          field ldflags   => T();
        };
      },
    );
  

  };

};

done_testing;



( run in 1.424 second using v1.01-cache-2.11-cpan-94b05bcf43c )