| 1 |
package FFI::Build::File::C; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use 5.008004; |
| 6 |
use parent qw( FFI::Build::File::Base ); |
| 7 |
use constant default_suffix => '.c'; |
| 8 |
use constant default_encoding => ':utf8'; |
| 9 |
use Capture::Tiny (); |
| 10 |
use File::Path (); |
| 11 |
use FFI::Build::File::Object; |
| 12 |
|
| 13 |
# ABSTRACT: Class to track C source file in FFI::Build |
| 14 |
our $VERSION = '2.12'; # VERSION |
| 15 |
|
| 16 |
|
| 17 |
sub accept_suffix |
| 18 |
{ |
| 19 |
(qr/\.(c|i)$/) |
| 20 |
} |
| 21 |
|
| 22 |
sub build_item |
| 23 |
{ |
| 24 |
my($self) = @_; |
| 25 |
|
| 26 |
my $oname = $self->basename; |
| 27 |
$oname =~ s/\.(c(xx|pp)|i)?$//; |
| 28 |
$oname .= $self->platform->object_suffix; |
| 29 |
|
| 30 |
my $buildname = '_build'; |
| 31 |
$buildname = $self->build->buildname if $self->build; |
| 32 |
|
| 33 |
my $object = FFI::Build::File::Object->new( |
| 34 |
[ $self->dirname, $buildname, $oname ], |
| 35 |
platform => $self->platform, |
| 36 |
build => $self->build, |
| 37 |
); |
| 38 |
|
| 39 |
return $object if -f $object->path && !$object->needs_rebuild($self->_deps); |
| 40 |
|
| 41 |
File::Path::mkpath($object->dirname, { verbose => 0, mode => oct(700) }); |
| 42 |
|
| 43 |
my @cmd = ( |
| 44 |
$self->_base_args, |
| 45 |
-c => $self->path, |
| 46 |
$self->platform->flag_object_output($object->path), |
| 47 |
); |
| 48 |
|
| 49 |
my($out, $exit) = Capture::Tiny::capture_merged(sub { |
| 50 |
$self->platform->run(@cmd); |
| 51 |
}); |
| 52 |
|
| 53 |
if($exit || !-f $object->path) |
| 54 |
{ |
| 55 |
print $out; |
| 56 |
die "error building $object from $self"; |
| 57 |
} |
| 58 |
elsif($self->build && $self->build->verbose >= 2) |
| 59 |
{ |
| 60 |
print $out; |
| 61 |
} |
| 62 |
elsif($self->build && $self->build->verbose >= 1) |
| 63 |
{ |
| 64 |
print "CC @{[ $self->path ]}\n"; |
| 65 |
} |
| 66 |
|
| 67 |
$object; |
| 68 |
} |
| 69 |
|
| 70 |
sub cc |
| 71 |
{ |
| 72 |
my($self) = @_; |
| 73 |
$self->platform->cc; |
| 74 |
} |
| 75 |
|
| 76 |
sub _base_args |
| 77 |
{ |
| 78 |
my($self) = @_; |
| 79 |
my @cmd = ($self->cc); |
| 80 |
push @cmd, $self->build->cflags_I if $self->build; |
| 81 |
push @cmd, $self->platform->ccflags; |
| 82 |
push @cmd, @{ $self->build->cflags } if $self->build; |
| 83 |
@cmd; |
| 84 |
} |
| 85 |
|
| 86 |
sub _base_args_cpp |
| 87 |
{ |
| 88 |
my($self) = @_; |
| 89 |
my @cmd = ($self->platform->cpp); |
| 90 |
push @cmd, $self->build->cflags_I if $self->build; |
| 91 |
push @cmd, grep /^-[DI]/, $self->platform->ccflags; |
| 92 |
push @cmd, grep /^-D/, @{ $self->build->cflags } if $self->build; |
| 93 |
@cmd; |
| 94 |
} |
| 95 |
|
| 96 |
sub build_item_cpp |
| 97 |
{ |
| 98 |
my($self) = @_; |
| 99 |
|
| 100 |
my $oname = $self->basename; |
| 101 |
$oname =~ s/\.(c(xx|pp)|i)$?$//; |
| 102 |
$oname .= '.i'; |
| 103 |
|
| 104 |
my $buildname = '_build'; |
| 105 |
$buildname = $self->build->buildname if $self->build; |
| 106 |
|
| 107 |
my $ifile = FFI::Build::File::C->new( |
| 108 |
[ $self->dirname, $buildname, $oname ], |
| 109 |
platform => $self->platform, |
| 110 |
build => $self->build, |
| 111 |
); |
| 112 |
|
| 113 |
File::Path::mkpath($ifile->dirname, { verbose => 0, mode => oct(700) }); |
| 114 |
|
| 115 |
my @cmd = ( |
| 116 |
$self->_base_args_cpp, |
| 117 |
$self->path, |
| 118 |
); |
| 119 |
|
| 120 |
my($out, $err, $exit) = Capture::Tiny::capture(sub { |
| 121 |
$self->platform->run(@cmd); |
| 122 |
}); |
| 123 |
|
| 124 |
if($exit) |
| 125 |
{ |
| 126 |
print "[out]\n$out\n" if defined $out && $out ne ''; |
| 127 |
print "[err]\n$err\n" if defined $err && $err ne ''; |
| 128 |
die "error building $ifile from $self"; |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
my $fh; |
| 133 |
open($fh, '>', $ifile->path); |
| 134 |
print $fh $out; |
| 135 |
close $fh; |
| 136 |
} |
| 137 |
|
| 138 |
$ifile; |
| 139 |
} |
| 140 |
|
| 141 |
sub _deps |
| 142 |
{ |
| 143 |
my($self) = @_; |
| 144 |
|
| 145 |
return $self->path unless $self->platform->cc_mm_works; |
| 146 |
|
| 147 |
my @cmd = ( |
| 148 |
$self->_base_args, |
| 149 |
'-MM', |
| 150 |
$self->path, |
| 151 |
); |
| 152 |
|
| 153 |
my($out,$err,$exit) = Capture::Tiny::capture(sub { |
| 154 |
$self->platform->run(@cmd); |
| 155 |
}); |
| 156 |
|
| 157 |
if($exit) |
| 158 |
{ |
| 159 |
print $out; |
| 160 |
print $err; |
| 161 |
warn "error computing dependencies for $self"; |
| 162 |
return ($self->path); |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
$out =~ s/^\+.*\n//; # remove the command line |
| 167 |
# which on windows could have an confusing : |
| 168 |
my(undef, $deps) = split /:/, $out, 2; |
| 169 |
$deps =~ s/^\s+//; |
| 170 |
$deps =~ s/\s+$//; |
| 171 |
return grep !/^\\$/, split /\s+/, $deps; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
1; |
| 176 |
|
| 177 |
__END__ |
| 178 |
|
| 179 |
=pod |
| 180 |
|
| 181 |
=encoding UTF-8 |
| 182 |
|
| 183 |
=head1 NAME |
| 184 |
|
| 185 |
FFI::Build::File::C - Class to track C source file in FFI::Build |
| 186 |
|
| 187 |
=head1 VERSION |
| 188 |
|
| 189 |
version 2.12 |
| 190 |
|
| 191 |
=head1 SYNOPSIS |
| 192 |
|
| 193 |
use FFI::Build::File::C; |
| 194 |
|
| 195 |
my $c = FFI::Build::File::C->new('src/foo.c'); |
| 196 |
|
| 197 |
=head1 DESCRIPTION |
| 198 |
|
| 199 |
File class for C source files. |
| 200 |
|
| 201 |
=head1 AUTHOR |
| 202 |
|
| 203 |
Author: Graham Ollis E<lt>[email protected]<gt> |
| 204 |
|
| 205 |
Contributors: |
| 206 |
|
| 207 |
Bakkiaraj Murugesan (bakkiaraj) |
| 208 |
|
| 209 |
Dylan Cali (calid) |
| 210 |
|
| 211 |
pipcet |
| 212 |
|
| 213 |
Zaki Mughal (zmughal) |
| 214 |
|
| 215 |
Fitz Elliott (felliott) |
| 216 |
|
| 217 |
Vickenty Fesunov (vyf) |
| 218 |
|
| 219 |
Gregor Herrmann (gregoa) |
| 220 |
|
| 221 |
Shlomi Fish (shlomif) |
| 222 |
|
| 223 |
Damyan Ivanov |
| 224 |
|
| 225 |
Ilya Pavlov (Ilya33) |
| 226 |
|
| 227 |
Petr Písař (ppisar) |
| 228 |
|
| 229 |
Mohammad S Anwar (MANWAR) |
| 230 |
|
| 231 |
Håkon Hægland (hakonhagland, HAKONH) |
| 232 |
|
| 233 |
Meredith (merrilymeredith, MHOWARD) |
| 234 |
|
| 235 |
Diab Jerius (DJERIUS) |
| 236 |
|
| 237 |
Eric Brine (IKEGAMI) |
| 238 |
|
| 239 |
szTheory |
| 240 |
|
| 241 |
José Joaquín Atria (JJATRIA) |
| 242 |
|
| 243 |
Pete Houston (openstrike, HOUSTON) |
| 244 |
|
| 245 |
Lukas Mai (MAUKE) |
| 246 |
|
| 247 |
=head1 COPYRIGHT AND LICENSE |
| 248 |
|
| 249 |
This software is copyright (c) 2015-2022 by Graham Ollis. |
| 250 |
|
| 251 |
This is free software; you can redistribute it and/or modify it under |
| 252 |
the same terms as the Perl 5 programming language system itself. |
| 253 |
|
| 254 |
=cut |