Debian Codegraph @

lib/FFI/Build/File/Base.pm

raw
1 package FFI::Build::File::Base;
2
3 use strict;
4 use warnings;
5 use 5.008004;
6 use Carp ();
7 use FFI::Temp;
8 use File::Basename ();
9 use FFI::Build::Platform;
10 use FFI::Build::PluginData;
11 use overload '""' => sub { $_[0]->path }, bool => sub { 1 }, fallback => 1;
12
13 # ABSTRACT: Base class for File::Build files
14 our $VERSION = '2.12'; # VERSION
15
16
17 sub new
18 {
19 my($class, $content, %config) = @_;
20
21 my $base = $config{base} || 'ffi_build_';
22 my $dir = $config{dir};
23 my $build = $config{build};
24 my $platform = $config{platform} || FFI::Build::Platform->new;
25
26 my $self = bless {
27 platform => $platform,
28 build => $build,
29 }, $class;
30
31 if(!defined $content)
32 {
33 Carp::croak("content is required");
34 }
35 elsif(ref($content) eq 'ARRAY')
36 {
37 $self->{path} = File::Spec->catfile(@$content);
38 }
39 elsif(ref($content) eq 'SCALAR')
40 {
41 my %args;
42 $args{TEMPLATE} = "${base}XXXXXX";
43 $args{DIR} = $dir if $dir;
44 $args{SUFFIX} = $self->default_suffix;
45 $args{UNLINK} = 0;
46
47 my $fh = $self->{fh} = FFI::Temp->new(%args);
48
49 binmode( $fh, $self->default_encoding );
50 print $fh $$content;
51 close $fh;
52
53 $self->{path} = $fh->filename;
54 $self->{temp} = 1;
55 }
56 elsif(ref($content) eq '')
57 {
58 $self->{path} = $content;
59 }
60
61 if($self->platform->osname eq 'MSWin32')
62 {
63 $self->{native} = File::Spec->catfile($self->{path});
64 $self->{path} =~ s{\\}{/}g;
65 }
66
67 $self;
68 }
69
70
71 sub default_suffix { die "must define a default extension in subclass" }
72 sub default_encoding { die "must define an encoding" }
73 sub accept_suffix { () }
74
75
76 sub path { shift->{path} }
77 sub basename { File::Basename::basename shift->{path} }
78 sub dirname { File::Basename::dirname shift->{path} }
79 sub is_temp { shift->{temp} }
80 sub platform { shift->{platform} }
81 sub build { shift->{build} }
82
83
84 sub native {
85 my($self) = @_;
86 $self->platform->osname eq 'MSWin32'
87 ? $self->{native}
88 : $self->{path};
89 }
90
91
92 sub slurp
93 {
94 my($self) = @_;
95 my $fh;
96 open($fh, '<', $self->path) || Carp::croak "Error opening @{[ $self->path ]} for read $!";
97 binmode($fh, $self->default_encoding);
98 my $content = do { local $/; <$fh> };
99 close $fh;
100 $content;
101 }
102
103
104 sub keep
105 {
106 delete shift->{temp};
107 }
108
109
110 sub build_item
111 {
112 Carp::croak("Not implemented!");
113 }
114
115
116 sub needs_rebuild
117 {
118 my($self, @source) = @_;
119 # if the target doesn't exist, then we definitely
120 # need a rebuild.
121 return 1 unless -f $self->path;
122 my $target_time = [stat $self->path]->[9];
123 foreach my $source (@source)
124 {
125 my $source_time = [stat "$source"]->[9];
126 return 1 if ! defined $source_time;
127 return 1 if $source_time > $target_time;
128 }
129 return 0;
130 }
131
132
133 sub ld
134 {
135 return undef;
136 }
137
138 sub DESTROY
139 {
140 my($self) = @_;
141
142 if($self->{temp})
143 {
144 unlink($self->path);
145 }
146 }
147
148 1;
149
150 __END__
151
152 =pod
153
154 =encoding UTF-8
155
156 =head1 NAME
157
158 FFI::Build::File::Base - Base class for File::Build files
159
160 =head1 VERSION
161
162 version 2.12
163
164 =head1 SYNOPSIS
165
166 Create your own file class
167
168 package FFI::Build::File::Foo;
169 use parent qw( FFI::Build::File::Base );
170 use constant default_suffix => '.foo';
171 use constant default_encoding => ':utf8';
172
173 Use it:
174
175 # use an existing file in the filesystem
176 my $file = FFI::Build::File::Foo->new('src/myfile.foo');
177
178 # generate a temp file with provided content
179 # file will be deletd when $file falls out of scope.
180 my $file = FFI::Build::File::Foo->new(\'content for a temp foo');
181
182 =head1 DESCRIPTION
183
184 This class is the base class for other C<FFI::Build::File::*> classes.
185
186 =head1 CONSTRUCTOR
187
188 =head2 new
189
190 my $file = FFI::Build::File::Base->new(\$content, %options);
191 my $file = FFI::Build::File::Base->new($filename, %options);
192
193 Create a new instance of the file class. You may provide either the
194 content of the file as a scalar reference, or the path to an existing
195 filename. Options:
196
197 =over 4
198
199 =item base
200
201 The base name for any temporary file C<ffi_build_> by default.
202
203 =item build
204
205 The L<FFI::Build> instance to use.
206
207 =item dir
208
209 The directory to store any temporary file.
210
211 =item platform
212
213 The L<FFI::Build::Platform> instance to use.
214
215 =back
216
217 =head1 METHODS
218
219 =head2 default_suffix
220
221 my $suffix = $file->default_suffix;
222
223 B<MUST> be overridden in the subclass. This is the standard extension for the file type. C<.c> for a C file, C<.o> or C<.obj> for an object file depending on platform. etc.
224
225 =head2 default_encoding
226
227 my $encoding = $file->default_encoding;
228
229 B<MUST> be overridden in the subclass. This is the passed to C<binmode> when the file is opened for reading or writing.
230
231 =head2 accept_suffix
232
233 my @suffix_list = $file->accept_suffix;
234
235 Returns a list of regexes that recognize the file type.
236
237 =head2 path
238
239 my $path = $file->path;
240
241 The full or relative path to the file.
242
243 =head2 basename
244
245 my $basename = $file->basename;
246
247 The base filename part of the path.
248
249 =head2 dirname
250
251 my $dir = $file->dirname;
252
253 The directory part of the path.
254
255 =head2 is_temp
256
257 my $bool = $file->is_temp;
258
259 Returns true if the file is temporary, that is, it will be deleted when the file object falls out of scope.
260 You can call C<keep>, to keep the file.
261
262 =head2 platform
263
264 my $platform = $file->platform;
265
266 The L<FFI::Build::Platform> instance used for this file object.
267
268 =head2 build
269
270 my $build = $file->build;
271
272 The L<FFI::Build> instance used for this file object, if any.
273
274 =head2 native
275
276 my $path = $file->native;
277
278 Returns the operating system native version of the filename path. On Windows, this means that forward slash C<\> is
279 used instead of backslash C</>.
280
281 =head2 slurp
282
283 my $content = $file->slurp;
284
285 Returns the content of the file.
286
287 =head2 keep
288
289 $file->keep;
290
291 Turns off the temporary flag on the file object, meaning it will not automatically be deleted when the
292 file object is deallocated or falls out of scope.
293
294 =head2 build_item
295
296 $file->build_item;
297
298 Builds the file into its natural output type, usually an object file. It returns a new file instance,
299 or if the file is an object file then it returns empty list.
300
301 =head2 build_all
302
303 $file->build_all;
304
305 If implemented the file in question can directly create a shared or dynamic library
306 without needing a link step. This is useful for languages that have their own build
307 systems.
308
309 =head2 needs_rebuild
310
311 my $bool = $file->needs_rebuild
312
313 =head2 ld
314
315 =head1 AUTHOR
316
317 Author: Graham Ollis E<lt>[email protected]<gt>
318
319 Contributors:
320
321 Bakkiaraj Murugesan (bakkiaraj)
322
323 Dylan Cali (calid)
324
325 pipcet
326
327 Zaki Mughal (zmughal)
328
329 Fitz Elliott (felliott)
330
331 Vickenty Fesunov (vyf)
332
333 Gregor Herrmann (gregoa)
334
335 Shlomi Fish (shlomif)
336
337 Damyan Ivanov
338
339 Ilya Pavlov (Ilya33)
340
341 Petr Písař (ppisar)
342
343 Mohammad S Anwar (MANWAR)
344
345 Håkon Hægland (hakonhagland, HAKONH)
346
347 Meredith (merrilymeredith, MHOWARD)
348
349 Diab Jerius (DJERIUS)
350
351 Eric Brine (IKEGAMI)
352
353 szTheory
354
355 José Joaquín Atria (JJATRIA)
356
357 Pete Houston (openstrike, HOUSTON)
358
359 Lukas Mai (MAUKE)
360
361 =head1 COPYRIGHT AND LICENSE
362
363 This software is copyright (c) 2015-2022 by Graham Ollis.
364
365 This is free software; you can redistribute it and/or modify it under
366 the same terms as the Perl 5 programming language system itself.
367
368 =cut