Debian Codegraph @

inc/Alien/Base/Wrapper.pm

raw
1 package Alien::Base::Wrapper;
2
3 use strict;
4 use warnings;
5 use 5.006;
6 use Config;
7 use Text::ParseWords qw( shellwords );
8
9 # NOTE: Although this module is now distributed with Alien-Build,
10 # it should have NO non-perl-core dependencies for all Perls
11 # 5.6.0-5.30.1 (as of this writing, and any Perl more recent).
12 # You should be able to extract this module from the rest of
13 # Alien-Build and use it by itself. (There is a dzil plugin
14 # for this [AlienBase::Wrapper::Bundle]
15
16 # ABSTRACT: Compiler and linker wrapper for Alien
17 our $VERSION = '2.84'; # VERSION
18
19
20 sub _join
21 {
22 join ' ',
23 map {
24 my $x = $_;
25 $x =~ s/(\s)/\\$1/g;
26 $x;
27 } @_;
28 }
29
30 sub new
31 {
32 my($class, @aliens) = @_;
33
34 my $export = 1;
35 my $writemakefile = 0;
36
37 my @cflags_I;
38 my @cflags_other;
39 my @ldflags_L;
40 my @ldflags_l;
41 my @ldflags_other;
42 my %requires = (
43 'ExtUtils::MakeMaker' => '6.52',
44 'Alien::Base::Wrapper' => '1.97',
45 );
46
47 foreach my $alien (@aliens)
48 {
49 if($alien eq '!export')
50 {
51 $export = 0;
52 next;
53 }
54 if($alien eq 'WriteMakefile')
55 {
56 $writemakefile = 1;
57 next;
58 }
59 my $version = 0;
60 if($alien =~ s/=(.*)$//)
61 {
62 $version = $1;
63 }
64 $alien = "Alien::$alien" unless $alien =~ /::/;
65 $requires{$alien} = $version;
66 my $alien_pm = $alien . '.pm';
67 $alien_pm =~ s/::/\//g;
68 require $alien_pm unless eval { $alien->can('cflags') } && eval { $alien->can('libs') };
69 my $cflags;
70 my $libs;
71 if($alien->install_type eq 'share' && $alien->can('cflags_static'))
72 {
73 $cflags = $alien->cflags_static;
74 $libs = $alien->libs_static;
75 }
76 else
77 {
78 $cflags = $alien->cflags;
79 $libs = $alien->libs;
80 }
81
82 $cflags = '' unless defined $cflags;
83 $libs = '' unless defined $libs;
84
85 push @cflags_I, grep /^-I/, shellwords $cflags;
86 push @cflags_other, grep !/^-I/, shellwords $cflags;
87
88 push @ldflags_L, grep /^-L/, shellwords $libs;
89 push @ldflags_l, grep /^-l/, shellwords $libs;
90 push @ldflags_other, grep !/^-[Ll]/, shellwords $libs;
91 }
92
93 my @cflags_define = grep /^-D/, @cflags_other;
94 my @cflags_other2 = grep !/^-D/, @cflags_other;
95
96 my @mm;
97
98 push @mm, INC => _join @cflags_I if @cflags_I;
99 push @mm, CCFLAGS => _join(@cflags_other2) . " $Config{ccflags}" if @cflags_other2;
100 push @mm, DEFINE => _join(@cflags_define) if @cflags_define;
101
102 # TODO: handle spaces in -L paths
103 push @mm, LIBS => ["@ldflags_L @ldflags_l"];
104 my @ldflags = (@ldflags_L, @ldflags_other);
105 push @mm, LDDLFLAGS => _join(@ldflags) . " $Config{lddlflags}" if @ldflags;
106 push @mm, LDFLAGS => _join(@ldflags) . " $Config{ldflags}" if @ldflags;
107
108 my @mb;
109
110 push @mb, extra_compiler_flags => _join(@cflags_I, @cflags_other);
111 push @mb, extra_linker_flags => _join(@ldflags_l);
112
113 if(@ldflags)
114 {
115 push @mb, config => {
116 lddlflags => _join(@ldflags) . " $Config{lddlflags}",
117 ldflags => _join(@ldflags) . " $Config{ldflags}",
118 },
119 }
120
121 bless {
122 cflags_I => \@cflags_I,
123 cflags_other => \@cflags_other,
124 ldflags_L => \@ldflags_L,
125 ldflags_l => \@ldflags_l,
126 ldflags_other => \@ldflags_other,
127 mm => \@mm,
128 mb => \@mb,
129 _export => $export,
130 _writemakefile => $writemakefile,
131 requires => \%requires,
132 }, $class;
133 }
134
135 my $default_abw = __PACKAGE__->new;
136
137 # for testing only
138 sub _reset { __PACKAGE__->new }
139
140
141 sub _myexec
142 {
143 my @command = @_;
144 if($^O eq 'MSWin32')
145 {
146 # To handle weird quoting on MSWin32
147 # this logic needs to be improved.
148 my $command = "@command";
149 $command =~ s{"}{\\"}g;
150 system $command;
151
152 if($? == -1 )
153 {
154 die "failed to execute: $!\n";
155 }
156 elsif($? & 127)
157 {
158 die "child died with signal @{[ $? & 128 ]}";
159 }
160 else
161 {
162 exit($? >> 8);
163 }
164 }
165 else
166 {
167 exec @command;
168 }
169 }
170
171 sub cc
172 {
173 my @command = (
174 shellwords($Config{cc}),
175 @{ $default_abw->{cflags_I} },
176 @{ $default_abw->{cflags_other} },
177 @ARGV,
178 );
179 print "@command\n" unless $ENV{ALIEN_BASE_WRAPPER_QUIET};
180 _myexec @command;
181 }
182
183
184 sub ld
185 {
186 my @command = (
187 shellwords($Config{ld}),
188 @{ $default_abw->{ldflags_L} },
189 @{ $default_abw->{ldflags_other} },
190 @ARGV,
191 @{ $default_abw->{ldflags_l} },
192 );
193 print "@command\n" unless $ENV{ALIEN_BASE_WRAPPER_QUIET};
194 _myexec @command;
195 }
196
197
198 sub mm_args
199 {
200 my $self = ref $_[0] ? shift : $default_abw;
201 @{ $self->{mm} };
202 }
203
204
205 sub mm_args2
206 {
207 my $self = shift;
208 $self = $default_abw unless ref $self;
209 my %args = @_;
210
211 my @mm = @{ $self->{mm} };
212
213 while(@mm)
214 {
215 my $key = shift @mm;
216 my $value = shift @mm;
217 if(defined $args{$key})
218 {
219 if($args{$key} eq 'LIBS')
220 {
221 require Carp;
222 # Todo: support this maybe?
223 Carp::croak("please do not specify your own LIBS key with mm_args2");
224 }
225 else
226 {
227 $args{$key} = join ' ', $value, $args{$key};
228 }
229 }
230 else
231 {
232 $args{$key} = $value;
233 }
234 }
235
236 foreach my $module (keys %{ $self->{requires} })
237 {
238 $args{CONFIGURE_REQUIRES}->{$module} = $self->{requires}->{$module};
239 }
240
241 %args;
242 }
243
244
245 sub mb_args
246 {
247 my $self = ref $_[0] ? shift : $default_abw;
248 @{ $self->{mb} };
249 }
250
251 sub import
252 {
253 shift;
254 my $abw = $default_abw = __PACKAGE__->new(@_);
255 if($abw->_export)
256 {
257 my $caller = caller;
258 no strict 'refs';
259 *{"${caller}::cc"} = \&cc;
260 *{"${caller}::ld"} = \&ld;
261 }
262 if($abw->_writemakefile)
263 {
264 my $caller = caller;
265 no strict 'refs';
266 *{"${caller}::WriteMakefile"} = \&WriteMakefile;
267 }
268 }
269
270
271 sub WriteMakefile
272 {
273 my %args = @_;
274
275 require ExtUtils::MakeMaker;
276 ExtUtils::MakeMaker->VERSION('6.52');
277
278 my @aliens;
279
280 if(my $reqs = delete $args{alien_requires})
281 {
282 if(ref $reqs eq 'HASH')
283 {
284 @aliens = map {
285 my $module = $_;
286 my $version = $reqs->{$module};
287 $version ? "$module=$version" : "$module";
288 } sort keys %$reqs;
289 }
290 elsif(ref $reqs eq 'ARRAY')
291 {
292 @aliens = @$reqs;
293 }
294 else
295 {
296 require Carp;
297 Carp::croak("aliens_require must be either a hash or array reference");
298 }
299 }
300 else
301 {
302 require Carp;
303 Carp::croak("You are using Alien::Base::Wrapper::WriteMakefile, but didn't specify any alien requirements");
304 }
305
306 ExtUtils::MakeMaker::WriteMakefile(
307 Alien::Base::Wrapper->new(@aliens)->mm_args2(%args),
308 );
309 }
310
311 sub _export { shift->{_export} }
312 sub _writemakefile { shift->{_writemakefile} }
313
314 1;
315
316 __END__
317
318 =pod
319
320 =encoding UTF-8
321
322 =head1 NAME
323
324 Alien::Base::Wrapper - Compiler and linker wrapper for Alien
325
326 =head1 VERSION
327
328 version 2.84
329
330 =head1 SYNOPSIS
331
332 From the command line:
333
334 % perl -MAlien::Base::Wrapper=Alien::Foo,Alien::Bar -e cc -- -o foo.o -c foo.c
335 % perl -MAlien::Base::Wrapper=Alien::Foo,Alien::Bar -e ld -- -o foo foo.o
336
337 From Makefile.PL (static):
338
339 use ExtUtils::MakeMaker;
340 use Alien::Base::Wrapper ();
341
342 WriteMakefile(
343 Alien::Base::Wrapper->new( 'Alien::Foo', 'Alien::Bar')->mm_args2(
344 'NAME' => 'Foo::XS',
345 'VERSION_FROM' => 'lib/Foo/XS.pm',
346 ),
347 );
348
349 From Makefile.PL (static with wrapper)
350
351 use Alien::Base::Wrapper qw( WriteMakefile);
352
353 WriteMakefile(
354 'NAME' => 'Foo::XS',
355 'VERSION_FROM' => 'lib/Foo/XS.pm',
356 'alien_requires' => {
357 'Alien::Foo' => 0,
358 'Alien::Bar' => 0,
359 },
360 );
361
362 From Makefile.PL (dynamic):
363
364 use Devel::CheckLib qw( check_lib );
365 use ExtUtils::MakeMaker 6.52;
366
367 my @mm_args;
368 my @libs;
369
370 if(check_lib( lib => [ 'foo' ] )
371 {
372 push @mm_args, LIBS => [ '-lfoo' ];
373 }
374 else
375 {
376 push @mm_args,
377 CC => '$(FULLPERL) -MAlien::Base::Wrapper=Alien::Foo -e cc --',
378 LD => '$(FULLPERL) -MAlien::Base::Wrapper=Alien::Foo -e ld --',
379 BUILD_REQUIRES => {
380 'Alien::Foo' => 0,
381 'Alien::Base::Wrapper' => 0,
382 }
383 ;
384 }
385
386 WriteMakefile(
387 'NAME' => 'Foo::XS',
388 'VERSION_FROM' => 'lib/Foo/XS.pm',
389 'CONFIGURE_REQUIRES => {
390 'ExtUtils::MakeMaker' => 6.52,
391 },
392 @mm_args,
393 );
394
395 =head1 DESCRIPTION
396
397 This module acts as a wrapper around one or more L<Alien> modules. It is designed to work
398 with L<Alien::Base> based aliens, but it should work with any L<Alien> which uses the same
399 essential API.
400
401 In the first example (from the command line), this class acts as a wrapper around the
402 compiler and linker that Perl is configured to use. It takes the normal compiler and
403 linker flags and adds the flags provided by the Aliens specified, and then executes the
404 command. It will print the command to the console so that you can see exactly what is
405 happening.
406
407 In the second example (from Makefile.PL non-dynamic), this class is used to generate the
408 appropriate L<ExtUtils::MakeMaker> (EUMM) arguments needed to C<WriteMakefile>.
409
410 In the third example (from Makefile.PL dynamic), we do a quick check to see if the simple
411 linker flag C<-lfoo> will work, if so we use that. If not, we use a wrapper around the
412 compiler and linker that will use the alien flags that are known at build time. The
413 problem that this form attempts to solve is that compiler and linker flags typically
414 need to be determined at I<configure> time, when a distribution is installed, meaning
415 if you are going to use an L<Alien> module then it needs to be a configure prerequisite,
416 even if the library is already installed and easily detected on the operating system.
417
418 The author of this module believes that the third (from Makefile.PL dynamic) form is
419 somewhat unnecessary. L<Alien> modules based on L<Alien::Base> have a few prerequisites,
420 but they are well maintained and reliable, so while there is a small cost in terms of extra
421 dependencies, the overall reliability thanks to reduced overall complexity.
422
423 =head1 CONSTRUCTOR
424
425 =head2 new
426
427 my $abw = Alien::Base::Wrapper->new(@aliens);
428
429 Instead of passing the aliens you want to use into this modules import you can create
430 a non-global instance of C<Alien::Base::Wrapper> using the OO interface.
431
432 =head1 FUNCTIONS
433
434 =head2 cc
435
436 % perl -MAlien::Base::Wrapper=Alien::Foo -e cc -- cflags
437
438 Invoke the C compiler with the appropriate flags from C<Alien::Foo> and what
439 is provided on the command line.
440
441 =head2 ld
442
443 % perl -MAlien::Base::Wrapper=Alien::Foo -e ld -- ldflags
444
445 Invoke the linker with the appropriate flags from C<Alien::Foo> and what
446 is provided on the command line.
447
448 =head2 mm_args
449
450 my %args = $abw->mm_args;
451 my %args = Alien::Base::Wrapper->mm_args;
452
453 Returns arguments that you can pass into C<WriteMakefile> to compile/link against
454 the specified Aliens. Note that this does not set C<CONFIGURE_REQUIRES>. You
455 probably want to use C<mm_args2> below instead for that reason.
456
457 =head2 mm_args2
458
459 my %args = $abw->mm_args2(%args);
460 my %args = Alien::Base::Wrapper->mm_args2(%args);
461
462 Returns arguments that you can pass into C<WriteMakefile> to compile/link against. It works
463 a little differently from C<mm_args> above in that you can pass in arguments. It also adds
464 the appropriate C<CONFIGURE_REQUIRES> for you so you do not have to do that explicitly.
465
466 =head2 mb_args
467
468 my %args = $abw->mb_args;
469 my %args = Alien::Base::Wrapper->mb_args;
470
471 Returns arguments that you can pass into the constructor to L<Module::Build>.
472
473 =head2 WriteMakefile
474
475 use Alien::Base::Wrapper qw( WriteMakefile );
476 WriteMakefile(%args, alien_requires => \%aliens);
477 WriteMakefile(%args, alien_requires => \@aliens);
478
479 This is a thin wrapper around C<WriteMakefile> from L<ExtUtils::MakeMaker>, which adds the
480 given aliens to the configure requirements and sets the appropriate compiler and linker
481 flags.
482
483 If the aliens are specified as a hash reference, then the keys are the module names and the
484 values are the versions. For a list it is just the name of the aliens.
485
486 For the list form you can specify a version by appending C<=version> to the name of the
487 Aliens, that is:
488
489 WriteMakefile(
490 alien_requires => [ 'Alien::libfoo=1.23', 'Alien::libbar=4.56' ],
491 );
492
493 The list form is recommended if the ordering of the aliens matter. The aliens are sorted in
494 the hash form to make it consistent, but it may not be the order that you want.
495
496 =head1 ENVIRONMENT
497
498 Alien::Base::Wrapper responds to these environment variables:
499
500 =over 4
501
502 =item ALIEN_BASE_WRAPPER_QUIET
503
504 If set to true, do not print the command before executing
505
506 =back
507
508 =head1 SEE ALSO
509
510 L<Alien::Base>, L<Alien::Base>
511
512 =head1 AUTHOR
513
514 Author: Graham Ollis E<lt>[email protected]<gt>
515
516 Contributors:
517
518 Diab Jerius (DJERIUS)
519
520 Roy Storey (KIWIROY)
521
522 Ilya Pavlov
523
524 David Mertens (run4flat)
525
526 Mark Nunberg (mordy, mnunberg)
527
528 Christian Walde (Mithaldu)
529
530 Brian Wightman (MidLifeXis)
531
532 Zaki Mughal (zmughal)
533
534 mohawk (mohawk2, ETJ)
535
536 Vikas N Kumar (vikasnkumar)
537
538 Flavio Poletti (polettix)
539
540 Salvador Fandiño (salva)
541
542 Gianni Ceccarelli (dakkar)
543
544 Pavel Shaydo (zwon, trinitum)
545
546 Kang-min Liu (劉康民, gugod)
547
548 Nicholas Shipp (nshp)
549
550 Juan Julián Merelo Guervós (JJ)
551
552 Joel Berger (JBERGER)
553
554 Petr Písař (ppisar)
555
556 Lance Wicks (LANCEW)
557
558 Ahmad Fatoum (a3f, ATHREEF)
559
560 José Joaquín Atria (JJATRIA)
561
562 Duke Leto (LETO)
563
564 Shoichi Kaji (SKAJI)
565
566 Shawn Laffan (SLAFFAN)
567
568 Paul Evans (leonerd, PEVANS)
569
570 Håkon Hægland (hakonhagland, HAKONH)
571
572 nick nauwelaerts (INPHOBIA)
573
574 Florian Weimer
575
576 =head1 COPYRIGHT AND LICENSE
577
578 This software is copyright (c) 2011-2022 by Graham Ollis.
579
580 This is free software; you can redistribute it and/or modify it under
581 the same terms as the Perl 5 programming language system itself.
582
583 =cut