File Coverage

inc/MyBuilder.pm
Criterion Covered Total %
statement 203 241 84.2
branch 33 86 38.3
condition 0 10 0.0
subroutine 21 22 95.4
pod 0 9 0.0
total 257 368 69.8


line stmt bran cond sub pod time code
1             package MyBuilder;
2 1     1   39 use base 'Module::Build';
  1         3  
  1         628  
3              
4 1     1   92764 use warnings;
  1         3  
  1         56  
5 1     1   5 use strict;
  1         2  
  1         24  
6              
7 1     1   7 use Config;
  1         3  
  1         49  
8 1     1   6 use Carp;
  1         3  
  1         129  
9              
10 1     1   1229 use Config::AutoConf;
  1         84937  
  1         253  
11 1     1   632 use ExtUtils::LibBuilder;
  1         63413  
  1         55  
12              
13 1     1   1001 use ExtUtils::ParseXS;
  1         53602  
  1         70  
14 1     1   597 use ExtUtils::Mkbootstrap;
  1         682  
  1         142  
15              
16 1     1   338 use File::Spec::Functions qw.catdir catfile.;
  1         738  
  1         70  
17 1     1   6 use File::Path qw.mkpath.;
  1         2  
  1         53  
18 1     1   4 use Cwd 'abs_path';
  1         2  
  1         2941  
19              
20             my @EXTRA_FLAGS = ();
21             my @BINARIES = qw(biblex bibparse dumpnames);
22              
23             ## debug
24             ## @EXTRA_FLAGS = ('-g', "-DDEBUG=2");
25              
26             sub ACTION_install {
27 0     0 0 0 my $self = shift;
28              
29 0         0 my $usrlib = $self->install_path( 'usrlib' );
30              
31 0 0       0 if ($^O =~ /darwin/i) {
32 0         0 my $libpath = $self->notes('lib_path');
33 0         0 $libpath = catfile($libpath, "libbtparse$LIBEXT");
34              
35             # do it for binaries as well.
36 0         0 `install_name_tool -id "$libpath" ./blib/usrlib/libbtparse.dylib`;
37             # binries
38 0         0 my $libfile = "btparse/src/libbtparse$LIBEXT";
39 0         0 my $abs_path = abs_path($libfile);
40 0         0 foreach my $bin (@BINARIES) {
41 0         0 `install_name_tool -change "$abs_path" "$libpath" ./blib/bin/$bin$EXEEXT`;
42             }
43 0         0 my $bundle = $self->notes("bundle");
44 0         0 `install_name_tool -change "$abs_path" "$libpath" $bundle`;
45             }
46              
47 0 0       0 if ($^O =~ /cygwin/i) { # cygwin uses windows lib searching (PATH instead of LD_LIBRARY_PATH)
    0          
48 0         0 $self->install_path( 'usrlib' => '/usr/local/bin' );
49             }
50             elsif (defined $self->{properties}{install_base}) {
51 0         0 $usrlib = catdir($self->{properties}{install_base} => 'lib');
52 0         0 $self->install_path( 'usrlib' => $usrlib );
53             }
54 0         0 $self->SUPER::ACTION_install;
55 0 0 0     0 if ($^O =~ /linux/ && $ENV{USER} eq 'root') {
56 0         0 my $linux = Config::AutoConf->check_prog("ldconfig");
57 0 0       0 system $linux if (-x $linux);
58             }
59 0 0 0     0 if ($^O =~ /(?:linux|bsd|sun|sol|dragonfly|hpux|irix|darwin|gnu)/
60             &&
61             $usrlib !~ m!^/usr(/local)?/lib/?$!)
62             {
63 0         0 warn "\n** WARNING **\n"
64             . "It seems you are installing in a non standard path.\n"
65             . "You might need to add $usrlib to your library search path.\n";
66             }
67            
68             }
69              
70             sub ACTION_code {
71 1     1 0 168 my $self = shift;
72              
73 1         24 for my $path (catdir("blib","bindoc"), catdir("blib","bin")) {
74 2 50       65 mkpath $path unless -d $path;
75             }
76              
77 1         33 my $libbuilder = ExtUtils::LibBuilder->new;
78 1         549153 $self->notes('libbuilder', $libbuilder);
79              
80 1         26972 my $version = $self->notes('btparse_version');
81              
82 1         26 my $alloca_h = 'undef HAVE_ALLOCA_H';
83 1 50       33 $alloca_h = 'define HAVE_ALLOCA_H 1' if Config::AutoConf->check_header("alloca.h");
84              
85 1         185499 my $vsnprintf = 'undef HAVE_VSNPRINTF';
86 1 50       38 $vsnprintf = 'define HAVE_VSNPRINTF 1' if Config::AutoConf->check_func('vsnprintf');
87              
88 1         396284 my $strlcat = 'undef HAVE_STRLCAT';
89 1 50       31 $strlcat = 'define HAVE_STRLCAT 1' if Config::AutoConf->check_func('strlcat');
90              
91 1         383138 _interpolate("btparse/src/bt_config.h.in",
92             "btparse/src/bt_config.h",
93             PACKAGE => "\"libbtparse\"",
94             FPACKAGE => "\"libbtparse $version\"",
95             VERSION => "\"$version\"",
96             ALLOCA_H => $alloca_h,
97             VSNPRINTF => $vsnprintf,
98             STRLCAT => $strlcat
99             );
100              
101              
102 1         30 $self->dispatch("create_manpages");
103 1         153 $self->dispatch("create_objects");
104 1         684657 $self->dispatch("create_library");
105 1         3199 $self->dispatch("create_binaries");
106 1         1229 $self->dispatch("create_tests");
107              
108 1         183442 $self->dispatch("compile_xscode");
109              
110 1         70540 $self->copy_if_modified( from => 'btparse/src/btparse.h',
111             to_dir => "blib/usrinclude",
112             flatten => 1);
113              
114 1         389 $self->SUPER::ACTION_code;
115             }
116              
117             sub ACTION_compile_xscode {
118 1     1 0 226 my $self = shift;
119 1         16 my $cbuilder = $self->cbuilder;
120              
121 1         31 my $archdir = catdir( $self->blib, 'arch', 'auto', 'Text', 'BibTeX');
122 1 50       84 mkpath( $archdir, 0, 0777 ) unless -d $archdir;
123              
124 1         13 print STDERR "\n** Preparing XS code\n";
125 1         19 my $cfile = catfile("xscode","BibTeX.c");
126 1         10 my $xsfile= catfile("xscode","BibTeX.xs");
127              
128 1         63 $self->add_to_cleanup($cfile); ## FIXME
129 1 50       395 if (!$self->up_to_date($xsfile, $cfile)) {
130 0         0 ExtUtils::ParseXS::process_file( filename => $xsfile,
131             prototypes => 0,
132             output => $cfile);
133             }
134              
135 1         99 my $ofile = catfile("xscode","BibTeX.o");
136 1         7 $self->add_to_cleanup($ofile); ## FIXME
137 1 50       102 if (!$self->up_to_date($cfile, $ofile)) {
138 0         0 $cbuilder->compile( source => $cfile,
139             extra_compiler_flags => [@EXTRA_FLAGS],
140             include_dirs => [ catdir("btparse","src") ],
141             object_file => $ofile);
142             }
143              
144             # Create .bs bootstrap file, needed by Dynaloader.
145 1         68 my $bs_file = catfile( $archdir, "BibTeX.bs" );
146 1 50       9 if ( !$self->up_to_date( $ofile, $bs_file ) ) {
147 1         95 ExtUtils::Mkbootstrap::Mkbootstrap($bs_file);
148 1 50       71 if ( !-f $bs_file ) {
149             # Create file in case Mkbootstrap didn't do anything.
150 0 0       0 open( my $fh, '>', $bs_file ) or confess "Can't open $bs_file: $!";
151             }
152 1         36 utime( (time) x 2, $bs_file ); # touch
153             }
154              
155 1         28 my $objects = $self->rscan_dir("xscode",qr/\.o$/);
156             # .o => .(a|bundle)
157 1         707 my $lib_file = catfile( $archdir, "BibTeX.$Config{dlext}" );
158 1         23 $self->notes("bundle", $lib_file); # useful for darwin
159 1 50       165 if ( !$self->up_to_date( [ @$objects ], $lib_file ) ) {
160 1         113 my $btparselibdir = $self->install_path('usrlib');
161 1         38 $cbuilder->link(
162             module_name => 'Text::BibTeX',
163             extra_linker_flags => "-Lbtparse/src -lbtparse ",
164             objects => $objects,
165             lib_file => $lib_file,
166             );
167             }
168             }
169              
170             sub ACTION_create_manpages {
171 1     1 0 455 my $self = shift;
172              
173 1         8 print STDERR "\n** Creating Manpages\n";
174              
175 1         81 my $pods = $self->rscan_dir(catdir("btparse","doc"), qr/\.pod$/);
176              
177 1         700 my $version = $self->notes('btparse_version');
178 1         80 for my $pod (@$pods) {
179 10         557 my $man = $pod;
180 10         54 $man =~ s!.pod!.1!;
181 10         30 $man =~ s!btparse/doc!blib/bindoc!; ## FIXME - path
182 10 50       42 next if $self->up_to_date($pod, $man);
183             ## FIXME
184 0         0 `pod2man --section=1 --center="btparse" --release="btparse, version $version" $pod $man`;
185             }
186              
187 1         60 my $pod = 'btool_faq.pod';
188 1         12 my $man = catfile('blib','bindoc','btool_faq.1');
189 1 50       5 unless ($self->up_to_date($pod, $man)) {
190             ## FIXME
191 0         0 `pod2man --section=1 --center="btparse" --release="btparse, version $version" $pod $man`;
192             }
193             }
194              
195             sub ACTION_create_objects {
196 1     1 0 152 my $self = shift;
197 1         19 my $cbuilder = $self->cbuilder;
198              
199 1         39962 print STDERR "\n** Compiling C files\n";
200 1         16 my $c_progs = $self->rscan_dir('btparse/progs', qr/\.c$/);
201 1         1107 my $c_src = $self->rscan_dir('btparse/src', qr/\.c$/);
202 1         1736 my $c_tests = $self->rscan_dir('btparse/tests', qr/\.c$/);
203 1         1466 my $c_xs = $self->rscan_dir('xscode/', qr/\.c$/);
204              
205 1         396 my @c_files = (@$c_progs, @$c_src, @$c_tests, @$c_xs);
206 1         11 for my $file (@c_files) {
207 40         10611986 my $object = $file;
208 40         1065 $object =~ s/\.c/.o/;
209 40 50       1068 next if $self->up_to_date($file, $object);
210 40         7193 $cbuilder->compile(object_file => $object,
211             extra_compiler_flags=>["-D_FORTIFY_SOURCE=1",@EXTRA_FLAGS],
212             source => $file,
213             include_dirs => ["btparse/src"]);
214             }
215             }
216              
217              
218             sub ACTION_create_binaries {
219 1     1 0 7320 my $self = shift;
220 1         27 my $cbuilder = $self->cbuilder;
221 1         39 my $libbuilder = $self->notes('libbuilder');
222 1         79 my $EXEEXT = $libbuilder->{exeext};
223 1         40 my $btparselibdir = $self->install_path('usrlib');
224              
225 1         53 print STDERR "\n** Creating binaries (",join(", ", map { $_.$EXEEXT } @BINARIES), ")\n";
  3         38  
226              
227 1 50       23 my $extra_linker_flags = sprintf("-Lbtparse/src %s -lbtparse ",
228             ($^O !~ /darwin/)?"-Wl,-R${btparselibdir}":"");
229              
230 1         18 my @toinstall;
231              
232 1         13 for my $bin (@BINARIES) {
233 3         216472 my $exe_file = catfile("btparse","progs","$bin$EXEEXT");
234 3         31 push @toinstall, $exe_file;
235 3         45 my $objects = [ catfile("btparse","progs","$bin.o") ];
236              
237 3 100       30 if ($bin eq "bibparse") { # hack for now
238 1         22 $objects = [map {catfile("btparse","progs","$_.o")} (qw.bibparse args getopt getopt1.)];
  4         39  
239             }
240              
241 3 50       55 if (!$self->up_to_date($objects, $exe_file)) {
242 3         427 $libbuilder->link_executable(exe_file => $exe_file,
243             objects => $objects ,
244             extra_linker_flags => $extra_linker_flags);
245             }
246             }
247              
248 1         69411 for my $file (@toinstall) {
249 3         3461 $self->copy_if_modified( from => $file,
250             to_dir => "blib/bin",
251             flatten => 1);
252             }
253              
254             }
255              
256             sub ACTION_create_tests {
257 1     1 0 161 my $self = shift;
258 1         14 my $cbuilder = $self->cbuilder;
259              
260 1         3387 my $libbuilder = $self->notes('libbuilder');
261 1         53 my $EXEEXT = $libbuilder->{exeext};
262              
263 1         75 print STDERR "\n** Creating test binaries\n";
264              
265 1         18 my $exe_file = catfile("btparse","tests","simple_test$EXEEXT");
266 1         6 my $objects = [ map{catfile("btparse","tests","$_.o")} (qw.simple_test testlib.) ];
  2         15  
267              
268 1 50       8 if (!$self->up_to_date($objects, $exe_file)) {
269 1         89 $libbuilder->link_executable(exe_file => $exe_file,
270             extra_linker_flags => '-Lbtparse/src -lbtparse ',
271             objects => $objects);
272             }
273              
274 1         101308 $exe_file = catfile("btparse","tests","read_test$EXEEXT");
275 1         15 $objects = [ map{catfile("btparse","tests","$_.o")}(qw.read_test testlib.) ];
  2         109  
276 1 50       36 if (!$self->up_to_date($objects, $exe_file)) {
277 1         216 $libbuilder->link_executable(exe_file => $exe_file,
278             extra_linker_flags => '-Lbtparse/src -lbtparse ',
279             objects => $objects);
280             }
281              
282 1         79110 $exe_file = catfile("btparse","tests","postprocess_test$EXEEXT");
283 1         16 $objects = [ map{catfile("btparse","tests","$_.o")}(qw.postprocess_test.) ];
  1         32  
284 1 50       31 if (!$self->up_to_date($objects, $exe_file)) {
285 1         123 $libbuilder->link_executable(exe_file => $exe_file,
286             extra_linker_flags => '-Lbtparse/src -lbtparse ',
287             objects => $objects);
288             }
289              
290 1         65940 $exe_file = catfile("btparse","tests","tex_test$EXEEXT");
291 1         13 $objects = [ map{catfile("btparse","tests","$_.o")}(qw.tex_test.) ];
  1         92  
292 1 50       25 if (!$self->up_to_date($objects, $exe_file)) {
293 1         109 $libbuilder->link_executable(exe_file => $exe_file,
294             extra_linker_flags => '-Lbtparse/src -lbtparse ',
295             objects => $objects);
296             }
297              
298 1         84134 $exe_file = catfile("btparse","tests","macro_test$EXEEXT");
299 1         13 $objects = [ map{catfile("btparse","tests","$_.o")}(qw.macro_test.) ];
  1         26  
300 1 50       58 if (!$self->up_to_date($objects, $exe_file)) {
301 1         111 $libbuilder->link_executable(exe_file => $exe_file,
302             extra_linker_flags => '-Lbtparse/src -lbtparse ',
303             objects => $objects);
304             }
305              
306 1         53372 $exe_file = catfile("btparse","tests","name_test$EXEEXT");
307 1         18 $objects = [ map{catfile("btparse","tests","$_.o")}(qw.name_test.) ];
  1         30  
308 1 50       33 if (!$self->up_to_date($objects, $exe_file)) {
309 1         121 $libbuilder->link_executable(exe_file => $exe_file,
310             extra_linker_flags => '-Lbtparse/src -lbtparse ',
311             objects => $objects);
312             }
313              
314 1         73645 $exe_file = catfile("btparse","tests","namebug$EXEEXT");
315 1         19 $objects = [ map{catfile("btparse","tests","$_.o")}(qw.namebug.) ];
  1         28  
316 1 50       27 if (!$self->up_to_date($objects, $exe_file)) {
317 1         118 $libbuilder->link_executable(exe_file => $exe_file,
318             extra_linker_flags => '-Lbtparse/src -lbtparse ',
319             objects => $objects);
320             }
321              
322 1         61174 $exe_file = catfile("btparse","tests","purify_test$EXEEXT");
323 1         19 $objects = [ map{catfile("btparse","tests","$_.o")}(qw.purify_test.) ];
  1         26  
324 1 50       28 if (!$self->up_to_date($objects, $exe_file)) {
325 1         145 $libbuilder->link_executable(exe_file => $exe_file,
326             extra_linker_flags => '-Lbtparse/src -lbtparse ',
327             objects => $objects);
328             }
329             }
330              
331             sub ACTION_create_library {
332 1     1 0 296 my $self = shift;
333 1         24 my $cbuilder = $self->cbuilder;
334              
335              
336 1         149 my $libbuilder = $self->notes('libbuilder');
337 1         99 my $LIBEXT = $libbuilder->{libext};
338              
339 1         18 print STDERR "\n** Creating libbtparse$LIBEXT\n";
340              
341 1         34 my @modules = qw:init input bibtex err scan error
342             lex_auxiliary parse_auxiliary bibtex_ast sym
343             util postprocess macros traversal modify
344             names tex_tree string_util format_name:;
345              
346 1         15 my @objects = map { "btparse/src/$_.o" } @modules;
  19         67  
347              
348 1         12 my $libpath = $self->notes('lib_path');
349 1         52 $libpath = catfile($libpath, "libbtparse$LIBEXT");
350 1         11 my $libfile = "btparse/src/libbtparse$LIBEXT";
351              
352 1         8 my $extra_linker_flags = "";
353 1 50       27 if ($^O =~ /darwin/) {
    50          
354 0         0 my $abs_path = abs_path($libfile);
355 0         0 $extra_linker_flags = "-install_name $abs_path";
356             } elsif ($LIBEXT eq ".so") {
357 1         6 $extra_linker_flags = "-Wl,-soname,libbtparse$LIBEXT";
358             }
359              
360 1 50       20 if (!$self->up_to_date(\@objects, $libfile)) {
361 1         482 $libbuilder->link(module_name => 'btparse',
362             objects => \@objects,
363             lib_file => $libfile,
364             extra_linker_flags => $extra_linker_flags);
365             }
366              
367 1         79447 my $libdir = catdir($self->blib, 'usrlib');
368 1 50       94 mkpath( $libdir, 0, 0777 ) unless -d $libdir;
369              
370 1         101 $self->copy_if_modified( from => $libfile,
371             to_dir => $libdir,
372             flatten => 1 );
373             }
374              
375             sub ACTION_test {
376 1     1 0 101 my $self = shift;
377              
378 1 50       21 if ($^O =~ /darwin/i) {
    50          
    0          
    0          
    0          
    0          
379 0         0 $ENV{DYLD_LIBRARY_PATH} = catdir($self->blib, "usrlib");
380             }
381             elsif ($^O =~ /(?:linux|bsd|sun|sol|dragonfly|hpux|irix|gnu)/i) {
382 1         24 $ENV{LD_LIBRARY_PATH} = catdir($self->blib, "usrlib");
383             }
384             elsif ($^O =~ /aix/i) {
385 0   0     0 my $oldlibpath = $ENV{LIBPATH} || '/lib:/usr/lib';
386 0         0 $ENV{LIBPATH} = catdir($self->blib, "usrlib").":$oldlibpath";
387             }
388             elsif ($^O =~ /haiku/i) {
389 0   0     0 my $oldlibpath = $ENV{LIBRARY_PATH} || '/boot/system/lib:/boot/system/lib/x86';
390 0         0 $ENV{LIBRARY_PATH} = catdir($self->blib, "usrlib").":$oldlibpath";
391             }
392             elsif ($^O =~ /cygwin/i) {
393             # cygwin uses windows lib searching (PATH instead of LD_LIBRARY_PATH)
394 0         0 my $oldpath = $ENV{PATH};
395 0         0 $ENV{PATH} = catdir($self->blib, "usrlib").":$oldpath";
396             }
397             elsif ($^O =~ /mswin32/i) {
398 0         0 my $oldpath = $ENV{PATH};
399 0         0 $ENV{PATH} = catdir($self->blib, "usrlib").";$oldpath";
400             }
401             $self->SUPER::ACTION_test
402 1         87 }
403              
404              
405             sub _interpolate {
406 1     1   37 my ($from, $to, %config) = @_;
407              
408 1         10 print "Creating new '$to' from '$from'.\n";
409 1 50       79 open FROM, $from or die "Cannot open file '$from' for reading.\n";
410 1 50       106 open TO, ">", $to or die "Cannot open file '$to' for writing.\n";
411 1         45 while () {
412 115         216 s/\[%\s*(\S+)\s*%\]/$config{$1}/ge;
  8         41  
413 115         326 print TO;
414             }
415 1         275 close TO;
416 1         26 close FROM;
417             }
418              
419              
420             1;