MakeMaker must handle an empty $self->{LIBS} array.
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / MM_Any.pm
1 package ExtUtils::MM_Any;
2
3 use strict;
4 our $VERSION = '6.52';
5
6 use Carp;
7 use File::Spec;
8 use File::Basename;
9 BEGIN { our @ISA = qw(File::Spec); }
10
11 # We need $Verbose
12 use ExtUtils::MakeMaker qw($Verbose);
13
14 use ExtUtils::MakeMaker::Config;
15
16
17 # So we don't have to keep calling the methods over and over again,
18 # we have these globals to cache the values.  Faster and shrtr.
19 my $Curdir  = __PACKAGE__->curdir;
20 my $Rootdir = __PACKAGE__->rootdir;
21 my $Updir   = __PACKAGE__->updir;
22
23
24 =head1 NAME
25
26 ExtUtils::MM_Any - Platform-agnostic MM methods
27
28 =head1 SYNOPSIS
29
30   FOR INTERNAL USE ONLY!
31
32   package ExtUtils::MM_SomeOS;
33
34   # Temporarily, you have to subclass both.  Put MM_Any first.
35   require ExtUtils::MM_Any;
36   require ExtUtils::MM_Unix;
37   @ISA = qw(ExtUtils::MM_Any ExtUtils::Unix);
38
39 =head1 DESCRIPTION
40
41 B<FOR INTERNAL USE ONLY!>
42
43 ExtUtils::MM_Any is a superclass for the ExtUtils::MM_* set of
44 modules.  It contains methods which are either inherently
45 cross-platform or are written in a cross-platform manner.
46
47 Subclass off of ExtUtils::MM_Any I<and> ExtUtils::MM_Unix.  This is a
48 temporary solution.
49
50 B<THIS MAY BE TEMPORARY!>
51
52
53 =head1 METHODS
54
55 Any methods marked I<Abstract> must be implemented by subclasses.
56
57
58 =head2 Cross-platform helper methods
59
60 These are methods which help writing cross-platform code.
61
62
63
64 =head3 os_flavor  I<Abstract>
65
66     my @os_flavor = $mm->os_flavor;
67
68 @os_flavor is the style of operating system this is, usually
69 corresponding to the MM_*.pm file we're using.  
70
71 The first element of @os_flavor is the major family (ie. Unix,
72 Windows, VMS, OS/2, etc...) and the rest are sub families.
73
74 Some examples:
75
76     Cygwin98       ('Unix',  'Cygwin', 'Cygwin9x')
77     Windows NT     ('Win32', 'WinNT')
78     Win98          ('Win32', 'Win9x')
79     Linux          ('Unix',  'Linux')
80     MacOS X        ('Unix',  'Darwin', 'MacOS', 'MacOS X')
81     OS/2           ('OS/2')
82
83 This is used to write code for styles of operating system.  
84 See os_flavor_is() for use.
85
86
87 =head3 os_flavor_is
88
89     my $is_this_flavor = $mm->os_flavor_is($this_flavor);
90     my $is_this_flavor = $mm->os_flavor_is(@one_of_these_flavors);
91
92 Checks to see if the current operating system is one of the given flavors.
93
94 This is useful for code like:
95
96     if( $mm->os_flavor_is('Unix') ) {
97         $out = `foo 2>&1`;
98     }
99     else {
100         $out = `foo`;
101     }
102
103 =cut
104
105 sub os_flavor_is {
106     my $self = shift;
107     my %flavors = map { ($_ => 1) } $self->os_flavor;
108     return (grep { $flavors{$_} } @_) ? 1 : 0;
109 }
110
111
112 =head3 split_command
113
114     my @cmds = $MM->split_command($cmd, @args);
115
116 Most OS have a maximum command length they can execute at once.  Large
117 modules can easily generate commands well past that limit.  Its
118 necessary to split long commands up into a series of shorter commands.
119
120 C<split_command> will return a series of @cmds each processing part of
121 the args.  Collectively they will process all the arguments.  Each
122 individual line in @cmds will not be longer than the
123 $self->max_exec_len being careful to take into account macro expansion.
124
125 $cmd should include any switches and repeated initial arguments.
126
127 If no @args are given, no @cmds will be returned.
128
129 Pairs of arguments will always be preserved in a single command, this
130 is a heuristic for things like pm_to_blib and pod2man which work on
131 pairs of arguments.  This makes things like this safe:
132
133     $self->split_command($cmd, %pod2man);
134
135
136 =cut
137
138 sub split_command {
139     my($self, $cmd, @args) = @_;
140
141     my @cmds = ();
142     return(@cmds) unless @args;
143
144     # If the command was given as a here-doc, there's probably a trailing
145     # newline.
146     chomp $cmd;
147
148     # set aside 30% for macro expansion.
149     my $len_left = int($self->max_exec_len * 0.70);
150     $len_left -= length $self->_expand_macros($cmd);
151
152     do {
153         my $arg_str = '';
154         my @next_args;
155         while( @next_args = splice(@args, 0, 2) ) {
156             # Two at a time to preserve pairs.
157             my $next_arg_str = "\t  ". join ' ', @next_args, "\n";
158
159             if( !length $arg_str ) {
160                 $arg_str .= $next_arg_str
161             }
162             elsif( length($arg_str) + length($next_arg_str) > $len_left ) {
163                 unshift @args, @next_args;
164                 last;
165             }
166             else {
167                 $arg_str .= $next_arg_str;
168             }
169         }
170         chop $arg_str;
171
172         push @cmds, $self->escape_newlines("$cmd \n$arg_str");
173     } while @args;
174
175     return @cmds;
176 }
177
178
179 sub _expand_macros {
180     my($self, $cmd) = @_;
181
182     $cmd =~ s{\$\((\w+)\)}{
183         defined $self->{$1} ? $self->{$1} : "\$($1)"
184     }e;
185     return $cmd;
186 }
187
188
189 =head3 echo
190
191     my @commands = $MM->echo($text);
192     my @commands = $MM->echo($text, $file);
193     my @commands = $MM->echo($text, $file, $appending);
194
195 Generates a set of @commands which print the $text to a $file.
196
197 If $file is not given, output goes to STDOUT.
198
199 If $appending is true the $file will be appended to rather than
200 overwritten.
201
202 =cut
203
204 sub echo {
205     my($self, $text, $file, $appending) = @_;
206     $appending ||= 0;
207
208     my @cmds = map { '$(NOECHO) $(ECHO) '.$self->quote_literal($_) } 
209                split /\n/, $text;
210     if( $file ) {
211         my $redirect = $appending ? '>>' : '>';
212         $cmds[0] .= " $redirect $file";
213         $_ .= " >> $file" foreach @cmds[1..$#cmds];
214     }
215
216     return @cmds;
217 }
218
219
220 =head3 wraplist
221
222   my $args = $mm->wraplist(@list);
223
224 Takes an array of items and turns them into a well-formatted list of
225 arguments.  In most cases this is simply something like:
226
227     FOO \
228     BAR \
229     BAZ
230
231 =cut
232
233 sub wraplist {
234     my $self = shift;
235     return join " \\\n\t", @_;
236 }
237
238
239 =head3 maketext_filter
240
241     my $filter_make_text = $mm->maketext_filter($make_text);
242
243 The text of the Makefile is run through this method before writing to
244 disk.  It allows systems a chance to make portability fixes to the
245 Makefile.
246
247 By default it does nothing.
248
249 This method is protected and not intended to be called outside of
250 MakeMaker.
251
252 =cut
253
254 sub maketext_filter { return $_[1] }
255
256
257 =head3 cd  I<Abstract>
258
259   my $subdir_cmd = $MM->cd($subdir, @cmds);
260
261 This will generate a make fragment which runs the @cmds in the given
262 $dir.  The rough equivalent to this, except cross platform.
263
264   cd $subdir && $cmd
265
266 Currently $dir can only go down one level.  "foo" is fine.  "foo/bar" is
267 not.  "../foo" is right out.
268
269 The resulting $subdir_cmd has no leading tab nor trailing newline.  This
270 makes it easier to embed in a make string.  For example.
271
272       my $make = sprintf <<'CODE', $subdir_cmd;
273   foo :
274       $(ECHO) what
275       %s
276       $(ECHO) mouche
277   CODE
278
279
280 =head3 oneliner  I<Abstract>
281
282   my $oneliner = $MM->oneliner($perl_code);
283   my $oneliner = $MM->oneliner($perl_code, \@switches);
284
285 This will generate a perl one-liner safe for the particular platform
286 you're on based on the given $perl_code and @switches (a -e is
287 assumed) suitable for using in a make target.  It will use the proper
288 shell quoting and escapes.
289
290 $(PERLRUN) will be used as perl.
291
292 Any newlines in $perl_code will be escaped.  Leading and trailing
293 newlines will be stripped.  Makes this idiom much easier:
294
295     my $code = $MM->oneliner(<<'CODE', [...switches...]);
296 some code here
297 another line here
298 CODE
299
300 Usage might be something like:
301
302     # an echo emulation
303     $oneliner = $MM->oneliner('print "Foo\n"');
304     $make = '$oneliner > somefile';
305
306 All dollar signs must be doubled in the $perl_code if you expect them
307 to be interpreted normally, otherwise it will be considered a make
308 macro.  Also remember to quote make macros else it might be used as a
309 bareword.  For example:
310
311     # Assign the value of the $(VERSION_FROM) make macro to $vf.
312     $oneliner = $MM->oneliner('$$vf = "$(VERSION_FROM)"');
313
314 Its currently very simple and may be expanded sometime in the figure
315 to include more flexible code and switches.
316
317
318 =head3 quote_literal  I<Abstract>
319
320     my $safe_text = $MM->quote_literal($text);
321
322 This will quote $text so it is interpreted literally in the shell.
323
324 For example, on Unix this would escape any single-quotes in $text and
325 put single-quotes around the whole thing.
326
327
328 =head3 escape_newlines  I<Abstract>
329
330     my $escaped_text = $MM->escape_newlines($text);
331
332 Shell escapes newlines in $text.
333
334
335 =head3 max_exec_len  I<Abstract>
336
337     my $max_exec_len = $MM->max_exec_len;
338
339 Calculates the maximum command size the OS can exec.  Effectively,
340 this is the max size of a shell command line.
341
342 =for _private
343 $self->{_MAX_EXEC_LEN} is set by this method, but only for testing purposes.
344
345
346 =head3 make
347
348     my $make = $MM->make;
349
350 Returns the make variant we're generating the Makefile for.  This attempts
351 to do some normalization on the information from %Config or the user.
352
353 =cut
354
355 sub make {
356     my $self = shift;
357
358     my $make = lc $self->{MAKE};
359
360     # Truncate anything like foomake6 to just foomake.
361     $make =~ s/^(\w+make).*/$1/;
362
363     # Turn gnumake into gmake.
364     $make =~ s/^gnu/g/;
365
366     return $make;
367 }
368
369
370 =head2 Targets
371
372 These are methods which produce make targets.
373
374
375 =head3 all_target
376
377 Generate the default target 'all'.
378
379 =cut
380
381 sub all_target {
382     my $self = shift;
383
384     return <<'MAKE_EXT';
385 all :: pure_all
386         $(NOECHO) $(NOOP)
387 MAKE_EXT
388
389 }
390
391
392 =head3 blibdirs_target
393
394     my $make_frag = $mm->blibdirs_target;
395
396 Creates the blibdirs target which creates all the directories we use
397 in blib/.
398
399 The blibdirs.ts target is deprecated.  Depend on blibdirs instead.
400
401
402 =cut
403
404 sub blibdirs_target {
405     my $self = shift;
406
407     my @dirs = map { uc "\$(INST_$_)" } qw(libdir archlib
408                                            autodir archautodir
409                                            bin script
410                                            man1dir man3dir
411                                           );
412
413     my @exists = map { $_.'$(DFSEP).exists' } @dirs;
414
415     my $make = sprintf <<'MAKE', join(' ', @exists);
416 blibdirs : %s
417         $(NOECHO) $(NOOP)
418
419 # Backwards compat with 6.18 through 6.25
420 blibdirs.ts : blibdirs
421         $(NOECHO) $(NOOP)
422
423 MAKE
424
425     $make .= $self->dir_target(@dirs);
426
427     return $make;
428 }
429
430
431 =head3 clean (o)
432
433 Defines the clean target.
434
435 =cut
436
437 sub clean {
438 # --- Cleanup and Distribution Sections ---
439
440     my($self, %attribs) = @_;
441     my @m;
442     push(@m, '
443 # Delete temporary files but do not touch installed files. We don\'t delete
444 # the Makefile here so a later make realclean still has a makefile to use.
445
446 clean :: clean_subdirs
447 ');
448
449     my @files = values %{$self->{XS}}; # .c files from *.xs files
450     my @dirs  = qw(blib);
451
452     # Normally these are all under blib but they might have been
453     # redefined.
454     # XXX normally this would be a good idea, but the Perl core sets
455     # INST_LIB = ../../lib rather than actually installing the files.
456     # So a "make clean" in an ext/ directory would blow away lib.
457     # Until the core is adjusted let's leave this out.
458 #     push @dirs, qw($(INST_ARCHLIB) $(INST_LIB)
459 #                    $(INST_BIN) $(INST_SCRIPT)
460 #                    $(INST_MAN1DIR) $(INST_MAN3DIR)
461 #                    $(INST_LIBDIR) $(INST_ARCHLIBDIR) $(INST_AUTODIR) 
462 #                    $(INST_STATIC) $(INST_DYNAMIC) $(INST_BOOT)
463 #                 );
464                   
465
466     if( $attribs{FILES} ) {
467         # Use @dirs because we don't know what's in here.
468         push @dirs, ref $attribs{FILES}                ?
469                         @{$attribs{FILES}}             :
470                         split /\s+/, $attribs{FILES}   ;
471     }
472
473     push(@files, qw[$(MAKE_APERL_FILE) 
474                     perlmain.c tmon.out mon.out so_locations 
475                     blibdirs.ts pm_to_blib pm_to_blib.ts
476                     *$(OBJ_EXT) *$(LIB_EXT) perl.exe perl perl$(EXE_EXT)
477                     $(BOOTSTRAP) $(BASEEXT).bso
478                     $(BASEEXT).def lib$(BASEEXT).def
479                     $(BASEEXT).exp $(BASEEXT).x
480                    ]);
481
482     push(@files, $self->catfile('$(INST_ARCHAUTODIR)','extralibs.all'));
483     push(@files, $self->catfile('$(INST_ARCHAUTODIR)','extralibs.ld'));
484
485     # core files
486     push(@files, qw[core core.*perl.*.? *perl.core]);
487     push(@files, map { "core." . "[0-9]"x$_ } (1..5));
488
489     # OS specific things to clean up.  Use @dirs since we don't know
490     # what might be in here.
491     push @dirs, $self->extra_clean_files;
492
493     # Occasionally files are repeated several times from different sources
494     { my(%f) = map { ($_ => 1) } @files; @files = keys %f; }
495     { my(%d) = map { ($_ => 1) } @dirs;  @dirs  = keys %d; }
496
497     push @m, map "\t$_\n", $self->split_command('- $(RM_F)',  @files);
498     push @m, map "\t$_\n", $self->split_command('- $(RM_RF)', @dirs);
499
500     # Leave Makefile.old around for realclean
501     push @m, <<'MAKE';
502         - $(MV) $(FIRST_MAKEFILE) $(MAKEFILE_OLD) $(DEV_NULL)
503 MAKE
504
505     push(@m, "\t$attribs{POSTOP}\n")   if $attribs{POSTOP};
506
507     join("", @m);
508 }
509
510
511 =head3 clean_subdirs_target
512
513   my $make_frag = $MM->clean_subdirs_target;
514
515 Returns the clean_subdirs target.  This is used by the clean target to
516 call clean on any subdirectories which contain Makefiles.
517
518 =cut
519
520 sub clean_subdirs_target {
521     my($self) = shift;
522
523     # No subdirectories, no cleaning.
524     return <<'NOOP_FRAG' unless @{$self->{DIR}};
525 clean_subdirs :
526         $(NOECHO) $(NOOP)
527 NOOP_FRAG
528
529
530     my $clean = "clean_subdirs :\n";
531
532     for my $dir (@{$self->{DIR}}) {
533         my $subclean = $self->oneliner(sprintf <<'CODE', $dir);
534 chdir '%s';  system '$(MAKE) clean' if -f '$(FIRST_MAKEFILE)';
535 CODE
536
537         $clean .= "\t$subclean\n";
538     }
539
540     return $clean;
541 }
542
543
544 =head3 dir_target
545
546     my $make_frag = $mm->dir_target(@directories);
547
548 Generates targets to create the specified directories and set its
549 permission to PERM_DIR.
550
551 Because depending on a directory to just ensure it exists doesn't work
552 too well (the modified time changes too often) dir_target() creates a
553 .exists file in the created directory.  It is this you should depend on.
554 For portability purposes you should use the $(DIRFILESEP) macro rather
555 than a '/' to seperate the directory from the file.
556
557     yourdirectory$(DIRFILESEP).exists
558
559 =cut
560
561 sub dir_target {
562     my($self, @dirs) = @_;
563
564     my $make = '';
565     foreach my $dir (@dirs) {
566         $make .= sprintf <<'MAKE', ($dir) x 7;
567 %s$(DFSEP).exists :: Makefile.PL
568         $(NOECHO) $(MKPATH) %s
569         $(NOECHO) $(CHMOD) $(PERM_DIR) %s
570         $(NOECHO) $(TOUCH) %s$(DFSEP).exists
571
572 MAKE
573
574     }
575
576     return $make;
577 }
578
579
580 =head3 distdir
581
582 Defines the scratch directory target that will hold the distribution
583 before tar-ing (or shar-ing).
584
585 =cut
586
587 # For backwards compatibility.
588 *dist_dir = *distdir;
589
590 sub distdir {
591     my($self) = shift;
592
593     my $meta_target = $self->{NO_META} ? '' : 'distmeta';
594     my $sign_target = !$self->{SIGN}   ? '' : 'distsignature';
595
596     return sprintf <<'MAKE_FRAG', $meta_target, $sign_target;
597 create_distdir :
598         $(RM_RF) $(DISTVNAME)
599         $(PERLRUN) "-MExtUtils::Manifest=manicopy,maniread" \
600                 -e "manicopy(maniread(),'$(DISTVNAME)', '$(DIST_CP)');"
601
602 distdir : create_distdir %s %s
603         $(NOECHO) $(NOOP)
604
605 MAKE_FRAG
606
607 }
608
609
610 =head3 dist_test
611
612 Defines a target that produces the distribution in the
613 scratchdirectory, and runs 'perl Makefile.PL; make ;make test' in that
614 subdirectory.
615
616 =cut
617
618 sub dist_test {
619     my($self) = shift;
620
621     my $mpl_args = join " ", map qq["$_"], @ARGV;
622
623     my $test = $self->cd('$(DISTVNAME)',
624                          '$(ABSPERLRUN) Makefile.PL '.$mpl_args,
625                          '$(MAKE) $(PASTHRU)',
626                          '$(MAKE) test $(PASTHRU)'
627                         );
628
629     return sprintf <<'MAKE_FRAG', $test;
630 disttest : distdir
631         %s
632
633 MAKE_FRAG
634
635
636 }
637
638
639 =head3 dynamic (o)
640
641 Defines the dynamic target.
642
643 =cut
644
645 sub dynamic {
646 # --- Dynamic Loading Sections ---
647
648     my($self) = shift;
649     '
650 dynamic :: $(FIRST_MAKEFILE) $(INST_DYNAMIC) $(INST_BOOT)
651         $(NOECHO) $(NOOP)
652 ';
653 }
654
655
656 =head3 makemakerdflt_target
657
658   my $make_frag = $mm->makemakerdflt_target
659
660 Returns a make fragment with the makemakerdeflt_target specified.
661 This target is the first target in the Makefile, is the default target
662 and simply points off to 'all' just in case any make variant gets
663 confused or something gets snuck in before the real 'all' target.
664
665 =cut
666
667 sub makemakerdflt_target {
668     return <<'MAKE_FRAG';
669 makemakerdflt : all
670         $(NOECHO) $(NOOP)
671 MAKE_FRAG
672
673 }
674
675
676 =head3 manifypods_target
677
678   my $manifypods_target = $self->manifypods_target;
679
680 Generates the manifypods target.  This target generates man pages from
681 all POD files in MAN1PODS and MAN3PODS.
682
683 =cut
684
685 sub manifypods_target {
686     my($self) = shift;
687
688     my $man1pods      = '';
689     my $man3pods      = '';
690     my $dependencies  = '';
691
692     # populate manXpods & dependencies:
693     foreach my $name (keys %{$self->{MAN1PODS}}, keys %{$self->{MAN3PODS}}) {
694         $dependencies .= " \\\n\t$name";
695     }
696
697     my $manify = <<END;
698 manifypods : pure_all $dependencies
699 END
700
701     my @man_cmds;
702     foreach my $section (qw(1 3)) {
703         my $pods = $self->{"MAN${section}PODS"};
704         push @man_cmds, $self->split_command(<<CMD, %$pods);
705         \$(NOECHO) \$(POD2MAN) --section=$section --perm_rw=\$(PERM_RW)
706 CMD
707     }
708
709     $manify .= "\t\$(NOECHO) \$(NOOP)\n" unless @man_cmds;
710     $manify .= join '', map { "$_\n" } @man_cmds;
711
712     return $manify;
713 }
714
715
716 =head3 metafile_target
717
718     my $target = $mm->metafile_target;
719
720 Generate the metafile target.
721
722 Writes the file META.yml YAML encoded meta-data about the module in
723 the distdir.  The format follows Module::Build's as closely as
724 possible.
725
726 =cut
727
728 sub metafile_target {
729     my $self = shift;
730
731     return <<'MAKE_FRAG' if $self->{NO_META};
732 metafile :
733         $(NOECHO) $(NOOP)
734 MAKE_FRAG
735
736     my @metadata   = $self->metafile_data(
737         $self->{META_ADD}   || {},
738         $self->{META_MERGE} || {},
739     );
740     my $meta       = $self->metafile_file(@metadata);
741     my @write_meta = $self->echo($meta, 'META_new.yml');
742
743     return sprintf <<'MAKE_FRAG', join("\n\t", @write_meta);
744 metafile : create_distdir
745         $(NOECHO) $(ECHO) Generating META.yml
746         %s
747         -$(NOECHO) $(MV) META_new.yml $(DISTVNAME)/META.yml
748 MAKE_FRAG
749
750 }
751
752
753 =begin private
754
755 =head3 _sort_pairs
756
757     my @pairs = _sort_pairs($sort_sub, \%hash);
758
759 Sorts the pairs of a hash based on keys ordered according 
760 to C<$sort_sub>.
761
762 =end private
763
764 =cut
765
766 sub _sort_pairs {
767     my $sort  = shift;
768     my $pairs = shift;
769     return map  { $_ => $pairs->{$_} }
770            sort $sort
771            keys %$pairs;
772 }
773
774
775 # Taken from Module::Build::Base
776 sub _hash_merge {
777     my ($self, $h, $k, $v) = @_;
778     if (ref $h->{$k} eq 'ARRAY') {
779         push @{$h->{$k}}, ref $v ? @$v : $v;
780     } elsif (ref $h->{$k} eq 'HASH') {
781         $self->_hash_merge($h->{$k}, $_, $v->{$_}) foreach keys %$v;
782     } else {
783         $h->{$k} = $v;
784     }
785 }
786
787
788 =head3 metafile_data
789
790     my @metadata_pairs = $mm->metafile_data(\%meta_add, \%meta_merge);
791
792 Returns the data which MakeMaker turns into the META.yml file.
793
794 Values of %meta_add will overwrite any existing metadata in those
795 keys.  %meta_merge will be merged with them.
796
797 =cut
798
799 sub metafile_data {
800     my $self = shift;
801     my($meta_add, $meta_merge) = @_;
802
803     # The order in which standard meta keys should be written.
804     my @meta_order = qw(
805         name
806         version
807         abstract
808         author
809         license
810         distribution_type
811
812         configure_requires
813         build_requires
814         requires
815
816         resources
817
818         provides
819         no_index
820
821         generated_by
822         meta-spec
823     );
824
825     my $configure_requires;
826     if( $self->{CONFIGURE_REQUIRES} and ref($self->{CONFIGURE_REQUIRES}) eq 'HASH' ) {
827         $configure_requires = $self->{CONFIGURE_REQUIRES};
828     } else {
829         $configure_requires = {
830             'ExtUtils::MakeMaker'       => 0,
831         };
832     }
833
834     my %meta = (
835         name         => $self->{DISTNAME},
836         version      => $self->{VERSION},
837         abstract     => $self->{ABSTRACT},
838         license      => $self->{LICENSE} || 'unknown',
839         distribution_type => $self->{PM} ? 'module' : 'script',
840
841         configure_requires => $configure_requires,
842
843         build_requires => {
844             'ExtUtils::MakeMaker'       => 0
845         },
846
847         no_index     => {
848             directory   => [qw(t inc)]
849         },
850
851         generated_by => "ExtUtils::MakeMaker version $ExtUtils::MakeMaker::VERSION",
852         'meta-spec'  => {
853             url         => 'http://module-build.sourceforge.net/META-spec-v1.4.html', 
854             version     => 1.4
855         },
856     );
857
858     # The author key is required and it takes a list.
859     $meta{author}   = defined $self->{AUTHOR}    ? [$self->{AUTHOR}] : [];
860
861     $meta{requires} = $self->{PREREQ_PM} if defined $self->{PREREQ_PM};
862     $meta{requires}{perl} = $self->{MIN_PERL_VERSION} if $self->{MIN_PERL_VERSION};
863
864     while( my($key, $val) = each %$meta_add ) {
865         $meta{$key} = $val;
866     }
867
868     while( my($key, $val) = each %$meta_merge ) {
869         $self->_hash_merge(\%meta, $key, $val);
870     }
871
872     my @meta_pairs;
873
874     # Put the standard keys first in the proper order.
875     for my $key (@meta_order) {
876         next unless exists $meta{$key};
877
878         push @meta_pairs, $key, delete $meta{$key};
879     }
880
881     # Then tack everything else onto the end, alpha sorted.
882     for my $key (sort {lc $a cmp lc $b} keys %meta) {
883         push @meta_pairs, $key, $meta{$key};
884     }
885
886     return @meta_pairs
887 }
888
889 =begin private
890
891 =head3 _dump_hash
892
893     $yaml = _dump_hash(\%options, %hash);
894
895 Implements a fake YAML dumper for a hash given
896 as a list of pairs. No quoting/escaping is done. Keys
897 are supposed to be strings. Values are undef, strings, 
898 hash refs or array refs of strings.
899
900 Supported options are:
901
902     delta => STR - indentation delta
903     use_header => BOOL - whether to include a YAML header
904     indent => STR - a string of spaces 
905           default: ''
906
907     max_key_length => INT - maximum key length used to align
908         keys and values of the same hash
909         default: 20
910     key_sort => CODE - a sort sub 
911             It may be undef, which means no sorting by keys
912         default: sub { lc $a cmp lc $b }
913
914     customs => HASH - special options for certain keys 
915            (whose values are hashes themselves)
916         may contain: max_key_length, key_sort, customs
917
918 =end private
919
920 =cut
921
922 sub _dump_hash {
923     croak "first argument should be a hash ref" unless ref $_[0] eq 'HASH';
924     my $options = shift;
925     my %hash = @_;
926
927     # Use a list to preserve order.
928     my @pairs;
929
930     my $k_sort 
931         = exists $options->{key_sort} ? $options->{key_sort} 
932                                       : sub { lc $a cmp lc $b };
933     if ($k_sort) {
934         croak "'key_sort' should be a coderef" unless ref $k_sort eq 'CODE';
935         @pairs = _sort_pairs($k_sort, \%hash);
936     } else { # list of pairs, no sorting
937         @pairs = @_;
938     }
939
940     my $yaml     = $options->{use_header} ? "--- #YAML:1.0\n" : '';
941     my $indent   = $options->{indent} || '';
942     my $k_length = min(
943         ($options->{max_key_length} || 20),
944         max(map { length($_) + 1 } grep { !ref $hash{$_} } keys %hash)
945     );
946     my $customs  = $options->{customs} || {};
947
948     # printf format for key
949     my $k_format = "%-${k_length}s";
950
951     while( @pairs ) {
952         my($key, $val) = splice @pairs, 0, 2;
953         $val = '~' unless defined $val;
954         if(ref $val eq 'HASH') {
955             if ( keys %$val ) {
956                 my %k_options = ( # options for recursive call
957                     delta => $options->{delta},
958                     use_header => 0,
959                     indent => $indent . $options->{delta},
960                 );
961                 if (exists $customs->{$key}) {
962                     my %k_custom = %{$customs->{$key}};
963                     foreach my $k qw(key_sort max_key_length customs) {
964                         $k_options{$k} = $k_custom{$k} if exists $k_custom{$k};
965                     }
966                 }
967                 $yaml .= $indent . "$key:\n" 
968                   . _dump_hash(\%k_options, %$val);
969             }
970             else {
971                 $yaml .= $indent . "$key:  {}\n";
972             }
973         }
974         elsif (ref $val eq 'ARRAY') {
975             if( @$val ) {
976                 $yaml .= $indent . "$key:\n";
977
978                 for (@$val) {
979                     croak "only nested arrays of non-refs are supported" if ref $_;
980                     $yaml .= $indent . $options->{delta} . "- $_\n";
981                 }
982             }
983             else {
984                 $yaml .= $indent . "$key:  []\n";
985             }
986         }
987         elsif( ref $val and !blessed($val) ) {
988             croak "only nested hashes, arrays and objects are supported";
989         }
990         else {  # if it's an object, just stringify it
991             $yaml .= $indent . sprintf "$k_format  %s\n", "$key:", $val;
992         }
993     };
994
995     return $yaml;
996
997 }
998
999 sub blessed {
1000     return eval { $_[0]->isa("UNIVERSAL"); };
1001 }
1002
1003 sub max {
1004     return (sort { $b <=> $a } @_)[0];
1005 }
1006
1007 sub min {
1008     return (sort { $a <=> $b } @_)[0];
1009 }
1010
1011 =head3 metafile_file
1012
1013     my $meta_yml = $mm->metafile_file(@metadata_pairs);
1014
1015 Turns the @metadata_pairs into YAML.
1016
1017 This method does not implement a complete YAML dumper, being limited
1018 to dump a hash with values which are strings, undef's or nested hashes
1019 and arrays of strings. No quoting/escaping is done.
1020
1021 =cut
1022
1023 sub metafile_file {
1024     my $self = shift;
1025
1026     my %dump_options = (
1027         use_header => 1, 
1028         delta      => ' ' x 4, 
1029         key_sort   => undef,
1030     );
1031     return _dump_hash(\%dump_options, @_);
1032
1033 }
1034
1035
1036 =head3 distmeta_target
1037
1038     my $make_frag = $mm->distmeta_target;
1039
1040 Generates the distmeta target to add META.yml to the MANIFEST in the
1041 distdir.
1042
1043 =cut
1044
1045 sub distmeta_target {
1046     my $self = shift;
1047
1048     my $add_meta = $self->oneliner(<<'CODE', ['-MExtUtils::Manifest=maniadd']);
1049 eval { maniadd({q{META.yml} => q{Module meta-data (added by MakeMaker)}}) } 
1050     or print "Could not add META.yml to MANIFEST: $${'@'}\n"
1051 CODE
1052
1053     my $add_meta_to_distdir = $self->cd('$(DISTVNAME)', $add_meta);
1054
1055     return sprintf <<'MAKE', $add_meta_to_distdir;
1056 distmeta : create_distdir metafile
1057         $(NOECHO) %s
1058
1059 MAKE
1060
1061 }
1062
1063
1064 =head3 realclean (o)
1065
1066 Defines the realclean target.
1067
1068 =cut
1069
1070 sub realclean {
1071     my($self, %attribs) = @_;
1072
1073     my @dirs  = qw($(DISTVNAME));
1074     my @files = qw($(FIRST_MAKEFILE) $(MAKEFILE_OLD));
1075
1076     # Special exception for the perl core where INST_* is not in blib.
1077     # This cleans up the files built from the ext/ directory (all XS).
1078     if( $self->{PERL_CORE} ) {
1079         push @dirs, qw($(INST_AUTODIR) $(INST_ARCHAUTODIR));
1080         push @files, values %{$self->{PM}};
1081     }
1082
1083     if( $self->has_link_code ){
1084         push @files, qw($(OBJECT));
1085     }
1086
1087     if( $attribs{FILES} ) {
1088         if( ref $attribs{FILES} ) {
1089             push @dirs, @{ $attribs{FILES} };
1090         }
1091         else {
1092             push @dirs, split /\s+/, $attribs{FILES};
1093         }
1094     }
1095
1096     # Occasionally files are repeated several times from different sources
1097     { my(%f) = map { ($_ => 1) } @files;  @files = keys %f; }
1098     { my(%d) = map { ($_ => 1) } @dirs;   @dirs  = keys %d; }
1099
1100     my $rm_cmd  = join "\n\t", map { "$_" } 
1101                     $self->split_command('- $(RM_F)',  @files);
1102     my $rmf_cmd = join "\n\t", map { "$_" } 
1103                     $self->split_command('- $(RM_RF)', @dirs);
1104
1105     my $m = sprintf <<'MAKE', $rm_cmd, $rmf_cmd;
1106 # Delete temporary files (via clean) and also delete dist files
1107 realclean purge ::  clean realclean_subdirs
1108         %s
1109         %s
1110 MAKE
1111
1112     $m .= "\t$attribs{POSTOP}\n" if $attribs{POSTOP};
1113
1114     return $m;
1115 }
1116
1117
1118 =head3 realclean_subdirs_target
1119
1120   my $make_frag = $MM->realclean_subdirs_target;
1121
1122 Returns the realclean_subdirs target.  This is used by the realclean
1123 target to call realclean on any subdirectories which contain Makefiles.
1124
1125 =cut
1126
1127 sub realclean_subdirs_target {
1128     my $self = shift;
1129
1130     return <<'NOOP_FRAG' unless @{$self->{DIR}};
1131 realclean_subdirs :
1132         $(NOECHO) $(NOOP)
1133 NOOP_FRAG
1134
1135     my $rclean = "realclean_subdirs :\n";
1136
1137     foreach my $dir (@{$self->{DIR}}) {
1138         foreach my $makefile ('$(MAKEFILE_OLD)', '$(FIRST_MAKEFILE)' ) {
1139             my $subrclean .= $self->oneliner(sprintf <<'CODE', $dir, ($makefile) x 2);
1140 chdir '%s';  system '$(MAKE) $(USEMAKEFILE) %s realclean' if -f '%s';
1141 CODE
1142
1143             $rclean .= sprintf <<'RCLEAN', $subrclean;
1144         - %s
1145 RCLEAN
1146
1147         }
1148     }
1149
1150     return $rclean;
1151 }
1152
1153
1154 =head3 signature_target
1155
1156     my $target = $mm->signature_target;
1157
1158 Generate the signature target.
1159
1160 Writes the file SIGNATURE with "cpansign -s".
1161
1162 =cut
1163
1164 sub signature_target {
1165     my $self = shift;
1166
1167     return <<'MAKE_FRAG';
1168 signature :
1169         cpansign -s
1170 MAKE_FRAG
1171
1172 }
1173
1174
1175 =head3 distsignature_target
1176
1177     my $make_frag = $mm->distsignature_target;
1178
1179 Generates the distsignature target to add SIGNATURE to the MANIFEST in the
1180 distdir.
1181
1182 =cut
1183
1184 sub distsignature_target {
1185     my $self = shift;
1186
1187     my $add_sign = $self->oneliner(<<'CODE', ['-MExtUtils::Manifest=maniadd']);
1188 eval { maniadd({q{SIGNATURE} => q{Public-key signature (added by MakeMaker)}}) } 
1189     or print "Could not add SIGNATURE to MANIFEST: $${'@'}\n"
1190 CODE
1191
1192     my $sign_dist        = $self->cd('$(DISTVNAME)' => 'cpansign -s');
1193
1194     # cpansign -s complains if SIGNATURE is in the MANIFEST yet does not
1195     # exist
1196     my $touch_sig        = $self->cd('$(DISTVNAME)' => '$(TOUCH) SIGNATURE');
1197     my $add_sign_to_dist = $self->cd('$(DISTVNAME)' => $add_sign );
1198
1199     return sprintf <<'MAKE', $add_sign_to_dist, $touch_sig, $sign_dist
1200 distsignature : create_distdir
1201         $(NOECHO) %s
1202         $(NOECHO) %s
1203         %s
1204
1205 MAKE
1206
1207 }
1208
1209
1210 =head3 special_targets
1211
1212   my $make_frag = $mm->special_targets
1213
1214 Returns a make fragment containing any targets which have special
1215 meaning to make.  For example, .SUFFIXES and .PHONY.
1216
1217 =cut
1218
1219 sub special_targets {
1220     my $make_frag = <<'MAKE_FRAG';
1221 .SUFFIXES : .xs .c .C .cpp .i .s .cxx .cc $(OBJ_EXT)
1222
1223 .PHONY: all config static dynamic test linkext manifest blibdirs clean realclean disttest distdir
1224
1225 MAKE_FRAG
1226
1227     $make_frag .= <<'MAKE_FRAG' if $ENV{CLEARCASE_ROOT};
1228 .NO_CONFIG_REC: Makefile
1229
1230 MAKE_FRAG
1231
1232     return $make_frag;
1233 }
1234
1235
1236
1237
1238 =head2 Init methods
1239
1240 Methods which help initialize the MakeMaker object and macros.
1241
1242
1243 =head3 init_ABSTRACT
1244
1245     $mm->init_ABSTRACT
1246
1247 =cut
1248
1249 sub init_ABSTRACT {
1250     my $self = shift;
1251
1252     if( $self->{ABSTRACT_FROM} and $self->{ABSTRACT} ) {
1253         warn "Both ABSTRACT_FROM and ABSTRACT are set.  ".
1254              "Ignoring ABSTRACT_FROM.\n";
1255         return;
1256     }
1257
1258     if ($self->{ABSTRACT_FROM}){
1259         $self->{ABSTRACT} = $self->parse_abstract($self->{ABSTRACT_FROM}) or
1260             carp "WARNING: Setting ABSTRACT via file ".
1261                  "'$self->{ABSTRACT_FROM}' failed\n";
1262     }
1263 }
1264
1265 =head3 init_INST
1266
1267     $mm->init_INST;
1268
1269 Called by init_main.  Sets up all INST_* variables except those related
1270 to XS code.  Those are handled in init_xs.
1271
1272 =cut
1273
1274 sub init_INST {
1275     my($self) = shift;
1276
1277     $self->{INST_ARCHLIB} ||= $self->catdir($Curdir,"blib","arch");
1278     $self->{INST_BIN}     ||= $self->catdir($Curdir,'blib','bin');
1279
1280     # INST_LIB typically pre-set if building an extension after
1281     # perl has been built and installed. Setting INST_LIB allows
1282     # you to build directly into, say $Config{privlibexp}.
1283     unless ($self->{INST_LIB}){
1284         if ($self->{PERL_CORE}) {
1285             if (defined $Cross::platform) {
1286                 $self->{INST_LIB} = $self->{INST_ARCHLIB} = 
1287                   $self->catdir($self->{PERL_LIB},"..","xlib",
1288                                      $Cross::platform);
1289             }
1290             else {
1291                 $self->{INST_LIB} = $self->{INST_ARCHLIB} = $self->{PERL_LIB};
1292             }
1293         } else {
1294             $self->{INST_LIB} = $self->catdir($Curdir,"blib","lib");
1295         }
1296     }
1297
1298     my @parentdir = split(/::/, $self->{PARENT_NAME});
1299     $self->{INST_LIBDIR}      = $self->catdir('$(INST_LIB)',     @parentdir);
1300     $self->{INST_ARCHLIBDIR}  = $self->catdir('$(INST_ARCHLIB)', @parentdir);
1301     $self->{INST_AUTODIR}     = $self->catdir('$(INST_LIB)', 'auto', 
1302                                               '$(FULLEXT)');
1303     $self->{INST_ARCHAUTODIR} = $self->catdir('$(INST_ARCHLIB)', 'auto',
1304                                               '$(FULLEXT)');
1305
1306     $self->{INST_SCRIPT}  ||= $self->catdir($Curdir,'blib','script');
1307
1308     $self->{INST_MAN1DIR} ||= $self->catdir($Curdir,'blib','man1');
1309     $self->{INST_MAN3DIR} ||= $self->catdir($Curdir,'blib','man3');
1310
1311     return 1;
1312 }
1313
1314
1315 =head3 init_INSTALL
1316
1317     $mm->init_INSTALL;
1318
1319 Called by init_main.  Sets up all INSTALL_* variables (except
1320 INSTALLDIRS) and *PREFIX.
1321
1322 =cut
1323
1324 sub init_INSTALL {
1325     my($self) = shift;
1326
1327     if( $self->{ARGS}{INSTALL_BASE} and $self->{ARGS}{PREFIX} ) {
1328         die "Only one of PREFIX or INSTALL_BASE can be given.  Not both.\n";
1329     }
1330
1331     if( $self->{ARGS}{INSTALL_BASE} ) {
1332         $self->init_INSTALL_from_INSTALL_BASE;
1333     }
1334     else {
1335         $self->init_INSTALL_from_PREFIX;
1336     }
1337 }
1338
1339
1340 =head3 init_INSTALL_from_PREFIX
1341
1342   $mm->init_INSTALL_from_PREFIX;
1343
1344 =cut
1345
1346 sub init_INSTALL_from_PREFIX {
1347     my $self = shift;
1348
1349     $self->init_lib2arch;
1350
1351     # There are often no Config.pm defaults for these new man variables so 
1352     # we fall back to the old behavior which is to use installman*dir
1353     foreach my $num (1, 3) {
1354         my $k = 'installsiteman'.$num.'dir';
1355
1356         $self->{uc $k} ||= uc "\$(installman${num}dir)"
1357           unless $Config{$k};
1358     }
1359
1360     foreach my $num (1, 3) {
1361         my $k = 'installvendorman'.$num.'dir';
1362
1363         unless( $Config{$k} ) {
1364             $self->{uc $k}  ||= $Config{usevendorprefix}
1365                               ? uc "\$(installman${num}dir)"
1366                               : '';
1367         }
1368     }
1369
1370     $self->{INSTALLSITEBIN} ||= '$(INSTALLBIN)'
1371       unless $Config{installsitebin};
1372     $self->{INSTALLSITESCRIPT} ||= '$(INSTALLSCRIPT)'
1373       unless $Config{installsitescript};
1374
1375     unless( $Config{installvendorbin} ) {
1376         $self->{INSTALLVENDORBIN} ||= $Config{usevendorprefix} 
1377                                     ? $Config{installbin}
1378                                     : '';
1379     }
1380     unless( $Config{installvendorscript} ) {
1381         $self->{INSTALLVENDORSCRIPT} ||= $Config{usevendorprefix}
1382                                        ? $Config{installscript}
1383                                        : '';
1384     }
1385
1386
1387     my $iprefix = $Config{installprefixexp} || $Config{installprefix} || 
1388                   $Config{prefixexp}        || $Config{prefix} || '';
1389     my $vprefix = $Config{usevendorprefix}  ? $Config{vendorprefixexp} : '';
1390     my $sprefix = $Config{siteprefixexp}    || '';
1391
1392     # 5.005_03 doesn't have a siteprefix.
1393     $sprefix = $iprefix unless $sprefix;
1394
1395
1396     $self->{PREFIX}       ||= '';
1397
1398     if( $self->{PREFIX} ) {
1399         @{$self}{qw(PERLPREFIX SITEPREFIX VENDORPREFIX)} =
1400           ('$(PREFIX)') x 3;
1401     }
1402     else {
1403         $self->{PERLPREFIX}   ||= $iprefix;
1404         $self->{SITEPREFIX}   ||= $sprefix;
1405         $self->{VENDORPREFIX} ||= $vprefix;
1406
1407         # Lots of MM extension authors like to use $(PREFIX) so we
1408         # put something sensible in there no matter what.
1409         $self->{PREFIX} = '$('.uc $self->{INSTALLDIRS}.'PREFIX)';
1410     }
1411
1412     my $arch    = $Config{archname};
1413     my $version = $Config{version};
1414
1415     # default style
1416     my $libstyle = $Config{installstyle} || 'lib/perl5';
1417     my $manstyle = '';
1418
1419     if( $self->{LIBSTYLE} ) {
1420         $libstyle = $self->{LIBSTYLE};
1421         $manstyle = $self->{LIBSTYLE} eq 'lib/perl5' ? 'lib/perl5' : '';
1422     }
1423
1424     # Some systems, like VOS, set installman*dir to '' if they can't
1425     # read man pages.
1426     for my $num (1, 3) {
1427         $self->{'INSTALLMAN'.$num.'DIR'} ||= 'none'
1428           unless $Config{'installman'.$num.'dir'};
1429     }
1430
1431     my %bin_layouts = 
1432     (
1433         bin         => { s => $iprefix,
1434                          t => 'perl',
1435                          d => 'bin' },
1436         vendorbin   => { s => $vprefix,
1437                          t => 'vendor',
1438                          d => 'bin' },
1439         sitebin     => { s => $sprefix,
1440                          t => 'site',
1441                          d => 'bin' },
1442         script      => { s => $iprefix,
1443                          t => 'perl',
1444                          d => 'bin' },
1445         vendorscript=> { s => $vprefix,
1446                          t => 'vendor',
1447                          d => 'bin' },
1448         sitescript  => { s => $sprefix,
1449                          t => 'site',
1450                          d => 'bin' },
1451     );
1452     
1453     my %man_layouts =
1454     (
1455         man1dir         => { s => $iprefix,
1456                              t => 'perl',
1457                              d => 'man/man1',
1458                              style => $manstyle, },
1459         siteman1dir     => { s => $sprefix,
1460                              t => 'site',
1461                              d => 'man/man1',
1462                              style => $manstyle, },
1463         vendorman1dir   => { s => $vprefix,
1464                              t => 'vendor',
1465                              d => 'man/man1',
1466                              style => $manstyle, },
1467
1468         man3dir         => { s => $iprefix,
1469                              t => 'perl',
1470                              d => 'man/man3',
1471                              style => $manstyle, },
1472         siteman3dir     => { s => $sprefix,
1473                              t => 'site',
1474                              d => 'man/man3',
1475                              style => $manstyle, },
1476         vendorman3dir   => { s => $vprefix,
1477                              t => 'vendor',
1478                              d => 'man/man3',
1479                              style => $manstyle, },
1480     );
1481
1482     my %lib_layouts =
1483     (
1484         privlib     => { s => $iprefix,
1485                          t => 'perl',
1486                          d => '',
1487                          style => $libstyle, },
1488         vendorlib   => { s => $vprefix,
1489                          t => 'vendor',
1490                          d => '',
1491                          style => $libstyle, },
1492         sitelib     => { s => $sprefix,
1493                          t => 'site',
1494                          d => 'site_perl',
1495                          style => $libstyle, },
1496         
1497         archlib     => { s => $iprefix,
1498                          t => 'perl',
1499                          d => "$version/$arch",
1500                          style => $libstyle },
1501         vendorarch  => { s => $vprefix,
1502                          t => 'vendor',
1503                          d => "$version/$arch",
1504                          style => $libstyle },
1505         sitearch    => { s => $sprefix,
1506                          t => 'site',
1507                          d => "site_perl/$version/$arch",
1508                          style => $libstyle },
1509     );
1510
1511
1512     # Special case for LIB.
1513     if( $self->{LIB} ) {
1514         foreach my $var (keys %lib_layouts) {
1515             my $Installvar = uc "install$var";
1516
1517             if( $var =~ /arch/ ) {
1518                 $self->{$Installvar} ||= 
1519                   $self->catdir($self->{LIB}, $Config{archname});
1520             }
1521             else {
1522                 $self->{$Installvar} ||= $self->{LIB};
1523             }
1524         }
1525     }
1526
1527     my %type2prefix = ( perl    => 'PERLPREFIX',
1528                         site    => 'SITEPREFIX',
1529                         vendor  => 'VENDORPREFIX'
1530                       );
1531
1532     my %layouts = (%bin_layouts, %man_layouts, %lib_layouts);
1533     while( my($var, $layout) = each(%layouts) ) {
1534         my($s, $t, $d, $style) = @{$layout}{qw(s t d style)};
1535         my $r = '$('.$type2prefix{$t}.')';
1536
1537         print STDERR "Prefixing $var\n" if $Verbose >= 2;
1538
1539         my $installvar = "install$var";
1540         my $Installvar = uc $installvar;
1541         next if $self->{$Installvar};
1542
1543         $d = "$style/$d" if $style;
1544         $self->prefixify($installvar, $s, $r, $d);
1545
1546         print STDERR "  $Installvar == $self->{$Installvar}\n" 
1547           if $Verbose >= 2;
1548     }
1549
1550     # Generate these if they weren't figured out.
1551     $self->{VENDORARCHEXP} ||= $self->{INSTALLVENDORARCH};
1552     $self->{VENDORLIBEXP}  ||= $self->{INSTALLVENDORLIB};
1553
1554     return 1;
1555 }
1556
1557
1558 =head3 init_from_INSTALL_BASE
1559
1560     $mm->init_from_INSTALL_BASE
1561
1562 =cut
1563
1564 my %map = (
1565            lib      => [qw(lib perl5)],
1566            arch     => [('lib', 'perl5', $Config{archname})],
1567            bin      => [qw(bin)],
1568            man1dir  => [qw(man man1)],
1569            man3dir  => [qw(man man3)]
1570           );
1571 $map{script} = $map{bin};
1572
1573 sub init_INSTALL_from_INSTALL_BASE {
1574     my $self = shift;
1575
1576     @{$self}{qw(PREFIX VENDORPREFIX SITEPREFIX PERLPREFIX)} = 
1577                                                          '$(INSTALL_BASE)';
1578
1579     my %install;
1580     foreach my $thing (keys %map) {
1581         foreach my $dir (('', 'SITE', 'VENDOR')) {
1582             my $uc_thing = uc $thing;
1583             my $key = "INSTALL".$dir.$uc_thing;
1584
1585             $install{$key} ||= 
1586               $self->catdir('$(INSTALL_BASE)', @{$map{$thing}});
1587         }
1588     }
1589
1590     # Adjust for variable quirks.
1591     $install{INSTALLARCHLIB} ||= delete $install{INSTALLARCH};
1592     $install{INSTALLPRIVLIB} ||= delete $install{INSTALLLIB};
1593
1594     foreach my $key (keys %install) {
1595         $self->{$key} ||= $install{$key};
1596     }
1597
1598     return 1;
1599 }
1600
1601
1602 =head3 init_VERSION  I<Abstract>
1603
1604     $mm->init_VERSION
1605
1606 Initialize macros representing versions of MakeMaker and other tools
1607
1608 MAKEMAKER: path to the MakeMaker module.
1609
1610 MM_VERSION: ExtUtils::MakeMaker Version
1611
1612 MM_REVISION: ExtUtils::MakeMaker version control revision (for backwards 
1613              compat)
1614
1615 VERSION: version of your module
1616
1617 VERSION_MACRO: which macro represents the version (usually 'VERSION')
1618
1619 VERSION_SYM: like version but safe for use as an RCS revision number
1620
1621 DEFINE_VERSION: -D line to set the module version when compiling
1622
1623 XS_VERSION: version in your .xs file.  Defaults to $(VERSION)
1624
1625 XS_VERSION_MACRO: which macro represents the XS version.
1626
1627 XS_DEFINE_VERSION: -D line to set the xs version when compiling.
1628
1629 Called by init_main.
1630
1631 =cut
1632
1633 sub init_VERSION {
1634     my($self) = shift;
1635
1636     $self->{MAKEMAKER}  = $ExtUtils::MakeMaker::Filename;
1637     $self->{MM_VERSION} = $ExtUtils::MakeMaker::VERSION;
1638     $self->{MM_REVISION}= $ExtUtils::MakeMaker::Revision;
1639     $self->{VERSION_FROM} ||= '';
1640
1641     if ($self->{VERSION_FROM}){
1642         $self->{VERSION} = $self->parse_version($self->{VERSION_FROM});
1643         if( $self->{VERSION} eq 'undef' ) {
1644             carp("WARNING: Setting VERSION via file ".
1645                  "'$self->{VERSION_FROM}' failed\n");
1646         }
1647     }
1648
1649     # strip blanks
1650     if (defined $self->{VERSION}) {
1651         $self->{VERSION} =~ s/^\s+//;
1652         $self->{VERSION} =~ s/\s+$//;
1653     }
1654     else {
1655         $self->{VERSION} = '';
1656     }
1657
1658
1659     $self->{VERSION_MACRO}  = 'VERSION';
1660     ($self->{VERSION_SYM} = $self->{VERSION}) =~ s/\W/_/g;
1661     $self->{DEFINE_VERSION} = '-D$(VERSION_MACRO)=\"$(VERSION)\"';
1662
1663
1664     # Graham Barr and Paul Marquess had some ideas how to ensure
1665     # version compatibility between the *.pm file and the
1666     # corresponding *.xs file. The bottomline was, that we need an
1667     # XS_VERSION macro that defaults to VERSION:
1668     $self->{XS_VERSION} ||= $self->{VERSION};
1669
1670     $self->{XS_VERSION_MACRO}  = 'XS_VERSION';
1671     $self->{XS_DEFINE_VERSION} = '-D$(XS_VERSION_MACRO)=\"$(XS_VERSION)\"';
1672
1673 }
1674
1675
1676 =head3 init_others
1677
1678     $MM->init_others();
1679
1680 Initializes the macro definitions used by tools_other() and places them
1681 in the $MM object.
1682
1683 If there is no description, its the same as the parameter to
1684 WriteMakefile() documented in ExtUtils::MakeMaker.
1685
1686 Defines at least these macros.
1687
1688   Macro             Description
1689
1690   NOOP              Do nothing
1691   NOECHO            Tell make not to display the command itself
1692
1693   MAKEFILE
1694   FIRST_MAKEFILE
1695   MAKEFILE_OLD
1696   MAKE_APERL_FILE   File used by MAKE_APERL
1697
1698   SHELL             Program used to run shell commands
1699
1700   ECHO              Print text adding a newline on the end
1701   RM_F              Remove a file 
1702   RM_RF             Remove a directory          
1703   TOUCH             Update a file's timestamp   
1704   TEST_F            Test for a file's existence 
1705   CP                Copy a file                 
1706   MV                Move a file                 
1707   CHMOD             Change permissions on a file
1708   FALSE             Exit with non-zero
1709   TRUE              Exit with zero
1710
1711   UMASK_NULL        Nullify umask
1712   DEV_NULL          Suppress all command output
1713
1714 =cut
1715
1716 sub init_others {
1717     my $self = shift;
1718
1719     $self->{ECHO}     ||= $self->oneliner('print qq{@ARGV}', ['-l']);
1720     $self->{ECHO_N}   ||= $self->oneliner('print qq{@ARGV}');
1721
1722     $self->{TOUCH}    ||= $self->oneliner('touch', ["-MExtUtils::Command"]);
1723     $self->{CHMOD}    ||= $self->oneliner('chmod', ["-MExtUtils::Command"]);
1724     $self->{RM_F}     ||= $self->oneliner('rm_f',  ["-MExtUtils::Command"]);
1725     $self->{RM_RF}    ||= $self->oneliner('rm_rf', ["-MExtUtils::Command"]);
1726     $self->{TEST_F}   ||= $self->oneliner('test_f', ["-MExtUtils::Command"]);
1727     $self->{FALSE}    ||= $self->oneliner('exit 1');
1728     $self->{TRUE}     ||= $self->oneliner('exit 0');
1729
1730     $self->{MKPATH}   ||= $self->oneliner('mkpath', ["-MExtUtils::Command"]);
1731
1732     $self->{CP}       ||= $self->oneliner('cp', ["-MExtUtils::Command"]);
1733     $self->{MV}       ||= $self->oneliner('mv', ["-MExtUtils::Command"]);
1734
1735     $self->{MOD_INSTALL} ||= 
1736       $self->oneliner(<<'CODE', ['-MExtUtils::Install']);
1737 install([ from_to => {@ARGV}, verbose => '$(VERBINST)', uninstall_shadows => '$(UNINST)', dir_mode => '$(PERM_DIR)' ]);
1738 CODE
1739     $self->{DOC_INSTALL} ||= $self->oneliner('perllocal_install', ["-MExtUtils::Command::MM"]);
1740     $self->{UNINSTALL}   ||= $self->oneliner('uninstall', ["-MExtUtils::Command::MM"]);
1741     $self->{WARN_IF_OLD_PACKLIST} ||= 
1742       $self->oneliner('warn_if_old_packlist', ["-MExtUtils::Command::MM"]);
1743     $self->{FIXIN}       ||= $self->oneliner('MY->fixin(shift)', ["-MExtUtils::MY"]);
1744     $self->{EQUALIZE_TIMESTAMP} ||= $self->oneliner('eqtime', ["-MExtUtils::Command"]);
1745
1746     $self->{UNINST}     ||= 0;
1747     $self->{VERBINST}   ||= 0;
1748
1749     $self->{FIRST_MAKEFILE}     ||= $self->{MAKEFILE} || 'Makefile';
1750     $self->{MAKEFILE}           ||= $self->{FIRST_MAKEFILE};
1751     $self->{MAKEFILE_OLD}       ||= $self->{MAKEFILE}.'.old';
1752     $self->{MAKE_APERL_FILE}    ||= $self->{MAKEFILE}.'.aperl';
1753
1754     # Not everybody uses -f to indicate "use this Makefile instead"
1755     $self->{USEMAKEFILE}        ||= '-f';
1756
1757     # Some makes require a wrapper around macros passed in on the command 
1758     # line.
1759     $self->{MACROSTART}         ||= '';
1760     $self->{MACROEND}           ||= '';
1761
1762     $self->{SHELL}              ||= $Config{sh};
1763
1764     # UMASK_NULL is not used by MakeMaker but some CPAN modules
1765     # make use of it.
1766     $self->{UMASK_NULL}         ||= "umask 0";
1767
1768     # Not the greatest default, but its something.
1769     $self->{DEV_NULL}           ||= "> /dev/null 2>&1";
1770
1771     $self->{NOOP}               ||= '$(TRUE)';
1772     $self->{NOECHO}             = '@' unless defined $self->{NOECHO};
1773
1774     $self->{LD_RUN_PATH} = "";
1775
1776     # Compute EXTRALIBS, BSLOADLIBS and LDLOADLIBS from $self->{LIBS}
1777     # Lets look at $self->{LIBS} carefully: It may be an anon array, a string or
1778     # undefined. In any case we turn it into an anon array:
1779
1780     # May check $Config{libs} too, thus not empty.
1781     $self->{LIBS} = [$self->{LIBS}] unless ref $self->{LIBS};
1782
1783     $self->{LIBS} = [''] unless @{$self->{LIBS}} && defined $self->{LIBS}[0];
1784
1785     foreach my $libs ( @{$self->{LIBS}} ){
1786         $libs =~ s/^\s*(.*\S)\s*$/$1/; # remove leading and trailing whitespace
1787         my(@libs) = $self->extliblist($libs);
1788         if ($libs[0] or $libs[1] or $libs[2]){
1789             # LD_RUN_PATH now computed by ExtUtils::Liblist
1790             ($self->{EXTRALIBS},  $self->{BSLOADLIBS}, 
1791              $self->{LDLOADLIBS}, $self->{LD_RUN_PATH}) = @libs;
1792             last;
1793         }
1794     }
1795
1796     if ( $self->{OBJECT} ) {
1797         $self->{OBJECT} =~ s!\.o(bj)?\b!\$(OBJ_EXT)!g;
1798     } else {
1799         # init_dirscan should have found out, if we have C files
1800         $self->{OBJECT} = "";
1801         $self->{OBJECT} = '$(BASEEXT)$(OBJ_EXT)' if @{$self->{C}||[]};
1802     }
1803     $self->{OBJECT} =~ s/\n+/ \\\n\t/g;
1804
1805     $self->{BOOTDEP}  = (-f "$self->{BASEEXT}_BS") ? "$self->{BASEEXT}_BS" : "";
1806     $self->{PERLMAINCC} ||= '$(CC)';
1807     $self->{LDFROM} = '$(OBJECT)' unless $self->{LDFROM};
1808
1809     # Sanity check: don't define LINKTYPE = dynamic if we're skipping
1810     # the 'dynamic' section of MM.  We don't have this problem with
1811     # 'static', since we either must use it (%Config says we can't
1812     # use dynamic loading) or the caller asked for it explicitly.
1813     if (!$self->{LINKTYPE}) {
1814        $self->{LINKTYPE} = $self->{SKIPHASH}{'dynamic'}
1815                         ? 'static'
1816                         : ($Config{usedl} ? 'dynamic' : 'static');
1817     }
1818
1819     return 1;
1820 }
1821
1822
1823 =head3 tools_other
1824
1825     my $make_frag = $MM->tools_other;
1826
1827 Returns a make fragment containing definitions for the macros init_others() 
1828 initializes.
1829
1830 =cut
1831
1832 sub tools_other {
1833     my($self) = shift;
1834     my @m;
1835
1836     # We set PM_FILTER as late as possible so it can see all the earlier
1837     # on macro-order sensitive makes such as nmake.
1838     for my $tool (qw{ SHELL CHMOD CP MV NOOP NOECHO RM_F RM_RF TEST_F TOUCH 
1839                       UMASK_NULL DEV_NULL MKPATH EQUALIZE_TIMESTAMP
1840                       FALSE TRUE
1841                       ECHO ECHO_N
1842                       UNINST VERBINST
1843                       MOD_INSTALL DOC_INSTALL UNINSTALL
1844                       WARN_IF_OLD_PACKLIST
1845                       MACROSTART MACROEND
1846                       USEMAKEFILE
1847                       PM_FILTER
1848                       FIXIN
1849                     } ) 
1850     {
1851         next unless defined $self->{$tool};
1852         push @m, "$tool = $self->{$tool}\n";
1853     }
1854
1855     return join "", @m;
1856 }
1857
1858
1859 =head3 init_DIRFILESEP  I<Abstract>
1860
1861   $MM->init_DIRFILESEP;
1862   my $dirfilesep = $MM->{DIRFILESEP};
1863
1864 Initializes the DIRFILESEP macro which is the seperator between the
1865 directory and filename in a filepath.  ie. / on Unix, \ on Win32 and
1866 nothing on VMS.
1867
1868 For example:
1869
1870     # instead of $(INST_ARCHAUTODIR)/extralibs.ld
1871     $(INST_ARCHAUTODIR)$(DIRFILESEP)extralibs.ld
1872
1873 Something of a hack but it prevents a lot of code duplication between
1874 MM_* variants.
1875
1876 Do not use this as a seperator between directories.  Some operating
1877 systems use different seperators between subdirectories as between
1878 directories and filenames (for example:  VOLUME:[dir1.dir2]file on VMS).
1879
1880 =head3 init_linker  I<Abstract>
1881
1882     $mm->init_linker;
1883
1884 Initialize macros which have to do with linking.
1885
1886 PERL_ARCHIVE: path to libperl.a equivalent to be linked to dynamic
1887 extensions.
1888
1889 PERL_ARCHIVE_AFTER: path to a library which should be put on the
1890 linker command line I<after> the external libraries to be linked to
1891 dynamic extensions.  This may be needed if the linker is one-pass, and
1892 Perl includes some overrides for C RTL functions, such as malloc().
1893
1894 EXPORT_LIST: name of a file that is passed to linker to define symbols
1895 to be exported.
1896
1897 Some OSes do not need these in which case leave it blank.
1898
1899
1900 =head3 init_platform
1901
1902     $mm->init_platform
1903
1904 Initialize any macros which are for platform specific use only.
1905
1906 A typical one is the version number of your OS specific mocule.
1907 (ie. MM_Unix_VERSION or MM_VMS_VERSION).
1908
1909 =cut
1910
1911 sub init_platform {
1912     return '';
1913 }
1914
1915
1916 =head3 init_MAKE
1917
1918     $mm->init_MAKE
1919
1920 Initialize MAKE from either a MAKE environment variable or $Config{make}.
1921
1922 =cut
1923
1924 sub init_MAKE {
1925     my $self = shift;
1926
1927     $self->{MAKE} ||= $ENV{MAKE} || $Config{make};
1928 }
1929
1930
1931 =head2 Tools
1932
1933 A grab bag of methods to generate specific macros and commands.
1934
1935
1936
1937 =head3 manifypods
1938
1939 Defines targets and routines to translate the pods into manpages and
1940 put them into the INST_* directories.
1941
1942 =cut
1943
1944 sub manifypods {
1945     my $self          = shift;
1946
1947     my $POD2MAN_macro = $self->POD2MAN_macro();
1948     my $manifypods_target = $self->manifypods_target();
1949
1950     return <<END_OF_TARGET;
1951
1952 $POD2MAN_macro
1953
1954 $manifypods_target
1955
1956 END_OF_TARGET
1957
1958 }
1959
1960
1961 =head3 POD2MAN_macro
1962
1963   my $pod2man_macro = $self->POD2MAN_macro
1964
1965 Returns a definition for the POD2MAN macro.  This is a program
1966 which emulates the pod2man utility.  You can add more switches to the
1967 command by simply appending them on the macro.
1968
1969 Typical usage:
1970
1971     $(POD2MAN) --section=3 --perm_rw=$(PERM_RW) podfile1 man_page1 ...
1972
1973 =cut
1974
1975 sub POD2MAN_macro {
1976     my $self = shift;
1977
1978 # Need the trailing '--' so perl stops gobbling arguments and - happens
1979 # to be an alternative end of line seperator on VMS so we quote it
1980     return <<'END_OF_DEF';
1981 POD2MAN_EXE = $(PERLRUN) "-MExtUtils::Command::MM" -e pod2man "--"
1982 POD2MAN = $(POD2MAN_EXE)
1983 END_OF_DEF
1984 }
1985
1986
1987 =head3 test_via_harness
1988
1989   my $command = $mm->test_via_harness($perl, $tests);
1990
1991 Returns a $command line which runs the given set of $tests with
1992 Test::Harness and the given $perl.
1993
1994 Used on the t/*.t files.
1995
1996 =cut
1997
1998 sub test_via_harness {
1999     my($self, $perl, $tests) = @_;
2000
2001     return qq{\t$perl "-MExtUtils::Command::MM" }.
2002            qq{"-e" "test_harness(\$(TEST_VERBOSE), '\$(INST_LIB)', '\$(INST_ARCHLIB)')" $tests\n};
2003 }
2004
2005 =head3 test_via_script
2006
2007   my $command = $mm->test_via_script($perl, $script);
2008
2009 Returns a $command line which just runs a single test without
2010 Test::Harness.  No checks are done on the results, they're just
2011 printed.
2012
2013 Used for test.pl, since they don't always follow Test::Harness
2014 formatting.
2015
2016 =cut
2017
2018 sub test_via_script {
2019     my($self, $perl, $script) = @_;
2020     return qq{\t$perl "-I\$(INST_LIB)" "-I\$(INST_ARCHLIB)" $script\n};
2021 }
2022
2023
2024 =head3 tool_autosplit
2025
2026 Defines a simple perl call that runs autosplit. May be deprecated by
2027 pm_to_blib soon.
2028
2029 =cut
2030
2031 sub tool_autosplit {
2032     my($self, %attribs) = @_;
2033
2034     my $maxlen = $attribs{MAXLEN} ? '$$AutoSplit::Maxlen=$attribs{MAXLEN};' 
2035                                   : '';
2036
2037     my $asplit = $self->oneliner(sprintf <<'PERL_CODE', $maxlen);
2038 use AutoSplit; %s autosplit($$ARGV[0], $$ARGV[1], 0, 1, 1)
2039 PERL_CODE
2040
2041     return sprintf <<'MAKE_FRAG', $asplit;
2042 # Usage: $(AUTOSPLITFILE) FileToSplit AutoDirToSplitInto
2043 AUTOSPLITFILE = %s
2044
2045 MAKE_FRAG
2046
2047 }
2048
2049
2050 =head3 arch_check
2051
2052     my $arch_ok = $mm->arch_check(
2053         $INC{"Config.pm"},
2054         File::Spec->catfile($Config{archlibexp}, "Config.pm")
2055     );
2056
2057 A sanity check that what Perl thinks the architecture is and what
2058 Config thinks the architecture is are the same.  If they're not it
2059 will return false and show a diagnostic message.
2060
2061 When building Perl it will always return true, as nothing is installed
2062 yet.
2063
2064 The interface is a bit odd because this is the result of a
2065 quick refactoring.  Don't rely on it.
2066
2067 =cut
2068
2069 sub arch_check {
2070     my $self = shift;
2071     my($pconfig, $cconfig) = @_;
2072
2073     return 1 if $self->{PERL_SRC};
2074
2075     my($pvol, $pthinks) = $self->splitpath($pconfig);
2076     my($cvol, $cthinks) = $self->splitpath($cconfig);
2077
2078     $pthinks = $self->canonpath($pthinks);
2079     $cthinks = $self->canonpath($cthinks);
2080
2081     my $ret = 1;
2082     if ($pthinks ne $cthinks) {
2083         print "Have $pthinks\n";
2084         print "Want $cthinks\n";
2085
2086         $ret = 0;
2087
2088         my $arch = (grep length, $self->splitdir($pthinks))[-1];
2089
2090         print STDOUT <<END unless $self->{UNINSTALLED_PERL};
2091 Your perl and your Config.pm seem to have different ideas about the 
2092 architecture they are running on.
2093 Perl thinks: [$arch]
2094 Config says: [$Config{archname}]
2095 This may or may not cause problems. Please check your installation of perl 
2096 if you have problems building this extension.
2097 END
2098     }
2099
2100     return $ret;
2101 }
2102
2103
2104
2105 =head2 File::Spec wrappers
2106
2107 ExtUtils::MM_Any is a subclass of File::Spec.  The methods noted here
2108 override File::Spec.
2109
2110
2111
2112 =head3 catfile
2113
2114 File::Spec <= 0.83 has a bug where the file part of catfile is not
2115 canonicalized.  This override fixes that bug.
2116
2117 =cut
2118
2119 sub catfile {
2120     my $self = shift;
2121     return $self->canonpath($self->SUPER::catfile(@_));
2122 }
2123
2124
2125
2126 =head2 Misc
2127
2128 Methods I can't really figure out where they should go yet.
2129
2130
2131 =head3 find_tests
2132
2133   my $test = $mm->find_tests;
2134
2135 Returns a string suitable for feeding to the shell to return all
2136 tests in t/*.t.
2137
2138 =cut
2139
2140 sub find_tests {
2141     my($self) = shift;
2142     return -d 't' ? 't/*.t' : '';
2143 }
2144
2145
2146 =head3 extra_clean_files
2147
2148     my @files_to_clean = $MM->extra_clean_files;
2149
2150 Returns a list of OS specific files to be removed in the clean target in
2151 addition to the usual set.
2152
2153 =cut
2154
2155 # An empty method here tickled a perl 5.8.1 bug and would return its object.
2156 sub extra_clean_files { 
2157     return;
2158 }
2159
2160
2161 =head3 installvars
2162
2163     my @installvars = $mm->installvars;
2164
2165 A list of all the INSTALL* variables without the INSTALL prefix.  Useful
2166 for iteration or building related variable sets.
2167
2168 =cut
2169
2170 sub installvars {
2171     return qw(PRIVLIB SITELIB  VENDORLIB
2172               ARCHLIB SITEARCH VENDORARCH
2173               BIN     SITEBIN  VENDORBIN
2174               SCRIPT  SITESCRIPT  VENDORSCRIPT
2175               MAN1DIR SITEMAN1DIR VENDORMAN1DIR
2176               MAN3DIR SITEMAN3DIR VENDORMAN3DIR
2177              );
2178 }
2179
2180
2181 =head3 libscan
2182
2183   my $wanted = $self->libscan($path);
2184
2185 Takes a path to a file or dir and returns an empty string if we don't
2186 want to include this file in the library.  Otherwise it returns the
2187 the $path unchanged.
2188
2189 Mainly used to exclude version control administrative directories from
2190 installation.
2191
2192 =cut
2193
2194 sub libscan {
2195     my($self,$path) = @_;
2196     my($dirs,$file) = ($self->splitpath($path))[1,2];
2197     return '' if grep /^(?:RCS|CVS|SCCS|\.svn|_darcs)$/, 
2198                      $self->splitdir($dirs), $file;
2199
2200     return $path;
2201 }
2202
2203
2204 =head3 platform_constants
2205
2206     my $make_frag = $mm->platform_constants
2207
2208 Returns a make fragment defining all the macros initialized in
2209 init_platform() rather than put them in constants().
2210
2211 =cut
2212
2213 sub platform_constants {
2214     return '';
2215 }
2216
2217
2218 =head1 AUTHOR
2219
2220 Michael G Schwern <schwern@pobox.com> and the denizens of
2221 makemaker@perl.org with code from ExtUtils::MM_Unix and
2222 ExtUtils::MM_Win32.
2223
2224
2225 =cut
2226
2227 1;