File Coverage

File:config/auto/libffi.pm
Coverage:84.5%

linestmtbrancondsubcode
1# Copyright (C) 2010-2011, Parrot Foundation.
2#
3
4 - 12
=head1 NAME

config/auto/libffi - Check whether libffi is installed

=head1 DESCRIPTION

Note: The program F<pkg-config> is also required.

=cut
13
14package auto::libffi;
15
16
2
2
2
use strict;
17
2
2
2
use warnings;
18
19
2
2
2
use base qw(Parrot::Configure::Step);
20
21
2
2
2
use Parrot::Configure::Utils ':auto';
22
23sub _init {
24
3
    my $self = shift;
25
3
    my %data;
26
3
    $data{description} = q{Is libffi installed};
27
3
    $data{result} = q{};
28
3
    return \%data;
29}
30
31my @pkgconfig_variations =
32    defined( $ENV{TEST_PKGCONFIG} )
33    ? @{ $ENV{TEST_PKGCONFIG} }
34    : qw( pkg-config );
35
36sub runstep {
37
4
    my ( $self, $conf ) = @_;
38
39
4
    my ( $verbose, $without ) = $conf->options->get(
40        qw|
41            verbose
42            without-libffi
43        |
44    );
45
46
4
    if ($without) {
47
1
        $conf->data->set( HAS_LIBFFI => 0 );
48
1
        $conf->data->set( has_libffi => 0 );
49
1
        $self->set_result('no');
50
1
        return 1;
51    }
52
53
3
    my $osname = $conf->data->get('osname');
54
3
    print "\n" if $verbose;
55
3
    my $pkgconfig_exec = check_progs([ @pkgconfig_variations ], $verbose);
56
3
    unless ($pkgconfig_exec) {
57
0
        print "Program 'pkg-config' needed for libffi\n" if $verbose;
58
0
        $conf->data->set( HAS_LIBFFI => undef );
59
0
        $conf->data->set( has_libffi => undef );
60
0
        $self->set_result('lack pkg-config');
61
0
        return 1;
62    }
63
3
    my $rv = $self->_handle_pkgconfig_exec($conf, $pkgconfig_exec, $verbose);
64
3
    return 1 unless $rv;
65
66
3
    my $libffi_options_cflags = '';
67
3
    my $libffi_options_libs = '';
68
3
    my $libffi_options_linkflags = '';
69
70
3
    $libffi_options_linkflags =
71        capture_output($pkgconfig_exec, 'libffi --libs-only-L');
72
3
    chomp $libffi_options_linkflags;
73
3
    $libffi_options_libs = capture_output($pkgconfig_exec, 'libffi --libs-only-l');
74
3
    chomp $libffi_options_libs;
75
3
    $libffi_options_cflags = capture_output($pkgconfig_exec, 'libffi --cflags');
76
3
    chomp $libffi_options_cflags;
77
78
3
    my $extra_libs = $self->_select_lib( {
79        conf => $conf,
80        osname => $osname,
81        cc => $conf->data->get('cc'),
82        default => $libffi_options_libs . ' ' . $libffi_options_cflags,
83    } );
84
85
3
    $conf->cc_gen('config/auto/libffi/test_c.in');
86
3
3
    eval { $conf->cc_build( $libffi_options_cflags, $libffi_options_libs ) };
87
3
    my $has_libffi = 0;
88
3
    if ( !$@ ) {
89
3
        my $test = $conf->cc_run();
90
3
        $has_libffi = _evaluate_cc_run($test);
91    }
92
3
    $conf->cc_clean();
93
94
3
    if ($has_libffi) {
95
3
        $conf->data->set( HAS_LIBFFI => $has_libffi);
96
3
        $conf->data->set( has_libffi => $has_libffi);
97
3
        $conf->data->add( ' ', ccflags => $libffi_options_cflags );
98
3
        $conf->data->add( ' ', libs => $libffi_options_libs );
99
3
        $conf->data->add( ' ', linkflags => $libffi_options_linkflags );
100
3
        $self->set_result('yes');
101
3
        if ($verbose) {
102
1
            print 'libffi cflags: ', $libffi_options_cflags, "libffi libs: ", $libffi_options_libs, "\n";
103        }
104    }
105    else {
106
0
        $conf->data->set( HAS_LIBFFI => 0 );
107
0
        $conf->data->set( has_libffi => 0 );
108
0
        $self->set_result('no');
109
0
        print "No libffi found." if ($verbose);
110    }
111
112
3
    return 1;
113}
114
115sub _evaluate_cc_run {
116
5
    my ($output) = @_;
117
5
    my $has_libffi = ( $output =~ m/libffi worked/ ) ? 1 : 0;
118
5
    return $has_libffi;
119}
120
121sub _handle_pkgconfig_exec {
122
6
    my ($self, $conf, $pkgconfig_exec, $verbose) = @_;
123
6
    if (! $pkgconfig_exec) {
124
2
        print "Program 'pkg-config' needed for libffi\n" if $verbose;
125
2
        $conf->data->set( HAS_LIBFFI => undef );
126
2
        $conf->data->set( has_libffi => undef );
127
2
        $self->set_result('lack pkg-config');
128
2
        return;
129    }
130    else {
131
4
        return 1;
132    }
133}
134
1351;
136
137 - 141
=head1 AUTHOR

John Harrison <ash.gti at gmail dot com>

=cut
142
143# Local Variables:
144# mode: cperl
145# cperl-indent-level: 4
146# fill-column: 100
147# End:
148# vim: expandtab shiftwidth=4: