File Coverage

blib/lib/Devel/CheckCompiler.pm
Criterion Covered Total %
statement 38 39 97.4
branch 11 14 78.5
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 60 64 93.7


line stmt bran cond sub pod time code
1             package Devel::CheckCompiler;
2 5     5   242127 use strict;
  5         13  
  5         173  
3 5     5   26 use warnings;
  5         10  
  5         135  
4 5     5   120 use 5.008001;
  5         22  
  5         293  
5             our $VERSION = '0.05';
6 5     5   1783 use parent qw/Exporter/;
  5         691  
  5         28  
7              
8             our @EXPORT = qw/check_c99 check_c99_or_exit check_compile/;
9 5     5   11237 use ExtUtils::CBuilder;
  5         511273  
  5         2047  
10              
11             my $C99_SOURCE = <<'C99';
12             // include a C99 header
13             #include <stdbool.h>
14             inline // a C99 keyword with C99 style comments
15             int test_c99() {
16             int i = 0;
17             i++;
18             int j = i - 1; // another C99 feature: declaration after statement
19             return j;
20             }
21             C99
22              
23             sub check_c99 {
24 4     4 1 5445 check_compile($C99_SOURCE);
25             }
26              
27             sub check_c99_or_exit {
28 4 100   4 1 3654 check_compile($C99_SOURCE) or do {
29 2         253 warn "Your system is not support C99(OS unsupported)\n";
30 2         13 exit 0;
31             };
32             }
33              
34             sub check_compile {
35 11     11 1 8486 my ($src, %opt) = @_;
36              
37 11         66 my $cbuilder = ExtUtils::CBuilder->new(quiet => 1);
38 11 100       77 return 0 unless $cbuilder->have_compiler;
39              
40 7         91 require File::Temp;
41              
42 7         51 my $tmpfile = File::Temp->new(SUFFIX => '.c');
43 7         6201 $tmpfile->print($src);
44 7         112 $tmpfile->close();
45              
46 7         470 my $objname = eval {
47 7         40 $cbuilder->compile(source => $tmpfile->filename);
48             };
49 7 100       115 if ($objname) {
50 6         14 my $ret = 1;
51 6 100       33 if ($opt{executable}) {
52 2         4 my $execfile = eval {
53 2         11 $cbuilder->link_executable(objects => $objname,
54             extra_linker_flags => $opt{extra_linker_flags});
55             };
56              
57 2 50       26 if ($execfile) {
58 2 50       125 unlink $execfile or warn "Cannot unlink $execfile (ignored): $!";
59             } else {
60 0         0 $ret = 0;
61             }
62             }
63 6 50       381 unlink $objname or warn "Cannot unlink $objname (ignored): $!";
64 6         40 return $ret;
65             } else {
66 1         8 return 0;
67             }
68             }
69              
70             1;
71             __END__
72              
73             =encoding utf8
74              
75             =head1 NAME
76              
77             Devel::CheckCompiler - Check the compiler's availability
78              
79             =head1 SYNOPSIS
80              
81             use Devel::CheckCompiler;
82              
83             check_c99_or_exit();
84              
85             =head1 DESCRIPTION
86              
87             Devel::CheckCompiler is checker for compiler's availability.
88              
89             =head1 FUNCTIONS
90              
91             =over 4
92              
93             =item C<check_c99()>
94              
95             Returns true if the current system has a working C99 compiler, false otherwise.
96              
97             =item C<check_c99_or_exit()>
98              
99             Check the current system has a working C99 compiler, if it's not available, exit by 0.
100              
101             =item C<check_compile($src: Str, %options)>
102              
103             Compile C<$src> as C code. Return 1 if it's available, 0 otherwise.
104              
105             Possible options are:
106              
107             =over
108              
109             =item executable :Bool = false
110              
111             Check to see if generating executable is possible if this parameter is true.
112              
113             =item extra_linker_flags : Str | ArrayRef[Str]
114              
115             Any additional flags you wish to pass to the linker. This option is used
116             only when C<executable> option specified.
117              
118             =back
119              
120             =back
121              
122             =head1 AUTHOR
123              
124             Tokuhiro Matsuno E<lt>tokuhirom AAJKLFJEF@ GMAIL COME<gt>
125              
126             =head1 SEE ALSO
127              
128             L<ExtUtils::CBuilder>
129              
130             =head1 LICENSE
131              
132             Copyright (C) Tokuhiro Matsuno
133              
134             This library is free software; you can redistribute it and/or modify
135             it under the same terms as Perl itself.
136              
137             =cut