Alien-ROOT

 view release on metacpan or  search on metacpan

lib/Alien/ROOT.pm  view on Meta::CPAN

package Alien::ROOT;
use 5.008;
use strict;
use warnings;
use Carp ();

=head1 NAME

Alien::ROOT - Utility package to install and locate CERN's ROOT library

=cut

our $VERSION = '5.34.36.1';
$VERSION = eval $VERSION;

=head1 SYNOPSIS

  use Alien::ROOT;

  my $aroot = Alien::ROOT->new;

=head1 DESCRIPTION

Installs or detects CERN's ROOT library.

This version of C<Alien::ROOT> will download and install
C<ROOT v5.30.0> B<if necessary>. If an existing (and
compatible) installation of ROOT was detected, the
module will not download/compile/install a new version
of ROOT.

=head1 METHODS

=head2 Alien::ROOT->new

Creates a new C<Alien::ROOT> object, which essentially just has a few
convenience methods providing useful information like the path
to the ROOT installation (C<ROOTSYS> environment variable)
and the path to the F<root-config> utility.

=cut

sub new {
  my $class = shift;

  Carp::croak('You must call this as a class method') if ref($class);

  my $self = {
    installed    => 0,
    root_config  => undef,
    version      => undef,
    cflags       => undef,
    ldflags      => undef,
    features     => undef,
    libdir       => undef,
    bindir       => undef,
    incdir       => undef,
    etcdir       => undef,
    archdir      => undef, # internal
    private_root => undef,
  };

  bless($self, $class);

  $self->_load_modules();
  $self->_configure();

  return $self;
}

sub _load_modules {
  require File::Spec;
  require Config;
  require ExtUtils::MakeMaker;
  require IPC::Open3;
}

=head2 $aroot->installed

Determine if a valid installation of ROOT has been detected in the system.
This method will return a true value if it is, or undef otherwise.

Example code:

  print "okay\n" if $aroot->installed;

=cut

sub installed {
  my $self = shift;

  Carp::croak('You must call this method as an object') unless ref($self);

  return $self->{installed};
}

=head2 $aroot->run

Sets up the ROOT environment (see C<setup_environment>) and then invokes
the ROOT shell by simply calling C<root>.

=cut

sub run {
  my $self = shift;
  $self->setup_environment;
  system {'root'} 'root', @_;
}

=head2 $aroot->setup_environment

Sets up the C<PATH> and C<LD_LIBRARY_PATH> environment
variables to point at the correct paths for ROOT.

lib/Alien/ROOT.pm  view on Meta::CPAN


=cut

sub setup_environment {
  my $self = shift;
  Carp::croak('You must call this method as an object') unless ref($self);
  die "ROOT was not found. Make the 'root-config' utility accessible or set the ROOTSYS variable"
    if not $self->installed;
 
  my $bindir = $self->bindir;
  my $libdir = $self->libdir;

  if ($^O =~ /win32/i) {
    $ENV{PATH} = $self->_add_to_path($ENV{PATH}, $bindir, $libdir);
  }
  else {
    $ENV{PATH} = $self->_add_to_path($ENV{PATH}, $bindir);
  }
  $ENV{LD_LIBRARY_PATH} = $self->_add_to_path($ENV{LD_LIBRARY_PATH}, $libdir);
  #require DynaLoader;
  #unshift @DynaLoader::dl_library_path, $libdir; # doesn't help
}

sub _add_to_path {
  my $self = shift;
  my $string = shift;
  my @paths = @_;

  my $sep = $Config::Config{path_sep};
  my @split = (defined($string) ? split /\Q$sep\E/, $string : ());

  my %exists;
  $exists{$_}++ for @split;

  foreach my $path (@paths) {
    unshift @split, $path if not exists $exists{$path};
  }
  return join $sep, @split;
}


=head2 $aroot->version

Determine the installed version of ROOT, as a string.

Example code:

  my $version = $aroot->version;

=cut

sub version {
  my ($self) = @_;

  Carp::croak('You must call this method as an object') unless ref($self);

  return $self->_config_get_one_line_param('version', '--version');
}


=head2 $aroot->ldflags

=head2 $aroot->linker_flags

This returns the flags required to link C code with the local installation of
ROOT.

Example code:

  my $ldflags = $aroot->ldflags;

=cut

sub ldflags {
  my $self = shift;

  Carp::croak('You must call this method as an object') unless ref($self);

  return $self->_config_get_one_line_param('ldflags', qw(--ldflags --glibs --auxlibs));
}

# Glob to create an alias to ldflags
*linker_flags = *ldflags;


=head2 $aroot->cflags

=head2 $aroot->compiler_flags

This method returns the compiler option flags to compile C++ code which uses
the ROOT library (typically in the CFLAGS variable).

Example code:

  my $cflags = $aroot->cflags;

=cut

sub cflags {
  my $self = shift;

  Carp::croak('You must call this method as an object') unless ref($self);

  return $self->_config_get_one_line_param('cflags', qw(--cflags --auxcflags));
}
*compiler_flags = *cflags;


=head2 $aroot->features

This method returns a string of ROOT features that were enabled when ROOT
was compiled.

Example code:

  my $features = $aroot->features;
  if ($features !~ /\bexplicitlink\b/) {
    warn "ROOT was built without the --explicitlink option";
  }

=cut

sub features {
  my $self = shift;

  Carp::croak('You must call this method as an object') unless ref($self);

  return $self->_config_get_one_line_param('features', qw(--features));
}


=head2 $aroot->bindir

This method returns the path to the executable directory of ROOT.

Example code:

  my $dir = $aroot->bindir;
  system(File::Spec->catfile($dir, 'root'));

=cut

sub bindir {
  my $self = shift;

  Carp::croak('You must call this method as an object') unless ref($self);

  return $self->_config_get_one_line_param('bindir', qw(--bindir));
}


=head2 $aroot->libdir

This method returns the path to the library (F<lib/>) directory of ROOT.

Example code:

  my $dir = $aroot->libdir;

=cut

sub libdir {
  my $self = shift;

  Carp::croak('You must call this method as an object') unless ref($self);



( run in 0.853 second using v1.01-cache-2.11-cpan-9288abcf80b )