File Coverage

blib/lib/ExtUtils/Autoconf.pm
Criterion Covered Total %
statement 115 122 94.2
branch 32 40 80.0
condition 6 10 60.0
subroutine 23 23 100.0
pod 13 13 100.0
total 189 208 90.8


line stmt bran cond sub pod time code
1             package ExtUtils::Autoconf;
2              
3 5     5   65321 use strict;
  5         9  
  5         183  
4 5     5   27 use warnings;
  5         9  
  5         146  
5 5     5   28 use Cwd;
  5         16  
  5         1100  
6 5     5   31 use Config;
  5         10  
  5         197  
7 5     5   27 use File::Spec;
  5         22  
  5         130  
8 5     5   23 use File::Path qw(rmtree);
  5         10  
  5         356  
9 5     5   4751 use File::Which qw(which);
  5         4569  
  5         14444  
10              
11             =head1 NAME
12              
13             ExtUtils::Autoconf - Perl interface to GNU autoconf
14              
15             =head1 VERSION
16              
17             Version 0.02
18              
19             =cut
20              
21             our $VERSION = '0.02';
22              
23             =head1 SYNOPSIS
24              
25             use ExtUtils::Autoconf;
26              
27             my $ac = ExtUtils::Autoconf->new;
28              
29             $ac->autogen;
30             $ac->configure;
31              
32             $ac->clean;
33             $ac->realclean;
34              
35             =head1 DESCRIPTION
36              
37             ExtUtils::Autoconf is a thin wrapper around GNU autoconf/autoheader which
38             allows to run those tools easily from perl. This allows using autoconf for
39             configuring perl modules and especially extensions in a portable way without
40             messing around with the compilation of C code from perl.
41              
42             =head1 TYPICAL USAGE
43              
44             Typically ExtUtils::Autoconf is being used from Makefile.PLs in Perl module
45             distributions.
46              
47             =head2 AUTOCONF
48              
49             To use it in your module you first need to create a directory called
50             C (or call it something else and set C accordingly. This is the
51             working directory for ExtUtils::Autoconf and the programs it invokes.
52              
53             Create a C or C file in this directory. This file
54             contains invocations of autoconf macros that test the system features
55             your package needs or can use. Autoconf macros already exist to check for many
56             features; see L
57             Tests|http://www.gnu.org/software/autoconf/manual/html_node/Existing-Tests.html#Existing-Tests>,
58             for heir descriptions. For most other features, you can use Autoconf template
59             macros to produce custom checks; see L
60             Tests|http://www.gnu.org/software/autoconf/manual/html_node/Writing-Tests.html#Writing-Tests>,
61             for information about them.
62              
63             A typical C might look like this:
64              
65             AC_PREREQ(2.50)
66             AC_INIT
67             AC_CONFIG_HEADERS([config.h])
68              
69             AC_DEFINE_UNQUOTED(PERL_OSNAME, "$osname", [The operating system perl was ocnfigured for])
70              
71             AC_OUTPUT
72              
73             In this script we first require autoconf version 2.50, then initialize the
74             system and tell it to create a config header file called C with the
75             results it gathered while running the configure script.
76              
77             In this script we only have one result called C. This is simply
78             set to the value of the environment variable C<$osname>, which corresponds to
79             perls C<$Config{osname}>.
80              
81             After our tests we do C to write our results to C.
82              
83             That's it for the C part. Now include the C
84             file in your C or C++ code of the module and use the defines in there.
85              
86             =head2 Makefile.PL
87              
88             Execute the configure script which will be generated from the above
89             C when someone tries to build your module you'll need to modify
90             your C script.
91              
92             It's better to assume that the user of your module doesn't have autoconf and
93             autoheader installed so you need to generate C and C
94             before rolling your distribution together.
95              
96             =head3 ExtUtils::MakeMaker
97              
98             This is easily done by using the C target of your generated Makefile:
99              
100             WriteMakefile(
101             # usual arguments to WriteMakefile()..
102             dist => {
103             PREOP => q{$(PERLRUN) -MExtUtils::Autoconf -e'ExtUtils::Autoconf->run_autogen'},
104             },
105             );
106              
107             B and B automatically generate some files. To clean
108             those up automatically when doing C or C you can
109             use MakeMakers postamble feature. We also add some additional Makefile targets
110             for the ease of use:
111              
112             sub postamble {
113             return <<"EOM";
114             autogen :
115             \t\$(PERLRUN) -MExtUtils::Autoconf -e 'ExtUtils::Autoconf->run_autogen'
116              
117             configure :
118             \t\$(PERLRUN) -MExtUtils::Autoconf -e'ExtUtils::Autoconf->run_configure'
119              
120             autoclean :
121             \t\$(PERLRUN) -MExtUtils::Autoconf -e'ExtUtils::Autoconf->run_realclean'
122              
123             realclean purge ::
124             \t\$(PERLRUN) -MExtUtils::Autoconf -e 'ExtUtils::Autoconf->run_realclean'
125              
126             clean ::
127             \t\$(PERLRUN) -MExtUtils::Autoconf -e 'ExtUtils::Autoconf->run_clean'
128             EOM
129             }
130              
131             Now everything will work, except for the actual execution of the configure
132             script during C. To make it work do something like this in
133             C:
134              
135             use strict;
136             use warnings;
137             use ExtUtils::MakeMaker;
138              
139             if (!eval 'use ExtUtils::Autoconf;') {
140             print STDERR $@, "\n";
141              
142             WriteMakefile(
143             PREREQ_FATAL => 1,
144             PREREQ_PM => {
145             'ExtUtils::Autoconf' => 0,
146             },
147             );
148              
149             exit 1; # not reached
150             }
151              
152             my $ac = ExtUtils::Autoconf->new;
153             $ac->configure;
154              
155             # rest of the usual Makefile.PL here..
156              
157             The C condition covers those cases where ExtUtils::Autoconf isn't installed
158             yet. I'll raise a fatal error which will hopefully tell CPAN to install it and
159             rerun C.
160              
161             =head3 Module::Install
162              
163             If you're using L for your Makefile.PL, you can simply install
164             L and do B in it.
165              
166             =head1 SUBROUTINES/METHODS
167              
168             =head2 new
169              
170             my $ac = ExtUtils::Autoconf->new($arguments);
171              
172             This is the constructor.. Takes an optional hashref with additional
173             C<$arguments>. Returns a new ExtUtils::Autoconf instance.
174              
175             The following arguments are supported:
176              
177             =over
178              
179             =item * C
180              
181             The working directory for autoconf. All commands issued by ExtUtils::Autoconf
182             will be executed in this directory.
183              
184             =item * C
185              
186             The name of the autoconf executable.
187              
188             =item * C
189              
190             The name of the autoheader executable.
191              
192             =item * C
193              
194             A hash reference with the environment variables which will be set for each
195             program execution issued by ExtUtils::Autoconf.
196              
197             The default looks like this:
198              
199             {
200             MAKE => $Config{ make },
201             SHELL => $Config{ sh },
202             CC => $Config{ cc },
203             CPPFLAGS => $Config{ cppflags },
204             CFLAGS => $Config{ ccflags },
205             LDFLAGS => $Config{ ldflags },
206             LINKER => $Config{ ld },
207             LIBS => '',
208             %Config,
209             }
210              
211             Where C<%Config> is the local perl configuration from C. To add
212             additional environment variables or to override existing ones do:
213              
214             my $ac = ExtUtils::Autoconf->new({
215             env => {
216             key1 => 'val1',
217             key2 => 'val2',
218             },
219             });
220              
221             =back
222              
223             =cut
224              
225             sub new {
226 17     17 1 5754 my ($class, $args) = @_;
227              
228             my $self = {
229             wd => 'autoconf',
230             autoconf => (which('autoconf'))[0] || 'autoconf',
231             autoheader => (which('autoheader'))[0] || 'autoheader',
232             env => {
233             MAKE => $Config{ make },
234             SHELL => $Config{ sh },
235             CC => $Config{ cc },
236             CPPFLAGS => $Config{ cppflags },
237             CFLAGS => $Config{ ccflags },
238             LDFLAGS => $Config{ ldflags },
239             LINKER => $Config{ ld },
240 17   50     143 LIBS => q{},
      50        
241             %Config,
242             },
243             };
244              
245 17         294325 bless $self, $class;
246              
247 17 100 100     204 if (ref $args && ref $args eq 'HASH') {
248 9         170 $self->_process_args($args);
249             }
250              
251 17         82 return $self;
252             }
253              
254             sub _process_args {
255 9     9   22 my ($self, $args) = @_;
256              
257 9         25 while (my ($k, $v) = each %{ $args }) {
  20         127  
258 11 100       95 next unless $self->can($k);
259              
260 10 100       46 if (!ref $v) { #plain scalar
    100          
261 8         29 $self->$k($v);
262             }
263              
264             elsif (ref $v eq 'HASH') { #hashref (env)
265 1         6 while (my ($sk, $sv) = each %{ $v }) {
  2         15  
266 1         126 $self->$k($sk => $sv);
267             }
268             }
269              
270             }
271             }
272              
273             =head2 configure
274              
275             $ac->configure;
276              
277             Runs the configure script. If the configure script doesn't exist yet it'll run
278             B. Returns 1 on success or croaks on failure.
279              
280             =cut
281              
282             sub configure {
283 7     7 1 38 my ($self) = @_;
284              
285 7         22 my $configure = 'configure';
286              
287 7 50       28 if (!-x File::Spec->catfile($self->wd, $configure)) {
288 7         27 $self->autogen;
289             }
290              
291 0 0       0 if (!$self->_run_cmd("./$configure", '--prefix='. "\Q$Config{prefix}\E")) {
292 0         0 require Carp;
293 0         0 Carp::croak(
294             q{configure failed. Check }
295             . File::Spec->catfile($self->wd, 'config.log')
296             . q{.}
297             );
298             }
299              
300 0         0 return 1;
301             }
302              
303             =head2 autogen
304              
305             $ac->autogen;
306              
307             Runs autoheader and autoconf to generate config.h.in and configure from
308             configure.ac or configure.in. Returns 1 on success or croaks on failure.
309              
310             =cut
311              
312             sub autogen {
313 10     10 1 29 my ($self) = @_;
314              
315 10   33     40 my $ret = $self->_run_cmd($self->autoheader) && $self->_run_cmd($self->autoconf);
316 8 50       192 if (!$ret) {
317 8         342 require Carp;
318 8         621 Carp::croak('autogen failed. check the error messages above.');
319             }
320              
321 0         0 return 1;
322             }
323              
324             *reconf = \&autogen; #compat
325              
326             =head2 clean
327              
328             $ac->clean;
329              
330             Cleans up files which were generated during B. Always returns
331             something true.
332              
333             =cut
334              
335             sub clean {
336 8     8 1 45 my ($self) = @_;
337              
338 8         75 for my $file (qw( config.h config.log config.status )) {
339 24         175 my $path = File::Spec->catfile($self->wd, $file);
340 24 50       367 next unless -f $path;
341 0         0 unlink $path;
342             }
343              
344 8         161 return 1;
345             }
346              
347             =head2 realclean
348              
349             my $success = $ac->realclean;
350              
351             Cleans up files which were generated during B and B.
352             Always returns something true.
353              
354             =cut
355              
356             sub realclean {
357 6     6 1 38 my ($self) = @_;
358              
359 6         35 $self->clean;
360 6         22 for my $file (qw( config.h.in configure )) {
361 12         32 my $path = File::Spec->catfile($self->wd, $file);
362 12 50       150 next unless -f $path;
363 0         0 unlink $path;
364             }
365              
366 6         15 rmtree( File::Spec->catfile($self->wd, 'autom4te.cache'), 0, 0 );
367              
368 6         188 return 1;
369             }
370              
371             =head2 wd
372              
373             my $wd = $ac->wd;
374             $ac->wd($new_wd);
375              
376             Accessor for the wd (working directory) option. When called without any
377             argument it returns the current working directory. When called with one
378             argument the working directory is set to C<$new_wd> and the new working
379             directory is returned.
380              
381             =cut
382              
383             sub wd {
384 75     75 1 1739 my $self = shift;
385              
386 75 100       264 if (@_) {
387 12         43 $self->{wd} = shift;
388             }
389              
390 75         3313 return $self->{wd};
391             }
392              
393             =head2 autoconf
394              
395             my $autoconf = $ac->autoconf;
396             $ac->autoconf($new_autoconf);
397              
398             Accessor for the name of the autoconf executable.
399              
400             =cut
401              
402             sub autoconf {
403 3     3 1 9 my $self = shift;
404              
405 3 100       20 if (@_) {
406 2         5 $self->{autoconf} = shift;
407             }
408              
409 3         12 return $self->{autoconf};
410             }
411              
412             =head2 autoheader
413              
414             my $autoheader = $ac->autoheader;
415             $ac->autoheader($new_autoheader);
416              
417             Accessor for the name of the autoheader executable.
418              
419             =cut
420              
421             sub autoheader {
422 12     12 1 793 my $self = shift;
423              
424 12 100       41 if (@_) {
425 1         4 $self->{autoheader} = shift;
426             }
427              
428 12         73 return $self->{autoheader};
429             }
430              
431             =head2 env
432              
433             my $env = $ac->env;
434             my $value = $ac->env($key);
435             $ac->env($key => $value);
436              
437             Accessor for the environment option. When called without any option it returns
438             a hash reference with all the environment variables that will be set when
439             running any command.
440              
441             When called with one argument it'll return the value of the environment
442             variable corresponding to a given C<$key>.
443              
444             When called with two arguments the environment variable C<$key> will be set to
445             C<$value>.
446              
447             =cut
448              
449             sub env {
450 13     13 1 991 my $self = shift;
451              
452 13 100       75 if (scalar @_ == 1) {
    100          
    100          
453 10         45 my ($key) = @_;
454 10         82126 return $self->{env}->{ $key };
455             }
456             elsif (scalar @_ == 2) {
457 1         3 my ($key, $val) = @_;
458 1         4 $self->{env}->{ $key } = $val;
459 1         4 return $self->{env}->{ $key };
460             }
461             elsif (scalar @_ != 0) {
462 1         10 require Carp;
463 1         30 Carp::croak('ExtUtils::Config::env expects 0, 1 or 2 arguments');
464             }
465              
466 1         5 return $self->{env};
467             }
468              
469             sub _run_cmd {
470 10     10   22 my ($self, $cmd, @args) = @_;
471              
472 10         54120 my $cwd = cwd();
473 10 100       286 if (!chdir $self->wd) {
474 2         218 require Carp;
475 2         137 Carp::croak("Could not chdir to `". $self->wd ."'. Did you set the working dir correctly?");
476             }
477              
478 8         48 my $ret;
479             {
480 8         24 local %ENV = %ENV;
  8         3186  
481 8         71 while (my ($k, $v) = each %{ $self->{env} }) {
  9024         40344  
482 9016 100       18946 next unless defined $v;
483 7432         120292 $ENV{$k} = $v;
484             }
485              
486 8         86 $ret = system $self->env('SHELL'), -c => "\Q$cmd\E ". join(q{ }, @args);
487             }
488              
489 8         347 chdir $cwd;
490 8         272 return ($ret == 0);
491             }
492              
493             =head2 run_configure
494              
495             perl -MExtUtils::Autoconf -e'ExtUtils::Autoconf->run_configure' $wd
496              
497             This class method is intended to be used from Makefiles and similar things and
498             reads its arguments from C<@ARGV>. It constructs a new ExtUtils::Autoconf
499             instance with the given C<$wd> and runs B.
500              
501             =cut
502              
503             sub run_configure {
504 2     2 1 22 my ($class) = @_;
505              
506 2         14 my $ac = $class->new;
507 2         13 $ac->_process_argv;
508              
509 2         10 $ac->configure;
510             }
511              
512             =head2 run_autogen
513              
514             perl -MExtUtils::Autoconf -e'ExtUtils::Autoconf->run_autogen' $wd, $autoconf, $autoheader
515              
516             This class method is intended to be used from Makefiles and similar things and
517             reads its arguments from C<@ARGV>. It constructs a new ExtUtils::Autoconf
518             instance with the given C<$wd>, C<$autoconf>, C<$autoheader> and runs
519             B.
520              
521             =cut
522              
523             sub run_autogen {
524 1     1 1 10 my ($class) = @_;
525              
526 1         4 my $ac = $class->new;
527 1         34 $ac->_process_argv;
528              
529 1         4 $ac->autogen;
530             }
531              
532             =head2 run_clean
533              
534             perl -MExtUtils::Autoconf -e'ExtUtils::Autoconf->run_clean' $wd
535              
536             This class method is intended to be used from Makefiles and similar things and
537             reads its arguments from C<@ARGV>. It constructs a new ExtUtils::Autoconf
538             instance with the given C<$wd> and runs B.
539              
540             =cut
541              
542             sub run_clean {
543 1     1 1 18 my ($class) = @_;
544              
545 1         18 my $ac = $class->new;
546 1         14 $ac->_process_argv;
547              
548 1         7 $ac->clean;
549             }
550              
551             =head2 run_realclean
552              
553             perl -MExtUtils::Autoconf -e'ExtUtils::Autoconf->run_realclean' $wd
554              
555             This class method is intended to be used from Makefiles and similar things and
556             reads its arguments from C<@ARGV>. It constructs a new ExtUtils::Autoconf
557             instance with the given C<$wd> and runs B.
558              
559             =cut
560              
561             sub run_realclean {
562 1     1 1 10 my ($class) = @_;
563              
564 1         9 my $ac = $class->new;
565 1         6 $ac->_process_argv;
566              
567 1         4 $ac->realclean;
568             }
569              
570             sub _process_argv {
571 5     5   13 my ($self) = @_;
572 5         16 my ($wd, $autoconf, $autoheader) = @ARGV;
573              
574 5 100       25 $self->wd($wd) if defined $wd;
575 5 50       17 $self->autoconf($autoconf) if defined $autoconf;
576 5 50       20 $self->autoheader($autoheader) if defined $autoheader;
577             }
578              
579             =head1 DIAGNOSTICS
580              
581             =over
582              
583             =item C
584              
585             Running ./configure failed. Diagnostic messages may be found in the according
586             config.log file.
587              
588             =item C
589              
590             Running autoheader or autoconf failed. Probably your C is
591             erroneous. Try running autoheader and autoconf manually.
592              
593             =back
594              
595             =head1 AUTHOR
596              
597             Florian Ragwitz Erafl@debian.orgE
598              
599             =head1 BUGS
600              
601             Please report any bugs or feature requests to
602             Ebug-extutils-autoconf@rt.cpan.orgE, or through the web interface at
603             L. I will be
604             notified, and then you'll automatically be notified of progress on your bug as
605             I make changes.
606              
607             =head1 SUPPORT
608              
609             You can find documentation for this module with the perldoc command.
610              
611             perldoc ExtUtils::Autoconf
612              
613             You can also look for information at:
614              
615             =over 4
616              
617             =item * GNU Autoconf documentation
618              
619             L
620              
621             =item * Felipe Bergo's autotools tutorial
622              
623             L
624              
625             =item * AnnoCPAN: Annotated CPAN documentation
626              
627             L
628              
629             =item * CPAN Ratings
630              
631             L
632              
633             =item * RT: CPAN's request tracker
634              
635             L
636              
637             =item * Search CPAN
638              
639             L
640              
641             =back
642              
643             =head1 ACKNOWLEDGEMENTS
644              
645             Marc Lehmann Eschmorp@schmorp.deE for the idea and a prof-of-concept
646             implementation (autoconf.pm) in L.
647              
648             =head1 LICENSE AND COPYRIGHT
649              
650             Copyright 2006 Florian Ragwitz, all rights reserved.
651              
652             This program is free software; you can redistribute it and/or modify it
653             under the same terms as Perl itself.
654              
655             =cut
656              
657             1; # End of ExtUtils::Autoconf