| 1 | package ExtUtils::LibBuilder; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||
| 7 | 3 | our $DEBUG = 0; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use base 'ExtUtils::CBuilder'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use File::Temp qw/tempdir/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | ExtUtils::LibBuilder - A tool to build C libraries. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use ExtUtils::LibBuilder; | ||||||||||||||||||||||||||||
| 21 | my $libbuilder = ExtUtils::LibBuilder->new( %options ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Supports all the method from ExtUtils::CBuilder. The following three | ||||||||||||||||||||||||||||
| 26 | methods were adapted to be used in standalone C libraries. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head2 new | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | This method creates a new ExtUtils::LibBuilder object. While it | ||||||||||||||||||||||||||||
| 31 | supports all C<ExtUtils::CBuilder> methods some might work slightly | ||||||||||||||||||||||||||||
| 32 | differently (namely the two below). | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | You can supply to the constructor any option recognized by | ||||||||||||||||||||||||||||
| 35 | C<ExtUtils::CBuilder> constructor. None of them will be used by | ||||||||||||||||||||||||||||
| 36 | C<LibBuilder>. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head2 link | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | $libbuilder -> link( objects => [ "foo.o", "bar.o" ], | ||||||||||||||||||||||||||||
| 41 | module_name => "foobar", | ||||||||||||||||||||||||||||
| 42 | lib_file => "libfoobar$libbuilder->{libext}"); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Options to the link method are the same as the C<CBuilder> | ||||||||||||||||||||||||||||
| 45 | counterpart. Note that the result is a standalone C Library and not a | ||||||||||||||||||||||||||||
| 46 | bundle to be loaded by Perl. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Also, note that you can use the C<libext> key to retrieve from the | ||||||||||||||||||||||||||||
| 49 | object the common library extension on the running system (including | ||||||||||||||||||||||||||||
| 50 | the dot). | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head2 link_executable | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | $libbuilder->link_executable( objects => ["main.o"], | ||||||||||||||||||||||||||||
| 55 | extra_linker_flags => "-L. -lfoobar", | ||||||||||||||||||||||||||||
| 56 | exe_file => "foobarcmd$libbuilder->{exeext}"); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | The C<link_executable> needs, as C<extra_linker_flags> options, the | ||||||||||||||||||||||||||||
| 59 | name of the library and the search path. Future versions might include | ||||||||||||||||||||||||||||
| 60 | better handling on the library files. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Also, note that you can use the C<exeext> key to retrieve from the | ||||||||||||||||||||||||||||
| 63 | object the common executable extension on the running system | ||||||||||||||||||||||||||||
| 64 | (including the dot). | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub new { | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=45.1
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 1 | my %options = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | my $self = bless ExtUtils::CBuilder->new(%options) => $class; | |||||||||||||||||||||||||||
| 73 | # $self->{quiet} = 1; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | $self->{libext} = $^O eq "darwin" ? ".dylib" : ( $^O =~ /win/i ? ".dll" : ".so"); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | $self->{exeext} = $^O =~ /win32/i ? ".exe" : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | $DEBUG && print STDERR "\nTesting Linux\n\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | return $self if $^O !~ /darwin|win32/i && $self->_try; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $^O =~ /darwin|win32/i and $self->_try
MC/DC0%0/2
not $^O =~ /darwin|win32/i and $self->_try
not $^O =~ /darwin|win32/i
$self->_try
Truth table33%1/3
not(A) and B
A= not $^O =~ /darwin|win32/iB= $self->_try
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $DEBUG && print STDERR "\nTesting Darwin\n\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $self->{config}{lddlflags} =~ s/-bundle/-dynamiclib/; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | return $self if $^O !~ /win32/i && $self->_try; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $^O =~ /win32/i and $self->_try
MC/DC0%0/2
not $^O =~ /win32/i and $self->_try
not $^O =~ /win32/i
$self->_try
Truth table0%0/3
not(A) and B
A= not $^O =~ /win32/iB= $self->_try
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $DEBUG && print STDERR "\nTesting Win32\n\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | *link = sub { | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my ($self, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 88 | 0 | my $LD = $self->{config}{ld}; | |||||||||||||||||||||||||||
| 89 | 0 | ▶ | $options{objects} = [$options{objects}] unless ref $options{objects}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | system($LD, "-shared", "-o", | ||||||||||||||||||||||||||||
| 91 | $options{lib_file}, | ||||||||||||||||||||||||||||
| 92 | 0 | @{$options{objects}}); | |||||||||||||||||||||||||||
| 93 | 0 | }; | |||||||||||||||||||||||||||
| 94 | *link_executable = sub { | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my ($self, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 96 | 0 | my $LD = $self->{config}{ld}; | |||||||||||||||||||||||||||
| 97 | 0 | my @CFLAGS = split /\s+/, $options{extra_linker_flags}; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | $options{objects} = [$options{objects}] unless ref $options{objects}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | system($LD, "-o", | ||||||||||||||||||||||||||||
| 100 | $options{exe_file}, | ||||||||||||||||||||||||||||
| 101 | @CFLAGS, | ||||||||||||||||||||||||||||
| 102 | 0 | @{$options{objects}}); | |||||||||||||||||||||||||||
| 103 | 0 | }; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | return $self if $self->_try; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $DEBUG && print STDERR "\nNothing...\n\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | return undef; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub _try { | ||||||||||||||||||||||||||||
| 111 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_try: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 112 | 1 | my $tmp = tempdir CLEANUP => 1; | |||||||||||||||||||||||||||
| 113 | 1 | _write_files($tmp); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1 | my @csources = map { File::Spec->catfile($tmp, $_) } qw'library.c test.c'; | |||||||||||||||||||||||||||
| 116 | 1 | my @cobjects = map { $self->compile( source => $_) } @csources; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | my $libfile = File::Spec->catfile($tmp => "libfoo$self->{libext}"); | |||||||||||||||||||||||||||
| 119 | 1 | my $exefile = File::Spec->catfile($tmp => "foo$self->{exeext}"); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | $self->link( objects => [$cobjects[0]], | |||||||||||||||||||||||||||
| 122 | module_name => "foo", | ||||||||||||||||||||||||||||
| 123 | lib_file => $libfile ); | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | ▶ | return 0 unless -f $libfile; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | $self->link_executable( exe_file => $exefile, | |||||||||||||||||||||||||||
| 128 | extra_linker_flags => "-L$tmp -lfoo", | ||||||||||||||||||||||||||||
| 129 | objects => [$cobjects[1]]); | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | ▶ | return 0 unless -f $exefile && -x _; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-f $exefile and -x _
MC/DC0%0/2
-f $exefile and -x _
-f $exefile
-x _
Truth table33%1/3
A and B
A= -f $exefileB= -x _
| |||||||||||||||||||||||||||||
| 132 | 1 | return 1; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Alberto Simoes, C<< <ambs at cpan.org> >> | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 BUGS | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||
| 142 | C<bug-extutils-libbuilder at rt.cpan.org>, or through the web | ||||||||||||||||||||||||||||
| 143 | interface at | ||||||||||||||||||||||||||||
| 144 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ExtUtils-LibBuilder>. | ||||||||||||||||||||||||||||
| 145 | I will be notified, and then you'll automatically be notified of | ||||||||||||||||||||||||||||
| 146 | progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | perldoc ExtUtils::LibBuilder | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | You can also look for information at: | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =over 4 | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ExtUtils-LibBuilder> | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | L<http://annocpan.org/dist/ExtUtils-LibBuilder> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | L<http://cpanratings.perl.org/d/ExtUtils-LibBuilder> | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | L<http://search.cpan.org/dist/ExtUtils-LibBuilder/> | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =back | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Copyright 2010 Alberto Simoes. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 185 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 186 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =cut | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | sub _write_files { | ||||||||||||||||||||||||||||
| 195 | 1 | ▶ | my $outpath = shift; | ||||||||||||||||||||||||||
|
Subroutine
_write_files: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 196 | 1 | my $fh; | |||||||||||||||||||||||||||
| 197 | 1 | seek DATA, 0, 0; | |||||||||||||||||||||||||||
| 198 | 1 | ▶ | while(<DATA>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 226 | ▶ | if (m!^==\[(.*?)\]==!) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 200 | 2 | my $fname = $1; | |||||||||||||||||||||||||||
| 201 | 2 | $fname = File::Spec->catfile($outpath, $fname); | |||||||||||||||||||||||||||
| 202 | 2 | ▶ | open $fh, ">$fname" or die "Can't create temporary file $fname\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | } elsif ($fh) { | ||||||||||||||||||||||||||||
| 204 | 12 | print $fh $_; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 3 | 1; # End of ExtUtils::LibBuilder | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | __DATA__ | ||||||||||||||||||||||||||||