Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

428 lines
13 KiB

  1. package ExtUtils::Install;
  2. use 5.005_64;
  3. our(@ISA, @EXPORT, $VERSION);
  4. $VERSION = substr q$Revision: 1.28 $, 10;
  5. # $Date: 1998/01/25 07:08:24 $
  6. use Exporter;
  7. use Carp ();
  8. use Config qw(%Config);
  9. @ISA = ('Exporter');
  10. @EXPORT = ('install','uninstall','pm_to_blib', 'install_default');
  11. $Is_VMS = $^O eq 'VMS';
  12. my $splitchar = $^O eq 'VMS' ? '|' : ($^O eq 'os2' || $^O eq 'dos') ? ';' : ':';
  13. my @PERL_ENV_LIB = split $splitchar, defined $ENV{'PERL5LIB'} ? $ENV{'PERL5LIB'} : $ENV{'PERLLIB'} || '';
  14. my $Inc_uninstall_warn_handler;
  15. # install relative to here
  16. my $INSTALL_ROOT = $ENV{PERL_INSTALL_ROOT};
  17. use File::Spec;
  18. sub install_rooted_file {
  19. if (defined $INSTALL_ROOT) {
  20. MY->catfile($INSTALL_ROOT, $_[0]);
  21. } else {
  22. $_[0];
  23. }
  24. }
  25. sub install_rooted_dir {
  26. if (defined $INSTALL_ROOT) {
  27. MY->catdir($INSTALL_ROOT, $_[0]);
  28. } else {
  29. $_[0];
  30. }
  31. }
  32. #our(@EXPORT, @ISA, $Is_VMS);
  33. #use strict;
  34. sub forceunlink {
  35. chmod 0666, $_[0];
  36. unlink $_[0] or Carp::croak("Cannot forceunlink $_[0]: $!")
  37. }
  38. sub install {
  39. my($hash,$verbose,$nonono,$inc_uninstall) = @_;
  40. $verbose ||= 0;
  41. $nonono ||= 0;
  42. use Cwd qw(cwd);
  43. use ExtUtils::MakeMaker; # to implement a MY class
  44. use ExtUtils::Packlist;
  45. use File::Basename qw(dirname);
  46. use File::Copy qw(copy);
  47. use File::Find qw(find);
  48. use File::Path qw(mkpath);
  49. use File::Compare qw(compare);
  50. my(%hash) = %$hash;
  51. my(%pack, $dir, $warn_permissions);
  52. my($packlist) = ExtUtils::Packlist->new();
  53. # -w doesn't work reliably on FAT dirs
  54. $warn_permissions++ if $^O eq 'MSWin32';
  55. local(*DIR);
  56. for (qw/read write/) {
  57. $pack{$_}=$hash{$_};
  58. delete $hash{$_};
  59. }
  60. my($source_dir_or_file);
  61. foreach $source_dir_or_file (sort keys %hash) {
  62. #Check if there are files, and if yes, look if the corresponding
  63. #target directory is writable for us
  64. opendir DIR, $source_dir_or_file or next;
  65. for (readdir DIR) {
  66. next if $_ eq "." || $_ eq ".." || $_ eq ".exists";
  67. my $targetdir = install_rooted_dir($hash{$source_dir_or_file});
  68. if (-w $targetdir ||
  69. mkpath($targetdir)) {
  70. last;
  71. } else {
  72. warn "Warning: You do not have permissions to " .
  73. "install into $hash{$source_dir_or_file}"
  74. unless $warn_permissions++;
  75. }
  76. }
  77. closedir DIR;
  78. }
  79. my $tmpfile = install_rooted_file($pack{"read"});
  80. $packlist->read($tmpfile) if (-f $tmpfile);
  81. my $cwd = cwd();
  82. my($source);
  83. MOD_INSTALL: foreach $source (sort keys %hash) {
  84. #copy the tree to the target directory without altering
  85. #timestamp and permission and remember for the .packlist
  86. #file. The packlist file contains the absolute paths of the
  87. #install locations. AFS users may call this a bug. We'll have
  88. #to reconsider how to add the means to satisfy AFS users also.
  89. #October 1997: we want to install .pm files into archlib if
  90. #there are any files in arch. So we depend on having ./blib/arch
  91. #hardcoded here.
  92. my $targetroot = install_rooted_dir($hash{$source});
  93. if ($source eq "blib/lib" and
  94. exists $hash{"blib/arch"} and
  95. directory_not_empty("blib/arch")) {
  96. $targetroot = install_rooted_dir($hash{"blib/arch"});
  97. print "Files found in blib/arch: installing files in blib/lib into architecture dependent library tree\n";
  98. }
  99. chdir($source) or next;
  100. find(sub {
  101. my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  102. $atime,$mtime,$ctime,$blksize,$blocks) = stat;
  103. return unless -f _;
  104. return if $_ eq ".exists";
  105. my $targetdir = MY->catdir($targetroot, $File::Find::dir);
  106. my $targetfile = MY->catfile($targetdir, $_);
  107. my $diff = 0;
  108. if ( -f $targetfile && -s _ == $size) {
  109. # We have a good chance, we can skip this one
  110. $diff = compare($_,$targetfile);
  111. } else {
  112. print "$_ differs\n" if $verbose>1;
  113. $diff++;
  114. }
  115. if ($diff){
  116. if (-f $targetfile){
  117. forceunlink($targetfile) unless $nonono;
  118. } else {
  119. mkpath($targetdir,0,0755) unless $nonono;
  120. print "mkpath($targetdir,0,0755)\n" if $verbose>1;
  121. }
  122. copy($_,$targetfile) unless $nonono;
  123. print "Installing $targetfile\n";
  124. utime($atime,$mtime + $Is_VMS,$targetfile) unless $nonono>1;
  125. print "utime($atime,$mtime,$targetfile)\n" if $verbose>1;
  126. $mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
  127. chmod $mode, $targetfile;
  128. print "chmod($mode, $targetfile)\n" if $verbose>1;
  129. } else {
  130. print "Skipping $targetfile (unchanged)\n" if $verbose;
  131. }
  132. if (! defined $inc_uninstall) { # it's called
  133. } elsif ($inc_uninstall == 0){
  134. inc_uninstall($_,$File::Find::dir,$verbose,1); # nonono set to 1
  135. } else {
  136. inc_uninstall($_,$File::Find::dir,$verbose,0); # nonono set to 0
  137. }
  138. $packlist->{$targetfile}++;
  139. }, ".");
  140. chdir($cwd) or Carp::croak("Couldn't chdir to $cwd: $!");
  141. }
  142. if ($pack{'write'}) {
  143. $dir = install_rooted_dir(dirname($pack{'write'}));
  144. mkpath($dir,0,0755);
  145. print "Writing $pack{'write'}\n";
  146. $packlist->write(install_rooted_file($pack{'write'}));
  147. }
  148. }
  149. sub directory_not_empty ($) {
  150. my($dir) = @_;
  151. my $files = 0;
  152. find(sub {
  153. return if $_ eq ".exists";
  154. if (-f) {
  155. $File::Find::prune++;
  156. $files = 1;
  157. }
  158. }, $dir);
  159. return $files;
  160. }
  161. sub install_default {
  162. @_ < 2 or die "install_default should be called with 0 or 1 argument";
  163. my $FULLEXT = @_ ? shift : $ARGV[0];
  164. defined $FULLEXT or die "Do not know to where to write install log";
  165. my $INST_LIB = MM->catdir(MM->curdir,"blib","lib");
  166. my $INST_ARCHLIB = MM->catdir(MM->curdir,"blib","arch");
  167. my $INST_BIN = MM->catdir(MM->curdir,'blib','bin');
  168. my $INST_SCRIPT = MM->catdir(MM->curdir,'blib','script');
  169. my $INST_MAN1DIR = MM->catdir(MM->curdir,'blib','man1');
  170. my $INST_MAN3DIR = MM->catdir(MM->curdir,'blib','man3');
  171. install({
  172. read => "$Config{sitearchexp}/auto/$FULLEXT/.packlist",
  173. write => "$Config{installsitearch}/auto/$FULLEXT/.packlist",
  174. $INST_LIB => (directory_not_empty($INST_ARCHLIB)) ?
  175. $Config{installsitearch} :
  176. $Config{installsitelib},
  177. $INST_ARCHLIB => $Config{installsitearch},
  178. $INST_BIN => $Config{installbin} ,
  179. $INST_SCRIPT => $Config{installscript},
  180. $INST_MAN1DIR => $Config{installman1dir},
  181. $INST_MAN3DIR => $Config{installman3dir},
  182. },1,0,0);
  183. }
  184. sub uninstall {
  185. use ExtUtils::Packlist;
  186. my($fil,$verbose,$nonono) = @_;
  187. die "no packlist file found: $fil" unless -f $fil;
  188. # my $my_req = $self->catfile(qw(auto ExtUtils Install forceunlink.al));
  189. # require $my_req; # Hairy, but for the first
  190. my ($packlist) = ExtUtils::Packlist->new($fil);
  191. foreach (sort(keys(%$packlist))) {
  192. chomp;
  193. print "unlink $_\n" if $verbose;
  194. forceunlink($_) unless $nonono;
  195. }
  196. print "unlink $fil\n" if $verbose;
  197. forceunlink($fil) unless $nonono;
  198. }
  199. sub inc_uninstall {
  200. my($file,$libdir,$verbose,$nonono) = @_;
  201. my($dir);
  202. my %seen_dir = ();
  203. foreach $dir (@INC, @PERL_ENV_LIB, @Config{qw(archlibexp
  204. privlibexp
  205. sitearchexp
  206. sitelibexp)}) {
  207. next if $dir eq ".";
  208. next if $seen_dir{$dir}++;
  209. my($targetfile) = MY->catfile($dir,$libdir,$file);
  210. next unless -f $targetfile;
  211. # The reason why we compare file's contents is, that we cannot
  212. # know, which is the file we just installed (AFS). So we leave
  213. # an identical file in place
  214. my $diff = 0;
  215. if ( -f $targetfile && -s _ == -s $file) {
  216. # We have a good chance, we can skip this one
  217. $diff = compare($file,$targetfile);
  218. } else {
  219. print "#$file and $targetfile differ\n" if $verbose>1;
  220. $diff++;
  221. }
  222. next unless $diff;
  223. if ($nonono) {
  224. if ($verbose) {
  225. $Inc_uninstall_warn_handler ||= new ExtUtils::Install::Warn;
  226. $libdir =~ s|^\./||s ; # That's just cosmetics, no need to port. It looks prettier.
  227. $Inc_uninstall_warn_handler->add("$libdir/$file",$targetfile);
  228. }
  229. # if not verbose, we just say nothing
  230. } else {
  231. print "Unlinking $targetfile (shadowing?)\n";
  232. forceunlink($targetfile);
  233. }
  234. }
  235. }
  236. sub run_filter {
  237. my ($cmd, $src, $dest) = @_;
  238. open(my $CMD, "|$cmd >$dest") || die "Cannot fork: $!";
  239. open(my $SRC, $src) || die "Cannot open $src: $!";
  240. my $buf;
  241. my $sz = 1024;
  242. while (my $len = sysread($SRC, $buf, $sz)) {
  243. syswrite($CMD, $buf, $len);
  244. }
  245. close $SRC;
  246. close $CMD or die "Filter command '$cmd' failed for $src";
  247. }
  248. sub pm_to_blib {
  249. my($fromto,$autodir,$pm_filter) = @_;
  250. use File::Basename qw(dirname);
  251. use File::Copy qw(copy);
  252. use File::Path qw(mkpath);
  253. use File::Compare qw(compare);
  254. use AutoSplit;
  255. # my $my_req = $self->catfile(qw(auto ExtUtils Install forceunlink.al));
  256. # require $my_req; # Hairy, but for the first
  257. if (!ref($fromto) && -r $fromto)
  258. {
  259. # Win32 has severe command line length limitations, but
  260. # can generate temporary files on-the-fly
  261. # so we pass name of file here - eval it to get hash
  262. open(FROMTO,"<$fromto") or die "Cannot open $fromto:$!";
  263. my $str = '$fromto = {qw{'.join('',<FROMTO>).'}}';
  264. eval $str;
  265. close(FROMTO);
  266. }
  267. mkpath($autodir,0,0755);
  268. foreach (keys %$fromto) {
  269. my $dest = $fromto->{$_};
  270. next if -f $dest && -M $dest < -M $_;
  271. # When a pm_filter is defined, we need to pre-process the source first
  272. # to determine whether it has changed or not. Therefore, only perform
  273. # the comparison check when there's no filter to be ran.
  274. # -- RAM, 03/01/2001
  275. my $need_filtering = defined $pm_filter && length $pm_filter && /\.pm$/;
  276. if (!$need_filtering && 0 == compare($_,$dest)) {
  277. print "Skip $dest (unchanged)\n";
  278. next;
  279. }
  280. if (-f $dest){
  281. forceunlink($dest);
  282. } else {
  283. mkpath(dirname($dest),0,0755);
  284. }
  285. if ($need_filtering) {
  286. run_filter($pm_filter, $_, $dest);
  287. print "$pm_filter <$_ >$dest\n";
  288. } else {
  289. copy($_,$dest);
  290. print "cp $_ $dest\n";
  291. }
  292. my($mode,$atime,$mtime) = (stat)[2,8,9];
  293. utime($atime,$mtime+$Is_VMS,$dest);
  294. chmod(0444 | ( $mode & 0111 ? 0111 : 0 ),$dest);
  295. next unless /\.pm$/;
  296. autosplit($dest,$autodir);
  297. }
  298. }
  299. package ExtUtils::Install::Warn;
  300. sub new { bless {}, shift }
  301. sub add {
  302. my($self,$file,$targetfile) = @_;
  303. push @{$self->{$file}}, $targetfile;
  304. }
  305. sub DESTROY {
  306. unless(defined $INSTALL_ROOT) {
  307. my $self = shift;
  308. my($file,$i,$plural);
  309. foreach $file (sort keys %$self) {
  310. $plural = @{$self->{$file}} > 1 ? "s" : "";
  311. print "## Differing version$plural of $file found. You might like to\n";
  312. for (0..$#{$self->{$file}}) {
  313. print "rm ", $self->{$file}[$_], "\n";
  314. $i++;
  315. }
  316. }
  317. $plural = $i>1 ? "all those files" : "this file";
  318. print "## Running 'make install UNINST=1' will unlink $plural for you.\n";
  319. }
  320. }
  321. 1;
  322. __END__
  323. =head1 NAME
  324. ExtUtils::Install - install files from here to there
  325. =head1 SYNOPSIS
  326. B<use ExtUtils::Install;>
  327. B<install($hashref,$verbose,$nonono);>
  328. B<uninstall($packlistfile,$verbose,$nonono);>
  329. B<pm_to_blib($hashref);>
  330. =head1 DESCRIPTION
  331. Both install() and uninstall() are specific to the way
  332. ExtUtils::MakeMaker handles the installation and deinstallation of
  333. perl modules. They are not designed as general purpose tools.
  334. install() takes three arguments. A reference to a hash, a verbose
  335. switch and a don't-really-do-it switch. The hash ref contains a
  336. mapping of directories: each key/value pair is a combination of
  337. directories to be copied. Key is a directory to copy from, value is a
  338. directory to copy to. The whole tree below the "from" directory will
  339. be copied preserving timestamps and permissions.
  340. There are two keys with a special meaning in the hash: "read" and
  341. "write". After the copying is done, install will write the list of
  342. target files to the file named by C<$hashref-E<gt>{write}>. If there is
  343. another file named by C<$hashref-E<gt>{read}>, the contents of this file will
  344. be merged into the written file. The read and the written file may be
  345. identical, but on AFS it is quite likely that people are installing to a
  346. different directory than the one where the files later appear.
  347. install_default() takes one or less arguments. If no arguments are
  348. specified, it takes $ARGV[0] as if it was specified as an argument.
  349. The argument is the value of MakeMaker's C<FULLEXT> key, like F<Tk/Canvas>.
  350. This function calls install() with the same arguments as the defaults
  351. the MakeMaker would use.
  352. The argument-less form is convenient for install scripts like
  353. perl -MExtUtils::Install -e install_default Tk/Canvas
  354. Assuming this command is executed in a directory with a populated F<blib>
  355. directory, it will proceed as if the F<blib> was build by MakeMaker on
  356. this machine. This is useful for binary distributions.
  357. uninstall() takes as first argument a file containing filenames to be
  358. unlinked. The second argument is a verbose switch, the third is a
  359. no-don't-really-do-it-now switch.
  360. pm_to_blib() takes a hashref as the first argument and copies all keys
  361. of the hash to the corresponding values efficiently. Filenames with
  362. the extension pm are autosplit. Second argument is the autosplit
  363. directory. If third argument is not empty, it is taken as a filter command
  364. to be ran on each .pm file, the output of the command being what is finally
  365. copied, and the source for auto-splitting.
  366. You can have an environment variable PERL_INSTALL_ROOT set which will
  367. be prepended as a directory to each installed file (and directory).
  368. =cut