Update boot scripts for git
[ghc-hetmet.git] / boot-pkgs
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use File::Path 'rmtree';
6 use File::Basename;
7
8 my @library_dirs = ();
9 my @tarballs = glob("libraries/tarballs/*");
10
11 my $tarball;
12 my $package;
13 my $stamp;
14
15 for $tarball (@tarballs) {
16     $package = $tarball;
17     $package =~ s#^libraries/tarballs/##;
18     $package =~ s/-[0-9.]*(-snapshot)?\.tar\.gz$//;
19
20     # Sanity check, so we don't rmtree the wrong thing below
21     if (($package eq "") || ($package =~ m#[/.\\]#)) {
22         die "Bad package name: $package";
23     }
24
25     if (-d "libraries/$package/_darcs") {
26         print "Ignoring libraries/$package as it looks like a darcs checkout\n"
27     }
28     elsif (-d "libraries/$package/.git") {
29         print "Ignoring libraries/$package as it looks like a git checkout\n"
30     }
31     else {
32         if (! -d "libraries/stamp") {
33             mkdir "libraries/stamp";
34         }
35         $stamp = "libraries/stamp/$package";
36         if ((! -d "libraries/$package") || (! -f "$stamp")
37          || ((-M "libraries/stamp/$package") > (-M $tarball))) {
38             print "Unpacking $package\n";
39             if (-d "libraries/$package") {
40                 &rmtree("libraries/$package")
41                     or die "Can't remove libraries/$package: $!";
42             }
43             mkdir "libraries/$package"
44                 or die "Can't create libraries/$package: $!";
45             system ("sh", "-c", "cd 'libraries/$package' && { cat ../../$tarball | gzip -d | tar xf - ; } && mv */* .") == 0
46                 or die "Failed to unpack $package";
47             open STAMP, "> $stamp"
48                 or die "Failed to open stamp file: $!";
49             close STAMP
50                 or die "Failed to close stamp file: $!";
51         }
52     }
53 }
54
55 for $package (glob "libraries/*/") {
56     $package =~ s/\/$//;
57     my $pkgs = "$package/ghc-packages";
58     if (-f $pkgs) {
59         open PKGS, "< $pkgs"
60             or die "Failed to open $pkgs: $!";
61         while (<PKGS>) {
62             chomp;
63             if (/.+/) {
64                 push @library_dirs, "$package/$_";
65             }
66         }
67     }
68     else {
69         push @library_dirs, $package;
70     }
71 }
72
73 for $package (@library_dirs) {
74     my $dir = &basename($package);
75     my @cabals = glob("$package/*.cabal");
76     if ($#cabals > 0) {
77         die "Too many .cabal file in $package\n";
78     }
79     if ($#cabals eq 0) {
80         my $cabal = $cabals[0];
81         my $pkg;
82         my $top;
83         if (-f $cabal) {
84             $pkg = $cabal;
85             $pkg =~ s#.*/##;
86             $pkg =~ s/\.cabal$//;
87             $top = $package;
88             $top =~ s#[^/]+#..#g;
89             $dir = $package;
90             $dir =~ s#^libraries/##g;
91
92             print "Creating $package/ghc.mk\n";
93             open GHCMK, "> $package/ghc.mk"
94                 or die "Opening $package/ghc.mk failed: $!";
95             print GHCMK "${package}_PACKAGE = ${pkg}\n";
96             print GHCMK "${package}_dist-install_GROUP = libraries\n";
97             print GHCMK "\$(eval \$(call build-package,${package},dist-install,\$(if \$(filter ${dir},\$(STAGE2_PACKAGES)),2,1)))\n";
98             close GHCMK
99                 or die "Closing $package/ghc.mk failed: $!";
100
101             print "Creating $package/GNUmakefile\n";
102             open GNUMAKEFILE, "> $package/GNUmakefile"
103                 or die "Opening $package/GNUmakefile failed: $!";
104             print GNUMAKEFILE "dir = ${package}\n";
105             print GNUMAKEFILE "TOP = ${top}\n";
106             print GNUMAKEFILE "include \$(TOP)/mk/sub-makefile.mk\n";
107             print GNUMAKEFILE "FAST_MAKE_OPTS += stage=0\n";
108             close GNUMAKEFILE
109                 or die "Closing $package/GNUmakefile failed: $!";
110         }
111     }
112 }
113