ExtUtils::MakeMaker 6.03 -> 6.06_05ish
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / MM_Any.pm
CommitLineData
f6d6199c 1package ExtUtils::MM_Any;
2
3use strict;
4use vars qw($VERSION @ISA);
5$VERSION = 0.04;
479d2113 6@ISA = qw(File::Spec);
f6d6199c 7
8use Config;
9use File::Spec;
10
11
12=head1 NAME
13
14ExtUtils::MM_Any - Platform agnostic MM methods
15
16=head1 SYNOPSIS
17
18 FOR INTERNAL USE ONLY!
19
20 package ExtUtils::MM_SomeOS;
21
22 # Temporarily, you have to subclass both. Put MM_Any first.
23 require ExtUtils::MM_Any;
24 require ExtUtils::MM_Unix;
25 @ISA = qw(ExtUtils::MM_Any ExtUtils::Unix);
26
27=head1 DESCRIPTION
28
29B<FOR INTERNAL USE ONLY!>
30
31ExtUtils::MM_Any is a superclass for the ExtUtils::MM_* set of
32modules. It contains methods which are either inherently
33cross-platform or are written in a cross-platform manner.
34
35Subclass off of ExtUtils::MM_Any I<and> ExtUtils::MM_Unix. This is a
36temporary solution.
37
38B<THIS MAY BE TEMPORARY!>
39
40=head1 Inherently Cross-Platform Methods
41
42These are methods which are by their nature cross-platform and should
43always be cross-platform.
44
479d2113 45=head2 File::Spec wrappers
f6d6199c 46
479d2113 47ExtUtils::MM_Any is a subclass of File::Spec. The methods noted here
48override File::Spec.
f6d6199c 49
479d2113 50=over 4
51
52=item catfile
53
54File::Spec <= 0.83 has a bug where the file part of catfile is not
55canonicalized. This override fixes that bug.
56
57=cut
58
59sub catfile {
60 my $self = shift;
61 return $self->canonpath($self->SUPER::catfile(@_));
62}
63
64=back
65
66=head1 Thought To Be Cross-Platform Methods
67
68These are methods which are thought to be cross-platform by virtue of
69having been written in a way to avoid incompatibilities. They may
70require partial overrides.
f6d6199c 71
72=over 4
73
479d2113 74=item B<split_command>
75
76 my @cmds = $MM->split_command($cmd, @args);
77
78Most OS have a maximum command length they can execute at once. Large
79modules can easily generate commands well past that limit. Its
80necessary to split long commands up into a series of shorter commands.
81
82split_command() will return a series of @cmds each processing part of
83the args. Collectively they will process all the arguments. Each
84individual line in @cmds will not be longer than the
85$self->max_exec_len being careful to take into account macro expansion.
86
87$cmd should include any switches and repeated initial arguments.
88
89If no @args are given, no @cmds will be returned.
90
91Pairs of arguments will always be preserved in a single command, this
92is a heuristic for things like pm_to_blib and pod2man which work on
93pairs of arguments. This makes things like this safe:
94
95 $self->split_command($cmd, %pod2man);
96
f6d6199c 97
98=cut
99
479d2113 100sub split_command {
101 my($self, $cmd, @args) = @_;
102
103 my @cmds = ();
104 return(@cmds) unless @args;
105
106 # If the command was given as a here-doc, there's probably a trailing
107 # newline.
108 chomp $cmd;
109
110 # set aside 20% for macro expansion.
111 my $len_left = int($self->max_exec_len * 0.80);
112 $len_left -= length $self->_expand_macros($cmd);
113
114 do {
115 my $arg_str = '';
116 my @next_args;
117 while( @next_args = splice(@args, 0, 2) ) {
118 # Two at a time to preserve pairs.
119 my $next_arg_str = "\t ". join ' ', @next_args, "\n";
120
121 if( !length $arg_str ) {
122 $arg_str .= $next_arg_str
123 }
124 elsif( length($arg_str) + length($next_arg_str) > $len_left ) {
125 unshift @args, @next_args;
126 last;
127 }
128 else {
129 $arg_str .= $next_arg_str;
130 }
131 }
132 chop $arg_str;
133
134 push @cmds, $self->escape_newlines("$cmd\n$arg_str");
135 } while @args;
136
137 return @cmds;
f6d6199c 138}
139
479d2113 140
141sub _expand_macros {
142 my($self, $cmd) = @_;
143
144 $cmd =~ s{\$\((\w+)\)}{
145 defined $self->{$1} ? $self->{$1} : "\$($1)"
146 }e;
147 return $cmd;
148}
149
150
151=item B<echo>
152
153 my @commands = $MM->echo($text);
154 my @commands = $MM->echo($text, $file);
155 my @commands = $MM->echo($text, $file, $appending);
156
157Generates a set of @commands which print the $text to a $file.
158
159If $file is not given, output goes to STDOUT.
160
161If $appending is true the $file will be appended to rather than
162overwritten.
f6d6199c 163
164=cut
165
479d2113 166sub echo {
167 my($self, $text, $file, $appending) = @_;
168 $appending ||= 0;
169
170 my @cmds = map { '$(NOECHO) $(ECHO) '.$self->quote_literal($_) }
171 split /\n/, $text;
172 if( $file ) {
173 my $redirect = $appending ? '>>' : '>';
174 $cmds[0] .= " $redirect $file";
175 $_ .= " >> $file" foreach @cmds[1..$#cmds];
176 }
177
178 return @cmds;
f6d6199c 179}
180
479d2113 181
182=item init_VERSION
183
184 $mm->init_VERSION
185
186Initialize macros representing versions of MakeMaker and other tools
187
188MAKEMAKER: path to the MakeMaker module.
189
190MM_VERSION: ExtUtils::MakeMaker Version
191
192MM_REVISION: ExtUtils::MakeMaker version control revision (for backwards
193 compat)
194
195VERSION: version of your module
196
197VERSION_MACRO: which macro represents the version (usually 'VERSION')
198
199VERSION_SYM: like version but safe for use as an RCS revision number
200
201DEFINE_VERSION: -D line to set the module version when compiling
202
203XS_VERSION: version in your .xs file. Defaults to $(VERSION)
204
205XS_VERSION_MACRO: which macro represents the XS version.
206
207XS_DEFINE_VERSION: -D line to set the xs version when compiling.
208
209Called by init_main.
f6d6199c 210
211=cut
212
479d2113 213sub init_VERSION {
214 my($self) = shift;
215
216 $self->{MAKEMAKER} = $INC{'ExtUtils/MakeMaker.pm'};
217 $self->{MM_VERSION} = $ExtUtils::MakeMaker::VERSION;
218 $self->{MM_REVISION}= $ExtUtils::MakeMaker::Revision;
219 $self->{VERSION_FROM} ||= '';
220
221 if ($self->{VERSION_FROM}){
222 $self->{VERSION} = $self->parse_version($self->{VERSION_FROM});
223 if( $self->{VERSION} eq 'undef' ) {
224 require Carp;
225 Carp::carp("WARNING: Setting VERSION via file ".
226 "'$self->{VERSION_FROM}' failed\n");
227 }
228 }
229
230 # strip blanks
231 if (defined $self->{VERSION}) {
232 $self->{VERSION} =~ s/^\s+//;
233 $self->{VERSION} =~ s/\s+$//;
234 }
235 else {
236 $self->{VERSION} = '';
237 }
238
239
240 $self->{VERSION_MACRO} = 'VERSION';
241 ($self->{VERSION_SYM} = $self->{VERSION}) =~ s/\W/_/g;
242 $self->{DEFINE_VERSION} = '-D$(VERSION_MACRO)=\"$(VERSION)\"';
243
244
245 # Graham Barr and Paul Marquess had some ideas how to ensure
246 # version compatibility between the *.pm file and the
247 # corresponding *.xs file. The bottomline was, that we need an
248 # XS_VERSION macro that defaults to VERSION:
249 $self->{XS_VERSION} ||= $self->{VERSION};
250
251 $self->{XS_VERSION_MACRO} = 'XS_VERSION';
252 $self->{XS_DEFINE_VERSION} = '-D$(XS_VERSION_MACRO)=\"$(XS_VERSION)\"';
253
f6d6199c 254}
255
479d2113 256=item wraplist
257
258Takes an array of items and turns them into a well-formatted list of
259arguments. In most cases this is simply something like:
260
261 FOO \
262 BAR \
263 BAZ
f6d6199c 264
265=cut
266
479d2113 267sub wraplist {
268 my $self = shift;
269 return join " \\\n\t", @_;
f6d6199c 270}
271
479d2113 272=item manifypods
273
274Defines targets and routines to translate the pods into manpages and
275put them into the INST_* directories.
f6d6199c 276
277=cut
278
479d2113 279sub manifypods {
280 my $self = shift;
281
282 my $POD2MAN_EXE_macro = $self->POD2MAN_EXE_macro();
283 my $manifypods_target = $self->manifypods_target();
284
285 return <<END_OF_TARGET;
286
287# --- Begin manifypods section:
288$POD2MAN_EXE_macro
289
290$manifypods_target
291
292# --- End manifypods section --- #
293
294END_OF_TARGET
295
f6d6199c 296}
297
479d2113 298
299=item manifypods_target
300
301 my $manifypods_target = $self->manifypods_target;
302
303Generates the manifypods target. This target generates man pages from
304all POD files in MAN1PODS and MAN3PODS.
f6d6199c 305
306=cut
307
479d2113 308sub manifypods_target {
309 my($self) = shift;
310
311 my $man1pods = '';
312 my $man3pods = '';
313 my $dependencies = '';
314
315 # populate manXpods & dependencies:
316 foreach my $name (keys %{$self->{MAN1PODS}}, keys %{$self->{MAN3PODS}}) {
317 $dependencies .= " \\\n\t$name";
318 }
319
320 foreach my $name (keys %{$self->{MAN3PODS}}) {
321 $dependencies .= " \\\n\t$name"
322 }
323
324 my $manify = <<END;
325manifypods : pure_all $dependencies
326END
327
328 my @man_cmds;
329 foreach my $section (qw(1 3)) {
330 my $pods = $self->{"MAN${section}PODS"};
331 push @man_cmds, $self->split_command(<<CMD, %$pods);
332 \$(NOECHO) \$(POD2MAN_EXE) --section=$section --perm_rw=\$(PERM_RW)
333CMD
334 }
335
336 $manify .= join '', map { "$_\n" } @man_cmds;
337
338 return $manify;
f6d6199c 339}
340
479d2113 341
342=item makemakerdflt_target
343
344 my $make_frag = $mm->makemakerdflt_target
345
346Returns a make fragment with the makemakerdeflt_target specified.
347This target is the first target in the Makefile, is the default target
348and simply points off to 'all' just in case any make variant gets
349confused or something gets snuck in before the real 'all' target.
f6d6199c 350
351=cut
352
479d2113 353sub makemakerdflt_target {
354 return <<'MAKE_FRAG';
355makemakerdflt: all
356 $(NOECHO) $(NOOP)
357MAKE_FRAG
358
f6d6199c 359}
360
479d2113 361
362=item special_targets
363
364 my $make_frag = $mm->special_targets
365
366Returns a make fragment containing any targets which have special
367meaning to make. For example, .SUFFIXES and .PHONY.
f6d6199c 368
369=cut
370
479d2113 371sub special_targets {
372 my $make_frag = <<'MAKE_FRAG';
373.SUFFIXES: .xs .c .C .cpp .i .s .cxx .cc $(OBJ_EXT)
374
375.PHONY: all config static dynamic test linkext manifest
376
377MAKE_FRAG
378
379 $make_frag .= <<'MAKE_FRAG' if $ENV{CLEARCASE_ROOT};
380.NO_CONFIG_REC: Makefile
381
382MAKE_FRAG
383
384 return $make_frag;
f6d6199c 385}
386
479d2113 387=item POD2MAN_EXE_macro
f6d6199c 388
479d2113 389 my $pod2man_exe_macro = $self->POD2MAN_EXE_macro
f6d6199c 390
479d2113 391Returns a definition for the POD2MAN_EXE macro. This is a program
392which emulates the pod2man utility. You can add more switches to the
393command by simply appending them on the macro.
394
395Typical usage:
396
397 $(POD2MAN_EXE) --section=3 --perm_rw=$(PERM_RW) podfile man_page
398
399=cut
400
401sub POD2MAN_EXE_macro {
402 my $self = shift;
403
404# Need the trailing '--' so perl stops gobbling arguments and - happens
405# to be an alternative end of line seperator on VMS so we quote it
406 return <<'END_OF_DEF';
407POD2MAN_EXE = $(PERLRUN) "-MExtUtils::Command::MM" -e pod2man "--"
408END_OF_DEF
409}
f6d6199c 410
f6d6199c 411
412=item test_via_harness
413
414 my $command = $mm->test_via_harness($perl, $tests);
415
416Returns a $command line which runs the given set of $tests with
417Test::Harness and the given $perl.
418
419Used on the t/*.t files.
420
421=cut
422
423sub test_via_harness {
424 my($self, $perl, $tests) = @_;
425
e0678a30 426 return qq{\t$perl "-MExtUtils::Command::MM" }.
427 qq{"-e" "test_harness(\$(TEST_VERBOSE), '\$(INST_LIB)', '\$(INST_ARCHLIB)')" $tests\n};
f6d6199c 428}
429
430=item test_via_script
431
432 my $command = $mm->test_via_script($perl, $script);
433
434Returns a $command line which just runs a single test without
435Test::Harness. No checks are done on the results, they're just
436printed.
437
438Used for test.pl, since they don't always follow Test::Harness
439formatting.
440
441=cut
442
443sub test_via_script {
444 my($self, $perl, $script) = @_;
e0678a30 445 return qq{\t$perl "-I\$(INST_LIB)" "-I\$(INST_ARCHLIB)" $script\n};
f6d6199c 446}
447
479d2113 448=item libscan
449
450 my $wanted = $self->libscan($path);
451
452Takes a path to a file or dir and returns an empty string if we don't
453want to include this file in the library. Otherwise it returns the
454the $path unchanged.
455
456Mainly used to exclude RCS, CVS, and SCCS directories from
457installation.
458
459=cut
460
461sub libscan {
462 my($self,$path) = @_;
463 my($dirs,$file) = ($self->splitpath($path))[1,2];
464 return '' if grep /^RCS|CVS|SCCS|\.svn$/,
465 $self->splitdir($dirs), $file;
466
467 return $path;
468}
469
470=item tool_autosplit
471
472Defines a simple perl call that runs autosplit. May be deprecated by
473pm_to_blib soon.
474
475=cut
476
477sub tool_autosplit {
478 my($self, %attribs) = @_;
479
480 my $maxlen = $attribs{MAXLEN} ? '$$AutoSplit::Maxlen=$attribs{MAXLEN};'
481 : '';
482
483 my $asplit = $self->oneliner(sprintf <<'PERL_CODE', $maxlen);
484use AutoSplit; %s autosplit($$ARGV[0], $$ARGV[1], 0, 1, 1)
485PERL_CODE
486
487 return sprintf <<'MAKE_FRAG', $asplit;
488# Usage: $(AUTOSPLITFILE) FileToSplit AutoDirToSplitInto
489AUTOSPLITFILE = %s
490
491MAKE_FRAG
492
493}
494
495
496=item all_target
497
498Generate the default target 'all'.
499
500=cut
501
502sub all_target {
503 my $self = shift;
504
505 return <<'MAKE_EXT';
506all :: pure_all
507 $(NOECHO) $(NOOP)
508MAKE_EXT
509
510}
511
512
513=item metafile_target
514
515 my $target = $mm->metafile_target;
516
517Generate the metafile target.
518
519Writes the file META.yml, YAML encoded meta-data about the module. The
520format follows Module::Build's as closely as possible. Additionally, we
521include:
522
523 version_from
524 installdirs
525
526=cut
527
528sub metafile_target {
529 my $self = shift;
530
531 my $prereq_pm = '';
532 while( my($mod, $ver) = each %{$self->{PREREQ_PM}} ) {
533 $prereq_pm .= sprintf " %-30s %s\n", "$mod:", $ver;
534 }
535
536 my $meta = <<YAML;
537#XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX#
538name: $self->{DISTNAME}
539version: $self->{VERSION}
540version_from: $self->{VERSION_FROM}
541installdirs: $self->{INSTALLDIRS}
542requires:
543$prereq_pm
544distribution_type: module
545generated_by: ExtUtils::MakeMaker version $ExtUtils::MakeMaker::VERSION
546YAML
547
548 my @write_meta = $self->echo($meta, 'META.yml');
549 return sprintf <<'MAKE_FRAG', join "\n\t", @write_meta;
550metafile :
551 %s
552MAKE_FRAG
553
554}
555
556
557=item metafile_addtomanifest_target
558
559 my $target = $mm->metafile_addtomanifest_target
560
561Adds the META.yml file to the MANIFEST.
562
563=cut
564
565sub metafile_addtomanifest_target {
566 my $self = shift;
567
568 my $add_meta = $self->oneliner(<<'CODE', ['-MExtUtils::Manifest=maniadd']);
569maniadd({q{META.yml} => q{Module meta-data in YAML}});
570CODE
571
572 return sprintf <<'MAKE_FRAG', $add_meta;
573metafile_addtomanifest:
574 $(NOECHO) %s
575MAKE_FRAG
576
577}
578
579
580=back
581
582=head2 Abstract methods
583
584Methods which cannot be made cross-platform and each subclass will
585have to do their own implementation.
586
587=over 4
588
589=item oneliner
590
591 my $oneliner = $MM->oneliner($perl_code);
592 my $oneliner = $MM->oneliner($perl_code, \@switches);
593
594This will generate a perl one-liner safe for the particular platform
595you're on based on the given $perl_code and @switches (a -e is
596assumed) suitable for using in a make target. It will use the proper
597shell quoting and escapes.
598
599$(PERLRUN) will be used as perl.
600
601Any newlines in $perl_code will be escaped. Leading and trailing
602newlines will be stripped. Makes this idiom much easier:
603
604 my $code = $MM->oneliner(<<'CODE', [...switches...]);
605some code here
606another line here
607CODE
608
609Usage might be something like:
610
611 # an echo emulation
612 $oneliner = $MM->oneliner('print "Foo\n"');
613 $make = '$oneliner > somefile';
614
615All dollar signs must be doubled in the $perl_code if you expect them
616to be interpreted normally, otherwise it will be considered a make
617macro. Also remember to quote make macros else it might be used as a
618bareword. For example:
619
620 # Assign the value of the $(VERSION_FROM) make macro to $vf.
621 $oneliner = $MM->oneliner('$$vf = "$(VERSION_FROM)"');
622
623Its currently very simple and may be expanded sometime in the figure
624to include more flexible code and switches.
625
626
627=item B<quote_literal>
628
629 my $safe_text = $MM->quote_literal($text);
630
631This will quote $text so it is interpreted literally in the shell.
632
633For example, on Unix this would escape any single-quotes in $text and
634put single-quotes around the whole thing.
635
636
637=item B<escape_newlines>
638
639 my $escaped_text = $MM->escape_newlines($text);
640
641Shell escapes newlines in $text.
642
643
644=item max_exec_len
645
646 my $max_exec_len = $MM->max_exec_len;
647
648Calculates the maximum command size the OS can exec. Effectively,
649this is the max size of a shell command line.
650
651=for _private
652$self->{_MAX_EXEC_LEN} is set by this method, but only for testing purposes.
653
654=item B<init_others>
655
656 $MM->init_others();
657
658Initializes the macro definitions used by tools_other() and places them
659in the $MM object.
660
661If there is no description, its the same as the parameter to
662WriteMakefile() documented in ExtUtils::MakeMaker.
663
664Defines at least these macros.
665
666 Macro Description
667
668 NOOP
669 NOECHO
670
671 MAKEFILE
672 FIRST_MAKEFILE
673 MAKEFILE_OLD
674 MAKE_APERL_FILE File used by MAKE_APERL
675
676 SHELL Program used to run
677 shell commands
678
679 RM_F Remove a file
680 RM_RF Remove a directory
681 TOUCH Update a file's timestamp
682 TEST_F Test for a file's existence
683 CP Copy a file
684 MV Move a file
685 CHMOD Change permissions on a
686 file
687
688 UMASK_NULL Nullify umask
689 DEV_NULL Supress all command output
690
691=item init_DIRFILESEP
692
693 $MM->init_DIRFILESEP;
694 my $dirfilesep = $MM->{DIRFILESEP};
695
696Initializes the DIRFILESEP macro which is the seperator between the
697directory and filename in a filepath. ie. / on Unix, \ on Win32 and
698nothing on VMS.
699
700For example:
701
702 # instead of $(INST_ARCHAUTODIR)/extralibs.ld
703 $(INST_ARCHAUTODIR)$(DIRFILESEP)extralibs.ld
704
705Something of a hack but it prevents a lot of code duplication between
706MM_* variants.
707
708Do not use this as a seperator between directories. Some operating
709systems use different seperators between subdirectories as between
710directories and filenames (for example: VOLUME:[dir1.dir2]file on VMS).
711
712=item init_linker
713
714 $mm->init_linker;
715
716Initialize macros which have to do with linking.
717
718PERL_ARCHIVE: path to libperl.a equivalent to be linked to dynamic
719extensions.
720
721PERL_ARCHIVE_AFTER: path to a library which should be put on the
722linker command line I<after> the external libraries to be linked to
723dynamic extensions. This may be needed if the linker is one-pass, and
724Perl includes some overrides for C RTL functions, such as malloc().
725
726EXPORT_LIST: name of a file that is passed to linker to define symbols
727to be exported.
728
729Some OSes do not need these in which case leave it blank.
730
731
732=item init_platform
733
734 $mm->init_platform
735
736Initialize any macros which are for platform specific use only.
737
738A typical one is the version number of your OS specific mocule.
739(ie. MM_Unix_VERSION or MM_VMS_VERSION).
740
741=item platform_constants
742
743 my $make_frag = $mm->platform_constants
744
745Returns a make fragment defining all the macros initialized in
746init_platform() rather than put them in constants().
747
748=cut
749
750sub init_platform {
751 return '';
752}
753
754sub platform_constants {
755 return '';
756}
757
758
f6d6199c 759=back
760
761=head1 AUTHOR
762
479d2113 763Michael G Schwern <schwern@pobox.com> and the denizens of
764makemaker@perl.org with code from ExtUtils::MM_Unix and
765ExtUtils::MM_Win32.
f6d6199c 766
767
768=cut
769
7701;