4
0

build_perl_modules.in 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!@PERL@
  2. # SYNTAX:
  3. # build_perl_modules -d dest_dir [-c] [-m] [-t] [-i] [-s <section>] tarball_dir
  4. #
  5. # DESCRIPTION:
  6. # Installs perl modules found in tarball_dir
  7. # Expects a file called install_order, containing one line per distribution name
  8. # Will take action against each distribution in turn
  9. # -d is a necessary destination directory for the perl mods
  10. # If -c is set, will remove the module build directories and exit
  11. # If -e is set, will extract module
  12. # If -m is set, will run perl Makefile.PL and make
  13. # If -t is set, will run make test
  14. # If -i is set, will run make install
  15. # If -s <section> specified will only work on that section in the
  16. # install_order file - defaults to first section only
  17. # Options are discrete. This is because an overall ./configure, make, make test, make install
  18. # are run in different invocations. Obviously, you can't run a -t without a -m, but there's no
  19. # checking here for that
  20. # Can only use base modules
  21. use warnings;
  22. use strict;
  23. use Config;
  24. use Getopt::Std;
  25. use Cwd;
  26. use File::Path;
  27. # remove host site_lib directories to ensure this is a 'full & clean' build of deps
  28. BEGIN: {
  29. my @user_libs = split( /:/, $ENV{PERL5LIB} || "" );
  30. chomp(@user_libs);
  31. # clear out old PERL5LIB to avoid confusion with anything preinstalled
  32. foreach my $lib (@INC) {
  33. next if $lib eq ".";
  34. foreach my $var (qw/ sitelib_stem sitelib sitearch sitearchexp /) {
  35. foreach my $user_lib (@user_libs) {
  36. $lib = '' if ( $lib =~ m/$user_lib/ );
  37. }
  38. $lib = ''
  39. if ( ( $Config{$var} && $lib =~ m/^$Config{$var}/ )
  40. || $lib =~ m/site_perl/ );
  41. }
  42. }
  43. }
  44. my $file_regexp = '(\.pm)?-v?([\d_]+\.?)*\.(?:tgz|tar\.gz)$';
  45. my $have_yaml = 0;
  46. my $have_module_build = 0;
  47. my $opts = {};
  48. getopts( 'd:cemtis:', $opts ) || die "Invalid options";
  49. my $moddir = shift @ARGV
  50. or die "Must specify a directory where tarballs exist";
  51. my $prefix = $opts->{d};
  52. die "Must set a destination directory" unless $prefix;
  53. my $destdir = '';
  54. my $mm_destdir = '';
  55. my $mb_destdir = '';
  56. if ( $ENV{DESTDIR} ) {
  57. $destdir = $ENV{DESTDIR};
  58. $mm_destdir = 'DESTDIR=' . $destdir;
  59. $mb_destdir = '--destdir ' . $destdir;
  60. }
  61. chdir $moddir or die "Cannot change to $moddir";
  62. open F, "install_order" or die "Cannot open install_order file";
  63. my @files = grep { !/^#/ && chop } <F>;
  64. close F;
  65. # Remove linux only perl module from Solaris systems
  66. if ( $^O eq "solaris" ) {
  67. @files = grep { !/Sys-Statistics-Linux/ } @files;
  68. }
  69. my @filelist;
  70. opendir( DIR, "." );
  71. foreach my $found ( readdir(DIR) ) {
  72. push( @filelist, $found )
  73. if ( -f $found && $found =~ m/\.(?:tgz|tar\.gz)$/ );
  74. }
  75. close(DIR);
  76. my $tag = $opts->{s} || "default";
  77. my $in_section = 0;
  78. my @tarballs;
  79. foreach my $f (@files) {
  80. next
  81. if ( !$f || $f =~ m/^\s+$/ || $f =~ m/^\s*#/ ); # ignore all blank lines
  82. $f =~ s/\s+//; # remove all whitespaces from line
  83. $f =~ s/\s+#.*//; # remove all comments from the line
  84. if ( $f =~ m/^(\w+):$/ ) {
  85. if ( $tag && $1 ne $tag && $tag ne "all" ) {
  86. $in_section = 0;
  87. next;
  88. }
  89. $in_section = 1;
  90. $tag = $1 if ( !$tag );
  91. last if ( $1 ne $tag && $tag ne "all" );
  92. next;
  93. }
  94. next if ( !$in_section );
  95. # sort fully qualified names
  96. #$f =~ s/(\.pm)?-v?(\d+\.?)*\.(?:tgz|tar\.gz)//;
  97. #warn("b4 f=$f");
  98. $f =~ s/$file_regexp//;
  99. # Needs to be better. Also, what if there are two with same name?
  100. #warn("f=$f");
  101. my $tarball = ( grep( /^$f$file_regexp/, @filelist ) )[0];
  102. #warn("got f=$f tarball=$tarball");
  103. #eval '$tarball = <' . "$f" . '[-pmv0-9.]*.tar.gz>';
  104. die("Couldn't find tarball for $f in $moddir\n")
  105. unless ( $tarball && -f $tarball );
  106. push @tarballs, $tarball;
  107. ( my $dir = $tarball ) =~ s/\.(?:tgz|tar.gz)$//;
  108. # Need to do cleaning before doing each module in turn
  109. if ( $opts->{c} ) {
  110. print "Cleaning $dir", $/;
  111. rmtree($dir) if ($dir);
  112. }
  113. }
  114. if ( $opts->{c} ) {
  115. print "Finished cleaning", $/;
  116. exit;
  117. }
  118. my $libs = "$destdir/$prefix/lib:$destdir/$prefix/lib/$Config{archname}";
  119. my $topdir = cwd();
  120. # set an initial value if there isn't one already
  121. # Need to use PERL5LIB to ensure we get pre-installed mods from earlier
  122. # tags in the install_order file
  123. $ENV{PERL5LIB} ||= q{};
  124. # Set Module::AutoInstall to ignore CPAN, to avoid trying to pull dependencies in
  125. $ENV{PERL_AUTOINSTALL} = "--skipdeps";
  126. # keep a record of how many times a module build is done. This is so they may
  127. # be built a second time to include optional prereq's that couldn't
  128. # previously be built due to circular dependencies
  129. my %built_modules;
  130. foreach my $tarball (@tarballs) {
  131. ( my $dir = $tarball ) =~ s/\.(?:tgz|tar.gz)$//;
  132. die if ( $dir eq "exit" );
  133. if ( $opts->{e} ) {
  134. unless ( -e $dir ) {
  135. print 'Extracting ', $tarball, $/;
  136. system("gunzip -c $tarball | tar -xf -") == 0
  137. or die "Cannot extract $tarball";
  138. }
  139. next unless ( $opts->{m} || $opts->{t} || $opts->{i} );
  140. }
  141. # Need to add this so all modules is are for subsequent ones
  142. # Done here to partial previous builds can be continued
  143. $ENV{PERL5LIB} = "$topdir/$dir/blib/arch:" . $ENV{PERL5LIB}; # Required for IO-Compress, I think
  144. $ENV{PERL5LIB} = "$topdir/$dir/blib/lib:" . $ENV{PERL5LIB};
  145. # PathTools does something weird where it removes blib from @INC. We manually force ExtUtils::MakeMaker to be included
  146. $ENV{PERL5LIB} = "$topdir/$dir/lib:" . $ENV{PERL5LIB} if ($dir =~/ExtUtils-MakeMaker/);
  147. # warn("PERL5LIB=$ENV{PERL5LIB}");
  148. if ( !$have_yaml ) {
  149. $have_yaml = 0;
  150. }
  151. if ( !$have_module_build ) {
  152. $have_module_build = check_for_module('Module::Build');
  153. }
  154. if ( $opts->{m} ) {
  155. # Don't compile if already done - this is because of invocating this
  156. # script at different stages
  157. print "******************** $tarball\n";
  158. if ( $built_modules{$dir} || !-f "$dir/Makefile" && !-f "$dir/Build" ) {
  159. $built_modules{$dir}++;
  160. my @missing;
  161. chdir "$topdir/$dir" or die "Can't chdir into $dir";
  162. warn("\nWorking in: $topdir/$dir\n\n");
  163. # Another horrible hack. XML-Parser uses special Makefile variables, so we add these on here for Solaris only
  164. my $extra_args = "";
  165. if ( $^O eq "solaris" && $dir =~ /^XML-Parser-/ ) {
  166. $extra_args = "EXPATLIBPATH=/usr/sfw/lib EXPATINCPATH=/usr/sfw/share/src/expat/lib/";
  167. }
  168. #warn("PERL5LIB=$ENV{PERL5LIB}\n");
  169. if ( -f "Build.PL" && $have_module_build ) {
  170. warn("Using Build.PL\n");
  171. }
  172. elsif ( -f 'Makefile.PL' ) {
  173. warn("Using Makefile.PL\n");
  174. # Horribly hacky - remove xdefine if this is Time-HiRes
  175. # because the subsequent perl Makefile.PL will fail
  176. if ( $dir =~ /Time-HiRes/ ) {
  177. unlink "xdefine";
  178. }
  179. }
  180. else {
  181. die "No Makefile.PL nor Build.PL found";
  182. }
  183. my $command;
  184. if ( -f "Build.PL" && $have_module_build ) {
  185. open( CMD, "|-", "@PERL@ Build.PL $mb_destdir --install_base=$prefix --install_path lib=$prefix/lib --install_path arch=$prefix/lib/$Config{archname} --install_path bin=$prefix/bin --install_path script=$prefix/bin --install_path bindoc=$prefix/man/man1 --install_path libdoc=$prefix/man/man3" ) || die "Can't run perl Build.PL";
  186. $command = "./Build";
  187. }
  188. elsif ( -f 'Makefile.PL' ) {
  189. open( CMD, "|-", "@PERL@ Makefile.PL $mm_destdir INSTALL_BASE=$prefix INSTALLDIRS=site INSTALLSITELIB=$prefix/lib INSTALLSITEARCH=$prefix/lib/$Config{archname} $extra_args" ) || die "Can't run perl Makefile.PL";
  190. $command = "make";
  191. }
  192. else {
  193. die "No Makefile.PL nor Build.PL found";
  194. }
  195. close(CMD);
  196. system($command) == 0
  197. or die "Can't run $command. Please\n\trm -rf $topdir/$dir\nto remake from this point)";
  198. chdir $topdir or die "Can't chdir to top";
  199. }
  200. }
  201. chdir $dir or die "Can't chdir into $dir";
  202. if ( $opts->{t} ) {
  203. warn("****** Testing $dir ****** \n");
  204. if ( -f "Build.PL" ) {
  205. system("./Build test") == 0
  206. or die "'Build test' failed in $dir: $!\n";
  207. }
  208. else {
  209. system("make test") == 0
  210. or die "'make test' failed in $dir: $!\n";
  211. }
  212. }
  213. if ( $opts->{i} && !-f 'installed' ) {
  214. # Need to set this so that XML::SAX will install ParserDetails.ini by finding the right XML::SAX copy
  215. # Also makes sense to do this anyway, as I guess CPAN must be doing this for it to usually work
  216. my $saved_PERL5LIB = $ENV{PERL5LIB};
  217. $ENV{PERL5LIB} = "$destdir/$prefix/lib:$saved_PERL5LIB";
  218. if ( -f "Build" ) {
  219. system("./Build install") == 0
  220. or die "Can't run make install: $!\n";
  221. }
  222. else {
  223. system("make install") == 0
  224. or die "Can't run make install: $!\n";
  225. }
  226. $ENV{PERL5LIB} = $saved_PERL5LIB;
  227. open my $install_flag_file, '>', 'installed'
  228. or die 'Unable to touch "installed": ', $!, $/;
  229. close $install_flag_file
  230. or die 'Unable to close "installed": ', $!, $/;
  231. }
  232. chdir $topdir or die "Can't go back to $topdir";
  233. }
  234. sub check_for_module {
  235. my ($module) = @_;
  236. warn 'Checking if ', $module, ' is available yet...', $/;
  237. if ( system("$^X -M$module -e 0 2>/dev/null") == 0 ) {
  238. warn '... yes!', $/;
  239. return 1;
  240. }
  241. warn '... no!', $/;
  242. return 0;
  243. }