home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / perl / Perl / configpm < prev    next >
Text File  |  1995-05-25  |  5KB  |  205 lines

  1. #!./miniperl -w
  2.  
  3. $config_pm = $ARGV[0] || 'lib/Config.pm';
  4. @ARGV = "./config.sh";
  5.  
  6. # list names to put first (and hence lookup fastest)
  7. @fast = qw(osname osvers so libpth archlib
  8.     sharpbang startsh shsharp
  9.     dynamic_ext static_ext extensions dl_src
  10.     sig_name ccflags cppflags intsize);
  11.  
  12. # names of things which may need to have slashes changed to double-colons
  13. @extensions = qw(dynamic_ext static_ext extensions known_extensions);
  14.  
  15.  
  16. open CONFIG, ">$config_pm" or die "Can't open $config_pm: $!\n";
  17. $myver = sprintf("%.3f", $]);
  18. print CONFIG <<"ENDOFBEG";
  19. package Config;
  20. require Exporter;
  21. \@ISA = (Exporter);
  22. \@EXPORT = qw(%Config);
  23.  
  24. \$] == $myver or die sprintf
  25.     "Perl lib version ($myver) doesn't match executable version (%.3f)\\n", \$];
  26.  
  27. # This file was created by configpm when Perl was built. Any changes
  28. # made to this file will be lost the next time perl is built.
  29.  
  30. ENDOFBEG
  31.  
  32. print CONFIG <<'EndOfPod';
  33. =head1 NAME
  34.  
  35. Config - access Perl configuration option
  36.  
  37. =head1 SYNOPSIS
  38.  
  39.     use Config;
  40.     if ($Config{'cc'} =~ /gcc/) {
  41.     print "built by gcc\n";
  42.     } 
  43.  
  44. =head1 DESCRIPTION
  45.  
  46. The Config module contains everything that was available to the
  47. C<Configure> program at Perl build time.  Shell variables from
  48. F<config.sh> are stored in the readonly-variable C<%Config>, indexed by
  49. their names.
  50.  
  51. =head1 EXAMPLE
  52.  
  53. Here's a more sophisticated example of using %Config:
  54.  
  55.     use Config;
  56.  
  57.     defined $Config{sig_name} || die "No sigs?";
  58.     foreach $name (split(' ', $Config{sig_name})) {
  59.     $signo{$name} = $i;
  60.     $signame[$i] = $name;
  61.     $i++;
  62.     }   
  63.  
  64.     print "signal #17 = $signame[17]\n";
  65.     if ($signo{ALRM}) { 
  66.     print "SIGALRM is $signo{ALRM}\n";
  67.     }   
  68.  
  69. =head1 NOTE
  70.  
  71. This module contains a good example of how to make a variable
  72. readonly to those outside of it.  
  73.  
  74. =cut
  75.  
  76. EndOfPod
  77.  
  78. @fast{@fast} = @fast;
  79. @extensions{@extensions} = @extensions;
  80. @non_v=();
  81. @v_fast=();
  82. @v_others=();
  83.  
  84. while (<>) {
  85.     next if m:^#!/bin/sh:;
  86.     # Catch CONFIG=true and PATCHLEVEL=n line from Configure.
  87.     s/^(\w+)=(true|\d+)\s*$/$1='$2'\n/;
  88.     unless (m/^(\w+)='(.*)'\s*$/){
  89.     push(@non_v, "#$_"); # not a name='value' line
  90.     next;
  91.     }
  92.     $name = $1;
  93.     if ($extensions{$name}) { s,/,::,g }
  94.     if (!$fast{$name}){ push(@v_others, $_); next; }
  95.     push(@v_fast,$_);
  96. }
  97.  
  98. foreach(@non_v){ print CONFIG $_ }
  99.  
  100. print CONFIG "\n",
  101.     "\$config_sh=<<'!END!OF!CONFIG!';\n",
  102.     join("", @v_fast, sort @v_others),
  103.     "!END!OF!CONFIG!\n\n";
  104.  
  105.  
  106. print CONFIG <<'ENDOFEND';
  107.  
  108. tie %Config, Config;
  109. sub TIEHASH { bless {} }
  110. sub FETCH { 
  111.     # check for cached value (which maybe undef so we use exists not defined)
  112.     return $_[0]->{$_[1]} if (exists $_[0]->{$_[1]});
  113.  
  114.     my($value); # search for the item in the big $config_sh string
  115.     return undef unless (($value) = $config_sh =~ m/^$_[1]='(.*)'\s*$/m);
  116.  
  117.     $value = undef if $value eq 'undef'; # So we can say "if $Config{'foo'}".
  118.     $_[0]->{$_[1]} = $value; # cache it
  119.     return $value;
  120. }
  121.  
  122. sub FIRSTKEY {
  123.     $prevpos = 0;
  124.     my $key;
  125.     ($key) = $config_sh =~ m/^(.*)=/;
  126.     $key;
  127. }
  128.  
  129. sub NEXTKEY {
  130.     my ($pos, $len);
  131.     $pos = $prevpos;
  132.     $pos = index( $config_sh, "\n", $pos) + 1;
  133.     $prevpos = $pos;
  134.     $len = index( $config_sh, "=", $pos) - $pos;
  135.     $len > 0 ? substr( $config_sh, $pos, $len) : undef;
  136. }
  137.  
  138. sub EXISTS{ 
  139.      exists($_[0]->{$_[1]})  or  $config_sh =~ m/^$_[1]=/m; 
  140. }
  141.  
  142. sub readonly { die "\%Config::Config is read-only\n" }
  143.  
  144. sub myconfig {
  145.     my($output);
  146.     
  147.     $output = <<'END';
  148. Summary of my $package (patchlevel $PATCHLEVEL) configuration:
  149.   Platform:
  150.     osname=$osname, osver=$osvers, archname=$archname
  151.     uname='$myuname'
  152.     hint=$hint
  153.   Compiler:
  154.     cc='$cc', optimize='$optimize'
  155.     cppflags='$cppflags'
  156.     ccflags ='$ccflags'
  157.     ldflags ='$ldflags'
  158.     stdchar='$stdchar', d_stdstdio=$d_stdstdio, usevfork=$usevfork
  159.     voidflags=$voidflags, castflags=$castflags, d_casti32=$d_casti32, d_castneg=$d_castneg
  160.     intsize=$intsize, alignbytes=$alignbytes, usemymalloc=$usemymalloc, randbits=$randbits
  161.   Libraries:
  162.     so=$so
  163.     libpth=$libpth
  164.     libs=$libs
  165.     libc=$libc
  166.   Dynamic Linking:
  167.     dlsrc=$dlsrc, dlext=$dlext, d_dlsymun=$d_dlsymun
  168.     cccdlflags='$cccdlflags', ccdlflags='$ccdlflags', lddlflags='$lddlflags'
  169.  
  170. END
  171.     $output =~ s/\$(\w+)/$Config{$1}/ge;
  172.     $output;
  173. }
  174.  
  175. sub STORE { &readonly }
  176. sub DELETE{ &readonly }
  177. sub CLEAR { &readonly }
  178.  
  179.  
  180. 1;
  181. ENDOFEND
  182.  
  183. close(CONFIG);
  184.  
  185. # Now do some simple tests on the Config.pm file we have created
  186. unshift(@INC,'lib');
  187. require $config_pm;
  188. import Config;
  189.  
  190. die "$0: $config_pm not valid"
  191.     unless $Config{'CONFIG'} eq 'true';
  192.  
  193. die "$0: error processing $config_pm"
  194.     if defined($Config{'an impossible name'})
  195.     or $Config{'CONFIG'} ne 'true' # test cache
  196.     ;
  197.  
  198. die "$0: error processing $config_pm"
  199.     if eval '$Config{"cc"} = 1'
  200.     or eval 'delete $Config{"cc"}'
  201.     ;
  202.  
  203.  
  204. exit 0;
  205.