e6b96ef5624317592dca37ed5bd13ee558b6d722
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / MakeMaker.pm
1 package ExtUtils::MakeMaker;
2
3 $Version = 3.8;         # Last edited 17th Jan 1995 by Andreas Koenig
4
5 use Config;
6 use Carp;
7 use Cwd;
8
9 require Exporter;
10 @ISA = qw(Exporter);
11 @EXPORT = qw(&WriteMakefile &mkbootstrap $Verbose);
12 @EXPORT_OK = qw($Version %att %skip %Recognized_Att_Keys
13         @MM_Sections %MM_Sections
14         &help &lsdir);
15
16 $Is_VMS = $Config{'osname'} eq 'VMS';
17 require ExtUtils::MM_VMS if $Is_VMS;
18
19 use strict qw(refs);
20
21 $Version = $Version;# avoid typo warning
22 $Verbose = 0;
23 $^W=1;
24
25
26 =head1 NAME
27
28 ExtUtils::MakeMaker - create an extension Makefile
29
30 =head1 SYNOPSIS
31
32 use ExtUtils::MakeMaker;
33
34 WriteMakefile( ATTRIBUTE => VALUE [, ...] );
35
36 =head1 DESCRIPTION
37
38 This utility is designed to write a Makefile for an extension module
39 from a Makefile.PL. It is based on the Makefile.SH model provided by
40 Andy Dougherty and the perl5-porters.
41
42 It splits the task of generating the Makefile into several subroutines
43 that can be individually overridden.  Each subroutine returns the text
44 it wishes to have written to the Makefile.
45
46 =head2 Default Makefile Behaviour
47
48 The automatically generated Makefile enables the user of the extension
49 to invoke
50
51     perl Makefile.PL
52     make
53     make test
54     make install
55
56 (This section is yet to be completed ...)
57
58 =head2 Determination of Perl Library and Installation Locations
59
60 MakeMaker needs to know, or to guess, where certain things are located.
61 Especially INST_LIB, INST_ARCHLIB, PERL_LIB, PERL_ARCHLIB and PERL_SRC.
62
63 Extensions may be built anywhere within the file system after perl has
64 been installed correctly. Before perl is installed extensions have to
65 be built below the C<ext/> directory within the tree of the perl
66 source, i.e. where all the standard extensions are being built. The
67 generated Makefile will recognize, which of the two is the current
68 configuration and will set some variables accordingly.
69
70 Only if the extension is being built in PERL_SRC/ext, the variable
71 PERL_SRC is defined, otherwise it is undefined.  Consequently
72 MakeMaker will default PERL_LIB and PERL_ARCHLIB to PERL_SRC/lib only
73 if PERL_SRC is defined, otherwise PERL_*LIB will default to the public
74 library locations.
75
76 INST_LIB and INST_ARCHLIB default to PERL_LIB and PERL_ARCHLIB if we
77 are building below the PERL_SRC/ext directory. Else they default to
78 ./blib.
79
80 =head2 Useful Default Makefile Macros
81
82 FULLEXT = Pathname for extension directory (eg DBD/Oracle).
83
84 BASEEXT = Basename part of FULLEXT. May be just equal FULLEXT.
85
86 ROOTEXT = Directory part of FULLEXT with leading slash (eg /DBD)
87
88 PERL_LIB = Directory where we read the perl library files
89
90 PERL_ARCHLIB = Same as above for architecture dependent files
91
92 INST_LIB = Directory where we put library files of this extension
93         while building it. If we are building below PERL_SRC/ext
94         we default to PERL_SRC/lib, else we default to ./blib.
95
96 INST_ARCHLIB = Same as above for architecture dependent files
97
98 INST_LIBDIR = $(INST_LIB)$(ROOTEXT)
99
100 INST_AUTODIR = $(INST_LIB)/auto/$(FULLEXT)
101
102 INST_ARCHAUTODIR = $(INST_ARCHLIB)/auto/$(FULLEXT)
103
104 =head2 Customizing The Generated Makefile
105
106 If the Makefile generated does not fit your purpose you can change it
107 using the mechanisms described below.
108
109 =head2 Using Attributes (and Parameters)
110
111 The following attributes can be specified as arguments to WriteMakefile()
112 or as NAME=VALUE pairs on the command line:
113
114 This description is not yet documented; you can get at the description
115 with the command
116     perl -e 'use ExtUtils::MakeMaker qw(&help); &help;'
117
118 =head2 Overriding MakeMaker Methods
119
120 If you cannot achieve the desired Makefile behaviour by specifying
121 attributes you may define private subroutines in the Makefile.PL.
122 Each subroutines returns the text it wishes to have written to
123 the Makefile. To override a section of the Makefile you can
124 either say:
125
126         sub MY::c_o { "new literal text" }
127
128 or you can edit the default by saying something like:
129
130         sub MY::c_o { $_=MM->c_o; s/old text/new text/; $_ }
131
132 If you still need a different solution, try to develop another 
133 subroutine, that fits your needs and submit the diffs to 
134 perl5-porters@nicoh.com or comp.lang.perl as appropriate.
135
136
137 =head1 AUTHORS
138
139 Andy Dougherty <doughera@lafcol.lafayette.edu>, Andreas Koenig
140 <k@franz.ww.TU-Berlin.DE>, Tim Bunce <Tim.Bunce@ig.co.uk>
141
142 =head1 MODIFICATION HISTORY
143
144 v1, August 1994; by Andreas Koenig. Based on Andy Dougherty's Makefile.SH.
145 v2, September 1994 by Tim Bunce.
146 v3.0 October  1994 by Tim Bunce.
147 v3.1 November 11th 1994 by Tim Bunce.
148 v3.2 November 18th 1994 by Tim Bunce.
149 v3.3 November 27th 1994 by Andreas Koenig.
150 v3.4 December  7th 1994 by Andreas Koenig and Tim Bunce.
151 v3.5 December 15th 1994 by Tim Bunce.
152 v3.6 December 15th 1994 by Tim Bunce.
153
154 Added C and H attributes and corresponding macros. These default to the
155 list of *.c and *.h files in the directory. C also includes *.c file
156 names corresponding to any *.xs files in the directory. ARMAYBE should
157 now be specified as an attribute of the dynamic_lib section. The installpm
158 section now accepts a SPLITLIB attribute. This defaults to '$(INST_LIB)'.
159 Improved automatic setting of INST_ARCHLIB. Newlines in OBJECT now translate
160 into <space><backslash><newline><tab> for better formatting. Improved
161 descriptive comments for EXTRALIBS, LDLOADLIBS and BSLOADLIBS. Bootstrap
162 files are now always installed - (after a small patch) the DynaLoader will
163 only read a non-empty bootstrap file. Subdirectory dependencies have
164 been improved. The .c files produced from .xs files now depend on
165 XSUBPPDEPS (the typemaps).
166
167 v3.7 December 30th By Tim Bunce
168
169 Most, if not all, the MakeMaker support for no perl source is now
170 included. Recent ld and mkbootstrap patches applied.  -lX11_s suffix
171 fix applied.
172
173 Also contained patches to Makefile.SH, ext/DynaLoader/DynaLoader.pm,
174 ext/util/make_ext, and h2xs
175
176 v3.8 January 9th By Andreas Koenig and Tim Bunce
177
178 - Introduces ./blib as the directory, where the ready-to-use module
179 will live for the time of the building process if PERL_SRC/lib is not
180 available. ./blib is turned into an absolute pathname and gets passed
181 to subdirectories when writing the Makefile.
182
183 - INST_ARCHLIB will now be set equal to INST_LIB if we cannot
184 determine it with the methods near line 620 (in the intialize
185 subroutine).
186
187 - Introduced the install subroutine, that calls another make
188 recursively with INST_LIB, and INST_ARCHLIB set according to the local
189 conventions.
190
191 - Since version 3.6 all *.al and *.ix files are installed into
192 $installprivlib. In perl5.000 they were installed into
193 $installarchlib. Version 3.8 takes precautions to delete old *.{al,ix}
194 files that are left over in $installarchlib. Installperl is also
195 patched to both delete old *.al and *.ix files, that were put into
196 $installarchlib, and to install them properly from now on.
197
198 - h2xs is patched by Dean Roehrich to reintroduce the C<-A> option and
199 reconsiders the use of the AutoLoader. Also podifies h2xs.
200
201 - Patches to {DB_File,Fcntl,GDBM_File,POSIX,Socket}.pm, eliminating
202 AutoLoader from @ISA (by Dean Roehrich).
203
204 - A patch to AutoSplit.pm makes sure, the target directory of the
205 split exists.
206
207 - installperl now installs all *.h files into $installarchlib/CORE.
208
209 - added a simple help function
210
211 - AutoSplit:
212
213         Turned two constants into package variables.
214         Modified the default for &autosplit to be $CheckModTime=1.
215         Added warning and comment if auto dir does not exist.
216         Enhanced AutoLoader check (POSIX.pm was not getting AutoSplit!)
217         Improved logging if $Verbose>=2.
218
219 - MakeMaker:
220
221         EXPORT_OK lsdir(), it's very handy.
222         Deleted much of the early revision history.
223         Changed LDTARGET (bad name) to LDFROM (better, but not great :)
224         INST_ARCHLIB for ./blib found via mapping from INST_LIB ./blib
225           (this allows error message if INST_ARCHLIB still undefined)
226         Added optional regex filter to &lsdir.
227         Added cute auto handling of a ./$(BASEEXT)/*.pm directory as
228           per recent messages with Nick re ext/nTk/nTk/Text.pm
229         Added NOOP, RM_F, RM_RF, TOUCH, CP to %att.
230         NOOP is now "" on Unix, "<tab>" caused problems (see makefile section)
231         RM_F etc now expanded when Makefile written.
232         Old AUTOSPLITLIB definition deleted.
233         See new dynamic_lib section for new ARMAYBE handling.
234         install section now uses $(INSTALLPRIVLIB) because on some
235           systems INSTALLPRIVLIB != PRIVLIB (ditto for archlib)
236
237 =head1 NOTES
238
239 MakeMaker development work still to be done:
240
241 Needs more complete documentation.
242
243 The ext.libs file mechanism will need to be revised to allow a
244 make-a-perl [list-of-static-extensions] script to work.
245
246 Add method to take a list of files and wrap it in a Makefile
247 compatible way (<space><backslash><newline><tab>).
248
249 =cut
250
251
252 # Setup dummy package:
253 # MY exists for overriding methods to be defined within
254 unshift(@MY::ISA, qw(MM));
255
256 # Dummy package MM inherits actual methods from OS-specific
257 # default packages.  We use this intermediate package so
258 # MY->func() can call MM->func() and get the proper
259 # default routine without having to know under what OS
260 # it's running.
261 unshift(@MM::ISA, $Is_VMS ? qw(ExtUtils::MM_VMS MM_Unix) : qw(MM_Unix));
262
263 $Attrib_Help = <<'END';
264  NAME:          Perl module name for this extension (DBD::Oracle)
265                 This defaults to the directory name.
266
267  DISTNAME:      Your name for distributing the package (by tar file)
268                 This defaults to NAME above.
269
270  VERSION:       Your version number for distributing the package.
271                 This defaults to 0.1.
272
273  INST_LIB:      Perl library directory to install the module into.
274  INST_ARCHLIB:  Perl architecture-dependent library to install into
275                 (defaults to INST_LIB)
276
277  PERL_LIB:      Directory containing the Perl library to use.
278  PERL_SRC:      Directory containing the Perl source code
279                 (use of this should be avoided, it may be undefined)
280
281  INC:           Include file dirs eg: '-I/usr/5include -I/path/to/inc'
282  DEFINE:        something like "-DHAVE_UNISTD_H"
283  OBJECT:        List of object files, defaults to '$(BASEEXT).o',
284                 but can be a long string containing all object files,
285                     e.g. "tkpBind.o tkpButton.o tkpCanvas.o"
286  MYEXTLIB:      If the extension links to a library that it builds
287                 set this to the name of the library (see SDBM_File)
288
289  LIBS:          An anonymous array of alternative library specifications
290                 to be searched for (in order) until at least one library
291                 is found.
292                   'LIBS' => [ "-lgdbm", "-ldbm -lfoo", "-L/path -ldbm.nfs" ]
293                 Mind, that any element of the array contains a complete
294                 set of arguments for the ld command. So do not specify
295                   'LIBS' => ["-ltcl", "-ltk", "-lX11" ], #wrong
296                 See ODBM_File/Makefile.PL for an example, where an
297                 array is needed. If you specify a scalar as in
298                   'LIBS' => "-ltcl -ltk -lX11"
299                 MakeMaker will turn it into an array with one element.
300
301  LDFROM:        defaults to "$(OBJECT)" and is used in the ld command
302                 to specify what files to link/load from
303                 (also see dynamic_lib below for how to specify ld flags)
304
305  DIR:           Ref to array of subdirectories containing Makefile.PLs
306                 e.g. [ 'sdbm' ] in ext/SDBM_File
307
308  PM:            Hashref of .pm files and *.pl files to be installed.
309                 e.g. { 'name_of_file.pm' => '$(INST_LIBDIR)/install_as.pm' }
310                 By default this will include *.pm and *.pl. If a lib directory
311                 exists and is not listed in DIR (above) then any *.pm and
312                 *.pl files it contains will also be included by default.
313
314  XS:            Hashref of .xs files. MakeMaker will default this.
315                 e.g. { 'name_of_file.xs' => 'name_of_file.c' }
316                 The .c files will automatically be included in the list
317                 of files deleted by a make clean.
318
319  C:             Ref to array of *.c file names. Initialised from a directory scan
320                 and the values portion of the XS attribute hash. This is not
321                 currently used by MakeMaker but may be handy in Makefile.PLs.
322
323  H:             Ref to array of *.h file names. Similar to C: above.
324
325  LINKTYPE:      =>'static' or 'dynamic' (default unless usedl=undef in config.sh)
326                 Should only be used to force static linking (also see linkext below).
327
328  CONFIG:        =>[qw(archname manext)] defines ARCHNAME & MANEXT from config.sh
329  SKIP:          =>[qw(name1 name2)] skip (do not write) sections of the Makefile
330
331  PERL:
332  FULLPERL:
333
334 Additional lowercase attributes can be used to pass parameters to the
335 methods which implement that part of the Makefile. These are not
336 normally required:
337
338  installpm:     {SPLITLIB => '$(INST_LIB)' (default) or '$(INST_ARCHLIB)'}
339  linkext:       {LINKTYPE => 'static', 'dynamic' or ''}
340  dynamic_lib    {ARMAYBE => 'ar', OTHERLDFLAGS => '...'}
341  clean:         {FILES => "*.xyz foo"}
342  realclean:     {FILES => '$(INST_ARCHAUTODIR)/*.xyz'}
343  distclean:     {TARNAME=>'MyTarFile', TARFLAGS=>'cvfF', COMPRESS=>'gzip'}
344  tool_autosplit:        {MAXLEN => 8}
345 END
346
347 sub help {print $Attrib_Help;}
348
349 @MM_Sections_spec = (
350     'post_initialize'   => {},
351     'constants'         => {},
352     'const_config'      => {},
353     'const_loadlibs'    => {},
354     'const_cccmd'       => {},
355     'tool_autosplit'    => {},
356     'tool_xsubpp'       => {},
357     'tools_other'       => {},
358     'post_constants'    => {},
359     'c_o'               => {},
360     'xs_c'              => {},
361     'xs_o'              => {},
362     'top_targets'       => {},
363     'linkext'           => {},
364     'dynamic'           => {},
365     'dynamic_bs'        => {},
366     'dynamic_lib'       => {},
367     'static'            => {},
368     'static_lib'        => {},
369     'installpm'         => {},
370     'subdirs'           => {},
371     'clean'             => {},
372     'realclean'         => {},
373     'distclean'         => {},
374     'test'              => {},
375     'install'           => {},
376     'force'             => {},
377     'perldepend'        => {},
378     'makefile'          => {},
379     'postamble'         => {},
380 );
381 %MM_Sections = @MM_Sections_spec; # looses section ordering
382 @MM_Sections = grep(!ref, @MM_Sections_spec); # keeps order
383
384 %Recognized_Att_Keys = %MM_Sections; # All sections are valid keys.
385 foreach(split(/\n/,$Attrib_Help)){
386     chomp;
387     next unless m/^\s*(\w+):\s*(.*)/;
388     $Recognized_Att_Keys{$1} = $2;
389     print "Attribute '$1' => '$2'\n" if ($Verbose >= 2);
390 }
391
392 %att  = ();
393 %skip = ();
394
395 sub skipcheck{
396     my($section) = @_;
397     return 'skipped' if $skip{$section};
398     return '';
399 }
400
401
402 sub WriteMakefile {
403     %att = @_;
404     local($\)="\n";
405
406     print STDOUT "MakeMaker" if $Verbose;
407
408     parse_args(\%att, @ARGV);
409     my(%initial_att) = %att; # record initial attributes
410
411     MY->initialize(@ARGV);
412
413     print STDOUT "Writing Makefile for $att{NAME}";
414
415     unlink("Makefile", "MakeMaker.tmp", $Is_VMS ? 'Descrip.MMS' : '');
416     open MAKE, ">MakeMaker.tmp" or die "Unable to open MakeMaker.tmp: $!";
417     select MAKE; $|=1; select STDOUT;
418
419     print MAKE "# This Makefile is for the $att{NAME} extension to perl.\n#";
420     print MAKE "# It was generated automatically by MakeMaker from the contents";
421     print MAKE "# of Makefile.PL. Don't edit this file, edit Makefile.PL instead.";
422     print MAKE "#\n#    ANY CHANGES MADE HERE WILL BE LOST! \n#";
423     print MAKE "#   MakeMaker Parameters: ";
424     foreach $key (sort keys %initial_att){
425         my($v) = neatvalue($initial_att{$key});
426         $v =~ tr/\n/ /s;
427         print MAKE "#   $key => $v";
428     }
429
430     # build hash for SKIP to make testing easy
431     %skip = map( ($_,1), @{$att{'SKIP'} || []});
432
433     foreach $section ( @MM_Sections ){
434         print "Processing Makefile '$section' section" if ($Verbose >= 2);
435         my($skipit) = skipcheck($section);
436         if ($skipit){
437             print MAKE "\n# --- MakeMaker $section section $skipit.";
438         } else {
439             my(%a) = %{$att{$section} || {}};
440             print MAKE "\n# --- MakeMaker $section section:";
441             print MAKE "# ",%a if ($Verbose >= 2);
442             print(MAKE MY->nicetext(MY->$section( %a )));
443         }
444     }
445
446     if ($Verbose){
447         print MAKE "\n# Full list of MakeMaker attribute values:";
448         foreach $key (sort keys %att){
449             my($v) = neatvalue($att{$key});
450             $v =~ tr/\n/ /s;
451             print MAKE "#       $key => $v";
452         }
453     }
454
455     print MAKE "\n# End.";
456     close MAKE;
457     my($finalname) = $Is_VMS ? "Descrip.MMS" : "Makefile";
458     rename("MakeMaker.tmp", $finalname);
459
460     chmod 0644, $finalname;
461     system("$Config{'eunicefix'} $finalname") unless $Config{'eunicefix'} eq ":";
462
463     1;
464 }
465
466
467 sub mkbootstrap{
468     parse_args(\%att, @ARGV);
469     MY->mkbootstrap(@_);
470 }
471
472
473 sub parse_args{
474     my($attr, @args) = @_;
475     foreach (@args){
476         next unless m/(.*?)=(.*)/;
477         $$attr{$1} = $2;
478     }
479     # catch old-style 'potential_libs' and inform user how to 'upgrade'
480     if (defined $$attr{'potential_libs'}){
481         my($msg)="'potential_libs' => '$$attr{potential_libs}' should be";
482         if ($$attr{'potential_libs'}){
483             print STDERR "$msg changed to:\n\t'LIBS' => ['$$attr{potential_libs}']\n";
484         } else {
485             print STDERR "$msg deleted.\n";
486         }
487         $$attr{LIBS} = [$$attr{'potential_libs'}];
488         delete $$attr{'potential_libs'};
489     }
490     # catch old-style 'ARMAYBE' and inform user how to 'upgrade'
491     if (defined $$attr{'ARMAYBE'}){
492         my($armaybe) = $$attr{'ARMAYBE'};
493         print STDERR "ARMAYBE => '$armaybe' should be changed to:\n",
494                         "\t'dynamic_lib' => {ARMAYBE => '$armaybe'}\n";
495         my(%dl) = %{$$attr{'dynamic_lib'} || {}};
496         $$attr{'dynamic_lib'} = { %dl, ARMAYBE => $armaybe};
497         delete $$attr{'ARMAYBE'};
498     }
499     foreach(sort keys %{$attr}){
500         print STDOUT "  $_ => ".neatvalue($$attr{$_}) if ($Verbose);
501         warn "'$_' is not a known MakeMaker parameter name.\n"
502             unless exists $Recognized_Att_Keys{$_};
503     }
504 }
505
506
507 sub neatvalue{
508     my($v) = @_;
509     my($t) = ref $v;
510     return "'$v'" unless $t;
511     return "[ ".join(', ',map("'$_'",@$v))." ]" if ($t eq 'ARRAY');
512     return "$v" unless $t eq 'HASH';
513     my(@m, $key, $val);
514     push(@m,"$key=>".neatvalue($val)) while (($key,$val) = each %$v);
515     return "{ ".join(', ',@m)." }";
516 }
517
518
519 # ------ Define the MakeMaker default methods in package MM_Unix ------
520
521 package MM_Unix;
522
523 use Config;
524 use Cwd;
525 use File::Basename;
526 require Exporter;
527
528 Exporter::import('ExtUtils::MakeMaker',
529         qw(%att %skip %Recognized_Att_Keys $Verbose));
530
531 # These attributes cannot be overridden externally
532 @Other_Att_Keys{qw(EXTRALIBS BSLOADLIBS LDLOADLIBS)} = (1) x 3;
533
534 if ($Is_VMS = $Config{'osname'} eq 'VMS') {
535     require File::VMSspec;
536     import File::VMSspec 'vmsify';
537 }
538
539
540 sub initialize {
541     # Find out directory name.  This may contain the extension name.
542     my($pwd) = fastcwd(); # from Cwd.pm
543
544     # --- Initialize PERL_LIB, INST_LIB, PERL_SRC
545
546     # *Real* information: where did we get these two from? ...
547     $inc_config_dir = dirname($INC{'Config.pm'});
548     $inc_carp_dir   = dirname($INC{'Carp.pm'});
549
550     # Typically PERL_* and INST_* will be identical but that need
551     # not be the case (e.g., installing into project libraries etc).
552
553     # Perl Macro:    With source    No source
554     # PERL_LIB       ../../lib      /usr/local/lib/perl5
555     # PERL_ARCHLIB   ../../lib      /usr/local/lib/perl5/sun4-sunos
556     # PERL_SRC       ../..          (undefined)
557
558     # INST Macro:    Locally        Publically
559     # INST_LIB       ../../lib      ./blib
560     # INST_ARCHLIB   ../../lib      ./blib
561
562     unless ($att{PERL_SRC}){
563         foreach(qw(../.. ../../.. ../../../..)){
564             ($att{PERL_SRC}=$_, last) if -f "$_/config.sh";
565         }
566     }
567     unless ($att{PERL_SRC}){
568         warn "Unable to locate perl source.\n";
569         # we should also consider $ENV{PERL5LIB} here
570         $att{PERL_LIB}     = $Config{'privlib'} unless $att{PERL_LIB};
571         $att{PERL_ARCHLIB} = $Config{'archlib'} unless $att{PERL_ARCHLIB};
572         $att{PERL_INC}     = "$att{PERL_ARCHLIB}/CORE"; # wild guess for now
573         die "Try setting PERL_SRC in Makefile.PL or on command line.\n"
574                 unless (-f "$att{PERL_INC}/perl.h");
575     } else {
576         $att{PERL_LIB}     = "$att{PERL_SRC}/lib" unless $att{PERL_LIB};
577         $att{PERL_ARCHLIB} = $att{PERL_LIB};
578         $att{PERL_INC}     = $att{PERL_SRC};
579     }
580
581     # INST_LIB typically pre-set if building an extension after
582     # perl has been built and installed. Setting INST_LIB allows
583     # you to build directly into privlib and avoid installperl.
584     unless ($att{INST_LIB}){
585         if (defined $att{PERL_SRC}) {
586             $att{INST_LIB} = $att{PERL_LIB};
587         } else {
588             $att{INST_LIB} = "$pwd/blib";
589         }
590     }
591     # Try to work out what INST_ARCHLIB should be if not set:
592     unless ($att{INST_ARCHLIB}){
593         my(%archmap) = (
594             "$pwd/blib"         => "$pwd/blib", # our private build lib
595             $att{PERL_LIB}      => $att{PERL_ARCHLIB},
596             $Config{'privlib'}  => $Config{'archlib'},
597             $Config{'installprivlib'}   => $Config{'installarchlib'},
598             $inc_carp_dir       => $inc_config_dir,
599         );
600         $att{INST_ARCHLIB} = $archmap{$att{INST_LIB}};
601         die "Unable to determine INST_ARCHLIB. Please define it explicitly.\n"
602             unless $att{INST_ARCHLIB};
603     }
604
605     # make a few simple checks
606     die "PERL_LIB ($att{PERL_LIB}) is not a perl library directory"
607         unless (-f "$att{PERL_LIB}/Exporter.pm");
608
609     # --- Initialize Module Name and Paths
610
611     # NAME    = The perl module name for this extension (eg DBD::Oracle).
612     # FULLEXT = Pathname for extension directory (eg DBD/Oracle).
613     # BASEEXT = Basename part of FULLEXT. May be just equal FULLEXT.
614     # ROOTEXT = Directory part of FULLEXT with leading /.
615     unless($att{NAME}){ # we have to guess our name
616         my($name) = $pwd;
617         if ($Is_VMS) {
618           $name =~ s:.*?([^.\]]+)\]:$1: unless ($name =~ s:.*[.\[]ext\.::);
619           ($att{NAME}=$name) =~ s#[.\]]#::#g;
620         } else {
621           $name =~ s:.*/:: unless ($name =~ s:^.*/ext/::);
622           ($att{NAME} =$name) =~ s#/#::#g;
623         }
624     }
625     ($att{FULLEXT} =$att{NAME}) =~ s#::#/#g ;           #eg. BSD/Foo/Socket
626     ($att{BASEEXT} =$att{NAME}) =~ s#.*::##;            #eg. Socket
627     ($att{ROOTEXT} =$att{FULLEXT}) =~ s#/?\Q$att{BASEEXT}\E$## ; # eg. /BSD/Foo
628     $att{ROOTEXT} = "/$att{ROOTEXT}" if $att{ROOTEXT};
629     $att{ROOTEXT} = "" if $Is_VMS;
630
631     ($att{DISTNAME}=$att{NAME}) =~ s#(::)#-#g;
632     $att{VERSION} = "0.1" unless $att{VERSION};
633
634
635     # --- Initialize Perl Binary Locations
636
637     # Find Perl 5. The only contract here is that both 'PERL' and 'FULLPERL'
638     # will be working versions of perl 5.
639     $att{'PERL'} = MY->find_perl(5.0, [ qw(perl5 perl miniperl) ],
640             [ $att{PERL_SRC}, split(":", $ENV{PATH}), $Config{'bin'} ], 0 )
641         unless ($att{'PERL'} && -x $att{'PERL'});
642
643     # Define 'FULLPERL' to be a non-miniperl (used in test: target)
644     ($att{'FULLPERL'} = $att{'PERL'}) =~ s/miniperl/perl/
645         unless ($att{'FULLPERL'} && -x $att{'FULLPERL'});
646
647     if ($Is_VMS) {
648         ($att{'PERL'} = 'MCR ' . vmsify($att{'PERL'})) =~ s:.*/::;
649         ($att{'FULLPERL'} = 'MCR ' . vmsify($att{'FULLPERL'})) =~ s:.*/::;
650     }
651
652     # --- Initialize File and Directory Lists (.xs .pm etc)
653
654     {
655         my($name, %dir, %xs, %pm, %c, %h, %ignore);
656         $ignore{'test.pl'} = 1;
657         $ignore{'makefile.pl'} = 1 if $Is_VMS;
658         foreach $name (lsdir(".")){
659             next if ($name =~ /^\./ or $ignore{$name});
660             if (-d $name){
661                 $dir{$name} = $name if (-f "$name/Makefile.PL");
662             }elsif ($name =~ /\.xs$/){
663                 my($c); ($c = $name) =~ s/\.xs$/.c/;
664                 $xs{$name} = $c;
665                 $c{$c} = 1;
666             }elsif ($name =~ /\.c$/){
667                 $c{$name} = 1;
668             }elsif ($name =~ /\.h$/){
669                 $h{$name} = 1;
670             }elsif ($name =~ /\.p[ml]$/){
671                 $pm{$name} = "\$(INST_LIBDIR)/$name";
672             }
673         }
674
675         # If we have a ./lib dir that does NOT contain a Makefile.PL
676         # then add in any .pm and .pl files in that directory.
677         # This makes it easy and tidy to ship a number of perl files.
678         if (-d "lib" and !$dir{'lib'}){
679             foreach $name (lsdir("lib", '\.p[ml]$')){
680                 # next unless ($name =~ /\.p[ml]$/);
681                 $pm{"lib/$name"} = "\$(INST_LIBDIR)/$name";
682             }
683         }
684
685         # Similarly, if we have a ./$(BASEEXT) dir without a Makefile.PL
686         # we treat it as a directory containing *.pm files for modules
687         # which are nested below this one. E.g., ./Tk.pm & ./Tk/Text.pm
688         if (-d $att{BASEEXT} and !$dir{$att{BASEEXT}}){
689             foreach $name (lsdir($att{BASEEXT}, '\.p[ml]$')){
690                 # next unless ($name =~ /\.p[ml]$/);
691                 $pm{"$att{BASEEXT}/$name"} = "\$(INST_LIBDIR)/$att{BASEEXT}/$name";
692             }
693         }
694
695         $att{DIR} = [sort keys %dir] unless $att{DIRS};
696         $att{XS}  = \%xs             unless $att{XS};
697         $att{PM}  = \%pm             unless $att{PM};
698         $att{C}   = [sort keys %c]   unless $att{C};
699         $att{H}   = [sort keys %h]   unless $att{H};
700     }
701
702     # --- Initialize Other Attributes
703
704     for $key (keys(%Recognized_Att_Keys), keys(%Other_Att_Keys)){
705         # avoid warnings for uninitialized vars
706         next if exists $att{$key};
707         $att{$key} = "";
708     }
709
710     # Compute EXTRALIBS, BSLOADLIBS and LDLOADLIBS from $att{'LIBS'}
711     # Lets look at $att{LIBS} carefully: It may be an anon array, a string or
712     # undefined. In any case we turn it into an anon array:
713     $att{LIBS}=[] unless $att{LIBS};
714     $att{LIBS}=[$att{LIBS}] if ref \$att{LIBS} eq SCALAR;
715     foreach ( @{$att{'LIBS'}} ){
716         s/^\s*(.*\S)\s*$/$1/; # remove leading and trailing whitespace
717         my(@libs) = MY->extliblist($_);
718         if ($libs[0] or $libs[1] or $libs[2]){
719             @att{EXTRALIBS, BSLOADLIBS, LDLOADLIBS} = @libs;
720             last;
721         }
722     }
723
724     warn "CONFIG must be an array ref\n"
725         if ($att{CONFIG} and ref $att{CONFIG} ne 'ARRAY');
726     $att{CONFIG} = [] unless (ref $att{CONFIG});
727     push(@{$att{CONFIG}},
728         qw( cc libc ldflags lddlflags ccdlflags cccdlflags
729             ranlib so dlext dlsrc installprivlib installarchlib
730         ));
731     push(@{$att{CONFIG}}, 'shellflags') if $Config{'shellflags'};
732
733     if ($Is_VMS) {
734       # This will not make other Makefile.PLs portable. Any Makefile.PL
735       # which says OBJECT => "foo.o bar.o" will fail on VMS. It might
736       # be better to fix the c_o section to produce .o files.
737       $att{OBJECT} = '$(BASEEXT).obj' unless $att{OBJECT};
738       $att{OBJECT} =~ s/[^,\s]\s+/, /g;
739       $att{OBJECT} =~ s/\n+/, /g;
740     } else {
741       $att{OBJECT} = '$(BASEEXT).o' unless $att{OBJECT};
742       $att{OBJECT} =~ s/\n+/ \\\n\t/g;
743     }
744     $att{BOOTDEP}  = (-f "$att{BASEEXT}_BS") ? "$att{BASEEXT}_BS" : "";
745     $att{LD}       = ($Config{'ld'} || 'ld') unless $att{LD};
746     $att{LDFROM} = '$(OBJECT)' unless $att{LDFROM};
747     $att{LINKTYPE} = ($Config{'usedl'}) ? 'dynamic' : 'static'
748         unless $att{LINKTYPE};
749
750     # These get overridden for VMS and maybe some other systems
751     $att{NOOP}  = "";
752     $att{RM_F}  = "rm -f";
753     $att{RM_RF} = "rm -rf";
754     $att{TOUCH} = "touch";
755     $att{CP} = "cp";
756 }
757
758
759 sub lsdir{
760     my($dir, $regex) = @_;
761     local(*DIR, @ls);
762     opendir(DIR, $_[0] || ".") or die "opendir: $!";
763     @ls = readdir(DIR);
764     closedir(DIR);
765     @ls = grep(/$regex/, @ls) if $regex;
766     @ls;
767 }
768
769
770 sub find_perl{
771     my($self, $ver, $names, $dirs, $trace) = @_;
772     my($name, $dir);
773     print "Looking for perl $ver by these names: @$names, in these dirs: @$dirs\n"
774         if ($trace);
775     foreach $dir (@$dirs){
776         next unless defined $dir; # $att{PERL_SRC} may be undefined
777         foreach $name (@$names){
778             print "checking $dir/$name\n" if ($trace >= 2);
779             if ($Is_VMS) {
780               $name .= ".exe" unless -x "$dir/$name";
781             }
782             next unless -x "$dir/$name";
783             print "executing $dir/$name\n" if ($trace);
784             my($out);
785             if ($Is_VMS) {
786               my($vmscmd) = 'MCR ' . vmsify("$dir/$name");
787               $out = `$vmscmd -e "require $ver; print ""VER_OK\n"""`;
788             } else {
789               $out = `$dir/$name -e 'require $ver; print "VER_OK\n" ' 2>&1`;
790             }
791             return "$dir/$name" if $out =~ /VER_OK/;
792         }
793     }
794     warn "Unable to find a perl $ver (by these names: @$names, in these dirs: @$dirs)\n";
795     0; # false and not empty
796 }
797
798
799 sub post_initialize{
800     "";
801 }
802  
803
804 sub constants {
805     my(@m);
806
807     push @m, "
808 NAME = $att{NAME}
809 DISTNAME = $att{DISTNAME}
810 VERSION = $att{VERSION}
811
812 # In which library should we install this extension?
813 # This is typically the same as PERL_LIB.
814 # (also see INST_LIBDIR and relationship to ROOTEXT)
815 INST_LIB = $att{INST_LIB}
816 INST_ARCHLIB = $att{INST_ARCHLIB}
817
818 # Perl library to use when building the extension
819 PERL_LIB = $att{PERL_LIB}
820 PERL_ARCHLIB = $att{PERL_ARCHLIB}
821
822 # Where is the perl source code located? (Eventually we should
823 # be able to build extensions without requiring the perl source
824 # but that's a way off yet).
825 PERL_SRC = $att{PERL_SRC}
826 # Perl header files (will eventually be under PERL_LIB)
827 PERL_INC = $att{PERL_INC}
828 # Perl binaries
829 PERL = $att{'PERL'}
830 FULLPERL = $att{'FULLPERL'}
831
832 # FULLEXT = Pathname for extension directory (eg DBD/Oracle).
833 # BASEEXT = Basename part of FULLEXT. May be just equal FULLEXT.
834 # ROOTEXT = Directory part of FULLEXT with leading slash (eg /DBD)
835 FULLEXT = $att{FULLEXT}
836 BASEEXT = $att{BASEEXT}
837 ROOTEXT = $att{ROOTEXT}
838
839 # These will be removed later. Use PERL_SRC and BASEEXT instead.
840 TOP = \$(PERL_SRC)
841 EXT = CHANGE_EXT_TO_BASEEXT
842
843 INC = $att{INC}
844 DEFINE = $att{DEFINE}
845 OBJECT = $att{OBJECT}
846 LDFROM = $att{LDFROM}
847 LINKTYPE = $att{LINKTYPE}
848
849 # Source code:
850 XS= ".join(" \\\n\t", sort keys %{$att{XS}})."
851 C = ".join(" \\\n\t", @{$att{C}})."
852 H = ".join(" \\\n\t", @{$att{H}})."
853
854 .SUFFIXES: .xs
855
856 .PRECIOUS: Makefile
857
858 .PHONY: all config static dynamic test 
859
860 # This extension may link to it's own library (see SDBM_File)
861 MYEXTLIB = $att{MYEXTLIB}
862
863 # Where is the Config.pm that we are using/depend on
864 CONFIGDEP = \$(PERL_ARCHLIB)/Config.pm
865 ";
866
867     push @m, '
868 # Where to put things:
869 INST_LIBDIR     = $(INST_LIB)$(ROOTEXT)
870 INST_ARCHLIBDIR = $(INST_ARCHLIB)$(ROOTEXT)
871
872 ';
873
874     push @m, '
875 INST_AUTODIR      = $(INST_LIB)/auto/$(FULLEXT)
876 INST_ARCHAUTODIR  = $(INST_ARCHLIB)/auto/$(FULLEXT)
877 ';
878
879     push @m, '
880 INST_BOOT    = $(INST_ARCHAUTODIR)/$(BASEEXT).bs
881 INST_DYNAMIC = $(INST_ARCHAUTODIR)/$(BASEEXT).$(DLEXT)
882 INST_STATIC  = $(BASEEXT).a
883 INST_PM = '.join(" \\\n\t", sort values %{$att{PM}}).'
884 ';
885
886     join('',@m);
887 }
888
889
890 sub const_cccmd{
891     # This is implemented in the
892     # same manner as extliblist, e.g., do both and compare results during
893     # the transition period.
894   my($cc,$ccflags,$optimize,$large,$split)=@Config{qw(cc ccflags optimize large split)};
895   my($prog, $old);
896
897   chop($old = `cd $att{PERL_SRC}; sh $Config{'shellflags'} ./cflags $att{BASEEXT}.c`)
898         if $att{PERL_SRC};
899
900   # Why is this written this way ?
901   if ($prog = $Config{"$att{BASEEXT}_cflags"}) {
902     my(@o)=`cc=\"$cc\"
903       ccflags=\"$ccflags\"
904       optimize=\"$optimize\"
905       large=\"$large\"
906       split=\"$split\"
907       eval '$prog'
908       echo cc=\$cc
909       echo ccflags=\$ccflags
910       echo optimize=\$optimize
911       echo large=\$large
912       echo split=\$split
913       `;
914     my(%cflags);
915     foreach $line (@o){
916       chomp $line;
917       if ($line =~ /(.*?)\s*=\s*(.*)\s*$/){
918         $cflags{$1} = $2;
919         print STDERR "  $1 = $2" if $Verbose;
920       }
921     }
922     ($cc,$ccflags,$optimize,$large,$split)=@cflags{qw(cc ccflags optimize large split)};
923   }
924
925   my($new) = "$cc -c $ccflags $optimize  $large $split";
926   if (defined($old) and $new ne $old) {
927     warn "Warning (non-fatal): cflags evaluation in MakeMaker differs from shell output\n"
928       ."   package: $att{NAME}\n"
929       ."   old: $old\n"
930       ."   new: $new\n"
931       ."   Using 'old' set.\n"
932       ."Please notify perl5-porters\@nicoh.com\n";
933   }
934   my($cccmd)=($old) ? $old : $new;
935   "CCCMD = $cccmd\n";
936 }
937
938
939 # --- Constants Sections ---
940
941 sub const_config{
942     my(@m,$m);
943     push(@m,"\n# These definitions are from config.sh (via $INC{'Config.pm'})\n");
944     my(%once_only);
945     foreach $m (@{$att{'CONFIG'}}){
946         next if $once_only{$m};
947         warn "CONFIG key '$m' does not exist in Config.pm\n"
948                 unless exists $Config{$m};
949         push @m, "\U$m\E = $Config{$m}\n";
950         $once_only{$m} = 1;
951     }
952     join('', @m);
953 }
954
955
956 sub const_loadlibs{
957     "
958 # $att{NAME} might depend on some other libraries:
959 # (These comments may need revising:)
960 #
961 # Dependent libraries can be linked in one of three ways:
962 #
963 #  1.  (For static extensions) by the ld command when the perl binary
964 #      is linked with the extension library. See EXTRALIBS below.
965 #
966 #  2.  (For dynamic extensions) by the ld command when the shared
967 #      object is built/linked. See LDLOADLIBS below.
968 #
969 #  3.  (For dynamic extensions) by the DynaLoader when the shared
970 #      object is loaded. See BSLOADLIBS below.
971 #
972 # EXTRALIBS =   List of libraries that need to be linked with when
973 #               linking a perl binary which includes this extension
974 #               Only those libraries that actually exist are included.
975 #               These are written to a file and used when linking perl.
976 #
977 # LDLOADLIBS =  List of those libraries which can or must be linked into
978 #               the shared library when created using ld. These may be
979 #               static or dynamic libraries.
980 #
981 # BSLOADLIBS =  List of those libraries that are needed but can be
982 #               linked in dynamically at run time on this platform.
983 #               SunOS/Solaris does not need this because ld records
984 #               the information (from LDLOADLIBS) into the object file.
985 #               This list is used to create a .bs (bootstrap) file.
986 #               The bootstrap file is installed only if it's not empty.
987 #
988 EXTRALIBS  = $att{'EXTRALIBS'}
989 LDLOADLIBS = $att{'LDLOADLIBS'}
990 BSLOADLIBS = $att{'BSLOADLIBS'}
991 ";
992 }
993
994
995 # --- Tool Sections ---
996
997 sub tool_autosplit{
998     my($self, %attribs) = @_;
999     my($asl) = "";
1000     $asl = "\$AutoSplit::Maxlen=$attribs{MAXLEN};" if $attribs{MAXLEN};
1001     q{
1002 # Usage: $(AUTOSPLITFILE) FileToSplit AutoDirToSplitInto
1003 # Remark: the "" around the -I switches are helpful for the VMS support
1004 AUTOSPLITFILE = $(PERL) "-I$(PERL_ARCHLIB)" "-I$(PERL_LIB)" -e 'use AutoSplit;}.$asl.q{ AutoSplit::autosplit_file($$ARGV[0], $$ARGV[1], 0, 1, 1) ;'
1005 };
1006 }
1007
1008
1009 sub tool_xsubpp{
1010     my($xsdir)  = '$(PERL_LIB)/ExtUtils';
1011     # drop back to old location if xsubpp is not in new location yet
1012     $xsdir = '$(PERL_SRC)/ext' unless (-f "$att{PERL_LIB}/ExtUtils/xsubpp");
1013     my(@tmdeps) = ('$(XSUBPPDIR)/typemap');
1014     push(@tmdeps, "typemap") if -f "typemap";
1015     my(@tmargs) = map("-typemap $_", @tmdeps);
1016     "
1017 XSUBPPDIR = $xsdir
1018 XSUBPP = \$(XSUBPPDIR)/xsubpp
1019 XSUBPPDEPS = @tmdeps
1020 XSUBPPARGS = @tmargs
1021 ";
1022 };
1023
1024
1025 sub tools_other{
1026     "
1027 SHELL = /bin/sh
1028 LD = $att{LD}
1029 TOUCH = $att{TOUCH}
1030 CP = $att{CP}
1031 RM_F  = $att{RM_F}
1032 RM_RF = $att{RM_RF}
1033 ".q{
1034 # The following is a portable way to say mkdir -p
1035 MKPATH = $(PERL) -wle '$$"="/"; foreach $$p (@ARGV){ my(@p); foreach(split(/\//,$$p)){ push(@p,$$_); next if -d "@p/"; print "mkdir @p"; mkdir("@p",0777)||die $$! }} exit 0;'
1036 };
1037 }
1038
1039
1040 sub post_constants{
1041     "";
1042 }
1043
1044
1045 # --- Translation Sections ---
1046
1047 sub c_o {
1048     '
1049 .c.o:
1050         $(CCCMD) $(CCCDLFLAGS) -I$(PERL_INC) $(DEFINE) $(INC) $*.c
1051 ';
1052 }
1053
1054 sub xs_c {
1055     '
1056 .xs.c:
1057         $(PERL) $(XSUBPP) $(XSUBPPARGS) $*.xs >xstmp.c && mv xstmp.c $@
1058 ';
1059 }
1060
1061 sub xs_o {      # many makes are too dumb to use xs_c then c_o
1062     '
1063 .xs.o:
1064         $(PERL) $(XSUBPP) $(XSUBPPARGS) $*.xs >xstmp.c && mv xstmp.c $*.c
1065         $(CCCMD) $(CCCDLFLAGS) -I$(PERL_INC) $(DEFINE) $(INC) $*.c
1066 ';
1067 }
1068
1069
1070 # --- Target Sections ---
1071
1072 sub top_targets{
1073     '
1074 all ::  config linkext $(INST_PM)
1075 '.$att{NOOP}.'
1076
1077 config :: Makefile
1078         @$(MKPATH) $(INST_LIBDIR)
1079         @$(MKPATH) $(INST_ARCHAUTODIR)
1080 ';
1081 }
1082
1083 sub linkext {
1084     my($self, %attribs) = @_;
1085     # LINKTYPE => static or dynamic
1086     my($linktype) = $attribs{LINKTYPE} || '$(LINKTYPE)';
1087     "
1088 linkext :: $linktype
1089 $att{NOOP}
1090 ";
1091 }
1092
1093
1094 # --- Dynamic Loading Sections ---
1095
1096 sub dynamic {
1097     '
1098 # $(INST_PM) has been moved to the all: target.
1099 # It remains here for awhile to allow for old usage: "make dynamic"
1100 dynamic :: $(INST_DYNAMIC) $(INST_BOOT) $(INST_PM)
1101 '.$att{NOOP}.'
1102 ';
1103 }
1104
1105 sub dynamic_bs {
1106     my($self, %attribs) = @_;
1107     '
1108 BOOTSTRAP = '."$att{BASEEXT}.bs".'
1109
1110 # As MakeMaker mkbootstrap might not write a file (if none is required)
1111 # we use touch to prevent make continually trying to remake it.
1112 # The DynaLoader only reads a non-empty file.
1113 $(BOOTSTRAP): '.$att{BOOTDEP}.' $(CONFIGDEP) Makefile.PL
1114         $(PERL) -I$(PERL_ARCHLIB) -I$(PERL_LIB) \
1115                 -e \'use ExtUtils::MakeMaker; &mkbootstrap("$(BSLOADLIBS)");\' \
1116                 INST_LIB=$(INST_LIB) INST_ARCHLIB=$(INST_ARCHLIB) PERL_SRC=$(PERL_SRC) NAME=$(NAME)
1117         @$(TOUCH) $(BOOTSTRAP)
1118
1119 $(INST_BOOT): $(BOOTSTRAP)
1120         @'.$att{RM_RF}.' $(INST_BOOT)
1121         '.$att{CP}.' $(BOOTSTRAP) $(INST_BOOT)
1122 ';
1123 }
1124
1125 sub dynamic_lib {
1126     my($self, %attribs) = @_;
1127     my($otherldflags) = $attribs{OTHERLDFLAGS} || "";
1128     my($armaybe) = $attribs{ARMAYBE} || $att{ARMAYBE} || ":";
1129     my($ldfrom) = '$(LDFROM)';
1130     my(@m);
1131     push(@m,'
1132 ARMAYBE = '.$armaybe.'
1133 OTHERLDFLAGS = '.$otherldflags.'
1134
1135 $(INST_DYNAMIC): $(OBJECT) $(MYEXTLIB) $(BOOTSTRAP)
1136         @$(MKPATH) $(INST_ARCHAUTODIR)
1137 ');
1138     if ($armaybe ne ':'){
1139         $ldfrom = "tmp.a";
1140         push(@m,'       $(ARMAYBE) cr '.$ldfrom.' $(OBJECT)'."\n");
1141         push(@m,'       $(RANLIB) '."$ldfrom\n");
1142     }
1143     push(@m,'   $(LD) $(LDDLFLAGS) -o $@ '.$ldfrom.
1144                         ' $(OTHERLDFLAGS) $(MYEXTLIB) $(LDLOADLIBS)'."\n");
1145     join('',@m);
1146 }
1147
1148
1149 # --- Static Loading Sections ---
1150
1151 sub static {
1152     '
1153 # $(INST_PM) has been moved to the all: target.
1154 # It remains here for awhile to allow for old usage: "make static"
1155 static :: $(INST_STATIC) $(INST_PM) 
1156 '.$att{NOOP}.'
1157 ';
1158 }
1159
1160 sub static_lib{
1161     my(@m);
1162     push(@m, <<'END');
1163 $(INST_STATIC): $(OBJECT) $(MYEXTLIB)
1164 END
1165     # If this extension has it's own library (eg SDBM_File)
1166     # then copy that to $(INST_STATIC) and add $(OBJECT) into it.
1167     push(@m, "  $att{CP} \$(MYEXTLIB) \$\@\n") if $att{MYEXTLIB};
1168
1169     push(@m, <<'END');
1170         ar cr $@ $(OBJECT) && $(RANLIB) $@
1171         @: New mechanism - not yet used:
1172         @echo $(EXTRALIBS) > $(INST_ARCHAUTODIR)/extralibs.ld
1173         $(CP) $@ $(INST_ARCHAUTODIR)/
1174 END
1175     push(@m, <<'END') if $att{PERL_SRC};
1176         @: Old mechanism - still needed:
1177         @echo $(EXTRALIBS) >> $(PERL_SRC)/ext.libs
1178 END
1179     join('', "\n",@m);
1180 }
1181
1182
1183 sub installpm {
1184     my($self, %attribs) = @_;
1185     # By default .pm files are split into the architecture independent
1186     # library. This is a good thing. If a specific module requires that
1187     # it's .pm files are split into the architecture specific library
1188     # then it should use: installpm => {SPLITLIB=>'$(INST_ARCHLIB)'}
1189     # Note that installperl currently interferes with this (Config.pm)
1190     # User can disable split by saying: installpm => {SPLITLIB=>''}
1191     my($splitlib) = '$(INST_LIB)'; # NOT arch specific by default
1192     $splitlib = $attribs{SPLITLIB} if exists $attribs{SPLITLIB};
1193     my(@m, $dist);
1194     foreach $dist (sort keys %{$att{PM}}){
1195         my($inst) = $att{PM}->{$dist};
1196         push(@m, "\n# installpm: $dist => $inst, splitlib=$splitlib\n");
1197         push(@m, MY->installpm_x($dist, $inst, $splitlib));
1198         push(@m, "\n");
1199     }
1200     join('', @m);
1201 }
1202
1203 sub installpm_x { # called by installpm per file
1204     my($self, $dist, $inst, $splitlib) = @_;
1205     my($instdir) = $inst =~ m|(.*)/|;
1206     my(@m);
1207     push(@m,"
1208 $inst: $dist
1209 ".'     @'.$att{RM_F}.' $@
1210         @$(MKPATH) '.$instdir.'
1211         '.$att{CP}.' $? $@
1212 ');
1213     push(@m, "\t\$(AUTOSPLITFILE) \$@ $splitlib/auto\n")
1214         if ($splitlib and $inst =~ m/\.pm$/);
1215     join('', @m);
1216 }
1217
1218
1219 # --- Sub-directory Sections ---
1220
1221 sub subdirs {
1222     my(@m);
1223     # This method provides a mechanism to automatically deal with
1224     # subdirectories containing further Makefile.PL scripts.
1225     # It calls the subdir_x() method for each subdirectory.
1226     foreach(<*/Makefile.PL>){
1227         s:/Makefile\.PL$:: ;
1228         print "Including $_ subdirectory" if ($Verbose);
1229         push(@m, MY->subdir_x($_));
1230     }
1231     if (@m){
1232         unshift(@m, "
1233 # The default clean, realclean and test targets in this Makefile
1234 # have automatically been given entries for each subdir.
1235
1236 all :: subdirs
1237 ");
1238     } else {
1239         push(@m, "\n# none")
1240     }
1241     join('',@m);
1242 }
1243
1244 sub runsubdirpl{        # Experimental! See subdir_x section
1245     my($self,$subdir) = @_;
1246     chdir($subdir) or die "chdir($subdir): $!";
1247     require "Makefile.PL";
1248 }
1249
1250 sub subdir_x {
1251     my($self, $subdir) = @_;
1252     my(@m);
1253     # The intention is that the calling Makefile.PL should define the
1254     # $(SUBDIR_MAKEFILE_PL_ARGS) make macro to contain whatever
1255     # information needs to be passed down to the other Makefile.PL scripts.
1256     # If this does not suit your needs you'll need to write your own
1257     # MY::subdir_x() method to override this one.
1258     qq{
1259 config :: $subdir/Makefile
1260         cd $subdir ; \$(MAKE) config INST_LIB=\$(INST_LIB) INST_ARCHLIB=\$(INST_ARCHLIB)  LINKTYPE=\$(LINKTYPE)
1261
1262 $subdir/Makefile: $subdir/Makefile.PL \$(CONFIGDEP)
1263 }.'     @echo "Rebuilding $@ ..."
1264         $(PERL) -I$(PERL_ARCHLIB) -I$(PERL_LIB) \\
1265                 -e "use ExtUtils::MakeMaker; MM->runsubdirpl(qw('.$subdir.'))" \\
1266                 INST_LIB=$(INST_LIB) INST_ARCHLIB=$(INST_ARCHLIB) \\
1267                 $(SUBDIR_MAKEFILE_PL_ARGS)
1268         @echo "Rebuild of $@ complete."
1269 '.qq{
1270
1271 subdirs ::
1272         cd $subdir ; \$(MAKE) all LINKTYPE=\$(LINKTYPE)
1273
1274 };
1275 }
1276
1277
1278 # --- Cleanup and Distribution Sections ---
1279
1280 sub clean {
1281     my($self, %attribs) = @_;
1282     my(@m);
1283     push(@m, '
1284 # Delete temporary files but do not touch installed files. We don\'t delete
1285 # the Makefile here so a later make realclean still has a makefile to use.
1286
1287 clean ::
1288 ');
1289     # clean subdirectories first
1290     push(@m, map("\t-cd $_ && test -f Makefile && \$(MAKE) clean\n",@{$att{DIR}}));
1291     push(@m, "  $att{RM_F} *~ t/*~ *.o *.a mon.out core so_locations \$(BOOTSTRAP) \$(BASEEXT).bso\n");
1292     my(@otherfiles);
1293     # Automatically delete the .c files generated from *.xs files:
1294     push(@otherfiles, values %{$att{XS}});
1295     push(@otherfiles, $attribs{FILES}) if $attribs{FILES};
1296     push(@m, "  $att{RM_RF} @otherfiles\n") if @otherfiles;
1297     push(@m, "  $attribs{POSTOP}\n")   if $attribs{POSTOP};
1298     join("", @m);
1299 }
1300
1301 sub realclean {
1302     my($self, %attribs) = @_;
1303     my(@m);
1304     push(@m,'
1305 # Delete temporary files (via clean) and also delete installed files
1306 realclean purge ::  clean
1307 ');
1308     # clean subdirectories first
1309     push(@m, map("\t-cd $_ && test -f Makefile && \$(MAKE) realclean\n",@{$att{DIR}}));
1310     # try to clean those files first, where we might have no permissions, so Makefile won't be lost
1311     push(@m, "  $att{RM_RF} \$(INST_AUTODIR) \$(INST_ARCHAUTODIR)\n");
1312     push(@m, "  $att{RM_F} \$(INST_DYNAMIC) \$(INST_BOOT) \$(INST_PM)\n");
1313     push(@m, "  $att{RM_F} Makefile \$(INST_STATIC)\n");
1314     my(@otherfiles);
1315     push(@otherfiles, $attribs{FILES}) if $attribs{FILES};
1316     push(@m, "  $att{RM_RF} @otherfiles\n") if @otherfiles;
1317     push(@m, "  $attribs{POSTOP}\n")       if $attribs{POSTOP};
1318     join("", @m);
1319 }
1320
1321
1322 sub distclean {
1323     my($self, %attribs) = @_;
1324     # VERSION should be sanitised before use as a file name
1325     my($tarname)  = $attribs{TARNAME}  || '$(DISTNAME)-$(VERSION)';
1326     my($tarflags) = $attribs{TARFLAGS} || 'cvf';
1327     my($compress) = $attribs{COMPRESS} || 'compress'; # eg gzip
1328     my($preop)    = $attribs{PREOP}  || '@:'; # e.g., update MANIFEST
1329     my($postop)   = $attribs{POSTOP} || '@:';
1330     my($mkfiles)  = join(' ', map("$_/Makefile", ".", @{$att{DIR}}));
1331     "
1332 distclean:     clean
1333         $preop
1334         $att{RM_F} $mkfiles
1335         cd ..; tar $tarflags $tarname.tar \$(BASEEXT)
1336         cd ..; $compress $tarname.tar
1337         $postop
1338 ";
1339 }
1340
1341
1342 # --- Test and Installation Sections ---
1343
1344 sub test {
1345     my($self, %attribs) = @_;
1346     my($tests) = $attribs{TESTS} || (-d "t" ? "t/*.t" : "");
1347     my(@m);
1348     push(@m,"
1349 test :: all
1350 ");
1351     push(@m, <<"END") if $tests;
1352         \$(FULLPERL) -I\$(INST_ARCHLIB) -I\$(INST_LIB) -I\$(PERL_ARCHLIB) -I\$(PERL_LIB) -e 'use Test::Harness; runtests \@ARGV;' $tests
1353 END
1354     push(@m, <<'END') if -f "test.pl";
1355         $(FULLPERL) -I$(INST_ARCHLIB) -I$(INST_LIB) -I$(PERL_ARCHLIB) -I$(PERL_LIB) test.pl
1356 END
1357     push(@m, map("\tcd $_ && test -f Makefile && \$(MAKE) test LINKTYPE=\$(LINKTYPE)\n",@{$att{DIR}}));
1358     push(@m, "\t\@echo 'No tests defined for \$(NAME) extension.'\n") unless @m > 1;
1359     join("", @m);
1360 }
1361
1362
1363 sub install {
1364     my($self, %attribs) = @_;
1365     my(@m);
1366     push(@m, "
1367 install :: all
1368 ");
1369     # install subdirectories first
1370     push(@m, map("\tcd $_ && test -f Makefile && \$(MAKE) install\n",@{$att{DIR}}));
1371
1372     push(@m, "\t: perl5.000 used to autosplit into INST_ARCHLIB, we delete these old files here
1373         $att{RM_F} \$(INSTALLARCHLIB)/auto/\$(FULLEXT)/*.al \$(INSTALLARCHLIB)/auto/\$(FULLEXT)/*.ix
1374         make INST_LIB=\$(INSTALLPRIVLIB) INST_ARCHLIB=\$(INSTALLARCHLIB)
1375 ");
1376
1377     join("",@m);
1378 }
1379
1380 sub force {
1381     '# Phony target to force checking subdirectories.
1382 FORCE:
1383 ';
1384 }
1385
1386
1387 sub perldepend {
1388         my(@m);
1389     push(@m,'
1390 PERL_HDRS = $(PERL_INC)/EXTERN.h $(PERL_INC)/INTERN.h \
1391     $(PERL_INC)/XSUB.h  $(PERL_INC)/av.h        $(PERL_INC)/cop.h \
1392     $(PERL_INC)/cv.h    $(PERL_INC)/dosish.h    $(PERL_INC)/embed.h \
1393     $(PERL_INC)/form.h  $(PERL_INC)/gv.h        $(PERL_INC)/handy.h \
1394     $(PERL_INC)/hv.h    $(PERL_INC)/keywords.h  $(PERL_INC)/mg.h \
1395     $(PERL_INC)/op.h    $(PERL_INC)/opcode.h    $(PERL_INC)/patchlevel.h \
1396     $(PERL_INC)/perl.h  $(PERL_INC)/perly.h     $(PERL_INC)/pp.h \
1397     $(PERL_INC)/proto.h $(PERL_INC)/regcomp.h   $(PERL_INC)/regexp.h \
1398     $(PERL_INC)/scope.h $(PERL_INC)/sv.h        $(PERL_INC)/unixish.h \
1399     $(PERL_INC)/util.h
1400
1401 $(OBJECT) : $(PERL_HDRS)
1402 ');
1403
1404     push(@m,'
1405 $(PERL_INC)/config.h: $(PERL_SRC)/config.sh; cd $(PERL_SRC); /bin/sh config_h.SH
1406 ') if $att{PERL_SRC};
1407
1408     # This needs a better home:
1409     push(@m, join(" ", values %{$att{XS}})." : \$(XSUBPPDEPS)\n")
1410         if %{$att{XS}};
1411     join("\n",@m);
1412 }
1413
1414
1415 sub makefile {
1416     # We do not know what target was originally specified so we
1417     # must force a manual rerun to be sure. But as it would only
1418     # happen very rarely it is not a significant problem.
1419
1420 # This dependency, with no_op, causes problems. Make tries to build OBJECT via no_op!
1421 # So we'll just drop it for now.
1422 #$(OBJECT) : Makefile
1423 #'.$att{NOOP}.'
1424
1425         '
1426 Makefile:       Makefile.PL $(CONFIGDEP)
1427         $(PERL) -I$(PERL_ARCHLIB) -I$(PERL_LIB) Makefile.PL
1428         @echo "Now you must rerun make."; false
1429 ';
1430 }
1431
1432
1433 sub postamble{
1434     "";
1435 }
1436
1437
1438 # --- Determine libraries to use and how to use them ---
1439
1440 sub extliblist{
1441     my($self, $libs) = @_;
1442     return ("", "", "") unless $libs;
1443     print STDERR "Potential libraries are '$libs':" if $Verbose;
1444     my(@new) = MY->new_extliblist($libs);
1445
1446     if ($att{PERL_SRC}){
1447         my(@old) = MY->old_extliblist($libs);
1448         my($oldlibs) = join(" : ",@old);
1449         my($newlibs) = join(" : ",@new);
1450         warn "Warning (non-fatal): $att{NAME} extliblist consistency check failed:\n".
1451             "  old: $oldlibs\n".
1452             "  new: $newlibs\n".
1453             "Using 'new' set. Please notify perl5-porters\@nicoh.com.\n"
1454                 if ("$newlibs" ne "$oldlibs");
1455     }
1456     @new;
1457 }
1458
1459
1460 sub old_extliblist {
1461     my($self, $potential_libs)=@_;
1462     return ("", "", "") unless $potential_libs;
1463     die "old_extliblist requires PERL_SRC" unless $att{PERL_SRC};
1464
1465     my(%attrib, @w);
1466     # Now run ext/util/extliblist to discover what *libs definitions
1467     # are required for the needs of $potential_libs
1468     $ENV{'potential_libs'} = $potential_libs;
1469     my(@o)=`. $att{PERL_SRC}/config.sh
1470             . $att{PERL_SRC}/ext/util/extliblist;
1471             echo EXTRALIBS=\$extralibs
1472             echo BSLOADLIBS=\$dynaloadlibs
1473             echo LDLOADLIBS=\$statloadlibs
1474             `;
1475     foreach $line (@o){
1476         chomp $line;
1477         if ($line =~ /(.*)\s*=\s*(.*)\s*$/){
1478             $attrib{$1} = $2;
1479             print STDERR "      $1 = $2" if $Verbose;
1480         }else{
1481             push(@w, $line);
1482         }
1483     }
1484     print STDERR "Messages from extliblist:\n", join("\n",@w,'')
1485        if @w ;
1486     @attrib{qw(EXTRALIBS BSLOADLIBS LDLOADLIBS)};
1487 }
1488
1489
1490 sub new_extliblist {
1491     my($self, $potential_libs)=@_;
1492     return ("", "", "") unless $potential_libs;
1493
1494     my($so)   = $Config{'so'};
1495     my($libs) = $Config{'libs'};
1496
1497     # compute $extralibs, $bsloadlibs and $ldloadlibs from
1498     # $potential_libs
1499     # this is a rewrite of Andy Dougherty's extliblist in perl
1500     # its home is in <distribution>/ext/util
1501
1502     my(@searchpath); # from "-L/path" entries in $potential_libs
1503     my(@libpath) = split " ", $Config{'libpth'};
1504     my(@ldloadlibs);
1505     my(@bsloadlibs);
1506     my(@extralibs);
1507     my($fullname);
1508     my($pwd) = fastcwd(); # from Cwd.pm
1509
1510     foreach $thislib (split ' ', $potential_libs){
1511
1512         # Handle possible linker path arguments.
1513         if ($thislib =~ s/^(-[LR])//){  # save path flag type
1514             my($ptype) = $1;
1515             unless (-d $thislib){
1516                 warn "$ptype$thislib ignored, directory does not exist\n"
1517                         if $Verbose;
1518                 next;
1519             }
1520             if ($thislib !~ m|^/|) {
1521               warn "Warning: $ptype$thislib changed to $ptype$pwd/$thislib\n";
1522               $thislib = "$pwd/$thislib";
1523             }
1524             push(@searchpath, $thislib);
1525             push(@extralibs,  "$ptype$thislib");
1526             push(@ldloadlibs, "$ptype$thislib");
1527             next;
1528         }
1529
1530         # Handle possible library arguments.
1531         unless ($thislib =~ s/^-l//){
1532           warn "Unrecognized argument in LIBS ignored: '$thislib'\n";
1533           next;
1534         }
1535
1536         my($found_lib)=0;
1537         foreach $thispth (@searchpath, @libpath){
1538
1539             if (@fullname=<${thispth}/lib${thislib}.${so}.[0-9]*>){
1540                 $fullname=$fullname[-1]; #ATTN: 10 looses against 9!
1541             } elsif (-f ($fullname="$thispth/lib$thislib.$so")){
1542           } elsif (-f ($fullname="$thispth/lib${thislib}_s.a")
1543               && ($thislib .= "_s") ){ # we must explicitly ask for _s version
1544             } elsif (-f ($fullname="$thispth/lib$thislib.a")){
1545             } elsif (-f ($fullname="$thispth/Slib$thislib.a")){
1546             } else { 
1547                 warn "$thislib not found in $thispth\n" if $Verbose;
1548                 next;
1549             }
1550             warn "'-l$thislib' found at $fullname\n" if $Verbose;
1551             $found_lib++;
1552
1553             # Now update library lists
1554
1555             # what do we know about this library...
1556             my $is_dyna = ($fullname !~ /\.a$/);
1557             my $in_perl = ($libs =~ /\B-l${thislib}\b/s);
1558
1559             # Do not add it into the list if it is already linked in
1560             # with the main perl executable.
1561             # We have to special-case the NeXT, because all the math is also in libsys_s
1562             unless ( $in_perl || ($Config{'osname'} eq 'next' && $thislib eq 'm') ){
1563                 push(@extralibs, "-l$thislib");
1564             }
1565                         
1566
1567             # We might be able to load this archive file dynamically
1568             if ( $Config{'dlsrc'} =~ /dl_next|dl_dld/){
1569                 # We push -l$thislib instead of $fullname because
1570                 # it avoids hardwiring a fixed path into the .bs file.
1571                 # mkbootstrap will automatically add dl_findfile() to
1572                 # the .bs file if it sees a name in the -l format.
1573                 # USE THIS LATER: push(@bsloadlibs, "-l$thislib"); # " $fullname";
1574                 # USE THIS while checking results against old_extliblist
1575                 push(@bsloadlibs, "$fullname");
1576             } else {
1577                 if ($is_dyna){
1578                     # For SunOS4, do not add in this shared library if
1579                     # it is already linked in the main perl executable
1580                     push(@ldloadlibs, "-l$thislib")
1581                         unless ($in_perl and $Config{'osname'} eq 'sunos');
1582                 } else {
1583                     push(@ldloadlibs, "-l$thislib");
1584                 }
1585             }
1586             last;       # found one here so don't bother looking further
1587         }
1588         warn "Warning (non-fatal): No library found for -l$thislib\n" unless $found_lib>0;
1589     }
1590     ("@extralibs", "@bsloadlibs", "@ldloadlibs");
1591 }
1592
1593
1594 # --- Write a DynaLoader bootstrap file if required
1595
1596 sub mkbootstrap {
1597
1598 =head1 NAME
1599
1600 mkbootstrap
1601
1602 =head1 DESCRIPTION
1603
1604 Make a bootstrap file for use by this system's DynaLoader.
1605 It typically gets called from an extension Makefile.
1606
1607 There is no .bs file supplied with the extension. Instead a _BS file
1608 which has code for the special cases, like posix for berkeley db on the
1609 NeXT.
1610
1611 This file will get parsed, and produce a maybe empty
1612 @DynaLoader::dl_resolve_using array for the current architecture.
1613 That will be extended by $BSLOADLIBS, which was computed by Andy's
1614 extliblist script. If this array still is empty, we do nothing, else
1615 we write a .bs file with an @DynaLoader::dl_resolve_using array, but
1616 without any C<if>s, because there is no longer a need to deal with
1617 special cases.
1618
1619 The _BS file can put some code into the generated .bs file by placing
1620 it in $bscode. This is a handy 'escape' mechanism that may prove
1621 useful in complex situations.
1622
1623 If @DynaLoader::dl_resolve_using contains C<-L*> or C<-l*> entries then
1624 mkbootstrap will automatically add a dl_findfile() call to the
1625 generated .bs file.
1626
1627 =head1 AUTHORS
1628
1629 Andreas Koenig <k@otto.ww.TU-Berlin.DE>, Tim Bunce
1630 <Tim.Bunce@ig.co.uk>, Andy Dougherty <doughera@lafcol.lafayette.edu>
1631
1632 =cut
1633
1634     my($self, @bsloadlibs)=@_;
1635
1636     @bsloadlibs = grep($_, @bsloadlibs); # strip empty libs
1637
1638     print STDERR "      bsloadlibs=@bsloadlibs\n" if $Verbose;
1639
1640     # We need DynaLoader here because we and/or the *_BS file may
1641     # call dl_findfile(). We don't say `use' here because when
1642     # first building perl extensions the DynaLoader will not have
1643     # been built when MakeMaker gets first used.
1644     require DynaLoader;
1645     import DynaLoader;
1646
1647     initialize(@ARGV) unless defined $att{'BASEEXT'};
1648
1649     rename "$att{BASEEXT}.bs", "$att{BASEEXT}.bso";
1650
1651     if (-f "$att{BASEEXT}_BS"){
1652         $_ = "$att{BASEEXT}_BS";
1653         package DynaLoader; # execute code as if in DynaLoader
1654         local($osname, $dlsrc) = (); # avoid warnings
1655         ($osname, $dlsrc) = @Config::Config{qw(osname dlsrc)};
1656         $bscode = "";
1657         unshift @INC, ".";
1658         require $_;
1659         shift @INC;
1660     }
1661
1662     if ($Config{'dlsrc'} =~ /^dl_dld/){
1663         package DynaLoader;
1664         push(@dl_resolve_using, dl_findfile('-lc'));
1665     }
1666
1667     my(@all) = (@bsloadlibs, @DynaLoader::dl_resolve_using);
1668     my($method) = '';
1669     if (@all){
1670         open BS, ">$att{BASEEXT}.bs"
1671                 or die "Unable to open $att{BASEEXT}.bs: $!";
1672         print STDOUT "Writing $att{BASEEXT}.bs\n";
1673         print STDOUT "  containing: @all" if $Verbose;
1674         print BS "# $att{BASEEXT} DynaLoader bootstrap file for $Config{'osname'} architecture.\n";
1675         print BS "# Do not edit this file, changes will be lost.\n";
1676         print BS "# This file was automatically generated by the\n";
1677         print BS "# mkbootstrap routine in ExtUtils/MakeMaker.pm.\n";
1678         print BS "\@DynaLoader::dl_resolve_using = ";
1679         # If @all contains names in the form -lxxx or -Lxxx then it's asking for
1680         # runtime library location so we automatically add a call to dl_findfile()
1681         if (" @all" =~ m/ -[lLR]/){
1682             print BS "  dl_findfile(qw(\n  @all\n  ));\n";
1683         }else{
1684             print BS "  qw(@all);\n";
1685         }
1686         # write extra code if *_BS says so
1687         print BS $DynaLoader::bscode if $DynaLoader::bscode;
1688         print BS "\n1;\n";
1689         close BS;
1690     }
1691
1692     # special handling for systems which needs a list of all global
1693     # symbols exported by a modules to be dynamically linked.
1694     if ($Config{'dlsrc'} =~ /^dl_aix/){
1695        my($bootfunc);
1696        ($bootfunc = $att{NAME}) =~ s/\W/_/g;
1697        open EXP, ">$att{BASEEXT}.exp";
1698        print EXP "#!\nboot_$bootfunc\n";
1699        close EXP;
1700     }
1701 }
1702
1703
1704 # --- Output postprocessing section ---
1705 #nicetext is included to make VMS support easier
1706 sub nicetext { # Just return the input - no action needed
1707     my($self,$text) = @_;
1708     $text;
1709 }
1710  
1711 # the following keeps AutoSplit happy
1712 package ExtUtils::MakeMaker;
1713 1;
1714
1715 __END__