Source code of Windows XP (NT5)
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.

374 lines
12 KiB

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