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