File Coverage

File:config/auto/attributes.pm
Coverage:86.0%

linestmtbrancondsubcode
1# Copyright (C) 2007, Parrot Foundation.
2# $Id: attributes.pm 36833 2009-02-17 20:09:26Z allison $
3
4 - 13
=head1 NAME

config/auto/attributes.pm - Attributes detection

=head1 DESCRIPTION

Automagically detect what attributes, like HASATTRIBUTE_CONST, that
the compiler can support.

=cut
14
15package auto::attributes;
16
17
81
81
81
use strict;
18
81
81
81
use warnings;
19
20
81
81
81
use base qw(Parrot::Configure::Step);
21
22
81
81
81
use Parrot::Configure::Utils ();
23
81
81
81
use Parrot::BuildUtil;
24
25sub _init {
26
81
    my $self = shift;
27
81
    my %data;
28
81
    $data{description} = q{Detect compiler attributes};
29
81
    $data{result} = q{};
30
81
    return \%data;
31}
32
33our @potential_attributes = qw(
34    HASATTRIBUTE_CONST
35    HASATTRIBUTE_DEPRECATED
36    HASATTRIBUTE_FORMAT
37    HASATTRIBUTE_MALLOC
38    HASATTRIBUTE_NONNULL
39    HASATTRIBUTE_NORETURN
40    HASATTRIBUTE_PURE
41    HASATTRIBUTE_UNUSED
42    HASATTRIBUTE_WARN_UNUSED_RESULT
43    HASATTRIBUTE_NEVER_WORKS
44);
45
46sub runstep {
47
81
    my ( $self, $conf ) = @_;
48
49
81
    my $verbose = $conf->options->get('verbose');
50
81
    print "\n" if $verbose;
51
52
81
    for my $maybe_attr (@potential_attributes) {
53
810
        $self->try_attr( $conf, $maybe_attr, $verbose );
54    }
55
81
    return 1;
56}
57
58sub try_attr {
59
810
    my ( $self, $conf, $attr, $verbose ) = @_;
60
61
810
    my $output_file = 'test.cco';
62
63
810
    $verbose and print "trying attribute '$attr'\n";
64
65
810
    my $cc = $conf->option_or_data('cc');
66
810
    $conf->cc_gen('config/auto/attributes/test_c.in');
67
68
810
    my $disable_warnings = '';
69
70    # work around msvc warning for unused variable
71
810
    if ( defined $conf->option_or_data('msvcversion') ) {
72
0
        $disable_warnings = '-wd4101';
73    }
74
75
810
    my $ccflags = $conf->data->get('ccflags');
76
810
    my $tryflags = "$ccflags -D$attr $disable_warnings";
77
78
810
    my $command_line = Parrot::Configure::Utils::_build_compile_command( $cc, $tryflags );
79
810
    $verbose and print " ", $command_line, "\n";
80
81    # Don't use cc_build, because failure is expected.
82
810
    my $exit_code =
83        Parrot::Configure::Utils::_run_command( $command_line, $output_file, $output_file );
84
810
    $verbose and print " exit code: $exit_code\n";
85
86
810
    $conf->cc_clean();
87
810
    $conf->data->set( $attr => !$exit_code | 0 );
88
89
810
    if ($exit_code) {
90
162
        unlink $output_file or die "Unable to unlink $output_file: $!";
91
162
        $verbose and print "Rejecting bogus attribute: $attr\n";
92
162
        return;
93    }
94
95
648
    my $output = Parrot::BuildUtil::slurp_file($output_file);
96
648
    $verbose and print " output: $output\n";
97
98
648
    if ( $output !~ /error|warning/i ) {
99
648
        $conf->data->set( ccflags => $tryflags );
100
648
        my $ccflags = $conf->data->get("ccflags");
101
648
        $verbose and print " ccflags: $ccflags\n";
102    }
103
648
    unlink $output_file or die "Unable to unlink $output_file: $!";
104
105
648
    return;
106}
107
1081;
109
110# Local Variables:
111# mode: cperl
112# cperl-indent-level: 4
113# fill-column: 100
114# End:
115# vim: expandtab shiftwidth=4: